4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
EditSubscriptions.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: EditSubscriptions.java
5  * Description: Backbean for pages for adding, editing and deleting of StoryScope's subscriptions
6  */
7 
8 /**
9  * @file EditSubscriptions.java
10  *
11  * @brief Backbean for pages for adding, editing and deleting of StoryScope's subscriptions
12  */
13 
14 package cz.vutbr.fit.knot.annotations.web;
15 
22 import java.io.Serializable;
23 import java.util.List;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import javax.faces.bean.ManagedBean;
27 import javax.faces.bean.ViewScoped;
28 
29 /**
30  * Backbean for pages for adding, editing and deleting of StoryScope's subscriptions
31  *
32  * @brief Backbean for pages for adding, editing and deleting of StoryScope's subscriptions
33  * @author Martin Petr (xpetrm05)
34  */
35 @ManagedBean
36 @ViewScoped
37 public class EditSubscriptions implements Serializable{
38  /**
39  * Value represent URI of annotation's type
40  */
41  private String annotURI = null;
42  /**
43  * Value represent login of user
44  */
45  private String userLogin = null;
46  /**
47  * Value represent URI of source (StoryScope, Group, etc..)
48  */
49  private String sourceURI = null;
50  /**
51  * Value represent type of subscription (subscribe or unsubscribe)
52  */
53  private Integer subscriptionType = null;
54  /**
55  * Object of edited subscription
56  */
58  /**
59  * Error message displayed in form
60  */
61  private String errorMessage = null;
62  /**
63  * StorySapce which subscription belongs
64  */
65  private StoryScope editedStoryScope = null;
66 
67 
68  /**
69  * Constructor
70  */
71  public EditSubscriptions() {
72  }
73 
74  /**
75  * Gets value that represent type of subscription (subscribe or unsubscribe)
76  *
77  * @return Returns value that represent type of subscription (subscribe or unsubscribe)
78  */
79  public Integer getSubscriptionType(){
80  if(subscribedItem == null){
81  subscribedItem = SessionManager.getSession().getEditedSubscription();
82  if(subscribedItem == null){
84  subscribedItem.setStoryScopeId(SessionManager.getSession().getEditedStoryScope().getId());
85  }
86  }
87 
88  SubscribedItem backup = null;
89  if (SessionManager.getSession().getFormBackup() != null) {
90  if (SessionManager.getSession().getFormBackup().getClass().getName().endsWith("SubscribedItem")) {
91  backup = (SubscribedItem) SessionManager.getSession().getFormBackup();
92  if (backup.getId() != subscribedItem.getId()) {
93  backup = null;
94  }
95  }
96  }
97 
98  subscriptionType = SessionManager.getSession().getSaveSubscriptionType();
99 
100  if(subscriptionType == null && backup != null) {
101  subscriptionType = backup.getType();
102  } else if(subscriptionType == null && subscribedItem != null) {
103  subscriptionType = subscribedItem.getType();
104  } else if(subscriptionType == null) {
105  subscriptionType = 0;
106  }
107  return subscriptionType;
108  }
109 
110  /**
111  * Sets value that represent type of subscription (subscribe or unsubscribe)
112  *
113  * @param annotType value that represent type of subscription (subscribe or unsubscribe)
114  */
115  public void setSubscriptionType(Integer annotType){
116  this.subscriptionType = annotType;
117  }
118 
119  /**
120  * Gets edited subscription annotation type URI field in form
121  * If StoryScope not set yet, pick up it from session
122  *
123  * @return Returns subscription annotation type URI
124  */
125  public String getAnnotURI(){
126  if(subscribedItem == null){
127  subscribedItem = SessionManager.getSession().getEditedSubscription();
128  if(subscribedItem == null){
130  subscribedItem.setStoryScopeId(SessionManager.getSession().getEditedStoryScope().getId());
131  }
132  }
133 
134  SubscribedItem backup = null;
135  if (SessionManager.getSession().getFormBackup() != null) {
136  if (SessionManager.getSession().getFormBackup().getClass().getName().endsWith("SubscribedItem")) {
137  backup = (SubscribedItem) SessionManager.getSession().getFormBackup();
138  if (backup.getId() != subscribedItem.getId()) {
139  backup = null;
140  }
141  }
142  }
143 
144  SubscribedItem tempItem = null;
145 
146  annotURI = SessionManager.getSession().getAnnotationTypeAddress();
147 
148  if(annotURI == null && backup != null) {
149  tempItem = backup;
150  } else if(annotURI == null && subscribedItem != null) {
151  tempItem = subscribedItem;
152  } else if(annotURI == null) {
153  annotURI = "";
154  return annotURI;
155  }
156 
157  if(tempItem != null)
158  {
159  if(tempItem.getAnnotationType() == null){
160  annotURI = "";
161  return annotURI;
162  }
163 
164  Object[] params = {"id",tempItem.getAnnotationType().getId()};
165  @SuppressWarnings("unchecked")
166  List<AnnotType> list = AppBean.getPersistenceManager().queryDB("AnnotType.findById",params);
167  if(list == null){
168  errorMessage = MessageProvider.getMessage("ssSubsEditTypeNotFound");
169  return null;
170  }
171 
172  annotURI = list.get(0).getUri();
173  }
174  return annotURI;
175  }
176 
177  /**
178  * Sets value that represent edited subscription annotation type URI field in form
179  *
180  * @param annotURI value that represent edited subscription annotation type URI field in form
181  */
182  public void setAnnotURI(String annotURI){
183  this.annotURI = annotURI;
184  }
185 
186  /**
187  * Gets value that represent URI of source (StoryScope, Group, etc..)
188  *
189  * @return Returns value that represent URI of source (StoryScope, Group, etc..)
190  */
191  public String getSourceURI(){
192  if(subscribedItem == null){
193  subscribedItem = SessionManager.getSession().getEditedSubscription();
194  if(subscribedItem == null){
196  subscribedItem.setStoryScopeId(SessionManager.getSession().getEditedStoryScope().getId());
197  }
198  }
199 
200  SubscribedItem backup = null;
201  if (SessionManager.getSession().getFormBackup() != null) {
202  if (SessionManager.getSession().getFormBackup().getClass().getName().endsWith("SubscribedItem")) {
203  backup = (SubscribedItem) SessionManager.getSession().getFormBackup();
204  if (backup.getId() != subscribedItem.getId()) {
205  backup = null;
206  }
207  }
208  }
209 
210  sourceURI = SessionManager.getSession().getSaveSourceUri();
211 
212  if(sourceURI == null && backup != null) {
213  sourceURI = backup.getSource();
214  } else if(sourceURI == null && subscribedItem != null) {
215  sourceURI = subscribedItem.getSource();
216  } else if(sourceURI == null) {
217  sourceURI = "";
218  }
219 
220  return sourceURI;
221  }
222 
223  /**
224  * Sets value that represent URI of source (StoryScope, Group, etc..)
225  *
226  * @param sourceURI value that represent URI of source (StoryScope, Group, etc..)
227  */
228  public void setSourceURI(String sourceURI){
229  this.sourceURI = sourceURI;
230  }
231 
232  /**
233  * Gets login of user that is subscribed (or unsubscribed)
234  *
235  * @return Returns login of user that is subscribed (or unsubscribed)
236  */
237  public String getUserLogin(){
238  if(subscribedItem == null){
239  subscribedItem = SessionManager.getSession().getEditedSubscription();
240  }
241 
242  SubscribedItem backup = null;
243  if (SessionManager.getSession().getFormBackup() != null) {
244  if (SessionManager.getSession().getFormBackup().getClass().getName().endsWith("SubscribedItem")) {
245  backup = (SubscribedItem) SessionManager.getSession().getFormBackup();
246  if (backup.getId() != subscribedItem.getId()) {
247  backup = null;
248  }
249  }
250  }
251 
252  SubscribedItem tempItem = null;
253 
254  userLogin = SessionManager.getSession().getSaveUserLogin();
255 
256  if(userLogin == null && backup != null) {
257  tempItem = backup;
258  } else if(userLogin == null && subscribedItem != null) {
259  tempItem = subscribedItem;
260  } else if(userLogin == null) {
261  userLogin = "";
262  return userLogin;
263  }
264 
265  if(tempItem != null)
266  {
267  if(tempItem.getUser() == null){
268  userLogin = "";
269  return userLogin;
270  }
271 
272  Object[] params = {"id",tempItem.getUser().getId()};
273  @SuppressWarnings("unchecked")
274  List<User> list = AppBean.getPersistenceManager().queryDB("User.findById",params);
275  if(list == null){
276  errorMessage = MessageProvider.getMessage("ssSubsEditUserNotFound");
277  return null;
278  }
279 
280  userLogin = list.get(0).getLogin();
281  }
282  return userLogin;
283  }
284 
285  /**
286  * Sets login of user that will be subscribed (or unsubscribed)
287  *
288  * @param userURI login of user that will be subscribed (or unsubscribed)
289  */
290  public void setUserLogin(String userURI){
291  this.userLogin = userURI;
292  }
293 
294  /**
295  * Gets object of edited subscription
296  *
297  * @return Returns object of edited subscription
298  */
300  return subscribedItem;
301  }
302 
303  /**
304  * Sets object of edited subscription
305  *
306  * @param subscribedItem object of edited subscription
307  */
309  this.subscribedItem = subscribedItem;
310  }
311 
312  /**
313  * Gets error message displayed in form
314  *
315  * @return Returns error message displayed in form
316  */
317  public String getErrorMessage() {
318  return errorMessage;
319  }
320 
321  /**
322  * Sets error message to be displayed in form
323  *
324  * @param errorMessage Error message to be displayed in form
325  */
326  public void setErrorMessage(String errorMessage) {
327  this.errorMessage = errorMessage;
328  }
329 
330  /**
331  * Action listener for save button in the page for editing of the Subscription
332  *
333  * @return Returns page outcome (identificator of next page or null to stay here)
334  */
335  public String btnSaveAction(){
336  boolean isErrorOccured = false;
337  errorMessage = "";
338 
339  if(!annotURI.isEmpty() && annotURI != null){
340  Object[] params = {"uri",annotURI};
341  @SuppressWarnings("unchecked")
342  List<AnnotType> list = AppBean.getPersistenceManager().queryDB("AnnotType.findByUri",params);
343  if(list == null || list.isEmpty()){
344  errorMessage += MessageProvider.getMessage("ssSubsCantFindType");
345  isErrorOccured = true;
346  }else{
347  subscribedItem.setAnnotationType(list.get(0));
348  }
349  }else{
350  subscribedItem.setAnnotationType(null);
351  }
352 
353  if(!userLogin.isEmpty() && userLogin != null){
354  Object[] params = {"login",userLogin};
355  @SuppressWarnings("unchecked")
356  List<User> list = AppBean.getPersistenceManager().queryDB("User.findByLogin",params);
357  if(list == null || list.isEmpty()){
358  errorMessage += MessageProvider.getMessage("ssSubsCantFindUser");
359  isErrorOccured = true;
360  }else{
361  subscribedItem.setUser(list.get(0));
362  }
363  }else{
364  subscribedItem.setUser(null);
365  }
366 
367  if(!sourceURI.isEmpty() && sourceURI != null){
368  subscribedItem.setSource(sourceURI);
369  }else{
370  subscribedItem.setSource(null);
371  }
372 
374  errorMessage += MessageProvider.getMessage("ssSubsTypeIncorrect");
375  isErrorOccured = true;
376  }else{
377  subscribedItem.setType(subscriptionType);
378  }
379 
380  if(isErrorOccured){
381  return null;
382  }
383 
384  subscribedItem.setType(subscriptionType);
385 
386 
387  editedStoryScope = SessionManager.getSession().getEditedStoryScope();
388  if(editedStoryScope == null){
389  errorMessage = MessageProvider.getMessage("ssSubsCantReachSS");
391  String msg = "Can't reach edited StoryScope.";
392  Logger.getLogger(EditSubscriptions.class.getName()).log(Level.SEVERE, msg);
393  }
394 
395  return null;
396  }
397 
398 
399  subscribedItem.setStoryScopeId(editedStoryScope.getId());
400 
402  if (saved == null) {
403  errorMessage = MessageProvider.getMessage("ssSubsEditDatabaseFailure");
405  String msg = "Persisting of changes in subscribed item for StoryScope account failed.";
406  Logger.getLogger(EditSubscriptions.class.getName()).log(Level.SEVERE, msg);
407  }
408  return null;
409  } else { // if changes was successfully saved
410  subscribedItem = saved;
411  }
412 
413  SessionManager.getSession().setAnnotationTypeAddress(null);
414  SessionManager.getSession().setSaveUserLogin(null);
415  SessionManager.getSession().setSaveSubscriptionType(null);
416  SessionManager.getSession().setSaveSourceUri(null);
417  SessionManager.getSession().setEditedSubscription(null);
418  return "subscribed";
419  }
420 
421  /**
422  * Action listener for save button on page for adding new Subscription
423  *
424  * @return Returns page outcome (identificator of next page or null to stay here)
425  */
426  public String btnNewAction(){
427  boolean isErrorOccured = false;
429  errorMessage = "";
430 
431  // test if uri of annotation type is fill
432  if(!annotURI.isEmpty() && annotURI != null){
433  // if URI si fill then try to find type in database
434  Object[] params = {"uri",annotURI};
435  @SuppressWarnings("unchecked")
436  List<AnnotType> list = AppBean.getPersistenceManager().queryDB("AnnotType.findByUri",params);
437  if(list == null || list.isEmpty()){
438  // type not found, print error
439  errorMessage += MessageProvider.getMessage("ssSubsCantFindType");
440  isErrorOccured = true;
441  }else{
442  //when is URI correct save it
443  subscribedItem.setAnnotationType(list.get(0));
444  }
445  }
446 
447  // test if user login is fill
448  if(!userLogin.isEmpty() && userLogin != null){
449  // if is login fill try to find user in database
450  Object[] params = {"login",userLogin};
451  @SuppressWarnings("unchecked")
452  List<User> list = AppBean.getPersistenceManager().queryDB("User.findByLogin",params);
453  if(list == null || list.isEmpty()){
454  // user not found, print error
455  errorMessage += MessageProvider.getMessage("ssSubsCantFindUser");
456  isErrorOccured = true;
457  }else{
458  //when is user correct save it
459  subscribedItem.setUser(list.get(0));
460  }
461  }
462 
463  // if source uri is fill
464  if(!sourceURI.isEmpty() && sourceURI != null){
465  // save source uri
466  subscribedItem.setSource(sourceURI);
467  }
468 
469  // subscription type must be fill and must have correct value
471  errorMessage += MessageProvider.getMessage("ssSubsTypeIncorrect");
472  isErrorOccured = true;
473  }else{
474  subscribedItem.setType(subscriptionType);
475  }
476 
477  if(isErrorOccured){
478  return null;
479  }
480 
481  subscribedItem.setType(subscriptionType);
482 
483  editedStoryScope = SessionManager.getSession().getEditedStoryScope();
484  if(editedStoryScope == null){
485  errorMessage = MessageProvider.getMessage("ssSubsCantReachSS");
487  String msg = "Can't reach edited StoryScope.";
488  Logger.getLogger(EditSubscriptions.class.getName()).log(Level.SEVERE, msg);
489  }
490  return null;
491  }
492 
493 
494  subscribedItem.setStoryScopeId(editedStoryScope.getId());
495  //try to save into DB
496  if(AppBean.getPersistenceManager().persistEntity(subscribedItem)){
497  errorMessage = MessageProvider.getMessage("ssSubsAddDatabaseFailure");
499  String msg = "Persisting of new subscription for StoryScope account failed.";
500  Logger.getLogger(EditSubscriptions.class.getName()).log(Level.SEVERE, msg);
501  }
502  return null;
503  }
504 
505  SessionManager.getSession().setAnnotationTypeAddress(null);
506  SessionManager.getSession().setSaveUserLogin(null);
507  SessionManager.getSession().setSaveSubscriptionType(null);
508  SessionManager.getSession().setSaveSourceUri(null);
509  SessionManager.getSession().setEditedSubscription(null);
510  return "subscribed";
511  }
512 
513  /**
514  * Action listener for delete button on deleting page.
515  *
516  * @return Returns page outcome (identificator of next page or null to stay here)
517  */
518  public String btnDeleteAction(){
519  if(AppBean.getPersistenceManager().removeEntity(subscribedItem)){
520  errorMessage = MessageProvider.getMessage("ssSubsDeleteDatabaseFailure");
522  String msg = "DB failure during deleting of Subscribtion form StoryScope account.";
523  Logger.getLogger(EditSubscriptions.class.getName()).log(Level.SEVERE, msg);
524  }
525 
526  return null;
527  }
528 
529  SessionManager.getSession().setAnnotationTypeAddress(null);
530  SessionManager.getSession().setSaveUserLogin(null);
531  SessionManager.getSession().setSaveSubscriptionType(null);
532  SessionManager.getSession().setSaveSourceUri(null);
533  SessionManager.getSession().setFormBackup(null);
534  SessionManager.getSession().setEditedSubscription(null); // clean up variable in session
535  return "subscribed";
536  }
537 
538  /**
539  * Action listener for select button
540  *
541  * @return Returns page outcome
542  */
543  public String btnSelectAction() {
544  SessionManager.getSession().setAnnotationTypeAddress(null);
545  if(subscribedItem == null) SessionManager.getSession().setComeFrom("editSubscription");
546  else SessionManager.getSession().setComeFrom("addSubscription");
547  SessionManager.getSession().setSaveUserLogin(userLogin);
548  SessionManager.getSession().setSaveSubscriptionType(subscriptionType);
549  SessionManager.getSession().setSaveSourceUri(sourceURI);
550  return "typeSelect";
551  }
552 
553  /**
554  * Action listener for cancel button
555  *
556  * @return Returns page outcome
557  */
558  public String btnCancelAction() {
559  SessionManager.getSession().setAnnotationTypeAddress(null);
560  SessionManager.getSession().setSaveUserLogin(null);
561  SessionManager.getSession().setSaveSubscriptionType(null);
562  SessionManager.getSession().setSaveSourceUri(null);
563  SessionManager.getSession().setFormBackup(null);
564  SessionManager.getSession().setEditedSubscription(null); // clean up variable in session
565  return "subscribed";
566  }
567 }
Class for manipulating with session.
Class representing StoryScope for needs of SEC Interface.
Definition: StoryScope.java:48
Singleton for storing global variables.
Definition: AppBean.java:47
Class representing type of annotation.
Definition: AnnotType.java:58
Class representing item of subscripted or unsubscripted sources list.
Class representing user.
Definition: User.java:51
Backbean for pages for adding, editing and deleting of StoryScope's subscriptions.
void setSubscribedItem(SubscribedItem subscribedItem)