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