Search in sources :

Example 1 with Id

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);
}
Also used : AttributeChanges(com.haulmont.cuba.core.app.events.AttributeChanges) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Id(com.haulmont.cuba.core.entity.contracts.Id) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 2 with Id

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);
}
Also used : Order(com.company.sales.entity.Order) Inject(javax.inject.Inject) Component(org.springframework.stereotype.Component) EntityChangedEvent(com.haulmont.cuba.core.app.events.EntityChangedEvent) TransactionalDataManager(com.haulmont.cuba.core.TransactionalDataManager) EventListener(org.springframework.context.event.EventListener) UUID(java.util.UUID) OrderLine(com.company.sales.entity.OrderLine) Order(com.company.sales.entity.Order) Id(com.haulmont.cuba.core.entity.contracts.Id) OrderLine(com.company.sales.entity.OrderLine) UUID(java.util.UUID) EventListener(org.springframework.context.event.EventListener)

Aggregations

Id (com.haulmont.cuba.core.entity.contracts.Id)2 Order (com.company.sales.entity.Order)1 OrderLine (com.company.sales.entity.OrderLine)1 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 TransactionalDataManager (com.haulmont.cuba.core.TransactionalDataManager)1 AttributeChanges (com.haulmont.cuba.core.app.events.AttributeChanges)1 EntityChangedEvent (com.haulmont.cuba.core.app.events.EntityChangedEvent)1 UUID (java.util.UUID)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Inject (javax.inject.Inject)1 EventListener (org.springframework.context.event.EventListener)1 Component (org.springframework.stereotype.Component)1