use of com.vaadin.flow.templatemodel.PropertyFilter in project flow by vaadin.
the class PropertyFilterTest method filterWithAdditionalLevel.
@Test
public void filterWithAdditionalLevel() {
PropertyFilter outerFilter = new PropertyFilter(name -> !"middle.inner.foo".equals(name));
PropertyFilter middleFilter = new PropertyFilter(outerFilter, "middle", name -> !"inner.bar".equals(name));
PropertyFilter innerFilter = new PropertyFilter(middleFilter, "inner", name -> !"baz".equals(name));
// Rejected by outer filter
Assert.assertFalse(innerFilter.test("foo"));
// Rejected by middle filter
Assert.assertFalse(innerFilter.test("bar"));
// Rejected by inner filter
Assert.assertFalse(innerFilter.test("baz"));
// Not rejected by any filter
Assert.assertTrue(innerFilter.test("foobar"));
}
use of com.vaadin.flow.templatemodel.PropertyFilter in project flow by vaadin.
the class BeanModelType method importProperties.
/**
* Imports properties from a bean into a model map based on the properties
* in this model type.
*
* @param model
* the model map to import values into
* @param bean
* the bean to get values from
* @param propertyFilter
* defines which properties from this model type to import
*/
public void importProperties(ModelMap model, Object bean, PropertyFilter propertyFilter) {
Class<? extends Object> beanClass = bean.getClass();
assert isBean(beanClass);
/*
* Collect all values and let getters throw before starting to populate
* the model.
*
* Can't use Collectors.toMap() since it disallows null values.
*/
Map<String, Object> values = new HashMap<>();
properties.keySet().stream().filter(propertyFilter).map(name -> ReflectTools.getGetter(beanClass, name)).filter(Optional::isPresent).map(Optional::get).forEach(getter -> {
String propertyName = ReflectTools.getPropertyName(getter);
Type beanPropertyType = ReflectTools.getPropertyType(getter);
ModelType modelPropertyType = getPropertyType(propertyName);
if (!modelPropertyType.accepts(beanPropertyType)) {
throw new IllegalArgumentException(propertyName + " is not of the expected type");
}
try {
Object value = getter.invoke(bean);
values.put(propertyName, value);
} catch (Exception e) {
throw new IllegalArgumentException("Cannot access bean property " + propertyName, e);
}
});
// Populate the model with the extracted values
values.forEach((name, value) -> {
ModelType type = getPropertyType(name);
model.setValue(name, type.applicationToModel(value, new PropertyFilter(propertyFilter, name)));
});
}
Aggregations