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