Search in sources :

Example 16 with MetaProperty

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

the class DataGridLoader method loadColumn.

protected Column loadColumn(DataGrid component, Element element, Datasource ds) {
    String id = element.attributeValue("id");
    String property = element.attributeValue("property");
    if (id == null) {
        if (property != null) {
            id = property;
        } else {
            throw new GuiDevelopmentException("A column must have whether id or property specified", context.getCurrentFrameId(), "DataGrid ID", component.getId());
        }
    }
    Column column;
    if (property != null) {
        MetaPropertyPath metaPropertyPath = AppBeans.get(MetadataTools.NAME, MetadataTools.class).resolveMetaPropertyPath(ds.getMetaClass(), property);
        column = component.addColumn(id, metaPropertyPath);
    } else {
        column = component.addColumn(id, null);
    }
    String expandRatio = element.attributeValue("expandRatio");
    if (StringUtils.isNotEmpty(expandRatio)) {
        column.setExpandRatio(Integer.parseInt(expandRatio));
    }
    String collapsed = element.attributeValue("collapsed");
    if (StringUtils.isNotEmpty(collapsed)) {
        column.setCollapsed(Boolean.parseBoolean(collapsed));
    }
    String collapsible = element.attributeValue("collapsible");
    if (StringUtils.isNotEmpty(collapsible)) {
        column.setCollapsible(Boolean.parseBoolean(collapsible));
    }
    String collapsingToggleCaption = element.attributeValue("collapsingToggleCaption");
    if (StringUtils.isNotEmpty(collapsingToggleCaption)) {
        collapsingToggleCaption = loadResourceString(collapsingToggleCaption);
        column.setCollapsingToggleCaption(collapsingToggleCaption);
    }
    String sortable = element.attributeValue("sortable");
    if (StringUtils.isNotEmpty(sortable)) {
        column.setSortable(Boolean.parseBoolean(sortable));
    }
    String resizable = element.attributeValue("resizable");
    if (StringUtils.isNotEmpty(resizable)) {
        column.setResizable(Boolean.parseBoolean(resizable));
    }
    String editable = element.attributeValue("editable");
    if (StringUtils.isNotEmpty(editable)) {
        column.setEditable(Boolean.parseBoolean(editable));
    }
    // Default caption set to columns when it is added to a DataGrid,
    // so we need to set caption as null to get caption from
    // metaProperty if 'caption' attribute is empty
    column.setCaption(null);
    String caption = loadCaption(element);
    if (caption == null) {
        String columnCaption;
        if (column.getPropertyPath() != null) {
            MetaProperty metaProperty = column.getPropertyPath().getMetaProperty();
            String propertyName = metaProperty.getName();
            if (DynamicAttributesUtils.isDynamicAttribute(metaProperty)) {
                CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaProperty);
                columnCaption = LocaleHelper.isLocalizedValueDefined(categoryAttribute.getLocaleNames()) ? categoryAttribute.getLocaleName() : StringUtils.capitalize(categoryAttribute.getName());
            } else {
                MetaClass propertyMetaClass = metadataTools.getPropertyEnclosingMetaClass(column.getPropertyPath());
                columnCaption = messageTools.getPropertyCaption(propertyMetaClass, propertyName);
            }
        } else {
            Class<?> declaringClass = ds.getMetaClass().getJavaClass();
            String className = declaringClass.getName();
            int i = className.lastIndexOf('.');
            if (i > -1) {
                className = className.substring(i + 1);
            }
            columnCaption = messages.getMessage(declaringClass, className + "." + id);
        }
        column.setCaption(columnCaption);
    } else {
        column.setCaption(caption);
    }
    column.setXmlDescriptor(element);
    Integer width = loadWidth(element, "width");
    if (width != null) {
        column.setWidth(width);
    }
    Integer minimumWidth = loadWidth(element, "minimumWidth");
    if (minimumWidth != null) {
        column.setMinimumWidth(minimumWidth);
    }
    Integer maximumWidth = loadWidth(element, "maximumWidth");
    if (maximumWidth != null) {
        column.setMaximumWidth(maximumWidth);
    }
    column.setFormatter(loadFormatter(element));
    return column;
}
Also used : MetadataTools(com.haulmont.cuba.core.global.MetadataTools) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) MetaClass(com.haulmont.chile.core.model.MetaClass) Column(com.haulmont.cuba.gui.components.DataGrid.Column) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 17 with MetaProperty

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

