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