4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
UserGroup.java
Go to the documentation of this file.
1 /*
2  * Project: Server for annotations sharing
3  * Author: Ing. Jaroslav Dytrych idytrych@fit.vutbr.cz
4  * File: UserGroup.java
5  * Description: Class representing user group
6  */
7 
8 /**
9  * @file UserGroup.java
10  *
11  * @brief Class representing user group
12  */
13 
14 package cz.vutbr.fit.knot.annotations.entity;
15 
17 import java.io.Serializable;
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21 import javax.persistence.Basic;
22 import javax.persistence.CascadeType;
23 import javax.persistence.Column;
24 import javax.persistence.Entity;
25 import javax.persistence.GeneratedValue;
26 import javax.persistence.GenerationType;
27 import javax.persistence.Id;
28 import javax.persistence.JoinColumn;
29 import javax.persistence.JoinTable;
30 import javax.persistence.ManyToMany;
31 import javax.persistence.NamedQueries;
32 import javax.persistence.NamedQuery;
33 import javax.persistence.Table;
34 
35 /**
36  * Class representing user group
37  *
38  * @brief Class representing user group
39  * @author idytrych
40  */
41 @Entity
42 @Table(name = "userGroup")
43 @NamedQueries({
44  @NamedQuery(name = "UserGroup.findAll", query = "SELECT u FROM UserGroup u"),
45  @NamedQuery(name = "UserGroup.findById", query = "SELECT u FROM UserGroup u WHERE u.id = :id"),
46  @NamedQuery(name = "UserGroup.findByName", query = "SELECT u FROM UserGroup u WHERE u.name = :name")})
47 public class UserGroup implements Serializable {
48  private static final long serialVersionUID = 1L;
49  /** Group id */
50  @Id
51  @GeneratedValue(strategy = GenerationType.IDENTITY)
52  @Basic(optional = false)
53  @Column(name = "id")
54  private Integer id;
55  /** Group name */
56  @Basic(optional = false)
57  @Column(name = "name")
58  private String name;
59 
60  /** List of users in this group */
61  @ManyToMany(cascade = CascadeType.ALL)
62  @JoinTable(name = "userInGroup",
63  joinColumns =
64  @JoinColumn(name = "group_id", referencedColumnName = "id"),
65  inverseJoinColumns =
66  @JoinColumn(name = "user_id", referencedColumnName = "id"))
67  public List<User> users;
68 
69  /**
70  * Constructor
71  */
72  public UserGroup() {
73  }
74 
75  /**
76  * Constructor of reference objects for searching purposes - no initialization
77  * needed
78  *
79  * @param id Id of group
80  */
81  public UserGroup(Integer id) {
82  this.id = id;
83  }
84 
85  /**
86  * Constructor
87  *
88  * @param id Id of group
89  * @param name Name of group
90  */
91  public UserGroup(Integer id, String name) {
92  this.id = id;
93  this.name = name;
94  this.users = new ArrayList<User>();
95  }
96 
97  /**
98  * Constructor
99  *
100  * @param name Name of group
101  */
102  public UserGroup(String name) {
103  this.name = name;
104  this.users = new ArrayList<User>();
105  }
106 
107  /**
108  * Gets id of user group
109  *
110  * @return Id of user group
111  */
112  public Integer getId() {
113  return id;
114  }
115 
116  /**
117  * Sets id of user group
118  *
119  * @param id Id of user group
120  */
121  public void setId(Integer id) {
122  this.id = id;
123  }
124 
125  /**
126  * Gets name of group
127  *
128  * @return Returns name of group
129  */
130  public String getName() {
131  return name;
132  }
133 
134  /**
135  * Sets name of group
136  *
137  * @param name Name of group
138  */
139  public void setName(String name) {
140  this.name = name;
141  }
142 
143  /**
144  * Gets users in this group
145  *
146  * @return Returns users in this group
147  */
148  public List<User> getUsers() {
149  return users;
150  }
151 
152  /**
153  * Gets users in this group as ArrayList
154  *
155  * @return Returns users in this group as ArrayList
156  */
157  public ArrayList<User> getUsersAL() {
158  return new ArrayList<User>(users);
159  }
160 
161  /**
162  * Sets users in this group
163  *
164  * @param users Users in this group
165  */
166  public void setUsers(List<User> users) {
167  this.users = users;
168  }
169 
170  /**
171  * Sets users in this group (setting from ArrayList)
172  *
173  * @param users Users in this group
174  */
175  public void setUsers(ArrayList<User> users) {
176  this.users = users;
177  }
178 
179  /**
180  * Adds user to the group
181  *
182  * @param user User to be added
183  */
184  public void addUser(User user) {
185  this.users.add(user);
186  }
187 
188  /**
189  * Gets user group's URI (base URI + user group id)
190  *
191  * @return Returns user group's URI
192  */
193  public String getUri() {
194  return AppBean.getBaseGroupUri() + id;
195  }
196 
197  @Override
198  public int hashCode() {
199  int hash = 0;
200  hash += (id != null ? id.hashCode() : 0);
201  return hash;
202  }
203 
204  /**
205  * Compares this with other object and returns, whether objects are same type
206  * and have same id.
207  *
208  * @param object Object to compare with
209  * @return If object is same type and have same id, returns true, false otherwise
210  */
211  @Override
212  public boolean equals(Object object) {
213  if (!(object instanceof UserGroup)) {
214  return false;
215  }
216  UserGroup other = (UserGroup) object;
217  if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
218  return false;
219  }
220  return true;
221  }
222 
223  @Override
224  public String toString() {
225  return "cz.vutbr.fit.knot.annotations.entity.UserGroup[id=" + id + "]";
226  }
227 
228  /**
229  * Returns serialized informations about user group in XML
230  *
231  * @return Returns serialized informations about user group in XML
232  */
233  public String toXMLString() {
234  return "<group uri=\"" + getUri() + "\" name=\"" + name + "\"/>";
235  }
236 
237  /**
238  * Returns serialized informations about user group in XML with list of users
239  *
240  * @return Returns serialized informations about user group in XML with list of users
241  */
242  public String toXMLStringWP() {
243  String retStr;
244  retStr = "<group uri=\"" + getUri() + "\" name=\"" + name + "\">";
245  retStr = retStr + "<persons>";
246  Iterator<User> uIt = users.iterator();
247  while (uIt.hasNext()) {
248  User u = uIt.next();
249  retStr = retStr + u.toXMLString();
250  }
251  retStr = retStr + "</persons>";
252  retStr = retStr + "</group>";
253  return retStr;
254  }
255 
256 } // class UserGroup
Singleton for storing global variables.
Definition: AppBean.java:47
Class representing user group.
Definition: UserGroup.java:47
Class representing user.
Definition: User.java:51
void setUsers(ArrayList< User > users)
Definition: UserGroup.java:175