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