(no commit message)
authorErik Brakkee <erik@brakkee.org>
Mon, 26 Apr 2010 20:17:34 +0000 (20:17 +0000)
committerErik Brakkee <erik@brakkee.org>
Mon, 26 Apr 2010 20:17:34 +0000 (20:17 +0000)
security/impl/src/test/java/org/wamblee/usermgt/jpa/JpaGroupSet.java [new file with mode: 0644]

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 (file)
index 0000000..1a9be6f
--- /dev/null
@@ -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<Group> query = em.createNamedQuery(Group.QUERY_FIND_BY_NAME, Group.class);
+        query.setParameter(Group.NAME_PARAM, aName);
+        List<Group> 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<Group> list() {
+        List<Group> groups = em.createNamedQuery(Group.QUERY_ALL_GROUPS, Group.class).getResultList();
+        Set<Group> res = new TreeSet<Group>(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();
+    }
+}