001: package com.completex.objective.persistency.examples.ex003a.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.ex003a.GenDescriptors;
009: import com.completex.objective.persistency.examples.ex003a.GenObjects;
010: import com.completex.objective.persistency.examples.ex003a.domain.*;
011:
012: import java.io.IOException;
013: import java.math.BigDecimal;
014: import java.sql.SQLException;
015: import java.util.ArrayList;
016: import java.util.List;
017:
018: /**
019: * @author Gennady Krizhevsky
020: */
021: public class BusinessApp {
022:
023: public static final Log logger = StdErrorLogAdapter
024: .newLogInstance();
025: private CustomerDAO customerDAO;
026: private ProductDAO productDAO;
027: private OrderDAO orderDAO;
028: private TransactionManager transactionManager;
029:
030: public static final Long QUANTITY_ONE = new Long(1);
031: //
032: // Available colours:
033: //
034: public static final String COLOUR_RED = "red";
035: public static final String COLOUR_BLUE = "blue";
036: public static final String COLOUR_BLACK = "black";
037: public static final String COLOUR_YELLOW = "yellow";
038: public static final String COLOUR_WHITE = "white";
039:
040: //
041: // Available makes:
042: //
043: public static final String MAKE_FORD = "ford";
044: public static final String MAKE_CHEVROLET = "chevrolet";
045: //
046: // Available models:
047: public static final String MODEL_CW = "crown victoria";
048: public static final String MODEL_IMPLALA = "implala";
049: //
050:
051: String[][] orgNames = new String[][] {
052: { "Washington Police", "washington.police.com" },
053: { "Beck Taxi", "beck.taxi.com" },
054: { "Airport Taxi", "airport.taxi.com" }, };
055:
056: int nameCounter;
057: Customer[] customers;
058: Contact[] contacts;
059: Order[] orders;
060:
061: private Product[] products;
062:
063: public BusinessApp(String configPath) throws IOException {
064: init(configPath);
065: }
066:
067: void init(String configPath) throws IOException {
068: DefaultPersistencyAdapter persistency = new DefaultPersistencyAdapter(
069: configPath);
070: transactionManager = persistency.getTransactionManager();
071: customerDAO = new CustomerDAO(persistency);
072: productDAO = new ProductDAO(persistency);
073: orderDAO = new OrderDAO(persistency);
074: }
075:
076: CpxCustomer createCustomer(String orgName, String url) {
077: CpxCustomer customer = new CpxCustomer();
078: customer.setOrgName(orgName);
079: customer.setUrl(url);
080: return customer;
081: }
082:
083: Contact createContact(Long customerId) {
084: nameCounter++;
085: Contact contact = new Contact();
086: contact.setCustomerId(customerId);
087: contact.setFirstName("FirstName" + nameCounter);
088: contact.setLastName("LastName" + nameCounter);
089: contact.setPhone("1-800-111-1111");
090: contact
091: .setShipAddress("475 LENFANT PLZ SW RM 10022 WASHINGTON DC 20260-00"
092: + nameCounter);
093: return contact;
094: }
095:
096: /**
097: * For now you have to set all the parameters if you add them through update
098: *
099: * @param contactId
100: * @param type
101: * @return CustomerContactRel
102: */
103: CustomerContactRel createContactRel(Long contactId, String type) {
104: return new CustomerContactRel(contactId, type, null);
105: }
106:
107: public static Log getLogger() {
108: return com.completex.objective.persistency.examples.ex003a.app.BusinessApp.logger;
109: }
110:
111: void createAllCustomers() throws CustomerException {
112: try {
113: info("Enter BusinessApp::createAllCustomers");
114: ArrayList customers = new ArrayList();
115: ArrayList contacts = new ArrayList();
116: for (int i = 0; i < orgNames.length; i++) {
117: String orgName = orgNames[i][0];
118: String url = orgNames[i][1];
119: CpxCustomer customer = createCustomer(orgName, url);
120: customers.add(customer);
121: customerDAO.insertCustomer(customer);
122:
123: Contact contact = createContact(customer
124: .getCustomerId());
125: contacts.add(contact);
126: customerDAO.insertContact(contact);
127:
128: for (int j = 0; j < CustomerContactRel.SUPPORTED_TYPES.length - 1; j++) {
129: String contactType = CustomerContactRel.SUPPORTED_TYPES[j];
130: CustomerContactRel rel = createContactRel(contact
131: .getContactId(), contactType);
132: customer.addContactRel(rel);
133: }
134: customerDAO.updateCustomer(customer);
135: }
136: this .customers = (Customer[]) customers
137: .toArray(new Customer[customers.size()]);
138: this .contacts = (Contact[]) contacts
139: .toArray(new Contact[contacts.size()]);
140: } catch (RuntimeException e) {
141: throw e;
142: } catch (Exception e) {
143: throw new CustomerException(e);
144: }
145: }
146:
147: private void addBillingContacts() throws CustomerException {
148: info("Enter BusinessApp::addBillingContacts");
149:
150: try {
151: for (int i = 0; i < customers.length; i++) {
152: Customer createdCustomer = customers[i];
153: Long customerId = createdCustomer.getCustomerId();
154: CpxCustomer customer = (CpxCustomer) customerDAO
155: .loadCustomer(customerId);
156:
157: Contact[] contacts = customerDAO
158: .loadCustomerContacts(customerId);
159: customer.addContactRel(createContactRel(contacts[0]
160: .getContactId(), CustomerContactRel.BILL));
161: customerDAO.updateCustomer(customer);
162: }
163:
164: } catch (RuntimeException e) {
165: throw e;
166: } catch (Exception e) {
167: throw new CustomerException(e);
168: }
169: }
170:
171: public void createAllProducts() throws ProductException {
172: info("Enter BusinessApp::createAllProducts");
173: Product product;
174: //
175: // Police cars:
176: //
177: product = createProduct(
178: PoliceCar.TYPE,
179: com.completex.objective.persistency.examples.ex003a.app.BusinessApp.MAKE_CHEVROLET,
180: com.completex.objective.persistency.examples.ex003a.app.BusinessApp.MODEL_IMPLALA,
181: com.completex.objective.persistency.examples.ex003a.app.BusinessApp.COLOUR_BLUE,
182: "police car attrs", 22000, 16000);
183: productDAO.insertProduct(product);
184: product = createProduct(
185: PoliceCar.TYPE,
186: com.completex.objective.persistency.examples.ex003a.app.BusinessApp.MAKE_CHEVROLET,
187: com.completex.objective.persistency.examples.ex003a.app.BusinessApp.MODEL_IMPLALA,
188: com.completex.objective.persistency.examples.ex003a.app.BusinessApp.COLOUR_BLACK,
189: "police car attrs", 22000, 16000);
190: productDAO.insertProduct(product);
191: product = createProduct(
192: PoliceCar.TYPE,
193: com.completex.objective.persistency.examples.ex003a.app.BusinessApp.MAKE_FORD,
194: com.completex.objective.persistency.examples.ex003a.app.BusinessApp.MODEL_CW,
195: com.completex.objective.persistency.examples.ex003a.app.BusinessApp.COLOUR_WHITE,
196: "police car attrs", 28000, 20000);
197: productDAO.insertProduct(product);
198: //
199: // Taxis:
200: //
201: product = createProduct(
202: TaxiCar.TYPE,
203: com.completex.objective.persistency.examples.ex003a.app.BusinessApp.MAKE_CHEVROLET,
204: com.completex.objective.persistency.examples.ex003a.app.BusinessApp.MODEL_IMPLALA,
205: com.completex.objective.persistency.examples.ex003a.app.BusinessApp.COLOUR_YELLOW,
206: "taxi car attrs", 21000, 15000);
207: productDAO.insertProduct(product);
208: product = createProduct(
209: TaxiCar.TYPE,
210: com.completex.objective.persistency.examples.ex003a.app.BusinessApp.MAKE_CHEVROLET,
211: com.completex.objective.persistency.examples.ex003a.app.BusinessApp.MODEL_IMPLALA,
212: com.completex.objective.persistency.examples.ex003a.app.BusinessApp.COLOUR_RED,
213: "taxi car attrs", 21000, 15000);
214: productDAO.insertProduct(product);
215: product = createProduct(
216: TaxiCar.TYPE,
217: com.completex.objective.persistency.examples.ex003a.app.BusinessApp.MAKE_CHEVROLET,
218: com.completex.objective.persistency.examples.ex003a.app.BusinessApp.MODEL_IMPLALA,
219: com.completex.objective.persistency.examples.ex003a.app.BusinessApp.COLOUR_BLUE,
220: "taxi car attrs", 21000, 15000);
221: productDAO.insertProduct(product);
222: }
223:
224: private Product createProduct(String type, String make,
225: String model, String colour, String attrs, int price,
226: int cost) {
227: if (PoliceCar.TYPE.equals(type)) {
228: CpdPoliceCar policeCar = new CpdPoliceCar();
229: populateProductAttrs(policeCar, price, cost, type, make,
230: model, colour);
231: policeCar.setPoliceCarAttr(attrs);
232: return policeCar;
233: } else if (TaxiCar.TYPE.equals(type)) {
234: CpdTaxiCar taxiCar = new CpdTaxiCar();
235: populateProductAttrs(taxiCar, price, cost, type, make,
236: model, colour);
237: taxiCar.setTaxiCarAttr(attrs);
238: return taxiCar;
239: } else {
240: throw new RuntimeException("Unsupported type " + type);
241: }
242: }
243:
244: private void populateProductAttrs(Product product, int price,
245: int cost, String type, String make, String model,
246: String colour) {
247: product.setPrice(new BigDecimal(price));
248: product.setCost(new BigDecimal(cost));
249: product.setName(type);
250: product.setMake(make);
251: product.setModel(model);
252: product.setColour(colour);
253: }
254:
255: private CpxOrder createOrder(Long customerId, Long[] productIds)
256: throws OrderException {
257: try {
258: CpxOrder order = new CpxOrder();
259: order.setCustomerId(customerId);
260: for (int i = 0; i < productIds.length; i++) {
261: Long productId = productIds[i];
262: OrderItem orderItem = new OrderItem();
263: orderItem.setProductId(productId);
264: orderItem.setQuantity(BusinessApp.QUANTITY_ONE);
265: orderItem.setState(OrderItem.STATE_PENDING);
266: order.addOrderItem(orderItem);
267: }
268: return order;
269: } catch (RuntimeException e) {
270: throw e;
271: } catch (Exception e) {
272: throw new OrderException(e);
273: }
274: }
275:
276: public void createAllOrders() throws OrderException {
277: info("createAllOrders");
278: //
279: // Buy police cars:
280: //
281: ArrayList list = new ArrayList();
282: CpxOrder order;
283: order = createOrder(customers[0].getCustomerId(), new Long[] {
284: products[0].getProductId(), products[1].getProductId(),
285: products[2].getProductId(), });
286: orderDAO.insertOrder(order);
287: list.add(order);
288:
289: order = createOrder(customers[1].getCustomerId(),
290: new Long[] { products[3].getProductId(),
291: products[4].getProductId(),
292: products[5].getProductId(),
293: products[0].getProductId(), });
294: orderDAO.insertOrder(order);
295: list.add(order);
296: this .orders = (Order[]) list.toArray(new Order[list.size()]);
297: }
298:
299: public void modifyPendingOrders() throws OrderException {
300: try {
301: CpxOrder[] orders = orderDAO.loadOrders(customers[1]
302: .getCustomerId(), OrderItem.STATE_PENDING);
303: CpxOrder order = orders[0];
304: List orderItems = order.getOrderItems();
305: for (int i = 0; i < orderItems.size(); i++) {
306: OrderItem orderItem = (OrderItem) orderItems.get(i);
307: if (PoliceCar.TYPE.equals(productType(orderItem
308: .getProductId()))) {
309: orderItems.remove(orderItem);
310: }
311: }
312: orderDAO.updateOrder(order);
313: } catch (RuntimeException e) {
314: throw e;
315: } catch (Exception e) {
316: throw new OrderException(e);
317: }
318: }
319:
320: public void fulfillOrders() throws OrderException {
321: try {
322: CpxOrder[] orders = orderDAO.loadOrders(customers[1]
323: .getCustomerId(), OrderItem.STATE_PENDING);
324: for (int i = 0; i < orders.length; i++) {
325: CpxOrder order = orders[i];
326: List orderItems = order.getOrderItems();
327: for (int j = 0; j < orderItems.size(); j++) {
328: OrderItem orderItem = (OrderItem) orderItems.get(j);
329: orderItem.setState(OrderItem.STATE_FULFILLED);
330: }
331: orderDAO.updateOrder(order);
332: }
333: } catch (RuntimeException e) {
334: throw e;
335: } catch (Exception e) {
336: throw new OrderException(e);
337: }
338: }
339:
340: public String productType(Long productId) {
341: for (int i = 0; i < products.length; i++) {
342: Product product = products[i];
343: if (productId.equals(product.getProductId())) {
344: return product.getName();
345: }
346: }
347: return null;
348: }
349:
350: public void loadAllProducts() throws ProductException {
351: products = productDAO.loadAllProducts();
352: }
353:
354: private void info(String message) {
355: com.completex.objective.persistency.examples.ex003a.app.BusinessApp
356: .getLogger().info(
357: "======================================");
358: com.completex.objective.persistency.examples.ex003a.app.BusinessApp
359: .getLogger().info(message);
360: com.completex.objective.persistency.examples.ex003a.app.BusinessApp
361: .getLogger().info(
362: "--------------------------------------");
363: }
364:
365: Transaction begin() throws SQLException {
366: return transactionManager.begin();
367: }
368:
369: void commit(Transaction transaction) throws SQLException {
370: transactionManager.commit(transaction);
371: }
372:
373: public static void main(String[] args) throws SQLException,
374: CustomerException, IOException, ProductException,
375: OrderException {
376: GenDescriptors.createTables();
377: com.completex.objective.persistency.examples.ex003a.app.BusinessApp app = new com.completex.objective.persistency.examples.ex003a.app.BusinessApp(
378: GenObjects.configPath);
379: Transaction transaction = app.begin();
380: //
381: // Do business stuff:
382: //
383: app.createAllCustomers();
384: app.addBillingContacts();
385: app.createAllProducts();
386: app.loadAllProducts();
387: app.createAllOrders();
388: app.modifyPendingOrders();
389: app.fulfillOrders();
390: //
391: // Commit:
392: //
393: app.commit(transaction);
394: }
395:
396: }
|