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;
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>AnalysisInstrument</code>.
054 * </p>
055 *
056 * @see AnalysisInstrument
057 */
058public abstract class AnalysisInstrumentDaoBase
059    extends HibernateDaoSupport    
060    implements AnalysisInstrumentDao
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                "AnalysisInstrument.get - 'id' can not be null");
072        }
073        final AnalysisInstrument entity = get(AnalysisInstrumentImpl.class, id);
074        return transformEntity(transform, entity);
075    }
076    /**
077     * {@inheritDoc}
078     */
079    @Override
080    public AnalysisInstrument get(Integer id)
081    {
082        return (AnalysisInstrument)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                "AnalysisInstrument.load - 'id' can not be null");
095        }
096        final AnalysisInstrument entity = get(AnalysisInstrumentImpl.class, id);
097        return transformEntity(transform, entity);
098    }
099
100    /**
101     * {@inheritDoc}
102     */
103    @Override
104    public AnalysisInstrument load(Integer id)
105    {
106        return (AnalysisInstrument)this.load(TRANSFORM_NONE, id);
107    }
108
109    /**
110     * {@inheritDoc}
111     */
112    @Override
113    @SuppressWarnings({"unchecked"})
114    public Collection<AnalysisInstrument> loadAll()
115    {
116        return (Collection<AnalysisInstrument>) this.loadAll(AnalysisInstrumentDao.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(AnalysisInstrumentDao.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(AnalysisInstrumentImpl.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 AnalysisInstrument create(AnalysisInstrument analysisInstrument)
182    {
183        return (AnalysisInstrument)this.create(AnalysisInstrumentDao.TRANSFORM_NONE, analysisInstrument);
184    }
185
186    /**
187     * {@inheritDoc}
188     */
189    @Override
190    public Object create(final int transform, final AnalysisInstrument analysisInstrument)
191    {
192        if (analysisInstrument == null)
193        {
194            throw new IllegalArgumentException(
195                "AnalysisInstrument.create - 'analysisInstrument' can not be null");
196        }
197        this.getSessionFactory().getCurrentSession().save(analysisInstrument);
198        return this.transformEntity(transform, analysisInstrument);
199    }
200
201    /**
202     * {@inheritDoc}
203     */
204    @Override
205    @SuppressWarnings({"unchecked"})
206    public Collection<AnalysisInstrument> create(final Collection<AnalysisInstrument> entities)
207    {
208        return (Collection<AnalysisInstrument>) create(AnalysisInstrumentDao.TRANSFORM_NONE, entities);
209    }
210
211    /**
212     * {@inheritDoc}
213     */
214    @Override
215    public Collection<?> create(final int transform, final Collection<AnalysisInstrument> entities)
216    {
217        if (entities == null)
218        {
219            throw new IllegalArgumentException(
220                "AnalysisInstrument.create - 'entities' can not be null");
221        }
222                    for (AnalysisInstrument entity : entities)
223                    {
224                        create(transform, entity);
225                    }
226        return entities;
227    }
228
229    /**
230     * {@inheritDoc}
231     */
232    @Override
233    public AnalysisInstrument create(
234        String name,
235        String description,
236        Timestamp updateDate,
237        String comments)
238    {
239        return (AnalysisInstrument)this.create(AnalysisInstrumentDao.TRANSFORM_NONE, name, description, updateDate, comments);
240    }
241
242    /**
243     * {@inheritDoc}
244     */
245    @Override
246    public Object create(
247        final int transform,
248        String name,
249        String description,
250        Timestamp updateDate,
251        String comments)
252    {
253        AnalysisInstrument entity = new AnalysisInstrumentImpl();
254        entity.setName(name);
255        entity.setDescription(description);
256        entity.setUpdateDate(updateDate);
257        entity.setComments(comments);
258        return this.create(transform, entity);
259    }
260
261    /**
262     * {@inheritDoc}
263     */
264    @Override
265    public AnalysisInstrument create(
266        String name,
267        Status status,
268        Timestamp updateDate)
269    {
270        return (AnalysisInstrument)this.create(AnalysisInstrumentDao.TRANSFORM_NONE, name, status, updateDate);
271    }
272
273    /**
274     * {@inheritDoc}
275     */
276    @Override
277    public Object create(
278        final int transform,
279        String name,
280        Status status,
281        Timestamp updateDate)
282    {
283        AnalysisInstrument entity = new AnalysisInstrumentImpl();
284        entity.setName(name);
285        entity.setStatus(status);
286        entity.setUpdateDate(updateDate);
287        return this.create(transform, entity);
288    }
289
290    /**
291     * {@inheritDoc}
292     */
293    @Override
294    public void update(AnalysisInstrument analysisInstrument)
295    {
296        if (analysisInstrument == null)
297        {
298            throw new IllegalArgumentException(
299                "AnalysisInstrument.update - 'analysisInstrument' can not be null");
300        }
301        this.getSessionFactory().getCurrentSession().update(analysisInstrument);
302    }
303
304    /**
305     * {@inheritDoc}
306     */
307    @Override
308    public void update(final Collection<AnalysisInstrument> entities)
309    {
310        if (entities == null)
311        {
312            throw new IllegalArgumentException(
313                "AnalysisInstrument.update - 'entities' can not be null");
314        }
315                    for (AnalysisInstrument entity : entities)
316                    {
317                        update(entity);
318                    }
319    }
320
321    /**
322     * {@inheritDoc}
323     */
324    @Override
325    public void remove(AnalysisInstrument analysisInstrument)
326    {
327        if (analysisInstrument == null)
328        {
329            throw new IllegalArgumentException(
330                "AnalysisInstrument.remove - 'analysisInstrument' can not be null");
331        }
332        this.getSessionFactory().getCurrentSession().delete(analysisInstrument);
333    }
334
335    /**
336     * {@inheritDoc}
337     */
338    @Override
339    public void remove(Integer id)
340    {
341        if (id == null)
342        {
343            throw new IllegalArgumentException(
344                "AnalysisInstrument.remove - 'id' can not be null");
345        }
346        AnalysisInstrument entity = this.get(id);
347        if (entity != null)
348        {
349            this.remove(entity);
350        }
351    }
352
353    /**
354     * {@inheritDoc}
355     */
356    @Override
357    public void remove(Collection<AnalysisInstrument> entities)
358    {
359        if (entities == null)
360        {
361            throw new IllegalArgumentException(
362                "AnalysisInstrument.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>AnalysisInstrumentDao</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 AnalysisInstrumentDao}
376     * @param entity an entity that was found
377     * @return the transformed entity (i.e. new value object, etc)
378     * @see AnalysisInstrumentDao#transformEntity(int, AnalysisInstrument)
379     */
380    public Object transformEntity(final int transform, final AnalysisInstrument entity)
381    {
382        Object target = null;
383        if (entity != null)
384        {
385            switch (transform)
386            {
387                case AnalysisInstrumentDao.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 AnalysisInstrumentDao.TRANSFORM_NONE : // fall-through
404                default:
405                // do nothing;
406        }
407    }
408
409    /**
410     * @see AnalysisInstrumentDao#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 AnalysisInstrument}
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 AnalysisInstrument)
436                {
437                    result = input;
438                }
439                return result;
440            }
441        };
442
443    /**
444     * @param row
445     * @return AnalysisInstrument
446     */
447    protected AnalysisInstrument toEntity(Object[] row)
448    {
449        AnalysisInstrument 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 AnalysisInstrument)
457                {
458                    target = (AnalysisInstrument)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(), AnalysisInstrumentImpl.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(AnalysisInstrumentDao.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(), AnalysisInstrumentImpl.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<AnalysisInstrument> search(final Search search)
535    {
536        return (Set<AnalysisInstrument>) this.search(AnalysisInstrumentDao.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}