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 public class HibernateUserSet extends AbstractUserSet {
38 private static final String QUERY_FIND_BY_NAME = "findUserByName";
40 private static final String QUERY_FIND_BY_GROUP_NAME = "findUserByGroupName";
42 private static final String PARAM_NAME = "name";
44 private static final String QUERY_COUNT_USERS = "countUsers";
47 * Cache of users. Every user in the cache has its password validator and encoder set.
49 private Cache<String, User> _cache;
52 * Spring hibernate support.
54 private HibernateSupport _hibernateSupport;
57 * Constructs a user set backed by the database.
58 * @param aCache User cache to use.
60 public HibernateUserSet(Cache<String,User> aCache,
61 NameValidator aPasswordValidator, MessageDigester aPasswordEncoder) {
62 super(aPasswordValidator, aPasswordEncoder);
64 _hibernateSupport = new HibernateSupport();
68 * Sets the session factory.
69 * @param aFactory Session factory.
71 public void setSessionFactory(SessionFactory aFactory) {
72 _hibernateSupport.setSessionFactory(aFactory);
76 * Gets the hibernate template.
77 * @return Hibernate template.
79 private HibernateTemplate getHibernateTemplate() {
80 return _hibernateSupport.getHibernateTemplate();
86 * @see org.wamblee.usermgt.UserSet#userModified(org.wamblee.usermgt.User)
88 public void userModified(User aUser) {
89 assert aUser.getPrimaryKey() != null;
90 _hibernateSupport.merge(aUser);
91 _cache.remove(aUser.getName());
92 setPasswordInfo(aUser);
93 _cache.put(aUser.getName(), new User(aUser));
99 * @see org.wamblee.usermgt.UserSet#find(java.lang.String)
101 public User find(String aName) {
102 User user = _cache.get(aName);
106 List result = getHibernateTemplate().findByNamedQueryAndNamedParam(
107 QUERY_FIND_BY_NAME, PARAM_NAME, aName);
108 if (result.size() > 1) {
109 throw new RuntimeException(
110 "Implementation problem, more than one user with the same name!");
112 if (result.size() == 0) {
115 user = (User) result.get(0);
116 setPasswordInfo(user);
117 _cache.put(aName, user);
118 return new User(user);
124 * @see org.wamblee.usermgt.UserSet#contains(org.wamblee.usermgt.User)
126 public boolean contains(User aUser) {
127 return find(aUser.getName()) != null;
133 * @see org.wamblee.usermgt.UserSet#add(org.wamblee.usermgt.User)
135 public boolean add(User aUser) {
136 assert aUser.getPrimaryKey() == null;
137 if (contains(aUser)) {
140 getHibernateTemplate().saveOrUpdate(aUser);
141 setPasswordInfo(aUser);
142 _cache.put(aUser.getName(), aUser);
149 * @see org.wamblee.usermgt.UserSet#remove(org.wamblee.usermgt.User)
151 public boolean remove(User aUser) {
152 assert aUser.getPrimaryKey() != null;
153 if (!contains(aUser)) {
156 User user = (User) getHibernateTemplate().merge(aUser);
157 getHibernateTemplate().delete(user);
158 aUser.setPersistedVersion(-1);
159 aUser.setPrimaryKey(null);
160 _cache.remove(aUser.getName());
167 * @see org.wamblee.usermgt.UserSet#list()
169 public Set<User> list() {
170 Set<User> users = new TreeSet<User>();
171 List<User> list = getHibernateTemplate().loadAll(User.class);
172 for (User user : list) {
173 setPasswordInfo(user);
174 users.add(new User(user));
182 * @see org.wamblee.usermgt.UserSet#list(org.wamblee.usermgt.Group)
184 public Set<User> list(Group aGroup) {
185 Set<User> users = new TreeSet<User>();
186 List<User> list = getHibernateTemplate().findByNamedQueryAndNamedParam(
187 QUERY_FIND_BY_GROUP_NAME, PARAM_NAME, aGroup.getName());
188 for (User user : list) {
189 setPasswordInfo(user);
190 users.add(new User(user));
196 * @see org.wamblee.usermgt.UserSet#size()
199 Long result = (Long)getHibernateTemplate().findByNamedQuery(QUERY_COUNT_USERS).get(0);
200 return result.intValue();