Search in sources :

Example 56 with MetaPropertyPath

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

the class CollectionDsHelper method createProperties.

public static List<MetaPropertyPath> createProperties(View view, MetaClass metaClass) {
    List<MetaPropertyPath> properties = new ArrayList<>();
    MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
    if (view != null && metadataTools.isPersistent(metaClass)) {
        for (ViewProperty property : view.getProperties()) {
            final String name = property.getName();
            final MetaProperty metaProperty = metaClass.getProperty(name);
            if (metaProperty == null) {
                String message = String.format("Unable to find property %s for entity %s", name, metaClass.getName());
                throw new DevelopmentException(message);
            }
            if (!metadataTools.isPersistent(metaProperty)) {
                String message = String.format("Specified transient property %s in view for datasource with persistent entity %s", name, metaClass.getName());
                LoggerFactory.getLogger(CollectionDsHelper.class).warn(message);
                continue;
            }
            final Range range = metaProperty.getRange();
            if (range == null) {
                continue;
            }
            final Range.Cardinality cardinality = range.getCardinality();
            if (!cardinality.isMany()) {
                properties.add(new MetaPropertyPath(metaClass, metaProperty));
            }
        }
        // add all non-persistent properties
        for (MetaProperty metaProperty : metaClass.getProperties()) {
            if (metadataTools.isNotPersistent(metaProperty)) {
                properties.add(new MetaPropertyPath(metaClass, metaProperty));
            }
        }
    } else {
        if (view != null) {
            LoggerFactory.getLogger(CollectionDsHelper.class).warn("Specified view {} for datasource with not persistent entity {}", view.getName(), metaClass.getName());
        }
        for (MetaProperty metaProperty : metaClass.getProperties()) {
            final Range range = metaProperty.getRange();
            if (range == null)
                continue;
            final Range.Cardinality cardinality = range.getCardinality();
            if (!cardinality.isMany()) {
                properties.add(new MetaPropertyPath(metaClass, metaProperty));
            }
        }
    }
    return properties;
}
Also used : MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) ArrayList(java.util.ArrayList) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Range(com.haulmont.chile.core.model.Range)

Example 57 with MetaPropertyPath

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

the class GroupDelegate method groupItems.

protected GroupInfo<MetaPropertyPath> groupItems(int propertyIndex, GroupInfo parent, List<GroupInfo> children, T item, final LinkedMap groupValues) {
    final MetaPropertyPath property = (MetaPropertyPath) groupProperties[propertyIndex++];
    Object itemValue = getValueByProperty(item, property);
    groupValues.put(property, itemValue);
    GroupInfo<MetaPropertyPath> groupInfo = new GroupInfo<>(groupValues);
    itemGroups.put(item.getId(), groupInfo);
    if (!parents.containsKey(groupInfo)) {
        parents.put(groupInfo, parent);
    }
    if (!children.contains(groupInfo)) {
        children.add(groupInfo);
    }
    List<GroupInfo> groupChildren = this.children.computeIfAbsent(groupInfo, k -> new ArrayList<>());
    if (propertyIndex < groupProperties.length) {
        groupInfo = groupItems(propertyIndex, groupInfo, groupChildren, item, groupValues);
    }
    return groupInfo;
}
Also used : GroupInfo(com.haulmont.cuba.gui.data.GroupInfo) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath)

Example 58 with MetaPropertyPath

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

the class DynamicAttributeCustomFieldGenerator method generateField.