the class DsBuilder method buildDatasource.

public Datasource buildDatasource() {
    init();
    Datasource datasource;
    try {
        if (master == null && property == null) {
            if (dsClass == null) {
                datasource = createDatasource();
            } else {
                datasource = (Datasource) dsClass.newInstance();
            }
            datasource.setup(dsContext, dataSupplier, id, metaClass, view);
        } else {
            boolean isEmbedded = false;
            if (master != null) {
                MetaClass metaClass = master.getMetaClass();
                MetaProperty metaProperty = metaClass.getProperty(property);
                MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
                isEmbedded = metadataTools.isEmbedded(metaProperty);
            }
            if (dsClass == null) {
                datasource = isEmbedded ? createEmbeddedDatasource() : createPropertyDatasource();
            } else {
                datasource = (Datasource) dsClass.newInstance();
            }
            ((NestedDatasource) datasource).setup(id, master, property);
        }
    } catch (IllegalAccessException | InstantiationException e) {
        throw new RuntimeException(e);
    }
    datasource.setAllowCommit(allowCommit);
    registerDatasource(datasource);
    return datasource;
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 18 with MetaProperty

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

the class AbstractComparator method compareAsc.

protected int compareAsc(Object o1, Object o2) {
    int c;
    if (o1 instanceof String && o2 instanceof String) {
        c = ((String) o1).compareToIgnoreCase((String) o2);
    } else if (o1 instanceof Comparable && o2 instanceof Comparable) {
        c = ((Comparable) o1).compareTo(o2);
    } else if (o1 instanceof Instance && o2 instanceof Instance) {
        MetaClass metaClass = metadata.getClassNN(o1.getClass());
        Collection<MetaProperty> namePatternProperties = metadata.getTools().getNamePatternProperties(metaClass, true);
        if (namePatternProperties.isEmpty()) {
            c = ((Instance) o1).getInstanceName().compareToIgnoreCase(((Instance) o2).getInstanceName());
        } else {
            c = 0;
            for (MetaProperty property : namePatternProperties) {
                Object v1 = ((Instance) o1).getValue(property.getName());
                Object v2 = ((Instance) o2).getValue(property.getName());
                c = compareAsc(v1, v2);
                if (c != 0)
                    break;
            }
        }
    } else if (Objects.equals(o1, o2)) {
        c = 0;
    } else if (o1 == null && o2 != null) {
        c = nullsLast;
    } else {
        c = -nullsLast;
    }
    return c;
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) Instance(com.haulmont.chile.core.model.Instance) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 19 with MetaProperty

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

the class EntityCopyUtils method copyCompositionsBack.

public static void copyCompositionsBack(Entity source, Entity dest) {
    Preconditions.checkNotNullArgument(source, "source is null");
    Preconditions.checkNotNullArgument(dest, "dest is null");
    for (MetaProperty srcProperty : source.getMetaClass().getProperties()) {
        String name = srcProperty.getName();
        MetaProperty dstProperty = dest.getMetaClass().getProperty(name);
        if (dstProperty != null && !dstProperty.isReadOnly()) {
            try {
                Object value = source.getValue(name);
                if (value != null && srcProperty.getRange().getCardinality().isMany() && srcProperty.getType() == MetaProperty.Type.COMPOSITION) {
                    ((AbstractInstance) dest).setValue(name, source.getValue(name), false);
                } else {
                    dest.setValue(name, source.getValue(name));
                }
            } catch (RuntimeException e) {
                Throwable cause = ExceptionUtils.getRootCause(e);
                if (cause == null)
                    cause = e;
                // ignore exception on copy for not loaded fields
                if (!isNotLoadedAttributeException(cause))
                    throw e;
            }
        }
    }
}
Also used : AbstractInstance(com.haulmont.chile.core.model.impl.AbstractInstance) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 20 with MetaProperty

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

the class EntityCopyUtils method copyCompositions.

public static void copyCompositions(Entity source, Entity dest) {
    Preconditions.checkNotNullArgument(source, "source is null");
    Preconditions.checkNotNullArgument(dest, "dest is null");
    if (source instanceof BaseDbGeneratedIdEntity && dest instanceof BaseDbGeneratedIdEntity) {
        ((BaseDbGeneratedIdEntity) dest).setId(((BaseDbGeneratedIdEntity) source).getId());
    }
    for (MetaProperty srcProperty : source.getMetaClass().getProperties()) {
        String name = srcProperty.getName();
        MetaProperty dstProperty = dest.getMetaClass().getProperty(name);
        if (dstProperty != null && !dstProperty.isReadOnly()) {
            try {
                Object value = source.getValue(name);
                if (value != null && srcProperty.getRange().getCardinality().isMany() && srcProperty.getType() == MetaProperty.Type.COMPOSITION) {
                    // noinspection unchecked
                    Collection<Entity> srcCollection = (Collection) value;
                    // Copy first to a Set to remove duplicates that could be created on repeated editing newly
                    // added items
                    Collection<Entity> tmpCollection = new LinkedHashSet<>();
                    for (Entity item : srcCollection) {
                        Entity copy = copyCompositions(item);
                        tmpCollection.add(copy);
                    }
                    Collection<Entity> dstCollection;
                    if (value instanceof List)
                        dstCollection = new ArrayList<>(tmpCollection);
                    else
                        dstCollection = tmpCollection;
                    dest.setValue(name, dstCollection);
                } else {
                    dest.setValue(name, source.getValue(name));
                }
            } catch (RuntimeException e) {
                Throwable cause = ExceptionUtils.getRootCause(e);
                if (cause == null)
                    cause = e;
                // ignore exception on copy for not loaded fields
                if (!isNotLoadedAttributeException(cause))
                    throw e;
            }
        }
    }
    if (source instanceof BaseGenericIdEntity && dest instanceof BaseGenericIdEntity) {
        BaseGenericIdEntity destGenericEntity = (BaseGenericIdEntity) dest;
        BaseGenericIdEntity<?> sourceGenericEntity = (BaseGenericIdEntity<?>) source;
        BaseEntityInternalAccess.setDetached(destGenericEntity, BaseEntityInternalAccess.isDetached(sourceGenericEntity));
        BaseEntityInternalAccess.setNew(destGenericEntity, BaseEntityInternalAccess.isNew(sourceGenericEntity));
        destGenericEntity.setDynamicAttributes(sourceGenericEntity.getDynamicAttributes());
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BaseDbGeneratedIdEntity(com.haulmont.cuba.core.entity.BaseDbGeneratedIdEntity) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) ArrayList(java.util.ArrayList) BaseDbGeneratedIdEntity(com.haulmont.cuba.core.entity.BaseDbGeneratedIdEntity) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Aggregations

MetaProperty (com.haulmont.chile.core.model.MetaProperty)157 MetaClass (com.haulmont.chile.core.model.MetaClass)102 Entity (com.haulmont.cuba.core.entity.Entity)44 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)26 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)21 Range (com.haulmont.chile.core.model.Range)13 Nullable (javax.annotation.Nullable)11 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)10 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)9 java.util (java.util)9 Element (org.dom4j.Element)9 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)8 Collectors (java.util.stream.Collectors)6 Inject (javax.inject.Inject)6 Query (com.haulmont.cuba.core.Query)5 SoftDelete (com.haulmont.cuba.core.entity.SoftDelete)5 Collection (java.util.Collection)5 MessageTools (com.haulmont.cuba.core.global.MessageTools)4 PropertyDatasource (com.haulmont.cuba.gui.data.PropertyDatasource)4 Logger (org.slf4j.Logger)4