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