01: package com.completex.objective.persistency.examples.ex003a.app;
02:
03: import com.completex.objective.components.persistency.Link;
04: import com.completex.objective.components.persistency.OdalPersistencyException;
05: import com.completex.objective.components.persistency.Persistency;
06: import com.completex.objective.components.persistency.Query;
07: import com.completex.objective.persistency.examples.ex003a.domain.CpxOrder;
08: import com.completex.objective.persistency.examples.ex003a.domain.OrderItem;
09:
10: import java.util.Collection;
11:
12: /**
13: * @author Gennady Krizhevsky
14: */
15: public class OrderDAO {
16: private Persistency persistency;
17:
18: public OrderDAO(Persistency persistency) {
19: this .persistency = persistency;
20: }
21:
22: public void insertOrder(final CpxOrder order)
23: throws com.completex.objective.persistency.examples.ex003a.app.OrderException {
24: try {
25: persistency.insert(order);
26: } catch (OdalPersistencyException e) {
27: throw new OrderException(e);
28: }
29: }
30:
31: public void updateOrder(final CpxOrder order) throws OrderException {
32: try {
33: persistency.update(order);
34: } catch (OdalPersistencyException e) {
35: throw new OrderException(e);
36: }
37: }
38:
39: public CpxOrder loadOrder(final Long orderId) throws OrderException {
40: try {
41: return (CpxOrder) persistency.load(new CpxOrder(orderId));
42: } catch (OdalPersistencyException e) {
43: throw new OrderException(e);
44: }
45: }
46:
47: /**
48: * Load orders with only order items in certain state and having only those order items in the
49: * order items collection
50: */
51: public CpxOrder[] loadOrders(Long customerId, String state)
52: throws OrderException {
53: try {
54: OrderItem qbeOrderItem = new OrderItem();
55: qbeOrderItem.setState(state);
56: CpxOrder qbeOrder = new CpxOrder();
57: qbeOrder.setCustomerId(customerId);
58: qbeOrder.addOrderItem(qbeOrderItem);
59: Query query = persistency.getQueryFactory()
60: .newQueryByExample(qbeOrder);
61: // Inline order items query to retrieve everything in one SQL statement:
62: query.getChild(CpxOrder.CPX_CHILD_ORDER_ITEM)
63: .setInlineMode(Link.INNER_JOIN_MODE);
64: Collection orders = persistency.select(query);
65: return (CpxOrder[]) orders.toArray(new CpxOrder[orders
66: .size()]);
67: } catch (OdalPersistencyException e) {
68: throw new OrderException(e);
69: }
70: }
71:
72: }
|