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;
}
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();
}
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;
}
Aggregations