001: package com.completex.objective.persistency.examples.ex003.app;
002:
003: import com.completex.objective.components.persistency.*;
004: import com.completex.objective.components.persistency.core.impl.BasicLifeCycleController;
005: import com.completex.objective.components.persistency.type.TracingArrayList;
006: import com.completex.objective.components.persistency.type.TracingArrayListCollectionFactory;
007: import com.completex.objective.components.persistency.type.ArrayListCollectionFactory;
008: import com.completex.objective.persistency.examples.ex003.domain.Order;
009: import com.completex.objective.persistency.examples.ex003.domain.OrderItem;
010:
011: import java.util.List;
012: import java.util.HashMap;
013:
014: /**
015: * @author Gennady Krizhevsky
016: */
017: public class OrderDAO {
018: private Persistency persistency;
019:
020: public OrderDAO(Persistency persistency) {
021: this .persistency = persistency;
022: }
023:
024: public void insertOrder(final Order order) throws OrderException {
025: try {
026: persistency.insert(order);
027: insertOrderItems(order);
028: } catch (OdalPersistencyException e) {
029: throw new OrderException(e);
030: }
031: }
032:
033: private void insertOrderItems(final Order order)
034: throws OdalPersistencyException {
035: List orderItems = order.getOrderItems();
036: if (orderItems != null) {
037: for (int i = 0; i < orderItems.size(); i++) {
038: OrderItem orderItem = (OrderItem) orderItems.get(i);
039: orderItem.setOrderId(order.getOrderId());
040: persistency.insert(orderItem);
041: }
042: }
043: }
044:
045: public void updateOrder(final Order order) throws OrderException {
046: try {
047: persistency.update(order);
048: } catch (OdalPersistencyException e) {
049: throw new OrderException(e);
050: }
051: }
052:
053: public void updateOrderDetails(final Order order)
054: throws OrderException {
055: try {
056: List rels = order.getOrderItems();
057: persistency.update(rels);
058: } catch (OdalPersistencyException e) {
059: throw new OrderException(e);
060: }
061: }
062:
063: public Order loadOrder(final Long orderId) throws OrderException {
064: try {
065: Order order = (Order) persistency.load(new Order(orderId));
066: return loadOrderDetails(order);
067: } catch (OdalPersistencyException e) {
068: throw new OrderException(e);
069: }
070: }
071:
072: public Order loadOrderDetails(final Order order)
073: throws OrderException {
074: try {
075: OrderItem rel = new OrderItem();
076: rel.setOrderId(order.getOrderId());
077: Query query = persistency.getQueryFactory()
078: .newQueryByExample(rel);
079: query
080: .setMultipleResultFactory(TracingArrayListCollectionFactory
081: .factory());
082: TracingArrayList contactRels = (TracingArrayList) persistency
083: .select(query);
084: contactRels.setTrace();
085: order.setOrderItems(contactRels);
086: return order;
087: } catch (OdalPersistencyException e) {
088: throw new OrderException(e);
089: }
090: }
091:
092: /**
093: * Example how to "manually" populate order/orderItems tree getting all the orders
094: * for particular customer in specific state into the map.
095: *
096: * The query returns a "flat" structure that contains all the orders and order items of
097: * particular customer that then gets transfomed into the
098: *
099: * @param customerId
100: * @param state
101: * @return
102: * @throws OrderException
103: */
104: public Order[] loadOrders(Long customerId, String state)
105: throws OrderException {
106: try {
107: OrderItem queryOrderItem = new OrderItem();
108: queryOrderItem.setState(state);
109: Order queryOrder = new Order();
110: queryOrder.setCustomerId(customerId);
111: CompoundPersistentObject compoundQuery = new CompoundPersistentObject(
112: new PersistentObject[] { queryOrder, queryOrderItem });
113: Query query = persistency.getQueryFactory()
114: .newQueryByCompoundExample(compoundQuery);
115: query
116: .setMultipleResultFactory(ArrayListCollectionFactory.NULL_ARRAY_LIST_FACTORY);
117: LoadOrdersCtl controller = new LoadOrdersCtl();
118: persistency.select(query, controller);
119: return controller.getOrders();
120: } catch (OdalPersistencyException e) {
121: throw new OrderException(e);
122: }
123: }
124:
125: class LoadOrdersCtl extends BasicLifeCycleController {
126: final HashMap ordersMap = new HashMap();
127:
128: public void afterSelect(
129: AbstractPersistentObject persistentObject) {
130: CompoundPersistentObject compound = (CompoundPersistentObject) persistentObject;
131: Order order = ((Order) compound.compoundEntries()[0]);
132: PersistentObject orderItem = compound.compoundEntries()[1];
133: Order cachedOrder = (Order) ordersMap.get(order
134: .getOrderId());
135: if (cachedOrder == null) {
136: ordersMap.put(order.getOrderId(), order);
137: cachedOrder = order;
138: cachedOrder.unsetTrace();
139: }
140: List orderItems = cachedOrder.getOrderItems();
141: orderItems.add(orderItem);
142: }
143:
144: public Order[] getOrders() {
145: Order[] orders = (Order[]) ordersMap.values().toArray(
146: new Order[ordersMap.size()]);
147: for (int i = 0; i < orders.length; i++) {
148: Order order = orders[i];
149: order.setTrace();
150: }
151: return orders;
152: }
153: }
154:
155: }
|