Search in sources :

Example 1 with OrderLine

use of com.company.sales.entity.OrderLine 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

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