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