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