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