Search in sources :

Example 1 with ObjectChangeSet

use of org.eclipse.persistence.internal.sessions.ObjectChangeSet in project cuba by cuba-platform.

the class EntityLog method calculateDirtyFields.

protected Set<String> calculateDirtyFields(Entity entity, EntityAttributeChanges changes) {
    if (changes == null) {
        if (!(entity instanceof ChangeTracker) || !PersistenceHelper.isManaged(entity))
            return Collections.emptySet();
        PropertyChangeListener propertyChangeListener = ((ChangeTracker) entity)._persistence_getPropertyChangeListener();
        if (propertyChangeListener == null)
            throw new IllegalStateException("Entity '" + entity + "' is a ChangeTracker but has no PropertyChangeListener");
        changes = new EntityAttributeChanges();
        ObjectChangeSet objectChanges = ((AttributeChangeListener) propertyChangeListener).getObjectChangeSet();
        if (objectChanges != null) {
            changes.addChanges(objectChanges);
        }
    }
    return changes.getAttributes();
}
Also used : PropertyChangeListener(java.beans.PropertyChangeListener) AttributeChangeListener(org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener) ObjectChangeSet(org.eclipse.persistence.internal.sessions.ObjectChangeSet) ChangeTracker(org.eclipse.persistence.descriptors.changetracking.ChangeTracker)

Example 2 with ObjectChangeSet

use of org.eclipse.persistence.internal.sessions.ObjectChangeSet in project cuba by cuba-platform.

the class PersistenceTools method getDirtyFields.

/**
 * Returns the set of dirty attributes (changed since the last load from the database).
 * <p> If the entity is new, returns all its attributes.
 * <p> If the entity is not persistent or not in the Managed state, returns empty set.
 *
 * @param entity entity instance
 * @return dirty attribute names
 * @see #isDirty(Entity, String...)
 */
public Set<String> getDirtyFields(Entity entity) {
    Preconditions.checkNotNullArgument(entity, "entity is null");
    if (!(entity instanceof ChangeTracker) || !entityStates.isManaged(entity))
        return Collections.emptySet();
    HashSet<String> result = new HashSet<>();
    if (entityStates.isNew(entity)) {
        for (MetaProperty property : metadata.getClassNN(entity.getClass()).getProperties()) {
            if (metadata.getTools().isPersistent(property))
                result.add(property.getName());
        }
    } else {
        PropertyChangeListener propertyChangeListener = ((ChangeTracker) entity)._persistence_getPropertyChangeListener();
        if (propertyChangeListener == null)
            throw new IllegalStateException("Entity '" + entity + "' is a ChangeTracker but has no PropertyChangeListener");
        ObjectChangeSet objectChanges = ((AttributeChangeListener) propertyChangeListener).getObjectChangeSet();
        if (// can be null for example in AFTER_DELETE entity listener
        objectChanges != null)
            result.addAll(objectChanges.getChangedAttributeNames());
    }
    return result;
}
Also used : PropertyChangeListener(java.beans.PropertyChangeListener) AttributeChangeListener(org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener) ObjectChangeSet(org.eclipse.persistence.internal.sessions.ObjectChangeSet) MetaProperty(com.haulmont.chile.core.model.MetaProperty) ChangeTracker(org.eclipse.persistence.descriptors.changetracking.ChangeTracker)

Aggregations

PropertyChangeListener (java.beans.PropertyChangeListener)2 ChangeTracker (org.eclipse.persistence.descriptors.changetracking.ChangeTracker)2 AttributeChangeListener (org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener)2 ObjectChangeSet (org.eclipse.persistence.internal.sessions.ObjectChangeSet)2 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1