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