@Override
public Component generateField(Datasource datasource, String propertyId) {
    ComponentsFactory componentsFactory = AppBeans.get(ComponentsFactory.class);
    ListEditor listEditor = componentsFactory.createComponent(ListEditor.class);
    MetaPropertyPath metaPropertyPath = DynamicAttributesUtils.getMetaPropertyPath(datasource.getMetaClass(), propertyId);
    if (metaPropertyPath == null) {
        log.error("MetaPropertyPath for dynamic attribute {} not found", propertyId);
        return null;
    }
    CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaPropertyPath.getMetaProperty());
    if (categoryAttribute == null) {
        log.error("Dynamic attribute {} not found", propertyId);
        return null;
    }
    listEditor.setEntityJoinClause(categoryAttribute.getJoinClause());
    listEditor.setEntityWhereClause(categoryAttribute.getWhereClause());
    ListEditor.ItemType itemType = listEditorItemTypeFromDynamicAttrType(categoryAttribute.getDataType());
    listEditor.setItemType(itemType);
    Metadata metadata = AppBeans.get(Metadata.class);
    Scripting scripting = AppBeans.get(Scripting.class);
    if (!Strings.isNullOrEmpty(categoryAttribute.getEntityClass())) {
        Class<?> clazz = scripting.loadClass(categoryAttribute.getEntityClass());
        if (clazz == null) {
            log.error("Unable to find class of entity {} for dynamic attribute {}", categoryAttribute.getEntityClass(), categoryAttribute.getCode());
            return null;
        }
        MetaClass metaClass = metadata.getClassNN(clazz);
        listEditor.setEntityName(metaClass.getName());
        listEditor.setUseLookupField(BooleanUtils.isTrue(categoryAttribute.getLookup()));
    }
    // noinspection unchecked
    datasource.addStateChangeListener(e -> {
        if (e.getState() == Datasource.State.VALID) {
            Object value = datasource.getItem().getValue(propertyId);
            if (value != null && value instanceof Collection) {
                listEditor.setValue(value);
            }
        }
    });
    listEditor.addValueChangeListener(e -> {
        datasource.getItem().setValue(propertyId, e.getValue());
    });
    listEditor.setWidthFull();
    return listEditor;
}
Also used : ComponentsFactory(com.haulmont.cuba.gui.xml.layout.ComponentsFactory) ListEditor(com.haulmont.cuba.gui.components.ListEditor) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) Metadata(com.haulmont.cuba.core.global.Metadata) Collection(java.util.Collection) Scripting(com.haulmont.cuba.core.global.Scripting)

Example 59 with MetaPropertyPath

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

the class AbstractComponentGenerationStrategy method createComponentInternal.

protected Component createComponentInternal(ComponentGenerationContext context) {
    MetaClass metaClass = context.getMetaClass();
    MetaPropertyPath mpp = resolveMetaPropertyPath(metaClass, context.getProperty());
    Element xmlDescriptor = context.getXmlDescriptor();
    if (mpp != null) {
        Range mppRange = mpp.getRange();
        if (mppRange.isDatatype()) {
            Class type = mppRange.asDatatype().getJavaClass();
            MetaProperty metaProperty = mpp.getMetaProperty();
            if (DynamicAttributesUtils.isDynamicAttribute(metaProperty)) {
                CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaProperty);
                if (categoryAttribute != null && categoryAttribute.getDataType() == PropertyType.ENUMERATION) {
                    return createEnumField(context);
                }
            }
            if (xmlDescriptor != null && "true".equalsIgnoreCase(xmlDescriptor.attributeValue("link"))) {
                return createDatatypeLinkField(context);
            } else {
                boolean hasMaskAttribute = xmlDescriptor != null && xmlDescriptor.attribute("mask") != null;
                if (type.equals(String.class)) {
                    if (hasMaskAttribute) {
                        return createMaskedField(context);
                    } else {
                        return createStringField(context, mpp);
                    }
                } else if (type.equals(UUID.class)) {
                    return createUuidField(context);
                } else if (type.equals(Boolean.class)) {
                    return createBooleanField(context);
                } else if (type.equals(java.sql.Date.class) || type.equals(Date.class)) {
                    return createDateField(context);
                } else if (type.equals(Time.class)) {
                    return createTimeField(context);
                } else if (Number.class.isAssignableFrom(type)) {
                    if (hasMaskAttribute) {
                        return createMaskedField(context);
                    } else {
                        Field currencyField = createCurrencyField(context, mpp);
                        if (currencyField != null) {
                            return currencyField;
                        }
                        return createNumberField(context);
                    }
                }
            }
        } else if (mppRange.isClass()) {
            MetaProperty metaProperty = mpp.getMetaProperty();
            Class<?> javaType = metaProperty.getJavaType();
            if (FileDescriptor.class.isAssignableFrom(javaType)) {
                return createFileUploadField(context);
            }
            if (!Collection.class.isAssignableFrom(javaType)) {
                return createEntityField(context, mpp);
            }
        } else if (mppRange.isEnum()) {
            return createEnumField(context);
        }
    }
    return null;
}
Also used : Element(org.dom4j.Element) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) Range(com.haulmont.chile.core.model.Range) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaClass(com.haulmont.chile.core.model.MetaClass) DynamicAttributesMetaProperty(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesMetaProperty) MetaProperty(com.haulmont.chile.core.model.MetaProperty) UUID(java.util.UUID)

