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