Search in sources :

Example 1 with ChangeTracker

use of org.eclipse.persistence.descriptors.changetracking.ChangeTracker 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 ChangeTracker

use of org.eclipse.persistence.descriptors.changetracking.ChangeTracker in project cuba by cuba-platform.

the class PersistenceTools method isDirty.

/**
 * Returns true if the given entity has dirty attributes (changed since the last load from the database).
 * <br> If the entity is new, returns true.
 * <br> If the entity is not persistent or not in the Managed state, returns false.
 *
 * @param entity entity instance
 * @see #getDirtyFields(Entity)
 * @see #isDirty(Entity, String...)
 */
public boolean isDirty(Entity entity) {
    Preconditions.checkNotNullArgument(entity, "entity is null");
    if (!(entity instanceof ChangeTracker) || !entityStates.isManaged(entity))
        return false;
    if (entityStates.isNew(entity))
        return true;
    AttributeChangeListener attributeChangeListener = (AttributeChangeListener) ((ChangeTracker) entity)._persistence_getPropertyChangeListener();
    return attributeChangeListener != null && attributeChangeListener.hasChanges();
}
Also used : AttributeChangeListener(org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener) ChangeTracker(org.eclipse.persistence.descriptors.changetracking.ChangeTracker)

Example 3 with ChangeTracker

use of org.eclipse.persistence.descriptors.changetracking.ChangeTracker 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 ChangeTracker

use of org.eclipse.persistence.descriptors.changetracking.ChangeTracker in project jmix by jmix-framework.

the class EclipselinkAttributeChangesProvider method buildChangesByImplementation.

@Override
protected void buildChangesByImplementation(AttributeChanges.Builder builder, Object entity, BiFunction<Object, MetaProperty, Object> transformer) {
    checkEntityByImplementation(entity);
    PropertyChangeListener propertyChangeListener = ((ChangeTracker) entity)._persistence_getPropertyChangeListener();
    ObjectChangeSet objectChanges = ((AttributeChangeListener) propertyChangeListener).getObjectChangeSet();
    if (objectChanges != null) {
        buildChanges(builder, objectChanges, metadata.getClass(entity), transformer);
    }
}
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)

Example 5 with ChangeTracker

use of org.eclipse.persistence.descriptors.changetracking.ChangeTracker in project jmix by jmix-framework.

the class EclipselinkPersistenceSupport method beforeStore.

protected void beforeStore(ContainerResourceHolder container, EntityVisitor visitor, Collection<Object> instances, Set<Object> processed, boolean warnAboutImplicitFlush) {
    boolean possiblyChanged = false;
    Set<Object> withoutPossibleChanges = createEntitySet();
    for (Object entity : instances) {
        processed.add(entity);
        if (!(entity instanceof ChangeTracker))
            continue;
        boolean result = visitor.visit(entity);
        if (!result) {
            withoutPossibleChanges.add(entity);
        }
        possiblyChanged = result || possiblyChanged;
    }
    if (!possiblyChanged)
        return;
    if (warnAboutImplicitFlush) {
        if (implicitFlushLog.isTraceEnabled()) {
            implicitFlushLog.trace("Implicit flush due to query execution, see stack trace for the cause:\n" + StackTrace.asString());
        } else {
            implicitFlushLog.debug("Implicit flush due to query execution");
        }
    }
    Collection<Object> afterProcessing = container.getAllInstances();
    if (afterProcessing.size() > processed.size()) {
        afterProcessing.removeAll(processed);
        beforeStore(container, visitor, afterProcessing, processed, false);
    }
    if (!withoutPossibleChanges.isEmpty()) {
        afterProcessing = withoutPossibleChanges.stream().filter(instance -> {
            ChangeTracker changeTracker = (ChangeTracker) instance;
            AttributeChangeListener changeListener = (AttributeChangeListener) changeTracker._persistence_getPropertyChangeListener();
            return changeListener != null && changeListener.hasChanges();
        }).collect(Collectors.toList());
        if (!afterProcessing.isEmpty()) {
            beforeStore(container, visitor, afterProcessing, processed, false);
        }
    }
}
Also used : AttributeChangeListener(org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener) ChangeTracker(org.eclipse.persistence.descriptors.changetracking.ChangeTracker)

Aggregations

ChangeTracker (org.eclipse.persistence.descriptors.changetracking.ChangeTracker)10 AttributeChangeListener (org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener)9 PropertyChangeListener (java.beans.PropertyChangeListener)6 ObjectChangeSet (org.eclipse.persistence.sessions.changesets.ObjectChangeSet)3 ObjectChangeSet (org.eclipse.persistence.internal.sessions.ObjectChangeSet)2 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 HashSet (java.util.HashSet)1 AggregateChangeRecord (org.eclipse.persistence.sessions.changesets.AggregateChangeRecord)1 ChangeRecord (org.eclipse.persistence.sessions.changesets.ChangeRecord)1 Nullable (org.springframework.lang.Nullable)1