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