001// license-header java merge-point 002// 003// Attention: Generated code! Do not modify by hand! 004// Generated by: hibernate/SpringHibernateDaoBase.vsl in <project>/mda/src/main/cartridge/custom/... 005// 006package fr.ifremer.adagio.core.dao.data.sample; 007 008/* 009 * #%L 010 * SIH-Adagio :: Core 011 * $Id:$ 012 * $HeadURL:$ 013 * %% 014 * Copyright (C) 2012 - 2014 Ifremer 015 * %% 016 * This program is free software: you can redistribute it and/or modify 017 * it under the terms of the GNU Affero General Public License as published by 018 * the Free Software Foundation, either version 3 of the License, or 019 * (at your option) any later version. 020 * 021 * This program is distributed in the hope that it will be useful, 022 * but WITHOUT ANY WARRANTY; without even the implied warranty of 023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 024 * GNU General Public License for more details. 025 * 026 * You should have received a copy of the GNU Affero General Public License 027 * along with this program. If not, see <http://www.gnu.org/licenses/>. 028 * #L% 029 */ 030 031import fr.ifremer.adagio.core.dao.PrincipalStore; 032import fr.ifremer.adagio.core.dao.PropertySearch; 033import fr.ifremer.adagio.core.dao.Search; 034import fr.ifremer.adagio.core.dao.administration.programStrategy.Program; 035import fr.ifremer.adagio.core.dao.administration.user.Department; 036import fr.ifremer.adagio.core.dao.referential.QualityFlag; 037import fr.ifremer.adagio.core.dao.referential.pmfm.Matrix; 038import fr.ifremer.adagio.core.dao.technical.hibernate.HibernateDaoSupport; 039import java.security.Principal; 040import java.sql.Timestamp; 041import java.util.Collection; 042import java.util.Date; 043import java.util.LinkedHashSet; 044import java.util.List; 045import java.util.Set; 046import javax.annotation.Resource; 047import org.andromda.spring.PaginationResult; 048import org.apache.commons.collections.CollectionUtils; 049import org.apache.commons.collections.Transformer; 050import org.hibernate.Criteria; 051import org.hibernate.HibernateException; 052import org.hibernate.Query; 053import org.hibernate.ScrollableResults; 054 055/** 056 * <p> 057 * Base Spring DAO Class: is able to create, update, remove, load, and find 058 * objects of type <code>Sample</code>. 059 * </p> 060 * 061 * @see Sample 062 */ 063public abstract class SampleDaoBase 064 extends HibernateDaoSupport 065 implements SampleDao 066{ 067 /** 068 * {@inheritDoc} 069 */ 070 @Override 071 public Object get(final int transform, final Integer id) 072 { 073 if (id == null) 074 { 075 throw new IllegalArgumentException( 076 "Sample.get - 'id' can not be null"); 077 } 078 final Sample entity = get(SampleImpl.class, id); 079 return transformEntity(transform, entity); 080 } 081 /** 082 * {@inheritDoc} 083 */ 084 @Override 085 public Sample get(Integer id) 086 { 087 return (Sample)this.get(TRANSFORM_NONE, id); 088 } 089 090 /** 091 * {@inheritDoc} 092 */ 093 @Override 094 public Object load(final int transform, final Integer id) 095 { 096 if (id == null) 097 { 098 throw new IllegalArgumentException( 099 "Sample.load - 'id' can not be null"); 100 } 101 final Sample entity = get(SampleImpl.class, id); 102 return transformEntity(transform, entity); 103 } 104 105 /** 106 * {@inheritDoc} 107 */ 108 @Override 109 public Sample load(Integer id) 110 { 111 return (Sample)this.load(TRANSFORM_NONE, id); 112 } 113 114 /** 115 * {@inheritDoc} 116 */ 117 @Override 118 @SuppressWarnings({"unchecked"}) 119 public Collection<Sample> loadAll() 120 { 121 return (Collection<Sample>) this.loadAll(SampleDao.TRANSFORM_NONE); 122 } 123 124 /** 125 * {@inheritDoc} 126 */ 127 @Override 128 public Collection<?> loadAll(final int transform) 129 { 130 return this.loadAll(transform, -1, -1); 131 } 132 133 /** 134 * {@inheritDoc} 135 */ 136 @Override 137 public Collection<?> loadAll(final int pageNumber, final int pageSize) 138 { 139 return this.loadAll(SampleDao.TRANSFORM_NONE, pageNumber, pageSize); 140 } 141 142 /** 143 * {@inheritDoc} 144 */ 145 @Override 146 public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize) 147 { 148 try 149 { 150 final Criteria criteria = this.getSession().createCriteria(SampleImpl.class); 151 if (pageNumber > 0 && pageSize > 0) 152 { 153 criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize)); 154 criteria.setMaxResults(pageSize); 155 } 156 final Collection<?> results = criteria.list(); 157 this.transformEntities(transform, results); 158 return results; 159 } 160 catch (HibernateException ex) 161 { 162 throw ex; 163 } 164 } 165 166 /** 167 * firstResult = (pageNumber - 1) * pageSize 168 * @param pageNumber 169 * @param pageSize 170 * @return firstResult 171 */ 172 protected int calculateFirstResult(int pageNumber, int pageSize) 173 { 174 int firstResult = 0; 175 if (pageNumber > 0) 176 { 177 firstResult = (pageNumber - 1) * pageSize; 178 } 179 return firstResult; 180 } 181 182 /** 183 * {@inheritDoc} 184 */ 185 @Override 186 public Sample create(Sample sample) 187 { 188 return (Sample)this.create(SampleDao.TRANSFORM_NONE, sample); 189 } 190 191 /** 192 * {@inheritDoc} 193 */ 194 @Override 195 public Object create(final int transform, final Sample sample) 196 { 197 if (sample == null) 198 { 199 throw new IllegalArgumentException( 200 "Sample.create - 'sample' can not be null"); 201 } 202 this.getSessionFactory().getCurrentSession().save(sample); 203 return this.transformEntity(transform, sample); 204 } 205 206 /** 207 * {@inheritDoc} 208 */ 209 @Override 210 @SuppressWarnings({"unchecked"}) 211 public Collection<Sample> create(final Collection<Sample> entities) 212 { 213 return (Collection<Sample>) create(SampleDao.TRANSFORM_NONE, entities); 214 } 215 216 /** 217 * {@inheritDoc} 218 */ 219 @Override 220 public Collection<?> create(final int transform, final Collection<Sample> entities) 221 { 222 if (entities == null) 223 { 224 throw new IllegalArgumentException( 225 "Sample.create - 'entities' can not be null"); 226 } 227 for (Sample entity : entities) 228 { 229 create(transform, entity); 230 } 231 return entities; 232 } 233 234 /** 235 * {@inheritDoc} 236 */ 237 @Override 238 public Sample create( 239 String label, 240 Date sampleDate, 241 Short individualCount, 242 Float size, 243 244 String comments, 245 Date creationDate, 246 Date controlDate, 247 Date validationDate, 248 Date qualificationDate, 249 String qualificationComments, 250 Timestamp updateDate) 251 { 252 return (Sample)this.create(SampleDao.TRANSFORM_NONE, label, sampleDate, individualCount, size, comments, creationDate, controlDate, validationDate, qualificationDate, qualificationComments, updateDate); 253 } 254 255 /** 256 * {@inheritDoc} 257 */ 258 @Override 259 public Object create( 260 final int transform, 261 String label, 262 Date sampleDate, 263 Short individualCount, 264 Float size, 265 266 String comments, 267 Date creationDate, 268 Date controlDate, 269 Date validationDate, 270 Date qualificationDate, 271 String qualificationComments, 272 Timestamp updateDate) 273 { 274 Sample entity = new SampleImpl(); 275 entity.setLabel(label); 276 entity.setSampleDate(sampleDate); 277 entity.setIndividualCount(individualCount); 278 entity.setSize(size); 279 280 entity.setComments(comments); 281 entity.setCreationDate(creationDate); 282 entity.setControlDate(controlDate); 283 entity.setValidationDate(validationDate); 284 entity.setQualificationDate(qualificationDate); 285 entity.setQualificationComments(qualificationComments); 286 entity.setUpdateDate(updateDate); 287 return this.create(transform, entity); 288 } 289 290 /** 291 * {@inheritDoc} 292 */ 293 @Override 294 public Sample create( 295 Date creationDate, 296 String label, 297 Matrix matrix, 298 Program program, 299 QualityFlag qualityFlag, 300 Department recorderDepartment, 301 302 Timestamp updateDate) 303 { 304 return (Sample)this.create(SampleDao.TRANSFORM_NONE, creationDate, label, matrix, program, qualityFlag, recorderDepartment, updateDate); 305 } 306 307 /** 308 * {@inheritDoc} 309 */ 310 @Override 311 public Object create( 312 final int transform, 313 Date creationDate, 314 String label, 315 Matrix matrix, 316 Program program, 317 QualityFlag qualityFlag, 318 Department recorderDepartment, 319 320 Timestamp updateDate) 321 { 322 Sample entity = new SampleImpl(); 323 entity.setCreationDate(creationDate); 324 entity.setLabel(label); 325 entity.setMatrix(matrix); 326 entity.setProgram(program); 327 entity.setQualityFlag(qualityFlag); 328 entity.setRecorderDepartment(recorderDepartment); 329 330 entity.setUpdateDate(updateDate); 331 return this.create(transform, entity); 332 } 333 334 /** 335 * {@inheritDoc} 336 */ 337 @Override 338 public void update(Sample sample) 339 { 340 if (sample == null) 341 { 342 throw new IllegalArgumentException( 343 "Sample.update - 'sample' can not be null"); 344 } 345 this.getSessionFactory().getCurrentSession().update(sample); 346 } 347 348 /** 349 * {@inheritDoc} 350 */ 351 @Override 352 public void update(final Collection<Sample> entities) 353 { 354 if (entities == null) 355 { 356 throw new IllegalArgumentException( 357 "Sample.update - 'entities' can not be null"); 358 } 359 for (Sample entity : entities) 360 { 361 update(entity); 362 } 363 } 364 365 /** 366 * {@inheritDoc} 367 */ 368 @Override 369 public void remove(Sample sample) 370 { 371 if (sample == null) 372 { 373 throw new IllegalArgumentException( 374 "Sample.remove - 'sample' can not be null"); 375 } 376 this.getSessionFactory().getCurrentSession().delete(sample); 377 } 378 379 /** 380 * {@inheritDoc} 381 */ 382 @Override 383 public void remove(Integer id) 384 { 385 if (id == null) 386 { 387 throw new IllegalArgumentException( 388 "Sample.remove - 'id' can not be null"); 389 } 390 Sample entity = this.get(id); 391 if (entity != null) 392 { 393 this.remove(entity); 394 } 395 } 396 397 /** 398 * {@inheritDoc} 399 */ 400 @Override 401 public void remove(Collection<Sample> entities) 402 { 403 if (entities == null) 404 { 405 throw new IllegalArgumentException( 406 "Sample.remove - 'entities' can not be null"); 407 } 408 deleteAll(entities); 409 } 410 /** 411 * Allows transformation of entities into value objects 412 * (or something else for that matter), when the <code>transform</code> 413 * flag is set to one of the constants defined in <code>SampleDao</code>, please note 414 * that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself 415 * will be returned. 416 * 417 * If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed. 418 * 419 * @param transform one of the constants declared in {@link SampleDao} 420 * @param entity an entity that was found 421 * @return the transformed entity (i.e. new value object, etc) 422 * @see SampleDao#transformEntity(int, Sample) 423 */ 424 public Object transformEntity(final int transform, final Sample entity) 425 { 426 Object target = null; 427 if (entity != null) 428 { 429 switch (transform) 430 { 431 case SampleDao.TRANSFORM_NONE : // fall-through 432 default: 433 target = entity; 434 } 435 } 436 return target; 437 } 438 439 /** 440 * {@inheritDoc} 441 */ 442 @Override 443 public void transformEntities(final int transform, final Collection<?> entities) 444 { 445 switch (transform) 446 { 447 case SampleDao.TRANSFORM_NONE : // fall-through 448 default: 449 // do nothing; 450 } 451 } 452 453 /** 454 * @see SampleDao#toEntities(Collection) 455 */ 456 public void toEntities(final Collection<?> results) 457 { 458 if (results != null) 459 { 460 CollectionUtils.transform(results, this.ENTITYTRANSFORMER); 461 } 462 } 463 464 /** 465 * This anonymous transformer is designed to transform report query results 466 * (which result in an array of entities) to {@link Sample} 467 * using the Jakarta Commons-Collections Transformation API. 468 */ 469 private Transformer ENTITYTRANSFORMER = 470 new Transformer() 471 { 472 public Object transform(Object input) 473 { 474 Object result = null; 475 if (input instanceof Object[]) 476 { 477 result = toEntity((Object[])input); 478 } 479 else if (input instanceof Sample) 480 { 481 result = input; 482 } 483 return result; 484 } 485 }; 486 487 /** 488 * @param row 489 * @return Sample 490 */ 491 protected Sample toEntity(Object[] row) 492 { 493 Sample target = null; 494 if (row != null) 495 { 496 final int numberOfObjects = row.length; 497 for (int ctr = 0; ctr < numberOfObjects; ctr++) 498 { 499 final Object object = row[ctr]; 500 if (object instanceof Sample) 501 { 502 target = (Sample)object; 503 break; 504 } 505 } 506 } 507 return target; 508 } 509 510 /** 511 * Gets the current <code>principal</code> if one has been set, 512 * otherwise returns <code>null</code>. 513 * 514 * @return the current principal 515 */ 516 protected Principal getPrincipal() 517 { 518 return PrincipalStore.get(); 519 } 520 521 /** 522 * {@inheritDoc} 523 */ 524 @Override 525 @SuppressWarnings({ "unchecked" }) 526 public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search) 527 { 528 try 529 { 530 search.setPageNumber(pageNumber); 531 search.setPageSize(pageSize); 532 final PropertySearch propertySearch = new PropertySearch( 533 this.getSession(), SampleImpl.class, search); 534 final List results = propertySearch.executeAsList(); 535 this.transformEntities(transform, results); 536 return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount()); 537 } 538 catch (HibernateException ex) 539 { 540 throw ex; /*super.convertHibernateAccessException(ex);*/ 541 } 542 } 543 544 /** 545 * {@inheritDoc} 546 */ 547 @Override 548 public PaginationResult search(final int pageNumber, final int pageSize, final Search search) 549 { 550 return this.search(SampleDao.TRANSFORM_NONE, pageNumber, pageSize, search); 551 } 552 553 /** 554 * {@inheritDoc} 555 */ 556 @Override 557 public Set<?> search(final int transform, final Search search) 558 { 559 try 560 { 561 final PropertySearch propertySearch = new PropertySearch( 562 this.getSession(), SampleImpl.class, search); 563 final Set<?> results = propertySearch.executeAsSet(); 564 this.transformEntities(transform, results); 565 return results; 566 } 567 catch (HibernateException ex) 568 { 569 throw ex; /*super.convertHibernateAccessException(ex);*/ 570 } 571 } 572 573 /** 574 * {@inheritDoc} 575 */ 576 @Override 577 @SuppressWarnings("unchecked") 578 public Set<Sample> search(final Search search) 579 { 580 return (Set<Sample>) this.search(SampleDao.TRANSFORM_NONE, search); 581 } 582 583 /** 584 * Executes and returns the given Hibernate queryObject as a {@link PaginationResult} instance. 585 * @param queryObject 586 * @param transform 587 * @param pageNumber 588 * @param pageSize 589 * @return PaginationResult 590 */ 591 @SuppressWarnings({ "unchecked" }) 592 protected PaginationResult getPaginationResult( 593 final Query queryObject, 594 final int transform, int pageNumber, int pageSize) 595 { 596 try 597 { 598 final ScrollableResults scrollableResults = queryObject.scroll(); 599 scrollableResults.last(); 600 int totalCount = scrollableResults.getRowNumber(); 601 totalCount = totalCount >= 0 ? totalCount + 1 : 0; 602 if (pageNumber > 0 && pageSize > 0) 603 { 604 queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize)); 605 queryObject.setMaxResults(pageSize); 606 } 607 // Unchecked transformation because Set object is reused, cannot be strongly typed. 608 Set results = new LinkedHashSet(queryObject.list()); 609 transformEntities(transform, results); 610 return new PaginationResult(results.toArray(new Object[results.size()]), totalCount); 611 } 612 catch (HibernateException ex) 613 { 614 throw ex; /*super.convertHibernateAccessException(ex);*/ 615 } 616 } 617 618 // spring-hibernate-dao-base merge-point 619}