4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
Subscription.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: Subscription.java
5  * Description: Class representing subscription.
6  */
7 
8 /**
9  * @file Subscription.java
10  *
11  * @brief Class representing subscription.
12  */
13 
14 package cz.vutbr.fit.knot.annotations.entity;
15 
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.List;
22 import javax.persistence.Basic;
23 import javax.persistence.CascadeType;
24 import javax.persistence.Column;
25 import javax.persistence.Entity;
26 import javax.persistence.GeneratedValue;
27 import javax.persistence.GenerationType;
28 import javax.persistence.Id;
29 import javax.persistence.JoinColumn;
30 import javax.persistence.NamedQueries;
31 import javax.persistence.NamedQuery;
32 import javax.persistence.OneToMany;
33 import javax.persistence.OneToOne;
34 import javax.persistence.Table;
35 import javax.persistence.Transient;
36 import javax.validation.constraints.NotNull;
37 import javax.validation.constraints.Size;
38 
39 /**
40  * Class representing subscription.
41  *
42  * @brief Class representing subscription
43  * @author Martin Petr (xpetrm05)
44  */
45 @Entity
46 @Table(name = "subscription")
47 @NamedQueries({
48  @NamedQuery(name = "Subscription.findAll", query = "SELECT s FROM Subscription s"),
49  @NamedQuery(name = "Subscription.findById", query = "SELECT s FROM Subscription s WHERE s.id = :id"),
50  @NamedQuery(name = "Subscription.findByName", query = "SELECT s FROM Subscription s WHERE s.name = :name"),
51  @NamedQuery(name = "Subscription.findByUserId", query = "SELECT s FROM Subscription s WHERE s.userId = :userId"),
52  @NamedQuery(name = "Subscription.findByGroupId", query = "SELECT s FROM Subscription s WHERE s.groupId = :groupId"),
53  @NamedQuery(name = "Subscription.findByIdAndUserId", query = "SELECT s FROM Subscription s WHERE s.id = :id AND s.userId = :userId"),
54 })
55 public class Subscription implements Serializable {
56  /** Id of subscription */
57  @Id
58  @GeneratedValue(strategy = GenerationType.IDENTITY)
59  @Basic(optional = false)
60  @Column(name = "id")
61  private Integer id;
62 
63  /** Name of subscription */
64  @Basic(optional = false)
65  @NotNull
66  @Size(min = 1, max = 255)
67  @Column(name = "name")
68  private String name;
69 
70  /** Id of author of subscription */
71  @Basic(optional = false)
72  @NotNull
73  @Column(name = "userId", insertable=false, updatable=false)
74  private int userId;
75 
76  /** Id of group of subscription */
77  @Basic(optional = false)
78  @NotNull
79  @Column(name = "groupId", insertable=false, updatable=false)
80  private int groupId;
81 
82  /** List of subscriptions */
83  @OneToMany(mappedBy = "refSubscription", cascade = CascadeType.ALL,
84  targetEntity = Source.class)
85  private List<Source> subscriptions;
86 
87  /** Author of subscription (user) */
88  @OneToOne(optional = false)
89  @JoinColumn(name = "userId", referencedColumnName = "id")
90  private User user;
91 
92  /** Group of subscription */
93  @OneToOne(optional = false)
94  @JoinColumn(name = "groupId", referencedColumnName = "id")
95  private UserGroup group;
96 
97  @Transient
98  /* Temporary id of subscription */
99  private Integer tmpId;
100 
101  /**
102  * Constructor
103  */
104  public Subscription() {
105  }
106 
107  /**
108  * Constructor
109  *
110  * @param id id of subscription
111  */
112  public Subscription(Integer id) {
113  this.id = id;
114  }
115 
116  /**
117  * Constructor
118  *
119  * @param id id of subscription
120  * @param name name of subscription
121  * @param userId id of author of subscription
122  */
123  public Subscription(Integer id, String name, int userId) {
124  this.id = id;
125  this.name = name;
126  this.userId = userId;
127  }
128 
129  /**
130  * Gets id of subscription.
131  *
132  * @return id of subscription
133  */
134  public Integer getId() {
135  return id;
136  }
137 
138  /**
139  * Sets id of subscription.
140  *
141  * @param id id of subscription
142  */
143  public void setId(Integer id) {
144  this.id = id;
145  }
146 
147  /**
148  * Gets name of subscription.
149  *
150  * @return name of subscription
151  */
152  public String getName() {
153  return name;
154  }
155 
156  /**
157  * Sets name of subscription.
158  *
159  * @param name name of subscription
160  */
161  public void setName(String name) {
162  this.name = name;
163  }
164 
165  /**
166  * Gets id of author of subscription.
167  *
168  * @return id of author of subscription
169  */
170  public int getUserId() {
171  return userId;
172  }
173 
174  /**
175  * Sets id of author of subscription.
176  *
177  * @param userId id of author of subscription
178  */
179  public void setUserId(int userId) {
180  this.userId = userId;
181  }
182 
183  /**
184  * Gets list of subscriptions.
185  *
186  * @return list of subscriptions
187  */
188  public List<Source> getSubscriptions() {
189  return subscriptions;
190  }
191 
192  /**
193  * Add source of subscription.
194  *
195  * @param subscription source of subscription
196  */
197  public void addSubscription(Source subscription) {
198  if(this.subscriptions == null){
199  this.subscriptions = new ArrayList<Source>();
200  }
201 
202  this.subscriptions.add(subscription);
203  }
204 
205  /**
206  * Sets list of subscriptions.
207  *
208  * @param subscriptions list of subscriptions
209  */
210  public void setSubscriptions(List<Source> subscriptions) {
211  this.subscriptions = subscriptions;
212  }
213 
214  /**
215  * Gets author of subscription.
216  *
217  * @return author of subscription
218  */
219  public User getUser() {
220  return user;
221  }
222 
223  /**
224  * Sets author of subscription.
225  *
226  * @param user author of subscription
227  */
228  public void setUser(User user) {
229  this.user = user;
230  }
231 
232  /**
233  * Gets temporary id of subscription.
234  *
235  * @return temporary id of subscription
236  */
237  public Integer getTmpId() {
238  return tmpId;
239  }
240 
241  /**
242  * Sets temporary id of subscription
243  *
244  * @param tmpId temporary id of subscription
245  */
246  public void setTmpId(Integer tmpId) {
247  this.tmpId = tmpId;
248  }
249 
250  /**
251  * Gets id of user group to which this subscription belongs
252  *
253  * @return Returns id of user group to which this subscription belongs
254  */
255  public int getGroupId() {
256  return groupId;
257  }
258 
259  /**
260  * Sets id of user group to which this subscription belongs
261  *
262  * @param groupId Id of user group to which this subscription belongs
263  */
264  public void setGroupId(int groupId) {
265  this.groupId = groupId;
266  }
267 
268  /**
269  * Gets user group to which this subscription belongs
270  *
271  * @return Returns user group to which this subscription belongs
272  */
273  public UserGroup getGroup() {
274  return group;
275  }
276 
277  /**
278  * Sets user group to which this subscription belongs
279  *
280  * @param group User group to which this subscription belongs
281  */
282  public void setGroup(UserGroup group) {
283  this.group = group;
284  }
285 
286  @Override
287  public int hashCode() {
288  int hash = 0;
289  hash += (id != null ? id.hashCode() : 0);
290  return hash;
291  }
292 
293  /**
294  * Compares this with other object and returns, whether objects are same type
295  * and have same id.
296  *
297  * @param object Object to compare with
298  * @return If object is same type and have same id, returns true, false otherwise
299  */
300  @Override
301  public boolean equals(Object object) {
302  if (!(object instanceof Subscription)) {
303  return false;
304  }
305  Subscription other = (Subscription) object;
306  if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
307  return false;
308  }
309  return true;
310  }
311 
312  @Override
313  public String toString() {
314  return "cz.vutbr.fit.knot.annotations.entity.Subscription[ id=" + id + " ]";
315  }
316 
317  /**
318  * Returns serialized informations about subscription in XML for protocol
319  * version 2.
320  *
321  * @return Returns serialized informations about subscription in XML
322  */
323  public String toXMLStringV2(){
324  StringBuilder result = new StringBuilder("<subscription uri=\"");
325  result.append(AppBean.getBaseSubscriptionUri());
326  result.append(id.toString());
327  result.append("\" name=\"");
328  result.append(name);
329  result.append("\" authorUri=\"");
330  result.append(user.getURI());
331 
332 
333  StringBuilder sources = new StringBuilder();
334 
335  if(subscriptions != null){
336  Iterator<Source> subscrIt = subscriptions.iterator();
337  while(subscrIt.hasNext()){
338  Source actualSubs = subscrIt.next();
339  sources.append(actualSubs.toXMLStringV2());
340  }
341  }
342 
343  if(sources.length() > 0){
344  result.append("\">");
345  result.append(sources.toString());
346  result.append("</subscription>");
347  }else{
348  result.append("\"/>");
349  }
350 
351  return result.toString();
352  }
353 
354  /**
355  * Returns serialized informations about subscription in XML for protocol
356  * version 2 with specific tag name.
357  *
358  * @param tagName name of tag
359  *
360  * @return Returns serialized informations about subscription in XML
361  */
362  public String toXMLStringV2(String tagName){
363  StringBuilder result = new StringBuilder("<");
364  result.append(tagName);
365  result.append(" uri=\"");
366  result.append(AppBean.getBaseSubscriptionUri());
367  result.append(id.toString());
368  result.append("\" name=\"");
369  result.append(name);
370  result.append("\" authorUri=\"");
371  result.append(user.getURI());
372 
373 
374  StringBuilder sources = new StringBuilder();
375 
376  if(subscriptions != null){
377  Iterator<Source> subscrIt = subscriptions.iterator();
378  while(subscrIt.hasNext()){
379  Source actualSubs = subscrIt.next();
380  sources.append(actualSubs.toXMLStringV2());
381  }
382  }
383 
384  if(sources.length() > 0){
385  result.append("\">");
386  result.append(sources.toString());
387  result.append("</");
388  result.append(tagName);
389  result.append(">");
390  }else{
391  result.append("\"/>");
392  }
393 
394  return result.toString();
395  }
396 
397  /**
398  * Gets negative subscription as list of SubscribedSources.
399  *
400  * @return List of negative SubscribedSources
401  */
402  public ArrayList<SubscribedSource> getNegSourcesAsSubSource(){
403  ArrayList<SubscribedSource> returnList = null;
404  if(this.subscriptions != null){
405  returnList = new ArrayList<SubscribedSource>(this.subscriptions.size());
406  Iterator<Source> sourceIt = this.subscriptions.iterator();
407  while(sourceIt.hasNext()){
408  returnList.add(sourceIt.next().getSourceAsSubSource());
409  }
410  }
411 
412  return returnList;
413  }
414 
415  /**
416  * Gets positive subscription as list of SubscribedSources.
417  *
418  * @return List of positive SubscribedSources
419  */
420  public ArrayList<SubscribedSource> getSourcesAsSubSource(boolean polarity){
421  ArrayList<SubscribedSource> returnList = null;
422  if(this.subscriptions != null){
423  returnList = new ArrayList<SubscribedSource>(this.subscriptions.size());
424  Iterator<Source> sourceIt = this.subscriptions.iterator();
425  while(sourceIt.hasNext()){
426  Source currentSource = sourceIt.next();
427  if(currentSource.getPolarity() == polarity){
428  returnList.add(currentSource.getSourceAsSubSource());
429  }
430  }
431  }
432 
433  return returnList;
434  }
435 } // public class Subscription
Singleton for storing global variables.
Definition: AppBean.java:47
ArrayList< SubscribedSource > getSourcesAsSubSource(boolean polarity)
Class representing user group.
Definition: UserGroup.java:47
ArrayList< SubscribedSource > getNegSourcesAsSubSource()
Class representing user.
Definition: User.java:51
void setSubscriptions(List< Source > subscriptions)
Class representing source of subscription.
Definition: Source.java:47
Subscription(Integer id, String name, int userId)