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