Hi good people,
I need your help in designing a scenario I have…
I have a person registration screen (Person Entity)
Invoice screen (Invoice Entity)
Invoice lines (Invoice Lines Entity)
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
shiryaeva
(Olga Shiryaeva)
September 4, 2018, 8:29am
#3
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:
I’m just getting started with Cuba Platform and Studio, so this may be a pretty basic question. I have two entities (Visit and Mse) in a one-to-one relationship (with Visit owning). When I open a screen to create a new Visit, I would like to be...
I am trying to create an associated entity from the browse screen of the parent entity. I have an entity, Activity, which is the parent entity has many Contact Attempt entities associated. I want to be able to create a Contact Attempt for the...
What Entity properties, when accessed through CUBA Studio may fix this problem? If not through CUBA, what will I need to add to my Java Class for the related Entities. I guess I don’t understand what cascade PERSIST really is. IllegalStateException:...
2 Likes
neutrino36
(neutrino neutrino)
January 9, 2020, 8:01am
#4
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