Search in sources :

Example 1 with EntityChangedEventInfo

use of io.jmix.data.impl.EntityChangedEventInfo in project jmix by jmix-framework.

the class EntityChangedEventManager method merge.

private List<EntityChangedEventInfo> merge(Collection<EntityChangedEventInfo> collection1, Collection<EntityChangedEventInfo> collection2) {
    List<EntityChangedEventInfo> list1 = collection1 != null ? new ArrayList<>(collection1) : new ArrayList<>();
    Collection<EntityChangedEventInfo> list2 = collection2 != null ? collection2 : Collections.emptyList();
    for (EntityChangedEventInfo info2 : list2) {
        Optional<EntityChangedEventInfo> opt = list1.stream().filter(info1 -> info1.getEntity() == info2.getEntity()).findAny();
        if (opt.isPresent()) {
            opt.get().mergeWith(info2);
        } else {
            list1.add(info2);
        }
    }
    log.trace("merged {}", list1);
    return list1;
}
Also used : EntityChangedEventInfo(io.jmix.data.impl.EntityChangedEventInfo) EntitySystemAccess(io.jmix.core.entity.EntitySystemAccess) AttributeChangeListener(org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener) Id(io.jmix.core.Id) MetaClass(io.jmix.core.metamodel.model.MetaClass) java.util(java.util) AggregateChangeRecord(org.eclipse.persistence.sessions.changesets.AggregateChangeRecord) ChangeRecord(org.eclipse.persistence.sessions.changesets.ChangeRecord) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) EntityValues(io.jmix.core.entity.EntityValues) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) ResourceHolderSupport(org.springframework.transaction.support.ResourceHolderSupport) ResourceHolderSynchronization(org.springframework.transaction.support.ResourceHolderSynchronization) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) EntityChangedEvent(io.jmix.core.event.EntityChangedEvent) Nullable(javax.annotation.Nullable) EntitySystemAccess.getEntityEntry(io.jmix.core.entity.EntitySystemAccess.getEntityEntry) EntityChangedEventInfo(io.jmix.data.impl.EntityChangedEventInfo) Logger(org.slf4j.Logger) AttributeChanges(io.jmix.core.event.AttributeChanges) ChangeTracker(org.eclipse.persistence.descriptors.changetracking.ChangeTracker) Metadata(io.jmix.core.Metadata) ObjectChangeSet(org.eclipse.persistence.sessions.changesets.ObjectChangeSet) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass) Component(org.springframework.stereotype.Component) ExtendedEntities(io.jmix.core.ExtendedEntities) Entity(io.jmix.core.Entity) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 2 with EntityChangedEventInfo

use of io.jmix.data.impl.EntityChangedEventInfo in project jmix by jmix-framework.

the class EntityChangedEventManager method internalCollect.

public List<EntityChangedEventInfo> internalCollect(Collection<Object> entities) {
    List<EntityChangedEventInfo> list = new ArrayList<>();
    for (Object entity : entities) {
        EntityChangedEvent.Type type = null;
        AttributeChanges.Builder attributeChangesBuilder = null;
        if (getEntityEntry(entity).isNew()) {
            type = EntityChangedEvent.Type.CREATED;
            attributeChangesBuilder = getEntityAttributeChanges(entity, false);
        } else {
            AttributeChangeListener changeListener = (AttributeChangeListener) ((ChangeTracker) entity)._persistence_getPropertyChangeListener();
            if (changeListener == null) {
                log.debug("Cannot publish EntityChangedEvent for {} because its AttributeChangeListener is null", entity);
                continue;
            }
            if (persistenceSupport.isDeleted(entity, changeListener)) {
                type = EntityChangedEvent.Type.DELETED;
                attributeChangesBuilder = getEntityAttributeChanges(entity, true);
            } else if (changeListener.hasChanges()) {
                type = EntityChangedEvent.Type.UPDATED;
                attributeChangesBuilder = getEntityAttributeChanges(entity, changeListener.getObjectChangeSet());
            }
        }
        if (type != null && attributeChangesBuilder != null) {
            MetaClass originalMetaClass = extendedEntities.getOriginalOrThisMetaClass(metadata.getClass(entity));
            EntityChangedEventInfo eventData = new EntityChangedEventInfo(this, entity, type, attributeChangesBuilder.build(), originalMetaClass);
            list.add(eventData);
        }
    }
    log.trace("collected {}", list);
    return list;
}
Also used : EntityChangedEventInfo(io.jmix.data.impl.EntityChangedEventInfo) AttributeChanges(io.jmix.core.event.AttributeChanges) MetaClass(io.jmix.core.metamodel.model.MetaClass) EntityChangedEvent(io.jmix.core.event.EntityChangedEvent) AttributeChangeListener(org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener)

