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