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.fishingArea; 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.referential.location.Location; 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>FishingArea2RegulationLocation</code>. 054 * </p> 055 * 056 * @see FishingArea2RegulationLocation 057 */ 058public abstract class FishingArea2RegulationLocationDaoBase 059 extends HibernateDaoSupport 060 implements FishingArea2RegulationLocationDao 061{ 062 /** 063 * {@inheritDoc} 064 */ 065 @Override 066 public Object get(final int transform, final FishingArea2RegulationLocationPK fishingArea2RegulationLocationPk) 067 { 068 if (fishingArea2RegulationLocationPk == null) 069 { 070 throw new IllegalArgumentException( 071 "FishingArea2RegulationLocation.get - 'fishingArea2RegulationLocationPk' can not be null"); 072 } 073 final FishingArea2RegulationLocation entity = get(FishingArea2RegulationLocationImpl.class, fishingArea2RegulationLocationPk); 074 return transformEntity(transform, entity); 075 } 076 /** 077 * {@inheritDoc} 078 */ 079 @Override 080 public FishingArea2RegulationLocation get(FishingArea2RegulationLocationPK fishingArea2RegulationLocationPk) 081 { 082 return (FishingArea2RegulationLocation)this.get(TRANSFORM_NONE, fishingArea2RegulationLocationPk); 083 } 084 085 /** 086 * {@inheritDoc} 087 */ 088 @Override 089 public Object load(final int transform, final FishingArea2RegulationLocationPK fishingArea2RegulationLocationPk) 090 { 091 if (fishingArea2RegulationLocationPk == null) 092 { 093 throw new IllegalArgumentException( 094 "FishingArea2RegulationLocation.load - 'fishingArea2RegulationLocationPk' can not be null"); 095 } 096 final FishingArea2RegulationLocation entity = get(FishingArea2RegulationLocationImpl.class, fishingArea2RegulationLocationPk); 097 return transformEntity(transform, entity); 098 } 099 100 /** 101 * {@inheritDoc} 102 */ 103 @Override 104 public FishingArea2RegulationLocation load(FishingArea2RegulationLocationPK fishingArea2RegulationLocationPk) 105 { 106 return (FishingArea2RegulationLocation)this.load(TRANSFORM_NONE, fishingArea2RegulationLocationPk); 107 } 108 109 /** 110 * {@inheritDoc} 111 */ 112 @Override 113 @SuppressWarnings({"unchecked"}) 114 public Collection<FishingArea2RegulationLocation> loadAll() 115 { 116 return (Collection<FishingArea2RegulationLocation>) this.loadAll(FishingArea2RegulationLocationDao.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(FishingArea2RegulationLocationDao.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(FishingArea2RegulationLocationImpl.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 FishingArea2RegulationLocation create(FishingArea2RegulationLocation fishingArea2RegulationLocation) 182 { 183 return (FishingArea2RegulationLocation)this.create(FishingArea2RegulationLocationDao.TRANSFORM_NONE, fishingArea2RegulationLocation); 184 } 185 186 /** 187 * {@inheritDoc} 188 */ 189 @Override 190 public Object create(final int transform, final FishingArea2RegulationLocation fishingArea2RegulationLocation) 191 { 192 if (fishingArea2RegulationLocation == null) 193 { 194 throw new IllegalArgumentException( 195 "FishingArea2RegulationLocation.create - 'fishingArea2RegulationLocation' can not be null"); 196 } 197 this.getSessionFactory().getCurrentSession().save(fishingArea2RegulationLocation); 198 return this.transformEntity(transform, fishingArea2RegulationLocation); 199 } 200 201 /** 202 * {@inheritDoc} 203 */ 204 @Override 205 @SuppressWarnings({"unchecked"}) 206 public Collection<FishingArea2RegulationLocation> create(final Collection<FishingArea2RegulationLocation> entities) 207 { 208 return (Collection<FishingArea2RegulationLocation>) create(FishingArea2RegulationLocationDao.TRANSFORM_NONE, entities); 209 } 210 211 /** 212 * {@inheritDoc} 213 */ 214 @Override 215 public Collection<?> create(final int transform, final Collection<FishingArea2RegulationLocation> entities) 216 { 217 if (entities == null) 218 { 219 throw new IllegalArgumentException( 220 "FishingArea2RegulationLocation.create - 'entities' can not be null"); 221 } 222 for (FishingArea2RegulationLocation entity : entities) 223 { 224 create(transform, entity); 225 } 226 return entities; 227 } 228 229 /** 230 * {@inheritDoc} 231 */ 232 @Override 233 public FishingArea2RegulationLocation create( 234 FishingArea fishingArea, 235 Location location) 236 { 237 return (FishingArea2RegulationLocation)this.create(FishingArea2RegulationLocationDao.TRANSFORM_NONE, fishingArea, location); 238 } 239 240 /** 241 * {@inheritDoc} 242 */ 243 @Override 244 public Object create( 245 final int transform, 246 FishingArea fishingArea, 247 Location location) 248 { 249 FishingArea2RegulationLocation entity = new FishingArea2RegulationLocationImpl(); 250 entity.setFishingArea(fishingArea); 251 entity.setLocation(location); 252 return this.create(transform, entity); 253 } 254 255 /** 256 * {@inheritDoc} 257 */ 258 @Override 259 public void update(FishingArea2RegulationLocation fishingArea2RegulationLocation) 260 { 261 if (fishingArea2RegulationLocation == null) 262 { 263 throw new IllegalArgumentException( 264 "FishingArea2RegulationLocation.update - 'fishingArea2RegulationLocation' can not be null"); 265 } 266 this.getSessionFactory().getCurrentSession().update(fishingArea2RegulationLocation); 267 } 268 269 /** 270 * {@inheritDoc} 271 */ 272 @Override 273 public void update(final Collection<FishingArea2RegulationLocation> entities) 274 { 275 if (entities == null) 276 { 277 throw new IllegalArgumentException( 278 "FishingArea2RegulationLocation.update - 'entities' can not be null"); 279 } 280 for (FishingArea2RegulationLocation entity : entities) 281 { 282 update(entity); 283 } 284 } 285 286 /** 287 * {@inheritDoc} 288 */ 289 @Override 290 public void remove(FishingArea2RegulationLocation fishingArea2RegulationLocation) 291 { 292 if (fishingArea2RegulationLocation == null) 293 { 294 throw new IllegalArgumentException( 295 "FishingArea2RegulationLocation.remove - 'fishingArea2RegulationLocation' can not be null"); 296 } 297 this.getSessionFactory().getCurrentSession().delete(fishingArea2RegulationLocation); 298 } 299 300 /** 301 * {@inheritDoc} 302 */ 303 @Override 304 public void remove(FishingArea2RegulationLocationPK fishingArea2RegulationLocationPk) 305 { 306 if (fishingArea2RegulationLocationPk == null) 307 { 308 throw new IllegalArgumentException( 309 "FishingArea2RegulationLocation.remove - 'fishingArea2RegulationLocationPk' can not be null"); 310 } 311 FishingArea2RegulationLocation entity = this.get(fishingArea2RegulationLocationPk); 312 if (entity != null) 313 { 314 this.remove(entity); 315 } 316 } 317 318 /** 319 * {@inheritDoc} 320 */ 321 @Override 322 public void remove(Collection<FishingArea2RegulationLocation> entities) 323 { 324 if (entities == null) 325 { 326 throw new IllegalArgumentException( 327 "FishingArea2RegulationLocation.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>FishingArea2RegulationLocationDao</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 FishingArea2RegulationLocationDao} 341 * @param entity an entity that was found 342 * @return the transformed entity (i.e. new value object, etc) 343 * @see FishingArea2RegulationLocationDao#transformEntity(int, FishingArea2RegulationLocation) 344 */ 345 public Object transformEntity(final int transform, final FishingArea2RegulationLocation entity) 346 { 347 Object target = null; 348 if (entity != null) 349 { 350 switch (transform) 351 { 352 case FishingArea2RegulationLocationDao.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 FishingArea2RegulationLocationDao.TRANSFORM_NONE : // fall-through 369 default: 370 // do nothing; 371 } 372 } 373 374 /** 375 * @see FishingArea2RegulationLocationDao#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 FishingArea2RegulationLocation} 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 FishingArea2RegulationLocation) 401 { 402 result = input; 403 } 404 return result; 405 } 406 }; 407 408 /** 409 * @param row 410 * @return FishingArea2RegulationLocation 411 */ 412 protected FishingArea2RegulationLocation toEntity(Object[] row) 413 { 414 FishingArea2RegulationLocation 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 FishingArea2RegulationLocation) 422 { 423 target = (FishingArea2RegulationLocation)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(), FishingArea2RegulationLocationImpl.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(FishingArea2RegulationLocationDao.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(), FishingArea2RegulationLocationImpl.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<FishingArea2RegulationLocation> search(final Search search) 500 { 501 return (Set<FishingArea2RegulationLocation>) this.search(FishingArea2RegulationLocationDao.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}