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