Search in sources :

Example 1 with ChangeRecord

use of org.eclipse.persistence.sessions.changesets.ChangeRecord in project cuba by cuba-platform.

the class EntityAttributeChanges method addChanges.

public void addChanges(ObjectChangeSet changeSet) {
    if (changeSet == null)
        return;
    for (ChangeRecord changeRecord : changeSet.getChanges()) {
        changes.add(new Change(changeRecord.getAttribute(), changeRecord.getOldValue()));
        if (changeRecord instanceof AggregateChangeRecord) {
            embeddedChanges.computeIfAbsent(changeRecord.getAttribute(), s -> {
                EntityAttributeChanges embeddedChanges = new EntityAttributeChanges();
                embeddedChanges.addChanges(((AggregateChangeRecord) changeRecord).getChangedObject());
                return embeddedChanges;
            });
        }
    }
}
Also used : AggregateChangeRecord(org.eclipse.persistence.sessions.changesets.AggregateChangeRecord) AggregateChangeRecord(org.eclipse.persistence.sessions.changesets.AggregateChangeRecord) ChangeRecord(org.eclipse.persistence.sessions.changesets.ChangeRecord)

Example 2 with ChangeRecord

use of org.eclipse.persistence.sessions.changesets.ChangeRecord in project cuba by cuba-platform.

the class EntityChangedEventManager method addDynamicAttributeChanges.

