Search in sources :

Example 1 with EntityChangedEvent

use of io.jmix.core.event.EntityChangedEvent in project jmix-docs by Haulmont.

the class OrderLineEventListener method onOrderLineChangedBeforeCommit.

@EventListener
void onOrderLineChangedBeforeCommit(EntityChangedEvent<OrderLine> event) {
    Order order;
    if (event.getType() == EntityChangedEvent.Type.DELETED) {
        // <1>
        // <2>
        Id<Order> orderId = event.getChanges().getOldReferenceId("order");
        order = dataManager.load(orderId).one();
    } else {
        OrderLine orderLine = dataManager.load(event.getEntityId()).one();
        order = orderLine.getOrder();
    }
    BigDecimal amount = order.getLines().stream().map(line -> line.getProduct().getPrice().multiply(BigDecimal.valueOf(line.getQuantity()))).reduce(BigDecimal.ZERO, BigDecimal::add);
    order.setAmount(amount);
    dataManager.save(order);
}
Also used : Order(dataaccess.ex1.entity.Order) BigDecimal(java.math.BigDecimal) DataManager(io.jmix.core.DataManager) Id(io.jmix.core.Id) Component(org.springframework.stereotype.Component) OrderLine(dataaccess.ex1.entity.OrderLine) Autowired(org.springframework.beans.factory.annotation.Autowired) EventListener(org.springframework.context.event.EventListener) Order(dataaccess.ex1.entity.Order) EntityChangedEvent(io.jmix.core.event.EntityChangedEvent) OrderLine(dataaccess.ex1.entity.OrderLine) BigDecimal(java.math.BigDecimal) EventListener(org.springframework.context.event.EventListener)

Example 2 with EntityChangedEvent

use of io.jmix.core.event.EntityChangedEvent in project jmix by jmix-framework.

the class EntityTrackingListener method processEntityChangedEvent.

protected void processEntityChangedEvent(EntityChangedEvent<?> event) {
    Id<?> entityId = event.getEntityId();
    Class<?> entityClass = entityId.getEntityClass();
    MetaClass metaClass = metadata.getClass(entityClass);
    EntityChangedEvent.Type eventType = event.getType();
    String entityName = metaClass.getName();
    AttributeChanges changes = event.getChanges();
    if (indexConfigurationManager.isDirectlyIndexed(entityName)) {
        log.debug("{} is directly indexed", entityId);
        switch(eventType) {
            case CREATED:
                indexingQueueManager.enqueueIndexByEntityId(entityId);
                break;
            case UPDATED:
                if (isUpdateRequired(entityClass, changes)) {
                    indexingQueueManager.enqueueIndexByEntityId(entityId);
                }
                break;
            case DELETED:
                indexingQueueManager.enqueueDeleteByEntityId(entityId);
                break;
        }
    }
    if (EntityChangedEvent.Type.UPDATED.equals(eventType)) {
        Set<Id<?>> dependentEntityIds = getEntityIdsDependentOnUpdatedEntity(entityId, metaClass, changes);
        if (!dependentEntityIds.isEmpty()) {
            indexingQueueManager.enqueueIndexCollectionByEntityIds(dependentEntityIds);
        }
    } else if (EntityChangedEvent.Type.DELETED.equals(eventType)) {
        Set<Id<?>> dependentEntityIds = removalDependencies.getIfPresent(entityId);
        if (CollectionUtils.isNotEmpty(dependentEntityIds)) {
            indexingQueueManager.enqueueIndexCollectionByEntityIds(dependentEntityIds);
            removalDependencies.invalidate(entityId);
        }
    }
}
Also used : AttributeChanges(io.jmix.core.event.AttributeChanges) MetaClass(io.jmix.core.metamodel.model.MetaClass) EntityChangedEvent(io.jmix.core.event.EntityChangedEvent)

Example 3 with EntityChangedEvent

use of io.jmix.core.event.EntityChangedEvent 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 4 with EntityChangedEvent

use of io.jmix.core.event.EntityChangedEvent 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)4 AttributeChanges (io.jmix.core.event.AttributeChanges)2 MetaClass (io.jmix.core.metamodel.model.MetaClass)2 EntityChangedEventInfo (io.jmix.data.impl.EntityChangedEventInfo)2 Order (dataaccess.ex1.entity.Order)1 OrderLine (dataaccess.ex1.entity.OrderLine)1 DataManager (io.jmix.core.DataManager)1 Id (io.jmix.core.Id)1 UniqueConstraintViolationException (io.jmix.data.exception.UniqueConstraintViolationException)1 BigDecimal (java.math.BigDecimal)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 EntityManager (javax.persistence.EntityManager)1 PersistenceException (javax.persistence.PersistenceException)1 AttributeChangeListener (org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 EventListener (org.springframework.context.event.EventListener)1 Component (org.springframework.stereotype.Component)1