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