Example 60 with MetaPropertyPath

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

the class EntityDiffManager method getPropertyDiffs.

/**
 * Get diffs for entity properties
 *
 * @param diffView     View
 * @param firstEntity  First entity
 * @param secondEntity Second entity
 * @param diffBranch   Diff branch
 * @return Diff list
 */
protected List<EntityPropertyDiff> getPropertyDiffs(View diffView, Entity firstEntity, Entity secondEntity, Stack<Object> diffBranch) {
    List<EntityPropertyDiff> propertyDiffs = new LinkedList<>();
    MetaClass viewMetaClass = metadata.getSession().getClass(diffView.getEntityClass());
    MetaClass metaClass = extendedEntities.getEffectiveMetaClass(viewMetaClass);
    Collection<MetaPropertyPath> metaProperties = metadataTools.getViewPropertyPaths(diffView, metaClass);
    for (MetaPropertyPath metaPropertyPath : metaProperties) {
        MetaProperty metaProperty = metaPropertyPath.getMetaProperty();
        if (!metadataTools.isNotPersistent(metaProperty) && !metadataTools.isSystem(metaProperty)) {
            ViewProperty viewProperty = diffView.getProperty(metaProperty.getName());
            Object firstValue = firstEntity != null ? getPropertyValue(firstEntity, metaPropertyPath) : null;
            Object secondValue = secondEntity != null ? getPropertyValue(secondEntity, metaPropertyPath) : null;
            EntityPropertyDiff diff = getPropertyDifference(firstValue, secondValue, metaProperty, viewProperty, diffBranch);
            if (diff != null)
                propertyDiffs.add(diff);
        }
    }
    Collection<CategoryAttribute> categoryAttributes = dynamicAttributesManagerAPI.getAttributesForMetaClass(metaClass);
    if (categoryAttributes != null) {
        for (CategoryAttribute categoryAttribute : categoryAttributes) {
            MetaPropertyPath metaPropertyPath = DynamicAttributesUtils.getMetaPropertyPath(metaClass, categoryAttribute);
            MetaProperty metaProperty = metaPropertyPath.getMetaProperty();
            Object firstValue = firstEntity != null ? getPropertyValue(firstEntity, metaPropertyPath) : null;
            Object secondValue = secondEntity != null ? getPropertyValue(secondEntity, metaPropertyPath) : null;
            EntityPropertyDiff diff = getDynamicAttributeDifference(firstValue, secondValue, metaProperty, categoryAttribute);
            if (diff != null)
                propertyDiffs.add(diff);
        }
    }
    Comparator<EntityPropertyDiff> comparator = Comparator.comparing(EntityPropertyDiff::getName);
    Collections.sort(propertyDiffs, comparator);
    return propertyDiffs;
}
Also used : CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

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