Search in sources :

Example 1 with CustomValidationException

use of com.haulmont.cuba.core.global.validation.CustomValidationException 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 CustomValidationException

use of com.haulmont.cuba.core.global.validation.CustomValidationException 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)2 MetaProperty (com.haulmont.chile.core.model.MetaProperty)2 DataStore (com.haulmont.cuba.core.app.DataStore)2 RdbmsStore (com.haulmont.cuba.core.app.RdbmsStore)2 CustomValidationException (com.haulmont.cuba.core.global.validation.CustomValidationException)2