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