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