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