5c076d72083a33d8651e3c7ae0379211b83fbdc3
[utils] / security / impl / 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     public JpaGroupSet(EntityManager aEm) {
39         em = aEm;
40     }
41
42     @Override
43     public boolean add(Group aGroup) {
44         assert aGroup.getPrimaryKey() == null;
45         if (contains(aGroup)) {
46             return false;
47         }
48         em.persist(aGroup);
49         em.flush(); // to make sure the version is updated. 
50         return true;
51     }
52
53     @Override
54     public boolean contains(Group aGroup) {
55         return find(aGroup.getName()) != null;
56     }
57
58     @Override
59     public Group find(String aName) {
60         TypedQuery<Group> query = em.createNamedQuery(Group.QUERY_FIND_BY_NAME,
61             Group.class);
62         query.setParameter(Group.NAME_PARAM, aName);
63         List<Group> groups = query.getResultList();
64         if (groups.size() > 1) {
65             throw new RuntimeException(
66                 "More than one group with the same name '" + aName + "'");
67         }
68
69         if (groups.size() == 0) {
70             return null;
71         }
72         return groups.get(0);
73     }
74
75     @Override
76     public void groupModified(Group aGroup) {
77         assert aGroup.getPrimaryKey() != null;
78         Group merged = em.merge(aGroup);
79         // Need to flush so that version of the merged instance is updated so we
80         // can use
81         // the updated version in the original group passed in. That allows the
82         // same
83         // group object to continue to be used as a detached object.
84         em.flush();
85         JpaMergeSupport.merge(merged, aGroup);
86     }
87
88     @Override
89     public Set<Group> list() {
90         List<Group> groups = em.createNamedQuery(Group.QUERY_ALL_GROUPS,
91             Group.class).getResultList();
92         Set<Group> res = new TreeSet<Group>(groups);
93         return res;
94     }
95
96     @Override
97     public boolean remove(Group aGroup) {
98         Group group = find(aGroup.getName());
99         if (group == null) {
100             return false;
101         }
102         em.remove(group);
103         return true;
104     }
105
106     @Override
107     public int size() {
108         Long res = (Long) em.createNamedQuery(Group.QUERY_COUNT_GROUPS)
109             .getSingleResult();
110         return res.intValue();
111     }
112 }