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