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