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