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.administration.user.Quser;
33 import fr.ifremer.quadrige2.core.dao.technical.hibernate.HibernateDaoSupport;
34 import java.security.Principal;
35 import java.sql.Timestamp;
36 import java.util.Collection;
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 MapProjectDaoBase
58 extends HibernateDaoSupport
59 implements MapProjectDao
60 {
61
62
63
64 @Override
65 public Object get(final int transform, final Integer mapProjectId)
66 {
67 if (mapProjectId == null)
68 {
69 throw new IllegalArgumentException(
70 "MapProject.get - 'mapProjectId' can not be null");
71 }
72 final MapProject entity = get(MapProjectImpl.class, mapProjectId);
73 return transformEntity(transform, entity);
74 }
75
76
77
78 @Override
79 public MapProject get(Integer mapProjectId)
80 {
81 return (MapProject)this.get(TRANSFORM_NONE, mapProjectId);
82 }
83
84
85
86
87 @Override
88 public Object load(final int transform, final Integer mapProjectId)
89 {
90 if (mapProjectId == null)
91 {
92 throw new IllegalArgumentException(
93 "MapProject.load - 'mapProjectId' can not be null");
94 }
95 final MapProject entity = get(MapProjectImpl.class, mapProjectId);
96 return transformEntity(transform, entity);
97 }
98
99
100
101
102 @Override
103 public MapProject load(Integer mapProjectId)
104 {
105 return (MapProject)this.load(TRANSFORM_NONE, mapProjectId);
106 }
107
108
109
110
111 @Override
112 @SuppressWarnings({"unchecked"})
113 public Collection<MapProject> loadAll()
114 {
115 return (Collection<MapProject>) this.loadAll(MapProjectDao.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(MapProjectDao.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(MapProjectImpl.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 MapProject create(MapProject mapProject)
181 {
182 return (MapProject)this.create(MapProjectDao.TRANSFORM_NONE, mapProject);
183 }
184
185
186
187
188 @Override
189 public Object create(final int transform, final MapProject mapProject)
190 {
191 if (mapProject == null)
192 {
193 throw new IllegalArgumentException(
194 "MapProject.create - 'mapProject' can not be null");
195 }
196 this.getSessionFactory().getCurrentSession().save(mapProject);
197 return this.transformEntity(transform, mapProject);
198 }
199
200
201
202
203 @Override
204 @SuppressWarnings({"unchecked"})
205 public Collection<MapProject> create(final Collection<MapProject> entities)
206 {
207 return (Collection<MapProject>) create(MapProjectDao.TRANSFORM_NONE, entities);
208 }
209
210
211
212
213 @Override
214 public Collection<?> create(final int transform, final Collection<MapProject> entities)
215 {
216 if (entities == null)
217 {
218 throw new IllegalArgumentException(
219 "MapProject.create - 'entities' can not be null");
220 }
221 for (MapProject entity : entities)
222 {
223 create(transform, entity);
224 }
225 return entities;
226 }
227
228
229
230
231 @Override
232 public MapProject create(
233 String mapProjectNm,
234 String mapProjetConfig,
235 Timestamp updateDt)
236 {
237 return (MapProject)this.create(MapProjectDao.TRANSFORM_NONE, mapProjectNm, mapProjetConfig, updateDt);
238 }
239
240
241
242
243 @Override
244 public Object create(
245 final int transform,
246 String mapProjectNm,
247 String mapProjetConfig,
248 Timestamp updateDt)
249 {
250 MapProject entity = new MapProjectImpl();
251 entity.setMapProjectNm(mapProjectNm);
252 entity.setMapProjetConfig(mapProjetConfig);
253 entity.setUpdateDt(updateDt);
254 return this.create(transform, entity);
255 }
256
257
258
259
260 @Override
261 public MapProject create(
262 String mapProjectNm,
263 Timestamp updateDt,
264 Quser quserId)
265 {
266 return (MapProject)this.create(MapProjectDao.TRANSFORM_NONE, mapProjectNm, updateDt, quserId);
267 }
268
269
270
271
272 @Override
273 public Object create(
274 final int transform,
275 String mapProjectNm,
276 Timestamp updateDt,
277 Quser quserId)
278 {
279 MapProject entity = new MapProjectImpl();
280 entity.setMapProjectNm(mapProjectNm);
281 entity.setUpdateDt(updateDt);
282 entity.setQuserId(quserId);
283 return this.create(transform, entity);
284 }
285
286
287
288
289 @Override
290 public void update(MapProject mapProject)
291 {
292 if (mapProject == null)
293 {
294 throw new IllegalArgumentException(
295 "MapProject.update - 'mapProject' can not be null");
296 }
297 this.getSessionFactory().getCurrentSession().update(mapProject);
298 }
299
300
301
302
303 @Override
304 public void update(final Collection<MapProject> entities)
305 {
306 if (entities == null)
307 {
308 throw new IllegalArgumentException(
309 "MapProject.update - 'entities' can not be null");
310 }
311 for (MapProject entity : entities)
312 {
313 update(entity);
314 }
315 }
316
317
318
319
320 @Override
321 public void remove(MapProject mapProject)
322 {
323 if (mapProject == null)
324 {
325 throw new IllegalArgumentException(
326 "MapProject.remove - 'mapProject' can not be null");
327 }
328 this.getSessionFactory().getCurrentSession().delete(mapProject);
329 }
330
331
332
333
334 @Override
335 public void remove(Integer mapProjectId)
336 {
337 if (mapProjectId == null)
338 {
339 throw new IllegalArgumentException(
340 "MapProject.remove - 'mapProjectId' can not be null");
341 }
342 MapProject entity = this.get(mapProjectId);
343 if (entity != null)
344 {
345 this.remove(entity);
346 }
347 }
348
349
350
351
352 @Override
353 public void remove(Collection<MapProject> entities)
354 {
355 if (entities == null)
356 {
357 throw new IllegalArgumentException(
358 "MapProject.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 MapProject entity)
377 {
378 Object target = null;
379 if (entity != null)
380 {
381 switch (transform)
382 {
383 case MapProjectDao.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 MapProjectDao.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 MapProject)
432 {
433 result = input;
434 }
435 return result;
436 }
437 };
438
439
440
441
442
443 protected MapProject toEntity(Object[] row)
444 {
445 MapProject 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 MapProject)
453 {
454 target = (MapProject)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(), MapProjectImpl.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(MapProjectDao.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(), MapProjectImpl.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<MapProject> search(final Search search)
531 {
532 return (Set<MapProject>) this.search(MapProjectDao.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 }