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