c928b843eba8d4f02149e381a0b1a089523c60eb
[utils] / security / usermgt / src / main / java / org / wamblee / security / authentication / jpa / JpaGroupSet.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.security.authentication.jpa;
17
18 import java.util.List;
19 import java.util.Set;
20 import java.util.TreeSet;
21
22 import javax.persistence.EntityManager;
23 import javax.persistence.TypedQuery;
24
25 import org.wamblee.persistence.JpaMergeSupport;
26 import org.wamblee.security.authentication.Group;
27 import org.wamblee.security.authentication.GroupSet;
28
29 /**
30  * Group set backed by the database.
31  * 
32  * @author Erik Brakkee
33  */
34 public class JpaGroupSet implements GroupSet {
35
36     private EntityManager em;
37
38     /**
39      * Constructs the group set. 
40      * @param aEm Contextual reference to an entity manager. 
41      */
42     public JpaGroupSet(EntityManager aEm) {
43         em = aEm;
44     }
45
46     @Override
47     public boolean add(Group aGroup) {
48         assert aGroup.getPrimaryKey() == null;
49         if (contains(aGroup)) {
50             return false;
51         }
52         em.persist(aGroup);
53         em.flush(); // to make sure the version is updated. 
54         return true;
55     }
56
57     @Override
58     public boolean contains(Group aGroup) {
59         return find(aGroup.getName()) != null;
60     }
61
62     @Override
63     public Group find(String aName) {
64         TypedQuery<Group> query = em.createNamedQuery(Group.QUERY_FIND_BY_NAME,
65             Group.class);
66         query.setParameter(Group.NAME_PARAM, aName);
67         List<Group> groups = query.getResultList();
68         if (groups.size() > 1) {
69             throw new RuntimeException(
70                 "More than one group with the same name '" + aName + "'");
71         }
72
73         if (groups.size() == 0) {
74             return null;
75         }
76         return groups.get(0);
77     }
78
79     @Override
80     public void groupModified(Group aGroup) {
81         assert aGroup.getPrimaryKey() != null;
82         Group merged = em.merge(aGroup);
83         // Need to flush so that version of the merged instance is updated so we
84         // can use
85         // the updated version in the original group passed in. That allows the
86         // same
87         // group object to continue to be used as a detached object.
88         em.flush();
89         JpaMergeSupport.merge(merged, aGroup);
90     }
91
92     @Override
93     public Set<Group> list() {
94         List<Group> groups = em.createNamedQuery(Group.QUERY_ALL_GROUPS,
95             Group.class).getResultList();
96         Set<Group> res = new TreeSet<Group>(groups);
97         return res;
98     }
99
100     @Override
101     public boolean remove(Group aGroup) {
102         Group group = find(aGroup.getName());
103         if (group == null) {
104             return false;
105         }
106         em.remove(group);
107         return true;
108     }
109
110     @Override
111     public int size() {
112         Long res = (Long) em.createNamedQuery(Group.QUERY_COUNT_GROUPS)
113             .getSingleResult();
114         return res.intValue();
115     }
116 }