Source Code Cross Referenced for CustomerFactory.java in  » Database-ORM » ProjectJulp » org » julp » examples » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » ProjectJulp » org.julp.examples 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.julp.examples;
002:
003:        import java.util.*;
004:        import org.julp.*;
005:        import org.julp.misc.search.*;
006:        import java.sql.*;
007:
008:        public class CustomerFactory extends org.julp.DomainObjectFactory
009:                implements  java.io.Serializable, Cloneable {
010:
011:            protected Properties sqlMap = null;
012:
013:            public CustomerFactory() {
014:                /* IT IS NOT NECESSARY TO LOAD MAPPINGS THIS WAY, COULD BE ANYTHING: XML, JNDI, DATABASE, ETC... */
015:                sqlMap = loadMappings("Customer.sql");
016:                initFactory();
017:                setRequestor(Customer.class);
018:            }
019:
020:            protected void initFactory() {
021:                try {
022:                    BasicDataSourceImpl ds = new BasicDataSourceImpl();
023:                    ds.setDriverName("org.hsqldb.jdbcDriver");
024:                    ds.setDbURL("jdbc:hsqldb:hsql://localhost");
025:                    ds.setUserName("sa");
026:                    ds.setPassword("");
027:                    this .dbServices = new DBServices();
028:                    dbServices.setDataSource(ds);
029:                    setMapping(loadMappings("Customer.properties"));
030:                } catch (Exception e) {
031:                    e.printStackTrace();
032:                }
033:            }
034:
035:            public Properties loadMappings(String path) {
036:                java.io.InputStream inStream = null;
037:                Properties props = new Properties();
038:                try {
039:                    inStream = this .getClass().getResourceAsStream(path);
040:                    props.load(inStream);
041:                } catch (java.io.IOException ioe) {
042:                    throw new RuntimeException(ioe);
043:                } finally {
044:                    try {
045:                        inStream.close();
046:                    } catch (java.io.IOException ioe) {
047:                        throw new RuntimeException(ioe);
048:                    }
049:                }
050:                return props;
051:            }
052:
053:            public void selectAs() {
054:                System.out
055:                        .println("\n======================= this is an example of using some methods to populate collections ===========================\n");
056:                try {
057:                    setMapping(loadMappings("Customer.properties"));
058:                    String sql = sqlMap
059:                            .getProperty("findAllCustomersWithPhone");
060:                    System.out
061:                            .println("============== as ValueObjects list (first column value and second column display) ==================");
062:                    List list = dbServices.getResultAsValueObjectList(sql);
063:                    System.out.println(list);
064:                    System.out
065:                            .println("============== as Vector of Vectors ==================");
066:                    Vector vector1 = dbServices.getResultAsVector(sql);
067:                    System.out.println(vector1);
068:                    System.out
069:                            .println("============== as Map of Lists (key=column name, value=list of values for the column) ==================");
070:                    Map map = dbServices.getResultAsMap(sql);
071:                    System.out.println(map);
072:                    //System.out.println("============== ==================");
073:                    //Vector vector2 = dbServices.getResultAsVector(sql);
074:                    //System.out.println(vector2);
075:                    System.out
076:                            .println("============== as DataHolders array ==================");
077:
078:                    DataHolder[] dataHolder = dbServices
079:                            .getResultAsDataHoldersArray(sql, false);
080:                    for (int i = 0; i < dataHolder.length; i++) {
081:                        System.out.println(dataHolder[i]);
082:                    }
083:                } catch (Exception e) {
084:                    e.printStackTrace();
085:                    throw new RuntimeException(e);
086:                } finally {
087:                    try {
088:                        dbServices.release(true);
089:                    } catch (Exception ex) {
090:                        ex.printStackTrace();
091:                    }
092:                }
093:            }
094:
095:            public void inClause() {
096:                System.out
097:                        .println("\n======================= this is an example of using 'IN' CLAUSE \n======================= and using SELECT with number of parameters \n======================= more than DBMS can except (like more then 254 in Oracle) to populate objectList ===========================\n");
098:                try {
099:                    dbServices.setMaxNumberOfParams(3);
100:                    dbServices.setPlaceHolder(":#"); // could be any String
101:                    //dbServices.setDebug(true);
102:                    dbServices.setCacheStatements(true);
103:                    setRequestor(Customer.class);
104:                    setMapping(loadMappings("Customer.properties"));
105:
106:                    List argsList = new ArrayList();
107:                    String sql = sqlMap.getProperty("inClause");
108:
109:                    //List city = new ArrayList();
110:                    //city.add("City1");
111:                    //city.add("City2");
112:                    //city.add("City3");
113:                    //
114:                    //List phone = new ArrayList();
115:                    //phone.add("1111111111");
116:                    //phone.add("2222222222");
117:
118:                    List custId = new ArrayList();
119:                    custId.add(new Integer(0));
120:                    custId.add(new Integer(1));
121:                    custId.add(new Integer(2));
122:                    custId.add(new Integer(3));
123:                    custId.add(new Integer(4));
124:                    custId.add(new Integer(5));
125:                    custId.add(new Integer(6));
126:                    custId.add(new Integer(7));
127:
128:                    argsList.add("John Doh");
129:                    //argsList.add(city);
130:                    //argsList.add(phone);
131:                    argsList.add(custId);
132:
133:                    load(dbServices.getResultSets(sql, argsList));
134:
135:                    Iterator it1 = getObjectList().iterator();
136:                    while (it1.hasNext()) {
137:                        System.out.println(it1.next());
138:                    }
139:                    dbServices.release(false);
140:                    System.out.println("==================================");
141:
142:                    List list = dbServices.getResultsAsDataHoldersList(sql,
143:                            argsList, false);
144:
145:                    Iterator it2 = list.iterator();
146:                    while (it2.hasNext()) {
147:                        System.out.println(it2.next());
148:                    }
149:                } catch (Exception e) {
150:                    e.printStackTrace();
151:                    throw new RuntimeException(e);
152:                } finally {
153:                    try {
154:                        dbServices.release(true);
155:                    } catch (Exception ex) {
156:                        ex.printStackTrace();
157:                    }
158:                }
159:            }
160:
161:            public int findAllCustomers() {
162:                int records = 0;
163:                try {
164:                    String sql = sqlMap.getProperty("findAllCustomers");
165:                    records = this .load(this .dbServices.getResultSet(sql));
166:                    printAllCustomers();
167:                } catch (SQLException sqle) {
168:                    throw new RuntimeException(sqle);
169:                }
170:                return records;
171:            }
172:
173:            public int findAllCustomersWithPhone() {
174:                System.out
175:                        .println("\n======================= this is an example of using inheritance ===========================\n");
176:                this .setRequestor(CustomerWithPhone.class);
177:
178:                setMapping(loadMappings("CustomersWithPhone.properties"));
179:
180:                /*or:  comment line above: setMapping(loadMappings("CustomersWithPhone.properties"));
181:                      and uncomment two lines below */
182:
183:                /*
184:                setMapping(loadMappings("Customer.properties"));
185:                this.mapping.put("CUSTOMER.PHONE", "phone");
186:                 */
187:
188:                int records = 0;
189:                try {
190:                    records = this .load(this .dbServices.getResultSet(sqlMap
191:                            .getProperty("findAllCustomersWithPhone")));
192:                    printAllCustomers();
193:                } catch (SQLException sqle) {
194:                    throw new RuntimeException(sqle);
195:                }
196:                return records;
197:            }
198:
199:            public void createAndStoreCustomers() {
200:                System.out
201:                        .println("\n======================= this is an example of creating and storing objects (INSERT & UPDATE) ===========================\n");
202:                this .setRequestor(CustomerWithPhone.class);
203:                setMapping(loadMappings("CustomersWithPhone.properties"));
204:                this .getDBServices().setCacheStatements(true);
205:                int records = findAllCustomers();
206:                CustomerWithPhone customerWithPhone = new CustomerWithPhone();
207:                // this is NOT proper way to genarate id
208:                customerWithPhone.setCustomerId(new Integer(records + 1));
209:                customerWithPhone.setFirstName("Joe");
210:                customerWithPhone.setLastName("Doe");
211:                customerWithPhone.setStreet("Main st.");
212:                customerWithPhone.setCity("SomeTown");
213:                customerWithPhone.setPhone("7777777777");
214:
215:                this .create(customerWithPhone);
216:                System.out.println("\ncreated customer: " + customerWithPhone
217:                        + "\n");
218:
219:                /*       another way:
220:                 customerWithPhone.create();
221:                 customerWithPhone.setObjectId(this.getNextObjectId());
222:                 this.getObjectList().add(product);
223:                 or:
224:                 customerWithPhone.create();
225:                 this.setObject(customerWithPhone);
226:                 */
227:
228:                ListIterator li = this .objectList.listIterator();
229:                while (li.hasNext()) {
230:                    CustomerWithPhone customer = (CustomerWithPhone) li.next();
231:                    String lastName = customer.getLastName();
232:                    if (lastName.equals("Miller")) {
233:                        customer.setPhone("8888888888");
234:                        customer.store();
235:                    }
236:                }
237:
238:                System.out
239:                        .println("\n======================= this is after data modifications ===========================\n");
240:                printAllCustomers();
241:
242:                try {
243:                    this .dbServices.beginTran();
244:                    boolean success = this .writeData();
245:                    Throwable t = this .getWhatIsWrong();
246:                    if (t != null) {
247:                        throw t;
248:                    }
249:                    if (success) {
250:                        this .dbServices.commitTran();
251:                    } else {
252:                        throw new SQLException("Data modification: failed");
253:                    }
254:                    this .synchronizePersistentState();
255:                } catch (Throwable t) {
256:                    try {
257:                        this .dbServices.rollbackTran();
258:                    } catch (SQLException sqle) {
259:                        sqle.printStackTrace();
260:                        throw new RuntimeException(sqle);
261:                    }
262:                    t.printStackTrace();
263:                } finally {
264:                    try {
265:                        this .dbServices.release(true);
266:                    } catch (SQLException sqle) {
267:                        sqle.printStackTrace();
268:                        throw new RuntimeException(sqle);
269:                    }
270:                }
271:
272:                System.out
273:                        .println("\n======================= this is after COMMIT & synchronizePersistentState() or after ROLLBACK ===========================\n");
274:                printAllCustomers();
275:            }
276:
277:            public void findCustomer() {
278:                System.out
279:                        .println("\n======================= this is an example of using Criteria  ===========================\n");
280:                setMapping(loadMappings("Customer.properties"));
281:                this .populateMetaData();
282:                List holders = new ArrayList(2);
283:                SearchCriteriaHolder searchCriteriaHolder1 = new SearchCriteriaHolder();
284:                searchCriteriaHolder1.setFieldName("lastName");
285:                searchCriteriaHolder1.setOperator(SearchCriteriaHolder.LIKE);
286:                searchCriteriaHolder1.setFunctions("UPPER(${})"); //UPPER(col_name)
287:                searchCriteriaHolder1.setSearchValue("MI%");
288:                searchCriteriaHolder1
289:                        .setBooleanCondition(SearchCriteriaHolder.OR);
290:                holders.add(searchCriteriaHolder1);
291:                SearchCriteriaHolder searchCriteriaHolder2 = new SearchCriteriaHolder();
292:                searchCriteriaHolder2.setFieldName("lastName");
293:                searchCriteriaHolder2.setOperator(SearchCriteriaHolder.LIKE);
294:                searchCriteriaHolder2.setFunctions("UPPER(${})");
295:                searchCriteriaHolder2.setSearchValue("S%");
296:                holders.add(searchCriteriaHolder2);
297:                CustomerCriteria criteria = new CustomerCriteria();
298:                criteria.setMetaData(this .metaData);
299:                criteria.setSearchCriteriaHolders(holders);
300:                criteria.buildCriteria();
301:                String select = this .sqlMap.getProperty("findAllCustomers");
302:                criteria.setSelect(select);
303:                String sql = criteria.getQuery();
304:                Collection params = criteria.getArguments();
305:
306:                try {
307:                    this .dbServices.setDebug(true);
308:                    this .load(this .dbServices.getResultSet(sql, params));
309:                } catch (SQLException sqle) {
310:                    throw new RuntimeException(sqle);
311:                }
312:                printAllCustomers();
313:            }
314:
315:            protected void printAllCustomers() {
316:                List products = this .getObjectList();
317:                Iterator iter = products.iterator();
318:                while (iter.hasNext()) {
319:                    Customer customer = (Customer) iter.next();
320:                    System.out.println(customer);
321:                }
322:            }
323:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.