Search in sources :

Example 16 with MetaPropertyPath

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

the class FieldGroupLoader method getDefaultCaption.

protected String getDefaultCaption(FieldGroup.FieldConfig fieldConfig, Datasource fieldDatasource) {
    String caption = fieldConfig.getCaption();
    if (caption == null) {
        String propertyId = fieldConfig.getProperty();
        MetaPropertyPath propertyPath = fieldDatasource != null ? fieldDatasource.getMetaClass().getPropertyPath(propertyId) : null;
        if (propertyPath != null) {
            MetaClass propertyMetaClass = metadataTools.getPropertyEnclosingMetaClass(propertyPath);
            String propertyName = propertyPath.getMetaProperty().getName();
            caption = messageTools.getPropertyCaption(propertyMetaClass, propertyName);
        }
    }
    return caption;
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath)

Example 17 with MetaPropertyPath

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

the class FieldGroupLoader method loadEditable.

protected void loadEditable(FieldGroup resultComponent, FieldGroup.FieldConfig field) {
    Element element = field.getXmlDescriptor();
    if (element != null) {
        String editable = element.attributeValue("editable");
        if (StringUtils.isNotEmpty(editable)) {
            field.setEditable(Boolean.parseBoolean(editable));
        }
    }
    if (!field.isCustom() && BooleanUtils.isNotFalse(field.isEditable())) {
        MetaClass metaClass = getMetaClass(resultComponent, field);
        MetaPropertyPath propertyPath = metadataTools.resolveMetaPropertyPath(metaClass, field.getProperty());
        checkNotNullArgument(propertyPath, "Could not resolve property path '%s' in '%s'", field.getId(), metaClass);
        if (!security.isEntityAttrUpdatePermitted(metaClass, propertyPath.toString())) {
            field.setEditable(false);
        }
    }
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) Element(org.dom4j.Element) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath)

Example 18 with MetaPropertyPath

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

the class FieldGroupLoader method loadRequired.

protected void loadRequired(FieldGroup resultComponent, FieldGroup.FieldConfig field) {
    if (field.isCustom()) {
        Element element = field.getXmlDescriptor();
        if (element == null) {
            return;
        }
        String required = element.attributeValue("required");
        if (StringUtils.isNotEmpty(required)) {
            field.setRequired(Boolean.parseBoolean(required));
        }
        String requiredMessage = element.attributeValue("requiredMessage");
        if (StringUtils.isNotEmpty(requiredMessage)) {
            field.setRequiredMessage(loadResourceString(requiredMessage));
        }
    } else {
        MetaClass metaClass = getMetaClass(resultComponent, field);
        Element element = field.getXmlDescriptor();
        String required = element.attributeValue("required");
        if (StringUtils.isNotEmpty(required)) {
            field.setRequired(Boolean.parseBoolean(required));
        }
        String requiredMsg = element.attributeValue("requiredMessage");
        if (StringUtils.isEmpty(requiredMsg) && metaClass != null) {
            MetaPropertyPath propertyPath = metaClass.getPropertyPath(field.getProperty());
            checkNotNullArgument(propertyPath, "Could not resolve property path '%s' in '%s'", field.getProperty(), metaClass);
            requiredMsg = messageTools.getDefaultRequiredMessage(metaClass, propertyPath.toString());
        }
        field.setRequiredMessage(loadResourceString(requiredMsg));
    }
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) Element(org.dom4j.Element) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath)

Example 19 with MetaPropertyPath

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

the class FieldGroupLoader method loadField.

