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.location;
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.Point;
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.referential.Status;
036import fr.ifremer.adagio.core.dao.referential.ValidityStatus;
037import fr.ifremer.adagio.core.dao.technical.hibernate.HibernateDaoSupport;
038import java.security.Principal;
039import java.sql.Timestamp;
040import java.util.Collection;
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>LocationPoint</code>.
057 * </p>
058 *
059 * @see LocationPoint
060 */
061public abstract class LocationPointDaoBase
062    extends HibernateDaoSupport    
063    implements LocationPointDao
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                "LocationPoint.get - 'id' can not be null");
075        }
076        final LocationPoint entity = get(LocationPointImpl.class, id);
077        return transformEntity(transform, entity);
078    }
079    /**
080     * {@inheritDoc}
081     */
082    @Override
083    public LocationPoint get(Integer id)
084    {
085        return (LocationPoint)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                "LocationPoint.load - 'id' can not be null");
098        }
099        final LocationPoint entity = get(LocationPointImpl.class, id);
100        return transformEntity(transform, entity);
101    }
102
103    /**
104     * {@inheritDoc}
105     */
106    @Override
107    public LocationPoint load(Integer id)
108    {
109        return (LocationPoint)this.load(TRANSFORM_NONE, id);
110    }
111
112    /**
113     * {@inheritDoc}
114     */
115    @Override
116    @SuppressWarnings({"unchecked"})
117    public Collection<LocationPoint> loadAll()
118    {
119        return (Collection<LocationPoint>) this.loadAll(LocationPointDao.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(LocationPointDao.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(LocationPointImpl.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 LocationPoint create(LocationPoint locationPoint)
185    {
186        return (LocationPoint)this.create(LocationPointDao.TRANSFORM_NONE, locationPoint);
187    }
188
189    /**
190     * {@inheritDoc}
191     */
192    @Override
193    public Object create(final int transform, final LocationPoint locationPoint)
194    {
195        if (locationPoint == null)
196        {
197            throw new IllegalArgumentException(
198                "LocationPoint.create - 'locationPoint' can not be null");
199        }
200        this.getSessionFactory().getCurrentSession().save(locationPoint);
201        return this.transformEntity(transform, locationPoint);
202    }
203
204    /**
205     * {@inheritDoc}
206     */
207    @Override
208    @SuppressWarnings({"unchecked"})
209    public Collection<LocationPoint> create(final Collection<LocationPoint> entities)
210    {
211        return (Collection<LocationPoint>) create(LocationPointDao.TRANSFORM_NONE, entities);
212    }
213
214    /**
215     * {@inheritDoc}
216     */
217    @Override
218    public Collection<?> create(final int transform, final Collection<LocationPoint> entities)
219    {
220        if (entities == null)
221        {
222            throw new IllegalArgumentException(
223                "LocationPoint.create - 'entities' can not be null");
224        }
225                    for (LocationPoint entity : entities)
226                    {
227                        create(transform, entity);
228                    }
229        return entities;
230    }
231
232    /**
233     * {@inheritDoc}
234     */
235    @Override
236    public LocationPoint create(
237        Point position,
238        Timestamp positionUpdateDate,
239        String label,
240        String name,
241        Float bathymetry,
242        Short utFormat,
243        Boolean daylightSavingTime,
244        String comments,
245        Timestamp updateDate)
246    {
247        return (LocationPoint)this.create(LocationPointDao.TRANSFORM_NONE, position, positionUpdateDate, label, name, bathymetry, utFormat, daylightSavingTime, comments, updateDate);
248    }
249
250    /**
251     * {@inheritDoc}
252     */
253    @Override
254    public Object create(
255        final int transform,
256        Point position,
257        Timestamp positionUpdateDate,
258        String label,
259        String name,
260        Float bathymetry,
261        Short utFormat,
262        Boolean daylightSavingTime,
263        String comments,
264        Timestamp updateDate)
265    {
266        LocationPoint entity = new LocationPointImpl();
267        entity.setPosition(position);
268        entity.setPositionUpdateDate(positionUpdateDate);
269        entity.setLabel(label);
270        entity.setName(name);
271        entity.setBathymetry(bathymetry);
272        entity.setUtFormat(utFormat);
273        entity.setDaylightSavingTime(daylightSavingTime);
274        entity.setComments(comments);
275        entity.setUpdateDate(updateDate);
276        return this.create(transform, entity);
277    }
278
279    /**
280     * {@inheritDoc}
281     */
282    @Override
283    public LocationPoint create(
284        LocationClassification locationClassification,
285        LocationLevel locationLevel,
286        String name,
287        Status status,
288        Timestamp updateDate,
289        ValidityStatus validityStatus,
290        Point position)
291    {
292        return (LocationPoint)this.create(LocationPointDao.TRANSFORM_NONE, locationClassification, locationLevel, name, status, updateDate, validityStatus, position);
293    }
294
295    /**
296     * {@inheritDoc}
297     */
298    @Override
299    public Object create(
300        final int transform,
301        LocationClassification locationClassification,
302        LocationLevel locationLevel,
303        String name,
304        Status status,
305        Timestamp updateDate,
306        ValidityStatus validityStatus,
307        Point position)
308    {
309        LocationPoint entity = new LocationPointImpl();
310        entity.setLocationClassification(locationClassification);
311        entity.setLocationLevel(locationLevel);
312        entity.setName(name);
313        entity.setStatus(status);
314        entity.setUpdateDate(updateDate);
315        entity.setValidityStatus(validityStatus);
316        entity.setPosition(position);
317        return this.create(transform, entity);
318    }
319
320    /**
321     * {@inheritDoc}
322     */
323    @Override
324    public void update(LocationPoint locationPoint)
325    {
326        if (locationPoint == null)
327        {
328            throw new IllegalArgumentException(
329                "LocationPoint.update - 'locationPoint' can not be null");
330        }
331        this.getSessionFactory().getCurrentSession().update(locationPoint);
332    }
333
334    /**
335     * {@inheritDoc}
336     */
337    @Override
338    public void update(final Collection<LocationPoint> entities)
339    {
340        if (entities == null)
341        {
342            throw new IllegalArgumentException(
343                "LocationPoint.update - 'entities' can not be null");
344        }
345                    for (LocationPoint entity : entities)
346                    {
347                        update(entity);
348                    }
349    }
350
351    /**
352     * {@inheritDoc}
353     */
354    @Override
355    public void remove(LocationPoint locationPoint)
356    {
357        if (locationPoint == null)
358        {
359            throw new IllegalArgumentException(
360                "LocationPoint.remove - 'locationPoint' can not be null");
361        }
362        this.getSessionFactory().getCurrentSession().delete(locationPoint);
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                "LocationPoint.remove - 'id' can not be null");
375        }
376        LocationPoint 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<LocationPoint> entities)
388    {
389        if (entities == null)
390        {
391            throw new IllegalArgumentException(
392                "LocationPoint.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>LocationPointDao</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 LocationPointDao}
406     * @param entity an entity that was found
407     * @return the transformed entity (i.e. new value object, etc)
408     * @see LocationPointDao#transformEntity(int, LocationPoint)
409     */
410    public Object transformEntity(final int transform, final LocationPoint entity)
411    {
412        Object target = null;
413        if (entity != null)
414        {
415            switch (transform)
416            {
417                case LocationPointDao.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 LocationPointDao.TRANSFORM_NONE : // fall-through
434                default:
435                // do nothing;
436        }
437    }
438
439    /**
440     * @see LocationPointDao#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 LocationPoint}
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 LocationPoint)
466                {
467                    result = input;
468                }
469                return result;
470            }
471        };
472
473    /**
474     * @param row
475     * @return LocationPoint
476     */
477    protected LocationPoint toEntity(Object[] row)
478    {
479        LocationPoint 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 LocationPoint)
487                {
488                    target = (LocationPoint)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(), LocationPointImpl.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(LocationPointDao.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(), LocationPointImpl.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<LocationPoint> search(final Search search)
565    {
566        return (Set<LocationPoint>) this.search(LocationPointDao.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}