2 * Copyright 2005-2010 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.jpa;
18 import java.util.List;
20 import java.util.TreeSet;
22 import javax.persistence.EntityManager;
23 import javax.persistence.NoResultException;
24 import javax.persistence.TypedQuery;
26 import org.wamblee.cache.Cache;
27 import org.wamblee.persistence.JpaMergeSupport;
28 import org.wamblee.security.encryption.MessageDigester;
29 import org.wamblee.usermgt.AbstractUserSet;
30 import org.wamblee.usermgt.Group;
31 import org.wamblee.usermgt.NameValidator;
32 import org.wamblee.usermgt.User;
35 * User set backed by the database.
37 * @author Erik Brakkee
39 public class JpaUserSet extends AbstractUserSet {
42 * Cache of users. Every user in the cache has its password validator and
45 private Cache<String, User> cache;
47 private EntityManager entityManager;
50 * Constructs a user set backed by the database.
55 public JpaUserSet(Cache<String, User> aCache,
56 NameValidator aPasswordValidator, MessageDigester aPasswordEncoder,
57 EntityManager aEntityManager) {
58 super(aPasswordValidator, aPasswordEncoder);
60 entityManager = aEntityManager;
66 * @see org.wamblee.usermgt.UserSet#userModified(org.wamblee.usermgt.User)
68 public void userModified(User aUser) {
69 assert aUser.getPrimaryKey() != null;
70 User merged = entityManager.merge(aUser);
71 // Need to flush the entity manager to make sure the version is updated.
72 entityManager.flush();
73 JpaMergeSupport.merge(merged, aUser);
74 cache.remove(aUser.getName());
75 setPasswordInfo(aUser);
76 cache.put(aUser.getName(), new User(aUser));
82 * @see org.wamblee.usermgt.UserSet#find(java.lang.String)
84 public User find(String aName) {
85 User user = cache.get(aName);
91 TypedQuery<User> query = entityManager.createNamedQuery(
92 User.QUERY_FIND_BY_NAME, User.class);
93 query.setParameter(User.NAME_PARAM, aName);
95 user = query.getSingleResult();
96 user = new User(user);
97 setPasswordInfo(user);
98 cache.put(aName, user);
100 } catch (NoResultException e) {
108 * @see org.wamblee.usermgt.UserSet#contains(org.wamblee.usermgt.User)
110 public boolean contains(User aUser) {
111 return find(aUser.getName()) != null;
117 * @see org.wamblee.usermgt.UserSet#add(org.wamblee.usermgt.User)
119 public boolean add(User aUser) {
120 assert aUser.getPrimaryKey() == null;
122 if (contains(aUser)) {
126 entityManager.persist(aUser);
127 setPasswordInfo(aUser);
128 cache.put(aUser.getName(), aUser);
136 * @see org.wamblee.usermgt.UserSet#remove(org.wamblee.usermgt.User)
138 public boolean remove(User aUser) {
139 if (!contains(aUser)) {
143 User user = entityManager.merge(aUser);
144 entityManager.remove(user);
145 cache.remove(aUser.getName());
153 * @see org.wamblee.usermgt.UserSet#list()
155 public Set<User> list() {
156 Set<User> users = new TreeSet<User>();
157 List<User> list = entityManager.createNamedQuery(User.QUERY_ALL_USERS,
158 User.class).getResultList();
160 for (User user : list) {
161 setPasswordInfo(user);
162 users.add(new User(user));
171 * @see org.wamblee.usermgt.UserSet#list(org.wamblee.usermgt.Group)
173 public Set<User> list(Group aGroup) {
174 Set<User> users = new TreeSet<User>();
175 TypedQuery<User> query = entityManager.createNamedQuery(
176 User.QUERY_FIND_BY_GROUP_NAME, User.class);
177 query.setParameter(User.NAME_PARAM, aGroup.getName());
179 List<User> list = query.getResultList();
181 for (User user : list) {
182 setPasswordInfo(user);
183 users.add(new User(user));
192 * @see org.wamblee.usermgt.UserSet#size()
195 Long result = entityManager.createNamedQuery(User.QUERY_COUNT_USERS, Long.class).getSingleResult();
196 return result.intValue();