protected FieldGroup.FieldConfig loadField(Element element, Datasource ds, String columnWidth) {
    String id = element.attributeValue("id");
    String property = element.attributeValue("property");
    if (Strings.isNullOrEmpty(id) && Strings.isNullOrEmpty(property)) {
        throw new GuiDevelopmentException(String.format("id/property is not defined for field of FieldGroup '%s'. " + "Set id or property attribute.", resultComponent.getId()), context.getFullFrameId());
    }
    if (Strings.isNullOrEmpty(property)) {
        property = id;
    } else if (Strings.isNullOrEmpty(id)) {
        id = property;
    }
    Datasource targetDs = ds;
    Datasource datasource = loadDatasource(element);
    if (datasource != null) {
        targetDs = datasource;
    }
    CollectionDatasource optionsDs = null;
    String optDsName = element.attributeValue("optionsDatasource");
    if (StringUtils.isNotBlank(optDsName)) {
        DsContext dsContext = getContext().getFrame().getDsContext();
        optionsDs = findDatasourceRecursively(dsContext, optDsName);
        if (optionsDs == null) {
            throw new GuiDevelopmentException(String.format("Options datasource %s not found for field %s", optDsName, id), context.getFullFrameId());
        }
    }
    boolean customField = false;
    String custom = element.attributeValue("custom");
    if (StringUtils.isNotEmpty(custom)) {
        customField = Boolean.parseBoolean(custom);
    }
    if (StringUtils.isNotEmpty(element.attributeValue("generator"))) {
        customField = true;
    }
    List<Element> elements = Dom4j.elements(element);
    List<Element> customElements = elements.stream().filter(e -> !("formatter".equals(e.getName()) || "validator".equals(e.getName()))).collect(Collectors.toList());
    if (!customElements.isEmpty()) {
        if (customElements.size() > 1) {
            throw new GuiDevelopmentException(String.format("FieldGroup field %s element cannot contains two or more custom field definitions", id), context.getCurrentFrameId());
        }
        if (customField) {
            throw new GuiDevelopmentException(String.format("FieldGroup field %s cannot use both custom/generator attribute and inline component definition", id), context.getCurrentFrameId());
        }
        customField = true;
    }
    if (!customField && targetDs == null) {
        throw new GuiDevelopmentException(String.format("Datasource is not defined for FieldGroup field '%s'. " + "Only custom fields can have no datasource.", property), context.getFullFrameId());
    }
    FieldGroup.FieldConfig field = resultComponent.createField(id);
    if (property != null) {
        field.setProperty(property);
    }
    if (datasource != null) {
        field.setDatasource(datasource);
    }
    if (optionsDs != null) {
        field.setOptionsDatasource(optionsDs);
    }
    String stylename = element.attributeValue("stylename");
    if (StringUtils.isNotEmpty(stylename)) {
        field.setStyleName(stylename);
    }
    MetaPropertyPath metaPropertyPath = null;
    if (targetDs != null && property != null) {
        MetaClass metaClass = targetDs.getMetaClass();
        metaPropertyPath = metadataTools.resolveMetaPropertyPath(targetDs.getMetaClass(), property);
        if (metaPropertyPath == null) {
            if (!customField) {
                throw new GuiDevelopmentException(String.format("Property '%s' is not found in entity '%s'", property, metaClass.getName()), context.getFullFrameId());
            }
        }
    }
    String propertyName = metaPropertyPath != null ? metaPropertyPath.getMetaProperty().getName() : null;
    if (metaPropertyPath != null && DynamicAttributesUtils.isDynamicAttribute(metaPropertyPath.getMetaProperty())) {
        CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaPropertyPath.getMetaProperty());
        field.setCaption(categoryAttribute != null ? categoryAttribute.getLocaleName() : propertyName);
    } else {
        loadCaption(field, element);
        if (field.getCaption() == null) {
            field.setCaption(getDefaultCaption(field, targetDs));
        }
    }
    loadDescription(field, element);
    loadContextHelp(field, element);
    field.setXmlDescriptor(element);
    com.haulmont.cuba.gui.components.Formatter formatter = loadFormatter(element);
    if (formatter != null) {
        field.setFormatter(formatter);
    }
    String defaultWidth = element.attributeValue("width");
    if (StringUtils.isEmpty(defaultWidth)) {
        defaultWidth = columnWidth;
    }
    loadWidth(field, defaultWidth);
    if (customField) {
        field.setCustom(true);
    }
    String required = element.attributeValue("required");
    if (StringUtils.isNotEmpty(required)) {
        field.setRequired(Boolean.parseBoolean(required));
    }
    String requiredMsg = element.attributeValue("requiredMessage");
    if (requiredMsg != null) {
        requiredMsg = loadResourceString(requiredMsg);
        field.setRequiredMessage(requiredMsg);
    }
    String tabIndex = element.attributeValue("tabIndex");
    if (StringUtils.isNotEmpty(tabIndex)) {
        field.setTabIndex(Integer.parseInt(tabIndex));
    }
    loadInputPrompt(field, element);
    if (customElements.size() == 1) {
        // load nested component defined as inline
        Element customFieldElement = customElements.get(0);
        LayoutLoader loader = new LayoutLoader(context, factory, layoutLoaderConfig);
        loader.setLocale(getLocale());
        loader.setMessagesPack(getMessagesPack());
        ComponentLoader childComponentLoader = loader.createComponent(customFieldElement);
        childComponentLoader.loadComponent();
        Component customComponent = childComponentLoader.getResultComponent();
        String inlineAttachMode = element.attributeValue("inlineAttachMode");
        if (StringUtils.isNotEmpty(inlineAttachMode)) {
            field.setComponent(customComponent, FieldGroup.FieldAttachMode.valueOf(inlineAttachMode));
        } else {
            field.setComponent(customComponent);
        }
    }
    return field;
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Iterables(com.google.common.collect.Iterables) StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) Datasource(com.haulmont.cuba.gui.data.Datasource) DynamicAttributesGuiTools(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools) Dom4j(com.haulmont.bali.util.Dom4j) DynamicAttributeCustomFieldGenerator(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributeCustomFieldGenerator) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) DynamicAttributesUtils(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesUtils) AppBeans(com.haulmont.cuba.core.global.AppBeans) MetaClass(com.haulmont.chile.core.model.MetaClass) BooleanUtils(org.apache.commons.lang.BooleanUtils) Strings(com.google.common.base.Strings) MetadataTools(com.haulmont.cuba.core.global.MetadataTools) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) Preconditions.checkNotNullArgument(com.haulmont.bali.util.Preconditions.checkNotNullArgument) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) DsContext(com.haulmont.cuba.gui.data.DsContext) Nullable(javax.annotation.Nullable) LayoutLoader(com.haulmont.cuba.gui.xml.layout.LayoutLoader) MetaProperty(com.haulmont.chile.core.model.MetaProperty) DeclarativeFieldGenerator(com.haulmont.cuba.gui.xml.DeclarativeFieldGenerator) CustomFieldGenerator(com.haulmont.cuba.gui.components.FieldGroup.CustomFieldGenerator) Collectors(java.util.stream.Collectors) ComponentLoader(com.haulmont.cuba.gui.xml.layout.ComponentLoader) EntityOp(com.haulmont.cuba.security.entity.EntityOp) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException) ComponentsHelper(com.haulmont.cuba.gui.ComponentsHelper) FieldCaptionAlignment(com.haulmont.cuba.gui.components.FieldGroup.FieldCaptionAlignment) Element(org.dom4j.Element) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) MessageTools(com.haulmont.cuba.core.global.MessageTools) LayoutLoader(com.haulmont.cuba.gui.xml.layout.LayoutLoader) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) DsContext(com.haulmont.cuba.gui.data.DsContext) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Element(org.dom4j.Element) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) ComponentLoader(com.haulmont.cuba.gui.xml.layout.ComponentLoader) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) MetaClass(com.haulmont.chile.core.model.MetaClass) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException)

