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