001: package org.julp.examples;
002:
003: import java.util.*;
004: import org.julp.*;
005: import java.sql.*;
006: import javax.sql.DataSource;
007: import org.julp.util.db.*;
008:
009: public class JulpExamples {
010:
011: public JulpExamples() {
012: //System.setProperty("debug-org.julp.examples.CustomerFactory", "true");
013: //System.setProperty("debug-org.julp.DBServices", "true");
014: }
015:
016: protected String driver = null;
017: protected String dbURL = null;
018: protected String user = null;
019: protected String password = "";
020: protected String connectionOptions = null;
021: protected String filePath = null;
022: protected String example = null;
023: protected SQLScriptExecutor setup = new SQLScriptExecutor();
024:
025: public void findAllProducts() {
026: ProductFactory productFactory = new ProductFactory();
027: productFactory.setNoFullColumnName(true); //hsqldb1.7.1 bug workaround
028: try {
029: productFactory.getDBServices().setDebug(false);
030: System.out
031: .println("\n======================= Re-creating database =======================\n");
032: setup.execute(driver, dbURL, user, password,
033: connectionOptions, filePath, ";");
034: System.out
035: .println("\n======================= Running example ============================\n");
036: productFactory.setDataSource(getDataSource());
037: productFactory.getDBServices().setDebug(true);
038: productFactory.findAllProducts();
039: } catch (Exception e) {
040: e.printStackTrace();
041: throw new RuntimeException(e);
042: } finally {
043: try {
044: productFactory.getDBServices().release(true);
045: } catch (SQLException sqle) {
046: sqle.printStackTrace();
047: throw new RuntimeException(sqle);
048: }
049: }
050: }
051:
052: public void getProductPages() {
053: ProductFactory productFactory = new ProductFactory();
054: productFactory.setNoFullColumnName(true); //hsqldb1.7.1 bug workaround
055: try {
056: productFactory.getDBServices().setDebug(false);
057: System.out
058: .println("\n======================= Re-creating database =======================\n");
059: setup.execute(driver, dbURL, user, password,
060: connectionOptions, filePath, ";");
061: System.out
062: .println("\n======================= Running example ============================\n");
063: productFactory.setDataSource(getDataSource());
064: productFactory.getDBServices().setDebug(true);
065: productFactory.getProductPages();
066: } catch (Exception e) {
067: e.printStackTrace();
068: throw new RuntimeException(e);
069: } finally {
070: try {
071: productFactory.getDBServices().release(true);
072: } catch (SQLException sqle) {
073: sqle.printStackTrace();
074: throw new RuntimeException(sqle);
075: }
076: }
077: }
078:
079: public void findAllCustomers() {
080: CustomerFactory customerFactory = new CustomerFactory();
081: customerFactory.setRequestor(Customer.class);
082: customerFactory.setMapping(customerFactory
083: .loadMappings("Customer.properties"));
084: customerFactory.setNoFullColumnName(true); //hsqldb1.7.1 bug workaround
085: try {
086: customerFactory.getDBServices().setDebug(false);
087: System.out
088: .println("\n======================= Re-creating database =======================\n");
089: setup.execute(driver, dbURL, user, password,
090: connectionOptions, filePath, ";");
091: System.out
092: .println("\n======================= Running example ============================\n");
093: customerFactory.setDataSource(getDataSource());
094: customerFactory.getDBServices().setDebug(true);
095: customerFactory.findAllCustomers();
096: } catch (Exception e) {
097: e.printStackTrace();
098: throw new RuntimeException(e);
099: } finally {
100: try {
101: customerFactory.getDBServices().release(true);
102: } catch (SQLException sqle) {
103: sqle.printStackTrace();
104: throw new RuntimeException(sqle);
105: }
106: }
107: }
108:
109: public void findCustomer() {
110: CustomerFactory customerFactory = new CustomerFactory();
111: customerFactory.setRequestor(Customer.class);
112: customerFactory.setMapping(customerFactory
113: .loadMappings("Customer.properties"));
114: customerFactory.setNoFullColumnName(true); //hsqldb1.7.1 bug workaround
115: try {
116: customerFactory.getDBServices().setDebug(false);
117: System.out
118: .println("\n======================= Re-creating database =======================\n");
119: setup.execute(driver, dbURL, user, password,
120: connectionOptions, filePath, ";");
121: System.out
122: .println("\n======================= Running example ============================\n");
123: customerFactory.setDataSource(getDataSource());
124: customerFactory.getDBServices().setDebug(true);
125: customerFactory.findCustomer();
126: } catch (Exception e) {
127: e.printStackTrace();
128: throw new RuntimeException(e);
129: } finally {
130: try {
131: customerFactory.getDBServices().release(true);
132: } catch (SQLException sqle) {
133: sqle.printStackTrace();
134: throw new RuntimeException(sqle);
135: }
136: }
137: }
138:
139: public void findAllCustomersWithPhone() {
140: CustomerFactory customerFactory = new CustomerFactory();
141: customerFactory.setNoFullColumnName(true); //hsqldb1.7.1 bug workaround
142: try {
143: customerFactory.getDBServices().setDebug(false);
144: System.out
145: .println("\n======================= Re-creating database =======================\n");
146: setup.execute(driver, dbURL, user, password,
147: connectionOptions, filePath, ";");
148: System.out
149: .println("\n======================= Running example ============================\n");
150: //customerFactory.setDBServices(getDBServices());
151: customerFactory.setDataSource(getDataSource());
152: customerFactory.getDBServices().setDebug(true);
153: customerFactory.findAllCustomersWithPhone();
154: } catch (Exception e) {
155: e.printStackTrace();
156: throw new RuntimeException(e);
157: } finally {
158: try {
159: customerFactory.getDBServices().release(true);
160: } catch (SQLException sqle) {
161: sqle.printStackTrace();
162: throw new RuntimeException(sqle);
163: }
164: }
165: }
166:
167: public void createAndStoreProductsLater() {
168: ProductFactory productFactory = new ProductFactory();
169: productFactory.setNoFullColumnName(true); //hsqldb1.7.1 bug workaround
170: try {
171: productFactory.getDBServices().setDebug(false);
172: System.out
173: .println("\n======================= Re-creating database =======================\n");
174: setup.execute(driver, dbURL, user, password,
175: connectionOptions, filePath, ";");
176: System.out
177: .println("\n======================= Running example ============================\n");
178: productFactory.setDataSource(getDataSource());
179: productFactory.getDBServices().setDebug(true);
180: productFactory.createAndStoreProductsLater();
181: } catch (Exception e) {
182: e.printStackTrace();
183: throw new RuntimeException(e);
184: } finally {
185: try {
186: productFactory.getDBServices().release(true);
187: } catch (SQLException sqle) {
188: sqle.printStackTrace();
189: throw new RuntimeException(sqle);
190: }
191: }
192: }
193:
194: public void createAndStoreCustomers() {
195: CustomerFactory customerFactory = new CustomerFactory();
196: customerFactory.setNoFullColumnName(true); //hsqldb1.7.1 bug workaround
197: try {
198: customerFactory.getDBServices().setDebug(false);
199: setup.execute(driver, dbURL, user, password,
200: connectionOptions, filePath, ";");
201: customerFactory.setDataSource(getDataSource());
202: customerFactory.getDBServices().setDebug(true);
203: customerFactory.createAndStoreCustomers();
204: } catch (Exception e) {
205: e.printStackTrace();
206: throw new RuntimeException(e);
207: } finally {
208: try {
209: customerFactory.getDBServices().release(true);
210: } catch (SQLException sqle) {
211: sqle.printStackTrace();
212: throw new RuntimeException(sqle);
213: }
214: }
215: }
216:
217: public void modifyInvoices() {
218: SpreadsheetFactory spreadsheetFactory = new SpreadsheetFactory();
219: spreadsheetFactory.setNoFullColumnName(true); //hsqldb1.7.1 bug workaround
220: try {
221: spreadsheetFactory.getDBServices().setDebug(false);
222: System.out
223: .println("\n======================= Re-creating database =======================\n");
224: setup.execute(driver, dbURL, user, password,
225: connectionOptions, filePath, ";");
226: System.out
227: .println("\n======================= Running example ============================\n");
228: spreadsheetFactory.setDataSource(getDataSource());
229: spreadsheetFactory.getDBServices().setDebug(true);
230: spreadsheetFactory.modifyInvoices();
231: } catch (Exception e) {
232: e.printStackTrace();
233: throw new RuntimeException(e);
234: } finally {
235: try {
236: spreadsheetFactory.getDBServices().release(true);
237: } catch (SQLException sqle) {
238: sqle.printStackTrace();
239: throw new RuntimeException(sqle);
240: }
241: }
242: }
243:
244: public void spreadsheet() {
245: SpreadsheetFactory spreadsheetFactory = new SpreadsheetFactory();
246: spreadsheetFactory.setNoFullColumnName(true); //hsqldb1.7.1 bug workaround
247: try {
248: spreadsheetFactory.getDBServices().setDebug(false);
249: System.out
250: .println("\n======================= Re-creating database =======================\n");
251: setup.execute(driver, dbURL, user, password,
252: connectionOptions, filePath, ";");
253: System.out
254: .println("\n======================= Running example ============================\n");
255: spreadsheetFactory.setDataSource(getDataSource());
256: spreadsheetFactory.getDBServices().setDebug(true);
257: spreadsheetFactory.findAllCustomerInvoices();
258: } catch (Exception e) {
259: e.printStackTrace();
260: throw new RuntimeException(e);
261: } finally {
262: try {
263: spreadsheetFactory.getDBServices().release(true);
264: } catch (SQLException sqle) {
265: sqle.printStackTrace();
266: throw new RuntimeException(sqle);
267: }
268: }
269: }
270:
271: public void inClause() {
272: CustomerFactory factory = new CustomerFactory();
273: factory.setReadOnly(true);
274: try {
275: factory.getDBServices().setDebug(false);
276: System.out
277: .println("\n======================= Re-creating database =======================\n");
278: setup.execute(driver, dbURL, user, password,
279: connectionOptions, filePath, ";");
280: System.out
281: .println("\n======================= Running example ============================\n");
282: factory.setDataSource(getDataSource());
283: factory.getDBServices().setDebug(true);
284: factory.inClause();
285: } catch (Exception e) {
286: e.printStackTrace();
287: throw new RuntimeException(e);
288: } finally {
289: try {
290: factory.getDBServices().release(true);
291: } catch (SQLException sqle) {
292: sqle.printStackTrace();
293: throw new RuntimeException(sqle);
294: }
295: }
296: }
297:
298: public void selectAs() {
299: CustomerFactory factory = new CustomerFactory();
300: factory.setReadOnly(true);
301: try {
302: factory.getDBServices().setDebug(false);
303: System.out
304: .println("\n======================= Re-creating database =======================\n");
305: setup.execute(driver, dbURL, user, password,
306: connectionOptions, filePath, ";");
307: System.out
308: .println("\n======================= Running example ============================\n");
309: factory.setDataSource(getDataSource());
310: factory.selectAs();
311: } catch (Exception e) {
312: e.printStackTrace();
313: throw new RuntimeException(e);
314: } finally {
315: try {
316: factory.getDBServices().release(true);
317: } catch (SQLException sqle) {
318: sqle.printStackTrace();
319: throw new RuntimeException(sqle);
320: }
321: }
322: }
323:
324: // public void tableModel(){
325: // CustomerFactory factory = new CustomerFactory();
326: // factory.setMapping(factory.loadMappings("Customer.properties"));
327: // try{
328: // factory.getDBServices().setDebug(false);
329: // System.out.println("\n======================= Re-creating database =======================\n");
330: // setup.execute(driver, dbURL, user, password, connectionOptions, filePath, ";");
331: // System.out.println("\n======================= Running example ============================\n");
332: // getDBServices().setDebug(true);
333: // factory.setDBServices(getDBServices());
334: // JulpTableModelExample modelExample = new JulpTableModelExample();
335: // modelExample.setFactory(factory);
336: // modelExample.setVisible(true);
337: // }catch (Exception e){
338: // e.printStackTrace();
339: // throw new RuntimeException(e);
340: // }finally{
341: // try{
342: // factory.getDBServices().release(true);
343: // }catch (SQLException sqle){
344: // sqle.printStackTrace();
345: // throw new RuntimeException(sqle);
346: // }
347: // }
348: // }
349:
350: protected DataSource getDataSource() {
351:
352: BasicDataSourceImpl dataSource = new BasicDataSourceImpl();
353: dataSource.setUserName(user);
354: dataSource.setPassword(password);
355: dataSource.setDriverName(driver);
356: dataSource.setDbURL(dbURL);
357: Properties prop = new Properties();
358: prop.setProperty("user", user);
359: prop.setProperty("password", password);
360: if (connectionOptions != null
361: && connectionOptions.trim().length() > 0) {
362: StringTokenizer st = new StringTokenizer(connectionOptions,
363: ",", false);
364: while (st.hasMoreTokens()) {
365: String token = st.nextToken();
366: int idx = token.indexOf("=");
367: if (idx == -1) {
368: throw new IllegalArgumentException(
369: "Invalid optional connection properties format. \nIt must have format: name1=value1, name2=value2, ...");
370: }
371: String name = token.substring(0, idx);
372: String value = token.substring(idx + 1);
373: prop.setProperty(name, value);
374: }
375: }
376:
377: dataSource.setConnectionProperties(prop);
378:
379: return dataSource;
380: }
381:
382: /*
383: protected DBServices getDBServices(){
384: DBServices dbServices = new DBServices();
385: Properties prop = new Properties();
386: try{
387: prop.setProperty("user", user);
388: prop.setProperty("password", password);
389: if (connectionOptions != null && connectionOptions.trim().length() > 0){
390: StringTokenizer st = new StringTokenizer(connectionOptions, ",", false);
391: while (st.hasMoreTokens()){
392: String token = st.nextToken();
393: int idx = token.indexOf("=");
394: if (idx == -1){
395: throw new IllegalArgumentException("Invalid optional connection properties format. \nIt must have format: name1=value1, name2=value2, ...");
396: }
397: String name = token.substring(0, idx);
398: String value = token.substring(idx + 1);
399: prop.setProperty(name, value);
400: }
401: }
402:
403: BasicDataSourceImpl dataSource = new BasicDataSourceImpl();
404: dataSource.setUserName(user);
405: dataSource.setPassword(password);
406: dataSource.setDriverName(driver);
407: dataSource.setDbURL(dbURL);
408: dataSource.setConnectionProperties(prop);
409: dbServices.setDataSource(dataSource);
410: }catch (Exception e) {
411: e.printStackTrace();
412: throw new RuntimeException(e);
413: }
414: return dbServices;
415: }
416: */
417:
418: /** Getter for property connectionOptions.
419: * @return Value of property connectionOptions.
420: *
421: */
422: public java.lang.String getConnectionOptions() {
423: return connectionOptions;
424: }
425:
426: /** Setter for property connectionOptions.
427: * @param connectionOptions New value of property connectionOptions.
428: *
429: */
430: public void setConnectionOptions(java.lang.String connectionOptions) {
431: this .connectionOptions = connectionOptions;
432: }
433:
434: /** Getter for property dbURL.
435: * @return Value of property dbURL.
436: *
437: */
438: public java.lang.String getDbURL() {
439: return dbURL;
440: }
441:
442: /** Setter for property dbURL.
443: * @param dbURL New value of property dbURL.
444: *
445: */
446: public void setDbURL(java.lang.String dbURL) {
447: this .dbURL = dbURL;
448: }
449:
450: /** Getter for property driver.
451: * @return Value of property driver.
452: *
453: */
454: public java.lang.String getDriver() {
455: return driver;
456: }
457:
458: /** Setter for property driver.
459: * @param driver New value of property driver.
460: *
461: */
462: public void setDriver(java.lang.String driver) {
463: this .driver = driver;
464: }
465:
466: /** Getter for property example.
467: * @return Value of property example.
468: *
469: */
470: public java.lang.String getExample() {
471: return example;
472: }
473:
474: /** Setter for property example.
475: * @param example New value of property example.
476: *
477: */
478: public void setExample(java.lang.String example) {
479: this .example = example;
480: }
481:
482: /** Getter for property filePath.
483: * @return Value of property filePath.
484: *
485: */
486: public java.lang.String getFilePath() {
487: return filePath;
488: }
489:
490: /** Setter for property filePath.
491: * @param filePath New value of property filePath.
492: *
493: */
494: public void setFilePath(java.lang.String filePath) {
495: this .filePath = filePath;
496: }
497:
498: /** Getter for property password.
499: * @return Value of property password.
500: *
501: */
502: public java.lang.String getPassword() {
503: return password;
504: }
505:
506: /** Setter for property password.
507: * @param password New value of property password.
508: *
509: */
510: public void setPassword(java.lang.String password) {
511: this .password = password;
512: }
513:
514: /** Getter for property user.
515: * @return Value of property user.
516: *
517: */
518: public java.lang.String getUser() {
519: return user;
520: }
521:
522: /** Setter for property user.
523: * @param user New value of property user.
524: *
525: */
526: public void setUser(java.lang.String user) {
527: this.user = user;
528: }
529:
530: }
|