4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
Persister.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: Persister.java
5  * Description: Static class which persists data to the database
6  */
7 
8 /**
9  * @file Persister.java
10  *
11  * @brief Static class which persists data to the database
12  */
13 
14 package cz.vutbr.fit.knot.annotations.comet;
15 
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Map.Entry;
37 import java.util.logging.Level;
38 import java.util.logging.Logger;
39 import javax.persistence.EntityManager;
40 import javax.persistence.EntityTransaction;
41 import javax.persistence.Query;
42 
43 /**
44  * Static class which persists data to the database
45  *
46  * @brief Static class which persists data to the database
47  * @author idytrych
48  */
49 public class Persister {
50 
51  /**
52  * Persist informations from request to the database
53  * (it persists derived and related informations too)
54  *
55  * @param requestInfo Informations about client request
56  * @return If succeed, returns false, in case of error returns true
57  */
58  public static boolean persist(RequestInfo requestInfo) {
59  Flier flier = requestInfo.getFlier();
60  EntityManager em = AppBean.getPersistenceManager().getEM();
61  EntityTransaction transaction = em.getTransaction();
62  boolean errorOccurred = false;
63  try {
64  transaction.begin();
65 
66  /* Update settings */
67  if (requestInfo.getSettings() != null && !requestInfo.getSettings().isEmpty()) {
68  /* Remove old settings */
69  ArrayList<Settings> settings = new ArrayList<Settings>();
70  Object[] params = new Object[2];
71  params[0] = "userId";
72  params[1] = requestInfo.getSession().getUser().getId();
73  List sList = AppBean.getPersistenceManager().queryDB("Settings.findByUser", params);
74  if (sList != null && !sList.isEmpty()) {
75  Iterator sListIter = sList.iterator();
76  while (sListIter.hasNext()) {
77  settings.add((Settings) sListIter.next());
78  }
79  }
80 
81  // Condition that check whether protocol version 2 is used
82  if(requestInfo.getSession().getProtocolLOD() == Constants.PROTOCOL_LOD_V2){
83  //Getting current user's settings
84  Iterator<Settings> clientSettIt = requestInfo.getSettings().iterator();
85 
86  while(clientSettIt.hasNext()){
87  Iterator<Settings> oldSettingIt = settings.iterator();
88  Settings clientTmpSett = clientSettIt.next();
89  Boolean flag = false;
90 
91  //Looking for a current setting within old ones
92  while(oldSettingIt.hasNext()){
93  Settings oldSetting = oldSettingIt.next();
94 
95  if((oldSetting.getName()).equals(clientTmpSett.getName())){
96  flag = true;
97  //Update current setting
98  if(clientTmpSett.getValue() != null){
99  oldSetting = em.merge(oldSetting);
100  oldSetting.setValue(clientTmpSett.getValue());
101  oldSetting.setDescription(clientTmpSett.getDescription());
102  }
103  //Remove current setting
104  else{
105  oldSetting = em.merge(oldSetting);
106  em.remove(oldSetting);
107  }
108  break;
109  }
110  }
111 
112  //Adding new setting into database
113  if(flag == false){
114  if(clientTmpSett.getValue() != null)
115  em.persist(clientTmpSett);
116  }
117  }
118  }
119  // Protocol version 1 is used
120  else{
121  Iterator<Settings> oldSettingsIter = settings.iterator();
122  while (oldSettingsIter.hasNext()) {
123  Settings param = oldSettingsIter.next();
124  param = em.merge(param);
125  em.remove(param);
126  }
127 
128  /* Save new settings */
129  Iterator<Settings> settingsIter = requestInfo.getSettings().iterator();
130  while (settingsIter.hasNext()) {
131  Settings param = settingsIter.next();
132  em.persist(param);
133  }
134  }
135  }
136 
137  /* Create new subscriptions */
138  if(requestInfo.getNewSubscriptions() != null && !requestInfo.getNewSubscriptions().isEmpty()){
139  ArrayList<Subscription> persistedSubscriptions = new ArrayList<Subscription>();
140  Iterator<Subscription> subNewIter = requestInfo.getNewSubscriptions().iterator();
141  while (subNewIter.hasNext()) {
142  Subscription subscription = subNewIter.next();
143  Integer savedTmpId = subscription.getTmpId();
144  subscription = em.merge(subscription);
145  em.persist(subscription);
146  subscription.setTmpId(savedTmpId);
147  persistedSubscriptions.add(subscription);
148  }
149 
150  requestInfo.setNewSubscriptions(persistedSubscriptions);
151  requestInfo.getFlier().setNewSubscriptions(persistedSubscriptions);
152  }
153 
154  /* Update subscriptions */
155  if(requestInfo.getUpdatedSubscriptions() != null && !requestInfo.getUpdatedSubscriptions().isEmpty()){
156  ArrayList<Subscription> changedSubscriptions = new ArrayList<Subscription>();
157  Iterator<Subscription> subChangeIter = requestInfo.getUpdatedSubscriptions().iterator();
158  while (subChangeIter.hasNext()){
159  Subscription currentSub = subChangeIter.next();
160 
161  // get subscription
162  Object[] params = new Object[2];
163  params[0] = "id";
164  params[1] = currentSub.getId();
165  @SuppressWarnings("unchecked")
166  List<Subscription> SubsList = AppBean.getPersistenceManager().queryDB("Subscription.findById", params);
167  if(SubsList == null || SubsList.isEmpty()){
168  //error
169  break;
170  }
171 
172  Subscription oldSub = SubsList.get(0);
173  requestInfo.addBeforeUpdatedSubscriptions(oldSub);
174 
175  // delete old sources
176  Iterator<Source> oldSourcesIt = oldSub.getSubscriptions().iterator();
177  while(oldSourcesIt.hasNext()){
178  Source currentSource = oldSourcesIt.next();
179  Source mergedSource = em.merge(currentSource);
180  em.remove(mergedSource);
181  }
182 
183  // save subscriptions with new sources
184  em.merge(currentSub);
185  if(requestInfo.getSession().isInSubscribeList(oldSub)){
186  requestInfo.addToUnsubscribe(oldSub);
187  requestInfo.addToSubscribe(oldSub);
188  }
189  requestInfo.getFlier().addChangedSubscription(currentSub);
190  }
191  }
192 
193  /* Delete subscriptions */
194  if(requestInfo.getDeletedSubscriptions() != null && !requestInfo.getDeletedSubscriptions().isEmpty()){
195  ArrayList<Subscription> deletedSubscriptions = new ArrayList<Subscription>();
196  Iterator<Subscription> subDeleteIter = requestInfo.getDeletedSubscriptions().iterator();
197  while (subDeleteIter.hasNext()) {
198  Subscription subscription = subDeleteIter.next();
199  subscription = em.merge(subscription);
200  em.remove(subscription);
201  deletedSubscriptions.add(subscription);
202 
203  if(requestInfo.getSession().isInSubscribeList(subscription)){
204  requestInfo.addToUnsubscribe(subscription);
205  }
206  }
207 
208  requestInfo.setDeletedSubscriptions(deletedSubscriptions);
209  }
210 
211  /* Update user groups */
212  if (!requestInfo.getJoinedGroups().isEmpty() || !requestInfo.getLeavedGroups().isEmpty()) {
213  requestInfo.getSession().setUser(em.merge(requestInfo.getSession().getUser()));
214  Iterator<UserGroup> ugIter = requestInfo.getJoinedGroups().iterator();
215  while (ugIter.hasNext()) {
216  UserGroup jug = ugIter.next();
217  em.merge(jug);
218  }
219  Iterator<UserGroup> lgIter = requestInfo.getLeavedGroups().iterator();
220  while (lgIter.hasNext()) {
221  UserGroup lug = lgIter.next();
222  em.merge(lug);
223  }
224  }
225 
226  /* Persist new document or changes */
227  if (requestInfo.getSyncDocument() != null) {
228  if (requestInfo.isNewDocument()) {
229  em.persist(requestInfo.getSyncDocument());
230  } else {
231  em.merge(requestInfo.getSyncDocument());
232  }
233  }
234  if (requestInfo.isDocumentChanged()) {
235  em.merge(requestInfo.getSession().getSyncDocument());
236  }
237 
238  /* Persist types */
239 
240  // create list without attributes and ansestors and map with original types
241  ArrayList<AnnotType> nTWA = new ArrayList<AnnotType>();
242  Map<String, AnnotType> nTMap = new HashMap<String, AnnotType>();
243  for (Iterator<AnnotType> ntIt = flier.getAddedTypes().iterator(); ntIt.hasNext();) {
244  AnnotType type = ntIt.next();
245  AnnotType nT = new AnnotType(type.getUri(), type.getName(), null, type.getGroup());
246  nT.setRestrictedAtt(type.getRestrictedAtt());
247  nT.setUriInOntology(type.getUriInOntology());
248  nT.setComment(type.getComment());
249  nTWA.add(nT);
250  nTMap.put(type.getUri(), type);
251  }
252 
253  // merge types without attributes and create map with merged types
254  ArrayList<AnnotType> mergedTypes = new ArrayList<AnnotType>();
255  Map<String, AnnotType> mTypesMap = new HashMap<String, AnnotType>();
256  for (Iterator<AnnotType> nTWAIt = nTWA.iterator(); nTWAIt.hasNext();) {
257  AnnotType type = nTWAIt.next();
258  type = em.merge(type);
259  mergedTypes.add(type);
260  mTypesMap.put(type.getUri(), type);
261  }
262 
263  // fix, add and merge attributes and ancestors
264  for (Iterator<AnnotType> mIt = mergedTypes.iterator(); mIt.hasNext();) {
265  AnnotType mType = mIt.next();
266  AnnotType oType = nTMap.get(mType.getUri()); // get original type
267 
268  // attributes
269  List<AnnotTypeAttr> aList = oType.getAttributes();
270  for (Iterator<AnnotTypeAttr> aIt = aList.iterator(); aIt.hasNext();) {
271  AnnotTypeAttr aTA = aIt.next();
272  AnnotType aTAAT = aTA.getAttributeType();
273  if (aTAAT != null) { // if it is structured attribute
274  // replace type with merged one
275  AnnotType rType = mTypesMap.get(aTAAT.getUri());
276  if (rType == null) { // it is not new type, it is in database
277  rType = queryType(aTAAT.getUri(), em);
278  }
279  aTA.setAttributeType(rType);
280  }
281  // fix back reference to annotation type
282  aTA.setAnnotationType(mTypesMap.get(aTA.getAnnotationType().getUri()));
283  }
284  mType.setAttributes(new ArrayList<AnnotTypeAttr>(aList));
285 
286  // primary ancestor
287  if (oType.getAncestorType() != null) {
288  // replace type with merged one
289  AnnotType rType = mTypesMap.get(oType.getAncestorType().getUri());
290  if (rType == null) { // it is not new type, it is in database
291  rType = queryType(oType.getAncestorType().getUri(), em);
292  }
293  mType.setAncestorType(rType);
294  }
295 
296  // ancestors
297  ArrayList<AnnotType> newAncestors = new ArrayList<AnnotType>();
298  for (Iterator<AnnotType> ancIt = oType.getAncestorTypes().iterator(); ancIt.hasNext();) {
299  AnnotType ancType = ancIt.next();
300  AnnotType mAnc = mTypesMap.get(ancType.getUri());
301  if (mAnc == null) { // it is not new type, it is in database
302  mAnc = queryType(ancType.getUri(), em);
303  }
304  newAncestors.add(mAnc);
305  }
306  mType.setAncestorTypes(newAncestors);
307 
308  mType = em.merge(mType);
309  } // fix, add and merge attributes and ancestors
310 
311 
312  Iterator<AnnotType> editAnnotTypeIter = flier.getEditedTypes().iterator();
313  while (editAnnotTypeIter.hasNext()) { // traverse edited types
314  AnnotType type = editAnnotTypeIter.next();
315  Query query = em.createNamedQuery("AnnotType.findById");
316  query.setParameter("id", type.getId());
317  List tList = query.getResultList();
318  if (tList == null || tList.isEmpty()) {
320  String msg = "Edited type of annotation was not found by Persister: " + type.getUri();
321  Logger.getLogger(Persister.class.getName()).log(Level.SEVERE, msg);
322  }
323  throw new RuntimeException("Object not found.");
324  }
325  AnnotType pType = (AnnotType) tList.get(0);
326  boolean differ = false;
327  List<AnnotTypeAttr> attrs = pType.getAttributes();
328  Iterator<AnnotTypeAttr> attrIter = attrs.iterator();
329  Iterator<AnnotTypeAttr> newAttrIter = type.getAttributes().iterator();
330  while (attrIter.hasNext()) {
331  AnnotTypeAttr attr = attrIter.next();
332  if (!newAttrIter.hasNext()) {
333  em.remove(attr);
334  } else {
335  AnnotTypeAttr newAttr = newAttrIter.next();
336  newAttr.setId(attr.getId());
337  }
338  }
339  // create new list of ancestors (with all types from database)
340  ArrayList<AnnotType> newAnc = new ArrayList<AnnotType>();
341  for (Iterator<AnnotType> ancIt = type.getAncestorTypes().iterator(); ancIt.hasNext();) {
342  AnnotType ancT = ancIt.next();
343  if (ancT == null) {
344  // if it's a new type of annotation, it must be set again after merge
345  AnnotType annotType = null;
346  Object[] params = new Object[2];
347  params[0] = "uri";
348  params[1] = ancT.getUri();
349  Query q = em.createNamedQuery("AnnotType.findByUri");
350  for (int p = 0; p < params.length; p = p + 2) {
351  q.setParameter((String) params[p], params[p + 1]);
352  }
353  List aList = q.getResultList();
354  if (aList != null && !aList.isEmpty()) { // if type was found
355  annotType = (AnnotType) aList.get(0);
356  annotType = em.merge(annotType);
357  newAnc.add(annotType);
358  } else { // if type was not found (merge failed)
360  String msg = "Ancestor type of annotaton was not found by Persister: " + ancT.getUri();
361  Logger.getLogger(Persister.class.getName()).log(Level.SEVERE, msg);
362  }
363  throw new RuntimeException("New type not found!");
364  }
365  } else { // if ancestor is not new
366  ancT = em.merge(ancT);
367  newAnc.add(ancT);
368  }
369  } // create new list of ancestors
370 
371  type.setAncestorTypes(newAnc); // replace old list with new
372 
373  // save changes and new attribues
374  type = em.merge(type);
375  } // traverse edited types
376 
377  Iterator<AnnotType> delAnnotTypeIter = flier.getRemovedTypes().iterator();
378  while (delAnnotTypeIter.hasNext()) {
379  AnnotType type = delAnnotTypeIter.next();
380  type = em.merge(type);
381  em.remove(type);
382  }
383 
384  /* Persist annotations */
385  ArrayList<Annotation> nowSavedAnnots = new ArrayList<Annotation>();
386  ArrayList<Annotation> justSavedAnnots = new ArrayList<Annotation>();
387  ArrayList<Annotation> nestedBeforeSave = new ArrayList<Annotation>();
388  ArrayList<Annotation> nestedAfterSave = new ArrayList<Annotation>();
389  HashMap<Annotation,ArrayList<BaseAttribute>> unSavedLinks = new HashMap<Annotation, ArrayList<BaseAttribute>>();
390  Iterator<Annotation> addedAnnotIter = flier.getAddedAnnotations().iterator();
391  while (addedAnnotIter.hasNext()) { // for each added annotation
392  Annotation annotation = addedAnnotIter.next();
393  if (annotation.getAnnotType().getId() == null) {
394  updateTypes(annotation, em, "New type of annotation was not found by Persister: ");
395  }
396  updateTypesInNested(annotation, em); // update types in nested annotations
397  if (justSavedAnnots.contains(annotation)) { // skip just saved to avoid duplicity
398  continue;
399  }
400  // update tmpId links in attributes
401  saveTmpIdRefAnnotations(annotation, requestInfo, em, justSavedAnnots, nowSavedAnnots, nestedBeforeSave, nestedAfterSave, unSavedLinks);
402  } // for each added annotation
403  saveTmpIdRefAnnotationsSolveLinks(requestInfo, em, justSavedAnnots, nowSavedAnnots, nestedBeforeSave, nestedAfterSave, unSavedLinks);
404  flier.setAddedAnnotations(nowSavedAnnots);
405 
406  // Find mappings of temp URI to serv URI for nested annotations
407  Iterator<Annotation> naIt = nestedBeforeSave.iterator();
408  while (naIt.hasNext()) {
409  Annotation a = naIt.next();
410  int ni = nestedBeforeSave.indexOf(a);
411  Annotation sav = nestedAfterSave.get(ni);
412  requestInfo.getFlier().addJustAddedAnnotation(AppBean.getBaseUri() + "/temp/" + a.getTmpId(), sav.getURIV2());
413  }
414 
415  Iterator<Annotation> editAnnotIter = flier.getEditedAnnotations().iterator();
416  while (editAnnotIter.hasNext()) {
417  Annotation annotation = editAnnotIter.next();
418  if (annotation.getAnnotType().getId() == null) {
419  updateTypes(annotation, em, "New type of annotation for edited annotation was not found by Persister: ");
420  }
421  updateAnnot(em, annotation, requestInfo);
422  }
423 
424  Iterator<Annotation> delAnnotIter = flier.getRemovedAnnotations().iterator();
425  while (delAnnotIter.hasNext()) {
426  Annotation annotation = delAnnotIter.next();
427  annotation = em.merge(annotation);
428  em.remove(annotation);
429  }
430 
431  em.flush();
432 
433  // Update URI of linked annotations
434  Iterator<Annotation>addeAnnotIter = flier.getAddedAnnotations().iterator();
435  while(addeAnnotIter.hasNext()){
436  Annotation actualAnnot = addeAnnotIter.next();
437  setUriOfAnnotationsParams(actualAnnot);
438  }
439  Iterator<Annotation>edAnnotIter = flier.getEditedAnnotations().iterator();
440  while(edAnnotIter.hasNext()){
441  Annotation actualAnnot = edAnnotIter.next();
442  setUriOfAnnotationsParams(actualAnnot);
443  }
444  Iterator<Annotation>updatedAnnotIter = flier.getAutoUpdatedAnnotations().iterator();
445  while(updatedAnnotIter.hasNext()){
446  Annotation actualAnnot = updatedAnnotIter.next();
447  setUriOfAnnotationsParams(actualAnnot);
448  }
449  // update confirmed version of the suggestion with the one persisted in the database
450  Iterator<SuggestionLogEntry>confirmedSuggIt = requestInfo.getConfirmedSuggestions().iterator();
451  while (confirmedSuggIt.hasNext()) {
452  SuggestionLogEntry sle = confirmedSuggIt.next();
453  Iterator<Annotation>aAnnotIter = nowSavedAnnots.iterator();
454  while (aAnnotIter.hasNext()) {
455  Annotation addAn = aAnnotIter.next();
456  // some annotations can exist without tmpId, therefore the value must
457  // be compared with a null value
458  if (addAn.getTmpId() != null && addAn.getTmpId().equals(sle.getTmpId())) {
459  sle.setConfirmedVersion(addAn);
460  break;
461  }
462  }
463  }
464 
465  em.flush();
466  transaction.commit();
467  } catch (Exception e) {
468  transaction.rollback();
470  String msg = "Exception during the persisting of data.";
471  Logger.getLogger(Persister.class.getName()).log(Level.SEVERE, msg, e);
472  }
473  errorOccurred = true;
474  } finally {
475  em.close();
476  }
477 
478  if (errorOccurred && (!requestInfo.getJoinedGroups().isEmpty() || !requestInfo.getLeavedGroups().isEmpty())) {
479  // refresh user informations
480  Object[] params = new Object[2];
481  params[0] = "id";
482  params[1] = requestInfo.getSession().getUser().getId();
483  List uList = AppBean.getPersistenceManager().queryDB("User.findById", params);
484  if (uList != null && !uList.isEmpty()) {
485  requestInfo.getSession().setUser((User) uList.get(0));
486  requestInfo.getSession().actualizeAllCachedSettings();
487  }
488  }
489 
490  return errorOccurred;
491  } // persist
492 
493  /**
494  * Sets uri from value in annotation link parameters of given annotation, also
495  * recursively in nested annotations
496  *
497  * @param annot Given annotation
498  */
499  public static void setUriOfAnnotationsParams(Annotation annot){
500  List attrList = annot.getAttributes();
501  Iterator attrIt = attrList.iterator();
502  while(attrIt.hasNext()){ // For all attributes od Annotation
503  BaseAttribute attr = (BaseAttribute)attrIt.next();
504 
506  attr = AttributeManager.changeAttributeInstance(attr);
507  }
508 
509  //for all annotation links which have uri set to null or empty value
510  if(attr.getSimpleType().equals("AnnotationLink") && ((LinkedAnnotationAttribute)attr).getValue() != null){
511  if(((LinkedAnnotationAttribute)attr).getUri() == null || ((LinkedAnnotationAttribute)attr).getUri().isEmpty()){
512  Annotation value = (Annotation) ((LinkedAnnotationAttribute)attr).getValue();
513  ((LinkedAnnotationAttribute)attr).setUri(value.getURI());
514  }
515  }
516 
517  //recursively update nested annotations
518  if(attr.getSimpleType().equals("NestedAnnotation") && ((NestedAnnotationAttribute)attr).getValue() != null){
519  Annotation value = (Annotation) ((NestedAnnotationAttribute)attr).getValue();
521  }
522  }
523  }
524 
525  /**
526  * Updates complete informations about annotation in database
527  *
528  * @param em Entity manager
529  * @param annotation Updated annotation
530  * @param requestInfo Informations about client request
531  */
532  public static void updateAnnot(EntityManager em, Annotation annotation, RequestInfo requestInfo) throws RuntimeException {
533  Query query = em.createNamedQuery("Annotation.findById");
534  query.setParameter("id", annotation.getId());
535  List aList = query.getResultList();
536  if (aList == null || aList.isEmpty()) {
538  String msg = "Updated annotation was not found by the Persister.";
539  Logger.getLogger(Persister.class.getName()).log(Level.SEVERE, msg);
540  }
541  throw new RuntimeException("Object not found.");
542  }
543  Annotation a = (Annotation) aList.get(0);
544  // update fragments
545  List<Fragment> fragments = a.getFragments();
546  Iterator<Fragment> frIter = fragments.iterator();
547  Iterator<Fragment> newFrIter = annotation.getFragments().iterator();
548  while (frIter.hasNext()) {
549  Fragment fr = frIter.next();
550  if (!newFrIter.hasNext()) {
551  em.remove(fr);
552  } else {
553  Fragment newFr = newFrIter.next();
554  newFr.setId(fr.getId());
555  }
556  }
557  // update attributes
558  List attrs = a.getAttributes();
559  List newAttrs = annotation.getAttributes();
560  Iterator attrsIt = attrs.iterator();
561 
562  // find already saved nested annotations
563  HashMap<Integer, Annotation> nestedAnnots = new HashMap<Integer, Annotation>();
564  ArrayList<Integer> updatedNested = new ArrayList<Integer>();
565  ArrayList<BaseAttribute> newNested = new ArrayList<BaseAttribute>();
566 
567  while (attrsIt.hasNext()) {
568  BaseAttribute attr = (BaseAttribute) attrsIt.next();
569  if (attr.getNestedAnnotation() != null) {
570  nestedAnnots.put(attr.getNestedAnnotation().getId(), attr.getNestedAnnotation());
571  }
572 
573  // delete additional atributes in entity attribute
574  /*if(attr.getEntityAdditionalAttributes() != null){
575  Iterator<EntityAdditionalAttribute> additionalAttrIt = attr.getEntityAdditionalAttributes().iterator();
576  while(additionalAttrIt.hasNext()){
577  em.remove(additionalAttrIt);
578  }
579  }*/
580  }
581 
582  // update tmpId links in attributes
583  saveTmpIdRefAnnotations(annotation, requestInfo, em);
584 
585  attrsIt = attrs.iterator();
586  Iterator newAttrsIt = newAttrs.iterator();
587 
588  // delete old attributes
589  while (attrsIt.hasNext()) {
590  BaseAttribute attr = (BaseAttribute) attrsIt.next();
591  attr.setNestedAnnotation(null);
592  em.remove(attr);
593  }
594  ArrayList<BaseAttribute> empty = new ArrayList<BaseAttribute>();
595  a.setAttributes(empty);
596  // delete
597 
598 
599  // delete attributes id
600  while (newAttrsIt.hasNext()) {
601  BaseAttribute attr = (BaseAttribute) newAttrsIt.next();
602  attr.setId(null);
603  }
604 
605  // set iterator again
606  newAttrsIt = newAttrs.iterator();
607 
608  // save new attributes
609  while (newAttrsIt.hasNext()) {
610  BaseAttribute newAttr = (BaseAttribute) newAttrsIt.next();
611  if (newAttr.getNestedAnnotation() != null) {
612  if(nestedAnnots.containsKey(newAttr.getNestedAnnotation().getId())) {
613  Annotation lost = nestedAnnots.get(newAttr.getNestedAnnotation().getId());
614  updatedNested.add(lost.getId());
615  newAttr.getNestedAnnotation().setId(lost.getId());
616  updateAnnot(em, newAttr.getNestedAnnotation(), requestInfo);
617  newAttr.getNestedAnnotation().setNestedInAnnot(a);
618  } else {
619  newAttr.getNestedAnnotation().setNestedInAnnot(a);
620  newNested.add(newAttr);
621  }
622  }
623 
624  // persist additional atributes in entity attribute
625  /*if(newAttr.getEntityAdditionalAttributes() != null && !newAttr.getEntityAdditionalAttributes().isEmpty()){
626  ArrayList<EntityAdditionalAttribute> newAdditionalAttrs = new ArrayList<EntityAdditionalAttribute>();
627  Iterator<EntityAdditionalAttribute> additionalAttrIt = attr.getEntityAdditionalAttributes().iterator();
628  while(additionalAttrIt.hasNext()){
629  EntityAdditionalAttribute savedAttr = em.persist(additionalAttrIt);
630  }
631  }*/
632  }
633 
634  // remove lost nested annotations
635  Iterator<Entry<Integer,Annotation>> lIter = nestedAnnots.entrySet().iterator();
636  while (lIter.hasNext()) {
637  Annotation lAnnot = lIter.next().getValue();
638  if(!updatedNested.contains(lAnnot.getId())){
639  requestInfo.getFlier().AddRemovedNestedAnnotation(lAnnot);
640  em.remove(lAnnot);
641  }
642  }
643  // merge new nested annotations in attributes
644  // (to set their id it must be done here)
645  Iterator<BaseAttribute> newNestedIt = newNested.iterator();
646  while (newNestedIt.hasNext()) {
647  BaseAttribute newNestedAt = newNestedIt.next();
648  if (newNestedAt.getNestedAnnotation() != null) {
649  String nNUri = AppBean.getBaseUri() + "/temp/" + newNestedAt.getNestedAnnotation().getTmpId();
650  Annotation newN = em.merge(newNestedAt.getNestedAnnotation());
651  newNestedAt.setNestedAnnotation(newN);
652  em.flush();
653  requestInfo.getFlier().addJustAddedAnnotation(nNUri, newN.getURIV2());
654  requestInfo.getFlier().getAddedAnnotations().add(newN);
655  }
656  }
657 
658  // check if we are updating nestedAnnotation and update nestedin reference
659  // this code shouldn't be here but it fixes client bug with only nested update
660  if(annotation.getNestedInAnnot() == null && a.getNestedInAnnot() != null){
661  annotation.setNestedInAnnot(a.getNestedInAnnot());
662 
663  ArrayList<Annotation> editedInFlier = requestInfo.getFlier().getEditedAnnotations();
664  if(editedInFlier.contains(annotation)){
665  editedInFlier.remove(annotation);
666  Annotation parent = a.getNestedInAnnot();
667  while(parent.getNestedInAnnot() != null){
668  parent = parent.getNestedInAnnot();
669  }
670  editedInFlier.add(parent);
671  }
672  }
673  // update nestedin reference
674 
675  // merge Annotation
676  Annotation mergenAn = em.merge(annotation);
677  em.flush();
678  annotation.setAttributes(new ArrayList<BaseAttribute>(mergenAn.getAttributes()));
679  } // updateAnnot()
680 
681  /**
682  * Query database for type of annotation
683  *
684  * @param uri URI of type of annotation
685  * @param em Entity manager
686  * @return Returns type of annotation with given URI
687  * @throws RuntimeException If type of annotation was not found, throws exception
688  */
689  private static AnnotType queryType(String uri, EntityManager em) throws RuntimeException {
690  AnnotType annotType = null;
691  Object[] params = new Object[2];
692  params[0] = "uri";
693  params[1] = uri;
694  Query q = em.createNamedQuery("AnnotType.findByUri");
695  for (int p = 0; p < params.length; p = p + 2) {
696  q.setParameter((String) params[p], params[p + 1]);
697  }
698  List aList = q.getResultList();
699  if (aList != null && !aList.isEmpty()) { // if type was found
700  annotType = (AnnotType) aList.get(0);
701  } else { // if type was not found (merge failed)
703  String msg = "Queried type of annotation was not found by the Persister: " + uri;
704  Logger.getLogger(Persister.class.getName()).log(Level.SEVERE, msg);
705  }
706  throw new RuntimeException("New type not found!");
707  }
708  return annotType;
709  } // queryType()
710 
711  /**
712  * Persist informations about automatically updated annotations
713  *
714  * @param requestInfo Informations about client request
715  * @return If succeed, returns false, in case of error returns true
716  */
717  public static boolean persistAUpdAnnot(RequestInfo requestInfo) {
718  Flier flier = requestInfo.getFlier();
719  EntityManager em = AppBean.getPersistenceManager().getEM();
720  EntityTransaction transaction = em.getTransaction();
721  boolean errorOccurred = false;
722  try {
723  transaction.begin();
724 
725  Iterator<Annotation> autoUpdAnnotIter = flier.getAutoUpdatedAnnotations().iterator();
726  while (autoUpdAnnotIter.hasNext()) { // for each automatically updated annotation
727  Annotation annotation = autoUpdAnnotIter.next();
728  if (annotation.getAnnotType().getId() == null) {
729  // if it's a new type of annotation, it must be set again after merge
730  AnnotType annotType = queryType(annotation.getAnnotType().getUri(), em);
731  annotation.setAnnotType(annotType);
732  // type of the nested and linked attribute also has to be updated
733  Iterator<BaseAttribute> attIt = annotation.getAttributes().iterator();
734  while (attIt.hasNext()) {
735  BaseAttribute att = attIt.next();
736  // nested and linked attributes must have type set
737  if (att instanceof NestedAnnotationAttribute || att instanceof LinkedAnnotationAttribute) {
738  // second parameter of the query is the uri of the attribute type
739  if (att.getAttributeType() == null) {
740  continue;
741  }
742  Object[] params = new Object[2];
743  params[0] = "uri";
744  params[1] = att.getAttributeType().getUri();
745  Query attTypeQuery = em.createNamedQuery("AnnotType.findByUri");
746  attTypeQuery.setParameter((String) params[0], params[1]);
747  // search in the database for the type with given uri
748  List typeList = attTypeQuery.getResultList();
749  // if the type's been found
750  if (typeList != null && !typeList.isEmpty()) {
751  // set attribute type to the one from the database
752  att.setAttributeType((AnnotType)typeList.get(0));
753  }
754  }
755  }
756  }
757  updateAnnot(em, annotation, requestInfo);
758  } // for each automatically updated annotation
759 
760  em.flush();
761  transaction.commit();
762  } catch (Exception e) {
763  transaction.rollback();
765  String msg = "Exception during the persisting of automatically updated annotations.";
766  Logger.getLogger(Persister.class.getName()).log(Level.SEVERE, msg, e);
767  }
768  errorOccurred = true;
769  } finally {
770  em.close();
771  }
772 
773  return errorOccurred;
774  } // persistAUpdAnnot
775 
776  /**
777  * Updates types in nested annotations (use types stored in DB)
778  *
779  * @param annotation Annotation, which attributes will be updated
780  * @param em Entity manager
781  */
782  private static void updateTypesInNested(Annotation annotation, EntityManager em) {
783  Iterator<BaseAttribute> aIt = annotation.getAttributes().iterator();
784  while (aIt.hasNext()) { // for each attribute
785  BaseAttribute attribute = aIt.next();
786  Annotation nested = attribute.getNestedAnnotation();
787  if (nested != null) { // if nested annotation is included
788  if (nested.getAnnotType().getId() == null) { // if it is a new type
789  // if it's a new type of annotation, it must be set again after merge
790  AnnotType annotType = queryType(nested.getAnnotType().getUri(), em);
791  nested.setAnnotType(annotType);
792  // type of the nested and linked attribute also has to be updated
793  Iterator<BaseAttribute> attIt = annotation.getAttributes().iterator();
794  while (attIt.hasNext()) {
795  BaseAttribute att = attIt.next();
796  // nested and linked attributes must have type set
797  if (att instanceof NestedAnnotationAttribute || att instanceof LinkedAnnotationAttribute) {
798  // second parameter of the query is the uri of the attribute type
799  if (att.getAttributeType() == null) {
800  continue;
801  }
802  Object[] params = new Object[2];
803  params[0] = "uri";
804  params[1] = att.getAttributeType().getUri();
805  Query attTypeQuery = em.createNamedQuery("AnnotType.findByUri");
806  attTypeQuery.setParameter((String) params[0], params[1]);
807  // search in the database for the type with given uri
808  List typeList = attTypeQuery.getResultList();
809  // if the type's been found
810  if (typeList != null && !typeList.isEmpty()) {
811  // set attribute type to the one from the database
812  att.setAttributeType((AnnotType)typeList.get(0));
813  }
814  }
815  }
816  } // if it is a new type
817  updateTypesInNested(nested, em); // update nested annotations recursively
818  } // if nested annotation is included
819  } // for each attribute
820  } // updateTypesInNested()
821 
822  /**
823  * Recursively saves annotations in attributes of given annotation.
824  * @param annot Annotation for reference update
825  * @param requestInfo Informations about client request
826  * @param em current EntityManager
827  * @return uri of the new saved annotation
828  * @throws RuntimeException
829  */
830  private static Annotation saveTmpIdRefAnnotations(Annotation annot, RequestInfo requestInfo, EntityManager em) throws RuntimeException {
831  Annotation saved = null;
832  List attrList = annot.getAttributes();
833  Iterator attrIt = attrList.iterator();
834  while(attrIt.hasNext()){ // For all attributes of Annotation
835  BaseAttribute attr = (BaseAttribute)attrIt.next();
836  if(attr.getSimpleType().equals("AnnotationLink") && ((LinkedAnnotationAttribute)attr).getValue() != null){
837  Annotation val = (Annotation) ((LinkedAnnotationAttribute)attr).getValue();
838  if (val.getId() == null) {
839  Annotation annot4update = saveTmpIdRefAnnotations(val, requestInfo, em);
840  attr.setValue(annot4update);
841  if(annot4update != null){
842  attr.setUri(annot4update.getURI());
843  attr.setAttributeType(annot4update.getAnnotType());
844  }
845  }
846  } else if(attr.getSimpleType().equals("NestedAnnotation") && attr.getNestedAnnotation() != null){
847  saveTmpIdRefAnnotations(attr.getNestedAnnotation(), requestInfo, em);
848  }
849  }
850  if(annot.getId() == null && annot.getNestedInAnnot() == null){
851  updateTypes(annot, em, "New type of annotation for tmpId linked annotation was not found by Persister: ");
852  updateTypesInNested(annot, em);
853  saved = em.merge(annot);
854  em.flush();
855  saved.setTmpId(annot.getTmpId());
856  updateTmpIdRefInAddedEditedASuggested(annot.getTmpId(), saved, requestInfo);
857  }
858 
859  return saved;
860  }
861 
862  /**
863  * Updates links in saved annotations
864  *
865  * @param requestInfo Informations about client request
866  * @param em current EntityManager
867  * @param justSaved List of just saved annotations (versions before saving)
868  * @param nowSaved List of just saved annotations (really saved versions)
869  * @param nestedBeforeSave List of just saved nested annotations (versions before saving)
870  * @param nestedAfterSave List of just saved nested annotations (really saved versions)
871  * @param unSavedLinks HashMap of arraylist of unsaved links for annotation
872  * @throws RuntimeException
873  */
874  private static void saveTmpIdRefAnnotationsSolveLinks(RequestInfo requestInfo, EntityManager em,
875  ArrayList<Annotation> justSaved, ArrayList<Annotation> nowSaved,
876  ArrayList<Annotation> nestedBeforeSave, ArrayList<Annotation> nestedAfterSave,
877  HashMap<Annotation,ArrayList<BaseAttribute>> unSavedLinks) throws RuntimeException {
878  //update links
879  Iterator<Entry<Annotation,ArrayList<BaseAttribute>>> unsavedIt = unSavedLinks.entrySet().iterator();
880  while(unsavedIt.hasNext()){
881  Entry<Annotation,ArrayList<BaseAttribute>> entry = unsavedIt.next();
882  Annotation solvedAnnot = entry.getKey();
883  Iterator<BaseAttribute> solvedLinksIt = entry.getValue().iterator();
884 
885  Integer nowSavedIndex = null;
886  Integer nestedIndex = null;
887  // saved nested annotations - we have to find real reference
888  if(solvedAnnot.getId() == null){
889  if(nestedBeforeSave.contains(solvedAnnot)){
890  nestedIndex = nestedBeforeSave.indexOf(solvedAnnot);
891  solvedAnnot = nestedAfterSave.get(nestedIndex);
892  }
893  else{
894  //should never happen
895  solvedAnnot = null;
896  }
897  }else{
898  nowSavedIndex = nowSaved.indexOf(solvedAnnot);
899  }
900 
901  if(solvedAnnot != null){
902  //save new link attributes for annotation
903  while(solvedLinksIt.hasNext()){
904  BaseAttribute attr = (BaseAttribute) solvedLinksIt.next();
905  Annotation link = (Annotation) ((LinkedAnnotationAttribute)attr).getValue();
906  attr.setRefAnnotation(solvedAnnot);
907  if(justSaved.contains(link)){
908  int index = justSaved.indexOf(link);
909  Annotation annot4update = nowSaved.get(index);
910  attr.setValue(annot4update);
911  if(annot4update != null){
912  attr.setUri(annot4update.getURI());
913  attr.setAttributeType(annot4update.getAnnotType());
914  }
915  }
916  else if(nowSaved.contains(link) || nestedAfterSave.contains(link)){
917  attr.setValue(link);
918  if(link != null){
919  attr.setUri(link.getURI());
920  attr.setAttributeType(link.getAnnotType());
921  }
922  }
923  else if(nestedBeforeSave.contains(link)){
924  int index = nestedBeforeSave.indexOf(link);
925  Annotation annot4update = nestedAfterSave.get(index);
926  attr.setValue(annot4update);
927  if(annot4update != null){
928  attr.setUri(annot4update.getURI());
929  attr.setAttributeType(annot4update.getAnnotType());
930  }
931  }
932  else{
933  //should never happen
934  attr.setValue(null);
935  }
936  solvedAnnot.addAttribute(attr);
937  }
938  //merge new links
939  Annotation afterMerge = em.merge(solvedAnnot);
940  updateTmpIdRefInAddedEditedASuggested(solvedAnnot.getTmpId(), afterMerge, requestInfo);
941 
942  //updates saved lists
943  if(nowSavedIndex != null){
944  nowSaved.set((int)nowSavedIndex, afterMerge);
945  }
946  else if(nestedIndex != null){
947  nestedAfterSave.set((int)nestedIndex, afterMerge);
948  }
949  }
950  }
951  //update links
952 
953  //update ConfirmedVersions
954  Iterator<SuggestionLogEntry> confirmedIt = requestInfo.getConfirmedSuggestions().iterator();
955  while(confirmedIt.hasNext()){
956  SuggestionLogEntry sugLog = confirmedIt.next();
957  if(nestedBeforeSave.contains(sugLog.getConfirmedVersion())){
958  int index = nestedBeforeSave.indexOf(sugLog.getConfirmedVersion());
959  sugLog.setConfirmedVersion(nestedAfterSave.get(index));
960  }
961  }
962  //update ConfirmedVersions
963  }
964 
965  /**
966  * Recursively saves annotations in attributes of given annotation.
967  * @param annot Annotation for reference update
968  * @param requestInfo Informations about client request
969  * @param em current EntityManager
970  * @param justSaved List of just saved annotations (versions before saving)
971  * @param nowSaved List of just saved annotations (really saved versions)
972  * @param nested4Links temporary list for nested annotation links
973  * @param nested4LinksFromDB temporary list for saved nested annotation links
974  * @param unSavedLinks HashMap of arraylist of unsaved links for annotation
975  * @return uri of the new saved annotation
976  * @throws RuntimeException
977  */
978  private static Annotation saveTmpIdRefAnnotations(Annotation annot, RequestInfo requestInfo, EntityManager em,
979  ArrayList<Annotation> justSaved, ArrayList<Annotation> nowSaved,
980  ArrayList<Annotation> nested4Links, ArrayList<Annotation> nested4LinksFromDB,
981  HashMap<Annotation,ArrayList<BaseAttribute>> unSavedLinks) throws RuntimeException {
982  Annotation saved = null;
983  ArrayList<BaseAttribute> annotLinks = new ArrayList<BaseAttribute>();
984  List attrList = annot.getAttributes();
985  Iterator attrIt = attrList.iterator();
986  while(attrIt.hasNext()){ // For all attributes of Annotation
987  BaseAttribute attr = (BaseAttribute)attrIt.next();
988  if(attr.getSimpleType().equals("AnnotationLink") && ((LinkedAnnotationAttribute)attr).getValue() != null){
989  Annotation val = (Annotation) ((LinkedAnnotationAttribute)attr).getValue();
990  //if we have some link on already saved annotation then don't save
991  if (nowSaved.contains(val) || nested4LinksFromDB.contains(val) || val.getId() == null) {
992  annotLinks.add(attr);
993  attrIt.remove();
994  }
995  } else if(attr.getSimpleType().equals("NestedAnnotation") && attr.getNestedAnnotation() != null){
996  nested4Links.add(attr.getNestedAnnotation());
997  saveTmpIdRefAnnotations(attr.getNestedAnnotation(), requestInfo, em, justSaved, nowSaved, nested4Links, nested4LinksFromDB, unSavedLinks);
998  }
999  }
1000  if(annot.getId() == null && annot.getNestedInAnnot() == null){
1001  updateTypes(annot, em, "New type of annotation for tmpId linked annotation was not found by Persister: ");
1002  updateTypesInNested(annot, em);
1003  saved = em.merge(annot);
1004  em.flush();
1005  saved.setTmpId(annot.getTmpId());
1006  justSaved.add(annot);
1007  nowSaved.add(saved);
1008  requestInfo.getFlier().addJustAddedAnnotation(AppBean.getBaseUri() + "/temp/" + annot.getTmpId(), saved.getURIV2());
1009  //add new reference on nested annotations
1010  addNewReferenceOnNestedAnnotation(saved, nested4LinksFromDB);
1011  updateTmpIdRefInAddedEditedASuggested(annot.getTmpId(), saved, requestInfo);
1012  }
1013 
1014  //add links for future update
1015  if(unSavedLinks != null && !annotLinks.isEmpty()){
1016  if(saved != null){
1017  unSavedLinks.put(saved, annotLinks);
1018  }
1019  else{
1020  unSavedLinks.put(annot, annotLinks);
1021  }
1022  }
1023 
1024  return saved;
1025  } // saveTmpIdAnnotations
1026 
1027  /**
1028  * Add new reference on nested annotations recursively to list
1029  * @param saved Annotation with nested
1030  * @param nested4LinksFromDB List of new references
1031  */
1032  private static void addNewReferenceOnNestedAnnotation(Annotation saved, ArrayList<Annotation> nested4LinksFromDB) {
1033  Iterator<BaseAttribute> nowSavedAttrIt = saved.getAttributes().iterator();
1034  while(nowSavedAttrIt.hasNext()){
1035  BaseAttribute savedAttribute = nowSavedAttrIt.next();
1036  if(savedAttribute.getSimpleType().equals("NestedAnnotation") && savedAttribute.getNestedAnnotation() != null){
1037  Annotation nestedAnnotation = savedAttribute.getNestedAnnotation();
1038  nested4LinksFromDB.add(nestedAnnotation);
1039  addNewReferenceOnNestedAnnotation(nestedAnnotation, nested4LinksFromDB);
1040  }
1041  }
1042  }
1043  /**
1044  * Method updates types in Annotation
1045  *
1046  * @param annotation annotation for update
1047  * @param em current EntityManager
1048  * @param logMsg Message which should be used for logging.
1049  * @throws RuntimeException
1050  */
1051  private static void updateTypes(Annotation annotation, EntityManager em, String logMsg) throws RuntimeException {
1052  // if it's a new type of annotation, it must be set again after merge
1053  AnnotType annotType = null;
1054  Object[] params = new Object[2];
1055  params[0] = "uri";
1056  params[1] = annotation.getAnnotType().getUri();
1057  Query q = em.createNamedQuery("AnnotType.findByUri");
1058  for (int p = 0; p < params.length; p = p + 2) {
1059  q.setParameter((String) params[p], params[p + 1]);
1060  }
1061  List aList = q.getResultList();
1062  if (aList != null && !aList.isEmpty()) { // if type was found
1063  annotType = (AnnotType) aList.get(0);
1064  annotation.setAnnotType(annotType);
1065  // type of the nested and linked attribute also has to be updated
1066  Iterator<BaseAttribute> attIt = annotation.getAttributes().iterator();
1067  while (attIt.hasNext()) {
1068  BaseAttribute att = attIt.next();
1069  // nested and linked attributes must have type set
1070  if (att instanceof NestedAnnotationAttribute || att instanceof LinkedAnnotationAttribute) {
1071  if (att.getAttributeType() == null) {
1072  continue;
1073  }
1074  // second parameter of the query is the uri of the attribute type
1075  params[1] = att.getAttributeType().getUri();
1076  Query attTypeQuery = em.createNamedQuery("AnnotType.findByUri");
1077  attTypeQuery.setParameter((String) params[0], params[1]);
1078  // search in the database for the type with given uri
1079  List typeList = attTypeQuery.getResultList();
1080  // if the type's been found
1081  if (typeList != null && !typeList.isEmpty()) {
1082  // set attribute type to the one from the database
1083  att.setAttributeType((AnnotType)typeList.get(0));
1084  }
1085  }
1086  }
1087  } else { // if type was not found (merge failed)
1089  String msg = logMsg + annotation.getAnnotType().getUri();
1090  Logger.getLogger(Persister.class.getName()).log(Level.SEVERE, msg);
1091  }
1092  throw new RuntimeException("New type not found!");
1093  }
1094  } // updateTypes
1095 
1096  /**
1097  * Updates LinkedAnnotationAttribute references after saving new Annotations to DB
1098  * @param tmpId old tmpId link of Annotation
1099  * @param newLink new link of Annotation
1100  * @param requestInfo Informations about client request
1101  */
1102  private static void updateTmpIdRefInAddedEditedASuggested(String tmpId, Annotation newLink, RequestInfo requestInfo){
1103  Flier flier = requestInfo.getFlier();
1104 
1105  // Replace all links in added annotations.
1106  Iterator<Annotation> addedIt = flier.getAddedAnnotations().iterator();
1107  while(addedIt.hasNext()){
1108  Annotation annot = addedIt.next();
1109  Iterator attrIt = annot.getAttributes().iterator();
1110  while(attrIt.hasNext()){
1111  BaseAttribute attr = (BaseAttribute)attrIt.next();
1112  if(attr.getSimpleType().equals("AnnotationLink") && tmpId != null &&
1113  ((LinkedAnnotationAttribute)attr).getTmpId() != null &&
1114  ((LinkedAnnotationAttribute)attr).getTmpId().equals(tmpId)){
1115  attr.setValue(newLink);
1116  if(newLink != null){
1117  attr.setAttributeType(newLink.getAnnotType());
1118  }
1119  }
1120  else if(attr.getSimpleType().equals("NestedAnnotation") && attr.getNestedAnnotation() != null){
1121  updateTmpIdRefInNested(tmpId, attr.getNestedAnnotation(), newLink);
1122  }
1123  }
1124  }
1125 
1126  // Replace all links in edited annotations
1127  Iterator<Annotation> editedIt = flier.getEditedAnnotations().iterator();
1128  while(editedIt.hasNext()){
1129  Annotation annot = editedIt.next();
1130  Iterator attrIt = annot.getAttributes().iterator();
1131  while(attrIt.hasNext()){
1132  BaseAttribute attr = (BaseAttribute)attrIt.next();
1133  if(attr.getSimpleType().equals("AnnotationLink") && tmpId != null &&
1134  ((LinkedAnnotationAttribute)attr).getTmpId() != null &&
1135  ((LinkedAnnotationAttribute)attr).getTmpId().equals(tmpId)){
1136  attr.setValue(newLink);
1137  if(newLink != null){
1138  attr.setAttributeType(newLink.getAnnotType());
1139  }
1140  }
1141  else if(attr.getSimpleType().equals("NestedAnnotation") && attr.getNestedAnnotation() != null){
1142  updateTmpIdRefInNested(tmpId, attr.getNestedAnnotation(), newLink);
1143  }
1144  }
1145  }
1146 
1147  // Replace all links in confirmed suggestions
1148  Iterator<SuggestionLogEntry> suggIt = requestInfo.getConfirmedSuggestions().iterator();
1149  while(suggIt.hasNext()){
1150  SuggestionLogEntry suggest = suggIt.next();
1151  Annotation annot = suggest.getConfirmedVersion();
1152  Iterator attrIt = annot.getAttributes().iterator();
1153  while(attrIt.hasNext()){
1154  BaseAttribute attr = (BaseAttribute)attrIt.next();
1155  if(attr.getSimpleType().equals("AnnotationLink") && tmpId != null &&
1156  ((LinkedAnnotationAttribute)attr).getTmpId() != null &&
1157  ((LinkedAnnotationAttribute)attr).getTmpId().equals(tmpId)){
1158  attr.setValue(newLink);
1159  if(newLink != null){
1160  attr.setAttributeType(newLink.getAnnotType());
1161  }
1162  }
1163  else if(attr.getSimpleType().equals("NestedAnnotation") && attr.getNestedAnnotation() != null){
1164  updateTmpIdRefInNested(tmpId, attr.getNestedAnnotation(), newLink);
1165  }
1166  }
1167  }
1168 
1169  } // updateTmpIdRefInAddedEditedASuggested
1170 
1171 
1172  /**
1173  * Updates NestAnnotationAttribute references after saving new Annotations to DB
1174  * @param tmpId old tmpId link of Annotation
1175  * @param nestedAnn Nested Annotation link
1176  * @param newLink new link of Annotation
1177  */
1178  private static void updateTmpIdRefInNested(String tmpId, Annotation nestedAnn, Annotation newLink){
1179  Iterator<BaseAttribute> aIt = nestedAnn.getAttributes().iterator();
1180  while (aIt.hasNext()) { // for each attribute
1181  BaseAttribute attribute = aIt.next();
1182  if(attribute.getSimpleType().equals("AnnotationLink") && tmpId != null && !tmpId.isEmpty() &&
1183  ((LinkedAnnotationAttribute)attribute).getTmpId() != null &&
1184  ((LinkedAnnotationAttribute)attribute).getTmpId().equals(tmpId)){
1185  attribute.setValue(newLink);
1186  if(newLink != null){
1187  attribute.setAttributeType(newLink.getAnnotType());
1188  }
1189  }
1190  else if(attribute.getSimpleType().equals("NestedAnnotation") && attribute.getNestedAnnotation() != null){
1191  updateTmpIdRefInNested(tmpId, attribute.getNestedAnnotation(), newLink);
1192  }
1193  } // for each attribute
1194  } // updateTmpIdRefInNested
1195 
1196 } // class Persister
Static class which persists data to the database.
Definition: Persister.java:49
ArrayList< Subscription > getUpdatedSubscriptions()
static Annotation saveTmpIdRefAnnotations(Annotation annot, RequestInfo requestInfo, EntityManager em, ArrayList< Annotation > justSaved, ArrayList< Annotation > nowSaved, ArrayList< Annotation > nested4Links, ArrayList< Annotation > nested4LinksFromDB, HashMap< Annotation, ArrayList< BaseAttribute >> unSavedLinks)
Definition: Persister.java:978
Class representing attribute of type of annotation.
static boolean persist(RequestInfo requestInfo)
Definition: Persister.java:58
Singleton for storing global variables.
Definition: AppBean.java:47
static void updateTypesInNested(Annotation annotation, EntityManager em)
Definition: Persister.java:782
static void updateTmpIdRefInNested(String tmpId, Annotation nestedAnn, Annotation newLink)
static void setUriOfAnnotationsParams(Annotation annot)
Definition: Persister.java:499
Suggested annotation with informations about suggestion.
Class representing user group.
Definition: UserGroup.java:47
Class representing parameter of user settings.
Definition: Settings.java:45
static boolean persistAUpdAnnot(RequestInfo requestInfo)
Definition: Persister.java:717
static void addNewReferenceOnNestedAnnotation(Annotation saved, ArrayList< Annotation > nested4LinksFromDB)
Base class representing attribute of annotation.
static AnnotType queryType(String uri, EntityManager em)
Definition: Persister.java:689
Class representing type of annotation.
Definition: AnnotType.java:58
static void saveTmpIdRefAnnotationsSolveLinks(RequestInfo requestInfo, EntityManager em, ArrayList< Annotation > justSaved, ArrayList< Annotation > nowSaved, ArrayList< Annotation > nestedBeforeSave, ArrayList< Annotation > nestedAfterSave, HashMap< Annotation, ArrayList< BaseAttribute >> unSavedLinks)
Definition: Persister.java:874
Class representing user.
Definition: User.java:51
Flier with informations for comet handlers.
Definition: Flier.java:31
ArrayList< Subscription > getDeletedSubscriptions()
static void updateTypes(Annotation annotation, EntityManager em, String logMsg)
Class attribute manager provides a way how to create new attributes.
Processed informations about client request.
static void updateAnnot(EntityManager em, Annotation annotation, RequestInfo requestInfo)
Definition: Persister.java:532
ArrayList< Subscription > getNewSubscriptions()
ArrayList< AnnotType > getAddedTypes()
Definition: Flier.java:101
Class representing annotated fragment.
Definition: Fragment.java:48
Class representing source of subscription.
Definition: Source.java:47
static Annotation saveTmpIdRefAnnotations(Annotation annot, RequestInfo requestInfo, EntityManager em)
Definition: Persister.java:830
static void updateTmpIdRefInAddedEditedASuggested(String tmpId, Annotation newLink, RequestInfo requestInfo)