Search in sources :

Example 1 with AttributeChangeListener

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

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();
}
Also used : AttributeChangeListener(org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener) ChangeTracker(org.eclipse.persistence.descriptors.changetracking.ChangeTracker)

Example 3 with AttributeChangeListener

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;
}
Also used : AttributeChanges(com.haulmont.cuba.core.app.events.AttributeChanges) EntityChangedEvent(com.haulmont.cuba.core.app.events.EntityChangedEvent) MetaClass(com.haulmont.chile.core.model.MetaClass) AttributeChangeListener(org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 4 with AttributeChangeListener

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;
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : AttributeChanges(com.haulmont.cuba.core.app.events.AttributeChanges) AttributeChangeListener(org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener) MetaClass(com.haulmont.chile.core.model.MetaClass) AggregateChangeRecord(org.eclipse.persistence.sessions.changesets.AggregateChangeRecord) ChangeRecord(org.eclipse.persistence.sessions.changesets.ChangeRecord)

Example 5 with AttributeChangeListener

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

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