4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
EditATA.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: EditATA.java
5  * Description: Backbean for pages for adding and editing attributes of of types
6  * of annotations
7  */
8 
9 /**
10  * @file EditATA.java
11  *
12  * @brief Backbean for managing attributes of types of annotations
13  */
14 package cz.vutbr.fit.knot.annotations.web;
15 
25 import java.io.Serializable;
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31 import javax.faces.bean.ManagedBean;
32 import javax.faces.bean.ViewScoped;
33 import javax.faces.model.SelectItem;
34 import javax.persistence.EntityManager;
35 import javax.persistence.EntityTransaction;
36 
37 /**
38  * Backbean for pages for adding and editing attributes of of types of annotations
39  *
40  * @brief Backbean for managing attributes of types of annotations
41  * @author idytrych
42  */
43 @ManagedBean
44 @ViewScoped
45 public class EditATA implements Serializable {
46  /**
47  * Value of name field in form
48  */
49  private String name = null;
50  /**
51  * Edited attribute of type of annotation
52  */
53  private AnnotTypeAttr annotTAttr = null;
54  /**
55  * Value of simple type field in form
56  */
57  private String simpleType = null;
58  /**
59  * Value of structured type field in form
60  */
61  private String structuredType = null;
62  /**
63  * Value of comment field in form
64  */
65  private String comment = null;
66  /**
67  * Value of required checkbox
68  */
69  private Boolean required = null;
70  /**
71  * Value of uri field in form
72  */
73  private String uriInOntology = null;
74  /**
75  * Error message displayed in form
76  */
77  private String errorMessage = "";
78  /**
79  * Differ between page for adding and between page for editing attribute
80  */
81  private boolean adding = false;
82 
83  /**
84  * Action listener for save button in the page for editing of the attribute of type of annotation
85  *
86  * @return Returns page outcome (identificator of next page or null to stay here)
87  */
88  public String btnSaveAction() {
89 
90  AnnotType sType = null;
91 
92  if (name == null || name.isEmpty()) {
93  errorMessage = MessageProvider.getMessage("nameCantBeEmpty");
94  return null;
95  }
96 
97  if (simpleType != null && !simpleType.isEmpty()
98  && structuredType != null && !structuredType.isEmpty()) {
99  errorMessage = MessageProvider.getMessage("simpleAndStructuredError");
100  return null;
101  } else if (structuredType != null && !structuredType.isEmpty()) {
102  String uri = AppBean.getBaseTypeUri() + "g"
103  + annotTAttr.getAnnotationType().getGroup().getId()
104  + "/" + MessageProcessor.replaceArrows(structuredType);
105  Object[] params = new Object[2];
106  params[0] = "uri";
107  params[1] = uri;
108  List tList = AppBean.getPersistenceManager().queryDB("AnnotType.findByUri", params);
109  if (tList != null && !tList.isEmpty()) {
110  sType = (AnnotType) tList.get(0);
111  }
112  if (sType == null) {
113  errorMessage = MessageProvider.getMessage("unknownStructuredType");
114  return null;
115  }
116  simpleType = null;
117  } else if (simpleType != null && !simpleType.isEmpty()) {
118  if (!Constants.SIMPLE_TYPES.contains(simpleType)) {
119  errorMessage = MessageProvider.getMessage("unknownSimpleType");
120  return null;
121  }
122  } else {
123  errorMessage = MessageProvider.getMessage("typeMustBeSet");
124  return null;
125  }
126 
127  // set new values
128  annotTAttr.setName(name);
129  annotTAttr.setSimpleType(simpleType);
130  annotTAttr.setAttributeType(sType);
131  annotTAttr.setComment(comment);
132  annotTAttr.setRequired(required);
133  annotTAttr.setUriInOntology(uriInOntology);
134 
135  // persist changes
136  AnnotTypeAttr saved = (AnnotTypeAttr) AppBean.getPersistenceManager().saveEntityChanges(annotTAttr);
137  if (saved == null) {
138  errorMessage = MessageProvider.getMessage("changesNSDatabaseFailure");
140  String msg = "Persisting changes in attribute of type of annotation failed.";
141  Logger.getLogger(EditATA.class.getName()).log(Level.SEVERE, msg);
142  }
143  return null;
144  } else { // if changes was successfully saved
145  annotTAttr = saved;
146  }
147 
148  // prepare messages for connected clients
149  AnnotType editedAT = SessionManager.getSession().getEditedAT();
150  ResponseCreator rc = AppBean.getResponseCreator();
151  synchronized(AppBean.getSessions()){
152  Iterator<EditorSession> sesIt = AppBean.getSessions().iterator();
153  while (sesIt.hasNext()) { // for each client
154  EditorSession es = sesIt.next();
155  Flier flier = new Flier();
156  flier.AddEditedType(editedAT); // create list of changed types
157  es.addMessageTS(rc.createCometResponse(flier, es)); // create message
158  }
159  }
160 
161  SessionManager.getSession().setSecondFormBackup(null);
162  SessionManager.getSession().setEditedATA(null);
163  return "editAT";
164  } // btnSaveAction()
165 
166  /**
167  * Action listener for save button in the page for adding of the attribute of type of annotation
168  *
169  * @return Returns page outcome (identificator of next page or null to stay here)
170  */
171  public String btnSaveNewAction() {
172 
173  AnnotType editedAT = SessionManager.getSession().getEditedAT();
174 
175  if (name == null || name.isEmpty()) {
176  errorMessage = MessageProvider.getMessage("nameCantBeEmpty");
177  return null;
178  }
179 
180  AnnotType sType = null;
181 
182  if (simpleType != null && !simpleType.isEmpty()
183  && structuredType != null && !structuredType.isEmpty()) {
184  errorMessage = MessageProvider.getMessage("simpleAndStructuredError");
185  return null;
186  } else if (structuredType != null && !structuredType.isEmpty()) {
187  String uri = AppBean.getBaseTypeUri() + "g"
188  + editedAT.getGroup().getId()
190  Object[] params = new Object[2];
191  params[0] = "uri";
192  params[1] = uri;
193  List tList = AppBean.getPersistenceManager().queryDB("AnnotType.findByUri", params);
194  if (tList != null && !tList.isEmpty()) {
195  sType = (AnnotType) tList.get(0);
196  }
197  if (sType == null) {
198  errorMessage = MessageProvider.getMessage("unknownStructuredType");
199  return null;
200  }
201  simpleType = null;
202  } else if (simpleType != null && !simpleType.isEmpty()) {
203  if (!Constants.SIMPLE_TYPES.contains(simpleType)) {
204  errorMessage = MessageProvider.getMessage("unknownSimpleType");
205  return null;
206  }
207  } else {
208  errorMessage = MessageProvider.getMessage("typeMustBeSet");
209  return null;
210  }
211 
212  // create new attribute
213  annotTAttr = new AnnotTypeAttr(editedAT, name, simpleType, required);
214  annotTAttr.setAttributeType(sType);
215  annotTAttr.setComment(comment);
216  annotTAttr.setUriInOntology(uriInOntology);
217 
218  // persist new attribute
219  try {
220  EntityManager em = AppBean.getPersistenceManager().getEM();
221  EntityTransaction transaction = em.getTransaction();
222  transaction.begin();
223  editedAT = em.merge(editedAT);
224  annotTAttr.setAnnotationType(editedAT);
225  editedAT.getAttributes().add(annotTAttr);
226  SessionManager.getSession().setEditedAT(editedAT);
227  em.flush();
228  transaction.commit();
229  } catch (Exception e) {
230  errorMessage = MessageProvider.getMessage("newAttrNSDatabaseFailure");
232  String msg = "Persisting new attribute of type of annotation failed.";
233  Logger.getLogger(EditATA.class.getName()).log(Level.SEVERE, msg, e);
234  }
235  return null;
236  }
237 
238  // prepare messages for connected clients
239  ResponseCreator rc = AppBean.getResponseCreator();
240  synchronized(AppBean.getSessions()){
241  Iterator<EditorSession> sesIt = AppBean.getSessions().iterator();
242  while (sesIt.hasNext()) { // for each client
243  EditorSession es = sesIt.next();
244  Flier flier = new Flier();
245  flier.AddEditedType(editedAT); // create list of changed types
246  es.addMessageTS(rc.createCometResponse(flier, es)); // create message
247  }
248  }
249 
250  SessionManager.getSession().setSecondFormBackup(null);
251  SessionManager.getSession().setEditedATA(null);
252  return "editAT";
253  } // btnSaveNewAction()
254 
255  /**
256  * Creates backup of form data
257  *
258  * @return Returns backup of form data
259  */
261 
262  AnnotType editedAT = SessionManager.getSession().getEditedAT();
263 
264  AnnotType sType = null;
265 
266  if (structuredType != null && !structuredType.isEmpty()) {
267  String uri = AppBean.getBaseTypeUri() + "g"
268  + editedAT.getGroup().getId()
270  Object[] params = new Object[2];
271  params[0] = "uri";
272  params[1] = uri;
273  List tList = AppBean.getPersistenceManager().queryDB("AnnotType.findByUri", params);
274  if (tList != null && !tList.isEmpty()) {
275  sType = (AnnotType) tList.get(0);
276  }
277  }
278  if (simpleType != null && !simpleType.isEmpty()) {
279  if (!Constants.SIMPLE_TYPES.contains(simpleType)) {
280  simpleType = null;
281  }
282  }
283 
284  // create backup attribute
285  AnnotTypeAttr backup = new AnnotTypeAttr(editedAT, name, simpleType, required);
286  backup.setAttributeType(sType);
287  backup.setComment(comment);
288  backup.setUriInOntology(uriInOntology);
289  return backup;
290  } // createBackup()
291 
292  /**
293  * Action listener for cancel button
294  *
295  * @return Returns page outcome (identificator of next page or null to stay here)
296  */
297  public String btnCancelAction() {
298  SessionManager.getSession().setSecondFormBackup(null);
299  SessionManager.getSession().setEditedATA(null); // clean up variable in session
300  return "editAT";
301  }
302 
303  /**
304  * Gets value of name field in form
305  *
306  * @return Returns value of name field in form
307  */
308  public String getName() {
309  AnnotTypeAttr backup = getBackup();
310  if (name == null && backup != null) {
311  name = backup.getName();
312  } else if (name == null && annotTAttr != null) {
313  name = annotTAttr.getName();
314  } else if (name == null) {
315  name = "";
316  }
317  return name;
318  } // getName()
319 
320  /**
321  * Sets value of name field in form
322  *
323  * @param name Value of name field in form
324  */
325  public void setName(String name) {
326  this.name = name;
327  }
328 
329  /**
330  * Gets value of simple type field in form
331  *
332  * @return Returns value of simple type field in form
333  */
334  public String getSimpleType() {
335  AnnotTypeAttr backup = getBackup();
336  if (simpleType == null && backup != null) {
337  simpleType = backup.getSimpleType();
338  } else if (simpleType == null && annotTAttr != null) {
339  simpleType = annotTAttr.getSimpleType();
340  } else if (simpleType == null) {
341  simpleType = "";
342  }
343  if (simpleType != null) {
344  if (simpleType.equals("geoPoint")) {
345  simpleType = "GeoPoint";
346  }
347  if (simpleType.equals("person")) {
348  simpleType = "Person";
349  }
350  } else {
351  simpleType = "";
352  }
353  return simpleType;
354  } // getSimpleType()
355 
356  /**
357  * Sets value of simple type field in form
358  *
359  * @param simpleType Value of simple type field in form
360  */
361  public void setSimpleType(String simpleType) {
362  this.simpleType = simpleType;
363  }
364 
365  /**
366  * Gets value of structured type field in form
367  *
368  * @return Returns value of structured type field in form
369  */
370  public String getStructuredType() {
371  AnnotTypeAttr backup = getBackup();
372  if (structuredType == null && backup != null) {
373  structuredType = backup.getAttributeType().getLinearizedName();
374  } else if (structuredType == null && annotTAttr != null) {
375  if (annotTAttr.getAttributeType() != null) {
376  structuredType = annotTAttr.getAttributeType().getLinearizedName();
377  }
378  } else if (structuredType == null) {
379  structuredType = "";
380  }
381  return structuredType;
382  } // getStructuredType()
383 
384  /**
385  * Sets value of structured type field in form
386  *
387  * @param structuredType Value of structured type field in form
388  */
389  public void setStructuredType(String structuredType) {
390  this.structuredType = structuredType;
391  }
392 
393  /**
394  * Gets value of comment field in form
395  *
396  * @return Returns value of comment field in form
397  */
398  public String getComment() {
399  AnnotTypeAttr backup = getBackup();
400  if (comment == null && backup != null) {
401  comment = backup.getComment();
402  } else if (comment == null && annotTAttr != null) {
403  comment = annotTAttr.getComment();
404  } else if (comment == null) {
405  comment = "";
406  }
407  return comment;
408  } // getComment()
409 
410  /**
411  * Sets value of comment field in form
412  *
413  * @param comment Value of comment field in form
414  */
415  public void setComment(String comment) {
416  this.comment = comment;
417  }
418 
419  /**
420  * Gets value of required checkbox
421  *
422  * @return Returns value of required checkbox
423  */
424  public Boolean getRequired() {
425  AnnotTypeAttr backup = getBackup();
426  if (required == null && backup != null) {
427  required = backup.getRequired();
428  } else if (required == null && annotTAttr != null) {
429  required = annotTAttr.getRequired();
430  } else if (required == null) {
431  required = false;
432  }
433  return required;
434  }
435 
436  /**
437  * Sets value of required checkbox
438  *
439  * @param required Value of required checkbox
440  */
441  public void setRequired(Boolean required) {
442  this.required = required;
443  }
444 
445  /**
446  * Gets value of uri field in form
447  *
448  * @return Returns value of uri field in form
449  */
450  public String getUriInOntology() {
451  AnnotTypeAttr backup = getBackup();
452  if (uriInOntology == null && backup != null) {
453  uriInOntology = backup.getUriInOntology();
454  } else if (uriInOntology == null && annotTAttr != null) {
455  uriInOntology = annotTAttr.getUriInOntology();
456  } else if (uriInOntology == null) {
457  uriInOntology = "";
458  }
459  return uriInOntology;
460  }
461 
462  /**
463  * Sets value of uri field in form
464  * @param uriInOntology Value of uri field in form
465  */
466  public void setUriInOntology(String uriInOntology) {
467  this.uriInOntology = uriInOntology;
468  }
469 
470  /**
471  * Action listener for button for selecting structured type of attribute
472  *
473  * @return Returns page outcome (identificator of next page)
474  */
475  public String btnSelectSTypeAction() {
476  AnnotType editedAT = SessionManager.getSession().getEditedAT();
477  AnnotTypeAttr backup = createBackup();
478  SessionManager.getSession().setSecondFormBackup(backup);
479  SessionManager.getSession().setSelectedUG(editedAT.getGroup());
480  SessionManager.getSession().setEditedATA(annotTAttr);
481  SessionManager.getSession().setCameFrom("editATA");
482  return "selectAT";
483  }
484 
485  /**
486  * Action listener for button for selecting structured type of attribute
487  * (used in page for adding attribute of annotation)
488  *
489  * @return Returns page outcome (identificator of next page)
490  */
491  public String btnSelectSTypeActionN() {
492  AnnotType editedAT = SessionManager.getSession().getEditedAT();
493  AnnotTypeAttr backup = createBackup();
494  SessionManager.getSession().setSecondFormBackup(backup);
495  SessionManager.getSession().setSelectedUG(editedAT.getGroup());
496  SessionManager.getSession().setEditedATA(annotTAttr);
497  SessionManager.getSession().setCameFrom("addATA");
498  return "selectAT";
499  }
500 
501  /**
502  * Gets backup of form data
503  * If user came from another page, gets edited attribute of type of annotation
504  * from session.
505  * If user now returned from selection of type, set form field value.
506  *
507  * @return If backup is available, returns backup of form data, null otherwise
508  */
510  if (annotTAttr == null) { // user is camming to this page
511  annotTAttr = SessionManager.getSession().getEditedATA();
512  AnnotType sAT = SessionManager.getSession().getSelectedAT();
513  if (sAT != null && SessionManager.getSession().getCameFrom().equals("editATA")) {
514  structuredType = sAT.getLinearizedName();
515  SessionManager.getSession().setSelectedAT(null);
516  SessionManager.getSession().setCameFrom(null);
517  } else if (sAT != null && SessionManager.getSession().getCameFrom().equals("addATA")) {
518  structuredType = sAT.getLinearizedName();
519  SessionManager.getSession().setSelectedAT(null);
520  SessionManager.getSession().setCameFrom(null);
521  adding = true;
522  }
523  }
524  AnnotTypeAttr backup = null;
525  if (SessionManager.getSession().getSecondFormBackup() != null) {
526  if (SessionManager.getSession().getSecondFormBackup().getClass().getName().endsWith("AnnotTypeAttr")) {
527  backup = (AnnotTypeAttr) SessionManager.getSession().getSecondFormBackup();
528  if (!adding) { // it is not adding of attribute
529  if (backup.getId() != annotTAttr.getId()) {
530  backup = null;
531  }
532  }
533  }
534  }
535  return backup;
536  } // getBackup()
537 
538  /**
539  * Gets error message displayed in form
540  *
541  * @return Returns error message displayed in form
542  */
543  public String getErrorMessage() {
544  return errorMessage;
545  }
546 
547  /**
548  * Sets error message to be displayed in form
549  *
550  * @param errorMessage Error message to be displayed in form
551  */
552  public void setErrorMessage(String errorMessage) {
553  this.errorMessage = errorMessage;
554  }
555 
556  /**
557  * Gets edited attribute of type of annotation
558  *
559  * @return Returns edited attribute of type of annotation
560  */
562  getBackup();
563  return annotTAttr;
564  }
565 
566  /**
567  * Gets user group of edited type of annotation
568  *
569  * @return User group of edited type of annotation
570  */
571  public UserGroup getGroup() {
572  if (annotTAttr != null) {
573  return annotTAttr.getAnnotationType().getGroup();
574  }
575  return SessionManager.getSession().getEditedAT().getGroup();
576  } // getGroup()
577 
578  /**
579  * Gets simple types of attribute
580  *
581  * @return Returns simple types of attribute
582  */
583  public List<SelectItem> getSimpleTypes() {
584  List<SelectItem> rList = new ArrayList<SelectItem>();
585  for (Iterator<String> tIt = Constants.SIMPLE_TYPES.iterator(); tIt.hasNext();) {
586  String t = tIt.next();
587  if (t.equals("person") || t.equals("geoPoint")) { // skip compatibility variants
588  continue;
589  }
590  rList.add(new SelectItem(t, t));
591  }
592  rList.add(new SelectItem("", ""));
593  return rList;
594  } // getSimpleTypes()
595 
596 } // public class EditATA
Class representing attribute of type of annotation.
Class for manipulating with session.
void setUriInOntology(String uriInOntology)
Definition: EditATA.java:466
Singleton for storing global variables.
Definition: AppBean.java:47
void setRequired(Boolean required)
Definition: EditATA.java:441
void setStructuredType(String structuredType)
Definition: EditATA.java:389
void setErrorMessage(String errorMessage)
Definition: EditATA.java:552
static final ArrayList< String > SIMPLE_TYPES
Definition: Constants.java:153
Static class which parses and process XML with messages.
Class representing user group.
Definition: UserGroup.java:47
void setSimpleType(String simpleType)
Definition: EditATA.java:361
Class representing type of annotation.
Definition: AnnotType.java:58
Flier with informations for comet handlers.
Definition: Flier.java:31
Class that creates responses and persists data.
Backbean for managing attributes of types of annotations.
Definition: EditATA.java:45
Informations about client session.