001: package com.completex.objective.persistency.examples.ex005.app;
002:
003: import com.completex.objective.components.log.Log;
004: import com.completex.objective.components.log.adapter.StdErrorLogAdapter;
005: import com.completex.objective.components.persistency.core.adapter.DefaultPersistencyAdapter;
006: import com.completex.objective.components.persistency.transact.Transaction;
007: import com.completex.objective.components.persistency.transact.TransactionManager;
008: import com.completex.objective.persistency.examples.ex005.GenDescriptors;
009: import com.completex.objective.persistency.examples.ex005.GenObjects;
010: import com.completex.objective.persistency.examples.ex005.domain.CpxOrder;
011: import com.completex.objective.persistency.examples.ex005.domain.Order;
012: import com.completex.objective.persistency.examples.ex005.domain.OrderItem;
013:
014: import java.io.IOException;
015: import java.sql.SQLException;
016: import java.util.ArrayList;
017:
018: /**
019: * @author Gennady Krizhevsky
020: */
021: public class BusinessApp {
022:
023: public static final Log logger = StdErrorLogAdapter
024: .newLogInstance();
025: private OrderDAO orderDAO;
026: private TransactionManager transactionManager;
027:
028: public static final Long QUANTITY_ONE = new Long(1);
029: //
030:
031: String[][] orgNames = new String[][] {
032: { "Washington Police", "washington.police.com" },
033: { "Beck Taxi", "beck.taxi.com" },
034: { "Airport Taxi", "airport.taxi.com" }, };
035:
036: int nameCounter;
037: Order[] orders;
038:
039: public BusinessApp(String configPath) throws IOException {
040: init(configPath);
041: }
042:
043: void init(String configPath) throws IOException {
044: DefaultPersistencyAdapter persistency = new DefaultPersistencyAdapter(
045: configPath);
046: transactionManager = persistency.getTransactionManager();
047: orderDAO = new OrderDAO(persistency);
048: }
049:
050: public static Log getLogger() {
051: return com.completex.objective.persistency.examples.ex005.app.BusinessApp.logger;
052: }
053:
054: private CpxOrder createOrder(Long[] productIds, Long parentId)
055: throws OrderException {
056: try {
057: CpxOrder order = new CpxOrder();
058: order.setParentOrderId(parentId);
059: order.setCustomerId(CUSTOMER_ID);
060: addOrderItems(productIds, order);
061: return order;
062: } catch (SQLException e) {
063: throw new OrderException(e);
064: }
065: }
066:
067: private void addOrderItems(Long[] productIds, CpxOrder order)
068: throws SQLException {
069: for (int i = 0; i < productIds.length; i++) {
070: Long productId = productIds[i];
071: OrderItem orderItem = new OrderItem();
072: orderItem.setProductId(productId);
073: orderItem.setQuantity(BusinessApp.QUANTITY_ONE);
074: orderItem.setState(OrderItem.STATE_PENDING);
075: order.addOrderItem(orderItem);
076: }
077: }
078:
079: public static final Long CUSTOMER_ID = new Long(1);
080: public static final Long[] PRODUCT_IDS = new Long[] { new Long(1),
081: new Long(2), };
082:
083: private void createAllOrders() throws OrderException {
084: info("createAllOrders");
085: //
086: // Buy police cars:
087: //
088: ArrayList list = new ArrayList();
089: CpxOrder order;
090: // //
091: // // Alternative way to add order item - begin:
092: // //
093: // try {
094: // order = new CpxOrder();
095: // order.setParentOrderId(null);
096: // order.setCustomerId(CUSTOMER_ID);
097: // orderDAO.insertOrder(order);
098: // addOrderItems(PRODUCT_IDS, order);
099: // orderDAO.updateOrder(order);
100: // } catch (Exception e) {
101: // e.printStackTrace();
102: // }
103: // //
104: // // Alternative way to add order item - end:
105: // //
106:
107: //
108: // 1st order
109: //
110: order = createOrder(PRODUCT_IDS, null);
111: orderDAO.insertOrder(order);
112: list.add(order);
113: //
114: // 2nd order - child of the 1st one:
115: //
116: order = createOrder(PRODUCT_IDS, order.getOrderId());
117: orderDAO.insertOrder(order);
118: list.add(order);
119: //
120: // 3rd order - child of the 2nd one:
121: //
122: order = createOrder(PRODUCT_IDS, order.getOrderId());
123: orderDAO.insertOrder(order);
124: list.add(order);
125:
126: this .orders = (Order[]) list.toArray(new Order[list.size()]);
127: }
128:
129: public void loadAllOrders() throws OrderException {
130: try {
131: info("loadAllOrders");
132: //
133: // Get 1st order
134: //
135: CpxOrder order = orderDAO
136: .loadOrder(orders[orders.length - 1].getOrderId());
137: //
138: // Get 2nd order: Now get the next parent:
139: //
140: order = order.getParentOrder();
141: //
142: // Get 3rd order: Now get the next parent:
143: //
144: order = order.getParentOrder();
145: //
146: // Get the parent of the 3rd order - it will be null
147: // & it does not result in extra query sincethis is the endof object chain:
148: //
149: order = order.getParentOrder();
150: System.err.println("Parent of the 3rd order: " + order);
151: } catch (RuntimeException e) {
152: throw e;
153: } catch (Exception e) {
154: throw new OrderException(e);
155: }
156: }
157:
158: private void info(String message) {
159: com.completex.objective.persistency.examples.ex005.app.BusinessApp
160: .getLogger().info(
161: "======================================");
162: com.completex.objective.persistency.examples.ex005.app.BusinessApp
163: .getLogger().info(message);
164: com.completex.objective.persistency.examples.ex005.app.BusinessApp
165: .getLogger().info(
166: "--------------------------------------");
167: }
168:
169: Transaction begin() throws SQLException {
170: return transactionManager.begin();
171: }
172:
173: void commit(Transaction transaction) throws SQLException {
174: transactionManager.commit(transaction);
175: }
176:
177: public static void main(String[] args) throws SQLException,
178: IOException, OrderException {
179: GenDescriptors.createTables();
180: BusinessApp app = new BusinessApp(GenObjects.configPath);
181: Transaction transaction = app.begin();
182: //
183: // Do business stuff:
184: //
185: app.createAllOrders();
186: app.loadAllOrders();
187: //
188: // Commit:
189: //
190: app.commit(transaction);
191: }
192:
193: }
|