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