14 package cz.vutbr.fit.knot.annotations.web;
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;
46 public class EditAT implements Serializable {
80 private String
name = null;
118 if (
name.isEmpty()) {
119 errorMessage = MessageProvider.getMessage(
"nameCantBeEmpty");
123 List<AnnotType> affectedTypes =
new ArrayList<AnnotType>();
127 boolean ancestorChange =
false;
128 boolean nameChange =
false;
134 Object[] params =
new Object[2];
137 List pList = AppBean.getPersistenceManager().queryDB(
"AnnotType.findByUri", params);
138 if (pList != null && !pList.isEmpty()) {
142 errorMessage = MessageProvider.getMessage(
"errorUnknownAnc");
149 ancestorChange =
true;
153 ancestorChange =
true;
162 String backupN = annotType.getName();
163 AnnotType backupT = annotType.getAncestorType();
167 Object[] params =
new Object[2];
169 params[1] = annotType.getId();
170 List aList = AppBean.getPersistenceManager().queryDB(
"Annotation.findByType", params);
171 if (aList != null && !aList.isEmpty()) {
177 annotType.setName(
name);
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);
188 if (ancestorChange) {
189 annotType.setAncestorType(pa);
190 Object[] params =
new Object[2];
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);
202 if (ancestorChange || nameChange) {
203 boolean someChange =
true;
206 List<AnnotType> nowAdd =
new ArrayList<AnnotType>();
207 for (Iterator<AnnotType> afIt = affectedTypes.iterator(); afIt.hasNext();) {
210 Object[] params =
new Object[2];
211 params[0] =
"ancestor";
212 params[1] = afT.getId();
214 @SuppressWarnings(
"unchecked")
215 List<AnnotType> tList = AppBean.getPersistenceManager().queryDB(
"AnnotType.findByAncestor", params);
216 if (tList != null && !tList.isEmpty()) {
217 for (Iterator<AnnotType> tIt = tList.iterator(); tIt.hasNext();) {
219 if (!affectedTypes.contains(t) && !nowAdd.contains(t)) {
226 affectedTypes.addAll(nowAdd);
231 if (ancestorChange) {
232 for (Iterator<AnnotType> afTIt = affectedTypes.iterator(); afTIt.hasNext();) {
234 Object[] params =
new Object[2];
236 params[1] = afTT.getId();
237 List aList = AppBean.getPersistenceManager().queryDB(
"Annotation.findByType", params);
238 if (aList != null && !aList.isEmpty()) {
240 annotType.setName(backupN);
241 annotType.setAncestorType(backupT);
248 EntityManager em = AppBean.getPersistenceManager().getEM();
249 EntityTransaction transaction = em.getTransaction();
250 boolean errorOccurred =
false;
255 for (Iterator<AnnotType> aTIt = affectedTypes.iterator(); aTIt.hasNext();) {
259 sType.setName(annotType.getName());
260 sType.setComment(annotType.getComment());
261 sType.setRestrictedAtt(annotType.getRestrictedAtt());
262 sType.setUriInOntology(annotType.getUriInOntology());
263 if (ancestorChange) {
265 if (backupT != null) {
266 oAType =
queryType(backupT.getUri(), em);
269 sType.setAncestorType(null);
270 if (oAType != null) {
271 sType.getAncestorTypes().
remove(oAType);
275 sType.setAncestorType(nType);
276 if (!sType.getAncestorTypes().contains(nType)) {
277 sType.getAncestorTypes().add(nType);
283 sType.setUri(sType.getGeneratedURI());
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);
302 synchronized(AppBean.getSessions()){
303 Iterator<EditorSession> sesIt = AppBean.getSessions().iterator();
304 while (sesIt.hasNext()) {
308 for (Iterator<AnnotType> atIt = affectedTypes.iterator(); atIt.hasNext();) {
310 flier.AddEditedType(at);
312 es.addMessageTS(rc.createCometResponse(flier, es));
316 SessionManager.getSession().setFormBackup(null);
317 SessionManager.getSession().setEditedAT(null);
331 Object[] params =
new Object[2];
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]);
338 List aList = q.getResultList();
339 if (aList != null && !aList.isEmpty()) {
342 throw new RuntimeException(
"Type not found!");
353 SessionManager.getSession().setFormBackup(null);
354 SessionManager.getSession().setEditedAT(null);
370 EntityManager em = AppBean.getPersistenceManager().getEM();
371 EntityTransaction transaction = em.getTransaction();
373 attr = em.merge(
attr);
375 annotType.getAttributes().
remove(attr);
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);
390 synchronized(AppBean.getSessions()){
391 Iterator<EditorSession> sesIt = AppBean.getSessions().iterator();
392 while (sesIt.hasNext()) {
396 es.addMessageTS(rc.createCometResponse(flier, es));
415 SessionManager.getSession().setFormBackup(backup);
416 SessionManager.getSession().setEditedAT(
annotType);
417 SessionManager.getSession().setEditedATA(attr);
432 EntityManager em = AppBean.getPersistenceManager().getEM();
433 EntityTransaction transaction = em.getTransaction();
437 annotType.getAncestorTypes().
remove(anc);
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);
451 synchronized(AppBean.getSessions()){
452 Iterator<EditorSession> sesIt = AppBean.getSessions().iterator();
453 while (sesIt.hasNext()) {
457 es.addMessageTS(rc.createCometResponse(flier, es));
471 SessionManager.getSession().setFormBackup(backup);
472 SessionManager.getSession().setEditedAT(
annotType);
473 SessionManager.getSession().setEditedATA(null);
485 Object[] params =
new Object[2];
488 List uList = AppBean.getPersistenceManager().queryDB(
"UserGroup.findByName", params);
489 if (uList != null && !uList.isEmpty()) {
493 errorMessage = MessageProvider.getMessage(
"errorUnknownUG");
498 ug = annotType.getGroup();
503 String uri = AppBean.getBaseTypeUri() +
"g" + ug.
getId()
504 +
"/" + MessageProcessor.replaceArrows(
prAncStr);
505 Object[] params =
new Object[2];
508 List pList = AppBean.getPersistenceManager().queryDB(
"AnnotType.findByUri", params);
509 if (pList != null && !pList.isEmpty()) {
513 errorMessage = MessageProvider.getMessage(
"errorUnknownAnc");
531 SessionManager.getSession().setFormBackup(backup);
532 SessionManager.getSession().setEditedAT(
annotType);
533 SessionManager.getSession().setEditedATA(
attr);
534 SessionManager.getSession().setCameFrom(
"editAT");
545 if (
name.isEmpty()) {
546 errorMessage = MessageProvider.getMessage(
"nameCantBeEmpty");
552 Object[] params =
new Object[2];
555 List uList = AppBean.getPersistenceManager().queryDB(
"UserGroup.findByName", params);
556 if (uList != null && !uList.isEmpty()) {
560 errorMessage = MessageProvider.getMessage(
"errorUnknownUG");
564 errorMessage = MessageProvider.getMessage(
"errorUnknownUG");
570 String uri = AppBean.getBaseTypeUri() +
"g" + ug.
getId()
571 +
"/" + MessageProcessor.replaceArrows(
prAncStr);
572 Object[] params =
new Object[2];
575 List pList = AppBean.getPersistenceManager().queryDB(
"AnnotType.findByUri", params);
576 if (pList != null && !pList.isEmpty()) {
580 errorMessage = MessageProvider.getMessage(
"errorUnknownAnc");
586 annotType.setUri(annotType.getGeneratedURI());
592 Object[] params =
new Object[2];
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");
603 EntityManager em = AppBean.getPersistenceManager().getEM();
604 EntityTransaction transaction = em.getTransaction();
611 annotType.setAncestorType(pa);
612 annotType.addAncestorType(pa);
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);
632 synchronized(AppBean.getSessions()){
633 Iterator<EditorSession> sesIt = AppBean.getSessions().iterator();
634 while (sesIt.hasNext()) {
638 es.addMessageTS(rc.createCometResponse(flier, es));
642 SessionManager.getSession().setFormBackup(null);
643 SessionManager.getSession().setEditedAT(null);
654 Object[] params =
new Object[2];
656 params[1] = annotType.getId();
657 List aList = AppBean.getPersistenceManager().queryDB(
"Annotation.findByType", params);
658 if (aList != null && !aList.isEmpty()) {
659 errorMessage = MessageProvider.getMessage(
"ifAnnotationsExistsTypeCantBeDeleted");
665 EntityManager em = AppBean.getPersistenceManager().getEM();
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) {
675 if (tList != null && !tList.isEmpty()) {
676 errorMessage = MessageProvider.getMessage(
"ifChildrenExistsTypeCantBeDeleted");
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");
690 errorMessage = MessageProvider.getMessage(
"typeCantBeDeletedDatabaseFailure");
696 synchronized(AppBean.getSessions()){
697 Iterator<EditorSession> sesIt = AppBean.getSessions().iterator();
698 while (sesIt.hasNext()) {
702 es.addMessageTS(rc.createCometResponse(flier, es));
706 SessionManager.getSession().setFormBackup(null);
707 SessionManager.getSession().setEditedUG(null);
718 return annotType.getAttributes();
728 return annotType.getAncestorTypes();
738 if (
comment == null && backup != null) {
741 comment = annotType.getComment();
765 if (
name == null && backup != null) {
766 name = backup.getName();
768 name = annotType.getName();
769 }
else if (
name == null) {
791 if (
group == null && backup != null) {
795 }
else if (
group == null) {
942 if (
prAncStr == null && backup != null) {
944 prAncStr = backup.getAncestorType().getLinearizedName();
950 prAncStr = annotType.getAncestorType().getLinearizedName();
969 annotType = SessionManager.getSession().getEditedAT();
970 AnnotType nA = SessionManager.getSession().getSelectedAT();
973 SessionManager.getSession().setSelectedAT(null);
974 SessionManager.getSession().setCameFrom(null);
981 if (backup.
getId() != annotType.getId()) {
996 EntityManager em = AppBean.getPersistenceManager().getEM();
997 EntityTransaction transaction = em.getTransaction();
999 newAnc = em.merge(newAnc);
1001 annotType.addAncestorType(newAnc);
1003 transaction.commit();
1004 }
catch (Exception e) {
1005 errorMessage = MessageProvider.getMessage(
"ancCantBeAddedDF");
1011 synchronized(AppBean.getSessions()){
1012 Iterator<EditorSession> sesIt = AppBean.getSessions().iterator();
1013 while (sesIt.hasNext()) {
1017 es.addMessageTS(rc.createCometResponse(flier, es));
static WebSession getSession()
String actionRemoveAttr(Integer id)
Class representing attribute of type of annotation.
Class for manipulating with session.
static String replaceArrows(String aString)
AnnotType getAncestorType()
Singleton for storing global variables.
HtmlDataTable getAncTable()
HtmlDataTable getListTable()
void setAnc(AnnotType anc)
void setRestrictedAtt(Boolean restrictedAtt)
void setComment(String comment)
String actionEditAttr(Integer id)
Static class which parses and process XML with messages.
Class representing user group.
void setGroup(String group)
String actionRemoveAnc(Integer id)
void setAnnotType(AnnotType annotType)
void setAncTable(HtmlDataTable ancTable)
static AnnotType queryType(String uri, EntityManager em)
Backbean for adding, editing and deleting of types of annotations.
void setName(String name)
Boolean getRestrictedAtt()
static final int LOG_LEVEL
Class representing type of annotation.
Flier with informations for comet handlers.
List< AnnotTypeAttr > getAttrList()
void addSelAncestor(AnnotType newAnc)
String getUriInOntology()
static PersistM getPersistenceManager()
Class that creates responses and persists data.
static final int LOG_LEVEL_SERVER_ERRORS
String btnSaveNewAction()
void setUriInOntology(String uriInOntology)
void setListTable(HtmlDataTable listTable)
void setAttr(AnnotTypeAttr attr)
Informations about client session.
void setErrorMessage(String errorMessage)
List< AnnotType > getAncList()
void setPrAncStr(String prAncStr)