Search in sources :

Example 71 with MetaProperty

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

the class EntityImportExport method importOneToManyCollectionAttribute.

protected void importOneToManyCollectionAttribute(Entity srcEntity, Entity dstEntity, SecurityState srcSecurityState, EntityImportViewProperty viewProperty, View regularView, CommitContext commitContext, Collection<ReferenceInfo> referenceInfoList) {
    Collection<Entity> collectionValue = srcEntity.getValue(viewProperty.getName());
    Collection<Entity> prevCollectionValue = dstEntity.getValue(viewProperty.getName());
    MetaProperty metaProperty = srcEntity.getMetaClass().getPropertyNN(viewProperty.getName());
    MetaProperty inverseMetaProperty = metaProperty.getInverse();
    Collection dstFilteredIds = getFilteredIds(dstEntity, metaProperty.getName());
    Collection srcFilteredIds = getFilteredIds(srcSecurityState, metaProperty.getName());
    Collection<Entity> newCollectionValue = createNewCollection(metaProperty);
    CollectionCompare.with().onCreate(e -> {
        if (!dstFilteredIds.contains(referenceToEntitySupport.getReferenceId(e))) {
            Entity result = importEntity(e, null, viewProperty.getView(), regularView, commitContext, referenceInfoList);
            if (inverseMetaProperty != null) {
                result.setValue(inverseMetaProperty.getName(), dstEntity);
            }
            newCollectionValue.add(result);
        }
    }).onUpdate((src, dst) -> {
        if (!dstFilteredIds.contains(referenceToEntitySupport.getReferenceId(src))) {
            Entity result = importEntity(src, dst, viewProperty.getView(), regularView, commitContext, referenceInfoList);
            if (inverseMetaProperty != null) {
                result.setValue(inverseMetaProperty.getName(), dstEntity);
            }
            newCollectionValue.add(result);
        }
    }).onDelete(e -> {
        Object refId = referenceToEntitySupport.getReferenceId(e);
        if (viewProperty.getCollectionImportPolicy() == CollectionImportPolicy.REMOVE_ABSENT_ITEMS) {
            if (!dstFilteredIds.contains(refId) && !srcFilteredIds.contains(refId)) {
                commitContext.addInstanceToRemove(e);
            }
        }
        if (srcFilteredIds.contains(refId)) {
            newCollectionValue.add(e);
        }
    }).compare(collectionValue, prevCollectionValue);
    dstEntity.setValue(metaProperty.getName(), newCollectionValue);
}
Also used : PersistenceSecurity(com.haulmont.cuba.core.PersistenceSecurity) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) java.util(java.util) Persistence(com.haulmont.cuba.core.Persistence) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) MetaClass(com.haulmont.chile.core.model.MetaClass) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) RestApiChecks(com.haulmont.cuba.core.global.validation.groups.RestApiChecks) Inject(javax.inject.Inject) EntitySerializationOption(com.haulmont.cuba.core.app.serialization.EntitySerializationOption) EntityValidationException(com.haulmont.cuba.core.global.validation.EntityValidationException) ByteArrayInputStream(java.io.ByteArrayInputStream) com.haulmont.cuba.core.entity(com.haulmont.cuba.core.entity) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) DataStore(com.haulmont.cuba.core.app.DataStore) ConstraintViolation(javax.validation.ConstraintViolation) Nullable(javax.annotation.Nullable) Default(javax.validation.groups.Default) StoreFactory(com.haulmont.cuba.core.app.StoreFactory) Range(com.haulmont.chile.core.model.Range) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Validator(javax.validation.Validator) IOException(java.io.IOException) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) String.format(java.lang.String.format) DynamicAttributesManagerAPI(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesManagerAPI) EntitySerializationAPI(com.haulmont.cuba.core.app.serialization.EntitySerializationAPI) IOUtils(org.apache.commons.io.IOUtils) Component(org.springframework.stereotype.Component) Stream(java.util.stream.Stream) CRC32(java.util.zip.CRC32) RdbmsStore(com.haulmont.cuba.core.app.RdbmsStore) CustomValidationException(com.haulmont.cuba.core.global.validation.CustomValidationException) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 72 with MetaProperty

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

the class EntityImportExport method buildViewFromImportView.

/**
 * Method builds a regular {@link View} from the {@link EntityImportView}. The regular view will include all
 * properties defined in the import view.
 */
