4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
StoryScope.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: StoryScope.java
5  * Description: Class representing StoryScope for needs of Storyscope Interface
6  */
7 
8 /**
9  * @file StoryScope.java
10  *
11  * @brief Class representing StoryScope for needs of Storyscope Interface
12  */
13 
14 package cz.vutbr.fit.knot.annotations.modules.StoryscopeInterface;
15 
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import javax.persistence.Basic;
26 import javax.persistence.Column;
27 import javax.persistence.Entity;
28 import javax.persistence.GeneratedValue;
29 import javax.persistence.GenerationType;
30 import javax.persistence.Id;
31 import javax.persistence.NamedQueries;
32 import javax.persistence.NamedQuery;
33 import javax.persistence.Table;
34 import javax.persistence.Transient;
35 
36 
37 /**
38  * This class represent StoryScope for needs of SEC Interface.
39  *
40  * @brief Class representing StoryScope for needs of SEC Interface.
41  * @author Martin Petr (xpetrm05)
42  */
43 @Entity
44 @Table(name = "storyScopes")
45 @NamedQueries({
46  @NamedQuery(name = "StoryScope.findAll", query = "SELECT s FROM StoryScope s"),
47  @NamedQuery(name = "StoryScope.findById", query = "SELECT s FROM StoryScope s WHERE s.id = :id")})
48 public class StoryScope implements Serializable {
49  /** ID of StoryScope */
50  @Id
51  @GeneratedValue(strategy = GenerationType.IDENTITY)
52  @Basic(optional = false)
53  @Column(name = "id")
54  private Integer id;
55 
56  /** Name of StoryScope */
57  @Basic(optional = false)
58  @Column(name = "name")
59  private String name;
60 
61  /** URL address of SEC Client */
62  @Basic(optional = false)
63  @Column(name = "clientUrl")
64  private String clientURL;
65  /** Array of add messages */
66  @Transient
67  private ArrayList<String> addMessages = null;
68  /** Array of change messages */
69  @Transient
70  private ArrayList<String> changeMessages = null;
71  /** Array of remove messages */
72  @Transient
73  private ArrayList<String> deleteMessages = null;
74 
75  /**
76  * Constructor
77  */
78  public StoryScope() {
79  }
80 
81  /**
82  * Constructor
83  *
84  * @param id ID of StoryScope
85  * @param name name of StoryScope
86  * @param clientURL URL address of SEC Client
87  */
88  public StoryScope(Integer id, String name, String clientURL){
89  this.id = id;
90  this.name = name;
91  this.clientURL = clientURL;
92  }
93 
94  /**
95  * Gets id of StoryScope
96  *
97  * @return Returns id of StoryScope
98  */
99  public Integer getId() {
100  return id;
101  }
102 
103  /**
104  * Sets id of StoryScope
105  *
106  * @param id Id of StoryScope
107  */
108  public void setId(Integer id) {
109  this.id = id;
110  }
111 
112  /**
113  * Gets name of StoryScope
114  *
115  * @return Returns name of StoryScope
116  */
117  public String getName() {
118  return name;
119  }
120 
121  /**
122  * Sets name of StoryScope
123  *
124  * @param name Name of StoryScope
125  */
126  public void setName(String name) {
127  this.name = name;
128  }
129 
130  /**
131  * Gets URL addres of SEC Client of StoryScope
132  *
133  * @return Returns URL address of SEC client of StoryScope
134  */
135  public String getClientURL() {
136  return clientURL;
137  }
138 
139  /**
140  * Sets URL addres of SEC Client of StoryScope
141  *
142  * @param clientURL URL addres of SEC Client
143  */
144  public void setClientURL(String clientURL) {
145  this.clientURL = clientURL;
146  }
147 
148  /**
149  * Gets list of all subscriptions for StoryScope
150  *
151  * @return List of subscriptions
152  */
153  public List<Object> getSubscriptions() {
154  Object[] params = {"storyScope", id};
155  @SuppressWarnings("unchecked")
156  List<Object> retList = AppBean.getPersistenceManager().queryDB("SubscribedItem.findSubscribed", params);
157 
158  return retList;
159  }
160 
161  /**
162  * Gets array list of subscribed sources for StoryScope converted to
163  * SubscribeSource
164  * objects.
165  *
166  * @return Array list of subscribed sources
167  */
168  public ArrayList<SubscribedSource> getSubscribedList(){
169  Object[] params = {"storyScope",id};
170 
171  @SuppressWarnings("unchecked")
172  ArrayList<SubscribedSource> retList = this.makeArrayList(AppBean.getPersistenceManager().queryDB("SubscribedItem.findSubscribed",params));
173 
174  return retList;
175  }
176 
177  /**
178  * Gets array list of unsubscribed sources for StoryScope converted
179  * to SubscribedSource objects.
180  *
181  * @return Array list of unsubscribed sources
182  */
183  public ArrayList<SubscribedSource> getUnsubscribedList(){
184  Object[] params = {"storyScope",id};
185 
186  @SuppressWarnings("unchecked")
187  ArrayList<SubscribedSource> retList = this.makeArrayList(AppBean.getPersistenceManager().queryDB("SubscribedItem.findUnsubscribed",params));
188 
189  return retList;
190  }
191 
192  /**
193  * Gets string with all messages with added annotations for current StoryScope
194  * that can't be delivered before.
195  *
196  * @return String with added annotations messages
197  */
198  public String getMessagesAdd(){
199  return messagesToString(savedMessagesToAL(getSavedMessagesFromDB(SavedMessage.MESSAGE_ADD)));
200  }
201 
202  /**
203  * Gets string with all messages with changed annotations for current StoryScope
204  * that can't be delivered before.
205  *
206  * @return String with changed annotations messages
207  */
208  public String getMessagesChange(){
209  return messagesToString(savedMessagesToAL(getSavedMessagesFromDB(SavedMessage.MESSAGE_CHANGE)));
210  }
211 
212  /**
213  * Gets string with all messages with deleted annotations for current StoryScope
214  * that can't be delivered before.
215  *
216  * @return String with deleted annotations messages
217  */
218  public String getMessagesDelete(){
219  return messagesToString(savedMessagesToAL(getSavedMessagesFromDB(SavedMessage.MESSAGE_DELETE)));
220  }
221 
222  /**
223  * Method deletes all messages with annotations for current StoryScope.
224  */
225  public void deleteMessages(){
226  Object[] params = {"storyScopeId",id};
227  // get all saved messages for current StoryScope from DB
228  @SuppressWarnings("unchecked")
229  List<SavedMessage> messages = AppBean.getPersistenceManager().queryDB("SavedMessage.findByStoryScopeId",params);
230 
231  //if there is no message in DB , nothing to do here
232  if(messages != null){
233  Iterator <SavedMessage> messagesIt = messages.iterator();
234  while(messagesIt.hasNext()){
235  //delete all founded saved messages from DB
236  if(AppBean.getPersistenceManager().removeEntity(messagesIt.next())){
238  String msg = "DB failure during deleting saved message form StoryScope account.";
239  Logger.getLogger(StoryScope.class.getName()).log(Level.SEVERE, msg);
240  }
241  }
242  }
243  }
244  }
245 
246  /**
247  * Add new undelivered messages (one kind of messages) to StoryScope into
248  * database
249  *
250  * @param messages list of undelivered messages
251  * @param type of messages
252  */
253  public void addSavedMessages(ArrayList<String> messages, int type){
254  Iterator<String> messagesIt = messages.iterator();
255  while(messagesIt.hasNext()){
256  //go trough the list
257  SavedMessage newSavedMessage = new SavedMessage(id,type,messagesIt.next());
258  //try to save all new undelivered messages
259  if(AppBean.getPersistenceManager().persistEntity(newSavedMessage)){
261  String msg = "DB failure during saving message for StoryScope account.";
262  Logger.getLogger(StoryScope.class.getName()).log(Level.SEVERE, msg);
263  }
264  }
265  }
266  }
267 
268  @Override
269  public int hashCode() {
270  int hash = 0;
271  hash += (id != null ? id.hashCode() : 0);
272  return hash;
273  }
274 
275  /**
276  * Compares this with other object and returns, whether objects are same type
277  * and have same id.
278  *
279  * @param object Object to compare with
280  * @return If object is same type and have same id, returns true, false otherwise
281  */
282  @Override
283  public boolean equals(Object object) {
284  if (!(object instanceof SubscribedItem)) {
285  return false;
286  }
287  StoryScope tested = (StoryScope) object;
288  if ((this.id == null && tested.id != null) || (this.id != null && !this.id.equals(tested.id))) {
289  return false;
290  }
291  return true;
292  }
293 
294  @Override
295  public String toString() {
296  return "cz.vutbr.fit.knot.annotations.modules.StoryscopeInterface.StoryScope[id=" + id + ", name=" + name + ", client URL=" + clientURL + "]";
297  }
298 
299  /**
300  * Method converts list of SubscribedItems to ArrayList of SubscribedSources
301  * that is used in SEC module and other parts of server.
302  *
303  * @param objList list of SubscribedItems to convert
304  * @return Array list of subscriptions
305  */
306  private ArrayList<SubscribedSource> makeArrayList(List<SubscribedItem> objList){
307  if(objList == null || objList.isEmpty()) return null;
308  ArrayList <SubscribedSource> arrList = new ArrayList <SubscribedSource>();
309  Iterator objListIterator = objList.iterator();
310 
311  while(objListIterator.hasNext()){
312 
313  SubscribedItem listElement = (SubscribedItem) objListIterator.next();
314  String type = "*";
315  String user = null;
316  String source = null;
317 
318  if(listElement.getAnnotationType() != null){
319  type = listElement.getAnnotationType().getUri();
320  }
321 
322  if(listElement.getUser() != null){
323  user = listElement.getUser().getURI();
324  }
325  if(listElement.getSource() != null){
326  source = listElement.getSource();
327  }
328 
329  SubscribedSource newSource = new SubscribedSource(type,user,source);
330  arrList.add(newSource);
331  }
332 
333  return arrList;
334  }
335 
336  /**
337  * Method convert list of saved messages object to list of strings with messages
338  * (string representation of messages) for StoryScope.
339  *
340  * @param savedMessages list of saved messages to convert
341  * @return Array list of strings with messages
342  */
343  private ArrayList<String> savedMessagesToAL(List<SavedMessage> savedMessages){
344  ArrayList<String> result = new ArrayList<String>();
345  if(savedMessages != null)
346  {
347  Iterator<SavedMessage> messagesIt = savedMessages.iterator();
348  while(messagesIt.hasNext()){
349  result.add(messagesIt.next().getMessage());
350  }
351  }
352  return result;
353  }
354 
355  /**
356  * Method convert array strins with messages for StoryScope to one string object
357  * that contains all messages.
358  *
359  * @param messages list of saved messages to convert
360  * @return String with messages all together
361  */
362  public static String messagesToString(ArrayList<String> messages){
363  Iterator<String> messagesIt = messages.iterator();
364  String result = "";
365  while(messagesIt.hasNext()){
366  result += messagesIt.next();
367  }
368  return result;
369  }
370 
371  /**
372  * Method gets saved messages of specific type (and for current StorySapce)
373  * from database.
374  *
375  * @param typeOfMessages type of messages that will be returned
376  * @return List of saved messages of specific type
377  */
378  private List<SavedMessage> getSavedMessagesFromDB(int typeOfMessages){
379  Object[] params = {"storyScopeId",id,
380  "messageType",typeOfMessages};
381  @SuppressWarnings("unchecked")
382  List<SavedMessage> retList = AppBean.getPersistenceManager().queryDB("SavedMessage.findByStoryScopeAndType",params);
383 
384  return retList;
385  }
386 
387 } // class StoryScope
Class representing StoryScope for needs of SEC Interface.
Definition: StoryScope.java:48
Singleton for storing global variables.
Definition: AppBean.java:47
void addSavedMessages(ArrayList< String > messages, int type)
static String messagesToString(ArrayList< String > messages)
ArrayList< String > savedMessagesToAL(List< SavedMessage > savedMessages)
Class representing item of subscripted or unsubscripted sources list.
StoryScope(Integer id, String name, String clientURL)
Definition: StoryScope.java:88
List< SavedMessage > getSavedMessagesFromDB(int typeOfMessages)
ArrayList< SubscribedSource > makeArrayList(List< SubscribedItem > objList)
This class represent saved message that belongs unavailable StoryScope.