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.referential.taxon; 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.referential.location.Location; 035import fr.ifremer.adagio.core.dao.referential.pmfm.Parameter; 036import fr.ifremer.adagio.core.dao.referential.pmfm.Pmfm; 037import fr.ifremer.adagio.core.dao.referential.pmfm.Unit; 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>TaxonGroupLengthPmfm</code>. 059 * </p> 060 * 061 * @see TaxonGroupLengthPmfm 062 */ 063public abstract class TaxonGroupLengthPmfmDaoBase 064 extends HibernateDaoSupport 065 implements TaxonGroupLengthPmfmDao 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 "TaxonGroupLengthPmfm.get - 'id' can not be null"); 077 } 078 final TaxonGroupLengthPmfm entity = get(TaxonGroupLengthPmfmImpl.class, id); 079 return transformEntity(transform, entity); 080 } 081 /** 082 * {@inheritDoc} 083 */ 084 @Override 085 public TaxonGroupLengthPmfm get(Integer id) 086 { 087 return (TaxonGroupLengthPmfm)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 "TaxonGroupLengthPmfm.load - 'id' can not be null"); 100 } 101 final TaxonGroupLengthPmfm entity = get(TaxonGroupLengthPmfmImpl.class, id); 102 return transformEntity(transform, entity); 103 } 104 105 /** 106 * {@inheritDoc} 107 */ 108 @Override 109 public TaxonGroupLengthPmfm load(Integer id) 110 { 111 return (TaxonGroupLengthPmfm)this.load(TRANSFORM_NONE, id); 112 } 113 114 /** 115 * {@inheritDoc} 116 */ 117 @Override 118 @SuppressWarnings({"unchecked"}) 119 public Collection<TaxonGroupLengthPmfm> loadAll() 120 { 121 return (Collection<TaxonGroupLengthPmfm>) this.loadAll(TaxonGroupLengthPmfmDao.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(TaxonGroupLengthPmfmDao.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(TaxonGroupLengthPmfmImpl.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 TaxonGroupLengthPmfm create(TaxonGroupLengthPmfm taxonGroupLengthPmfm) 187 { 188 return (TaxonGroupLengthPmfm)this.create(TaxonGroupLengthPmfmDao.TRANSFORM_NONE, taxonGroupLengthPmfm); 189 } 190 191 /** 192 * {@inheritDoc} 193 */ 194 @Override 195 public Object create(final int transform, final TaxonGroupLengthPmfm taxonGroupLengthPmfm) 196 { 197 if (taxonGroupLengthPmfm == null) 198 { 199 throw new IllegalArgumentException( 200 "TaxonGroupLengthPmfm.create - 'taxonGroupLengthPmfm' can not be null"); 201 } 202 this.getSessionFactory().getCurrentSession().save(taxonGroupLengthPmfm); 203 return this.transformEntity(transform, taxonGroupLengthPmfm); 204 } 205 206 /** 207 * {@inheritDoc} 208 */ 209 @Override 210 @SuppressWarnings({"unchecked"}) 211 public Collection<TaxonGroupLengthPmfm> create(final Collection<TaxonGroupLengthPmfm> entities) 212 { 213 return (Collection<TaxonGroupLengthPmfm>) create(TaxonGroupLengthPmfmDao.TRANSFORM_NONE, entities); 214 } 215 216 /** 217 * {@inheritDoc} 218 */ 219 @Override 220 public Collection<?> create(final int transform, final Collection<TaxonGroupLengthPmfm> entities) 221 { 222 if (entities == null) 223 { 224 throw new IllegalArgumentException( 225 "TaxonGroupLengthPmfm.create - 'entities' can not be null"); 226 } 227 for (TaxonGroupLengthPmfm entity : entities) 228 { 229 create(transform, entity); 230 } 231 return entities; 232 } 233 234 /** 235 * {@inheritDoc} 236 */ 237 @Override 238 public TaxonGroupLengthPmfm create( 239 Date startDate, 240 Date endDate, 241 String description, 242 Float precision, 243 Timestamp updateDate) 244 { 245 return (TaxonGroupLengthPmfm)this.create(TaxonGroupLengthPmfmDao.TRANSFORM_NONE, startDate, endDate, description, precision, updateDate); 246 } 247 248 /** 249 * {@inheritDoc} 250 */ 251 @Override 252 public Object create( 253 final int transform, 254 Date startDate, 255 Date endDate, 256 String description, 257 Float precision, 258 Timestamp updateDate) 259 { 260 TaxonGroupLengthPmfm entity = new TaxonGroupLengthPmfmImpl(); 261 entity.setStartDate(startDate); 262 entity.setEndDate(endDate); 263 entity.setDescription(description); 264 entity.setPrecision(precision); 265 entity.setUpdateDate(updateDate); 266 return this.create(transform, entity); 267 } 268 269 /** 270 * {@inheritDoc} 271 */ 272 @Override 273 public TaxonGroupLengthPmfm create( 274 String description, 275 Location location, 276 Parameter parameter, 277 Pmfm pmfm, 278 Date startDate, 279 TaxonGroup taxonGroup, 280 Unit unit, 281 Timestamp updateDate) 282 { 283 return (TaxonGroupLengthPmfm)this.create(TaxonGroupLengthPmfmDao.TRANSFORM_NONE, description, location, parameter, pmfm, startDate, taxonGroup, unit, updateDate); 284 } 285 286 /** 287 * {@inheritDoc} 288 */ 289 @Override 290 public Object create( 291 final int transform, 292 String description, 293 Location location, 294 Parameter parameter, 295 Pmfm pmfm, 296 Date startDate, 297 TaxonGroup taxonGroup, 298 Unit unit, 299 Timestamp updateDate) 300 { 301 TaxonGroupLengthPmfm entity = new TaxonGroupLengthPmfmImpl(); 302 entity.setDescription(description); 303 entity.setLocation(location); 304 entity.setParameter(parameter); 305 entity.setPmfm(pmfm); 306 entity.setStartDate(startDate); 307 entity.setTaxonGroup(taxonGroup); 308 entity.setUnit(unit); 309 entity.setUpdateDate(updateDate); 310 return this.create(transform, entity); 311 } 312 313 /** 314 * {@inheritDoc} 315 */ 316 @Override 317 public void update(TaxonGroupLengthPmfm taxonGroupLengthPmfm) 318 { 319 if (taxonGroupLengthPmfm == null) 320 { 321 throw new IllegalArgumentException( 322 "TaxonGroupLengthPmfm.update - 'taxonGroupLengthPmfm' can not be null"); 323 } 324 this.getSessionFactory().getCurrentSession().update(taxonGroupLengthPmfm); 325 } 326 327 /** 328 * {@inheritDoc} 329 */ 330 @Override 331 public void update(final Collection<TaxonGroupLengthPmfm> entities) 332 { 333 if (entities == null) 334 { 335 throw new IllegalArgumentException( 336 "TaxonGroupLengthPmfm.update - 'entities' can not be null"); 337 } 338 for (TaxonGroupLengthPmfm entity : entities) 339 { 340 update(entity); 341 } 342 } 343 344 /** 345 * {@inheritDoc} 346 */ 347 @Override 348 public void remove(TaxonGroupLengthPmfm taxonGroupLengthPmfm) 349 { 350 if (taxonGroupLengthPmfm == null) 351 { 352 throw new IllegalArgumentException( 353 "TaxonGroupLengthPmfm.remove - 'taxonGroupLengthPmfm' can not be null"); 354 } 355 this.getSessionFactory().getCurrentSession().delete(taxonGroupLengthPmfm); 356 } 357 358 /** 359 * {@inheritDoc} 360 */ 361 @Override 362 public void remove(Integer id) 363 { 364 if (id == null) 365 { 366 throw new IllegalArgumentException( 367 "TaxonGroupLengthPmfm.remove - 'id' can not be null"); 368 } 369 TaxonGroupLengthPmfm entity = this.get(id); 370 if (entity != null) 371 { 372 this.remove(entity); 373 } 374 } 375 376 /** 377 * {@inheritDoc} 378 */ 379 @Override 380 public void remove(Collection<TaxonGroupLengthPmfm> entities) 381 { 382 if (entities == null) 383 { 384 throw new IllegalArgumentException( 385 "TaxonGroupLengthPmfm.remove - 'entities' can not be null"); 386 } 387 deleteAll(entities); 388 } 389 /** 390 * Allows transformation of entities into value objects 391 * (or something else for that matter), when the <code>transform</code> 392 * flag is set to one of the constants defined in <code>TaxonGroupLengthPmfmDao</code>, please note 393 * that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself 394 * will be returned. 395 * 396 * If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed. 397 * 398 * @param transform one of the constants declared in {@link TaxonGroupLengthPmfmDao} 399 * @param entity an entity that was found 400 * @return the transformed entity (i.e. new value object, etc) 401 * @see TaxonGroupLengthPmfmDao#transformEntity(int, TaxonGroupLengthPmfm) 402 */ 403 public Object transformEntity(final int transform, final TaxonGroupLengthPmfm entity) 404 { 405 Object target = null; 406 if (entity != null) 407 { 408 switch (transform) 409 { 410 case TaxonGroupLengthPmfmDao.TRANSFORM_NONE : // fall-through 411 default: 412 target = entity; 413 } 414 } 415 return target; 416 } 417 418 /** 419 * {@inheritDoc} 420 */ 421 @Override 422 public void transformEntities(final int transform, final Collection<?> entities) 423 { 424 switch (transform) 425 { 426 case TaxonGroupLengthPmfmDao.TRANSFORM_NONE : // fall-through 427 default: 428 // do nothing; 429 } 430 } 431 432 /** 433 * @see TaxonGroupLengthPmfmDao#toEntities(Collection) 434 */ 435 public void toEntities(final Collection<?> results) 436 { 437 if (results != null) 438 { 439 CollectionUtils.transform(results, this.ENTITYTRANSFORMER); 440 } 441 } 442 443 /** 444 * This anonymous transformer is designed to transform report query results 445 * (which result in an array of entities) to {@link TaxonGroupLengthPmfm} 446 * using the Jakarta Commons-Collections Transformation API. 447 */ 448 private Transformer ENTITYTRANSFORMER = 449 new Transformer() 450 { 451 public Object transform(Object input) 452 { 453 Object result = null; 454 if (input instanceof Object[]) 455 { 456 result = toEntity((Object[])input); 457 } 458 else if (input instanceof TaxonGroupLengthPmfm) 459 { 460 result = input; 461 } 462 return result; 463 } 464 }; 465 466 /** 467 * @param row 468 * @return TaxonGroupLengthPmfm 469 */ 470 protected TaxonGroupLengthPmfm toEntity(Object[] row) 471 { 472 TaxonGroupLengthPmfm target = null; 473 if (row != null) 474 { 475 final int numberOfObjects = row.length; 476 for (int ctr = 0; ctr < numberOfObjects; ctr++) 477 { 478 final Object object = row[ctr]; 479 if (object instanceof TaxonGroupLengthPmfm) 480 { 481 target = (TaxonGroupLengthPmfm)object; 482 break; 483 } 484 } 485 } 486 return target; 487 } 488 489 /** 490 * Gets the current <code>principal</code> if one has been set, 491 * otherwise returns <code>null</code>. 492 * 493 * @return the current principal 494 */ 495 protected Principal getPrincipal() 496 { 497 return PrincipalStore.get(); 498 } 499 500 /** 501 * {@inheritDoc} 502 */ 503 @Override 504 @SuppressWarnings({ "unchecked" }) 505 public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search) 506 { 507 try 508 { 509 search.setPageNumber(pageNumber); 510 search.setPageSize(pageSize); 511 final PropertySearch propertySearch = new PropertySearch( 512 this.getSession(), TaxonGroupLengthPmfmImpl.class, search); 513 final List results = propertySearch.executeAsList(); 514 this.transformEntities(transform, results); 515 return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount()); 516 } 517 catch (HibernateException ex) 518 { 519 throw ex; /*super.convertHibernateAccessException(ex);*/ 520 } 521 } 522 523 /** 524 * {@inheritDoc} 525 */ 526 @Override 527 public PaginationResult search(final int pageNumber, final int pageSize, final Search search) 528 { 529 return this.search(TaxonGroupLengthPmfmDao.TRANSFORM_NONE, pageNumber, pageSize, search); 530 } 531 532 /** 533 * {@inheritDoc} 534 */ 535 @Override 536 public Set<?> search(final int transform, final Search search) 537 { 538 try 539 { 540 final PropertySearch propertySearch = new PropertySearch( 541 this.getSession(), TaxonGroupLengthPmfmImpl.class, search); 542 final Set<?> results = propertySearch.executeAsSet(); 543 this.transformEntities(transform, results); 544 return results; 545 } 546 catch (HibernateException ex) 547 { 548 throw ex; /*super.convertHibernateAccessException(ex);*/ 549 } 550 } 551 552 /** 553 * {@inheritDoc} 554 */ 555 @Override 556 @SuppressWarnings("unchecked") 557 public Set<TaxonGroupLengthPmfm> search(final Search search) 558 { 559 return (Set<TaxonGroupLengthPmfm>) this.search(TaxonGroupLengthPmfmDao.TRANSFORM_NONE, search); 560 } 561 562 /** 563 * Executes and returns the given Hibernate queryObject as a {@link PaginationResult} instance. 564 * @param queryObject 565 * @param transform 566 * @param pageNumber 567 * @param pageSize 568 * @return PaginationResult 569 */ 570 @SuppressWarnings({ "unchecked" }) 571 protected PaginationResult getPaginationResult( 572 final Query queryObject, 573 final int transform, int pageNumber, int pageSize) 574 { 575 try 576 { 577 final ScrollableResults scrollableResults = queryObject.scroll(); 578 scrollableResults.last(); 579 int totalCount = scrollableResults.getRowNumber(); 580 totalCount = totalCount >= 0 ? totalCount + 1 : 0; 581 if (pageNumber > 0 && pageSize > 0) 582 { 583 queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize)); 584 queryObject.setMaxResults(pageSize); 585 } 586 // Unchecked transformation because Set object is reused, cannot be strongly typed. 587 Set results = new LinkedHashSet(queryObject.list()); 588 transformEntities(transform, results); 589 return new PaginationResult(results.toArray(new Object[results.size()]), totalCount); 590 } 591 catch (HibernateException ex) 592 { 593 throw ex; /*super.convertHibernateAccessException(ex);*/ 594 } 595 } 596 597 // spring-hibernate-dao-base merge-point 598}