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