Auto-Create An Entity With Creation of A Different Entity

Hi good people,

I need your help in designing a scenario I have…

  1. I have a person registration screen (Person Entity)
  2. Invoice screen (Invoice Entity)
  3. Invoice lines (Invoice Lines Entity)
  4. Inventory screens (Inventory Entity)

On inventories, I have the following attributes name, cost with a line item called registration fee - 100/-

What I need to achieve…

On new registration of person, I need to automatically create an invoice for the registration fee for that person.

I just need to refresh my creativity on how I can achieve this.

Let me here your thoughts.

Regards,
Kenince

Hi Kenince,

New entity instances can be created with metadata.create() method, for example, in the postCommit() method of the entity editor.

For example:

person-edit.xml

. . .
    <dsContext>
        <datasource id="personDs"
                    class="com.company.demo.entity.Person"
                    view="person-full">
            <collectionDatasource id="invoicesDs"
                                  property="invoices">
                <collectionDatasource id="linesDs"
                                      property="lines"/>
            </collectionDatasource>
        </datasource>
    </dsContext>
. . . 

PersonEdit.java

public class PersonEdit extends AbstractEditor<Person> {
    @Inject
    private Metadata metadata;
    @Inject
    private Datasource<Person> personDs;

    private boolean isNew = false;
    private static final String REG_FEE = "Registration fee";
    private static final BigDecimal REG_FEE_COST = BigDecimal.valueOf(100);

    @Override
    protected void initNewItem(Person item) {
        isNew = true;
    }

    private void addRegistrationFee(Person person) {
        Invoice invoice = metadata.create(Invoice.class);
        invoice.setDate(new Date());
        invoice.setPerson(person);

        InvoiceLine line = metadata.create(InvoiceLine.class);
        line.setFee(REG_FEE);
        line.setCost(REG_FEE_COST);
        line.setInvoice(invoice);

        List<InvoiceLine> lines = new ArrayList<>();
        lines.add(line);
        invoice.setLines(lines);

        List<Invoice> invoiceList = new ArrayList<>();
        invoiceList.add(invoice);
        personDs.getItem().setInvoices(invoiceList);
        personDs.commit();
    }

    @Override
    protected boolean postCommit(boolean committed, boolean close) {
        if (isNew && committed)
            addRegistrationFee(getItem());
        return super.postCommit(committed, close);
    }
}

I’ve created a small demo app that illustrates this approach: demo.zip (121.1 KB)

I would also recommend taking a look at the Modeling Problem Domain section of our cookbook and the related forum topics:

2 Likes

Hello,

This solution works only if we want to create new instances (from Edit Entity x create automatically a new record in Entity y). But how can we proceed if we want to Edit the newly created Entity x (some specific attribute)? It must also reflected in (the same) Entity y.

Regards,
-n