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