Fixed regression bug with uninitialized fields when rendering a public folder as...
[pithos] / src / gr / ebs / gss / server / domain / Group.java
1 /*
2  * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 package gr.ebs.gss.server.domain;
20
21 import gr.ebs.gss.server.domain.dto.GroupDTO;
22
23 import java.io.Serializable;
24 import java.util.Set;
25
26 import javax.persistence.CascadeType;
27 import javax.persistence.Embedded;
28 import javax.persistence.Entity;
29 import javax.persistence.FetchType;
30 import javax.persistence.GeneratedValue;
31 import javax.persistence.Id;
32 import javax.persistence.JoinColumn;
33 import javax.persistence.JoinTable;
34 import javax.persistence.ManyToMany;
35 import javax.persistence.ManyToOne;
36 import javax.persistence.OneToMany;
37 import javax.persistence.Table;
38 import javax.persistence.Version;
39
40 import org.hibernate.annotations.Cache;
41 import org.hibernate.annotations.CacheConcurrencyStrategy;
42
43 /**
44  * A group of users of the GSS service.
45  *
46  * @author past
47  */
48 @Entity
49 @Table(name = "GSS_Group")
50 @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
51 public class Group  implements Serializable{
52
53         /**
54          * The persistence ID of the object.
55          */
56         @Id
57         @GeneratedValue
58         private Long id;
59
60         /**
61          * Version field for optimistic locking.
62          */
63         @SuppressWarnings("unused")
64         @Version
65         private int version;
66
67         /**
68          * The audit information.
69          */
70         @Embedded
71         private AuditInfo auditInfo;
72
73         /**
74          * The name of the group.
75          */
76         private String name;
77
78         /**
79          * The user that owns this group.
80          */
81         @ManyToOne(optional = false)
82         @JoinColumn(nullable = false)
83         private User owner;
84
85         /**
86          * The set of users that belong to this group.
87          */
88         @ManyToMany(fetch = FetchType.LAZY)
89         @JoinTable(joinColumns = {@JoinColumn(nullable = false)}, inverseJoinColumns = {@JoinColumn(nullable = false)})
90         private Set<User> members;
91
92         @OneToMany(mappedBy="group", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
93         private Set<Permission> permissions;
94
95         /**
96          * A default constructor.
97          */
98         public Group() {
99                 // Empty.
100         }
101
102         /**
103          * Retrieve the permissions.
104          *
105          * @return the permissions
106          */
107         public Set<Permission> getPermissions() {
108                 return permissions;
109         }
110
111         /**
112          * Modify the permissions.
113          *
114          * @param newPermissions the permissions to set
115          */
116         public void setPermissions(Set<Permission> newPermissions) {
117                 permissions = newPermissions;
118         }
119
120         /**
121          * A constructor that creates a group with the specified name.
122          *
123          * @param aName
124          */
125         public Group(final String aName) {
126                 name = aName;
127         }
128
129         @Override
130         public String toString() {
131                 return name;
132         }
133
134         /**
135          * Retrieve the id.
136          *
137          * @return the id
138          */
139         public Long getId() {
140                 return id;
141         }
142
143         /**
144          * Retrieve the name.
145          *
146          * @return the name
147          */
148         public String getName() {
149                 return name;
150         }
151
152         /**
153          * Modify the name.
154          *
155          * @param newName the name to set
156          */
157         public void setName(final String newName) {
158                 name = newName;
159         }
160
161         /**
162          * Retrieve the owner.
163          *
164          * @return the owner
165          */
166         public User getOwner() {
167                 return owner;
168         }
169
170         /**
171          * Modify the owner.
172          *
173          * @param newOwner the owner to set
174          */
175         public void setOwner(final User newOwner) {
176                 owner = newOwner;
177         }
178
179         /**
180          * Retrieve the members.
181          *
182          * @return the members
183          */
184         public Set<User> getMembers() {
185                 return members;
186         }
187
188         /**
189          * Replace the member set.
190          *
191          * @param newMembers the new members
192          */
193         public void setMembers(final Set<User> newMembers) {
194                 members = newMembers;
195         }
196
197         /**
198          * Retrieve the audit info.
199          *
200          * @return the audit info
201          */
202         public AuditInfo getAuditInfo() {
203                 return auditInfo;
204         }
205
206         /**
207          * Modify the audit info.
208          *
209          * @param newAuditInfo the new audit info
210          */
211         public void setAuditInfo(final AuditInfo newAuditInfo) {
212                 auditInfo = newAuditInfo;
213         }
214
215         /**
216          * Returns a Data Transfer Object for this Group
217          *
218          * @return GroupDTO
219          */
220         public GroupDTO getDTO() {
221                 final GroupDTO g = new GroupDTO();
222                 g.setId(id);
223                 g.setName(name);
224                 g.setOwner(owner.getDTO());
225                 for (final User u : members)
226                         g.getMembers().add(u.getDTO());
227                 return g;
228         }
229
230         /**
231          * Checks if this groups contains the specified user
232          *
233          * @param user
234          * @return boolean
235          */
236         public boolean contains(final User user) {
237                 return members.contains(user);
238         }
239
240         public void removeMemberFromGroup(final User member){
241                 getMembers().remove(member);
242                 member.getGroupsMember().remove(this);
243         }
244 }