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