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