Alex
(Alex)
February 27, 2019, 11:00am
#1
Taking sample-sales as starting point.
I would like to add a column order_line_nr (persistent) to OrderLine that automatically increments for the Order when creating a new line.
Would be great if it could also handle special cases like re-numbering when a row was deleted.
Is there any suitable Service for this?
It may also be a non-persisent value. Important for me would be to have it somehow generated for the report (without Jasper). Something like “select row_number()…” would be enough, if it’s easier to implement. Persistent would be better though as it would keep the ordering.
I checked these, but didnt find the soluition, it might be there but didn’t get it :
Hello, I have a Microsoft SQL DB. In this database there are two tables with a classic master-details example, a Orders table and a Rows table. When I create a new Order and I add the Order Rows (always in the Order Edit Screen), I realized that...
Hi, i need to put in first Line of InvoiceDetails, Line = 1, but after adding more lines, how can i get the max line and sum 1 ?
Thanks Team!
krivopustov
(Konstantin Krivopustov)
March 5, 2019, 2:42pm
#3
Hi Alex,
I agree that the number attribute should better be persistent.
Add the attribute to OrderLine
entity:
@Entity(name = "sales_OrderLine")
public class OrderLine extends StandardEntity {
@NotNull
@Column(name = "NUM", nullable = false)
protected Integer num;
Add ordering by this attribute to the collection in Order
:
@Entity(name = "sales_Order")
public class Order extends StandardEntity {
@OrderBy("num")
@Composition
@OnDelete(DeletePolicy.CASCADE)
@OneToMany(mappedBy = "order")
protected List<OrderLine> lines;
In OrderEdit
screen, subscribe to CollectionChangeEvent
of the data container and recalculate numbers for all items:
@UiController("sales_Order.edit")
@UiDescriptor("order-edit.xml")
@EditedEntityContainer("orderDc")
@LoadDataBeforeShow
public class OrderEdit extends StandardEditor<Order> {
@Inject
private CollectionContainer<OrderLine> linesDc;
@Subscribe(id = "linesDc", target = Target.DATA_CONTAINER)
protected void onOrderLinesDcCollectionChange(CollectionContainer.CollectionChangeEvent<OrderLine> event) {
if (event.getChangeType() != CollectionChangeType.REFRESH) {
calculateAmount();
recalculateLineNumbers();
}
}
private void recalculateLineNumbers() {
int num = 1;
for (OrderLine orderLine : linesDc.getItems()) {
orderLine.setNum(num++);
}
}
That’s all. On each adding/removing of an order line, all lines will get correct numbers.
3 Likes