2 * Copyright 2005 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.usermgt.hibernate;
18 import org.hibernate.SessionFactory;
20 import org.springframework.orm.hibernate3.HibernateTemplate;
22 import org.wamblee.cache.Cache;
24 import org.wamblee.persistence.hibernate.HibernateSupport;
26 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;
33 import java.util.List;
35 import java.util.TreeSet;
38 * User set backed by the database.
40 * @author Erik Brakkee
42 public class HibernateUserSet extends AbstractUserSet {
43 private static final String QUERY_FIND_BY_NAME = "findUserByName";
45 private static final String QUERY_FIND_BY_GROUP_NAME = "findUserByGroupName";
47 private static final String PARAM_NAME = "name";
49 private static final String QUERY_COUNT_USERS = "countUsers";
52 * Cache of users. Every user in the cache has its password validator and
55 private Cache<String, User> cache;
58 * Spring hibernate support.
60 private HibernateSupport hibernateSupport;
63 * Constructs a user set backed by the database.
68 public HibernateUserSet(Cache<String, User> aCache,
69 NameValidator aPasswordValidator, MessageDigester aPasswordEncoder) {
70 super(aPasswordValidator, aPasswordEncoder);
72 hibernateSupport = new HibernateSupport();
76 * Sets the session factory.
81 public void setSessionFactory(SessionFactory aFactory) {
82 hibernateSupport.setSessionFactory(aFactory);
86 * Gets the hibernate template.
88 * @return Hibernate template.
90 private HibernateTemplate getHibernateTemplate() {
91 return hibernateSupport.getHibernateTemplate();
97 * @see org.wamblee.usermgt.UserSet#userModified(org.wamblee.usermgt.User)
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));
110 * @see org.wamblee.usermgt.UserSet#find(java.lang.String)
112 public User find(String aName) {
113 User user = cache.get(aName);
119 List result = getHibernateTemplate().findByNamedQueryAndNamedParam(
120 QUERY_FIND_BY_NAME, PARAM_NAME, aName);
122 if (result.size() > 1) {
123 throw new RuntimeException(
124 "Implementation problem, more than one user with the same name!");
127 if (result.size() == 0) {
131 user = (User) result.get(0);
132 setPasswordInfo(user);
133 cache.put(aName, user);
135 return new User(user);
141 * @see org.wamblee.usermgt.UserSet#contains(org.wamblee.usermgt.User)
143 public boolean contains(User aUser) {
144 return find(aUser.getName()) != null;
150 * @see org.wamblee.usermgt.UserSet#add(org.wamblee.usermgt.User)
152 public boolean add(User aUser) {
153 assert aUser.getPrimaryKey() == null;
155 if (contains(aUser)) {
159 getHibernateTemplate().saveOrUpdate(aUser);
160 setPasswordInfo(aUser);
161 cache.put(aUser.getName(), aUser);
169 * @see org.wamblee.usermgt.UserSet#remove(org.wamblee.usermgt.User)
171 public boolean remove(User aUser) {
172 assert aUser.getPrimaryKey() != null;
174 if (!contains(aUser)) {
178 User user = (User) getHibernateTemplate().merge(aUser);
179 getHibernateTemplate().delete(user);
180 aUser.setPersistedVersion(-1);
181 aUser.setPrimaryKey(null);
182 cache.remove(aUser.getName());
190 * @see org.wamblee.usermgt.UserSet#list()
192 public Set<User> list() {
193 Set<User> users = new TreeSet<User>();
194 List<User> list = getHibernateTemplate().loadAll(User.class);
196 for (User user : list) {
197 setPasswordInfo(user);
198 users.add(new User(user));
207 * @see org.wamblee.usermgt.UserSet#list(org.wamblee.usermgt.Group)
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());
214 for (User user : list) {
215 setPasswordInfo(user);
216 users.add(new User(user));
225 * @see org.wamblee.usermgt.UserSet#size()
228 Long result = (Long) getHibernateTemplate().findByNamedQuery(
229 QUERY_COUNT_USERS).get(0);
231 return result.intValue();