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