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