001: package org.julp.examples;
002:
003: import java.util.*;
004: import org.julp.*;
005: import java.sql.*;
006:
007: public class ProductFactory extends org.julp.DomainObjectFactory
008: implements java.io.Serializable, Cloneable {
009:
010: public ProductFactory() {
011: this .setRequestor(Product.class);
012: /* IT IS NOT NESSESARY TO LOAD MAPPINGS THIS WAY, COULD BE ANYTHING: XML, JNDI, DATABASE, ETC... */
013: setMapping(loadMappings("Product.properties"));
014: sqlMap = loadMappings("Product.sql");
015: }
016:
017: protected Properties sqlMap = null;
018:
019: public Properties loadMappings(String path) {
020: java.io.InputStream inStream = null;
021: Properties props = new Properties();
022: try {
023: inStream = this .getClass().getResourceAsStream(path);
024: props.load(inStream);
025: } catch (java.io.IOException ioe) {
026: throw new RuntimeException(ioe);
027: } finally {
028: try {
029: inStream.close();
030: } catch (java.io.IOException ioe) {
031: throw new RuntimeException(ioe);
032: }
033: }
034: return props;
035: }
036:
037: public int findAllProducts() {
038: int records = 0;
039: try {
040: records = this .load(this .dbServices.getResultSet(sqlMap
041: .getProperty("findAllProducts")));
042: printAllProducts();
043: } catch (SQLException sqle) {
044: throw new RuntimeException(sqle);
045: }
046: return records;
047: }
048:
049: public void createAndStoreProductsLater() {
050: this .setGenerateSQLOnly(true);
051: int records = findAllProducts();
052: Product product = new Product();
053: product.setProductId(new Integer(records + 1)); // this is NOT proper way to genarate id !!!
054: product.setName("Zaurus SL-5600");
055: product.setPrice(299.98);
056: product.setComments("Good deal!");
057: this .create(product);
058: System.out.println("\n=============== Created product: "
059: + product + "\n");
060:
061: /* another way:
062: product.create();
063: product.setObjectId(this.getNextObjectId());
064: this.getObjectList().add(product);
065: or:
066: product.create();
067: this.setObject(product);
068: */
069:
070: /*
071: Product productToRemove = (Product) this.objectList.get(0);
072: this.remove(productToRemove);
073: System.out.println("\nRemoved objects: " + this.getRemovedObjects() + "\n");
074: // another way:
075: //((Product) this.objectList.get(0)).remove();
076: */
077: java.text.NumberFormat nf = java.text.NumberFormat
078: .getInstance();
079: nf.setMaximumFractionDigits(2);
080: ListIterator li = this .objectList.listIterator();
081: while (li.hasNext()) {
082: Product productToUpdate = (Product) li.next();
083: double currentPrice = productToUpdate.getPrice();
084: if (currentPrice < 10) {
085: double newPrice = currentPrice * 1.1;
086: productToUpdate.setPrice(Double.parseDouble(nf
087: .format(newPrice)));
088: productToUpdate.store();
089: }
090: }
091:
092: //System.out.println("\n======================= this is after data modifications ===========================\n");
093: //printAllProducts();
094: /*
095: try{
096: this.dbServices.beginTran();
097: boolean success = this.writeData();
098: Throwable t = this.getWhatIsWrong();
099: if (t != null){
100: throw t;
101: }
102: if (success){
103: this.dbServices.commitTran();
104: }else{
105: throw new SQLException("Data modification: failed");
106: }
107: this.synchronizePersistentState()
108: }catch (Throwable t){
109: try{
110: this.dbServices.rollbackTran();
111: }catch (SQLException sqle){
112: sqle.printStackTrace();
113: throw new RuntimeException(sqle);
114: }
115: t.printStackTrace();
116: }finally{
117: try{
118: this.dbServices.release(true);
119: }catch (SQLException sqle){
120: sqle.printStackTrace();
121: throw new RuntimeException(sqle);
122: }
123: }
124: */
125:
126: String generatedSQLasXML = null;
127: List generatedSQL = null;
128: try {
129: boolean success = this .writeData();
130: Throwable t = this .getWhatIsWrong();
131: if (t != null) {
132: throw t;
133: }
134: if (!success) {
135: throw new SQLException("Data modification: failed");
136: }
137: // make sure you get this values BEFORE synchronizePersistentState()
138: generatedSQLasXML = this .getGeneratedSQLasXML();
139: generatedSQL = this .getGeneratedSQL();
140: this .synchronizePersistentState();
141: } catch (Throwable t) {
142: t.printStackTrace();
143: } finally {
144: try {
145: this .dbServices.release(true);
146: } catch (SQLException sqle) {
147: sqle.printStackTrace();
148: throw new RuntimeException(sqle);
149: }
150: }
151:
152: System.out
153: .println("\n======================= this generated XML which can be sent to another Application/ApplicationServer to update database ===========================\n");
154: System.out.println(generatedSQLasXML);
155: System.out
156: .println("\n======================= this generated stetements and parameters which can be sent to another Application/ApplicationServer to update database ===========================\n");
157: Iterator it = generatedSQL.iterator();
158: while (it.hasNext()) {
159: Object[] obj = (Object[]) it.next();
160: Object[] params = (Object[]) obj[1];
161: System.out.println("sql: " + obj[0] + " params: "
162: + Arrays.asList(params));
163: }
164: }
165:
166: public void getProductPages() {
167: System.out
168: .println("\n======================= this is all products ===========================\n");
169: findAllProducts();
170: this .setPageSize(10);
171: PageHolder page = this .getPage(1);
172: System.out.println("\n=============== Total records: "
173: + page.getObjectsTotal() + ", Page "
174: + page.getPageNumber() + " of " + page.getPagesTotal()
175: + " ===============\n");
176: Iterator iter1 = page.getPage().iterator();
177: while (iter1.hasNext()) {
178: Product product = (Product) iter1.next();
179: System.out.println(product);
180: }
181:
182: PageHolder thirdPage = this .getPage(3);
183: System.out.println("\n=============== Total records: "
184: + thirdPage.getObjectsTotal() + ", Page "
185: + thirdPage.getPageNumber() + " of "
186: + thirdPage.getPagesTotal() + " ===============\n");
187: Iterator iter2 = thirdPage.getPage().iterator();
188: while (iter2.hasNext()) {
189: Product product = (Product) iter2.next();
190: System.out.println(product);
191: }
192: }
193:
194: protected void printAllProducts() {
195: List products = this .getObjectList();
196: Iterator productsIter = products.iterator();
197: while (productsIter.hasNext()) {
198: Product product = (Product) productsIter.next();
199: System.out.println(product);
200: }
201: }
202: }
|