Search in sources :

Example 6 with PropertyFilter

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"));
}
Also used : PropertyFilter(com.vaadin.flow.templatemodel.PropertyFilter) Test(org.junit.Test)

Example 7 with PropertyFilter

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)));
    });
}
Also used : BasicModelType(com.vaadin.flow.templatemodel.BasicModelType) TemplateModelUtil(com.vaadin.flow.templatemodel.TemplateModelUtil) StateNode(com.vaadin.flow.internal.StateNode) Predicate(java.util.function.Predicate) Json(elemental.json.Json) ReflectTools(com.vaadin.flow.internal.ReflectTools) HashMap(java.util.HashMap) Function(java.util.function.Function) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) TemplateElementStateProvider(com.vaadin.flow.dom.impl.TemplateElementStateProvider) Serializable(java.io.Serializable) InvalidTemplateModelException(com.vaadin.flow.templatemodel.InvalidTemplateModelException) PropertyFilter(com.vaadin.flow.templatemodel.PropertyFilter) JsonValue(elemental.json.JsonValue) ModelType(com.vaadin.flow.templatemodel.ModelType) Stream(java.util.stream.Stream) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Map(java.util.Map) Optional(java.util.Optional) JsonObject(elemental.json.JsonObject) Method(java.lang.reflect.Method) ComplexModelType(com.vaadin.flow.templatemodel.ComplexModelType) BasicModelType(com.vaadin.flow.templatemodel.BasicModelType) ModelType(com.vaadin.flow.templatemodel.ModelType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ComplexModelType(com.vaadin.flow.templatemodel.ComplexModelType) Optional(java.util.Optional) HashMap(java.util.HashMap) BasicModelType(com.vaadin.flow.templatemodel.BasicModelType) ModelType(com.vaadin.flow.templatemodel.ModelType) ComplexModelType(com.vaadin.flow.templatemodel.ComplexModelType) JsonObject(elemental.json.JsonObject) PropertyFilter(com.vaadin.flow.templatemodel.PropertyFilter) InvalidTemplateModelException(com.vaadin.flow.templatemodel.InvalidTemplateModelException)

Aggregations

PropertyFilter (com.vaadin.flow.templatemodel.PropertyFilter)7 Test (org.junit.Test)6 StateNode (com.vaadin.flow.internal.StateNode)5 ModelMap (com.vaadin.flow.internal.nodefeature.ModelMap)5 Bean (com.vaadin.flow.templatemodel.Bean)4 Arrays (java.util.Arrays)4 Date (java.util.Date)4 Collectors (java.util.stream.Collectors)4 Assert (org.junit.Assert)4 TemplateElementStateProvider (com.vaadin.flow.dom.impl.TemplateElementStateProvider)1 ReflectTools (com.vaadin.flow.internal.ReflectTools)1 BasicModelType (com.vaadin.flow.templatemodel.BasicModelType)1 ComplexModelType (com.vaadin.flow.templatemodel.ComplexModelType)1 InvalidTemplateModelException (com.vaadin.flow.templatemodel.InvalidTemplateModelException)1 ModelType (com.vaadin.flow.templatemodel.ModelType)1 TemplateModelUtil (com.vaadin.flow.templatemodel.TemplateModelUtil)1 Json (elemental.json.Json)1 JsonObject (elemental.json.JsonObject)1 JsonValue (elemental.json.JsonValue)1 Serializable (java.io.Serializable)1