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