use of com.haulmont.cuba.core.entity.contracts.Id in project cuba by cuba-platform.
the class EntityChangedEventManager method getEntityAttributeChanges.
@SuppressWarnings("unchecked")
private AttributeChanges getEntityAttributeChanges(Entity entity, boolean deleted) {
Set<AttributeChanges.Change> changes = new HashSet<>();
Map<String, AttributeChanges> embeddedChanges = new HashMap<>();
for (MetaProperty property : metadata.getClassNN(entity.getClass()).getProperties()) {
Object value = entity.getValue(property.getName());
if (deleted) {
if (value instanceof EmbeddableEntity) {
EmbeddableEntity embedded = (EmbeddableEntity) value;
embeddedChanges.computeIfAbsent(property.getName(), s -> getEntityAttributeChanges(embedded, true));
} else if (value instanceof Entity) {
changes.add(new AttributeChanges.Change(property.getName(), Id.of((Entity) value)));
} else if (value instanceof Collection) {
Collection<Entity> coll = (Collection<Entity>) value;
Collection<Id> idColl = value instanceof List ? new ArrayList<>() : new LinkedHashSet<>();
for (Entity item : coll) {
idColl.add(Id.of(item));
}
changes.add(new AttributeChanges.Change(property.getName(), idColl));
} else {
changes.add(new AttributeChanges.Change(property.getName(), value));
}
} else {
if (value != null) {
changes.add(new AttributeChanges.Change(property.getName(), null));
}
}
}
if (deleted) {
addDynamicAttributeChanges(entity, changes, true);
}
return new AttributeChanges(changes, embeddedChanges);
}
use of com.haulmont.cuba.core.entity.contracts.Id in project documentation by cuba-platform.
the class OrderLineChangedListener method beforeCommit.
@EventListener
public void beforeCommit(EntityChangedEvent<OrderLine, UUID> event) {
Order order;
if (event.getType() != EntityChangedEvent.Type.DELETED) {
// <1>
order = // <2>
txDm.load(event.getEntityId()).view(// <3>
"orderLine-with-order").one().getOrder();
} else {
// <5>
Id<Order, UUID> orderId = event.getChanges().getOldReferenceId("order");
order = txDm.load(orderId).one();
}
long count = // <6>
txDm.load(OrderLine.class).query("select o from sales_OrderLine o where o.order = :order").parameter("order", order).view("orderLine-with-product").list().stream().filter(orderLine -> Boolean.TRUE.equals(orderLine.getProduct().getSpecial())).count();
order.setNumberOfSpecialProducts((int) count);
// <7>
txDm.save(order);
}
Aggregations