Removed DOCUMENT ME comments that were generated and applied source code
[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 package org.wamblee.usermgt.hibernate;
17
18 import org.wamblee.persistence.hibernate.HibernateSupport;
19
20 import org.wamblee.usermgt.Group;
21 import org.wamblee.usermgt.GroupSet;
22
23 import java.util.List;
24 import java.util.Set;
25 import java.util.TreeSet;
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     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     /**
40      * Creates a new HibernateGroupSet object.
41      */
42     public HibernateGroupSet() {
43         // Empty
44     }
45
46     /*
47      * (non-Javadoc)
48      * 
49      * @see
50      * org.wamblee.usermgt.GroupSet#groupModified(org.wamblee.usermgt.Group)
51      */
52     public void groupModified(Group aGroup) {
53         assert aGroup.getPrimaryKey() != null;
54         super.merge(aGroup);
55     }
56
57     /*
58      * (non-Javadoc)
59      * 
60      * @see org.wamblee.usermgt.GroupSet#find(java.lang.String)
61      */
62     public Group find(String aName) {
63         List list = getHibernateTemplate().findByNamedQueryAndNamedParam(
64             QUERY_FIND_BY_NAME, PARAM_NAME, aName);
65
66         if (list.size() > 1) {
67             throw new RuntimeException(
68                 "More than one group with the same name '" + aName + "'");
69         }
70
71         if (list.size() == 0) {
72             return null;
73         }
74
75         return new Group((Group) list.get(0));
76     }
77
78     /*
79      * (non-Javadoc)
80      * 
81      * @see org.wamblee.usermgt.GroupSet#contains(org.wamblee.usermgt.Group)
82      */
83     public boolean contains(Group aGroup) {
84         return find(aGroup.getName()) != null;
85     }
86
87     /*
88      * (non-Javadoc)
89      * 
90      * @see org.wamblee.usermgt.GroupSet#add(org.wamblee.usermgt.Group)
91      */
92     public boolean add(Group aGroup) {
93         assert aGroup.getPrimaryKey() == null;
94
95         if (contains(aGroup)) {
96             return false;
97         }
98
99         super.merge(aGroup);
100
101         return true;
102     }
103
104     /*
105      * (non-Javadoc)
106      * 
107      * @see org.wamblee.usermgt.GroupSet#remove(org.wamblee.usermgt.Group)
108      */
109     public boolean remove(Group aGroup) {
110         assert aGroup.getPrimaryKey() != null;
111
112         if (!contains(aGroup)) {
113             return false;
114         }
115
116         Group group = (Group) getHibernateTemplate().merge(aGroup);
117         getHibernateTemplate().delete(group);
118         aGroup.setPrimaryKey(null);
119         aGroup.setPersistedVersion(-1);
120
121         return true;
122     }
123
124     /*
125      * (non-Javadoc)
126      * 
127      * @see org.wamblee.usermgt.GroupSet#list()
128      */
129     public Set<Group> list() {
130         Set<Group> groups = new TreeSet<Group>();
131         List<Group> list = getHibernateTemplate().loadAll(Group.class);
132
133         for (Group group : list) {
134             groups.add(new Group(group));
135         }
136
137         return groups;
138     }
139
140     /*
141      * (non-Javadoc)
142      * 
143      * @see org.wamblee.usermgt.GroupSet#size()
144      */
145     public int size() {
146         Long result = (Long) getHibernateTemplate().findByNamedQuery(
147             QUERY_COUNT_GROUPS).get(0);
148
149         return result.intValue();
150     }
151 }