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.lang.annotation.*; |
25 |
|
import java.lang.reflect.*; |
26 |
|
import java.util.*; |
27 |
|
|
28 |
|
import org.hibernate.*; |
29 |
|
import org.hibernate.type.Type; |
30 |
|
import org.perfectjpattern.jee.api.integration.dao.*; |
31 |
|
|
32 |
|
|
33 |
|
@link |
34 |
|
|
35 |
|
@param |
36 |
|
@param |
37 |
|
|
38 |
|
@author |
39 |
|
@version |
40 |
|
|
41 |
|
public |
|
|
| 83.6% |
Uncovered Elements: 12 (73) |
Complexity: 21 |
Complexity Density: 0.48 |
|
42 |
|
class SpringGenericDao<Id extends Serializable, Element> |
43 |
|
extends HibernateGenericDao<Id, Element> |
44 |
|
implements IFinderExecutor<Element> |
45 |
|
{ |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
@inheritDoc |
51 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
52 |
3
|
@SuppressWarnings("unchecked")... |
53 |
|
public List<Element> |
54 |
|
execute(Method aMethod, Object... anArguments) |
55 |
|
{ |
56 |
3
|
final Query myQuery = prepareQuery(aMethod, anArguments); |
57 |
|
|
58 |
3
|
return myQuery.list(); |
59 |
|
} |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
@return |
66 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
67 |
3
|
public final IFinderNamingStrategy ... |
68 |
|
getNamingStrategy() |
69 |
|
{ |
70 |
3
|
return theNamingStrategy; |
71 |
|
} |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
@param |
78 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
79 |
2
|
public final void ... |
80 |
|
setNamingStrategy(IFinderNamingStrategy aNamingStrategy) |
81 |
|
{ |
82 |
2
|
theNamingStrategy = aNamingStrategy; |
83 |
|
} |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
@return |
90 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
91 |
5
|
public final IArgumentTypeResolver ... |
92 |
|
getArgumentTypeResolver() |
93 |
|
{ |
94 |
5
|
return theArgumentTypeResolver; |
95 |
|
} |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
@param |
102 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
103 |
0
|
public final void ... |
104 |
|
setArgumentTypeResolver(IArgumentTypeResolver anArgumentTypeResolver) |
105 |
|
{ |
106 |
0
|
theArgumentTypeResolver = anArgumentTypeResolver; |
107 |
|
} |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
@link |
114 |
|
@link |
115 |
|
@link@link |
116 |
|
@link |
117 |
|
|
118 |
|
@param |
119 |
|
@param |
120 |
|
@param |
121 |
|
@throws |
122 |
|
@throws |
123 |
|
@throws |
124 |
|
@throws |
125 |
|
|
126 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
127 |
2
|
protected... |
128 |
|
SpringGenericDao(Class<Element> aPersistentClass, |
129 |
|
ISessionStrategy aSessionStrategy, |
130 |
|
ITransactionStrategy aTransactionStrategy) |
131 |
|
{ |
132 |
2
|
super(aPersistentClass, aSessionStrategy, aTransactionStrategy); |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
|
|
| 90.9% |
Uncovered Elements: 2 (22) |
Complexity: 4 |
Complexity Density: 0.25 |
|
138 |
3
|
private Query ... |
139 |
|
prepareQuery(Method aMethod, Object... anArguments) |
140 |
|
{ |
141 |
|
assert aMethod != null : "'aMethod' must not be null"; |
142 |
|
assert anArguments != null : "'anArguments' must not be null"; |
143 |
|
|
144 |
3
|
final String myQueryName = getNamingStrategy().getQueryName( |
145 |
|
getPersistentClass(), aMethod); |
146 |
|
|
147 |
3
|
final Query myQuery = getActualSession().getNamedQuery(myQueryName); |
148 |
|
|
149 |
3
|
String[] myQueryParameters = myQuery.getNamedParameters(); |
150 |
3
|
if (myQueryParameters.length == 0) |
151 |
|
{ |
152 |
1
|
setPositionalParams(myQuery, anArguments); |
153 |
|
} |
154 |
|
else |
155 |
|
{ |
156 |
2
|
Annotation myAnnotation = null; |
157 |
2
|
final int myNumberOfAnnotations = aMethod.getAnnotations().length; |
158 |
2
|
for (int i = 0; i < myNumberOfAnnotations; ++i) |
159 |
|
{ |
160 |
2
|
if (aMethod.getAnnotations()[i] instanceof QueryParameters) |
161 |
|
{ |
162 |
2
|
myAnnotation = aMethod.getAnnotations()[i]; |
163 |
|
|
164 |
2
|
break; |
165 |
|
} |
166 |
|
} |
167 |
|
|
168 |
|
assert myAnnotation != null : "'" + aMethod.getName() + |
169 |
|
"' does not include the required QueryParameters annotation"; |
170 |
|
|
171 |
2
|
String[] myAnnotatedParameters = ((QueryParameters) |
172 |
|
myAnnotation).names(); |
173 |
|
|
174 |
2
|
final int myNumberOfAnnotated = myAnnotatedParameters.length; |
175 |
2
|
final int myNumberOfParameters = myQueryParameters.length; |
176 |
|
|
177 |
|
assert myNumberOfAnnotated == myNumberOfParameters : "Arguments " + |
178 |
|
"mismatch, all query parameters must be annotated"; |
179 |
|
|
180 |
|
|
181 |
2
|
setNamedParams(myQuery, anArguments, myQueryParameters, |
182 |
|
myAnnotatedParameters); |
183 |
|
} |
184 |
|
|
185 |
3
|
return myQuery; |
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
@param |
195 |
|
@param |
196 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
197 |
1
|
private void ... |
198 |
|
setPositionalParams(Query aNamedQuery, Object[] aMethodArguments) |
199 |
|
{ |
200 |
|
assert aNamedQuery != null : "'aNamedQuery' must not be null"; |
201 |
|
assert aMethodArguments != null : "'anArguments' must not be null"; |
202 |
|
|
203 |
|
|
204 |
2
|
for (int i = 0; i < aMethodArguments.length; i++) |
205 |
|
{ |
206 |
1
|
Object myArgument = aMethodArguments[i]; |
207 |
|
|
208 |
1
|
Type myArgumentType = getArgumentTypeResolver().getArgumentType( |
209 |
|
myArgument); |
210 |
|
|
211 |
1
|
if (myArgumentType != null) |
212 |
|
{ |
213 |
0
|
aNamedQuery.setParameter(i, myArgument, myArgumentType); |
214 |
|
} |
215 |
|
else |
216 |
|
{ |
217 |
1
|
aNamedQuery.setParameter(i, myArgument); |
218 |
|
} |
219 |
|
} |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
@link |
230 |
|
|
231 |
|
|
232 |
|
@param |
233 |
|
@param |
234 |
|
@param |
235 |
|
@param |
236 |
|
|
237 |
|
@throws |
238 |
|
|
|
|
| 76% |
Uncovered Elements: 6 (25) |
Complexity: 8 |
Complexity Density: 0.53 |
|
239 |
2
|
private void ... |
240 |
|
setNamedParams(Query aNamedQuery, Object[] aMethodArguments, |
241 |
|
String[] aQueryParameters, String[] aMethodParameters) |
242 |
|
{ |
243 |
|
assert aNamedQuery != null : "'aNamedQuery' must not be null"; |
244 |
|
assert aMethodArguments != null : "'anArguments' must not be null"; |
245 |
|
assert aQueryParameters != null : "'aNamedParameters' must not be null"; |
246 |
|
assert aMethodParameters != null : |
247 |
|
"'anOrderedArgumentNames' must not be null"; |
248 |
|
|
249 |
2
|
if (aMethodArguments.length != aQueryParameters.length || |
250 |
|
aQueryParameters.length != aMethodParameters.length) |
251 |
|
{ |
252 |
|
throw new IllegalAccessError("Mismatching number of arguments"); |
253 |
|
} |
254 |
|
|
255 |
|
|
256 |
2
|
Map<String, Object> myArgumentsMap = new HashMap<String, Object>(); |
257 |
2
|
final int myNumberOfParameters = aMethodArguments.length; |
258 |
6
|
for (int i = 0; i < myNumberOfParameters; i++) |
259 |
|
{ |
260 |
4
|
myArgumentsMap.put(aMethodParameters[i], aMethodArguments[i]); |
261 |
|
} |
262 |
|
|
263 |
|
|
264 |
2
|
for (String myQueryParameter : aQueryParameters) |
265 |
|
{ |
266 |
|
|
267 |
4
|
Object myArgument = null; |
268 |
4
|
if (myArgumentsMap.containsKey(myQueryParameter)) |
269 |
|
{ |
270 |
4
|
myArgument = myArgumentsMap.get(myQueryParameter); |
271 |
|
} |
272 |
|
|
273 |
|
assert myArgument != null : "Query parameter '" + |
274 |
|
myQueryParameter + "' was not found, check for misspelling"; |
275 |
|
|
276 |
4
|
Type myArgumentType = getArgumentTypeResolver().getArgumentType( |
277 |
|
myArgument); |
278 |
|
|
279 |
4
|
if (myArgumentType != null) |
280 |
|
{ |
281 |
0
|
aNamedQuery.setParameter(myQueryParameter, myArgument, |
282 |
|
myArgumentType); |
283 |
|
} |
284 |
|
else |
285 |
|
{ |
286 |
4
|
if (myArgument instanceof Collection) |
287 |
|
{ |
288 |
0
|
aNamedQuery.setParameterList(myQueryParameter, |
289 |
|
(Collection<?>) myArgument); |
290 |
|
} |
291 |
|
else |
292 |
|
{ |
293 |
4
|
aNamedQuery.setParameter(myQueryParameter, myArgument); |
294 |
|
} |
295 |
|
} |
296 |
|
} |
297 |
|
} |
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
|
305 |
|
private IFinderNamingStrategy theNamingStrategy = |
306 |
|
new SimpleFinderNamingStrategy(); |
307 |
|
|
308 |
|
|
309 |
|
|
310 |
|
|
311 |
|
private IArgumentTypeResolver theArgumentTypeResolver = |
312 |
|
new SimpleArgumentTypeResolver(); |
313 |
|
} |