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.gear;
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>GearClassificationAssociation</code>.
054 * </p>
055 *
056 * @see GearClassificationAssociation
057 */
058public abstract class GearClassificationAssociationDaoBase
059    extends HibernateDaoSupport    
060    implements GearClassificationAssociationDao
061{
062    /**
063     * {@inheritDoc}
064     */
065    @Override
066    public Object get(final int transform, final GearClassificationAssociationPK gearClassificationAssociationPk)
067    {
068        if (gearClassificationAssociationPk == null)
069        {
070            throw new IllegalArgumentException(
071                "GearClassificationAssociation.get - 'gearClassificationAssociationPk' can not be null");
072        }
073        final GearClassificationAssociation entity = get(GearClassificationAssociationImpl.class, gearClassificationAssociationPk);
074        return transformEntity(transform, entity);
075    }
076    /**
077     * {@inheritDoc}
078     */
079    @Override
080    public GearClassificationAssociation get(GearClassificationAssociationPK gearClassificationAssociationPk)
081    {
082        return (GearClassificationAssociation)this.get(TRANSFORM_NONE, gearClassificationAssociationPk);
083    }
084
085    /**
086     * {@inheritDoc}
087     */
088    @Override
089    public Object load(final int transform, final GearClassificationAssociationPK gearClassificationAssociationPk)
090    {
091        if (gearClassificationAssociationPk == null)
092        {
093            throw new IllegalArgumentException(
094                "GearClassificationAssociation.load - 'gearClassificationAssociationPk' can not be null");
095        }
096        final GearClassificationAssociation entity = get(GearClassificationAssociationImpl.class, gearClassificationAssociationPk);
097        return transformEntity(transform, entity);
098    }
099
100    /**
101     * {@inheritDoc}
102     */
103    @Override
104    public GearClassificationAssociation load(GearClassificationAssociationPK gearClassificationAssociationPk)
105    {
106        return (GearClassificationAssociation)this.load(TRANSFORM_NONE, gearClassificationAssociationPk);
107    }
108
109    /**
110     * {@inheritDoc}
111     */
112    @Override
113    @SuppressWarnings({"unchecked"})
114    public Collection<GearClassificationAssociation> loadAll()
115    {
116        return (Collection<GearClassificationAssociation>) this.loadAll(GearClassificationAssociationDao.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(GearClassificationAssociationDao.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(GearClassificationAssociationImpl.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 GearClassificationAssociation create(GearClassificationAssociation gearClassificationAssociation)
182    {
183        return (GearClassificationAssociation)this.create(GearClassificationAssociationDao.TRANSFORM_NONE, gearClassificationAssociation);
184    }
185
186    /**
187     * {@inheritDoc}
188     */
189    @Override
190    public Object create(final int transform, final GearClassificationAssociation gearClassificationAssociation)
191    {
192        if (gearClassificationAssociation == null)
193        {
194            throw new IllegalArgumentException(
195                "GearClassificationAssociation.create - 'gearClassificationAssociation' can not be null");
196        }
197        this.getSessionFactory().getCurrentSession().save(gearClassificationAssociation);
198        return this.transformEntity(transform, gearClassificationAssociation);
199    }
200
201    /**
202     * {@inheritDoc}
203     */
204    @Override
205    @SuppressWarnings({"unchecked"})
206    public Collection<GearClassificationAssociation> create(final Collection<GearClassificationAssociation> entities)
207    {
208        return (Collection<GearClassificationAssociation>) create(GearClassificationAssociationDao.TRANSFORM_NONE, entities);
209    }
210
211    /**
212     * {@inheritDoc}
213     */
214    @Override
215    public Collection<?> create(final int transform, final Collection<GearClassificationAssociation> entities)
216    {
217        if (entities == null)
218        {
219            throw new IllegalArgumentException(
220                "GearClassificationAssociation.create - 'entities' can not be null");
221        }
222                    for (GearClassificationAssociation entity : entities)
223                    {
224                        create(transform, entity);
225                    }
226        return entities;
227    }
228
229    /**
230     * {@inheritDoc}
231     */
232    @Override
233    public GearClassificationAssociation create(
234        Timestamp updateDate)
235    {
236        return (GearClassificationAssociation)this.create(GearClassificationAssociationDao.TRANSFORM_NONE, updateDate);
237    }
238
239    /**
240     * {@inheritDoc}
241     */
242    @Override
243    public Object create(
244        final int transform,
245        Timestamp updateDate)
246    {
247        GearClassificationAssociation entity = new GearClassificationAssociationImpl();
248        entity.setUpdateDate(updateDate);
249        return this.create(transform, entity);
250    }
251
252    /**
253     * {@inheritDoc}
254     */
255    @Override
256    public GearClassificationAssociation create(
257        GearClassification fromGearClassification,
258        GearClassification toGearClassification,
259        Timestamp updateDate)
260    {
261        return (GearClassificationAssociation)this.create(GearClassificationAssociationDao.TRANSFORM_NONE, fromGearClassification, toGearClassification, updateDate);
262    }
263
264    /**
265     * {@inheritDoc}
266     */
267    @Override
268    public Object create(
269        final int transform,
270        GearClassification fromGearClassification,
271        GearClassification toGearClassification,
272        Timestamp updateDate)
273    {
274        GearClassificationAssociation entity = new GearClassificationAssociationImpl();
275        entity.setFromGearClassification(fromGearClassification);
276        entity.setToGearClassification(toGearClassification);
277        entity.setUpdateDate(updateDate);
278        return this.create(transform, entity);
279    }
280
281    /**
282     * {@inheritDoc}
283     */
284    @Override
285    public void update(GearClassificationAssociation gearClassificationAssociation)
286    {
287        if (gearClassificationAssociation == null)
288        {
289            throw new IllegalArgumentException(
290                "GearClassificationAssociation.update - 'gearClassificationAssociation' can not be null");
291        }
292        this.getSessionFactory().getCurrentSession().update(gearClassificationAssociation);
293    }
294
295    /**
296     * {@inheritDoc}
297     */
298    @Override
299    public void update(final Collection<GearClassificationAssociation> entities)
300    {
301        if (entities == null)
302        {
303            throw new IllegalArgumentException(
304                "GearClassificationAssociation.update - 'entities' can not be null");
305        }
306                    for (GearClassificationAssociation entity : entities)
307                    {
308                        update(entity);
309                    }
310    }
311
312    /**
313     * {@inheritDoc}
314     */
315    @Override
316    public void remove(GearClassificationAssociation gearClassificationAssociation)
317    {
318        if (gearClassificationAssociation == null)
319        {
320            throw new IllegalArgumentException(
321                "GearClassificationAssociation.remove - 'gearClassificationAssociation' can not be null");
322        }
323        this.getSessionFactory().getCurrentSession().delete(gearClassificationAssociation);
324    }
325
326    /**
327     * {@inheritDoc}
328     */
329    @Override
330    public void remove(GearClassificationAssociationPK gearClassificationAssociationPk)
331    {
332        if (gearClassificationAssociationPk == null)
333        {
334            throw new IllegalArgumentException(
335                "GearClassificationAssociation.remove - 'gearClassificationAssociationPk' can not be null");
336        }
337        GearClassificationAssociation entity = this.get(gearClassificationAssociationPk);
338        if (entity != null)
339        {
340            this.remove(entity);
341        }
342    }
343
344    /**
345     * {@inheritDoc}
346     */
347    @Override
348    public void remove(Collection<GearClassificationAssociation> entities)
349    {
350        if (entities == null)
351        {
352            throw new IllegalArgumentException(
353                "GearClassificationAssociation.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>GearClassificationAssociationDao</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 GearClassificationAssociationDao}
367     * @param entity an entity that was found
368     * @return the transformed entity (i.e. new value object, etc)
369     * @see GearClassificationAssociationDao#transformEntity(int, GearClassificationAssociation)
370     */
371    public Object transformEntity(final int transform, final GearClassificationAssociation entity)
372    {
373        Object target = null;
374        if (entity != null)
375        {
376            switch (transform)
377            {
378                case GearClassificationAssociationDao.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 GearClassificationAssociationDao.TRANSFORM_NONE : // fall-through
395                default:
396                // do nothing;
397        }
398    }
399
400    /**
401     * @see GearClassificationAssociationDao#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 GearClassificationAssociation}
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 GearClassificationAssociation)
427                {
428                    result = input;
429                }
430                return result;
431            }
432        };
433
434    /**
435     * @param row
436     * @return GearClassificationAssociation
437     */
438    protected GearClassificationAssociation toEntity(Object[] row)
439    {
440        GearClassificationAssociation 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 GearClassificationAssociation)
448                {
449                    target = (GearClassificationAssociation)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(), GearClassificationAssociationImpl.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(GearClassificationAssociationDao.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(), GearClassificationAssociationImpl.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<GearClassificationAssociation> search(final Search search)
526    {
527        return (Set<GearClassificationAssociation>) this.search(GearClassificationAssociationDao.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}