X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=security%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fusermgt%2Fhibernate%2FHibernateUserSet.java;h=910ea431479ffa5b98e4954126a491b2f1b701b5;hb=f7fc25fbfd96a245bb09e14242107ed633d86dd8;hp=df13cac154f8006ff0d22508506c1e0d8f7fba8a;hpb=81fe8784a2182e25f92a7591ec5b0fba00afb828;p=utils diff --git a/security/src/main/java/org/wamblee/usermgt/hibernate/HibernateUserSet.java b/security/src/main/java/org/wamblee/usermgt/hibernate/HibernateUserSet.java index df13cac1..910ea431 100644 --- a/security/src/main/java/org/wamblee/usermgt/hibernate/HibernateUserSet.java +++ b/security/src/main/java/org/wamblee/usermgt/hibernate/HibernateUserSet.java @@ -1,5 +1,5 @@ /* - * Copyright 2005 the original author or authors. + * 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. @@ -12,76 +12,85 @@ * 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.hibernate; -import java.util.List; -import java.util.Set; -import java.util.TreeSet; - import org.hibernate.SessionFactory; + import org.springframework.orm.hibernate3.HibernateTemplate; + import org.wamblee.cache.Cache; + import org.wamblee.persistence.hibernate.HibernateSupport; + 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; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; + /** * User set backed by the database. - * + * * @author Erik Brakkee */ public class HibernateUserSet extends AbstractUserSet { - private static final String QUERY_FIND_BY_NAME = "findUserByName"; private static final String QUERY_FIND_BY_GROUP_NAME = "findUserByGroupName"; private static final String PARAM_NAME = "name"; - - private static final String QUERY_COUNT_USERS = "countUsers"; + + private static final String QUERY_COUNT_USERS = "countUsers"; /** - * Cache of users. Every user in the cache has its password validator and encoder set. + * Cache of users. Every user in the cache has its password validator and + * encoder set. */ private Cache cache; - - /** - * Spring hibernate support. + + /** + * Spring hibernate support. */ - private HibernateSupport hibernateSupport; + private HibernateSupport hibernateSupport; /** * Constructs a user set backed by the database. - * @param aCache User cache to use. + * + * @param aCache + * User cache to use. */ - public HibernateUserSet(Cache aCache, - NameValidator aPasswordValidator, MessageDigester aPasswordEncoder) { + public HibernateUserSet(Cache aCache, + NameValidator aPasswordValidator, MessageDigester aPasswordEncoder) { super(aPasswordValidator, aPasswordEncoder); cache = aCache; hibernateSupport = new HibernateSupport(); } - + /** - * Sets the session factory. - * @param aFactory Session factory. + * Sets the session factory. + * + * @param aFactory + * Session factory. */ - public void setSessionFactory(SessionFactory aFactory) { + public void setSessionFactory(SessionFactory aFactory) { hibernateSupport.setSessionFactory(aFactory); } - + /** - * Gets the hibernate template. - * @return Hibernate template. + * Gets the hibernate template. + * + * @return Hibernate template. */ - private HibernateTemplate getHibernateTemplate() { + private HibernateTemplate getHibernateTemplate() { return hibernateSupport.getHibernateTemplate(); } - + /* * (non-Javadoc) * @@ -102,21 +111,27 @@ public class HibernateUserSet extends AbstractUserSet { */ public User find(String aName) { User user = cache.get(aName); + if (user != null) { return user; } + List result = getHibernateTemplate().findByNamedQueryAndNamedParam( - QUERY_FIND_BY_NAME, PARAM_NAME, aName); + QUERY_FIND_BY_NAME, PARAM_NAME, aName); + if (result.size() > 1) { throw new RuntimeException( - "Implementation problem, more than one user with the same name!"); + "Implementation problem, more than one user with the same name!"); } + if (result.size() == 0) { return null; } + user = (User) result.get(0); setPasswordInfo(user); cache.put(aName, user); + return new User(user); } @@ -136,12 +151,15 @@ public class HibernateUserSet extends AbstractUserSet { */ public boolean add(User aUser) { assert aUser.getPrimaryKey() == null; + if (contains(aUser)) { return false; } + getHibernateTemplate().saveOrUpdate(aUser); setPasswordInfo(aUser); cache.put(aUser.getName(), aUser); + return true; } @@ -152,14 +170,17 @@ public class HibernateUserSet extends AbstractUserSet { */ public boolean remove(User aUser) { assert aUser.getPrimaryKey() != null; + if (!contains(aUser)) { return false; } + User user = (User) getHibernateTemplate().merge(aUser); getHibernateTemplate().delete(user); aUser.setPersistedVersion(-1); aUser.setPrimaryKey(null); cache.remove(aUser.getName()); + return true; } @@ -171,10 +192,12 @@ public class HibernateUserSet extends AbstractUserSet { public Set list() { Set users = new TreeSet(); List list = getHibernateTemplate().loadAll(User.class); + for (User user : list) { setPasswordInfo(user); users.add(new User(user)); } + return users; } @@ -186,19 +209,25 @@ public class HibernateUserSet extends AbstractUserSet { public Set list(Group aGroup) { Set users = new TreeSet(); List list = getHibernateTemplate().findByNamedQueryAndNamedParam( - QUERY_FIND_BY_GROUP_NAME, PARAM_NAME, aGroup.getName()); + QUERY_FIND_BY_GROUP_NAME, PARAM_NAME, aGroup.getName()); + for (User user : list) { setPasswordInfo(user); users.add(new User(user)); } + return users; } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see org.wamblee.usermgt.UserSet#size() */ public int size() { - Long result = (Long)getHibernateTemplate().findByNamedQuery(QUERY_COUNT_USERS).get(0); - return result.intValue(); + Long result = (Long) getHibernateTemplate().findByNamedQuery( + QUERY_COUNT_USERS).get(0); + + return result.intValue(); } }