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