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