1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package org.perfectjpattern.jee.integration.dao; |
22 |
|
|
23 |
|
import java.io.*; |
24 |
|
import java.util.*; |
25 |
|
|
26 |
|
import javax.persistence.*; |
27 |
|
|
28 |
|
import org.apache.commons.lang.*; |
29 |
|
import org.perfectjpattern.jee.api.integration.dao.*; |
30 |
|
|
31 |
|
|
32 |
|
@link |
33 |
|
@link |
34 |
|
|
35 |
|
|
36 |
|
@author |
37 |
|
@version |
38 |
|
|
39 |
|
@SuppressWarnings("hiding") |
40 |
|
public |
|
|
| 100% |
Uncovered Elements: 0 (78) |
Complexity: 26 |
Complexity Density: 0.45 |
|
41 |
|
class JpaBaseDao<Id extends Serializable, Element> |
42 |
|
implements IBaseDao<Id, Element> |
43 |
|
{ |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
@inheritDoc |
49 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
50 |
4
|
public boolean ... |
51 |
|
contains(Element anElement) |
52 |
|
throws DaoException, IllegalArgumentException |
53 |
|
{ |
54 |
4
|
Validate.notNull(anElement, "'anElement' must not be null"); |
55 |
|
|
56 |
4
|
boolean myExists = false; |
57 |
4
|
try |
58 |
|
{ |
59 |
4
|
ISession mySession = getSession(); |
60 |
|
|
61 |
4
|
myExists = mySession.contains(anElement); |
62 |
|
|
63 |
4
|
return myExists; |
64 |
|
} |
65 |
|
|
66 |
|
catch (RuntimeException anException) |
67 |
|
|
68 |
|
{ |
69 |
|
throw new DaoException(anException); |
70 |
|
} |
71 |
|
} |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
@inheritDoc |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
77 |
14
|
@SuppressWarnings("unchecked")... |
78 |
|
public int |
79 |
|
count() |
80 |
|
throws DaoException |
81 |
|
{ |
82 |
14
|
int myCount = 0; |
83 |
|
|
84 |
14
|
try |
85 |
|
{ |
86 |
14
|
ISession mySession = getSession(); |
87 |
|
|
88 |
14
|
IQuery myQuery = mySession.createQuery("select count(e) " + |
89 |
|
"from " + thePersistentClass.getSimpleName() + " e"); |
90 |
|
|
91 |
14
|
myCount = ((Long) myQuery.getSingleResult()).intValue(); |
92 |
|
|
93 |
14
|
return myCount; |
94 |
|
} |
95 |
|
|
96 |
|
catch (RuntimeException anException) |
97 |
|
|
98 |
|
{ |
99 |
|
anException.printStackTrace(); |
100 |
|
|
101 |
|
throw new DaoException(anException); |
102 |
|
} |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
@inheritDoc |
108 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
109 |
4
|
@SuppressWarnings("unchecked")... |
110 |
|
public Element |
111 |
|
findById(Id anId) |
112 |
|
throws DaoException, IllegalArgumentException |
113 |
|
{ |
114 |
4
|
Validate.notNull(anId, "'anId' must not be null"); |
115 |
|
|
116 |
4
|
Element myElement = null; |
117 |
4
|
try |
118 |
|
{ |
119 |
4
|
ISession mySession = getSession(); |
120 |
|
|
121 |
4
|
myElement = (Element) mySession.find(getPersistentClass(), anId); |
122 |
|
|
123 |
4
|
return myElement; |
124 |
|
} |
125 |
|
|
126 |
|
catch (RuntimeException anException) |
127 |
|
|
128 |
|
{ |
129 |
|
throw new DaoException(anException); |
130 |
|
} |
131 |
|
} |
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
@inheritDoc |
136 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
137 |
9
|
@SuppressWarnings("unchecked")... |
138 |
|
public List<Element> |
139 |
|
findAll() |
140 |
|
throws DaoException |
141 |
|
{ |
142 |
9
|
List<Element> myElements = null; |
143 |
|
|
144 |
9
|
try |
145 |
|
{ |
146 |
9
|
ISession mySession = getSession(); |
147 |
|
|
148 |
9
|
myElements = mySession.createQuery("select e from " + |
149 |
|
getPersistentClass().getSimpleName() + " e").getResultList(); |
150 |
|
|
151 |
9
|
return myElements; |
152 |
|
} |
153 |
|
|
154 |
|
catch (RuntimeException anException) |
155 |
|
|
156 |
|
{ |
157 |
|
throw new DaoException(anException); |
158 |
|
} |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
@inheritDoc |
164 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
165 |
16
|
@SuppressWarnings("unchecked")... |
166 |
|
public Id |
167 |
|
create(Element anElement) |
168 |
|
throws DaoException, IllegalArgumentException |
169 |
|
{ |
170 |
16
|
Validate.notNull(anElement, "'anElement' must not be null"); |
171 |
|
|
172 |
16
|
Id myId = null; |
173 |
|
|
174 |
16
|
try |
175 |
|
{ |
176 |
16
|
ISession mySession = getSession(); |
177 |
|
|
178 |
16
|
myId = (Id) mySession.persist(anElement); |
179 |
|
|
180 |
16
|
return myId; |
181 |
|
} |
182 |
|
|
183 |
|
catch (RuntimeException anException) |
184 |
|
|
185 |
|
{ |
186 |
|
throw new DaoException(anException); |
187 |
|
} |
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
@inheritDoc |
193 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
194 |
4
|
public boolean ... |
195 |
|
update(Element anElement) |
196 |
|
throws DaoException |
197 |
|
{ |
198 |
4
|
Validate.notNull(anElement, "'anElement' must not be null"); |
199 |
|
|
200 |
4
|
try |
201 |
|
{ |
202 |
4
|
ISession mySession = getSession(); |
203 |
|
|
204 |
4
|
mySession.update(anElement); |
205 |
|
|
206 |
4
|
return true; |
207 |
|
} |
208 |
|
|
209 |
|
catch (RuntimeException anException) |
210 |
|
|
211 |
|
{ |
212 |
|
throw new DaoException(anException); |
213 |
|
} |
214 |
|
} |
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
@inheritDoc |
219 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
220 |
2
|
public boolean ... |
221 |
|
delete(Element anElement) |
222 |
|
throws DaoException |
223 |
|
{ |
224 |
2
|
Validate.notNull(anElement, "'anElement' must not be null"); |
225 |
|
|
226 |
2
|
try |
227 |
|
{ |
228 |
2
|
ISession mySession = getSession(); |
229 |
|
|
230 |
2
|
mySession.remove(anElement); |
231 |
|
|
232 |
2
|
return true; |
233 |
|
} |
234 |
|
|
235 |
|
catch (RuntimeException anException) |
236 |
|
|
237 |
|
{ |
238 |
|
throw new DaoException(anException); |
239 |
|
} |
240 |
|
} |
241 |
|
|
242 |
|
|
243 |
|
|
244 |
|
@inheritDoc |
245 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
246 |
2
|
public void ... |
247 |
|
deleteAll() |
248 |
|
throws DaoException |
249 |
|
{ |
250 |
2
|
try |
251 |
|
{ |
252 |
2
|
ISession mySession = getSession(); |
253 |
|
|
254 |
2
|
mySession.createQuery("delete from " + getPersistentClass(). |
255 |
|
getName()).executeUpdate(); |
256 |
|
} |
257 |
|
|
258 |
|
catch (RuntimeException anException) |
259 |
|
|
260 |
|
{ |
261 |
|
throw new DaoException(anException); |
262 |
|
} |
263 |
|
} |
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
@inheritDoc |
268 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
269 |
71
|
public ITransaction... |
270 |
|
getTransaction() |
271 |
|
{ |
272 |
71
|
return theTransactionStrategy.getTransaction(); |
273 |
|
} |
274 |
|
|
275 |
|
|
276 |
|
|
277 |
|
@inheritDoc |
278 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
279 |
64
|
public ISession ... |
280 |
|
getSession() |
281 |
|
{ |
282 |
64
|
if (!isManaged()) |
283 |
|
{ |
284 |
58
|
ITransaction myTransaction = getTransaction(); |
285 |
58
|
if (!myTransaction.isActive()) |
286 |
|
{ |
287 |
17
|
myTransaction.begin(); |
288 |
|
} |
289 |
|
} |
290 |
|
|
291 |
64
|
return theSessionStrategy.getSession(); |
292 |
|
} |
293 |
|
|
294 |
|
|
295 |
|
|
296 |
|
|
297 |
|
|
298 |
|
@link |
299 |
|
@link |
300 |
|
@link@link |
301 |
|
@link |
302 |
|
|
303 |
|
@param |
304 |
|
@param |
305 |
|
@param |
306 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
307 |
7
|
protected... |
308 |
|
JpaBaseDao(Class<Element> aPersistentClass, |
309 |
|
ISessionStrategy aSessionStrategy, |
310 |
|
ITransactionStrategy aTransactionStrategy) |
311 |
|
throws IllegalArgumentException |
312 |
|
{ |
313 |
|
assert aPersistentClass != null : "'aPersistentClass' must not be null"; |
314 |
|
assert !aPersistentClass.isInterface() : |
315 |
|
"'aPersistentClass' must be a class type"; |
316 |
|
assert aSessionStrategy != null : |
317 |
|
"'aDaoSessionStrategy' must not be null"; |
318 |
|
|
319 |
7
|
thePersistentClass = aPersistentClass; |
320 |
7
|
theSessionStrategy = aSessionStrategy; |
321 |
7
|
theTransactionStrategy = aTransactionStrategy; |
322 |
|
} |
323 |
|
|
324 |
|
|
325 |
|
|
326 |
|
@link |
327 |
|
@link@link |
328 |
|
|
329 |
|
@param@link |
330 |
|
@throws |
331 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
332 |
2
|
protected void... |
333 |
|
setEntityManager(EntityManager anEntityManager) |
334 |
|
throws IllegalArgumentException |
335 |
|
{ |
336 |
|
assert anEntityManager != null : "'anEntityManager' must not be null"; |
337 |
|
|
338 |
|
assert theSessionStrategy instanceof JpaManagedSessionStrategy : |
339 |
|
"SessionStrategy must be set to JpaManagedSessionStrategy"; |
340 |
|
|
341 |
2
|
JpaManagedSessionStrategy mySessionStrategy = |
342 |
|
(JpaManagedSessionStrategy) theSessionStrategy; |
343 |
|
|
344 |
2
|
mySessionStrategy.setEntityManager(anEntityManager); |
345 |
|
} |
346 |
|
|
347 |
|
|
348 |
|
|
349 |
|
|
350 |
|
|
351 |
|
@return |
352 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
353 |
22
|
protected final Class<Element> ... |
354 |
|
getPersistentClass() |
355 |
|
{ |
356 |
22
|
return thePersistentClass; |
357 |
|
} |
358 |
|
|
359 |
|
|
360 |
|
|
361 |
|
|
362 |
|
|
363 |
|
@return |
364 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
365 |
2
|
protected final ISessionStrategy ... |
366 |
|
getSessionStrategy() |
367 |
|
{ |
368 |
2
|
return theSessionStrategy; |
369 |
|
} |
370 |
|
|
371 |
|
|
372 |
|
|
373 |
|
|
374 |
|
|
375 |
|
@return |
376 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
377 |
2
|
protected final ITransactionStrategy ... |
378 |
|
getTransactionStrategy() |
379 |
|
{ |
380 |
2
|
return theTransactionStrategy; |
381 |
|
} |
382 |
|
|
383 |
|
|
384 |
|
|
385 |
|
|
386 |
|
|
387 |
|
|
388 |
|
|
389 |
|
@return |
390 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
391 |
64
|
private final boolean... |
392 |
|
isManaged() |
393 |
|
{ |
394 |
64
|
boolean myManaged = theSessionStrategy instanceof |
395 |
|
JpaManagedSessionStrategy; |
396 |
|
|
397 |
64
|
return myManaged; |
398 |
|
} |
399 |
|
|
400 |
|
|
401 |
|
|
402 |
|
|
403 |
|
private final Class<Element> thePersistentClass; |
404 |
|
private final ISessionStrategy theSessionStrategy; |
405 |
|
private final ITransactionStrategy theTransactionStrategy; |
406 |
|
} |