Order and OrderLines example

Good morning, it’s my first topic in this forum. I just started working with Cuba Studio so I apologize in advance if the question will seem trivial.

Building a simple relation master detail orders and order lines without using nested datasource, with two indipendent datasources, you can give me an example of how I can access the values of the master entities (orders) in onBeforeInsert section of OrderLinesEntityListener?


@Component("erp_OrderLinesEntityListener")
public class OrderLinesEntityListener implements BeforeInsertEntityListener<OrderLines>
{
    @Override
    public void onBeforeInsert(OrderLines entity, EntityManager entityManager)
    {
        //In this section i would access to the values in Order ...
    }
}

Hi Maurizio,

An entity in a entity listener is in managed state, so you can freely access all related entities just by calling getters, for example:


@Component("sample_OrderLineEntityListener")
public class OrderLineEntityListener implements BeforeInsertEntityListener<OrderLine>, BeforeUpdateEntityListener<OrderLine> {

    @Override
    public void onBeforeInsert(OrderLine entity, EntityManager entityManager) {
        System.out.println("onBeforeInsert: order=" + entity.getOrder());
    }

    @Override
    public void onBeforeUpdate(OrderLine entity, EntityManager entityManager) {
        System.out.println("onBeforeUpdate: order=" + entity.getOrder());
    }
}

I think that when you use standalone datasources for Order and OrderLines, you can have a problem when saving a new line for a newly created order, because the order is not in the database yet. Composition and nested datasources solve this problem automatically because all OrderLines go to the database only together with their Order. In your case, a typical solution is to disable creating OrderLines until a new Order is saved. It can be done as follows:

  • In Order edit screen designer, select extended for windowActions frame. The frame will display 3 buttons: “OK & Close”, “OK”, “Cancel”.

  • Make the OrderLines create action disabled by default.

  • In the screens’s controller, override the postInit() method:


public class OrderEdit extends AbstractEditor<Order> {

    @Named("orderLinesTable.create")
    private CreateAction orderLinesTableCreate;

    @Override
    protected void postInit() {
        // Pass current Order to be set to newly created OrderLines
        orderLinesTableCreate.setInitialValues(ParamsMap.of("order", getItem()));
        // Enable create action if the Order is already saved
        if (!PersistenceHelper.isNew(getItem())) {
            orderLinesTableCreate.setEnabled(true);
        }
        // Disable notification on saving the Order (optionally)
        setShowSaveNotification(false);
    }
}

Thanks for the reply Konstantin. Now my question:

which is the section in the order lines, where I’ll use the map containing values of order passed by the setInitialValues CreateAction?

Please can you send me an example?

Thanks again.

When you do this:


createAction.setInitialValues(ParamsMap.of("order", getItem()));

the action will use the value returned by getItem() (which is the currently edited instance of Order) and assign it to the order attribute of the created OrderLine. See how it works here. So you don’t have to add anything except my code above.