+++ /dev/null
-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();
- }
-}