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