Search in sources :

Example 6 with AttributeChangeListener

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

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

Example 8 with AttributeChangeListener

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

the class EntityAttributeChanges method addChanges.

/**
 * Accumulates changes for the entity. Stores changed attribute names and old values.
 */
public void addChanges(Entity entity) {
    if (!(entity instanceof ChangeTracker))
        return;
    AttributeChangeListener changeListener = (AttributeChangeListener) ((ChangeTracker) entity)._persistence_getPropertyChangeListener();
    if (changeListener == null)
        return;
    addChanges(changeListener.getObjectChangeSet());
}
Also used : AttributeChangeListener(org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener) ChangeTracker(org.eclipse.persistence.descriptors.changetracking.ChangeTracker)

Example 9 with AttributeChangeListener

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

Example 10 with AttributeChangeListener

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

the class PersistenceImplSupport method beforeStore.

protected void beforeStore(ContainerResourceHolder container, EntityVisitor visitor, Collection<Entity> instances, Set<Entity> processed, boolean warnAboutImplicitFlush) {
    boolean possiblyChanged = false;
    Set<Entity> withoutPossibleChanges = createEntitySet();
    for (Entity instance : instances) {
        processed.add(instance);
        if (!(instance instanceof ChangeTracker && instance instanceof BaseGenericIdEntity))
            continue;
        BaseGenericIdEntity entity = (BaseGenericIdEntity) instance;
        boolean result = visitor.visit(entity);
        if (!result) {
            withoutPossibleChanges.add(instance);
        }
        possiblyChanged = result || possiblyChanged;
    }
    if (!possiblyChanged)
        return;
    if (warnAboutImplicitFlush) {
        statisticsAccumulator.incImplicitFlushCount();
        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<Entity> 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

AttributeChangeListener (org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener)12 ChangeTracker (org.eclipse.persistence.descriptors.changetracking.ChangeTracker)9 PropertyChangeListener (java.beans.PropertyChangeListener)5 ObjectChangeSet (org.eclipse.persistence.sessions.changesets.ObjectChangeSet)3 MetaClass (com.haulmont.chile.core.model.MetaClass)2 AttributeChanges (com.haulmont.cuba.core.app.events.AttributeChanges)2 ObjectChangeSet (org.eclipse.persistence.internal.sessions.ObjectChangeSet)2 AggregateChangeRecord (org.eclipse.persistence.sessions.changesets.AggregateChangeRecord)2 ChangeRecord (org.eclipse.persistence.sessions.changesets.ChangeRecord)2 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 EntityChangedEvent (com.haulmont.cuba.core.app.events.EntityChangedEvent)1 AttributeChanges (io.jmix.core.event.AttributeChanges)1 EntityChangedEvent (io.jmix.core.event.EntityChangedEvent)1 MetaClass (io.jmix.core.metamodel.model.MetaClass)1 EntityChangedEventInfo (io.jmix.data.impl.EntityChangedEventInfo)1 HashSet (java.util.HashSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Nullable (org.springframework.lang.Nullable)1