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