DataSource Listeners vs EntityListeners

Hello,

I had to change some values related to EnumList (see below), I chosen to use addItemChangeListener, but the problem was even on the screen the changes was made they are not persistent in the database. The solution was to choose another Entity Listener: onBeforeUpdate.
Please advice when we should use DataSource Listeners and when Entity Listeners?

invoiceDs.addItemChangeListener(event -> {
    if (item.getCurrency() == InvCurrencyEnum.usd) {
       BigDecimal amount = BigDecimal.ZERO;
       for (InvoiceLine line : linesDs.getItems()) {
            line.setNameline(line.getDescription().getName());
            line.setPriceline(line.getDescription().getPrice());      
               amount = amount.add(line.getPriceline().multiply(line.getQuantity()));

       }
        getItem().setAmount(amount);
    }
});

vs

public void onBeforeUpdate(Invoice entity, EntityManager entityManager) {
    if (entity.getCurrency() == InvCurrencyEnum.usd) {
        BigDecimal amount = BigDecimal.ZERO;
        for (InvoiceLine line : entity.getLines()) {
            line.setNameline(line.getDescription().getName());
            line.setPriceline(line.getDescription().getPrice());
            amount = amount.add(line.getPriceline().multiply(line.getQuantity()));
        }
        entity.setAmount(amount);

Datasource listeners work on UI level in screens. Changes, made by them, may or may not be saved to the database. Entity listeners are invoked on the middle tier when entity is created/updated/deleted in the database.

So Datasource listeners is a purely UI thing, entity listeners is a purely backend thing.

Datasource listeners work on UI level in screens. Changes, made by them, may or may not be saved to the database…what do you mean? Is not normal to save the changes (in database) when I press OK button?

My problem is that when I use Entity Listeners I cannot see the price changes on the screen (only if I close the window and enter again). Is like in UniqueNumbersAPI case.

On the other hand if I use CollectionChangeListener it works only when I make some changes in priceList. If I enter again on the same (invoice) record and change ONLY enum (usd with eur or vice-versa) nothing is commited in database (only in UI screen).

enum is part of Invoice-edit.xml.
Below the code:

invoiceLineDs.addCollectionChangeListener(event -> {

                Invoice item = invoiceDs.getItem();

                if (item.getCurrency() == InvCurrencyEnum.usd) {

                    BigDecimal amount = BigDecimal.ZERO;
                    for (InvoiceLine line : invoiceLineDs.getItems()) {

                        line.setNameline(line.getDescription().getName());
                        line.setPriceline(line.getDescription().getPrice());
                        amount = amount.add(line.getPriceline().multiply(line.getQuantity()));
                    }
                    item.setAmount(amount);

                } else if (item.getCurrency() == InvCurrencyEnum.eur) {
                    BigDecimal d = new BigDecimal(1.2);
                    BigDecimal amount = BigDecimal.ZERO;
                    for (InvoiceLine line : invoiceLineDs.getItems()) {
                        BigDecimal x = line.getDescription().getPrice();
                        line.setNameline(line.getDescription().getName());
                        line.setPriceline(line.getDescription().getPrice());
                        line.setPriceline(x.divide(d, 2, RoundingMode.CEILING))
                        amount = amount.add(line.getPriceline().multiply(line.getQuantity()));
                    }
                    item.setAmount(amount);
                }
            });

Changes that you make in the edited entity instance should be saved when the editor screen is committed.

You are right. The problem is these changes are made If I close and re-open the Invoice editor.
I don’t know how to avoid this.

We would take a look if you provide a small test project demonstrating your problem.

I’ve tried but unfortunately is too large :slight_smile: