View Javadoc

1   /*
2   * Copyright 2005 Arnaud Prost
3   * 
4   * Arnaud.prost@gmail.com
5   * 
6   * This software is a computer program whose purpose is to ease the 
7   * management of software project.
8   * 
9   * This software is governed by the CeCILL  license under French law and
10  * abiding by the rules of distribution of free software.  You can  use, 
11  * modify and/ or redistribute the software under the terms of the CeCILL
12  * license as circulated by CEA, CNRS and INRIA at the following URL
13  * "http://www.cecill.info". 
14  * 
15  * As a counterpart to the access to the source code and  rights to copy,
16  * modify and redistribute granted by the license, users are provided only
17  * with a limited warranty  and the software's author,  the holder of the
18  * economic rights,  and the successive licensors  have only  limited
19  * liability. 
20  * 
21  * In this respect, the user's attention is drawn to the risks associated
22  * with loading,  using,  modifying and/or developing or reproducing the
23  * software by the user in light of its specific status of free software,
24  * that may mean  that it is complicated to manipulate,  and  that  also
25  * therefore means  that it is reserved for developers  and  experienced
26  * professionals having in-depth computer knowledge. Users are therefore
27  * encouraged to load and test the software's suitability as regards their
28  * requirements in conditions enabling the security of their systems and/or 
29  * data to be ensured and,  more generally, to use and operate it in the 
30  * same conditions as regards security. 
31  * 
32  * The fact that you are presently reading this means that you have had
33  * knowledge of the CeCILL license and that you accept its terms.
34  */
35  
36  package net.sf.pmr.core.data.user;
37  
38  import java.util.ArrayList;
39  import java.util.Collection;
40  import java.util.HashSet;
41  import java.util.List;
42  import java.util.Set;
43  
44  import net.sf.pmr.core.domain.basicProject.BasicProject;
45  import net.sf.pmr.core.domain.user.User;
46  import net.sf.pmr.core.domain.user.UserImpl;
47  
48  import org.springframework.dao.support.DataAccessUtils;
49  
50  /***
51   * @author Arnaud
52   */
53  public class UserMapperImpl extends
54          org.springframework.orm.hibernate3.support.HibernateDaoSupport
55          implements UserMapper {
56  
57      /***
58       * find a user by his Id
59       * 
60       * @param id
61       *            id
62       * @return Entity User find
63       */
64      public final Object findById(final int id) {
65  
66          return (User) getHibernateTemplate().get(UserImpl.class,
67                  new Integer(id));
68  
69      }
70  
71      /***
72       * find a user by his login
73       * 
74       * @param login
75       *            login
76       * @return Entity User find
77       */
78      public final User findByLogin(final String login) {
79  
80          List list = getHibernateTemplate()
81                  .find(
82                          "from net.sf.pmr.core.domain.user.UserImpl user where user.Login = ?",
83                          login);
84          return (User) DataAccessUtils.uniqueResult(list);
85  
86      }
87  
88      /***
89       * find all users
90       * 
91       * @return List of user
92       */
93      public final List findAll() {
94  
95          return getHibernateTemplate().loadAll(User.class);
96  
97      }
98  
99      /***
100      * Count All User
101      * 
102      * @return int number of user
103      */
104     public final Integer countAll() {
105 
106         List list = getHibernateTemplate().find(
107                 "select count(*) from net.sf.pmr.core.domain.user.UserImpl");
108         return (Integer) DataAccessUtils.uniqueResult(list);
109 
110     }
111 
112     /***
113      * add or update a user
114      * 
115      * @param entity
116      *            user to update
117      */
118     public final void addOrUpdate(final Object entity) {
119 
120         getHibernateTemplate().saveOrUpdate((User) entity);
121 
122     }
123 
124     /***
125      * Delete a user
126      * 
127      * @param entity
128      *            user to delete
129      */
130     public final void delete(final Object entity) {
131 
132         getHibernateTemplate().delete((User) entity);
133 
134     }
135 
136     /*** 
137      * @see net.sf.pmr.keopsframework.data.DomainListMapper#findCollectionForObject(java.lang.Object)
138      */
139     public final Collection findCollectionForObject(final Object object) {
140 
141         Set collectionToReturn = new HashSet();
142 
143         if (object instanceof BasicProject) {
144 
145             BasicProject basicProject = (BasicProject) object;
146 
147             List list = getHibernateTemplate()
148                     .find(
149                             "from net.sf.pmr.core.domain.user.UserImpl user where ? in elements(user.Projects)",
150                             basicProject);
151             
152             collectionToReturn.addAll(list);
153 
154         }
155 
156         return collectionToReturn;
157 
158     }
159 }