Search in sources :

Example 1 with MetadataObject

use of com.haulmont.chile.core.model.MetadataObject in project cuba by cuba-platform.

the class ComponentsHelper method handleFilteredAttributes.

/**
 * Set field's "required" flag to false if the value has been filtered by Row Level Security
 * This is necessary to allow user to submit form with filtered attribute even if attribute is required
 */
public static void handleFilteredAttributes(Field component, Datasource datasource, MetaPropertyPath mpp) {
    if (component.isRequired() && datasource.getState() == Datasource.State.VALID && datasource.getItem() != null && mpp.getMetaProperty().getRange().isClass()) {
        Entity targetItem = datasource.getItem();
        MetaProperty[] propertiesChain = mpp.getMetaProperties();
        if (propertiesChain.length > 1) {
            String basePropertyItem = Arrays.stream(propertiesChain).limit(propertiesChain.length - 1).map(MetadataObject::getName).collect(Collectors.joining("."));
            targetItem = datasource.getItem().getValueEx(basePropertyItem);
        }
        if (targetItem instanceof BaseGenericIdEntity) {
            String metaPropertyName = mpp.getMetaProperty().getName();
            Object value = targetItem.getValue(metaPropertyName);
            BaseGenericIdEntity baseGenericIdEntity = (BaseGenericIdEntity) targetItem;
            String[] filteredAttributes = getFilteredAttributes(baseGenericIdEntity);
            if (value == null && filteredAttributes != null && ArrayUtils.contains(filteredAttributes, metaPropertyName)) {
                component.setRequired(false);
            }
        }
    }
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) MetadataObject(com.haulmont.chile.core.model.MetadataObject) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Aggregations

MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 MetadataObject (com.haulmont.chile.core.model.MetadataObject)1 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)1 Entity (com.haulmont.cuba.core.entity.Entity)1