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.
17 package org.wamblee.usermgt.hibernate;
19 import java.util.List;
21 import java.util.TreeSet;
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;
34 * User set backed by the database.
36 * @author Erik Brakkee
38 public class HibernateUserSet extends AbstractUserSet {
40 private static final String QUERY_FIND_BY_NAME = "findUserByName";
42 private static final String QUERY_FIND_BY_GROUP_NAME = "findUserByGroupName";
44 private static final String PARAM_NAME = "name";
46 private static final String QUERY_COUNT_USERS = "countUsers";
49 * Cache of users. Every user in the cache has its password validator and encoder set.
51 private Cache<String, User> _cache;
54 * Spring hibernate support.
56 private HibernateSupport _hibernateSupport;
59 * Constructs a user set backed by the database.
60 * @param aCache User cache to use.
62 public HibernateUserSet(Cache<String,User> aCache,
63 NameValidator aPasswordValidator, MessageDigester aPasswordEncoder) {
64 super(aPasswordValidator, aPasswordEncoder);
66 _hibernateSupport = new HibernateSupport();
70 * Sets the session factory.
71 * @param aFactory Session factory.
73 public void setSessionFactory(SessionFactory aFactory) {
74 _hibernateSupport.setSessionFactory(aFactory);
78 * Gets the hibernate template.
79 * @return Hibernate template.
81 private HibernateTemplate getHibernateTemplate() {
82 return _hibernateSupport.getHibernateTemplate();
88 * @see org.wamblee.usermgt.UserSet#userModified(org.wamblee.usermgt.User)
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));
101 * @see org.wamblee.usermgt.UserSet#find(java.lang.String)
103 public User find(String aName) {
104 User user = _cache.get(aName);
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!");
114 if (result.size() == 0) {
117 user = (User) result.get(0);
118 setPasswordInfo(user);
119 _cache.put(aName, user);
120 return new User(user);
126 * @see org.wamblee.usermgt.UserSet#contains(org.wamblee.usermgt.User)
128 public boolean contains(User aUser) {
129 return find(aUser.getName()) != null;
135 * @see org.wamblee.usermgt.UserSet#add(org.wamblee.usermgt.User)
137 public boolean add(User aUser) {
138 assert aUser.getPrimaryKey() == null;
139 if (contains(aUser)) {
142 getHibernateTemplate().saveOrUpdate(aUser);
143 setPasswordInfo(aUser);
144 _cache.put(aUser.getName(), aUser);
151 * @see org.wamblee.usermgt.UserSet#remove(org.wamblee.usermgt.User)
153 public boolean remove(User aUser) {
154 assert aUser.getPrimaryKey() != null;
155 if (!contains(aUser)) {
158 User user = (User) getHibernateTemplate().merge(aUser);
159 getHibernateTemplate().delete(user);
160 aUser.setPersistedVersion(-1);
161 aUser.setPrimaryKey(null);
162 _cache.remove(aUser.getName());
169 * @see org.wamblee.usermgt.UserSet#list()
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));
184 * @see org.wamblee.usermgt.UserSet#list(org.wamblee.usermgt.Group)
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));
198 * @see org.wamblee.usermgt.UserSet#size()
201 Long result = (Long)getHibernateTemplate().findByNamedQuery(QUERY_COUNT_USERS).get(0);
202 return result.intValue();