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.grouping;
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.referential.ObjectType;
035import fr.ifremer.adagio.core.dao.technical.hibernate.HibernateDaoSupport;
036import java.security.Principal;
037import java.sql.Timestamp;
038import java.util.Collection;
039import java.util.LinkedHashSet;
040import java.util.List;
041import java.util.Set;
042import javax.annotation.Resource;
043import org.andromda.spring.PaginationResult;
044import org.apache.commons.collections.CollectionUtils;
045import org.apache.commons.collections.Transformer;
046import org.hibernate.Criteria;
047import org.hibernate.HibernateException;
048import org.hibernate.Query;
049import org.hibernate.ScrollableResults;
050
051/**
052 * <p>
053 * Base Spring DAO Class: is able to create, update, remove, load, and find
054 * objects of type <code>GroupingClassification</code>.
055 * </p>
056 *
057 * @see GroupingClassification
058 */
059public abstract class GroupingClassificationDaoBase
060    extends HibernateDaoSupport    
061    implements GroupingClassificationDao
062{
063    /**
064     * {@inheritDoc}
065     */
066    @Override
067    public Object get(final int transform, final Integer id)
068    {
069        if (id == null)
070        {
071            throw new IllegalArgumentException(
072                "GroupingClassification.get - 'id' can not be null");
073        }
074        final GroupingClassification entity = get(GroupingClassificationImpl.class, id);
075        return transformEntity(transform, entity);
076    }
077    /**
078     * {@inheritDoc}
079     */
080    @Override
081    public GroupingClassification get(Integer id)
082    {
083        return (GroupingClassification)this.get(TRANSFORM_NONE, id);
084    }
085
086    /**
087     * {@inheritDoc}
088     */
089    @Override
090    public Object load(final int transform, final Integer id)
091    {
092        if (id == null)
093        {
094            throw new IllegalArgumentException(
095                "GroupingClassification.load - 'id' can not be null");
096        }
097        final GroupingClassification entity = get(GroupingClassificationImpl.class, id);
098        return transformEntity(transform, entity);
099    }
100
101    /**
102     * {@inheritDoc}
103     */
104    @Override
105    public GroupingClassification load(Integer id)
106    {
107        return (GroupingClassification)this.load(TRANSFORM_NONE, id);
108    }
109
110    /**
111     * {@inheritDoc}
112     */
113    @Override
114    @SuppressWarnings({"unchecked"})
115    public Collection<GroupingClassification> loadAll()
116    {
117        return (Collection<GroupingClassification>) this.loadAll(GroupingClassificationDao.TRANSFORM_NONE);
118    }
119
120    /**
121     * {@inheritDoc}
122     */
123    @Override
124    public Collection<?> loadAll(final int transform)
125    {
126        return this.loadAll(transform, -1, -1);
127    }
128
129    /**
130     * {@inheritDoc}
131     */
132    @Override
133    public Collection<?> loadAll(final int pageNumber, final int pageSize)
134    {
135        return this.loadAll(GroupingClassificationDao.TRANSFORM_NONE, pageNumber, pageSize);
136    }
137
138    /**
139     * {@inheritDoc}
140     */
141    @Override
142    public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize)
143    {
144        try
145        {
146            final Criteria criteria = this.getSession().createCriteria(GroupingClassificationImpl.class);
147            if (pageNumber > 0 && pageSize > 0)
148            {
149                criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
150                criteria.setMaxResults(pageSize);
151            }
152            final Collection<?> results = criteria.list();
153            this.transformEntities(transform, results);
154            return results;
155        }
156        catch (HibernateException ex)
157        {
158            throw ex;
159        }
160    }
161
162    /**
163     * firstResult = (pageNumber - 1) * pageSize
164     * @param pageNumber
165     * @param pageSize
166     * @return firstResult
167     */
168    protected int calculateFirstResult(int pageNumber, int pageSize)
169    {
170        int firstResult = 0;
171        if (pageNumber > 0)
172        {
173            firstResult = (pageNumber - 1) * pageSize;
174        }
175        return firstResult;
176    }
177
178    /**
179     * {@inheritDoc}
180     */
181    @Override
182    public GroupingClassification create(GroupingClassification groupingClassification)
183    {
184        return (GroupingClassification)this.create(GroupingClassificationDao.TRANSFORM_NONE, groupingClassification);
185    }
186
187    /**
188     * {@inheritDoc}
189     */
190    @Override
191    public Object create(final int transform, final GroupingClassification groupingClassification)
192    {
193        if (groupingClassification == null)
194        {
195            throw new IllegalArgumentException(
196                "GroupingClassification.create - 'groupingClassification' can not be null");
197        }
198        this.getSessionFactory().getCurrentSession().save(groupingClassification);
199        return this.transformEntity(transform, groupingClassification);
200    }
201
202    /**
203     * {@inheritDoc}
204     */
205    @Override
206    @SuppressWarnings({"unchecked"})
207    public Collection<GroupingClassification> create(final Collection<GroupingClassification> entities)
208    {
209        return (Collection<GroupingClassification>) create(GroupingClassificationDao.TRANSFORM_NONE, entities);
210    }
211
212    /**
213     * {@inheritDoc}
214     */
215    @Override
216    public Collection<?> create(final int transform, final Collection<GroupingClassification> entities)
217    {
218        if (entities == null)
219        {
220            throw new IllegalArgumentException(
221                "GroupingClassification.create - 'entities' can not be null");
222        }
223                    for (GroupingClassification entity : entities)
224                    {
225                        create(transform, entity);
226                    }
227        return entities;
228    }
229
230    /**
231     * {@inheritDoc}
232     */
233    @Override
234    public GroupingClassification create(
235        String label,
236        String name,
237        String description,
238        Boolean isMandatory,
239        String comments,
240        Timestamp updateDate,
241        String objectReferenceQuery)
242    {
243        return (GroupingClassification)this.create(GroupingClassificationDao.TRANSFORM_NONE, label, name, description, isMandatory, comments, updateDate, objectReferenceQuery);
244    }
245
246    /**
247     * {@inheritDoc}
248     */
249    @Override
250    public Object create(
251        final int transform,
252        String label,
253        String name,
254        String description,
255        Boolean isMandatory,
256        String comments,
257        Timestamp updateDate,
258        String objectReferenceQuery)
259    {
260        GroupingClassification entity = new GroupingClassificationImpl();
261        entity.setLabel(label);
262        entity.setName(name);
263        entity.setDescription(description);
264        entity.setIsMandatory(isMandatory);
265        entity.setComments(comments);
266        entity.setUpdateDate(updateDate);
267        entity.setObjectReferenceQuery(objectReferenceQuery);
268        return this.create(transform, entity);
269    }
270
271    /**
272     * {@inheritDoc}
273     */
274    @Override
275    public GroupingClassification create(
276        Boolean isMandatory,
277        String label,
278        String name,
279        ObjectType objectType,
280        Timestamp updateDate)
281    {
282        return (GroupingClassification)this.create(GroupingClassificationDao.TRANSFORM_NONE, isMandatory, label, name, objectType, updateDate);
283    }
284
285    /**
286     * {@inheritDoc}
287     */
288    @Override
289    public Object create(
290        final int transform,
291        Boolean isMandatory,
292        String label,
293        String name,
294        ObjectType objectType,
295        Timestamp updateDate)
296    {
297        GroupingClassification entity = new GroupingClassificationImpl();
298        entity.setIsMandatory(isMandatory);
299        entity.setLabel(label);
300        entity.setName(name);
301        entity.setObjectType(objectType);
302        entity.setUpdateDate(updateDate);
303        return this.create(transform, entity);
304    }
305
306    /**
307     * {@inheritDoc}
308     */
309    @Override
310    public void update(GroupingClassification groupingClassification)
311    {
312        if (groupingClassification == null)
313        {
314            throw new IllegalArgumentException(
315                "GroupingClassification.update - 'groupingClassification' can not be null");
316        }
317        this.getSessionFactory().getCurrentSession().update(groupingClassification);
318    }
319
320    /**
321     * {@inheritDoc}
322     */
323    @Override
324    public void update(final Collection<GroupingClassification> entities)
325    {
326        if (entities == null)
327        {
328            throw new IllegalArgumentException(
329                "GroupingClassification.update - 'entities' can not be null");
330        }
331                    for (GroupingClassification entity : entities)
332                    {
333                        update(entity);
334                    }
335    }
336
337    /**
338     * {@inheritDoc}
339     */
340    @Override
341    public void remove(GroupingClassification groupingClassification)
342    {
343        if (groupingClassification == null)
344        {
345            throw new IllegalArgumentException(
346                "GroupingClassification.remove - 'groupingClassification' can not be null");
347        }
348        this.getSessionFactory().getCurrentSession().delete(groupingClassification);
349    }
350
351    /**
352     * {@inheritDoc}
353     */
354    @Override
355    public void remove(Integer id)
356    {
357        if (id == null)
358        {
359            throw new IllegalArgumentException(
360                "GroupingClassification.remove - 'id' can not be null");
361        }
362        GroupingClassification entity = this.get(id);
363        if (entity != null)
364        {
365            this.remove(entity);
366        }
367    }
368
369    /**
370     * {@inheritDoc}
371     */
372    @Override
373    public void remove(Collection<GroupingClassification> entities)
374    {
375        if (entities == null)
376        {
377            throw new IllegalArgumentException(
378                "GroupingClassification.remove - 'entities' can not be null");
379        }
380        deleteAll(entities);
381    }
382    /**
383     * Allows transformation of entities into value objects
384     * (or something else for that matter), when the <code>transform</code>
385     * flag is set to one of the constants defined in <code>GroupingClassificationDao</code>, please note
386     * that the {@link #TRANSFORM_NONE} constant denotes no transformation, so the entity itself
387     * will be returned.
388     *
389     * If the integer argument value is unknown {@link #TRANSFORM_NONE} is assumed.
390     *
391     * @param transform one of the constants declared in {@link GroupingClassificationDao}
392     * @param entity an entity that was found
393     * @return the transformed entity (i.e. new value object, etc)
394     * @see GroupingClassificationDao#transformEntity(int, GroupingClassification)
395     */
396    public Object transformEntity(final int transform, final GroupingClassification entity)
397    {
398        Object target = null;
399        if (entity != null)
400        {
401            switch (transform)
402            {
403                case GroupingClassificationDao.TRANSFORM_NONE : // fall-through
404                default:
405                    target = entity;
406            }
407        }
408        return target;
409    }
410
411    /**
412     * {@inheritDoc}
413     */
414    @Override
415    public void transformEntities(final int transform, final Collection<?> entities)
416    {
417        switch (transform)
418        {
419            case GroupingClassificationDao.TRANSFORM_NONE : // fall-through
420                default:
421                // do nothing;
422        }
423    }
424
425    /**
426     * @see GroupingClassificationDao#toEntities(Collection)
427     */
428    public void toEntities(final Collection<?> results)
429    {
430        if (results != null)
431        {
432            CollectionUtils.transform(results, this.ENTITYTRANSFORMER);
433        }
434    }
435
436    /**
437     * This anonymous transformer is designed to transform report query results
438     * (which result in an array of entities) to {@link GroupingClassification}
439     * using the Jakarta Commons-Collections Transformation API.
440     */
441    private Transformer ENTITYTRANSFORMER =
442        new Transformer()
443        {
444            public Object transform(Object input)
445            {
446                Object result = null;
447                if (input instanceof Object[])
448                {
449                    result = toEntity((Object[])input);
450                }
451                else if (input instanceof GroupingClassification)
452                {
453                    result = input;
454                }
455                return result;
456            }
457        };
458
459    /**
460     * @param row
461     * @return GroupingClassification
462     */
463    protected GroupingClassification toEntity(Object[] row)
464    {
465        GroupingClassification target = null;
466        if (row != null)
467        {
468            final int numberOfObjects = row.length;
469            for (int ctr = 0; ctr < numberOfObjects; ctr++)
470            {
471                final Object object = row[ctr];
472                if (object instanceof GroupingClassification)
473                {
474                    target = (GroupingClassification)object;
475                    break;
476                }
477            }
478        }
479        return target;
480    }
481
482    /**
483     * Gets the current <code>principal</code> if one has been set,
484     * otherwise returns <code>null</code>.
485     *
486     * @return the current principal
487     */
488    protected Principal getPrincipal()
489    {
490        return PrincipalStore.get();
491    }
492
493    /**
494     * {@inheritDoc}
495     */
496    @Override
497    @SuppressWarnings({ "unchecked" })
498    public PaginationResult search(final int transform, final int pageNumber, final int pageSize, final Search search)
499    {
500        try
501        {
502            search.setPageNumber(pageNumber);
503            search.setPageSize(pageSize);
504            final PropertySearch propertySearch = new PropertySearch(
505                this.getSession(), GroupingClassificationImpl.class, search);
506            final List results = propertySearch.executeAsList();
507            this.transformEntities(transform, results);
508            return new PaginationResult(results.toArray(new Object[results.size()]), propertySearch.getTotalCount());
509        }
510        catch (HibernateException ex)
511        {
512            throw ex; /*super.convertHibernateAccessException(ex);*/
513        }
514    }
515
516    /**
517     * {@inheritDoc}
518     */
519    @Override
520    public PaginationResult search(final int pageNumber, final int pageSize, final Search search)
521    {
522        return this.search(GroupingClassificationDao.TRANSFORM_NONE, pageNumber, pageSize, search);
523    }
524
525    /**
526     * {@inheritDoc}
527     */
528    @Override
529    public Set<?> search(final int transform, final Search search)
530    {
531        try
532        {
533            final PropertySearch propertySearch = new PropertySearch(
534                this.getSession(), GroupingClassificationImpl.class, search);
535            final Set<?> results = propertySearch.executeAsSet();
536            this.transformEntities(transform, results);
537            return results;
538        }
539        catch (HibernateException ex)
540        {
541            throw ex; /*super.convertHibernateAccessException(ex);*/
542        }
543    }
544
545    /**
546     * {@inheritDoc}
547     */
548    @Override
549    @SuppressWarnings("unchecked")
550    public Set<GroupingClassification> search(final Search search)
551    {
552        return (Set<GroupingClassification>) this.search(GroupingClassificationDao.TRANSFORM_NONE, search);
553    }
554
555    /**
556     * Executes and returns the given Hibernate queryObject as a {@link PaginationResult} instance.
557     * @param queryObject
558     * @param transform
559     * @param pageNumber
560     * @param pageSize
561     * @return PaginationResult
562     */
563    @SuppressWarnings({ "unchecked" })
564    protected PaginationResult getPaginationResult(
565        final Query queryObject,
566        final int transform, int pageNumber, int pageSize)
567    {
568        try
569        {
570            final ScrollableResults scrollableResults = queryObject.scroll();
571            scrollableResults.last();
572            int totalCount = scrollableResults.getRowNumber();
573            totalCount = totalCount >= 0 ? totalCount + 1 : 0;
574            if (pageNumber > 0 && pageSize > 0)
575            {
576                queryObject.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
577                queryObject.setMaxResults(pageSize);
578            }
579            // Unchecked transformation because Set object is reused, cannot be strongly typed.
580            Set results = new LinkedHashSet(queryObject.list());
581            transformEntities(transform, results);
582            return new PaginationResult(results.toArray(new Object[results.size()]), totalCount);
583        }
584        catch (HibernateException ex)
585        {
586            throw ex; /*super.convertHibernateAccessException(ex);*/
587        }
588    }
589
590    // spring-hibernate-dao-base merge-point
591}