KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > model > core > Group


1 /*
2  * Copyright 2005 Blandware (http://www.blandware.com)
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 com.blandware.atleap.model.core;
17
18 import java.util.List JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.io.Serializable JavaDoc;
22
23 /**
24  * <p>
25  * This class represents a group of users. It's convinient to quickly assign
26  * a lot of roles to a user by placing that user to a group which has some
27  * roles assigned.
28  * </p>
29  * <p><a HREF="Group.java.htm"><i>View source</i></a></p>
30  *
31  * @author Roman Puchkovskiy <a HREF="mailto:roman.puchkovskiy@blandware.com">
32  * &lt;roman.puchkovskiy@blandware.com&gt;</a>
33  * @version $Revision: 1.3 $ $Date: 2006/03/16 11:09:37 $
34  * @see User
35  * @see Role
36  * @struts.form include-all="false" extends="BaseForm"
37  * @hibernate.class table="`al_core_group`" lazy="false"
38  * @hibernate.cache usage="read-write"
39  */

40 public class Group extends BaseObject implements Serializable JavaDoc {
41     /**
42      * Name of the group (used as a system identifier)
43      */

44     protected String JavaDoc name;
45     /**
46      * Human-readable name of the group
47      */

48     protected String JavaDoc title;
49     /**
50      * Description of the group; this may be quite long
51      */

52     protected String JavaDoc description;
53     /**
54      * Whether group is fixed. Fixed groups cannot be deleted
55      */

56     private Boolean JavaDoc fixed;
57     /**
58      * Version of this group (used in optimistic locking)
59      */

60     protected Long JavaDoc version;
61
62     /**
63      * List of roles which are assigned to this group
64      */

65     protected List JavaDoc roles = new ArrayList JavaDoc();
66
67     /**
68      * List of users which are members of this group
69      */

70     protected List JavaDoc users = new ArrayList JavaDoc();
71
72     /**
73      * Creates a group
74      */

75     public Group() {
76     }
77
78     /**
79      * Creates a group with a given name
80      *
81      * @param name group name
82      * @param title group title
83      */

84     public Group(String JavaDoc name, String JavaDoc title) {
85         this.name = name;
86         this.title = title;
87     }
88
89     //~ Methods ================================================================
90

91     /**
92      * Returns the name of the group (it's the analog of system identifier)
93      *
94      * @return group name
95      * @struts.form-field
96      * @struts.validator type="required"
97      * @struts.validator type="mask" msgkey="core.commons.errors.identifier"
98      * @struts.validator-args arg0resource="core.group.form.name"
99      * @struts.validator-var name="mask" value="${identifier}"
100      * @hibernate.id column="`groupname`" length="20"
101      * generator-class="assigned" unsaved-value="null"
102      */

103     public String JavaDoc getName() {
104         return this.name;
105     }
106
107     /**
108      * Returns human-readable title of the group.
109      *
110      * @return group title
111      * @struts.form-field
112      * @struts.validator type="required"
113      * @struts.validator-args arg0resource="core.group.form.title"
114      * @hibernate.property
115      * @hibernate.column name="`title`" not-null="true" unique="true" length="252"
116      */

117     public String JavaDoc getTitle() {
118         return this.title;
119     }
120
121     /**
122      * Returns the description of the group
123      *
124      * @return group description
125      * @struts.form-field
126      * @hibernate.property column="`description`" length="252"
127      */

128     public String JavaDoc getDescription() {
129         return this.description;
130     }
131
132     /**
133      * Returns <code>Boolean.TRUE</code>, if group is fixed, i.e. it cannot be deleted
134      *
135      * @return whether group is fixed
136      * @hibernate.property column="`fixed`" not-null="true" type="true_false"
137      */

138     public Boolean JavaDoc getFixed() {
139         return fixed;
140     }
141
142     /**
143      * Sets whether the group will be fixed (and no one will be able to delete it)
144      *
145      * @param fixed whether the group will be fixed
146      */

147     public void setFixed(Boolean JavaDoc fixed) {
148         this.fixed = fixed;
149     }
150
151     /**
152      * Sets name of the group
153      *
154      * @param name group name
155      */

156     public void setName(String JavaDoc name) {
157         this.name = name;
158     }
159
160     /**
161      * Sets human-readable title of the group
162      *
163      * @param title group title
164      */

165     public void setTitle(String JavaDoc title) {
166         this.title = title;
167     }
168
169     /**
170      * Sets description of the group
171      *
172      * @param description group description
173      */

174     public void setDescription(String JavaDoc description) {
175         this.description = description;
176     }
177
178     /**
179      * Gets all group roles, represented as string (roles are separated with commas)
180      *
181      * @return roles separated with commas
182      */

183     public String JavaDoc getRolesAsString() {
184         StringBuffer JavaDoc roles = new StringBuffer JavaDoc();
185         if ( this.roles != null && this.roles.size() > 0 ) {
186             for ( Iterator JavaDoc i = this.roles.iterator(); i.hasNext(); ) {
187                 Role role = (Role) i.next();
188                 roles.append(role.getTitle());
189                 if ( i.hasNext() ) {
190                     roles.append(", ");
191                 }
192             }
193         }
194         return roles.toString();
195     }
196
197     /**
198      * Returns version of the group
199      *
200      * @return version
201      * @struts.form-field
202      * @hibernate.version column="`version`" type="long" unsaved-value="null"
203      */

204     public Long JavaDoc getVersion() {
205         return version;
206     }
207
208     /**
209      * Sets version of the group
210      *
211      * @param version the version to set
212      */

213     public void setVersion(Long JavaDoc version) {
214         this.version = version;
215     }
216
217
218     /**
219      * Returns list of roles which are assigned to this group
220      *
221      * @return list of roles
222      * @hibernate.bag table="`al_core_group_role`" cascade="none" lazy="true" inverse="false" outer-join="false"
223      * @hibernate.collection-key
224      * @hibernate.collection-key-column name="`groupname`" length="20"
225      * @hibernate.collection-many-to-many class="com.blandware.atleap.model.core.Role"
226      * column="`rolename`" outer-join="false"
227      */

228     public List JavaDoc getRoles() {
229         return roles;
230     }
231
232     /**
233      * Sets list of roles which assigned to this group
234      *
235      * @param roles list of roles
236      */

237     public void setRoles(List JavaDoc roles) {
238         this.roles = roles;
239     }
240
241     /**
242      * Returns list of users which are members of this group
243      *
244      * @return list of users
245      * @hibernate.bag table="`al_core_group_user`" cascade="none" lazy="true" inverse="false" outer-join="false"
246      * @hibernate.collection-key
247      * @hibernate.collection-key-column name="`groupname`" length="20"
248      * @hibernate.collection-many-to-many class="com.blandware.atleap.model.core.User"
249      * column="`username`" outer-join="false"
250      */

251     public List JavaDoc getUsers() {
252         return users;
253     }
254
255     /**
256      * Sets list of users which are members of this group
257      *
258      * @param users list of users
259      */

260     public void setUsers(List JavaDoc users) {
261         this.users = users;
262     }
263
264     /**
265      * Adds a connection between this group and a given role
266      *
267      * @param role the role to be connected with
268      */

269     public void addRole(Role role) {
270         if ( !role.getGroups().contains(this) ) {
271             role.getGroups().add(this);
272         }
273         if ( !getRoles().contains(role) ) {
274             getRoles().add(role);
275         }
276     }
277
278     /**
279      * Removes a connection between this group and a given role
280      *
281      * @param role the role
282      */

283     public void removeRole(Role role) {
284         role.getGroups().remove(this);
285         getRoles().remove(role);
286     }
287
288     /**
289      * Adds a connection between this group and a given user
290      *
291      * @param user the user to be connected with
292      */

293     public void addUser(User user) {
294         if ( !user.getGroups().contains(this) ) {
295             user.getGroups().add(this);
296         }
297         if ( !getUsers().contains(user) ) {
298             getUsers().add(user);
299         }
300     }
301
302     /**
303      * Removes a connection between this group and a given user
304      *
305      * @param user the user
306      */

307     public void removeUser(User user) {
308         user.getGroups().remove(this);
309         getUsers().remove(user);
310     }
311
312     public boolean equals(Object JavaDoc o) {
313         if ( this == o ) {
314             return true;
315         }
316
317         if ( !(o instanceof Group) ) {
318             return false;
319         }
320         final Group group = (Group) o;
321
322         if ( title != null ? !title.equals(group.title) : group.title != null ) {
323             return false;
324         }
325
326         return true;
327     }
328
329     public int hashCode() {
330         if ( title == null ) {
331             return 0;
332         } else {
333             return title.hashCode();
334         }
335     }
336
337 }
338
Popular Tags