1
2
3
4
5
6 package fr.ifremer.quadrige3.core.dao.system.rule;
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 import fr.ifremer.quadrige3.core.dao.PrincipalStore;
29 import fr.ifremer.quadrige3.core.dao.PropertySearch;
30 import fr.ifremer.quadrige3.core.dao.Search;
31 import fr.ifremer.quadrige3.core.dao.technical.hibernate.HibernateDaoSupport;
32 import java.security.Principal;
33 import java.sql.Timestamp;
34 import java.util.Collection;
35 import java.util.Date;
36 import java.util.LinkedHashSet;
37 import java.util.List;
38 import java.util.Set;
39 import javax.annotation.Resource;
40 import org.andromda.spring.PaginationResult;
41 import org.apache.commons.collections.CollectionUtils;
42 import org.apache.commons.collections.Transformer;
43 import org.hibernate.Criteria;
44 import org.hibernate.HibernateException;
45 import org.hibernate.Query;
46 import org.hibernate.ScrollableResults;
47
48
49
50
51
52
53
54
55
56 public abstract class FunctionDaoBase
57 extends HibernateDaoSupport
58 implements FunctionDao
59 {
60
61
62
63 @Override
64 public Object get(final int transform, final Integer functionId)
65 {
66 if (functionId == null)
67 {
68 throw new IllegalArgumentException(
69 "Function.get - 'functionId' can not be null");
70 }
71 final Function entity = get(FunctionImpl.class, functionId);
72 return transformEntity(transform, entity);
73 }
74
75
76
77 @Override
78 public Function get(Integer functionId)
79 {
80 return (Function)this.get(TRANSFORM_NONE, functionId);
81 }
82
83
84
85
86 @Override
87 public Object load(final int transform, final Integer functionId)
88 {
89 if (functionId == null)
90 {
91 throw new IllegalArgumentException(
92 "Function.load - 'functionId' can not be null");
93 }
94 final Function entity = get(FunctionImpl.class, functionId);
95 return transformEntity(transform, entity);
96 }
97
98
99
100
101 @Override
102 public Function load(Integer functionId)
103 {
104 return (Function)this.load(TRANSFORM_NONE, functionId);
105 }
106
107
108
109
110 @Override
111 @SuppressWarnings({"unchecked"})
112 public Collection<Function> loadAll()
113 {
114 return (Collection<Function>) this.loadAll(FunctionDao.TRANSFORM_NONE);
115 }
116
117
118
119
120 @Override
121 public Collection<?> loadAll(final int transform)
122 {
123 return this.loadAll(transform, -1, -1);
124 }
125
126
127
128
129 @Override
130 public Collection<?> loadAll(final int pageNumber, final int pageSize)
131 {
132 return this.loadAll(FunctionDao.TRANSFORM_NONE, pageNumber, pageSize);
133 }
134
135
136
137
138 @Override
139 public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize)
140 {
141 try
142 {
143 final Criteria criteria = this.getSession().createCriteria(FunctionImpl.class);
144 if (pageNumber > 0 && pageSize > 0)
145 {
146 criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
147 criteria.setMaxResults(pageSize);
148 }
149 final Collection<?> results = criteria.list();
150 this.transformEntities(transform, results);
151 return results;
152 }
153 catch (HibernateException ex)
154 {
155 throw ex;
156 }
157 }
158
159
160
161
162
163
164
165 protected int calculateFirstResult(int pageNumber, int pageSize)
166 {
167 int firstResult = 0;
168 if (pageNumber > 0)
169 {
170 firstResult = (pageNumber - 1) * pageSize;
171 }
172 return firstResult;
173 }
174
175
176
177
178 @Override
179 public Function create(Function function)
180 {
181 return (Function)this.create(FunctionDao.TRANSFORM_NONE, function);
182 }
183
184
185
186
187 @Override
188 public Object create(final int transform, final Function function)
189 {
190 if (function == null)
191 {
192 throw new IllegalArgumentException(
193 "Function.create - 'function' can not be null");
194 }
195 this.getSessionFactory().getCurrentSession().save(function);
196 return this.transformEntity(transform, function);
197 }
198
199
200
201
202 @Override
203 @SuppressWarnings({"unchecked"})
204 public Collection<Function> create(final Collection<Function> entities)
205 {
206 return (Collection<Function>) create(FunctionDao.TRANSFORM_NONE, entities);
207 }
208
209
210
211
212 @Override
213 public Collection<?> create(final int transform, final Collection<Function> entities)
214 {
215 if (entities == null)
216 {
217 throw new IllegalArgumentException(
218 "Function.create - 'entities' can not be null");
219 }
220 for (Function entity : entities)
221 {
222 create(transform, entity);
223 }
224 return entities;
225 }
226
227
228
229
230 @Override
231 public Function create(
232 String functionNm,
233 String functionJavaFunctionClass,
234 Date functionCreationDt,
235 Timestamp updateDt)
236 {
237 return (Function)this.create(FunctionDao.TRANSFORM_NONE, functionNm, functionJavaFunctionClass, functionCreationDt, updateDt);
238 }
239
240
241
242
243 @Override
244 public Object create(
245 final int transform,
246 String functionNm,
247 String functionJavaFunctionClass,
248 Date functionCreationDt,
249 Timestamp updateDt)
250 {
251 Function entity = new FunctionImpl();
252 entity.setFunctionNm(functionNm);
253 entity.setFunctionJavaFunctionClass(functionJavaFunctionClass);
254 entity.setFunctionCreationDt(functionCreationDt);
255 entity.setUpdateDt(updateDt);
256 return this.create(transform, entity);
257 }
258
259
260
261
262 @Override
263 public void update(Function function)
264 {
265 if (function == null)
266 {
267 throw new IllegalArgumentException(
268 "Function.update - 'function' can not be null");
269 }
270 this.getSessionFactory().getCurrentSession().update(function);
271 }
272
273
274
275
276 @Override
277 public void update(final Collection<Function> entities)
278 {
279 if (entities == null)
280 {
281 throw new IllegalArgumentException(
282 "Function.update - 'entities' can not be null");
283 }
284 for (Function entity : entities)
285 {
286 update(entity);
287 }
288 }
289
290
291
292
293 @Override
294 public void remove(Function function)
295 {
296 if (function == null)
297 {
298 throw new IllegalArgumentException(
299 "Function.remove - 'function' can not be null");
300 }
301 this.getSessionFactory().getCurrentSession().delete(function);
302 }
303
304
305
306
307 @Override
308 public void remove(Integer functionId)
309 {
310 if (functionId == null)
311 {
312 throw new IllegalArgumentException(
313 "Function.remove - 'functionId' can not be null");
314 }
315 Function entity = this.get(functionId);
316 if (entity != null)
317 {
318 this.remove(entity);
319 }
320 }
321
322
323
324
325 @Override
326 public void remove(Collection<Function> entities)
327 {
328 if (entities == null)
329 {
330 throw new IllegalArgumentException(
331 "Function.remove - 'entities' can not be null");
332 }
333 deleteAll(entities);
334 }
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349 public Object transformEntity(final int transform, final Function entity)
350 {
351 Object target = null;
352 if (entity != null)
353 {
354 switch (transform)
355 {
356 case FunctionDao.TRANSFORM_NONE :
357 default:
358 target = entity;
359 }
360 }
361 return target;
362 }
363
364
365
366
367 @Override
368 public void transformEntities(final int transform, final Collection<?> entities)
369 {
370 switch (transform)
371 {
372 case FunctionDao.TRANSFORM_NONE :
373 default:
374
375 }
376 }
377
378
379
380
381 public void toEntities(final Collection<?> results)
382 {
383 if (results != null)
384 {
385 CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
386 }
387 }
388
389
390
391
392
393
394 private Transformer ENTITYTRANSFORMER =
395 new Transformer()
396 {
397 public Object transform(Object input)
398 {
399 Object result = null;
400 if (input instanceof Object[])
401 {
402 result = toEntity((Object[])input);
403 }
404 else if (input instanceof Function)
405 {
406 result = input;
407 }
408 return result;
409 }
410 };
411
412
413
414
415
416 protected Function toEntity(Object[] row)
417 {
418 Function target = null;
419 if (row != null)
420 {
421 final int numberOfObjects = row.length;
422 for (int ctr = 0; ctr < numberOfObjects; ctr++)
423 {
424 final Object object = row[ctr];
425 if (object instanceof Function)
426 {
427 target = (Function)object;
428 break;
429 }
430 }
431 }
432 return target;
433 }
434
435
436
437
438
439
440
441 protected Principal getPrincipal()
442 {
443 return PrincipalStore.get();
444 }
445
446
447
448
449 @Override
450 @SuppressWarnings({ "unchecked" })
451 public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search)
452 {
453 try
454 {
455 search.setPageNumber(pageNumber);
456 search.setPageSize(pageSize);
457 final PropertySearch propertySearch = new PropertySearch(
458 this.getSession(), FunctionImpl.class, search);
459 final List results = propertySearch.executeAsList();
460 this.transformEntities(transform, results);
461 return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount());
462 }
463 catch (HibernateException ex)
464 {
465 throw ex;
466 }
467 }
468
469
470
471
472 @Override
473 public PaginationResult search(final int pageNumber, final int pageSize, final Search search)
474 {
475 return this.search(FunctionDao.TRANSFORM_NONE, pageNumber, pageSize, search);
476 }
477
478
479
480
481 @Override
482 public Set<?> search(final int transform, final Search search)
483 {
484 try
485 {
486 final PropertySearch propertySearch = new PropertySearch(
487 this.getSession(), FunctionImpl.class, search);
488 final Set<?> results = propertySearch.executeAsSet();
489 this.transformEntities(transform, results);
490 return results;
491 }
492 catch (HibernateException ex)
493 {
494 throw ex;
495 }
496 }
497
498
499
500
501 @Override
502 @SuppressWarnings("unchecked")
503 public Set<Function> search(final Search search)
504 {
505 return (Set<Function>) this.search(FunctionDao.TRANSFORM_NONE, search);
506 }
507
508
509
510
511
512
513
514
515
516 @SuppressWarnings({ "unchecked" })
517 protected PaginationResult getPaginationResult(
518 final Query queryObject,
519 final int transform, int pageNumber, int pageSize)
520 {
521 try
522 {
523 final ScrollableResults scrollableResults = queryObject.scroll();
524 scrollableResults.last();
525 int totalCount = scrollableResults.getRowNumber();
526 totalCount = totalCount >= 0 ? totalCount + 1 : 0;
527 if (pageNumber > 0 && pageSize > 0)
528 {
529 queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
530 queryObject.setMaxResults(pageSize);
531 }
532
533 Set results = new LinkedHashSet(queryObject.list());
534 transformEntities(transform, results);
535 return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
536 }
537 catch (HibernateException ex)
538 {
539 throw ex;
540 }
541 }
542
543
544 }