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