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.administration.programStrategy; 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.data.vessel.Vessel; 035import fr.ifremer.adagio.core.dao.technical.hibernate.HibernateDaoSupport; 036import java.security.Principal; 037import java.util.Collection; 038import java.util.LinkedHashSet; 039import java.util.List; 040import java.util.Set; 041import javax.annotation.Resource; 042import org.andromda.spring.PaginationResult; 043import org.apache.commons.collections.CollectionUtils; 044import org.apache.commons.collections.Transformer; 045import org.hibernate.Criteria; 046import org.hibernate.HibernateException; 047import org.hibernate.Query; 048import org.hibernate.ScrollableResults; 049 050/** 051 * <p> 052 * Base Spring DAO Class: is able to create, update, remove, load, and find 053 * objects of type <code>Program2PersonException</code>. 054 * </p> 055 * 056 * @see Program2PersonException 057 */ 058public abstract class Program2PersonExceptionDaoBase 059 extends HibernateDaoSupport 060 implements Program2PersonExceptionDao 061{ 062 /** 063 * {@inheritDoc} 064 */ 065 @Override 066 public Object get(final int transform, final Program2PersonExceptionPK program2PersonExceptionPk) 067 { 068 if (program2PersonExceptionPk == null) 069 { 070 throw new IllegalArgumentException( 071 "Program2PersonException.get - 'program2PersonExceptionPk' can not be null"); 072 } 073 final Program2PersonException entity = get(Program2PersonExceptionImpl.class, program2PersonExceptionPk); 074 return transformEntity(transform, entity); 075 } 076 /** 077 * {@inheritDoc} 078 */ 079 @Override 080 public Program2PersonException get(Program2PersonExceptionPK program2PersonExceptionPk) 081 { 082 return (Program2PersonException)this.get(TRANSFORM_NONE, program2PersonExceptionPk); 083 } 084 085 /** 086 * {@inheritDoc} 087 */ 088 @Override 089 public Object load(final int transform, final Program2PersonExceptionPK program2PersonExceptionPk) 090 { 091 if (program2PersonExceptionPk == null) 092 { 093 throw new IllegalArgumentException( 094 "Program2PersonException.load - 'program2PersonExceptionPk' can not be null"); 095 } 096 final Program2PersonException entity = get(Program2PersonExceptionImpl.class, program2PersonExceptionPk); 097 return transformEntity(transform, entity); 098 } 099 100 /** 101 * {@inheritDoc} 102 */ 103 @Override 104 public Program2PersonException load(Program2PersonExceptionPK program2PersonExceptionPk) 105 { 106 return (Program2PersonException)this.load(TRANSFORM_NONE, program2PersonExceptionPk); 107 } 108 109 /** 110 * {@inheritDoc} 111 */ 112 @Override 113 @SuppressWarnings({"unchecked"}) 114 public Collection<Program2PersonException> loadAll() 115 { 116 return (Collection<Program2PersonException>) this.loadAll(Program2PersonExceptionDao.TRANSFORM_NONE); 117 } 118 119 /** 120 * {@inheritDoc} 121 */ 122 @Override 123 public Collection<?> loadAll(final int transform) 124 { 125 return this.loadAll(transform, -1, -1); 126 } 127 128 /** 129 * {@inheritDoc} 130 */ 131 @Override 132 public Collection<?> loadAll(final int pageNumber, final int pageSize) 133 { 134 return this.loadAll(Program2PersonExceptionDao.TRANSFORM_NONE, pageNumber, pageSize); 135 } 136 137 /** 138 * {@inheritDoc} 139 */ 140 @Override 141 public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize) 142 { 143 try 144 { 145 final Criteria criteria = this.getSession().createCriteria(Program2PersonExceptionImpl.class); 146 if (pageNumber > 0 && pageSize > 0) 147 { 148 criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize)); 149 criteria.setMaxResults(pageSize); 150 } 151 final Collection<?> results = criteria.list(); 152 this.transformEntities(transform, results); 153 return results; 154 } 155 catch (HibernateException ex) 156 { 157 throw ex; 158 } 159 } 160 161 /** 162 * firstResult = (pageNumber - 1) * pageSize 163 * @param pageNumber 164 * @param pageSize 165 * @return firstResult 166 */ 167 protected int calculateFirstResult(int pageNumber, int pageSize) 168 { 169 int firstResult = 0; 170 if (pageNumber > 0) 171 { 172 firstResult = (pageNumber - 1) * pageSize; 173 } 174 return firstResult; 175 } 176 177 /** 178 * {@inheritDoc} 179 */ 180 @Override 181 public Program2PersonException create(Program2PersonException program2PersonException) 182 { 183 return (Program2PersonException)this.create(Program2PersonExceptionDao.TRANSFORM_NONE, program2PersonException); 184 } 185 186 /** 187 * {@inheritDoc} 188 */ 189 @Override 190 public Object create(final int transform, final Program2PersonException program2PersonException) 191 { 192 if (program2PersonException == null) 193 { 194 throw new IllegalArgumentException( 195 "Program2PersonException.create - 'program2PersonException' can not be null"); 196 } 197 this.getSessionFactory().getCurrentSession().save(program2PersonException); 198 return this.transformEntity(transform, program2PersonException); 199 } 200 201 /** 202 * {@inheritDoc} 203 */ 204 @Override 205 @SuppressWarnings({"unchecked"}) 206 public Collection<Program2PersonException> create(final Collection<Program2PersonException> entities) 207 { 208 return (Collection<Program2PersonException>) create(Program2PersonExceptionDao.TRANSFORM_NONE, entities); 209 } 210 211 /** 212 * {@inheritDoc} 213 */ 214 @Override 215 public Collection<?> create(final int transform, final Collection<Program2PersonException> entities) 216 { 217 if (entities == null) 218 { 219 throw new IllegalArgumentException( 220 "Program2PersonException.create - 'entities' can not be null"); 221 } 222 for (Program2PersonException entity : entities) 223 { 224 create(transform, entity); 225 } 226 return entities; 227 } 228 229 /** 230 * {@inheritDoc} 231 */ 232 @Override 233 public Program2PersonException create( 234 Boolean isExclude, 235 String description) 236 { 237 return (Program2PersonException)this.create(Program2PersonExceptionDao.TRANSFORM_NONE, isExclude, description); 238 } 239 240 /** 241 * {@inheritDoc} 242 */ 243 @Override 244 public Object create( 245 final int transform, 246 Boolean isExclude, 247 String description) 248 { 249 Program2PersonException entity = new Program2PersonExceptionImpl(); 250 entity.setIsExclude(isExclude); 251 entity.setDescription(description); 252 return this.create(transform, entity); 253 } 254 255 /** 256 * {@inheritDoc} 257 */ 258 @Override 259 public Program2PersonException create( 260 Boolean isExclude, 261 Program2Person program2Person, 262 Vessel vessel) 263 { 264 return (Program2PersonException)this.create(Program2PersonExceptionDao.TRANSFORM_NONE, isExclude, program2Person, vessel); 265 } 266 267 /** 268 * {@inheritDoc} 269 */ 270 @Override 271 public Object create( 272 final int transform, 273 Boolean isExclude, 274 Program2Person program2Person, 275 Vessel vessel) 276 { 277 Program2PersonException entity = new Program2PersonExceptionImpl(); 278 entity.setIsExclude(isExclude); 279 entity.setProgram2Person(program2Person); 280 entity.setVessel(vessel); 281 return this.create(transform, entity); 282 } 283 284 /** 285 * {@inheritDoc} 286 */ 287 @Override 288 public void update(Program2PersonException program2PersonException) 289 { 290 if (program2PersonException == null) 291 { 292 throw new IllegalArgumentException( 293 "Program2PersonException.update - 'program2PersonException' can not be null"); 294 } 295 this.getSessionFactory().getCurrentSession().update(program2PersonException); 296 } 297 298 /** 299 * {@inheritDoc} 300 */ 301 @Override 302 public void update(final Collection<Program2PersonException> entities) 303 { 304 if (entities == null) 305 { 306 throw new IllegalArgumentException( 307 "Program2PersonException.update - 'entities' can not be null"); 308 } 309 for (Program2PersonException entity : entities) 310 { 311 update(entity); 312 } 313 } 314 315 /** 316 * {@inheritDoc} 317 */ 318 @Override 319 public void remove(Program2PersonException program2PersonException) 320 { 321 if (program2PersonException == null) 322 { 323 throw new IllegalArgumentException( 324 "Program2PersonException.remove - 'program2PersonException' can not be null"); 325 } 326 this.getSessionFactory().getCurrentSession().delete(program2PersonException); 327 } 328 329 /** 330 * {@inheritDoc} 331 */ 332 @Override 333 public void remove(Program2PersonExceptionPK program2PersonExceptionPk) 334 { 335 if (program2PersonExceptionPk == null) 336 { 337 throw new IllegalArgumentException( 338 "Program2PersonException.remove - 'program2PersonExceptionPk' can not be null"); 339 } 340 Program2PersonException entity = this.get(program2PersonExceptionPk); 341 if (entity != null) 342 { 343 this.remove(entity); 344 } 345 } 346 347 /** 348 * {@inheritDoc} 349 */ 350 @Override 351 public void remove(Collection<Program2PersonException> entities) 352 { 353 if (entities == null) 354 { 355 throw new IllegalArgumentException( 356 "Program2PersonException.remove - 'entities' can not be null"); 357 } 358 deleteAll(entities); 359 } 360 /** 361 * Allows transformation of entities into value objects 362 * (or something else for that matter), when the <code>transform</code> 363 * flag is set to one of the constants defined in <code>Program2PersonExceptionDao</code>, please note 364 * that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself 365 * will be returned. 366 * 367 * If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed. 368 * 369 * @param transform one of the constants declared in {@link Program2PersonExceptionDao} 370 * @param entity an entity that was found 371 * @return the transformed entity (i.e. new value object, etc) 372 * @see Program2PersonExceptionDao#transformEntity(int, Program2PersonException) 373 */ 374 public Object transformEntity(final int transform, final Program2PersonException entity) 375 { 376 Object target = null; 377 if (entity != null) 378 { 379 switch (transform) 380 { 381 case Program2PersonExceptionDao.TRANSFORM_NONE : // fall-through 382 default: 383 target = entity; 384 } 385 } 386 return target; 387 } 388 389 /** 390 * {@inheritDoc} 391 */ 392 @Override 393 public void transformEntities(final int transform, final Collection<?> entities) 394 { 395 switch (transform) 396 { 397 case Program2PersonExceptionDao.TRANSFORM_NONE : // fall-through 398 default: 399 // do nothing; 400 } 401 } 402 403 /** 404 * @see Program2PersonExceptionDao#toEntities(Collection) 405 */ 406 public void toEntities(final Collection<?> results) 407 { 408 if (results != null) 409 { 410 CollectionUtils.transform(results, this.ENTITYTRANSFORMER); 411 } 412 } 413 414 /** 415 * This anonymous transformer is designed to transform report query results 416 * (which result in an array of entities) to {@link Program2PersonException} 417 * using the Jakarta Commons-Collections Transformation API. 418 */ 419 private Transformer ENTITYTRANSFORMER = 420 new Transformer() 421 { 422 public Object transform(Object input) 423 { 424 Object result = null; 425 if (input instanceof Object[]) 426 { 427 result = toEntity((Object[])input); 428 } 429 else if (input instanceof Program2PersonException) 430 { 431 result = input; 432 } 433 return result; 434 } 435 }; 436 437 /** 438 * @param row 439 * @return Program2PersonException 440 */ 441 protected Program2PersonException toEntity(Object[] row) 442 { 443 Program2PersonException target = null; 444 if (row != null) 445 { 446 final int numberOfObjects = row.length; 447 for (int ctr = 0; ctr < numberOfObjects; ctr++) 448 { 449 final Object object = row[ctr]; 450 if (object instanceof Program2PersonException) 451 { 452 target = (Program2PersonException)object; 453 break; 454 } 455 } 456 } 457 return target; 458 } 459 460 /** 461 * Gets the current <code>principal</code> if one has been set, 462 * otherwise returns <code>null</code>. 463 * 464 * @return the current principal 465 */ 466 protected Principal getPrincipal() 467 { 468 return PrincipalStore.get(); 469 } 470 471 /** 472 * {@inheritDoc} 473 */ 474 @Override 475 @SuppressWarnings({ "unchecked" }) 476 public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search) 477 { 478 try 479 { 480 search.setPageNumber(pageNumber); 481 search.setPageSize(pageSize); 482 final PropertySearch propertySearch = new PropertySearch( 483 this.getSession(), Program2PersonExceptionImpl.class, search); 484 final List results = propertySearch.executeAsList(); 485 this.transformEntities(transform, results); 486 return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount()); 487 } 488 catch (HibernateException ex) 489 { 490 throw ex; /*super.convertHibernateAccessException(ex);*/ 491 } 492 } 493 494 /** 495 * {@inheritDoc} 496 */ 497 @Override 498 public PaginationResult search(final int pageNumber, final int pageSize, final Search search) 499 { 500 return this.search(Program2PersonExceptionDao.TRANSFORM_NONE, pageNumber, pageSize, search); 501 } 502 503 /** 504 * {@inheritDoc} 505 */ 506 @Override 507 public Set<?> search(final int transform, final Search search) 508 { 509 try 510 { 511 final PropertySearch propertySearch = new PropertySearch( 512 this.getSession(), Program2PersonExceptionImpl.class, search); 513 final Set<?> results = propertySearch.executeAsSet(); 514 this.transformEntities(transform, results); 515 return results; 516 } 517 catch (HibernateException ex) 518 { 519 throw ex; /*super.convertHibernateAccessException(ex);*/ 520 } 521 } 522 523 /** 524 * {@inheritDoc} 525 */ 526 @Override 527 @SuppressWarnings("unchecked") 528 public Set<Program2PersonException> search(final Search search) 529 { 530 return (Set<Program2PersonException>) this.search(Program2PersonExceptionDao.TRANSFORM_NONE, search); 531 } 532 533 /** 534 * Executes and returns the given Hibernate queryObject as a {@link PaginationResult} instance. 535 * @param queryObject 536 * @param transform 537 * @param pageNumber 538 * @param pageSize 539 * @return PaginationResult 540 */ 541 @SuppressWarnings({ "unchecked" }) 542 protected PaginationResult getPaginationResult( 543 final Query queryObject, 544 final int transform, int pageNumber, int pageSize) 545 { 546 try 547 { 548 final ScrollableResults scrollableResults = queryObject.scroll(); 549 scrollableResults.last(); 550 int totalCount = scrollableResults.getRowNumber(); 551 totalCount = totalCount >= 0 ? totalCount + 1 : 0; 552 if (pageNumber > 0 && pageSize > 0) 553 { 554 queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize)); 555 queryObject.setMaxResults(pageSize); 556 } 557 // Unchecked transformation because Set object is reused, cannot be strongly typed. 558 Set results = new LinkedHashSet(queryObject.list()); 559 transformEntities(transform, results); 560 return new PaginationResult(results.toArray(new Object[results.size()]), totalCount); 561 } 562 catch (HibernateException ex) 563 { 564 throw ex; /*super.convertHibernateAccessException(ex);*/ 565 } 566 } 567 568 // spring-hibernate-dao-base merge-point 569}