Skip to content
Aleksey Stukalov edited this page Sep 12, 2016 · 9 revisions

Code Snippets

6.2.1 Initialization of a New Record in Editor

Override method initNewItem and set status OrderStatus.NEW to the passed order:

@Override
protected void initNewItem(Order item) {
    super.initNewItem(item);
    item.setStatus(OrderStatus.NEW);
}

6.2.3 Adding Custom Behavior

Inject the ordersDS datasource from our screen

@Inject
private CollectionDatasource<Order, UUID> ordersDs;

onBtnNewStatusClick() method implementation:

public void onBtnSetNewStatusClick(Component source) {
    Order o = ordersDs.getItem();
    if (o != null) {
        o.setStatus(OrderStatus.NEW);
        ordersDs.commit();
    }
}

onBtnSetInProgressStatusClick() method implementation:

public void onBtnSetInProgressStatusClick(Component source) {
    Order o = ordersDs.getItem();
    if (o != null) {
        o.setStatus(OrderStatus.IN_PROGRESS);
        ordersDs.commit();
    }
}

onBtnSetReadyStatusClick() method implementation:

public void onBtnSetReadyStatusClick(Component source) {
    Order o = ordersDs.getItem();
    if (o != null) {
        o.setStatus(OrderStatus.READY);
        ordersDs.commit();
    }
}

6.3 Business Logic Development

caclulateAmount() method implementation:

@Override
public BigDecimal calculateOrderAmount(Order order) {
    BigDecimal amount = new BigDecimal(0);
    if (order.getHoursSpent() != null) {
        amount = amount.add(new BigDecimal(order.getHoursSpent())
                .multiply(order.getMechanic().getHourlyRate()));
    }
    if (order.getParts() != null) {
        for (SparePart part : order.getParts()) {
            amount = amount.add(part.getPrice());
        }
    }
    return amount;
}

Injecting OrderService into OrderEditor:

@Inject
private OrderService orderService;

Overriding the preCommit() method in OrderEditor:

@Override
protected boolean preCommit() {
    getItem().setAmount(orderService.calculateOrderAmount(getItem()));
    return super.preCommit();
}

6.4 REST API

Login request:

http://localhost:8080/app/dispatch/api/login?u=admin&p=admin

Request all orders, which have the NEW(10) OrderStatus:

http://localhost:8080/app/dispatch/api/query.json?e=workshop$Order&q=select+o+from+workshop$Order+o+where+o.status=10&s=[INSERT YOUR SESSION ID FROM THE RESPONSE]