Use an exponential backoff strategy for retrying rolled back transactions.
[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 /**
41  * A group of users of the GSS service.
42  *
43  * @author past
44  */
45 @Entity
46 @Table(name = "GSS_Group")
47 public class Group  implements Serializable{
48
49         /**
50          * The persistence ID of the object.
51          */
52         @Id
53         @GeneratedValue
54         private Long id;
55
56         /**
57          * Version field for optimistic locking.
58          */
59         @SuppressWarnings("unused")
60         @Version
61         private int version;
62
63         /**
64          * The audit information.
65          */
66         @Embedded
67         private AuditInfo auditInfo;
68
69         /**
70          * The name of the group.
71          */
72         private String name;
73
74         /**
75          * The user that owns this group.
76          */
77         @ManyToOne(optional = false)
78         @JoinColumn(nullable = false)
79         private User owner;
80
81         /**
82          * The set of users that belong to this group.
83          */
84         @ManyToMany(fetch = FetchType.LAZY)
85         @JoinTable(joinColumns = {@JoinColumn(nullable = false)}, inverseJoinColumns = {@JoinColumn(nullable = false)})
86         private Set<User> members;
87
88         @OneToMany(mappedBy="group", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
89         private Set<Permission> permissions;
90
91         /**
92          * A default constructor.
93          */
94         public Group() {
95                 // Empty.
96         }
97
98
99         /**
100          * Retrieve the permissions.
101          *
102          * @return the permissions
103          */
104         public Set<Permission> getPermissions() {
105                 return permissions;
106         }
107
108
109         /**
110          * Modify the permissions.
111          *
112          * @param permissions the permissions to set
113          */
114         public void setPermissions(Set<Permission> permissions) {
115                 this.permissions = permissions;
116         }
117
118         /**
119          * A constructor that creates a group with the specified name.
120          *
121          * @param aName
122          */
123         public Group(final String aName) {
124                 name = aName;
125         }
126
127         /*
128          * (non-Javadoc)
129          *
130          * @see java.lang.Object#toString()
131          */
132         @Override
133         public String toString() {
134                 return name;
135         }
136
137         /**
138          * Retrieve the id.
139          *
140          * @return the id
141          */
142         public Long getId() {
143                 return id;
144         }
145
146         /**
147          * Retrieve the name.
148          *
149          * @return the name
150          */
151         public String getName() {
152                 return name;
153         }
154
155         /**
156          * Modify the name.
157          *
158          * @param newName the name to set
159          */
160         public void setName(final String newName) {
161                 name = newName;
162         }
163
164         /**
165          * Retrieve the owner.
166          *
167          * @return the owner
168          */
169         public User getOwner() {
170                 return owner;
171         }
172
173         /**
174          * Modify the owner.
175          *
176          * @param newOwner the owner to set
177          */
178         public void setOwner(final User newOwner) {
179                 owner = newOwner;
180         }
181
182         /**
183          * Retrieve the members.
184          *
185          * @return the members
186          */
187         public Set<User> getMembers() {
188                 return members;
189         }
190
191         /**
192          * Replace the member set.
193          *
194          * @param newMembers the new members
195          */
196         public void setMembers(final Set<User> newMembers) {
197                 members = newMembers;
198         }
199
200         /**
201          * Retrieve the audit info.
202          *
203          * @return the audit info
204          */
205         public AuditInfo getAuditInfo() {
206                 return auditInfo;
207         }
208
209         /**
210          * Modify the audit info.
211          *
212          * @param newAuditInfo the new audit info
213          */
214         public void setAuditInfo(final AuditInfo newAuditInfo) {
215                 auditInfo = newAuditInfo;
216         }
217
218         /**
219          * Returns a Data Transfer Object for this Group
220          *
221          * @return GroupDTO
222          */
223         public GroupDTO getDTO() {
224                 final GroupDTO g = new GroupDTO();
225                 g.setId(id);
226                 g.setName(name);
227                 g.setOwner(owner.getDTO());
228                 for (final User u : members)
229                         g.getMembers().add(u.getDTO());
230                 return g;
231         }
232
233         /**
234          * Checks if this groups contains the specified user
235          *
236          * @param user
237          * @return boolean
238          */
239         public boolean contains(final User user) {
240                 return members.contains(user);
241         }
242
243         public void removeMemberFromGroup(final User member){
244                 getMembers().remove(member);
245                 member.getGroupsMember().remove(this);
246         }
247
248
249 }