|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Example | Line # 40 | 42 | 0% | 3 | 0 | 100% |
1.0
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
(1) | |||
Result | |||
1.0
|
org.perfectjpattern.jee.integration.dao.spring.TestExample.testSpringGenericDao org.perfectjpattern.jee.integration.dao.spring.TestExample.testSpringGenericDao | 1 PASS | |
1 | //---------------------------------------------------------------------- | |
2 | // | |
3 | // PerfectJPattern: "Design patterns are good but components are better!" | |
4 | // Example.java Copyright (c) 2009 Giovanni Azua Garcia | |
5 | // bravegag@hotmail.com | |
6 | // | |
7 | // This program is free software; you can redistribute it and/or | |
8 | // modify it under the terms of the GNU General Public License | |
9 | // as published by the Free Software Foundation; either version 3 | |
10 | // of the License, or (at your option) any later version. | |
11 | // | |
12 | // This program is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | // | |
17 | // You should have received a copy of the GNU General Public License | |
18 | // along with this program; if not, see <http://www.gnu.org/licenses/>. | |
19 | // | |
20 | //---------------------------------------------------------------------- | |
21 | package org.perfectjpattern.jee.integration.dao.spring; | |
22 | ||
23 | import java.text.*; | |
24 | import java.util.*; | |
25 | ||
26 | import org.perfectjpattern.example.datamodel.*; | |
27 | import org.perfectjpattern.example.datamodel.visitor.*; | |
28 | import org.perfectjpattern.jee.api.integration.dao.*; | |
29 | import org.slf4j.*; | |
30 | import org.springframework.context.support.*; | |
31 | ||
32 | /** | |
33 | * Startup Main for the Hibernate-Spring Generic DAO Pattern Example code | |
34 | * | |
35 | * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a> | |
36 | * @version $ $Date: Dec 5, 2008 2:06:29 AM $ | |
37 | */ | |
38 | //CHECKSTYLE:OFF | |
39 | public final | |
40 | class Example | |
41 | // CHECKSTYLE:ON | |
42 | { | |
43 | //------------------------------------------------------------------------ | |
44 | // public | |
45 | //------------------------------------------------------------------------ | |
46 | 1 | public static void |
47 | main(String[] anArguments) | |
48 | throws ParseException | |
49 | { | |
50 | // ToStringVisitor needs loading a ResourceBundle | |
51 | 1 | Locale.setDefault(new Locale("en", "US")); |
52 | ||
53 | // initialize IoC Spring container | |
54 | 1 | new ClassPathXmlApplicationContext( |
55 | new String[] | |
56 | { | |
57 | "genericDao-applicationContext.xml", | |
58 | "example-applicationContext.xml" | |
59 | }); | |
60 | ||
61 | 1 | try |
62 | { | |
63 | //--------------------------------------------------------------- | |
64 | // Create fixture/ business model | |
65 | //--------------------------------------------------------------- | |
66 | ||
67 | // available products | |
68 | 1 | Product myProduct1 = new Product("Nikon D700", 3200.0); |
69 | 1 | Product myProduct2 = new Product("Nikon D300", 2000.0); |
70 | 1 | Product myProduct3 = new Product("Nikon D40x", 350.0); |
71 | 1 | Product myProduct4 = new Product("Nikon 80-400mm", 1500.0); |
72 | 1 | Product myProduct5 = new Product("Nikon 70-200mm", 1800.0); |
73 | 1 | Product myProduct6 = new Product("Nikon 17-55mm", 1200.0); |
74 | ||
75 | // new customer | |
76 | 1 | Customer myCustomer1 = new Customer("Ernesto"); |
77 | ||
78 | // add customer orders | |
79 | 1 | Date myDate = DATE_FORMAT.parse("14.03.2007 08:00:00"); |
80 | 1 | Set<Product> myProducts = new HashSet<Product>(); |
81 | 1 | myProducts.addAll(Arrays.asList(new Product[] {myProduct1, |
82 | myProduct4, myProduct5 })); | |
83 | 1 | Order myOrder1 = new Order(myCustomer1, myDate, myProducts); |
84 | 1 | myCustomer1.getOrders().add(myOrder1); |
85 | ||
86 | //----------------------------------------------------------- | |
87 | // Accesses the AbstractFactory singleton LocalDaoFactory and | |
88 | // persists Customer model in storage. Note how the | |
89 | // LocalDaoFactory hides the framework-specific complexities. | |
90 | // LocalDaoFactory gets constructed from the Spring | |
91 | // configuration to: | |
92 | // | |
93 | // - HibernateCurrentSessionStrategy: | |
94 | // a) Receives a new SessionFactory via IoC defined in | |
95 | // the Spring configuration | |
96 | // b) Uses the getCurrentSession Hibernate API that | |
97 | // looks into the "current_session_context_class" | |
98 | // configuration, for this example is set to "thread" | |
99 | // | |
100 | // - HibernateConfiguredTransactionStrategy: | |
101 | // Simply accesses the Session Strategy and calls | |
102 | // getTransaction, that will load whatever is configured | |
103 | // in "hibernate.transaction_factory" configuration | |
104 | //----------------------------------------------------------- | |
105 | 1 | ICustomerDao myCustomerDao = LocalDaoFactory. |
106 | getInstance().createCustomerDao(); | |
107 | 1 | myCustomerDao.create(myCustomer1); |
108 | ||
109 | //----------------------------------------------------------- | |
110 | // Commit changes. Note how accessing whatever Transaction | |
111 | // implementation is transparent to the client | |
112 | //----------------------------------------------------------- | |
113 | 1 | myCustomerDao.getTransaction().commit(); |
114 | ||
115 | //----------------------------------------------------------- | |
116 | // Extend fixture/ business model | |
117 | //----------------------------------------------------------- | |
118 | 1 | Customer myCustomer2 = new Customer("Giovanni"); |
119 | ||
120 | // add customer orders | |
121 | 1 | myDate = DATE_FORMAT.parse("17.11.2007 08:00:00"); |
122 | 1 | myProducts = new HashSet<Product>(); |
123 | 1 | myProducts.addAll(Arrays.asList(new Product[] {myProduct1, |
124 | myProduct2, myProduct3 })); | |
125 | 1 | myOrder1 = new Order(myCustomer2, myDate, myProducts); |
126 | 1 | myCustomer2.getOrders().add(myOrder1); |
127 | ||
128 | 1 | myDate = DATE_FORMAT.parse("15.01.2008 17:35:00"); |
129 | 1 | myProducts = new HashSet<Product>(); |
130 | 1 | myProducts.addAll(Arrays.asList(new Product[] {myProduct4, |
131 | myProduct5, myProduct6 })); | |
132 | 1 | Order myOrder2 = new Order(myCustomer2, myDate, myProducts); |
133 | 1 | myCustomer2.getOrders().add(myOrder2); |
134 | ||
135 | //----------------------------------------------------------- | |
136 | // Persist Customer models in storage | |
137 | //----------------------------------------------------------- | |
138 | 1 | myCustomerDao.create(myCustomer2); |
139 | ||
140 | //----------------------------------------------------------- | |
141 | // Commit changes. | |
142 | //----------------------------------------------------------- | |
143 | 1 | myCustomerDao.getTransaction().commit(); |
144 | ||
145 | //----------------------------------------------------------- | |
146 | // Now we can try our new search capabilities | |
147 | //----------------------------------------------------------- | |
148 | 1 | long myMinimum = 2; |
149 | 1 | Date myBegin = DATE_FORMAT.parse("01.01.2007 00:00:00"); |
150 | 1 | Date myEnd = DATE_FORMAT.parse("01.01.2009 00:00:00"); |
151 | 1 | List<Customer> myCustomers = myCustomerDao. |
152 | findByNumberOfOrdersBetween(myBegin, myEnd, myMinimum); | |
153 | ||
154 | 1 | ToStringVisitor myVisitor = new ToStringVisitor(); |
155 | 1 | for (Customer myCustomer : myCustomers) |
156 | { | |
157 | 1 | myVisitor.visit(myCustomer); |
158 | 1 | String myResult = myVisitor.getResult(); |
159 | ||
160 | 1 | theLogger.debug(myResult); |
161 | } | |
162 | } | |
163 | finally | |
164 | { | |
165 | //----------------------------------------------------------- | |
166 | // Final cleanup | |
167 | //----------------------------------------------------------- | |
168 | 1 | LocalDaoFactory.getInstance().shutdown(); |
169 | } | |
170 | } | |
171 | ||
172 | //------------------------------------------------------------------------ | |
173 | // protected | |
174 | //------------------------------------------------------------------------ | |
175 | 1 | protected static void |
176 | setLogger(Logger aLogger) | |
177 | { | |
178 | 1 | theLogger = aLogger; |
179 | } | |
180 | ||
181 | //------------------------------------------------------------------------ | |
182 | // members | |
183 | //------------------------------------------------------------------------ | |
184 | /** | |
185 | * Provides logging facilities for this class | |
186 | */ | |
187 | private static Logger theLogger = LoggerFactory.getLogger(Example.class); | |
188 | private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat( | |
189 | "dd.MM.yyyy HH:mm:ss"); | |
190 | } |
|