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