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