Example 20 with MetaPropertyPath

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

the class AggregatableDelegate method doAggregation.

protected Map<AggregationInfo, String> doAggregation(Collection<K> itemIds, AggregationInfo[] aggregationInfos) {
    final Map<AggregationInfo, String> aggregationResults = new HashMap<>();
    for (AggregationInfo aggregationInfo : aggregationInfos) {
        final Object value = doPropertyAggregation(aggregationInfo, itemIds);
        String formattedValue;
        if (aggregationInfo.getFormatter() != null) {
            // noinspection unchecked
            formattedValue = aggregationInfo.getFormatter().format(value);
        } else {
            MetaPropertyPath propertyPath = aggregationInfo.getPropertyPath();
            final Range range = propertyPath.getRange();
            if (range.isDatatype()) {
                if (aggregationInfo.getType() != AggregationInfo.Type.COUNT) {
                    Class resultClass;
                    if (aggregationInfo.getStrategy() == null) {
                        Class rangeJavaClass = propertyPath.getRangeJavaClass();
                        Aggregation aggregation = Aggregations.get(rangeJavaClass);
                        resultClass = aggregation.getResultClass();
                    } else {
                        resultClass = aggregationInfo.getStrategy().getResultClass();
                    }
                    UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.NAME);
                    Locale locale = userSessionSource.getLocale();
                    formattedValue = Datatypes.getNN(resultClass).format(value, locale);
                } else {
                    formattedValue = value.toString();
                }
            } else {
                if (aggregationInfo.getStrategy() != null) {
                    Class resultClass = aggregationInfo.getStrategy().getResultClass();
                    UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.NAME);
                    Locale locale = userSessionSource.getLocale();
                    formattedValue = Datatypes.getNN(resultClass).format(value, locale);
                } else {
                    formattedValue = value.toString();
                }
            }
        }
        aggregationResults.put(aggregationInfo, formattedValue);
    }
    return aggregationResults;
}
Also used : Aggregation(com.haulmont.cuba.gui.data.aggregation.Aggregation) UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) AggregationInfo(com.haulmont.cuba.gui.components.AggregationInfo) Range(com.haulmont.chile.core.model.Range)

Aggregations

MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)84 MetaClass (com.haulmont.chile.core.model.MetaClass)34 MetaProperty (com.haulmont.chile.core.model.MetaProperty)27 Element (org.dom4j.Element)16 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)11 Entity (com.haulmont.cuba.core.entity.Entity)9 MetadataTools (com.haulmont.cuba.core.global.MetadataTools)9 Datasource (com.haulmont.cuba.gui.data.Datasource)6 Table (com.haulmont.cuba.gui.components.Table)5 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)5 MessageTools (com.haulmont.cuba.core.global.MessageTools)4 Instance (com.haulmont.chile.core.model.Instance)3 Op (com.haulmont.cuba.core.global.filter.Op)3 FocusableTable (com.haulmont.cuba.desktop.sys.vcl.FocusableTable)3 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)3 Formatter (com.haulmont.cuba.gui.components.Formatter)3 Window (com.haulmont.cuba.gui.components.Window)3 CollectionFormatter (com.haulmont.cuba.gui.components.formatters.CollectionFormatter)3 GroupInfo (com.haulmont.cuba.gui.data.GroupInfo)3 java.util (java.util)3