Search in sources :

Example 1 with RdbmsStore

use of com.haulmont.cuba.core.app.RdbmsStore 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 2 with RdbmsStore

use of com.haulmont.cuba.core.app.RdbmsStore in project cuba by cuba-platform.

the class EntityImportExport method getFilteredIds.

protected Collection getFilteredIds(Entity entity, String propertyName) {
    if (entity instanceof BaseGenericIdEntity) {
        String storeName = metadata.getTools().getStoreName(entity.getMetaClass());
        DataStore dataStore = storeFactory.get(storeName);
        if (dataStore instanceof RdbmsStore) {
            persistenceSecurity.restoreSecurityState(entity);
            return Optional.ofNullable(BaseEntityInternalAccess.getFilteredData(entity)).map(v -> v.get(propertyName)).orElse(Collections.emptyList());
        }
    }
    return Collections.emptyList();
}
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) DataStore(com.haulmont.cuba.core.app.DataStore) RdbmsStore(com.haulmont.cuba.core.app.RdbmsStore)

Example 3 with RdbmsStore

use of com.haulmont.cuba.core.app.RdbmsStore in project cuba by cuba-platform.

the class EntityImportExport method importEntity.

/**
 * Method imports the entity.
 *
 * @param srcEntity         entity that came to the {@code EntityImportExport} bean
 * @param dstEntity         reloaded srcEntity or null if entity doesn't exist in the database
 * @param importView        importView used for importing the entity
 * @param regularView       view that was used for loading dstEntity
 * @param commitContext     entities that must be commited or deleted will be set to the commitContext
 * @param referenceInfoList list of referenceInfos for further processing
 * @return dstEntity that has fields values from the srcEntity
 */
protected Entity importEntity(Entity srcEntity, @Nullable Entity dstEntity, EntityImportView importView, View regularView, CommitContext commitContext, Collection<ReferenceInfo> referenceInfoList) {
    MetaClass metaClass = srcEntity.getMetaClass();
    boolean createOp = false;
    if (dstEntity == null) {
        dstEntity = metadata.create(metaClass);
        dstEntity.setValue("id", srcEntity.getId());
        createOp = true;
    }
    // we must specify a view here because otherwise we may get UnfetchedAttributeException during merge
    commitContext.addInstanceToCommit(dstEntity, regularView);
    SecurityState dstSecurityState = null;
    SecurityState srcSecurityState = null;
    if (dstEntity instanceof BaseGenericIdEntity && !createOp) {
        String storeName = metadata.getTools().getStoreName(dstEntity.getMetaClass());
        DataStore dataStore = storeFactory.get(storeName);
        if (dataStore instanceof RdbmsStore) {
            if (useSecurityToken()) {
                persistenceSecurity.assertTokenForREST(srcEntity, regularView);
                persistenceSecurity.restoreSecurityState(srcEntity);
                srcSecurityState = BaseEntityInternalAccess.getSecurityState(srcEntity);
            }
            persistenceSecurity.restoreSecurityState(dstEntity);
            dstSecurityState = BaseEntityInternalAccess.getSecurityState(dstEntity);
        }
    }
    for (EntityImportViewProperty importViewProperty : importView.getProperties()) {
        String propertyName = importViewProperty.getName();
        MetaProperty metaProperty = metaClass.getPropertyNN(propertyName);
        if (BaseEntityInternalAccess.isHiddenOrReadOnly(dstSecurityState, propertyName)) {
            continue;
        }
        if (BaseEntityInternalAccess.isRequired(dstSecurityState, propertyName) && srcEntity.getValue(propertyName) == null) {
            throw new CustomValidationException(format("Attribute [%s] is required for entity %s", propertyName, srcEntity));
        }
        if ((metaProperty.getRange().isDatatype() && !"version".equals(metaProperty.getName())) || metaProperty.getRange().isEnum()) {
            dstEntity.setValue(propertyName, srcEntity.getValue(propertyName));
        } else if (metaProperty.getRange().isClass()) {
            View regularPropertyView = regularView.getProperty(propertyName) != null ? regularView.getProperty(propertyName).getView() : null;
            if (metadata.getTools().isEmbedded(metaProperty)) {
                if (importViewProperty.getView() != null) {
                    Entity embeddedEntity = importEmbeddedAttribute(srcEntity, dstEntity, createOp, importViewProperty, regularPropertyView, commitContext, referenceInfoList);
                    dstEntity.setValue(propertyName, embeddedEntity);
                }
            } else {
                switch(metaProperty.getRange().getCardinality()) {
                    case MANY_TO_MANY:
                        importManyToManyCollectionAttribute(srcEntity, dstEntity, srcSecurityState, importViewProperty, regularPropertyView, commitContext, referenceInfoList);
                        break;
                    case ONE_TO_MANY:
                        importOneToManyCollectionAttribute(srcEntity, dstEntity, srcSecurityState, importViewProperty, regularPropertyView, commitContext, referenceInfoList);
                        break;
                    default:
                        importReference(srcEntity, dstEntity, importViewProperty, regularPropertyView, commitContext, referenceInfoList);
                }
            }
        }
    }
    if (entityHasDynamicAttributes(srcEntity)) {
        ((BaseGenericIdEntity) dstEntity).setDynamicAttributes(((BaseGenericIdEntity) srcEntity).getDynamicAttributes());
    }
    return dstEntity;
}
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)

Aggregations

MetaClass (com.haulmont.chile.core.model.MetaClass)3 MetaProperty (com.haulmont.chile.core.model.MetaProperty)3 DataStore (com.haulmont.cuba.core.app.DataStore)3 RdbmsStore (com.haulmont.cuba.core.app.RdbmsStore)3 CustomValidationException (com.haulmont.cuba.core.global.validation.CustomValidationException)3 Range (com.haulmont.chile.core.model.Range)1 Persistence (com.haulmont.cuba.core.Persistence)1 PersistenceSecurity (com.haulmont.cuba.core.PersistenceSecurity)1 StoreFactory (com.haulmont.cuba.core.app.StoreFactory)1 DynamicAttributesManagerAPI (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesManagerAPI)1 EntitySerializationAPI (com.haulmont.cuba.core.app.serialization.EntitySerializationAPI)1 EntitySerializationOption (com.haulmont.cuba.core.app.serialization.EntitySerializationOption)1 com.haulmont.cuba.core.entity (com.haulmont.cuba.core.entity)1 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)1 EntityValidationException (com.haulmont.cuba.core.global.validation.EntityValidationException)1 RestApiChecks (com.haulmont.cuba.core.global.validation.groups.RestApiChecks)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 String.format (java.lang.String.format)1