From b55c3f58098670d17012905823adf9b9db627609 Mon Sep 17 00:00:00 2001 From: erik Date: Mon, 26 Apr 2010 20:17:34 +0000 Subject: [PATCH] --- .../org/wamblee/usermgt/jpa/JpaGroupSet.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 security/impl/src/test/java/org/wamblee/usermgt/jpa/JpaGroupSet.java 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(); + } +} -- 2.31.1