use of org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener 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.internal.descriptors.changetracking.AttributeChangeListener 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.internal.descriptors.changetracking.AttributeChangeListener in project cuba by cuba-platform.
the class EntityChangedEventManager method internalCollect.
public List<EntityChangedEventInfo> internalCollect(Collection<Entity> entities) {
List<EntityChangedEventInfo> list = new ArrayList<>();
for (Entity entity : entities) {
PublishingInfo info = infoCache.computeIfAbsent(entity.getClass(), aClass -> {
MetaClass metaClass = metadata.getClassNN(entity.getClass());
Map attrMap = (Map) metaClass.getAnnotations().get(PublishEntityChangedEvents.class.getName());
if (attrMap != null) {
if (!(entity instanceof BaseGenericIdEntity)) {
log.warn("Cannot publish EntityChangedEvent for {} because it is not a BaseGenericIdEntity", entity);
} else {
return new PublishingInfo(Boolean.TRUE.equals(attrMap.get("created")), Boolean.TRUE.equals(attrMap.get("updated")), Boolean.TRUE.equals(attrMap.get("deleted")));
}
}
return new PublishingInfo();
});
if (info.publish) {
EntityChangedEvent.Type type = null;
AttributeChanges attributeChanges = null;
if (info.onCreated && BaseEntityInternalAccess.isNew((BaseGenericIdEntity) entity)) {
type = EntityChangedEvent.Type.CREATED;
attributeChanges = getEntityAttributeChanges(entity, false);
} else {
if (info.onUpdated || info.onDeleted) {
AttributeChangeListener changeListener = (AttributeChangeListener) ((ChangeTracker) entity)._persistence_getPropertyChangeListener();
if (changeListener == null) {
log.trace("Cannot publish EntityChangedEvent for {} because its AttributeChangeListener is null", entity);
continue;
}
if (info.onDeleted && PersistenceImplSupport.isDeleted((BaseGenericIdEntity) entity, changeListener)) {
type = EntityChangedEvent.Type.DELETED;
attributeChanges = getEntityAttributeChanges(entity, true);
} else if (info.onUpdated && changeListener.hasChanges()) {
type = EntityChangedEvent.Type.UPDATED;
attributeChanges = getEntityAttributeChanges(entity, changeListener.getObjectChangeSet());
}
}
}
if (type != null) {
@SuppressWarnings("unchecked") EntityChangedEventInfo eventData = new EntityChangedEventInfo(this, entity, type, attributeChanges);
list.add(eventData);
}
}
}
log.trace("collected {}", list);
return list;
}
use of org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener 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;
}
}
}
}
}
}
}
}
use of org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener 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;
}
Aggregations