4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
EditAT.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: EditAT.java
5  * Description: Backbean for pages for adding, editing and deleting of types of annotations
6  */
7 
8 /**
9  * @file EditAT.java
10  *
11  * @brief Backbean for adding, editing and deleting of types of annotations
12  */
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.component.html.HtmlDataTable;
34 import javax.persistence.EntityManager;
35 import javax.persistence.EntityTransaction;
36 import javax.persistence.Query;
37 
38 /**
39  * Backbean for pages for adding, editing and deleting of types of annotations
40  *
41  * @brief Backbean for adding, editing and deleting of types of annotations
42  * @author idytrych
43  */
44 @ManagedBean
45 @ViewScoped
46 public class EditAT implements Serializable {
47  /**
48  * Table with attributes of given type of annotation
49  */
50  private HtmlDataTable listTable;
51 
52  /**
53  * Table with ancestors of given type of annotation
54  */
55  private HtmlDataTable ancTable;
56 
57  /**
58  * Attribute selected in the table
59  */
61 
62  /**
63  * Value of primary ancestor field in form
64  */
65  private String prAncStr;
66 
67  /**
68  * Ancestor selected in the table
69  */
70  private AnnotType anc;
71 
72  /**
73  * Edited type of annotation or type of annotation to be deleted
74  */
75  private AnnotType annotType = null;
76 
77  /**
78  * Value of name field in form
79  */
80  private String name = null;
81 
82  /**
83  * Value of group field in form
84  */
85  private String group = null;
86 
87  /**
88  * Value of comment field in form
89  */
90  private String comment = null;
91 
92  /**
93  * Value of restricted attributes field in form
94  */
95  private Boolean restrictedAtt = null;
96 
97  /**
98  * Value of URI field in form
99  */
100  private String uriInOntology = null;
101 
102  /**
103  * Error message displayed in form
104  */
105  private String errorMessage = "";
106 
107  /**
108  * Action listener for save button in the page for editing of the type of annotation
109  *
110  * @return Returns page outcome (identificator of next page or null to stay here)
111  */
112  public String btnSaveAction() {
113 
114  annotType.setComment(comment); // set new comment
115  annotType.setRestrictedAtt(restrictedAtt);
116  annotType.setUriInOntology(uriInOntology);
117 
118  if (name.isEmpty()) {
119  errorMessage = MessageProvider.getMessage("nameCantBeEmpty");
120  return null;
121  }
122 
123  List<AnnotType> affectedTypes = new ArrayList<AnnotType>();
124 
125  affectedTypes.add(annotType); // type will be affected by change
126 
127  boolean ancestorChange = false;
128  boolean nameChange = false;
129 
130  AnnotType pa = null;
131  if (prAncStr != null && !prAncStr.isEmpty()) {
132  String uri = AppBean.getBaseTypeUri() + "g" + annotType.getGroup().getId()
134  Object[] params = new Object[2];
135  params[0] = "uri";
136  params[1] = uri;
137  List pList = AppBean.getPersistenceManager().queryDB("AnnotType.findByUri", params);
138  if (pList != null && !pList.isEmpty()) {
139  pa = (AnnotType) pList.get(0);
140  }
141  if (pa == null) {
142  errorMessage = MessageProvider.getMessage("errorUnknownAnc");
143  return null;
144  }
145  }
146 
147  if (annotType.getAncestorType() == null) { // if type haven't ancestor
148  if (!prAncStr.isEmpty()) {
149  ancestorChange = true;
150  }
151  } else { // if type have ancestor
152  if (!annotType.getAncestorType().getLinearizedName().equals(prAncStr)) {
153  ancestorChange = true;
154  }
155  }
156 
157  if (!annotType.getName().equals(name)) {
158  nameChange = true;
159  }
160 
161  // backup informations
162  String backupN = annotType.getName();
163  AnnotType backupT = annotType.getAncestorType();
164 
165  if (nameChange) { // if name should be changed
166  // check, whether name can be changed
167  Object[] params = new Object[2]; // parameters for database query
168  params[0] = "type";
169  params[1] = annotType.getId();
170  List aList = AppBean.getPersistenceManager().queryDB("Annotation.findByType", params);
171  if (aList != null && !aList.isEmpty()) { // if some annotations found
172  errorMessage = errorMessage + MessageProvider.getMessage("ifAnnotExistsNameCantBeChanged");
173  return null; // name can't be changed
174  }
175 
176  // check, whether new name is unique
177  annotType.setName(name); // set new name
178  params[0] = "uri";
179  params[1] = annotType.getGeneratedURI();
180  List duplicit = AppBean.getPersistenceManager().queryDB("AnnotType.findByUri", params);
181  if (duplicit != null && !duplicit.isEmpty()) {
182  errorMessage = MessageProvider.getMessage("newNameAlreadyExists");
183  annotType.setName(backupN);
184  return null;
185  }
186  } // if name should be changed
187 
188  if (ancestorChange) { // if ancestor should be changed
189  annotType.setAncestorType(pa); // set new primary ancestor
190  Object[] params = new Object[2]; // parameters for database query
191  params[0] = "uri";
192  params[1] = annotType.getGeneratedURI();
193  List duplicit = AppBean.getPersistenceManager().queryDB("AnnotType.findByUri", params);
194  if (duplicit != null && !duplicit.isEmpty()) {
195  errorMessage = MessageProvider.getMessage("newNameAlreadyExistsOnPos");
196  annotType.setAncestorType(backupT);
197  annotType.setName(backupN);
198  return null;
199  }
200  } // if ancestor should be changed
201 
202  if (ancestorChange || nameChange) { // if name or ancestor should be changed
203  boolean someChange = true;
204  while (someChange) { // while some types added
205  someChange = false;
206  List<AnnotType> nowAdd = new ArrayList<AnnotType>();
207  for (Iterator<AnnotType> afIt = affectedTypes.iterator(); afIt.hasNext();) { // for all affected
208  AnnotType afT = afIt.next();
209  // query DB for types which primary ancestor is actual type
210  Object[] params = new Object[2]; // parameters for database query
211  params[0] = "ancestor";
212  params[1] = afT.getId();
213 
214  @SuppressWarnings("unchecked")
215  List<AnnotType> tList = AppBean.getPersistenceManager().queryDB("AnnotType.findByAncestor", params);
216  if (tList != null && !tList.isEmpty()) { // if some type found
217  for (Iterator<AnnotType> tIt = tList.iterator(); tIt.hasNext();) {
218  AnnotType t = tIt.next();
219  if (!affectedTypes.contains(t) && !nowAdd.contains(t)) {
220  nowAdd.add(t); // type will be affected by change of ancestor
221  someChange = true;
222  }
223  }
224  } // if some type found
225  } // for all affected
226  affectedTypes.addAll(nowAdd);
227  } // while some types added
228  } // if name or ancestor should be changed
229 
230  // check whether ancestor can be changed
231  if (ancestorChange) { // if ancestor was changed
232  for (Iterator<AnnotType> afTIt = affectedTypes.iterator(); afTIt.hasNext();) {
233  AnnotType afTT = afTIt.next();
234  Object[] params = new Object[2]; // parameters for database query
235  params[0] = "type";
236  params[1] = afTT.getId();
237  List aList = AppBean.getPersistenceManager().queryDB("Annotation.findByType", params);
238  if (aList != null && !aList.isEmpty()) { // if some annotations found
239  errorMessage = errorMessage + MessageProvider.getMessage("ifAnnotExistsAncCantBeChanged");
240  annotType.setName(backupN);
241  annotType.setAncestorType(backupT);
242  return null; // name can't be changed
243  }
244  }
245  } // if ancestor was changed
246 
247  // persist changes
248  EntityManager em = AppBean.getPersistenceManager().getEM();
249  EntityTransaction transaction = em.getTransaction();
250  boolean errorOccurred = false;
251  try {
252  transaction.begin();
253 
254  // update all affected
255  for (Iterator<AnnotType> aTIt = affectedTypes.iterator(); aTIt.hasNext();) { // for each type
256  AnnotType aType = aTIt.next();
257  AnnotType sType = queryType(aType.getUri(), em); // get stored variant of type
258  if (aType == annotType) {
259  sType.setName(annotType.getName()); // update name
260  sType.setComment(annotType.getComment());
261  sType.setRestrictedAtt(annotType.getRestrictedAtt());
262  sType.setUriInOntology(annotType.getUriInOntology());
263  if (ancestorChange) { // if ancestor was changed
264  AnnotType oAType = null;
265  if (backupT != null) { // if old ancestor exists, get it
266  oAType = queryType(backupT.getUri(), em);
267  }
268  if (annotType.getAncestorType() == null) { // if new ancestor is null
269  sType.setAncestorType(null);
270  if (oAType != null) {
271  sType.getAncestorTypes().remove(oAType); // remove old ancestor from list
272  }
273  } else { // if new ancestor is not null
274  AnnotType nType = queryType(annotType.getAncestorType().getUri(), em);
275  sType.setAncestorType(nType);
276  if (!sType.getAncestorTypes().contains(nType)) {
277  sType.getAncestorTypes().add(nType); // add new ancestor to list
278  }
279  }
280  } // if ancestor was changed
281  annotType = sType;
282  }
283  sType.setUri(sType.getGeneratedURI()); // update URI
284  } // for each type
285 
286  em.flush();
287  transaction.commit();
288  } catch (Exception e) {
289  transaction.rollback();
290  errorMessage = MessageProvider.getMessage("changesNSDatabaseFailure");
292  String msg = "Saving of changes in type of annotation failed.";
293  Logger.getLogger(EditAT.class.getName()).log(Level.SEVERE, msg, e);
294  }
295  return null;
296  } finally {
297  em.close();
298  }
299 
300  // prepare messages for connected clients
301  ResponseCreator rc = AppBean.getResponseCreator();
302  synchronized(AppBean.getSessions()){
303  Iterator<EditorSession> sesIt = AppBean.getSessions().iterator();
304  while (sesIt.hasNext()) { // for each client
305  EditorSession es = sesIt.next();
306  Flier flier = new Flier();
307  // create list of changed types
308  for (Iterator<AnnotType> atIt = affectedTypes.iterator(); atIt.hasNext();) {
309  AnnotType at = atIt.next();
310  flier.AddEditedType(at);
311  }
312  es.addMessageTS(rc.createCometResponse(flier, es)); // create message
313  }
314  }
315 
316  SessionManager.getSession().setFormBackup(null);
317  SessionManager.getSession().setEditedAT(null);
318  return "aTypes";
319  } // btnSaveAction()
320 
321  /**
322  * Query database for type of annotation
323  *
324  * @param uri URI of type of annotation
325  * @param em Entity manager
326  * @return Returns type of annotation with given URI
327  * @throws RuntimeException If type of annotation was not found, throws exception
328  */
329  private static AnnotType queryType(String uri, EntityManager em) throws RuntimeException {
330  AnnotType annotType = null;
331  Object[] params = new Object[2];
332  params[0] = "uri";
333  params[1] = uri;
334  Query q = em.createNamedQuery("AnnotType.findByUri");
335  for (int p = 0; p < params.length; p = p + 2) {
336  q.setParameter((String) params[p], params[p + 1]);
337  }
338  List aList = q.getResultList();
339  if (aList != null && !aList.isEmpty()) { // if type was found
340  annotType = (AnnotType) aList.get(0);
341  } else { // if type was not found (merge failed)
342  throw new RuntimeException("Type not found!");
343  }
344  return annotType;
345  } // queryType()
346 
347  /**
348  * Action listener for cancel button
349  *
350  * @return Returns page outcome (identificator of next page or null to stay here)
351  */
352  public String btnCancelAction() {
353  SessionManager.getSession().setFormBackup(null);
354  SessionManager.getSession().setEditedAT(null); // clean up variable in session
355  return "aTypes";
356  }
357 
358 
359  /**
360  * Action listener for link for remove attribute from edited type of annotation
361  *
362  * @param id Id of removed attribute
363  * @return Returns null to stay in this page
364  */
365  public String actionRemoveAttr(Integer id) {
366  AnnotTypeAttr refAt = new AnnotTypeAttr(id);
367  int index = getAttrList().indexOf(refAt);
368  attr = getAttrList().get(index);
369  try {
370  EntityManager em = AppBean.getPersistenceManager().getEM();
371  EntityTransaction transaction = em.getTransaction();
372  transaction.begin();
373  attr = em.merge(attr);
374  annotType = em.merge(annotType);
375  annotType.getAttributes().remove(attr);
376  em.remove(attr);
377  em.flush();
378  transaction.commit();
379  } catch (Exception e) {
380  errorMessage = MessageProvider.getMessage("attrCantBeRemovedDF");
382  String msg = "Removing of attribute from type of annotation failed.";
383  Logger.getLogger(EditAT.class.getName()).log(Level.SEVERE, msg, e);
384  }
385  return null;
386  }
387 
388  // prepare messages for connected clients
389  ResponseCreator rc = AppBean.getResponseCreator();
390  synchronized(AppBean.getSessions()){
391  Iterator<EditorSession> sesIt = AppBean.getSessions().iterator();
392  while (sesIt.hasNext()) { // for each client
393  EditorSession es = sesIt.next();
394  Flier flier = new Flier();
395  flier.AddEditedType(annotType); // create list of changed types
396  es.addMessageTS(rc.createCometResponse(flier, es)); // create message
397  }
398  }
399 
400  return null;
401  } // actionRemoveAttr()
402 
403  /**
404  * Action listener for link for edit attribute
405  *
406  * @param id Id of edited attribute
407  * @return Returns page outcome (identificator of transition)
408  */
409  public String actionEditAttr(Integer id) {
410  AnnotTypeAttr refAt = new AnnotTypeAttr(id);
411  int index = getAttrList().indexOf(refAt);
412  attr = getAttrList().get(index);
413 
414  AnnotType backup = createBackup();
415  SessionManager.getSession().setFormBackup(backup);
416  SessionManager.getSession().setEditedAT(annotType);
417  SessionManager.getSession().setEditedATA(attr);
418  return "editATA";
419  } // actionEditAttr()
420 
421  /**
422  * Action listener for link for remove ancestor from edited type of annotation
423  *
424  * @param id Id of removed attribute
425  * @return Returns null to stay in this page
426  */
427  public String actionRemoveAnc(Integer id) {
428  AnnotType refAT = new AnnotType(id);
429  int index = getAncList().indexOf(refAT);
430  anc = getAncList().get(index);
431  try {
432  EntityManager em = AppBean.getPersistenceManager().getEM();
433  EntityTransaction transaction = em.getTransaction();
434  transaction.begin();
435  anc = em.merge(anc);
436  annotType = em.merge(annotType);
437  annotType.getAncestorTypes().remove(anc);
438  em.flush();
439  transaction.commit();
440  } catch (Exception e) {
441  errorMessage = MessageProvider.getMessage("ancCantBeRemovedDF");
443  String msg = "Removing of ancestor from type of annotation failed.";
444  Logger.getLogger(EditAT.class.getName()).log(Level.SEVERE, msg, e);
445  }
446  return null;
447  }
448 
449  // prepare messages for connected clients
450  ResponseCreator rc = AppBean.getResponseCreator();
451  synchronized(AppBean.getSessions()){
452  Iterator<EditorSession> sesIt = AppBean.getSessions().iterator();
453  while (sesIt.hasNext()) { // for each client
454  EditorSession es = sesIt.next();
455  Flier flier = new Flier();
456  flier.AddEditedType(annotType); // create list of changed types
457  es.addMessageTS(rc.createCometResponse(flier, es)); // create message
458  }
459  }
460  return null;
461  } // actionRemoveAnc()
462 
463 
464  /**
465  * Action listener for link for add attribute to edited type of annotation
466  *
467  * @return Returns page outcome (identificator of transition)
468  */
469  public String actionAddAttr() {
470  AnnotType backup = createBackup();
471  SessionManager.getSession().setFormBackup(backup);
472  SessionManager.getSession().setEditedAT(annotType);
473  SessionManager.getSession().setEditedATA(null);
474  return "addATA";
475  } // actionAddAttr()
476 
477  /**
478  * Creates object with backup of form data
479  *
480  * @return Returns object with backup of form data
481  */
483  UserGroup ug = null;
484  if (group != null && !group.isEmpty()) {
485  Object[] params = new Object[2];
486  params[0] = "name";
487  params[1] = group;
488  List uList = AppBean.getPersistenceManager().queryDB("UserGroup.findByName", params);
489  if (uList != null && !uList.isEmpty()) {
490  ug = (UserGroup) uList.get(0);
491  }
492  if (ug == null) {
493  errorMessage = MessageProvider.getMessage("errorUnknownUG");
494  }
495  }
496 
497  if (ug == null && annotType != null) {
498  ug = annotType.getGroup();
499  }
500 
501  AnnotType pa = null;
502  if (prAncStr != null && !prAncStr.isEmpty()) {
503  String uri = AppBean.getBaseTypeUri() + "g" + ug.getId()
504  + "/" + MessageProcessor.replaceArrows(prAncStr);
505  Object[] params = new Object[2];
506  params[0] = "uri";
507  params[1] = uri;
508  List pList = AppBean.getPersistenceManager().queryDB("AnnotType.findByUri", params);
509  if (pList != null && !pList.isEmpty()) {
510  pa = (AnnotType) pList.get(0);
511  }
512  if (pa == null) {
513  errorMessage = MessageProvider.getMessage("errorUnknownAnc");
514  }
515  }
516 
517  AnnotType backup = new AnnotType(null,name,pa,ug);
518  backup.setComment(comment);
519  backup.setRestrictedAtt(restrictedAtt);
520  backup.setUriInOntology(uriInOntology);
521  return backup;
522  } // createBackup()
523 
524  /**
525  * Action listener for link for add ancestor to edited type of annotation
526  *
527  * @return Returns page outcome (identificator of transition)
528  */
529  public String actionAddAnc() {
530  AnnotType backup = createBackup();
531  SessionManager.getSession().setFormBackup(backup);
532  SessionManager.getSession().setEditedAT(annotType);
533  SessionManager.getSession().setEditedATA(attr);
534  SessionManager.getSession().setCameFrom("editAT");
535  SessionManager.getSession().setSelectedUG(annotType.getGroup());
536  return "selectAT";
537  } // actionAddAnc()
538 
539  /**
540  * Action listener for seve button on page for adding new type of annotation
541  *
542  * @return Returns page outcome (identificator of next page or null to stay here)
543  */
544  public String btnSaveNewAction() {
545  if (name.isEmpty()) {
546  errorMessage = MessageProvider.getMessage("nameCantBeEmpty");
547  return null;
548  }
549 
550  UserGroup ug = null;
551  if (group != null && !group.isEmpty()) {
552  Object[] params = new Object[2];
553  params[0] = "name";
554  params[1] = group;
555  List uList = AppBean.getPersistenceManager().queryDB("UserGroup.findByName", params);
556  if (uList != null && !uList.isEmpty()) {
557  ug = (UserGroup) uList.get(0);
558  }
559  if (ug == null) {
560  errorMessage = MessageProvider.getMessage("errorUnknownUG");
561  return null;
562  }
563  } else {
564  errorMessage = MessageProvider.getMessage("errorUnknownUG");
565  return null;
566  }
567 
568  AnnotType pa = null;
569  if (prAncStr != null && !prAncStr.isEmpty()) {
570  String uri = AppBean.getBaseTypeUri() + "g" + ug.getId()
571  + "/" + MessageProcessor.replaceArrows(prAncStr);
572  Object[] params = new Object[2];
573  params[0] = "uri";
574  params[1] = uri;
575  List pList = AppBean.getPersistenceManager().queryDB("AnnotType.findByUri", params);
576  if (pList != null && !pList.isEmpty()) {
577  pa = (AnnotType) pList.get(0);
578  }
579  if (pa == null) {
580  errorMessage = MessageProvider.getMessage("errorUnknownAnc");
581  return null;
582  }
583  }
584 
585  annotType = new AnnotType(null,name,pa,ug); // create new type of annotation
586  annotType.setUri(annotType.getGeneratedURI()); // generate URI
587  annotType.setComment(comment);
588  annotType.setRestrictedAtt(restrictedAtt);
589  annotType.setUriInOntology(uriInOntology);
590 
591  // check for duplicity
592  Object[] params = new Object[2];
593  params[0] = "uri";
594  params[1] = annotType.getUri();
595  List dList = AppBean.getPersistenceManager().queryDB("AnnotType.findByUri", params);
596  if (dList != null && !dList.isEmpty()) {
597  errorMessage = MessageProvider.getMessage("duplicitUriOfNewType");
598  annotType = null; // discard new type
599  return null;
600  }
601 
602  // persist new type of annotation
603  EntityManager em = AppBean.getPersistenceManager().getEM();
604  EntityTransaction transaction = em.getTransaction();
605  try {
606  transaction.begin();
607 
608  // merge ancestor
609  if (pa != null) {
610  pa = em.merge(pa);
611  annotType.setAncestorType(pa);
612  annotType.addAncestorType(pa); // add primary ancestor to list of ancestors
613  }
614  annotType = em.merge(annotType); // persist new type
615 
616  em.flush();
617  transaction.commit();
618  } catch (Exception e) {
619  transaction.rollback();
620  errorMessage = MessageProvider.getMessage("newATNSDatabaseFailure");
622  String msg = "Persisting of new type of annotation failed.";
623  Logger.getLogger(EditAT.class.getName()).log(Level.SEVERE, msg, e);
624  }
625  return null;
626  } finally {
627  em.close();
628  }
629 
630  // prepare messages for connected clients
631  ResponseCreator rc = AppBean.getResponseCreator();
632  synchronized(AppBean.getSessions()){
633  Iterator<EditorSession> sesIt = AppBean.getSessions().iterator();
634  while (sesIt.hasNext()) { // for each client
635  EditorSession es = sesIt.next();
636  Flier flier = new Flier();
637  flier.AddAddedType(annotType); // create list of added types
638  es.addMessageTS(rc.createCometResponse(flier, es)); // create message
639  }
640  }
641 
642  SessionManager.getSession().setFormBackup(null);
643  SessionManager.getSession().setEditedAT(null);
644  return "aTypes";
645  } // btnSaveNewAction()
646 
647  /**
648  * Action listener for delete button on deleting page
649  *
650  * @return Returns page outcome (identificator of next page or null to stay here)
651  */
652  public String btnDeleteAction() {
653  // find annotations of this type
654  Object[] params = new Object[2];
655  params[0] = "type";
656  params[1] = annotType.getId();
657  List aList = AppBean.getPersistenceManager().queryDB("Annotation.findByType", params);
658  if (aList != null && !aList.isEmpty()) { // if annotation was found
659  errorMessage = MessageProvider.getMessage("ifAnnotationsExistsTypeCantBeDeleted");
660  return null;
661  }
662 
663 
664  // query DB for types, in which ancestors is deleted type
665  EntityManager em = AppBean.getPersistenceManager().getEM();
666  List tList = null;
667  try {
668  Query query = em.createQuery("SELECT a FROM AnnotType a JOIN a.ancestorTypes at WHERE at.id='" + annotType.getId() + "'");
669  tList = query.getResultList();
670  } catch (Exception e) {
671  tList = null;
672  } finally {
673  em.close();
674  }
675  if (tList != null && !tList.isEmpty()) {
676  errorMessage = MessageProvider.getMessage("ifChildrenExistsTypeCantBeDeleted");
677  return null;
678  }
679 
680  // query DB for types which primary ancestor is actual node
681  params[0] = "ancestor";
682  params[1] = annotType.getId();
683  tList = AppBean.getPersistenceManager().queryDB("AnnotType.findByAncestor", params);
684  if (tList != null && !tList.isEmpty()) {
685  errorMessage = MessageProvider.getMessage("ifChildrenExistsTypeCantBeDeleted");
686  return null;
687  }
688 
689  if (AppBean.getPersistenceManager().removeEntity(annotType)) {
690  errorMessage = MessageProvider.getMessage("typeCantBeDeletedDatabaseFailure");
691  return null;
692  }
693 
694  // prepare messages for connected clients
695  ResponseCreator rc = AppBean.getResponseCreator();
696  synchronized(AppBean.getSessions()){
697  Iterator<EditorSession> sesIt = AppBean.getSessions().iterator();
698  while (sesIt.hasNext()) { // for each client
699  EditorSession es = sesIt.next();
700  Flier flier = new Flier();
701  flier.AddRemovedType(annotType); // create list of removed types
702  es.addMessageTS(rc.createCometResponse(flier, es)); // create message
703  }
704  }
705 
706  SessionManager.getSession().setFormBackup(null);
707  SessionManager.getSession().setEditedUG(null);
708  return "aTypes";
709  } // btnDeleteAction()
710 
711  /**
712  * Gets list of attributes of given type of annotation
713  *
714  * @return Returns list of attributes of given type of annotation
715  */
716  public List<AnnotTypeAttr> getAttrList() {
717  getBackup();
718  return annotType.getAttributes();
719  }
720 
721  /**
722  * Gets list of ancestors of given type of annotation
723  *
724  * @return Returns list of ancestors of given type of annotation
725  */
726  public List<AnnotType> getAncList() {
727  getBackup();
728  return annotType.getAncestorTypes();
729  }
730 
731  /**
732  * Gets value of comment field in form
733  *
734  * @return Returns value of comment field in form
735  */
736  public String getComment() {
737  AnnotType backup = getBackup();
738  if (comment == null && backup != null) {
739  comment = backup.getComment();
740  } else if (comment == null && annotType != null) {
741  comment = annotType.getComment();
742  } else if (comment == null) {
743  comment = "";
744  }
745  return comment;
746  } // getComment()
747 
748  /**
749  * Sets value of comment field in form
750  *
751  * @param comment Value of comment field in form
752  */
753  public void setComment(String comment) {
754  this.comment = comment;
755  }
756 
757 
758  /**
759  * Gets value of name field in form
760  *
761  * @return Returns value of name field in form
762  */
763  public String getName() {
764  AnnotType backup = getBackup();
765  if (name == null && backup != null) {
766  name = backup.getName();
767  } else if (name == null && annotType != null) {
768  name = annotType.getName();
769  } else if (name == null) {
770  name = "";
771  }
772  return name;
773  } // getName()
774 
775  /**
776  * Sets value of name field in form
777  *
778  * @param name Value of name field in form
779  */
780  public void setName(String name) {
781  this.name = name;
782  }
783 
784  /**
785  * Gets value of group field in form
786  *
787  * @return Returns value of group field in form
788  */
789  public String getGroup() {
790  AnnotType backup = getBackup();
791  if (group == null && backup != null) {
792  group = backup.getGroup().getName();
793  } else if (group == null && annotType != null) {
794  group = annotType.getGroup().getName();
795  } else if (group == null) {
796  group = "";
797  }
798  return group;
799  } // getGroup()
800 
801  /**
802  * Sets value of group field in form
803  *
804  * @param group Value of group field in form
805  */
806  public void setGroup(String group) {
807  this.group = group;
808  }
809 
810  /**
811  * Gets value of restricted attributes field in form
812  *
813  * @return Returns value of restricted attributes field in form
814  */
815  public Boolean getRestrictedAtt() {
816  AnnotType backup = getBackup();
817  if (restrictedAtt == null && backup != null) {
818  restrictedAtt = backup.getRestrictedAtt();
819  } else if (restrictedAtt == null && annotType != null) {
820  restrictedAtt = annotType.getRestrictedAtt();
821  } else if (restrictedAtt == null) {
822  restrictedAtt = false;
823  }
824  return restrictedAtt;
825  } // getRestrictedAtt()
826 
827  /**
828  * Sets value of restricted attributes field in form
829  *
830  * @param restrictedAtt Value of restricted attributes field in form
831  */
832  public void setRestrictedAtt(Boolean restrictedAtt) {
833  this.restrictedAtt = restrictedAtt;
834  }
835 
836  /**
837  * Gets value of URI field in form
838  *
839  * @return Returns value of URI field in form
840  */
841  public String getUriInOntology() {
842  AnnotType backup = getBackup();
843  if (uriInOntology == null && backup != null) {
844  uriInOntology = backup.getUriInOntology();
845  } else if (uriInOntology == null && annotType != null) {
846  uriInOntology = annotType.getUriInOntology();
847  } else if (uriInOntology == null) {
848  uriInOntology = "";
849  }
850  return uriInOntology;
851  } // getUriInOntology()
852 
853  /**
854  * Sets value of URI field in form
855  *
856  * @param uriInOntology Value of URI field in form
857  */
858  public void setUriInOntology(String uriInOntology) {
859  this.uriInOntology = uriInOntology;
860  }
861 
862  /**
863  * Gets selected attribute in table
864  *
865  * @return Returns selected attribute in table
866  */
868  return attr;
869  }
870 
871  /**
872  * Sets selected attribute in table
873  *
874  * @param attr Selected attribute in table
875  */
876  public void setAttr(AnnotTypeAttr attr) {
877  this.attr = attr;
878  }
879 
880  /**
881  * Gets selected ancestor in table
882  *
883  * @return Returns selected attribute in table
884  */
885  public AnnotType getAnc() {
886  return anc;
887  }
888 
889  /**
890  * Sets selected ancestor in table
891  *
892  * @param anc Selected ancestor in table
893  */
894  public void setAnc(AnnotType anc) {
895  this.anc = anc;
896  }
897 
898 
899  /**
900  * Gets table with attributes of given type of annotation
901  *
902  * @return Returns table with attributes of given type of annotation
903  */
904  public HtmlDataTable getListTable() {
905  return listTable;
906  }
907 
908  /**
909  * Sets table with attributes of given type of annotation
910  *
911  * @param listTable Table with attributes of given type of annotation
912  */
913  public void setListTable(HtmlDataTable listTable) {
914  this.listTable = listTable;
915  }
916 
917  /**
918  * Gets table with ancestors of given type of annotation
919  *
920  * @return Returns table with ancestors of given type of annotation
921  */
922  public HtmlDataTable getAncTable() {
923  return ancTable;
924  }
925 
926  /**
927  * Sets table with ancestors of given type of annotation
928  *
929  * @param ancTable Table with ancestors of given type of annotation
930  */
931  public void setAncTable(HtmlDataTable ancTable) {
932  this.ancTable = ancTable;
933  }
934 
935  /**
936  * Gets value of primary ancestor field in form
937  *
938  * @return Returns value of primary ancestor field in form
939  */
940  public String getPrAncStr() {
941  AnnotType backup = getBackup();
942  if (prAncStr == null && backup != null) {
943  if (backup.getAncestorType() != null) {
944  prAncStr = backup.getAncestorType().getLinearizedName();
945  } else {
946  prAncStr = "";
947  }
948  } else if (prAncStr == null && annotType != null) {
949  if (annotType.getAncestorType() != null) {
950  prAncStr = annotType.getAncestorType().getLinearizedName();
951  } else {
952  prAncStr = "";
953  }
954  } else if (prAncStr == null) {
955  prAncStr = "";
956  }
957  return prAncStr;
958  } // getPrAncStr()
959 
960  /**
961  * Gets backup of form data
962  * If user came from another page, gets edited type of annotation from session.
963  * If user now returned from selection of new ancestor, adds this ancestor to type.
964  *
965  * @return If backup is available, returns backup of form data, null otherwise
966  */
967  private AnnotType getBackup() {
968  if (annotType == null) { // user is camming to this page
969  annotType = SessionManager.getSession().getEditedAT();
970  AnnotType nA = SessionManager.getSession().getSelectedAT();
971  if (nA != null && SessionManager.getSession().getCameFrom().equals("editAT")) {
972  addSelAncestor(nA); // add ancestor to type
973  SessionManager.getSession().setSelectedAT(null);
974  SessionManager.getSession().setCameFrom(null);
975  }
976  }
977  AnnotType backup = null;
978  if (SessionManager.getSession().getFormBackup() != null) {
979  if (SessionManager.getSession().getFormBackup().getClass().getName().endsWith("AnnotType")) {
980  backup = (AnnotType) SessionManager.getSession().getFormBackup();
981  if (backup.getId() != annotType.getId()) {
982  backup = null;
983  }
984  }
985  }
986  return backup;
987  } // getBackup()
988 
989  /**
990  * Adds selected type of annotation to ancestors
991  *
992  * @param newAnc Type of annotation to add to ancestors
993  */
994  private void addSelAncestor(AnnotType newAnc) {
995  try {
996  EntityManager em = AppBean.getPersistenceManager().getEM();
997  EntityTransaction transaction = em.getTransaction();
998  transaction.begin();
999  newAnc = em.merge(newAnc);
1000  annotType = em.merge(annotType);
1001  annotType.addAncestorType(newAnc);
1002  em.flush();
1003  transaction.commit();
1004  } catch (Exception e) {
1005  errorMessage = MessageProvider.getMessage("ancCantBeAddedDF");
1006  return;
1007  }
1008 
1009  // prepare messages for connected clients
1010  ResponseCreator rc = AppBean.getResponseCreator();
1011  synchronized(AppBean.getSessions()){
1012  Iterator<EditorSession> sesIt = AppBean.getSessions().iterator();
1013  while (sesIt.hasNext()) { // for each client
1014  EditorSession es = sesIt.next();
1015  Flier flier = new Flier();
1016  flier.AddEditedType(annotType); // create list of changed types
1017  es.addMessageTS(rc.createCometResponse(flier, es)); // create message
1018  }
1019  }
1020  } // addSelAncestor()
1021 
1022  /**
1023  * Sets value of primary ancestor field in form
1024  *
1025  * @param prAncStr Value of primary ancestor field in form
1026  */
1027  public void setPrAncStr(String prAncStr) {
1028  this.prAncStr = prAncStr;
1029  }
1030 
1031  /**
1032  * Gets error message displayed in form
1033  *
1034  * @return Returns error message displayed in form
1035  */
1036  public String getErrorMessage() {
1037  return errorMessage;
1038  }
1039 
1040  /**
1041  * Sets error message to be displayed in form
1042  *
1043  * @param errorMessage Error message to be displayed in form
1044  */
1045  public void setErrorMessage(String errorMessage) {
1046  this.errorMessage = errorMessage;
1047  }
1048 
1049  /**
1050  * Gets edited type of annotation
1051  *
1052  * @return Returns edited type of annotation
1053  */
1055  getBackup();
1056  return annotType;
1057  }
1058 
1059  /**
1060  * Sets edited type of annotation
1061  *
1062  * @param annotType Edited type of annotation
1063  */
1065  this.annotType = annotType;
1066  }
1067 
1068 } // public class EditAT
Class representing attribute of type of annotation.
Class for manipulating with session.
Singleton for storing global variables.
Definition: AppBean.java:47
void setRestrictedAtt(Boolean restrictedAtt)
Definition: EditAT.java:832
Static class which parses and process XML with messages.
Class representing user group.
Definition: UserGroup.java:47
void setAnnotType(AnnotType annotType)
Definition: EditAT.java:1064
void setAncTable(HtmlDataTable ancTable)
Definition: EditAT.java:931
static AnnotType queryType(String uri, EntityManager em)
Definition: EditAT.java:329
Backbean for adding, editing and deleting of types of annotations.
Definition: EditAT.java:46
Class representing type of annotation.
Definition: AnnotType.java:58
Flier with informations for comet handlers.
Definition: Flier.java:31
List< AnnotTypeAttr > getAttrList()
Definition: EditAT.java:716
void addSelAncestor(AnnotType newAnc)
Definition: EditAT.java:994
Class that creates responses and persists data.
void setUriInOntology(String uriInOntology)
Definition: EditAT.java:858
void setListTable(HtmlDataTable listTable)
Definition: EditAT.java:913
void setAttr(AnnotTypeAttr attr)
Definition: EditAT.java:876
Informations about client session.
void setErrorMessage(String errorMessage)
Definition: EditAT.java:1045