Removed DOCUMENT ME comments that were generated and applied source code
[utils] / security / src / main / java / org / wamblee / usermgt / hibernate / HibernateUserSet.java
1 /*
2  * Copyright 2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.usermgt.hibernate;
17
18 import org.hibernate.SessionFactory;
19
20 import org.springframework.orm.hibernate3.HibernateTemplate;
21
22 import org.wamblee.cache.Cache;
23
24 import org.wamblee.persistence.hibernate.HibernateSupport;
25
26 import org.wamblee.security.encryption.MessageDigester;
27
28 import org.wamblee.usermgt.AbstractUserSet;
29 import org.wamblee.usermgt.Group;
30 import org.wamblee.usermgt.NameValidator;
31 import org.wamblee.usermgt.User;
32
33 import java.util.List;
34 import java.util.Set;
35 import java.util.TreeSet;
36
37 /**
38  * User set backed by the database.
39  * 
40  * @author Erik Brakkee
41  */
42 public class HibernateUserSet extends AbstractUserSet {
43     private static final String QUERY_FIND_BY_NAME = "findUserByName";
44
45     private static final String QUERY_FIND_BY_GROUP_NAME = "findUserByGroupName";
46
47     private static final String PARAM_NAME = "name";
48
49     private static final String QUERY_COUNT_USERS = "countUsers";
50
51     /**
52      * Cache of users. Every user in the cache has its password validator and
53      * encoder set.
54      */
55     private Cache<String, User> cache;
56
57     /**
58      * Spring hibernate support.
59      */
60     private HibernateSupport hibernateSupport;
61
62     /**
63      * Constructs a user set backed by the database.
64      * 
65      * @param aCache
66      *            User cache to use.
67      */
68     public HibernateUserSet(Cache<String, User> aCache,
69         NameValidator aPasswordValidator, MessageDigester aPasswordEncoder) {
70         super(aPasswordValidator, aPasswordEncoder);
71         cache = aCache;
72         hibernateSupport = new HibernateSupport();
73     }
74
75     /**
76      * Sets the session factory.
77      * 
78      * @param aFactory
79      *            Session factory.
80      */
81     public void setSessionFactory(SessionFactory aFactory) {
82         hibernateSupport.setSessionFactory(aFactory);
83     }
84
85     /**
86      * Gets the hibernate template.
87      * 
88      * @return Hibernate template.
89      */
90     private HibernateTemplate getHibernateTemplate() {
91         return hibernateSupport.getHibernateTemplate();
92     }
93
94     /*
95      * (non-Javadoc)
96      * 
97      * @see org.wamblee.usermgt.UserSet#userModified(org.wamblee.usermgt.User)
98      */
99     public void userModified(User aUser) {
100         assert aUser.getPrimaryKey() != null;
101         hibernateSupport.merge(aUser);
102         cache.remove(aUser.getName());
103         setPasswordInfo(aUser);
104         cache.put(aUser.getName(), new User(aUser));
105     }
106
107     /*
108      * (non-Javadoc)
109      * 
110      * @see org.wamblee.usermgt.UserSet#find(java.lang.String)
111      */
112     public User find(String aName) {
113         User user = cache.get(aName);
114
115         if (user != null) {
116             return user;
117         }
118
119         List result = getHibernateTemplate().findByNamedQueryAndNamedParam(
120             QUERY_FIND_BY_NAME, PARAM_NAME, aName);
121
122         if (result.size() > 1) {
123             throw new RuntimeException(
124                 "Implementation problem, more than one user with the same name!");
125         }
126
127         if (result.size() == 0) {
128             return null;
129         }
130
131         user = (User) result.get(0);
132         setPasswordInfo(user);
133         cache.put(aName, user);
134
135         return new User(user);
136     }
137
138     /*
139      * (non-Javadoc)
140      * 
141      * @see org.wamblee.usermgt.UserSet#contains(org.wamblee.usermgt.User)
142      */
143     public boolean contains(User aUser) {
144         return find(aUser.getName()) != null;
145     }
146
147     /*
148      * (non-Javadoc)
149      * 
150      * @see org.wamblee.usermgt.UserSet#add(org.wamblee.usermgt.User)
151      */
152     public boolean add(User aUser) {
153         assert aUser.getPrimaryKey() == null;
154
155         if (contains(aUser)) {
156             return false;
157         }
158
159         getHibernateTemplate().saveOrUpdate(aUser);
160         setPasswordInfo(aUser);
161         cache.put(aUser.getName(), aUser);
162
163         return true;
164     }
165
166     /*
167      * (non-Javadoc)
168      * 
169      * @see org.wamblee.usermgt.UserSet#remove(org.wamblee.usermgt.User)
170      */
171     public boolean remove(User aUser) {
172         assert aUser.getPrimaryKey() != null;
173
174         if (!contains(aUser)) {
175             return false;
176         }
177
178         User user = (User) getHibernateTemplate().merge(aUser);
179         getHibernateTemplate().delete(user);
180         aUser.setPersistedVersion(-1);
181         aUser.setPrimaryKey(null);
182         cache.remove(aUser.getName());
183
184         return true;
185     }
186
187     /*
188      * (non-Javadoc)
189      * 
190      * @see org.wamblee.usermgt.UserSet#list()
191      */
192     public Set<User> list() {
193         Set<User> users = new TreeSet<User>();
194         List<User> list = getHibernateTemplate().loadAll(User.class);
195
196         for (User user : list) {
197             setPasswordInfo(user);
198             users.add(new User(user));
199         }
200
201         return users;
202     }
203
204     /*
205      * (non-Javadoc)
206      * 
207      * @see org.wamblee.usermgt.UserSet#list(org.wamblee.usermgt.Group)
208      */
209     public Set<User> list(Group aGroup) {
210         Set<User> users = new TreeSet<User>();
211         List<User> list = getHibernateTemplate().findByNamedQueryAndNamedParam(
212             QUERY_FIND_BY_GROUP_NAME, PARAM_NAME, aGroup.getName());
213
214         for (User user : list) {
215             setPasswordInfo(user);
216             users.add(new User(user));
217         }
218
219         return users;
220     }
221
222     /*
223      * (non-Javadoc)
224      * 
225      * @see org.wamblee.usermgt.UserSet#size()
226      */
227     public int size() {
228         Long result = (Long) getHibernateTemplate().findByNamedQuery(
229             QUERY_COUNT_USERS).get(0);
230
231         return result.intValue();
232     }
233 }