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