From: Erik Brakkee Date: Mon, 26 Apr 2010 20:17:34 +0000 (+0000) Subject: (no commit message) X-Git-Tag: wamblee-utils-0.7~511 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=72e6ad63f7e52f143c886aa33607d0efa7bfb4e6;p=utils --- diff --git a/security/impl/src/test/java/org/wamblee/usermgt/jpa/JpaGroupSet.java b/security/impl/src/test/java/org/wamblee/usermgt/jpa/JpaGroupSet.java new file mode 100644 index 00000000..1a9be6f2 --- /dev/null +++ b/security/impl/src/test/java/org/wamblee/usermgt/jpa/JpaGroupSet.java @@ -0,0 +1,80 @@ +package org.wamblee.usermgt.jpa; + +import java.util.List; +import java.util.Set; +import java.util.TreeSet; + +import javax.persistence.EntityManager; +import javax.persistence.TypedQuery; + +import org.wamblee.usermgt.Group; +import org.wamblee.usermgt.GroupSet; + +public class JpaGroupSet implements GroupSet { + + private EntityManager em; + + public JpaGroupSet(EntityManager aEm) { + em = aEm; + } + + @Override + public boolean add(Group aGroup) { + assert aGroup.getPrimaryKey() == null; + if (contains(aGroup)) { + return false; + } + em.persist(aGroup); + return true; + } + + @Override + public boolean contains(Group aGroup) { + return find(aGroup.getName()) != null; + } + + @Override + public Group find(String aName) { + TypedQuery query = em.createNamedQuery(Group.QUERY_FIND_BY_NAME, Group.class); + query.setParameter(Group.NAME_PARAM, aName); + List groups = query.getResultList(); + if (groups.size() > 1) { + throw new RuntimeException( + "More than one group with the same name '" + aName + "'"); + } + + if (groups.size() == 0) { + return null; + } + return groups.get(0); + } + + @Override + public void groupModified(Group aGroup) { + assert aGroup.getPrimaryKey() != null; + em.merge(aGroup); + } + + @Override + public Set list() { + List groups = em.createNamedQuery(Group.QUERY_ALL_GROUPS, Group.class).getResultList(); + Set res = new TreeSet(groups); + return res; + } + + @Override + public boolean remove(Group aGroup) { + Group group = find(aGroup.getName()); + if ( group == null ) { + return false; + } + em.remove(group); + return true; + } + + @Override + public int size() { + Long res = (Long)em.createNamedQuery(Group.QUERY_COUNT_GROUPS).getSingleResult(); + return res.intValue(); + } +}