protected View buildViewFromImportView(EntityImportView importView) {
    View regularView = new View(importView.getEntityClass());
    MetaClass metaClass = metadata.getClassNN(importView.getEntityClass());
    for (EntityImportViewProperty importViewProperty : importView.getProperties()) {
        EntityImportView importViewPropertyView = importViewProperty.getView();
        if (importViewPropertyView == null) {
            MetaProperty metaProperty = metaClass.getPropertyNN(importViewProperty.getName());
            if (metaProperty.getRange().isClass()) {
                MetaClass propertyMetaClass = metaProperty.getRange().asClass();
                regularView.addProperty(importViewProperty.getName(), viewRepository.getView(propertyMetaClass, View.MINIMAL));
            } else {
                regularView.addProperty(importViewProperty.getName());
            }
        } else {
            regularView.addProperty(importViewProperty.getName(), buildViewFromImportView(importViewPropertyView));
        }
    }
    return regularView;
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 73 with MetaProperty

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

the class EntityImportExport method importEmbeddedAttribute.

protected Entity importEmbeddedAttribute(Entity srcEntity, Entity dstEntity, boolean createOp, EntityImportViewProperty importViewProperty, View regularView, CommitContext commitContext, Collection<ReferenceInfo> referenceInfoList) {
    String propertyName = importViewProperty.getName();
    MetaProperty metaProperty = srcEntity.getMetaClass().getPropertyNN(propertyName);
    Entity srcEmbeddedEntity = srcEntity.getValue(propertyName);
    if (srcEmbeddedEntity == null) {
        return null;
    }
    Entity dstEmbeddedEntity = dstEntity.getValue(propertyName);
    MetaClass embeddedAttrMetaClass = metaProperty.getRange().asClass();
    if (dstEmbeddedEntity == null) {
        dstEmbeddedEntity = metadata.create(embeddedAttrMetaClass);
    }
    SecurityState dstSecurityState = null;
    SecurityState srcSecurityState = null;
    if (dstEntity instanceof BaseGenericIdEntity && !createOp) {
        String storeName = metadata.getTools().getStoreName(dstEntity.getMetaClass());
        DataStore dataStore = storeFactory.get(storeName);
        // row-level security works only for entities from RdbmsStore
        if (dataStore instanceof RdbmsStore) {
            if (useSecurityToken()) {
                persistenceSecurity.assertTokenForREST(srcEmbeddedEntity, regularView);
                persistenceSecurity.restoreSecurityState(srcEmbeddedEntity);
                srcSecurityState = BaseEntityInternalAccess.getSecurityState(srcEmbeddedEntity);
            }
            persistenceSecurity.restoreSecurityState(dstEmbeddedEntity);
            dstSecurityState = BaseEntityInternalAccess.getSecurityState(dstEmbeddedEntity);
        }
    }
    for (EntityImportViewProperty vp : importViewProperty.getView().getProperties()) {
        MetaProperty mp = embeddedAttrMetaClass.getPropertyNN(vp.getName());
        if (BaseEntityInternalAccess.isHiddenOrReadOnly(dstSecurityState, mp.getName())) {
            continue;
        }
        if (BaseEntityInternalAccess.isRequired(dstSecurityState, mp.getName()) && srcEmbeddedEntity.getValue(mp.getName()) == null) {
            throw new CustomValidationException(format("Attribute [%s] is required for entity %s", mp.getName(), srcEmbeddedEntity));
        }
        if ((mp.getRange().isDatatype() && !"version".equals(mp.getName())) || mp.getRange().isEnum()) {
            dstEmbeddedEntity.setValue(vp.getName(), srcEmbeddedEntity.getValue(vp.getName()));
        } else if (mp.getRange().isClass()) {
            View propertyRegularView = regularView.getProperty(propertyName) != null ? regularView.getProperty(propertyName).getView() : null;
            if (metaProperty.getRange().getCardinality() == Range.Cardinality.ONE_TO_MANY) {
                importOneToManyCollectionAttribute(srcEmbeddedEntity, dstEmbeddedEntity, srcSecurityState, vp, propertyRegularView, commitContext, referenceInfoList);
            } else if (metaProperty.getRange().getCardinality() == Range.Cardinality.MANY_TO_MANY) {
                importManyToManyCollectionAttribute(srcEmbeddedEntity, dstEmbeddedEntity, srcSecurityState, vp, propertyRegularView, commitContext, referenceInfoList);
            } else {
                importReference(srcEmbeddedEntity, dstEmbeddedEntity, vp, propertyRegularView, commitContext, referenceInfoList);
            }
        }
    }
    return dstEmbeddedEntity;
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) DataStore(com.haulmont.cuba.core.app.DataStore) RdbmsStore(com.haulmont.cuba.core.app.RdbmsStore) MetaProperty(com.haulmont.chile.core.model.MetaProperty) CustomValidationException(com.haulmont.cuba.core.global.validation.CustomValidationException)

Example 74 with MetaProperty

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

the class WebAbstractField method initRequired.

protected void initRequired(MetaPropertyPath metaPropertyPath) {
    MetaProperty metaProperty = metaPropertyPath.getMetaProperty();
    boolean newRequired = metaProperty.isMandatory();
    Object notNullUiComponent = metaProperty.getAnnotations().get(NotNull.class.getName() + "_notnull_ui_component");
    if (Boolean.TRUE.equals(notNullUiComponent)) {
        newRequired = true;
    }
    setRequired(newRequired);
    if (StringUtils.isEmpty(getRequiredMessage())) {
        MessageTools messageTools = AppBeans.get(MessageTools.NAME);
        setRequiredMessage(messageTools.getDefaultRequiredMessage(metaPropertyPath.getMetaClass(), metaPropertyPath.toString()));
    }
}
Also used : MessageTools(com.haulmont.cuba.core.global.MessageTools) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 75 with MetaProperty

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

the class WebAbstractTable method addColumn.

@Override
public void addColumn(Table.Column column) {
    checkNotNullArgument(column, "Column must be non null");
    Object columnId = column.getId();
    component.addContainerProperty(columnId, column.getType(), null);
    if (StringUtils.isNotBlank(column.getDescription())) {
        component.setColumnDescription(columnId, column.getDescription());
    }
    if (StringUtils.isNotBlank(column.getValueDescription())) {
        component.setAggregationDescription(columnId, column.getValueDescription());
    } else if (column.getAggregation() != null && column.getAggregation().getType() != AggregationInfo.Type.CUSTOM) {
        Messages messages = AppBeans.get(Messages.NAME);
        String aggregationTypeLabel;
        switch(column.getAggregation().getType()) {
            case AVG:
                aggregationTypeLabel = "aggreagtion.avg";
                break;
            case COUNT:
                aggregationTypeLabel = "aggreagtion.count";
                break;
            case SUM:
                aggregationTypeLabel = "aggreagtion.sum";
                break;
            case MIN:
                aggregationTypeLabel = "aggreagtion.min";
                break;
            case MAX:
                aggregationTypeLabel = "aggreagtion.max";
                break;
            default:
                throw new IllegalArgumentException(String.format("AggregationType %s is not supported", column.getAggregation().getType().toString()));
        }
        component.setAggregationDescription(columnId, messages.getMainMessage(aggregationTypeLabel));
    }
    if (!column.isSortable()) {
        component.setColumnSortable(columnId, column.isSortable());
    }
    columns.put(columnId, column);
    columnsOrder.add(column);
    if (column.getWidth() != null) {
        component.setColumnWidth(columnId, column.getWidth());
    }
    if (column.getAlignment() != null) {
        component.setColumnAlignment(columnId, WebComponentsHelper.convertColumnAlignment(column.getAlignment()));
    }
    final String caption = getColumnCaption(columnId, column);
    setColumnHeader(columnId, caption);
    column.setOwner(this);
    if (column.getFormatter() == null && columnId instanceof MetaPropertyPath) {
        MetaProperty metaProperty = ((MetaPropertyPath) columnId).getMetaProperty();
        if (Collection.class.isAssignableFrom(metaProperty.getJavaType())) {
            final Formatter collectionFormatter = new CollectionFormatter();
            column.setFormatter(collectionFormatter);
        }
    }
    if (columnId instanceof MetaPropertyPath) {
        PersistenceManagerService persistenceManagerService = AppBeans.get(PersistenceManagerClient.NAME);
        MetaPropertyPath propertyPath = (MetaPropertyPath) columnId;
        MetaProperty metaProperty = propertyPath.getMetaProperty();
        MetaClass propertyMetaClass = metadataTools.getPropertyEnclosingMetaClass(propertyPath);
        String storeName = metadataTools.getStoreName(propertyMetaClass);
        if (metadataTools.isLob(metaProperty) && !persistenceManagerService.supportsLobSortingAndFiltering(storeName)) {
            component.setColumnSortable(columnId, false);
        }
    }
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) CollectionFormatter(com.haulmont.cuba.gui.components.formatters.CollectionFormatter) Formatter(com.haulmont.cuba.gui.components.Formatter) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) PersistenceManagerService(com.haulmont.cuba.core.app.PersistenceManagerService) MetaProperty(com.haulmont.chile.core.model.MetaProperty) CollectionFormatter(com.haulmont.cuba.gui.components.formatters.CollectionFormatter)

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