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