Example 3 with EntityChangedEventInfo

use of io.jmix.data.impl.EntityChangedEventInfo in project jmix by jmix-framework.

the class JpaDataStore method beforeSaveTransactionCommit.

@Override
protected void beforeSaveTransactionCommit(SaveContext context, Collection<Object> savedEntities, Collection<Object> removedEntities) {
    if (context.isJoinTransaction()) {
        List<Object> entities = new ArrayList<>(savedEntities);
        entities.addAll(removedEntities);
        List<EntityChangedEventInfo> eventsInfo;
        EntityManager em = storeAwareLocator.getEntityManager(storeName);
        boolean softDeletionBefore = PersistenceHints.isSoftDeletion(em);
        try {
            em.setProperty(PersistenceHints.SOFT_DELETION, context.getHints().get(PersistenceHints.SOFT_DELETION));
            persistenceSupport.processFlush(em, false);
            eventsInfo = entityChangedEventManager.collect(persistenceSupport.getInstances(em));
            ((EntityManager) em.getDelegate()).flush();
        } catch (PersistenceException e) {
            Pattern pattern = getUniqueConstraintViolationPattern();
            Matcher matcher = pattern.matcher(e.toString());
            if (matcher.find()) {
                throw new UniqueConstraintViolationException(e.getMessage(), resolveConstraintName(matcher), e);
            }
            throw e;
        } finally {
            em.setProperty(PersistenceHints.SOFT_DELETION, softDeletionBefore);
        }
        // noinspection rawtypes
        List<EntityChangedEvent> events = new ArrayList<>(eventsInfo.size());
        for (EntityChangedEventInfo info : eventsInfo) {
            events.add(new EntityChangedEvent<>(info.getSource(), Id.of(info.getEntity()), info.getType(), info.getChanges(), info.getOriginalMetaClass()));
        }
        for (Object entity : entities) {
            detachEntity(em, entity, context.getFetchPlans().get(entity), true);
        }
        entityChangedEventManager.publish(events);
    }
}
Also used : EntityChangedEventInfo(io.jmix.data.impl.EntityChangedEventInfo) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) EntityChangedEvent(io.jmix.core.event.EntityChangedEvent) UniqueConstraintViolationException(io.jmix.data.exception.UniqueConstraintViolationException) EntityManager(javax.persistence.EntityManager) PersistenceException(javax.persistence.PersistenceException)

Aggregations

EntityChangedEvent (io.jmix.core.event.EntityChangedEvent)3 EntityChangedEventInfo (io.jmix.data.impl.EntityChangedEventInfo)3 AttributeChanges (io.jmix.core.event.AttributeChanges)2 MetaClass (io.jmix.core.metamodel.model.MetaClass)2 AttributeChangeListener (org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener)2 Entity (io.jmix.core.Entity)1 ExtendedEntities (io.jmix.core.ExtendedEntities)1 Id (io.jmix.core.Id)1 Metadata (io.jmix.core.Metadata)1 EntitySystemAccess (io.jmix.core.entity.EntitySystemAccess)1 EntitySystemAccess.getEntityEntry (io.jmix.core.entity.EntitySystemAccess.getEntityEntry)1 EntityValues (io.jmix.core.entity.EntityValues)1 EnumClass (io.jmix.core.metamodel.datatype.impl.EnumClass)1 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)1 UniqueConstraintViolationException (io.jmix.data.exception.UniqueConstraintViolationException)1 java.util (java.util)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Nullable (javax.annotation.Nullable)1 EntityManager (javax.persistence.EntityManager)1