957ee44da8931135cb4eb3fa3d80f2112865a4ea
[utils] / security / src / main / java / org / wamblee / usermgt / hibernate / HibernateGroupSet.java
1 /*
2  * Copyright 2005 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
17 package org.wamblee.usermgt.hibernate;
18
19 import java.util.List;
20 import java.util.Set;
21 import java.util.TreeSet;
22
23 import org.wamblee.persistence.hibernate.HibernateSupport;
24 import org.wamblee.usermgt.Group;
25 import org.wamblee.usermgt.GroupSet;
26
27 /**
28  * Set of groups backed by the database. 
29  *
30  * @author Erik Brakkee
31  */
32 public class HibernateGroupSet extends HibernateSupport implements GroupSet {
33     
34     
35     private static final String QUERY_FIND_BY_NAME = "findGroupByName";
36     
37     private static final String PARAM_NAME = "name";
38     
39     private static final String QUERY_COUNT_GROUPS = "countGroups";
40     
41     public HibernateGroupSet() {
42         // Empty
43     }
44     
45     /* (non-Javadoc)
46      * @see org.wamblee.usermgt.GroupSet#groupModified(org.wamblee.usermgt.Group)
47      */
48     public void groupModified(Group aGroup) {
49         assert aGroup.getPrimaryKey() != null; 
50         super.merge(aGroup);        
51     }
52
53     /* (non-Javadoc)
54      * @see org.wamblee.usermgt.GroupSet#find(java.lang.String)
55      */
56     public Group find(String aName) {
57         List list = getHibernateTemplate().findByNamedQueryAndNamedParam(QUERY_FIND_BY_NAME, PARAM_NAME, aName);
58         if ( list.size() > 1 ) { 
59             throw new RuntimeException("More than one group with the same name '" + aName + "'");
60         }
61         if ( list.size() == 0 ) { 
62             return null; 
63         }
64         return new Group((Group)list.get(0));
65     }
66
67     /* (non-Javadoc)
68      * @see org.wamblee.usermgt.GroupSet#contains(org.wamblee.usermgt.Group)
69      */
70     public boolean contains(Group aGroup) {
71         return find(aGroup.getName()) != null; 
72     }
73
74     /* (non-Javadoc)
75      * @see org.wamblee.usermgt.GroupSet#add(org.wamblee.usermgt.Group)
76      */
77     public boolean add(Group aGroup) {
78         assert aGroup.getPrimaryKey() == null; 
79         if ( contains(aGroup) ) { 
80             return false; 
81         }
82         super.merge(aGroup);
83         return true; 
84     }
85
86     /* (non-Javadoc)
87      * @see org.wamblee.usermgt.GroupSet#remove(org.wamblee.usermgt.Group)
88      */
89     public boolean remove(Group aGroup) {
90         assert aGroup.getPrimaryKey() != null; 
91         if ( !contains(aGroup)) { 
92             return false; 
93         }
94         Group group = (Group) getHibernateTemplate().merge(aGroup); 
95         getHibernateTemplate().delete(group);
96         aGroup.setPrimaryKey(null); 
97         aGroup.setPersistedVersion(-1);
98         return true; 
99     }
100
101     /* (non-Javadoc)
102      * @see org.wamblee.usermgt.GroupSet#list()
103      */
104     public Set<Group> list() {
105         Set<Group> groups = new TreeSet<Group>(); 
106         List<Group> list = getHibernateTemplate().loadAll(Group.class);
107         for (Group group: list) { 
108             groups.add(new Group(group));
109         }
110         return groups; 
111     }
112
113     /* (non-Javadoc)
114      * @see org.wamblee.usermgt.GroupSet#size()
115      */
116     public int size() {
117         Long result = (Long) getHibernateTemplate().findByNamedQuery(QUERY_COUNT_GROUPS).get(0);
118         return result.intValue(); 
119     }
120 }