@SuppressWarnings("unchecked")
private void addDynamicAttributeChanges(@Nullable Entity entity, Set<AttributeChanges.Change> changes, boolean deleted) {
    if (entity instanceof BaseGenericIdEntity && ((BaseGenericIdEntity) entity).getDynamicAttributes() != null) {
        Map<String, CategoryAttributeValue> dynamicAttributes = ((BaseGenericIdEntity) entity).getDynamicAttributes();
        if (dynamicAttributes == null) {
            throw new RuntimeException("Entity dynamicAttributes is null");
        }
        for (CategoryAttributeValue cav : dynamicAttributes.values()) {
            if (BaseEntityInternalAccess.isNew(cav)) {
                changes.add(new AttributeChanges.Change(DynamicAttributesUtils.encodeAttributeCode(cav.getCode()), null));
            } else {
                if (deleted) {
                    Object oldValue;
                    switch(cav.getCategoryAttribute().getDataType()) {
                        case STRING:
                        case ENUMERATION:
                            oldValue = cav.getStringValue();
                            break;
                        case INTEGER:
                            oldValue = cav.getIntValue();
                            break;
                        case DOUBLE:
                            oldValue = cav.getDoubleValue();
                            break;
                        case DECIMAL:
                            oldValue = cav.getDecimalValue();
                            break;
                        case BOOLEAN:
                            oldValue = cav.getBooleanValue();
                            break;
                        case DATE_WITHOUT_TIME:
                            oldValue = cav.getDateWithoutTimeValue();
                            break;
                        case DATE:
                            oldValue = cav.getDateValue();
                            break;
                        case ENTITY:
                            Object entityId = cav.getEntityValue().getObjectEntityId();
                            Class entityClass = cav.getCategoryAttribute().getJavaClassForEntity();
                            oldValue = entityId != null ? Id.of(entityId, entityClass) : null;
                            break;
                        default:
                            log.warn("Unsupported dynamic attribute type: " + cav.getCategoryAttribute().getDataType());
                            oldValue = null;
                    }
                    changes.add(new AttributeChanges.Change(DynamicAttributesUtils.encodeAttributeCode(cav.getCode()), oldValue));
                } else {
                    AttributeChangeListener changeListener = (AttributeChangeListener) ((ChangeTracker) cav)._persistence_getPropertyChangeListener();
                    if (changeListener != null && changeListener.getObjectChangeSet() != null) {
                        Object oldValue = null;
                        boolean changed = false;
                        for (ChangeRecord changeRecord : changeListener.getObjectChangeSet().getChanges()) {
                            switch(changeRecord.getAttribute()) {
                                case "stringValue":
                                case "intValue":
                                case "doubleValue":
                                case "decimalValue":
                                case "booleanValue":
                                case "dateWithoutTimeValue":
                                case "dateValue":
                                    oldValue = changeRecord.getOldValue();
                                    changed = true;
                                    break;
                                case "entityValue":
                                    Object entityId = ((ReferenceToEntity) changeRecord.getOldValue()).getObjectEntityId();
                                    Class entityClass = cav.getCategoryAttribute().getJavaClassForEntity();
                                    oldValue = entityId != null ? Id.of(entityId, entityClass) : null;
                                    changed = true;
                                    break;
                            }
                            if (changed) {
                                changes.add(new AttributeChanges.Change(DynamicAttributesUtils.encodeAttributeCode(cav.getCode()), oldValue));
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : AttributeChanges(com.haulmont.cuba.core.app.events.AttributeChanges) AttributeChangeListener(org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener) MetaClass(com.haulmont.chile.core.model.MetaClass) AggregateChangeRecord(org.eclipse.persistence.sessions.changesets.AggregateChangeRecord) ChangeRecord(org.eclipse.persistence.sessions.changesets.ChangeRecord)

Example 3 with ChangeRecord

use of org.eclipse.persistence.sessions.changesets.ChangeRecord in project jmix by jmix-framework.

the class EclipselinkAttributeChangesProvider method getOldValueByImplementation.

@Override
@Nullable
protected Object getOldValueByImplementation(Object entity, String attribute) {
    checkEntityByImplementation(entity);
    PropertyChangeListener propertyChangeListener = ((ChangeTracker) entity)._persistence_getPropertyChangeListener();
    ObjectChangeSet objectChanges = ((AttributeChangeListener) propertyChangeListener).getObjectChangeSet();
    if (objectChanges != null) {
        ChangeRecord changeRecord = objectChanges.getChangesForAttributeNamed(attribute);
        if (changeRecord != null) {
            return changeRecord.getOldValue();
        }
    }
    return null;
}
Also used : PropertyChangeListener(java.beans.PropertyChangeListener) AttributeChangeListener(org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener) ObjectChangeSet(org.eclipse.persistence.sessions.changesets.ObjectChangeSet) ChangeTracker(org.eclipse.persistence.descriptors.changetracking.ChangeTracker) AggregateChangeRecord(org.eclipse.persistence.sessions.changesets.AggregateChangeRecord) ChangeRecord(org.eclipse.persistence.sessions.changesets.ChangeRecord) Nullable(org.springframework.lang.Nullable)

Example 4 with ChangeRecord

use of org.eclipse.persistence.sessions.changesets.ChangeRecord in project cuba by cuba-platform.

the class EntityAttributeChanges method addChanges.

protected void addChanges(ObjectChangeSet changeSet) {
    if (changeSet == null)
        return;
    for (ChangeRecord changeRecord : changeSet.getChanges()) {
        addChange(changeRecord.getAttribute(), changeRecord.getOldValue());
        if (changeRecord instanceof AggregateChangeRecord) {
            embeddedChanges.computeIfAbsent(changeRecord.getAttribute(), s -> {
                EntityAttributeChanges embeddedChanges = new EntityAttributeChanges();
                embeddedChanges.addChanges(((AggregateChangeRecord) changeRecord).getChangedObject());
                return embeddedChanges;
            });
        }
    }
}
Also used : AggregateChangeRecord(org.eclipse.persistence.sessions.changesets.AggregateChangeRecord) AggregateChangeRecord(org.eclipse.persistence.sessions.changesets.AggregateChangeRecord) ChangeRecord(org.eclipse.persistence.sessions.changesets.ChangeRecord)

Example 5 with ChangeRecord

use of org.eclipse.persistence.sessions.changesets.ChangeRecord in project cuba by cuba-platform.

the class EntityChangedEventManager method getEntityAttributeChanges.

@SuppressWarnings("unchecked")
private AttributeChanges getEntityAttributeChanges(@Nullable Entity entity, ObjectChangeSet changeSet) {
    if (changeSet == null)
        return null;
    Set<AttributeChanges.Change> changes = new HashSet<>();
    Map<String, AttributeChanges> embeddedChanges = new HashMap<>();
    for (ChangeRecord changeRecord : changeSet.getChanges()) {
        if (changeRecord instanceof AggregateChangeRecord) {
            embeddedChanges.computeIfAbsent(changeRecord.getAttribute(), s -> getEntityAttributeChanges(null, ((AggregateChangeRecord) changeRecord).getChangedObject()));
        } else {
            Object oldValue = changeRecord.getOldValue();
            if (oldValue instanceof Entity) {
                changes.add(new AttributeChanges.Change(changeRecord.getAttribute(), Id.of((Entity) oldValue)));
            } else if (oldValue instanceof Collection) {
                Collection<Entity> coll = (Collection<Entity>) oldValue;
                Collection<Id> idColl = oldValue instanceof List ? new ArrayList<>() : new LinkedHashSet<>();
                for (Entity item : coll) {
                    idColl.add(Id.of(item));
                }
                changes.add(new AttributeChanges.Change(changeRecord.getAttribute(), idColl));
            } else {
                changes.add(new AttributeChanges.Change(changeRecord.getAttribute(), oldValue));
            }
        }
    }
    addDynamicAttributeChanges(entity, changes, false);
    return new AttributeChanges(changes, embeddedChanges);
}
Also used : AttributeChanges(com.haulmont.cuba.core.app.events.AttributeChanges) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AggregateChangeRecord(org.eclipse.persistence.sessions.changesets.AggregateChangeRecord) AggregateChangeRecord(org.eclipse.persistence.sessions.changesets.AggregateChangeRecord) ChangeRecord(org.eclipse.persistence.sessions.changesets.ChangeRecord)

Aggregations

AggregateChangeRecord (org.eclipse.persistence.sessions.changesets.AggregateChangeRecord)6 ChangeRecord (org.eclipse.persistence.sessions.changesets.ChangeRecord)6 AttributeChanges (com.haulmont.cuba.core.app.events.AttributeChanges)2 AttributeChangeListener (org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener)2 MetaClass (com.haulmont.chile.core.model.MetaClass)1 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ChangeTracker (org.eclipse.persistence.descriptors.changetracking.ChangeTracker)1 ObjectChangeSet (org.eclipse.persistence.sessions.changesets.ObjectChangeSet)1 Nullable (org.springframework.lang.Nullable)1