--- /dev/null
+/*
+ * Copyright 2005-2010 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.usermgt.jpa;
+
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+
+import javax.persistence.EntityManager;
+import javax.persistence.NoResultException;
+import javax.persistence.TypedQuery;
+
+import org.wamblee.cache.Cache;
+import org.wamblee.persistence.JpaMergeSupport;
+import org.wamblee.security.encryption.MessageDigester;
+import org.wamblee.usermgt.AbstractUserSet;
+import org.wamblee.usermgt.Group;
+import org.wamblee.usermgt.NameValidator;
+import org.wamblee.usermgt.User;
+
+/**
+ * User set backed by the database.
+ *
+ * @author Erik Brakkee
+ */
+public class JpaUserSet extends AbstractUserSet {
+
+ /**
+ * Cache of users. Every user in the cache has its password validator and
+ * encoder set.
+ */
+ private Cache<String, User> cache;
+
+ private EntityManager entityManager;
+
+ /**
+ * Constructs a user set backed by the database.
+ *
+ * @param aCache
+ * User cache to use.
+ */
+ public JpaUserSet(Cache<String, User> aCache,
+ NameValidator aPasswordValidator, MessageDigester aPasswordEncoder,
+ EntityManager aEntityManager) {
+ super(aPasswordValidator, aPasswordEncoder);
+ cache = aCache;
+ entityManager = aEntityManager;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.usermgt.UserSet#userModified(org.wamblee.usermgt.User)
+ */
+ public void userModified(User aUser) {
+ assert aUser.getPrimaryKey() != null;
+ User merged = entityManager.merge(aUser);
+ // Need to flush the entity manager to make sure the version is updated.
+ entityManager.flush();
+ JpaMergeSupport.merge(merged, aUser);
+ cache.remove(aUser.getName());
+ setPasswordInfo(aUser);
+ cache.put(aUser.getName(), new User(aUser));
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.usermgt.UserSet#find(java.lang.String)
+ */
+ public User find(String aName) {
+ User user = cache.get(aName);
+
+ if (user != null) {
+ return user;
+ }
+
+ TypedQuery<User> query = entityManager.createNamedQuery(
+ User.QUERY_FIND_BY_NAME, User.class);
+ query.setParameter(User.NAME_PARAM, aName);
+ try {
+ user = query.getSingleResult();
+ user = new User(user);
+ setPasswordInfo(user);
+ cache.put(aName, user);
+ return user;
+ } catch (NoResultException e) {
+ return null;
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.usermgt.UserSet#contains(org.wamblee.usermgt.User)
+ */
+ public boolean contains(User aUser) {
+ return find(aUser.getName()) != null;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.usermgt.UserSet#add(org.wamblee.usermgt.User)
+ */
+ public boolean add(User aUser) {
+ assert aUser.getPrimaryKey() == null;
+
+ if (contains(aUser)) {
+ return false;
+ }
+
+ entityManager.persist(aUser);
+ setPasswordInfo(aUser);
+ cache.put(aUser.getName(), aUser);
+
+ return true;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.usermgt.UserSet#remove(org.wamblee.usermgt.User)
+ */
+ public boolean remove(User aUser) {
+ if (!contains(aUser)) {
+ return false;
+ }
+
+ User user = entityManager.merge(aUser);
+ entityManager.remove(user);
+ cache.remove(aUser.getName());
+
+ return true;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.usermgt.UserSet#list()
+ */
+ public Set<User> list() {
+ Set<User> users = new TreeSet<User>();
+ List<User> list = entityManager.createNamedQuery(User.QUERY_ALL_USERS,
+ User.class).getResultList();
+
+ for (User user : list) {
+ setPasswordInfo(user);
+ users.add(new User(user));
+ }
+
+ return users;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.usermgt.UserSet#list(org.wamblee.usermgt.Group)
+ */
+ public Set<User> list(Group aGroup) {
+ Set<User> users = new TreeSet<User>();
+ TypedQuery<User> query = entityManager.createNamedQuery(
+ User.QUERY_FIND_BY_GROUP_NAME, User.class);
+ query.setParameter(User.NAME_PARAM, aGroup.getName());
+
+ List<User> list = query.getResultList();
+
+ for (User user : list) {
+ setPasswordInfo(user);
+ users.add(new User(user));
+ }
+
+ return users;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.wamblee.usermgt.UserSet#size()
+ */
+ public int size() {
+ Long result = entityManager.createNamedQuery(User.QUERY_COUNT_USERS, Long.class).getSingleResult();
+ return result.intValue();
+ }
+}