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