001: package org.julp.examples;
002:
003: import java.util.*;
004: import org.julp.*;
005: import java.sql.*;
006: import java.math.BigDecimal;
007:
008: public class SpreadsheetFactory extends org.julp.DomainObjectFactory
009: implements java.io.Serializable, Cloneable {
010:
011: public SpreadsheetFactory() {
012: this .setRequestor(Spreadsheet.class);
013: /* IT IS NOT NECESSARY TO LOAD MAPPINGS THIS WAY, COULD BE ANYTHING: XML, JNDI, DATABASE, ETC... */
014: setMapping(loadMappings("Spreadsheet.properties"));
015: sqlMap = loadMappings("Spreadsheet.sql");
016: }
017:
018: protected Properties sqlMap = null;
019:
020: public Properties loadMappings(String path) {
021: java.io.InputStream inStream = null;
022: Properties props = new Properties();
023: try {
024: inStream = this .getClass().getResourceAsStream(path);
025: props.load(inStream);
026: } catch (java.io.IOException ioe) {
027: throw new RuntimeException(ioe);
028: } finally {
029: try {
030: inStream.close();
031: } catch (java.io.IOException ioe) {
032: throw new RuntimeException(ioe);
033: }
034: }
035: return props;
036: }
037:
038: public int findAllCustomerInvoices() {
039: int records = 0;
040: try {
041: Collection arg = new ArrayList(1);
042: //arg.add(new Integer(4));
043: //records = this.load(this.dbServices.getResultSet(sqlMap.getProperty("findAll"), arg));
044: records = this .load(this .dbServices.getResultSet(sqlMap
045: .getProperty("findAll")));
046: printSpreadsheet();
047: } catch (SQLException sqle) {
048: throw new RuntimeException(sqle);
049: }
050: return records;
051: }
052:
053: public void modifyInvoices() {
054: // if another user/process UPDATEd or DELETEd rows in database then throw exception
055: this .setOptimisticLock(this .KEY_AND_UPDATEBLE_COLUMNS);
056: this .setThrowOptimisticLockDeleteException(true);
057: this .setThrowOptimisticLockUpdateException(true);
058: Map invoiceTotalMap = new HashMap();
059: Collection arg = new ArrayList(1);
060: arg.add(new Integer(4));
061: try {
062: this .dbServices.setDebug(true);
063: this .dbServices.setCacheStatements(true);
064: this .load(this .dbServices.getResultSet(sqlMap
065: .getProperty("findAllCustomerInvoices"), arg));
066: } catch (SQLException sqle) {
067: throw new RuntimeException(sqle);
068: }
069:
070: // remove some records
071: ListIterator li1 = this .objectList.listIterator();
072: while (li1.hasNext()) {
073: Spreadsheet record = (Spreadsheet) li1.next();
074: Integer invoiceId = record.getInvoiceId();
075: BigDecimal cost = record.getCost();
076: if (cost.doubleValue() > 30.0) {
077: record.remove();
078: } else {
079: BigDecimal invoiceTotal = (BigDecimal) invoiceTotalMap
080: .get(invoiceId);
081: if (invoiceTotal == null) {
082: invoiceTotal = new BigDecimal(0.0);
083: }
084: invoiceTotal = invoiceTotal.add(cost);
085: invoiceTotalMap.put(invoiceId, invoiceTotal);
086: }
087: }
088:
089: System.out
090: .println("\n======================= invoiceTotalMap: \n"
091: + invoiceTotalMap);
092:
093: //System.out.println("\n======================= this is after data modifications ===========================\n");
094: //printSpreadsheet();
095:
096: try {
097: // since Spreadsheet can modify more then one table
098: // we need to specify table name
099: this .setTable("ITEM");
100: this .dbServices.beginTran();
101: boolean success1 = this .writeData();
102: Throwable t1 = this .getWhatIsWrong();
103: if (t1 != null) {
104: throw t1;
105: }
106:
107: // update INVOICE table
108: //update invoices total
109: ListIterator li2 = this .objectList.listIterator();
110: while (li2.hasNext()) {
111: Spreadsheet record = (Spreadsheet) li2.next();
112: Integer invoiceId = record.getInvoiceId();
113: Object total = invoiceTotalMap.get(invoiceId);
114: BigDecimal invoiceTotal = null;
115: if (total == null) {
116: invoiceTotal = new BigDecimal(0);
117: } else {
118: invoiceTotal = (BigDecimal) total;
119: }
120: record.setTotal(invoiceTotal);
121: record.store();
122: }
123: this .setTable("INVOICE");
124: // do not DELETE invoice
125: char[] updateOnly = new char[1];
126: updateOnly[0] = this .DATA_MODIFICATION_SEQUENCE_UPDATE;
127: this .setDataModificationSequence(updateOnly);
128: boolean success2 = this .writeData();
129: Throwable t2 = this .getWhatIsWrong();
130: if (t2 != null) {
131: throw t2;
132: }
133: if (success1 == true && success2 == true) {
134: this .dbServices.commitTran();
135: } else {
136: throw new SQLException("Data modification: failed");
137: }
138: this .synchronizePersistentState();
139: } catch (Throwable t) {
140: try {
141: this .dbServices.rollbackTran();
142: } catch (SQLException sqle) {
143: sqle.printStackTrace();
144: throw new RuntimeException(sqle);
145: }
146: t.printStackTrace();
147: } finally {
148: try {
149: this .dbServices.release(true);
150: } catch (SQLException sqle) {
151: sqle.printStackTrace();
152: throw new RuntimeException(sqle);
153: }
154: }
155:
156: System.out
157: .println("\n======================= this is after COMMIT & synchronizePersistentState() or after ROLLBACK ===========================\n");
158: //printSpreadsheet();
159: }
160:
161: protected void printSpreadsheet() {
162: List rows = this .getObjectList();
163: Iterator iter = rows.iterator();
164: while (iter.hasNext()) {
165: Spreadsheet row = (Spreadsheet) iter.next();
166: System.out.println(row);
167: }
168: }
169: }
|