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