Search in sources :

Example 1 with MergeOptions

use of com.haulmont.cuba.gui.model.MergeOptions in project cuba by cuba-platform.

the class DataContextImpl method mergeState.

protected void mergeState(Entity srcEntity, Entity dstEntity, Set<Entity> mergedSet, boolean isRoot, MergeOptions options) {
    EntityStates entityStates = getEntityStates();
    boolean srcNew = entityStates.isNew(srcEntity);
    boolean dstNew = entityStates.isNew(dstEntity);
    mergeSystemState(srcEntity, dstEntity, isRoot, options);
    MetaClass metaClass = getMetadata().getClassNN(srcEntity.getClass());
    MetaProperty primaryKeyProperty = getMetadataTools().getPrimaryKeyProperty(metaClass);
    for (MetaProperty property : metaClass.getProperties()) {
        String propertyName = property.getName();
        if (// local
        !property.getRange().isClass() && // not PK
        property != primaryKeyProperty && // loaded src
        (srcNew || entityStates.isLoaded(srcEntity, propertyName)) && (dstNew || entityStates.isLoaded(dstEntity, propertyName))) {
            // loaded dst
            Object value = srcEntity.getValue(propertyName);
            // ignore null values in non-root source entities
            if (!isRoot && !options.isFresh() && value == null) {
                continue;
            }
            setPropertyValue(dstEntity, property, value);
        }
    }
    for (MetaProperty property : metaClass.getProperties()) {
        String propertyName = property.getName();
        if (// refs and collections
        property.getRange().isClass() && // not PK
        property != primaryKeyProperty && // loaded src
        (srcNew || entityStates.isLoaded(srcEntity, propertyName)) && (dstNew || entityStates.isLoaded(dstEntity, propertyName))) {
            // loaded dst
            Object value = srcEntity.getValue(propertyName);
            // ignore null values in non-root source entities
            if (!isRoot && !options.isFresh() && value == null) {
                continue;
            }
            if (value == null) {
                setPropertyValue(dstEntity, property, null);
                continue;
            }
            if (value instanceof Collection) {
                if (value instanceof List) {
                    mergeList((List) value, dstEntity, property, isRoot, options, mergedSet);
                } else if (value instanceof Set) {
                    mergeSet((Set) value, dstEntity, property, isRoot, options, mergedSet);
                } else {
                    throw new UnsupportedOperationException("Unsupported collection type: " + value.getClass().getName());
                }
            } else {
                Entity srcRef = (Entity) value;
                if (!mergedSet.contains(srcRef)) {
                    Entity managedRef = internalMerge(srcRef, mergedSet, false, options);
                    setPropertyValue(dstEntity, property, managedRef, false);
                    if (getMetadataTools().isEmbedded(property)) {
                        EmbeddedPropertyChangeListener listener = new EmbeddedPropertyChangeListener(dstEntity);
                        managedRef.addPropertyChangeListener(listener);
                        embeddedPropertyListeners.computeIfAbsent(dstEntity, e -> new HashMap<>()).put(propertyName, listener);
                    }
                } else {
                    Entity managedRef = find(srcRef.getClass(), srcRef.getId());
                    if (managedRef != null) {
                        setPropertyValue(dstEntity, property, managedRef, false);
                    } else {
                        // should never happen
                        log.debug("Instance was merged but managed instance is null: {}", srcRef);
                    }
                }
            }
        }
    }
}
Also used : java.util(java.util) DataContext(com.haulmont.cuba.gui.model.DataContext) LoggerFactory(org.slf4j.LoggerFactory) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) MetaClass(com.haulmont.chile.core.model.MetaClass) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) Subscription(com.haulmont.bali.events.Subscription) Instance(com.haulmont.chile.core.model.Instance) AbstractInstance(com.haulmont.chile.core.model.impl.AbstractInstance) com.haulmont.cuba.core.entity(com.haulmont.cuba.core.entity) Preconditions.checkNotNullArgument(com.haulmont.bali.util.Preconditions.checkNotNullArgument) Nullable(javax.annotation.Nullable) FetchGroupUtils(com.haulmont.cuba.core.sys.persistence.FetchGroupUtils) Logger(org.slf4j.Logger) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Field(java.lang.reflect.Field) ApplicationContext(org.springframework.context.ApplicationContext) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) InvocationTargetException(java.lang.reflect.InvocationTargetException) MergeOptions(com.haulmont.cuba.gui.model.MergeOptions) Consumer(java.util.function.Consumer) FetchGroup(org.eclipse.persistence.queries.FetchGroup) FetchGroupTracker(org.eclipse.persistence.queries.FetchGroupTracker) EventHub(com.haulmont.bali.events.EventHub) EntityReferencesNormalizer(com.haulmont.cuba.core.sys.EntityReferencesNormalizer) AnnotatedElement(java.lang.reflect.AnnotatedElement) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Aggregations

Sets (com.google.common.collect.Sets)1 EventHub (com.haulmont.bali.events.EventHub)1 Subscription (com.haulmont.bali.events.Subscription)1 Preconditions.checkNotNullArgument (com.haulmont.bali.util.Preconditions.checkNotNullArgument)1 Instance (com.haulmont.chile.core.model.Instance)1 MetaClass (com.haulmont.chile.core.model.MetaClass)1 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 AbstractInstance (com.haulmont.chile.core.model.impl.AbstractInstance)1 com.haulmont.cuba.core.entity (com.haulmont.cuba.core.entity)1 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)1 EntityReferencesNormalizer (com.haulmont.cuba.core.sys.EntityReferencesNormalizer)1 FetchGroupUtils (com.haulmont.cuba.core.sys.persistence.FetchGroupUtils)1 DataContext (com.haulmont.cuba.gui.model.DataContext)1 MergeOptions (com.haulmont.cuba.gui.model.MergeOptions)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 java.util (java.util)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1