001: package com.completex.objective.persistency.examples.ex004.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.ex004.GenDescriptors;
009: import com.completex.objective.persistency.examples.ex004.GenObjects;
010: import com.completex.objective.persistency.examples.ex004.domain.Contact;
011: import com.completex.objective.persistency.examples.ex004.domain.CpxCustomer;
012: import com.completex.objective.persistency.examples.ex004.domain.Customer;
013:
014: import java.io.IOException;
015: import java.sql.SQLException;
016: import java.util.ArrayList;
017: import java.util.List;
018:
019: /**
020: * @author Gennady Krizhevsky
021: */
022: public class BusinessApp {
023:
024: public static final Log logger = StdErrorLogAdapter
025: .newLogInstance();
026: private TransactionManager transactionManager;
027:
028: public static final Long QUANTITY_ONE = new Long(1);
029:
030: //
031:
032: String[][] orgNames = new String[][] {
033: { "Washington Police", "washington.police.com" },
034: { "Beck Taxi", "beck.taxi.com" },
035: { "Airport Taxi", "airport.taxi.com" }, };
036:
037: int nameCounter;
038: Customer[] customers;
039: Contact[] contacts;
040: private CustomerDAO customerDAO;
041:
042: public BusinessApp(String configPath) throws IOException {
043: init(configPath);
044: }
045:
046: void init(String configPath) throws IOException {
047: DefaultPersistencyAdapter persistency = new DefaultPersistencyAdapter(
048: configPath);
049: transactionManager = persistency.getTransactionManager();
050: customerDAO = new CustomerDAO(persistency);
051: }
052:
053: CpxCustomer createCustomer(String orgName, String url) {
054: CpxCustomer customer = new CpxCustomer();
055: customer.setOrgName(orgName);
056: customer.setUrl(url);
057: return customer;
058: }
059:
060: Contact createContact() {
061: nameCounter++;
062: Contact contact = new Contact();
063: contact.setFirstName("FirstName" + nameCounter);
064: contact.setLastName("LastName" + nameCounter);
065: contact.setPhone("1-800-111-1111");
066: contact
067: .setShipAddress("475 LENFANT PLZ SW RM 10022 WASHINGTON DC 20260-00"
068: + nameCounter);
069: return contact;
070: }
071:
072: public static Log getLogger() {
073: return BusinessApp.logger;
074: }
075:
076: void createAllCustomers() throws CustomerException {
077: info("Enter BusinessApp::createAllCustomers");
078: ArrayList customers = new ArrayList();
079: for (int i = 0; i < orgNames.length; i++) {
080: String orgName = orgNames[i][0];
081: String url = orgNames[i][1];
082: CpxCustomer customer = createCustomer(orgName, url);
083: Contact contact = createContact();
084: customer.setContact(contact);
085: customers.add(customer);
086: customerDAO.insertCustomer(customer);
087: }
088: this .customers = (Customer[]) customers
089: .toArray(new Customer[customers.size()]);
090: }
091:
092: void updateAllCustomers() throws CustomerException {
093: info("Enter BusinessApp::updateAllCustomers");
094:
095: List customers = customerDAO.loadAllCustomers();
096: for (int i = 0; i < customers.size(); i++) {
097: CpxCustomer cpxCustomer = (CpxCustomer) customers.get(i);
098: cpxCustomer.getContact().setFirstName(
099: cpxCustomer.getContact().getFirstName() + " Jr");
100: customerDAO.updateCustomer(cpxCustomer);
101: }
102: }
103:
104: void deleteAllCustomers() throws CustomerException {
105: info("Enter BusinessApp::deleteAllCustomers");
106:
107: List customers = customerDAO.loadAllCustomers();
108: for (int i = 0; i < customers.size(); i++) {
109: CpxCustomer cpxCustomer = (CpxCustomer) customers.get(i);
110: customerDAO.deleteCustomer(cpxCustomer);
111: }
112: }
113:
114: private void info(String message) {
115: BusinessApp.getLogger().info(
116: "======================================");
117: BusinessApp.getLogger().info(message);
118: BusinessApp.getLogger().info(
119: "--------------------------------------");
120: }
121:
122: Transaction begin() throws SQLException {
123: return transactionManager.begin();
124: }
125:
126: void commit(Transaction transaction) throws SQLException {
127: transactionManager.commit(transaction);
128: }
129:
130: public static void main(String[] args) throws SQLException,
131: CustomerException, IOException {
132: GenDescriptors.createTables();
133: BusinessApp app = new BusinessApp(GenObjects.configPath);
134: Transaction transaction = app.begin();
135: //
136: // Do business stuff:
137: //
138: app.createAllCustomers();
139: app.updateAllCustomers();
140: app.deleteAllCustomers();
141: //
142: // Commit:
143: //
144: app.commit(transaction);
145: }
146:
147: }
|