Search in sources :

Example 1 with BaseDbGeneratedIdEntity

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());
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BaseDbGeneratedIdEntity(com.haulmont.cuba.core.entity.BaseDbGeneratedIdEntity) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) ArrayList(java.util.ArrayList) BaseDbGeneratedIdEntity(com.haulmont.cuba.core.entity.BaseDbGeneratedIdEntity) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Aggregations

MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 BaseDbGeneratedIdEntity (com.haulmont.cuba.core.entity.BaseDbGeneratedIdEntity)1 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)1 Entity (com.haulmont.cuba.core.entity.Entity)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1