use of com.haulmont.cuba.core.entity.BaseDbGeneratedIdEntity 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());
}
}
Aggregations