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