df13cac154f8006ff0d22508506c1e0d8f7fba8a
[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
17 package org.wamblee.usermgt.hibernate;
18
19 import java.util.List;
20 import java.util.Set;
21 import java.util.TreeSet;
22
23 import org.hibernate.SessionFactory;
24 import org.springframework.orm.hibernate3.HibernateTemplate;
25 import org.wamblee.cache.Cache;
26 import org.wamblee.persistence.hibernate.HibernateSupport;
27 import org.wamblee.security.encryption.MessageDigester;
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 /**
34  * User set backed by the database.
35  *
36  * @author Erik Brakkee
37  */
38 public class HibernateUserSet extends AbstractUserSet {
39
40     private static final String QUERY_FIND_BY_NAME = "findUserByName";
41
42     private static final String QUERY_FIND_BY_GROUP_NAME = "findUserByGroupName";
43
44     private static final String PARAM_NAME = "name";
45     
46     private static final String QUERY_COUNT_USERS = "countUsers"; 
47
48     /**
49      * Cache of users. Every user in the cache has its password validator and encoder set.  
50      */
51     private Cache<String, User> cache;
52     
53     /** 
54      * Spring hibernate support. 
55      */
56     private HibernateSupport hibernateSupport; 
57
58     /**
59      * Constructs a user set backed by the database.
60      * @param aCache User cache to use. 
61      */
62     public HibernateUserSet(Cache<String,User> aCache, 
63             NameValidator aPasswordValidator, MessageDigester aPasswordEncoder) {
64         super(aPasswordValidator, aPasswordEncoder);
65         cache = aCache;
66         hibernateSupport = new HibernateSupport();
67     }
68     
69     /**
70      * Sets the session factory. 
71      * @param aFactory Session factory. 
72      */
73     public void setSessionFactory(SessionFactory aFactory) { 
74         hibernateSupport.setSessionFactory(aFactory);
75     }
76     
77     /**
78      * Gets the hibernate template. 
79      * @return Hibernate template. 
80      */
81     private HibernateTemplate getHibernateTemplate() { 
82         return hibernateSupport.getHibernateTemplate();
83     }
84    
85     /*
86      * (non-Javadoc)
87      * 
88      * @see org.wamblee.usermgt.UserSet#userModified(org.wamblee.usermgt.User)
89      */
90     public void userModified(User aUser) {
91         assert aUser.getPrimaryKey() != null;
92         hibernateSupport.merge(aUser);
93         cache.remove(aUser.getName());
94         setPasswordInfo(aUser);
95         cache.put(aUser.getName(), new User(aUser));
96     }
97
98     /*
99      * (non-Javadoc)
100      * 
101      * @see org.wamblee.usermgt.UserSet#find(java.lang.String)
102      */
103     public User find(String aName) {
104         User user = cache.get(aName);
105         if (user != null) {
106             return user;
107         }
108         List result = getHibernateTemplate().findByNamedQueryAndNamedParam(
109                 QUERY_FIND_BY_NAME, PARAM_NAME, aName);
110         if (result.size() > 1) {
111             throw new RuntimeException(
112                     "Implementation problem, more than one user with the same name!");
113         }
114         if (result.size() == 0) {
115             return null;
116         }
117         user = (User) result.get(0);
118         setPasswordInfo(user);
119         cache.put(aName, user);
120         return new User(user);
121     }
122
123     /*
124      * (non-Javadoc)
125      * 
126      * @see org.wamblee.usermgt.UserSet#contains(org.wamblee.usermgt.User)
127      */
128     public boolean contains(User aUser) {
129         return find(aUser.getName()) != null;
130     }
131
132     /*
133      * (non-Javadoc)
134      * 
135      * @see org.wamblee.usermgt.UserSet#add(org.wamblee.usermgt.User)
136      */
137     public boolean add(User aUser) {
138         assert aUser.getPrimaryKey() == null;
139         if (contains(aUser)) {
140             return false;
141         }
142         getHibernateTemplate().saveOrUpdate(aUser);
143         setPasswordInfo(aUser);
144         cache.put(aUser.getName(), aUser);
145         return true;
146     }
147
148     /*
149      * (non-Javadoc)
150      * 
151      * @see org.wamblee.usermgt.UserSet#remove(org.wamblee.usermgt.User)
152      */
153     public boolean remove(User aUser) {
154         assert aUser.getPrimaryKey() != null;
155         if (!contains(aUser)) {
156             return false;
157         }
158         User user = (User) getHibernateTemplate().merge(aUser);
159         getHibernateTemplate().delete(user);
160         aUser.setPersistedVersion(-1);
161         aUser.setPrimaryKey(null);
162         cache.remove(aUser.getName());
163         return true;
164     }
165
166     /*
167      * (non-Javadoc)
168      * 
169      * @see org.wamblee.usermgt.UserSet#list()
170      */
171     public Set<User> list() {
172         Set<User> users = new TreeSet<User>();
173         List<User> list = getHibernateTemplate().loadAll(User.class);
174         for (User user : list) {
175             setPasswordInfo(user);
176             users.add(new User(user));
177         }
178         return users;
179     }
180
181     /*
182      * (non-Javadoc)
183      * 
184      * @see org.wamblee.usermgt.UserSet#list(org.wamblee.usermgt.Group)
185      */
186     public Set<User> list(Group aGroup) {
187         Set<User> users = new TreeSet<User>();
188         List<User> list = getHibernateTemplate().findByNamedQueryAndNamedParam(
189                 QUERY_FIND_BY_GROUP_NAME, PARAM_NAME, aGroup.getName());
190         for (User user : list) {
191             setPasswordInfo(user);
192             users.add(new User(user));
193         }
194         return users;
195     }
196
197     /* (non-Javadoc)
198      * @see org.wamblee.usermgt.UserSet#size()
199      */
200     public int size() {
201         Long result = (Long)getHibernateTemplate().findByNamedQuery(QUERY_COUNT_USERS).get(0);
202         return result.intValue(); 
203     }
204 }