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