1 /*
2 * The SmartWeb Framework
3 * Copyright (C) 2004-2006
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * For further informations on the SmartWeb Framework please visit
20 *
21 * http://smartweb.sourceforge.net
22 */
23 package net.smartlab.web.auth;
24
25 import java.util.HashSet;
26 import java.util.Set;
27
28 import org.apache.commons.lang.builder.EqualsBuilder;
29 import org.apache.commons.lang.builder.HashCodeBuilder;
30 import org.apache.commons.lang.builder.ToStringBuilder;
31
32 /**
33 * This class aggregates users to simplify their management.
34 *
35 * @author rlogiacco
36 * @author svetrini
37 * @hibernate.joined-subclass schema="auth" table="`group`"
38 * @hibernate.joined-subclass-key column="`id`"
39 */
40 public class Group extends Subject {
41
42 private final static long serialVersionUID = -6251419333679466935L;
43
44 /**
45 * Describes the group.
46 *
47 * @uml.property name="description"
48 */
49 private String description;
50
51 /**
52 * Count of users belonging to this group.
53 *
54 * @uml.property name="size"
55 */
56 private int size;
57
58 /**
59 * The users belonging to this group.
60 *
61 * @link aggregation <{User}>
62 * @directed directed
63 * @supplierCardinality 0..*
64 * @uml.property name="users"
65 * @uml.associationEnd multiplicity="(0 -1)" aggregation="composite"
66 * inverse="groups:net.smartlab.web.auth.User"
67 */
68 private Set users = new HashSet();
69
70
71 /**
72 * @see net.smartlab.web.auth.Subject#getId()
73 * @hibernate.id column="`id`" generator-class="native"
74 */
75 public long getId() {
76 return super.getId();
77 }
78
79 /**
80 * Returns the description.
81 *
82 * @return the description
83 * @hibernate.property column="`description`" type="text"
84 * @uml.property name="description"
85 */
86 public String getDescription() {
87 return description;
88 }
89
90 /**
91 * Sets the description.
92 *
93 * @param description the description to set
94 * @uml.property name="description"
95 */
96 public void setDescription(String description) {
97 this.description = description;
98 }
99
100 /**
101 * Returns the users belonging to this group.
102 *
103 * @return the users belonging to this group.
104 * @hibernate.set lazy="false" schema="auth" table="`group_user`"
105 * cascade="none"
106 * @hibernate.collection-key column="`group`"
107 * @hibernate.collection-many-to-many column="`user`"
108 * class="net.smartlab.web.auth.User"
109 * @uml.property name="users"
110 */
111 public Set getUsers() {
112 return users;
113 }
114
115 /**
116 * Sets the users belonging to this group.
117 *
118 * @param users the users to set.
119 * @uml.property name="users"
120 */
121 public void setUsers(Set users) {
122 this.users = users;
123 }
124
125 /**
126 * Removes any user from this group.
127 */
128 public void clearUsers() {
129 users.clear();
130 size = 0;
131 }
132
133 /**
134 * Adds a user to this group.
135 *
136 * @param user the user to add.
137 */
138 public void add(User user) {
139 users.add(user);
140 }
141
142 /**
143 * Removes a user from this group.
144 *
145 * @param user the user to remove.
146 */
147 public void remove(User user) {
148 users.remove(user);
149 }
150
151 public boolean hasUser(User user) {
152 return users.contains(user);
153 }
154
155 /**
156 * Returns the number of users belonging to this group.
157 *
158 * @return the number of users belonging to this group.
159 * @FIXME hibernate.property insert="false" update="false" formula="( select
160 * count(*) from auth.`group_user` where
161 * `group_user`.`group`=id )"
162 * @uml.property name="size"
163 */
164 public int getSize() {
165 if (this.getUsers()!=null){
166 this.size = this.getUsers().size();
167 }
168 return this.size;
169
170 }
171
172 /**
173 * Sets the number of users belonging to this group.
174 *
175 * @param size the number of users belonging to this group.
176 * @uml.property name="size"
177 */
178 protected void setSize(int size) {
179 throw new UnsupportedOperationException();
180 //FIXME this.size = size;
181 }
182
183 /**
184 * @see java.lang.Object#toString()
185 */
186 public String toString() {
187 return new ToStringBuilder(this).append(super.toString()).append("size", size).toString();
188 }
189
190 /**
191 * @see java.lang.Object#hashCode()
192 */
193 /**
194 * @see java.lang.Object#hashCode()
195 */
196 public int hashCode() {
197 return new HashCodeBuilder(-27848275, 353473951).append(super.display).append(this.users).toHashCode();
198 }
199
200 /**
201 * @see java.lang.Object#equals(Object)
202 */
203 public boolean equals(Object object) {
204 if (!(object instanceof Group)) {
205 return false;
206 }
207 Group rhs = (Group)object;
208 return new EqualsBuilder().append(super.display, rhs.display).append(this.users, rhs.users).isEquals();
209 }
210
211 }