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