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