From: Erik Brakkee Date: Fri, 30 Apr 2010 22:08:23 +0000 (+0000) Subject: (no commit message) X-Git-Tag: wamblee-utils-0.7~489 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=d04e4a2462f3b67ff568a5646d70208438d67a7a;p=utils --- diff --git a/security/impl/src/main/java/org/wamblee/usermgt/jpa/JpaUserSet.java b/security/impl/src/main/java/org/wamblee/usermgt/jpa/JpaUserSet.java new file mode 100644 index 00000000..045eda00 --- /dev/null +++ b/security/impl/src/main/java/org/wamblee/usermgt/jpa/JpaUserSet.java @@ -0,0 +1,198 @@ +/* + * 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 cache; + + private EntityManager entityManager; + + /** + * Constructs a user set backed by the database. + * + * @param aCache + * User cache to use. + */ + public JpaUserSet(Cache 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 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 list() { + Set users = new TreeSet(); + List 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 list(Group aGroup) { + Set users = new TreeSet(); + TypedQuery query = entityManager.createNamedQuery( + User.QUERY_FIND_BY_GROUP_NAME, User.class); + query.setParameter(User.NAME_PARAM, aGroup.getName()); + + List 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(); + } +}