4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
User.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: User.java
5  * Description: Class representing user
6  */
7 
8 /**
9  * @file User.java
10  *
11  * @brief Class representing user
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.ManyToMany;
29 import javax.persistence.NamedQueries;
30 import javax.persistence.NamedQuery;
31 import javax.persistence.Table;
32 
33 /**
34  * Class representing user
35  *
36  * @brief Class representing user
37  * @author idytrych
38  */
39 @Entity
40 @Table(name = "user")
41 @NamedQueries({
42  @NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
43  @NamedQuery(name = "User.findById", query = "SELECT u FROM User u WHERE u.id = :id"),
44  @NamedQuery(name = "User.findByLogin", query = "SELECT u FROM User u WHERE u.login = :login"),
45  @NamedQuery(name = "User.findByName", query = "SELECT u FROM User u WHERE u.name = :name"),
46  @NamedQuery(name = "User.findByEmail", query = "SELECT u FROM User u WHERE u.email = :email"),
47  @NamedQuery(name = "User.findByNameAndEmail", query = "SELECT u FROM User u WHERE u.name = :name AND u.email = :email"),
48  @NamedQuery(name = "User.findByCredentials", query = "SELECT u FROM User u WHERE u.login = :login AND u.password = :password"),
49  @NamedQuery(name = "User.findByLoginAndSystem", query = "SELECT u FROM User u WHERE u.login = :login AND u.comeFrom = :comeFrom"),
50  @NamedQuery(name = "User.findByEmailAndPassword", query = "SELECT u FROM User u WHERE u.email = :email AND u.password = :password")})
51 public class User implements Serializable {
52  private static final long serialVersionUID = 1L;
53  /** User id */
54  @Id
55  @GeneratedValue(strategy = GenerationType.IDENTITY)
56  @Basic(optional = false)
57  @Column(name = "id")
58  private Integer id;
59  /** Login of user */
60  @Basic(optional = false)
61  @Column(name = "login")
62  private String login;
63  /** Full name */
64  @Basic(optional = true)
65  @Column(name = "name")
66  private String name;
67  /** e-mail */
68  @Basic(optional = false)
69  @Column(name = "email")
70  private String email;
71  /** Password */
72  @Basic(optional = false)
73  @Column(name = "password")
74  private String password;
75  /** Address of system from which user come */
76  @Basic(optional = true)
77  @Column(name = "comeFrom")
78  private String comeFrom;
79  /** URI where is user image located */
80  @Basic(optional = true)
81  @Column(name = "imageURI")
82  private String image;
83 
84  /** Groups to which user belongs */
85  @ManyToMany(mappedBy="users", cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
86  public List<UserGroup> groups;
87 
88  /**
89  * Constructor
90  */
91  public User() {
92  }
93 
94  /**
95  * Constructor of reference objects for searching purposes - no initialization
96  * needed
97  *
98  * @param id Id of user
99  */
100  public User(Integer id) {
101  this.id = id;
102  }
103 
104  /**
105  * Constructor
106  *
107  * @param login User's login
108  * @param name User's full name
109  * @param email User's e-mail
110  * @param password User's password
111  */
112  public User(String login, String name, String email, String password) {
113  this.login = login;
114  this.name = name;
115  this.email = email;
116  this.password = password;
117  }
118 
119  /**
120  * Constructor
121  *
122  * @param id User id
123  * @param login User's login
124  * @param name User's full name
125  * @param email User's e-mail
126  * @param password User's password
127  */
128  public User(Integer id, String login, String name, String email, String password) {
129  this.id = id;
130  this.login = login;
131  this.name = name;
132  this.email = email;
133  this.password = password;
134  }
135 
136  /**
137  * Gets id of user
138  *
139  * @return Returns id of user
140  */
141  public Integer getId() {
142  return id;
143  }
144 
145  /**
146  * Sets id of user
147  *
148  * @param id user id
149  */
150  public void setId(Integer id) {
151  this.id = id;
152  }
153 
154  /**
155  * Gets user groups to which user belongs
156  *
157  * @return Returns user groups to which user belongs
158  */
159  public List<UserGroup> getGroups() {
160  return groups;
161  }
162 
163  /**
164  * Setting user groups to which user belongs
165  *
166  * @param groups User groups to which user belongs
167  */
168  public void setGroups(List<UserGroup> groups) {
169  this.groups = groups;
170  }
171 
172  /**
173  * Gets user groups to which user belongs as ArrayList
174  *
175  * @return Returns user groups to which user belongs as ArrayList
176  */
177  public ArrayList<UserGroup> getGroupsAL() {
178  return new ArrayList<UserGroup>(groups);
179  }
180 
181  /**
182  * Setting user groups to which user belongs
183  *
184  * @param groups User groups to which user belongs
185  */
186  public void setGroups(ArrayList<UserGroup> groups) {
187  this.groups = groups;
188  }
189 
190  /**
191  * Join user to user group
192  *
193  * @param group User group to join
194  */
195  public void addGroup(UserGroup group) {
196  this.groups.add(group);
197  }
198 
199  /**
200  * Gets login of user
201  *
202  * @return Returns login of user
203  */
204  public String getLogin() {
205  return login;
206  }
207 
208  /**
209  * Sets login of user
210  *
211  * @param login Login of user
212  */
213  public void setLogin(String login) {
214  this.login = login;
215  }
216 
217  /**
218  * Gets full name of user
219  *
220  * @return Returns full name of user
221  */
222  public String getName() {
223  return name;
224  }
225 
226  /**
227  * Sets full name of user
228  *
229  * @param name Full name of user
230  */
231  public void setName(String name) {
232  this.name = name;
233  }
234 
235  /**
236  * Gets e-mail of user
237  *
238  * @return Returns e-mail of user
239  */
240  public String getEmail() {
241  return email;
242  }
243 
244  /**
245  * Sets e-mail of user
246  *
247  * @param email E-mail of user
248  */
249  public void setEmail(String email) {
250  this.email = email;
251  }
252 
253  /**
254  * Gets password of user
255  *
256  * @return Returns password of user
257  */
258  public String getPassword() {
259  return password;
260  }
261 
262  /**
263  * Sets password of user
264  *
265  * @param password Password of user
266  */
267  public void setPassword(String password) {
268  this.password = password;
269  }
270 
271  /**
272  * Gets address of system from which user come
273  *
274  * @return Returns address of system from which user come
275  */
276  public String getComeFrom() {
277  return comeFrom;
278  }
279 
280  /**
281  * Sets address of system from which user come
282  *
283  * @param comeFrom Address of system from which user come
284  */
285  public void setComeFrom(String comeFrom) {
286  this.comeFrom = comeFrom;
287  }
288 
289  /**
290  * Gets user's URI (base URI + user id)
291  *
292  * @return Returns user's URI
293  */
294  public String getURI() {
295  return AppBean.getBaseAuthorUri() + id;
296  }
297 
298  /**
299  * Gets user's URI (base URI + user id)
300  *
301  * @return Returns user's URI
302  */
303  public String getURIV2() {
304  return AppBean.getBaseUserUriV2() + id;
305  }
306 
307  /**
308  * Gets URI where is user image located
309  *
310  * @return URI where is user image located
311  */
312  public String getImage() {
313  return image;
314  }
315 
316  /**
317  * Sets URI where is user image located
318  *
319  * @param image URI where is user image located
320  */
321  public void setImage(String image) {
322  this.image = image;
323  }
324 
325  @Override
326  public int hashCode() {
327  int hash = 0;
328  hash += (id != null ? id.hashCode() : 0);
329  return hash;
330  }
331 
332  /**
333  * Compares this with other object and returns, whether objects are same type
334  * and have same id.
335  *
336  * @param object Object to compare with
337  * @return If object is same type and have same id, returns true, false otherwise
338  */
339  @Override
340  public boolean equals(Object object) {
341  if (!(object instanceof User)) {
342  return false;
343  }
344  User other = (User) object;
345  if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
346  return false;
347  }
348  return true;
349  }
350 
351  @Override
352  public String toString() {
353  return "cz.vutbr.fit.knot.annotations.entity.User[id=" + id + "]";
354  }
355 
356  /**
357  * Returns serialized informations about user in XML
358  *
359  * @return Returns serialized informations about user in XML
360  */
361  public String toXMLString() {
362  return "<person id=\"" + getURI() + "\" login=\"" + login + "\" name=\"" + name + "\" email=\"" + email + "\" photoURI=\"\"/>";
363  }
364 
365  /**
366  * Returns serialized informations about user in XML for protocol V2
367  *
368  * @return Returns serialized informations about user in XML for protocol V2
369  */
370  public String toXMLStringV2() {
371  return "<user uri=\"" + getURIV2() + "\" login=\"" + login + "\" name=\"" + name + "\" email=\"" + email + "\" photoURI=\"\"/>";
372  }
373 
374  /**
375  * Returns serialized informations about user in XML with list of user groups
376  *
377  * @return Returns serialized informations about user in XML with list of user groups
378  */
379  public String toXMLStringWG() {
380  String retStr;
381  retStr = "<person id=\"" + getURI() + "\" login=\"" + login + "\" name=\"" + name + "\" email=\"" + email + "\" photoURI=\"\">";
382  retStr = retStr + "<userGroups>";
383  Iterator<UserGroup> grIt = groups.iterator();
384  while (grIt.hasNext()) {
385  UserGroup ug = grIt.next();
386  retStr = retStr + ug.toXMLString();
387  }
388  retStr = retStr + "</userGroups>";
389  retStr = retStr + "</person>";
390  return retStr;
391  }
392 
393 } // class User
User(String login, String name, String email, String password)
Definition: User.java:112
Singleton for storing global variables.
Definition: AppBean.java:47
ArrayList< UserGroup > getGroupsAL()
Definition: User.java:177
Class representing user group.
Definition: UserGroup.java:47
void addGroup(UserGroup group)
Definition: User.java:195
User(Integer id, String login, String name, String email, String password)
Definition: User.java:128
Class representing user.
Definition: User.java:51
void setGroups(ArrayList< UserGroup > groups)
Definition: User.java:186
boolean equals(Object object)
Definition: User.java:340
void setPassword(String password)
Definition: User.java:267
void setGroups(List< UserGroup > groups)
Definition: User.java:168
void setComeFrom(String comeFrom)
Definition: User.java:285