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