4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
SugPersister.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: SugPersister.java
5  * Description: Static class which persists data to the database
6  */
7 
8 /**
9  * @file SugPersister.java
10  *
11  * @brief Static class which persists data to the database
12  */
13 
14 package cz.vutbr.fit.knot.annotations.modules.suggestionManager;
15 
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.logging.Level;
39 import java.util.logging.Logger;
40 import javax.persistence.EntityManager;
41 import javax.persistence.EntityTransaction;
42 import javax.persistence.Query;
43 
44 /**
45  * Static class which persists suggestion data to the database
46  *
47  * @brief Static class which persists suggestion data to the database
48  */
49 public class SugPersister {
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  * @param addedAnnotations Added Annotations
57  * @param editedAnnotations Edited Annotations
58  * @param NERsuggestions suggestionsFromNER
59  * @param solveFeedbacks If I should update suggestion feedback and confidence
60  * @param addedTypes Added types
61  * @return If succeed, returns false, in case of error returns true
62  */
63  public static ArrayList<Suggestion> persist(RequestInfo requestInfo, ArrayList<Annotation> addedAnnotations, ArrayList<Annotation> editedAnnotations,
64  ArrayList<Suggestion> NERsuggestions, boolean solveFeedbacks, ArrayList<AnnotType> addedTypes)
65  {
66  ArrayList<Annotation> requestAnnotations = null;
67  if (addedAnnotations != null || editedAnnotations != null) {
68  requestAnnotations = new ArrayList<Annotation>();
69  if (addedAnnotations != null) {
70  requestAnnotations.addAll(addedAnnotations);
71  }
72  if (editedAnnotations != null) {
73  requestAnnotations.addAll(editedAnnotations);
74  }
75  }
76 
77  EntityManager em = AppBean.getPersistenceManager().getEM();
78  EntityTransaction transaction = em.getTransaction();
79  boolean errorOccurred = false;
80  ArrayList<Suggestion> touchedSuggestions = new ArrayList<Suggestion>();
81  try {
82  transaction.begin();
83 
84  /* Persist types */
85  // create list without attributes and ansestors and map with original types
86  ArrayList<AnnotType> nTWA = new ArrayList<AnnotType>();
87  Map<String, AnnotType> nTMap = new HashMap<String, AnnotType>();
88  for (Iterator<AnnotType> ntIt = addedTypes.iterator(); ntIt.hasNext();) {
89  AnnotType type = ntIt.next();
90  // skip type, that is stored in DB
91  if (type.getId() != null) {
92  continue;
93  }
94  AnnotType nT = new AnnotType(type.getUri(), type.getName(), null, type.getGroup());
95  nT.setRestrictedAtt(type.getRestrictedAtt());
96  nT.setUriInOntology(type.getUriInOntology());
97  nT.setComment(type.getComment());
98  nTWA.add(nT);
99  nTMap.put(type.getUri(), type);
100  }
101 
102  // merge types without attributes and create map with merged types
103  ArrayList<AnnotType> newCreatedTypes = new ArrayList<AnnotType>();
104  ArrayList<AnnotType> mergedTypes = new ArrayList<AnnotType>();
105  Map<String, AnnotType> mTypesMap = new HashMap<String, AnnotType>();
106  for (Iterator<AnnotType> nTWAIt = nTWA.iterator(); nTWAIt.hasNext();) {
107  AnnotType type = nTWAIt.next();
108  // skip type, that is stored in DB
109  if (type.getId() != null) {
110  continue;
111  }
112  type = em.merge(type);
113  mergedTypes.add(type);
114  mTypesMap.put(type.getUri(), type);
115  }
116 
117  // fix, add and merge attributes and ancestors
118  for (Iterator<AnnotType> mIt = mergedTypes.iterator(); mIt.hasNext();) {
119  AnnotType mType = mIt.next();
120  AnnotType oType = nTMap.get(mType.getUri()); // get original type
121  // skip type, that is stored in DB
122  if (oType == null || mType == null){
123  continue;
124  }
125  // attributes
126  List<AnnotTypeAttr> aList = oType.getAttributes();
127  for (Iterator<AnnotTypeAttr> aIt = aList.iterator(); aIt.hasNext();) {
128  AnnotTypeAttr aTA = aIt.next();
129  AnnotType aTAAT = aTA.getAttributeType();
130  if (aTAAT != null) { // if it is structured attribute
131  // replace type with merged one
132  AnnotType rType = mTypesMap.get(aTAAT.getUri());
133  if (rType == null) { // it is not new type, it is in database
134  rType = queryType(aTAAT.getUri(), em);
135  }
136  aTA.setAttributeType(rType);
137  }
138  // fix back reference to annotation type
139  aTA.setAnnotationType(mTypesMap.get(aTA.getAnnotationType().getUri()));
140  }
141  mType.setAttributes(new ArrayList<AnnotTypeAttr>(aList));
142 
143  // primary ancestor
144  if (oType.getAncestorType() != null) {
145  // replace type with merged one
146  AnnotType rType = mTypesMap.get(oType.getAncestorType().getUri());
147  if (rType == null) { // it is not new type, it is in database
148  rType = queryType(oType.getAncestorType().getUri(), em);
149  }
150  // skip type, that is stored in DB
151  if (rType != null && rType.getId() == null){
152  mType.setAncestorType(rType);
153  }
154  }
155 
156  // ancestors
157  ArrayList<AnnotType> newAncestors = new ArrayList<AnnotType>();
158  if (oType.getAncestorTypes() != null) {
159  for (Iterator<AnnotType> ancIt = oType.getAncestorTypes().iterator(); ancIt.hasNext();) {
160  AnnotType ancType = ancIt.next();
161  AnnotType mAnc = mTypesMap.get(ancType.getUri());
162  if (mAnc == null) { // it is not new type, it is in database
163  mAnc = queryType(ancType.getUri(), em);
164  }
165  // skip type, that is stored in DB
166  if (mAnc != null && (ancType.getId() != null || mAnc.getId() != null)) {
167  continue;
168  }
169  newAncestors.add(mAnc);
170  }
171  }
172  mType.setAncestorTypes(newAncestors);
173 
174  mType = em.merge(mType);
175  newCreatedTypes.add(mType);
176  } // fix, add and merge attributes and ancestors
177 
178  // delete every type without id from addedTypes and add types with id
179  for (Iterator<AnnotType> it = addedTypes.iterator(); it.hasNext();) {
180  AnnotType type = it.next();
181  if (type.getId() == null) {
182  it.remove();
183  }
184  }
185  addedTypes.addAll(newCreatedTypes);
186 
187 
188  // get all confirmed suggestions
189  ArrayList<Suggestion> suggestions = new ArrayList<Suggestion>();
190  ArrayList<SuggestionLogEntry> confirmed = requestInfo.getConfirmedSuggestions();
191  ArrayList<Annotation> confirmedAnnotations = new ArrayList<Annotation>();
192  // Maps annotation on it's old suggestion, it will be used for
193  // linked attribute value update
194  HashMap<Annotation, Suggestion> anOldSugLinkMap = new HashMap<Annotation, Suggestion>();
195  // suggestion for annotation (that was edited) was like confirmed suggestions
196  if (editedAnnotations != null) {
197  for (Iterator<Annotation> it = editedAnnotations.iterator(); it.hasNext();) {
198  Annotation an = it.next();
199  Query query2 = em.createQuery("SELECT s FROM SuggestionFeedback s WHERE s.annotationId=?1 AND s.suggestionId IS NOT NULL");
200  query2.setParameter(1, an.getId());
201  List sList = query2.getResultList();
202  if (sList != null && !sList.isEmpty()) {
203  SuggestionFeedback sugFeed = (SuggestionFeedback) sList.get(0);
204  Suggestion s = sugFeed.getSuggestion();
205  s.setConfidence(0);
206  s = em.merge(s);
207  sugFeed.setAnnot(null);
208  em.merge(sugFeed);
209  setNestedZeroConfidence(em, an, touchedSuggestions);
210  anOldSugLinkMap.put(an, s);
211  if (!touchedSuggestions.contains(s)) {
212  touchedSuggestions.add(s);
213  }
214  }
215  }
216  }
217 
218  if(requestAnnotations != null){
219  if(confirmed != null && !confirmed.isEmpty()){
220  Iterator<SuggestionLogEntry> logIt = confirmed.iterator();
221  while(logIt.hasNext()){
222  SuggestionLogEntry actualLogItem = logIt.next();
223  Suggestion actualSuggestion = getSuggestionFromDB(Integer.parseInt(actualLogItem.getTmpId()), em);
224  if(actualSuggestion != null){
225  if (actualLogItem.getConfirmMethod() == Constants.CONFIRMED_MANUALLY_EDITED) {
226  actualSuggestion.setConfidence(0);
227  em.merge(actualSuggestion);
228  setNestedSugZeroConfidence(em, actualSuggestion, touchedSuggestions);
229  if (actualLogItem.getConfirmedVersion() != null && actualLogItem.getConfirmedVersion().getAnnotType() != null && actualLogItem.getConfirmedVersion().getAnnotType().getGroup() != null) {
230  createNewFeedback(em, null, actualSuggestion, actualLogItem.getConfirmedVersion().getAnnotType().getGroup());
231  }
232  if (!touchedSuggestions.contains(actualSuggestion)) {
233  touchedSuggestions.add(actualSuggestion);
234  }
235  } else {
236  suggestions.add(actualSuggestion);
237  confirmedAnnotations.add(actualLogItem.getConfirmedVersion());
238  }
239  }
240  }
241  }
242  }
243 
244 
245  // get all annotations which doesn't have suggestion and save their suggestions without nested and links
246  Iterator<Annotation> reqAnnotIt;
247  ArrayList<Annotation> nestedInReqAnnotations = new ArrayList<Annotation>();
248  HashMap<Integer, Suggestion> reqSuggs = new HashMap<Integer, Suggestion>();
249  // Maps annotation on it's new suggestion, it will be used for
250  // linked attribute value update
251  HashMap<Annotation, Suggestion> anNewSugLinkMap = new HashMap<Annotation, Suggestion>();
252  if(requestAnnotations != null){
253  reqAnnotIt = requestAnnotations.iterator();
254  while(reqAnnotIt.hasNext()){
255  Annotation annot = reqAnnotIt.next();
256  if(!confirmedAnnotations.contains(annot)){
257  saveSuggestionsWithoutLinks(requestInfo, em, annot, reqSuggs, nestedInReqAnnotations, touchedSuggestions, anNewSugLinkMap);
258  }
259  }
260  }
261  em.flush();
262 
263  // divide suggestions
264  ArrayList<Suggestion> sugForStore = new ArrayList<Suggestion>();
265  ArrayList<Suggestion> sugForActualization = new ArrayList<Suggestion>();
266  if(requestAnnotations != null){
267  for (Iterator<Suggestion> it = suggestions.iterator(); it.hasNext();) {
268  Suggestion sg = it.next();
269  if (sg.getId() == null) {
270  sugForStore.add(sg);
271  } else {
272  sugForActualization.add(sg);
273  }
274  }
275  }
276  else{
277  Iterator<Suggestion> sugListIt = NERsuggestions.iterator();
278  while(sugListIt.hasNext()){
279  Suggestion sug = sugListIt.next();
280 
281  List sList = null;
282  if (sug.getId() != null) {
283  Query query = em.createNamedQuery("Suggestion.findById");
284  query.setParameter("id", sug.getId());
285  sList = query.getResultList();
286  }
287 
288  if (sList == null || sList.isEmpty()) {
289  //new
290  sugForStore.add(sug);
291  }
292  else{
293  sugForActualization.add(sug);
294  }
295  }
296  }
297 
298  /* Persist suggestions */
299  ArrayList<Suggestion> nowSavedSugs = new ArrayList<Suggestion>();
300  ArrayList<Suggestion> justSavedSugs = new ArrayList<Suggestion>();
301  Iterator<Suggestion> addedSugIter = sugForStore.iterator();
302  while (addedSugIter.hasNext()) { // for each added suggestion
303  Suggestion sug = addedSugIter.next();
304  if (sug.getAnnotType() == null) {
305  continue;
306  }
307  if (sug.getAnnotType().getId() == null) {
308  updateTypes(sug, em, "New type of suggestion was not found by Persister: ");
309  }
310  updateTypesInNested(sug, em); // update types in nested suggestions
311  if (justSavedSugs.contains(sug)) { // skip just saved to avoid duplicity
312  continue;
313  }
314  // update tmpId links in attributes
315  saveTmpIdRefSuggestions(sug, requestInfo, em, justSavedSugs, nowSavedSugs);
316  } // for each added suggestion
317 
318  // change suggestion list
319  suggestions.clear();
320  if (NERsuggestions != null) {
321  NERsuggestions.clear();
322  }
323  for (Iterator<Suggestion> it = nowSavedSugs.iterator(); it.hasNext();) {
324  Suggestion sg = it.next();
325  suggestions.add(sg);
326  if (NERsuggestions != null) {
327  NERsuggestions.add(sg);
328  }
329  touchedSuggestions.add(sg);
330  }
331 
332  Iterator<Suggestion> editSuggIter = sugForActualization.iterator();
333  while (editSuggIter.hasNext()) {
334  Suggestion suggestion = editSuggIter.next();
335  if (suggestion.getAnnotType() == null || suggestion.getAnnotType().getId() == null) {
336  updateTypes(suggestion, em, "New type of annotation for edited suggestion was not found by Persister: ");
337  }
338  if(requestAnnotations != null){
339  updateSugg(em, suggestion, requestInfo, touchedSuggestions, solveFeedbacks);
340  }
341  else{
342  updateNerSugg(em, suggestion, touchedSuggestions, requestInfo);
343  }
344  if (!touchedSuggestions.contains(suggestion)) {
345  touchedSuggestions.add(suggestion);
346  }
347  if (NERsuggestions != null && !NERsuggestions.contains(suggestion)) {
348  NERsuggestions.add(suggestion);
349  }
350  }
351 
352  em.flush();
353 
354  if(requestAnnotations != null){
355  // do linked attributes of request annotations
356  reqAnnotIt = requestAnnotations.iterator();
357  while(reqAnnotIt.hasNext()){
358  updateLinksAndNested(reqAnnotIt.next(), reqSuggs, em, touchedSuggestions);
359  }
360 
361  // do linked attributes of nested in request annotations
362  Iterator<Annotation> nestedInReqIt = nestedInReqAnnotations.iterator();
363  while(nestedInReqIt.hasNext()){
364  updateLinksAndNested(nestedInReqIt.next(), reqSuggs, em, touchedSuggestions);
365  }
366  }
367 
368  // Update URI of linked annotations
369  Iterator<Suggestion>addeSuggIter = sugForStore.iterator();
370  while(addeSuggIter.hasNext()){
371  Suggestion actualSugg = addeSuggIter.next();
372  setUriOfSuggParams(actualSugg);
373  }
374 
375  Iterator<Suggestion>edSuggIter = sugForActualization.iterator();
376  while(edSuggIter.hasNext()){
377  Suggestion actualSugg = edSuggIter.next();
378  setUriOfSuggParams(actualSugg);
379  }
380 
381  // annotations link in DB that points to old suggestions will be update to point to new suggestions
382  Iterator oldSugIt = anOldSugLinkMap.entrySet().iterator();
383  // go trough map of edited annotations and corresponding suggestions from feedback
384  while (oldSugIt.hasNext()) {
385  Map.Entry pair = (Map.Entry)oldSugIt.next();
386  // find matching new annotations
387  if (anNewSugLinkMap.containsKey((Annotation)pair.getKey())) {
388  // get new and old suggestion for the given annotation
389  Suggestion newSug = anNewSugLinkMap.get((Annotation)pair.getKey());
390  Suggestion oldSug = (Suggestion)pair.getValue();
391 
392  // find attributes that links old suggestion
393  Query query = em.createNamedQuery("SuggestionAttribute.findByLinked");
394  query.setParameter("linked", oldSug.getId());
395 
396  @SuppressWarnings("unchecked")
397  List<SugBaseAttribute> attList = query.getResultList();
398  // linked attributes found in the DB
399  if (attList != null && !attList.isEmpty()) {
400  // update linked attributes from the database
401  Iterator<SugBaseAttribute> sugListIt = attList.iterator();
402  while (sugListIt.hasNext()) {
403  SugBaseAttribute sba = sugListIt.next();
404  // sla must be instance of SugLinkedAttribute
406  sba = SugAttributeManager.changeAttributeInstance(sba);
407  }
408  if (!(sba instanceof SugLinkedAttribute)) {
409  continue;
410  }
411  SugLinkedAttribute sla = (SugLinkedAttribute) sba;
412  // attribute's value is pointing to the old version
413  if (sla.getLinkedSuggestion() != null && sla.getLinkedSuggestion().getURI() != null
414  && oldSug.getURI() != null) {
415  // update the value
416  sla.setValue(newSug);
417  em.merge(sla);
418  } else {
420  String msg = "Wrong attribute.";
421  Logger.getLogger(SuggestionManager.class.getName()).log(Level.SEVERE, msg);
422  }
423  }
424  }
425  }
426  }
427  }
428 
429 
430  em.flush();
431  transaction.commit();
432  } catch (Exception e) {
433  transaction.rollback();
435  String msg = "Exception during the persisting of data.";
436  Logger.getLogger(SugPersister.class.getName()).log(Level.SEVERE, msg, e);
437  }
438  errorOccurred = true;
439  } finally {
440  em.close();
441  }
442 
443  if (errorOccurred && (!requestInfo.getJoinedGroups().isEmpty() || !requestInfo.getLeavedGroups().isEmpty())) {
444  // refresh user informations
445  Object[] params = new Object[2];
446  params[0] = "id";
447  params[1] = requestInfo.getSession().getUser().getId();
448  List uList = AppBean.getPersistenceManager().queryDB("User.findById", params);
449  if (uList != null && !uList.isEmpty()) {
450  requestInfo.getSession().setUser((User) uList.get(0));
451  requestInfo.getSession().actualizeAllCachedSettings();
452  }
453  }
454 
455  return touchedSuggestions;
456  } // persist()
457 
458  /**
459  * Update suggestion from NER
460  *
461  * @param em Entity Manager
462  * @param sug Suggestion
463  * @param touchedSug Changed suggestions
464  * @param request information about client request
465  */
466  public static void updateNerSugg(EntityManager em, Suggestion sug, ArrayList<Suggestion> touchedSug, RequestInfo request){
467  Query query = em.createNamedQuery("Suggestion.findById");
468  query.setParameter("id", sug.getId());
469  List sList = query.getResultList();
470 
471  if (sList == null || sList.isEmpty()) {
472  //new
473  sug = em.merge(sug);
474  }
475  else{
476  Suggestion s = (Suggestion) sList.get(0);
477  // update fragments
478  List<SuggestionFragment> fragments = s.getFragments();
479  Iterator<SuggestionFragment> frIter = fragments.iterator();
480  Iterator<SuggestionFragment> newFrIter = sug.getFragments().iterator();
481  while (frIter.hasNext()) {
482  SuggestionFragment fr = frIter.next();
483  if (!newFrIter.hasNext()) {
484  em.remove(fr);
485  } else {
486  SuggestionFragment newFr = newFrIter.next();
487  newFr.setId(fr.getId());
488  }
489  }
490 
491  // update attributes
492  List attrs = s.getAttributes();
493  Iterator attrsIt = attrs.iterator();
494 
495  // find already saved nested suggestions
496  HashMap<Integer, Suggestion> nestedSugs = new HashMap<Integer, Suggestion>();
497 
498  while (attrsIt.hasNext()) {
499  SugBaseAttribute attr = (SugBaseAttribute) attrsIt.next();
500  if (attr.getNestedSuggestion() != null) {
501  nestedSugs.put(attr.getNestedSuggestion().getId(), attr.getNestedSuggestion());
502  }
503  }
504 
505  // update tmpId links in attributes
506  saveTmpIdRefSuggestions(sug, request, em);
507 
508  // Update of attributes is not implemented and not necessary as they are not changing.
509  // Only fragments and confidences should be changed here if somebody would like to upgrade it.
510 
511  // merge Suggestion
512  em.merge(sug);
513  em.flush();
514  }
515  } // updateNerSugg()
516 
517  /**
518  * Function saves Annotation as Suggestion into DB without linked attributes.
519  *
520  * @param requestInfo information about client request
521  * @param em Entity manager
522  * @param annot Annotation which is converted
523  * @param reqSuggs HasMap with Original Annotation ID as key and new Suggestion as value
524  * @param nestedInReqAnnotations Array with nested annotations
525  * @param touchedSuggestions Array with changed suggestions
526  * @param anNewSugLinkMap Annotation and new link map
527  * @return saved suggestion
528  */
529  public static Suggestion saveSuggestionsWithoutLinks(RequestInfo requestInfo, EntityManager em, Annotation annot, HashMap<Integer, Suggestion> reqSuggs,
530  ArrayList<Annotation> nestedInReqAnnotations, ArrayList<Suggestion> touchedSuggestions, HashMap<Annotation, Suggestion> anNewSugLinkMap)
531  {
532  Suggestion newSug = null;
533  Object[] params = new Object[2];
534  params[0] = "id";
535  params[1] = annot.getId();
536  Query q = em.createNamedQuery("SuggestionFeedback.findByAnnotNotNull");
537  for (int p = 0; p < params.length; p = p + 2) {
538  q.setParameter((String) params[p], params[p + 1]);
539  }
540  List sList = q.getResultList();
541  if (sList == null || sList.isEmpty()) {
542  newSug = new Suggestion(annot);
543  // remove all nested attributes from suggestion
544  if (newSug.getAttributes() != null) {
545  Iterator<SugBaseAttribute> newSugAttIt = newSug.getAttributes().iterator();
546  if (newSugAttIt != null) {
547  while (newSugAttIt.hasNext()) {
548  SugBaseAttribute sba = newSugAttIt.next();
549  if (sba.getSimpleType().contentEquals("NestedSuggestion")) {
550  newSugAttIt.remove();
551  }
552  }
553  }
554  }
555  newSug.setConfidence(SuggestionManager.CONFIRMED_CONFIDENCE);
556  List<BaseAttribute> attrs = annot.getAttributes();
557  Iterator<BaseAttribute> attrsIt = attrs.iterator();
558 
559  // delete linked attributes
560  if (newSug.getAttributes() != null) {
561  for (Iterator it = newSug.getAttributes().iterator(); it.hasNext();) {
562  Object attribute = it.next();
563  if (attribute instanceof SugLinkedAttribute /*|| attribute instanceof SugNestedAttribute*/) {
564  it.remove();
565  }
566  }
567  }
568 
569  // to all suggesiton in this list should be set NestedInSuggestion newSug
570  ArrayList<Suggestion> toAddNestedIn = new ArrayList<Suggestion>();
571  // create nested attributes again
572  while(attrsIt.hasNext()){
573  BaseAttribute attr = attrsIt.next();
574  if(attr.getSimpleType().equalsIgnoreCase("NestedAnnotation")){
575  // create nested again with value
576  if (((NestedAnnotationAttribute)attr).getValue() != null) {
577  Suggestion nested = saveSuggestionsWithoutLinks(requestInfo, em, (Annotation) ((NestedAnnotationAttribute)attr).getValue(),
578  reqSuggs, nestedInReqAnnotations, touchedSuggestions, anNewSugLinkMap);
579  if(nested != null){
580  Boolean unknownError = false;
581  SugBaseAttribute nestedAttr = null;
582  try{
583  nestedAttr = SugAttributeManager.createAttribute(attr.getName(), "NestedSuggestion", newSug);
584  nestedAttr.updateFromBaseAttribut(attr);
585  }
586  catch(ClassNotFoundException ex){
587  unknownError = true;
588  }
589  if(!unknownError){
590  if (attr.getAttributeType() != null) {
591  ((SugNestedAttribute) nestedAttr).setAttributeType(attr.getAttributeType());
592  }
593  nestedAttr.setNestedSuggestion(nested);
594  toAddNestedIn.add(nested);
595  newSug.addAttribute(nestedAttr);
596  }
597  }
598  nestedInReqAnnotations.add((Annotation) ((NestedAnnotationAttribute)attr).getValue());
599  } else { // create nested again without value
600  try{
601  SugBaseAttribute nestedAttr = SugAttributeManager.createAttribute(attr.getName(), "NestedSuggestion", newSug);
602  nestedAttr.updateFromBaseAttribut(attr);
603  if (attr.getAttributeType() != null) {
604  ((SugNestedAttribute) nestedAttr).setAttributeType(attr.getAttributeType());
605  }
606  newSug.addAttribute(nestedAttr);
607  } catch(ClassNotFoundException ex) {
609  String msg = "Unknown type of attribute found.";
610  Logger.getLogger(SugPersister.class.getName()).log(Level.SEVERE, msg);
611  }
612  }
613  }
614  }
615  }
616 
617  newSug = em.merge(newSug);
618  anNewSugLinkMap.put(annot, newSug);
619  em.flush();
620 
621  // add nestedIn
622  for (Iterator<Suggestion> it = toAddNestedIn.iterator(); it.hasNext();) {
623  Suggestion nested = it.next();
624  nested.setNestedInSuggestion(newSug);
625  em.merge(nested);
626  }
627 
628  createNewFeedback(em, annot, newSug, annot.getAnnotType().getGroup());
629  if(!touchedSuggestions.contains(newSug)){
630  touchedSuggestions.add(newSug);
631  }
632  reqSuggs.put(annot.getId(), newSug);
633  }
634  return newSug;
635  } // saveSuggestionsWithoutLinks()
636 
637  /**
638  * Method updates links in saved suggestions
639  *
640  * @param annot Annotation which is updated
641  * @param reqSuggs HasMap with Original Annotation ID as key and new Suggestion as value
642  * @param em Entity manager
643  * @param touchedSuggestions changed suggestions
644  */
645  public static void updateLinksAndNested(Annotation annot,
646  HashMap<Integer, Suggestion> reqSuggs, EntityManager em, ArrayList<Suggestion> touchedSuggestions)
647  {
648  Iterator<BaseAttribute> attrsIt = annot.getAttributes().iterator();
649  while(attrsIt.hasNext()){
650  BaseAttribute attr = attrsIt.next();
651  if(attr.getSimpleType().equalsIgnoreCase("AnnotationLink")){
652  Suggestion lSug = reqSuggs.get(annot.getId());
653  if (lSug != null) {
654  SugLinkedAttribute linkedAttr = null;
655  try {
656  linkedAttr = (SugLinkedAttribute) SugAttributeManager.createAttribute(attr.getName(), "SuggestionLink", lSug);
657  } catch (Exception e) {
659  String msg = "Attempt to create unsupported type of attribute.";
660  Logger.getLogger(SugPersister.class.getName()).log(Level.SEVERE, msg);
661  }
662  continue;
663  }
664  linkedAttr.updateFromBaseAttribut(attr);
665  linkedAttr.setName(attr.getName());
666  lSug.addAttribute(linkedAttr);
668  if(laat.getValue() == null){
669  // annotation link is null
670  continue;
671  }
672  Annotation aVal = (Annotation) laat.getValue();
673  int linkedID = aVal.getId();
674  try {
675  Query query = em.createQuery("SELECT s FROM SuggestionFeedback s WHERE s.annotationId=?1 AND s.suggestionId IS NOT NULL");
676  query.setParameter(1, linkedID);
677  List sList = query.getResultList();
678  if (sList != null && !sList.isEmpty()) {
679  SuggestionFeedback linkedSugFeed = (SuggestionFeedback) sList.get(0);
680  Suggestion linkedSug = linkedSugFeed.getSuggestion();
681  linkedAttr.setValue(linkedSug);
682  em.merge(lSug);
683  if (!touchedSuggestions.contains(lSug)) {
684  touchedSuggestions.add(lSug);
685  }
686  }
687  } catch (Exception e) {
689  String msg = "Problem with database.";
690  Logger.getLogger(SugPersister.class.getName()).log(Level.SEVERE, msg);
691  }
692  }
693  } else {
694  try {
695  Query query = em.createQuery("SELECT s FROM SuggestionFeedback s WHERE s.annotationId=?1 AND s.suggestionId IS NOT NULL");
696  query.setParameter(1, annot.getId());
697  List sList = query.getResultList();
698  if (sList != null && !sList.isEmpty()) {
699  SuggestionFeedback lSugFeed = (SuggestionFeedback) sList.get(0);
700  lSug = lSugFeed.getSuggestion();
701  SugLinkedAttribute linkedAttr = (SugLinkedAttribute) SugAttributeManager.createAttribute(attr.getName(), "SuggestionLink", lSug);
702  linkedAttr.updateFromBaseAttribut(attr);
703  lSug.addAttribute(linkedAttr);
705  Annotation aVal = (Annotation) laat.getValue();
706  if(laat.getValue() == null){
707  // annotation link is null
708  continue;
709  }
710  int linkedID = aVal.getId();
711  Query query2 = em.createQuery("SELECT s FROM SuggestionFeedback s WHERE s.annotationId=?1 AND s.suggestionId IS NOT NULL");
712  query2.setParameter(1, linkedID);
713  sList = query2.getResultList();
714  if (sList != null && !sList.isEmpty()) {
715  SuggestionFeedback linkedSugFeed = (SuggestionFeedback) sList.get(0);
716  Suggestion linkedSug = linkedSugFeed.getSuggestion();
717  linkedAttr.setValue(linkedSug);
718  em.merge(lSug);
719  if (!touchedSuggestions.contains(lSug)) {
720  touchedSuggestions.add(lSug);
721  }
722  }
723  }
724  } catch (Exception e) {
726  String msg = "Problem with database.";
727  Logger.getLogger(SugPersister.class.getName()).log(Level.SEVERE, msg);
728  }
729  }
730  } // else
731  }
732  } // while(attrsIt.hasNext()){
733  } // updateLinksAndNested()
734 
735  /**
736  * Updates suggestion feedback and confidence in DB
737  *
738  * @param em Entity manager
739  * @param suggestion Updated suggestion
740  * @param requestInfo Information about client request
741  * @param touchedSuggestions Touched suggestions in persister
742  * @param solveFeedback determines if we are solving a feedback
743  * @throws RuntimeException
744  */
745  public static void updateSugg(EntityManager em, Suggestion suggestion, RequestInfo requestInfo,
746  ArrayList<Suggestion> touchedSuggestions, boolean solveFeedback) throws RuntimeException
747  {
748  Integer setConfidence = null;
749  SuggestionLogEntry actualEntry = null;
750 
751  ArrayList<SuggestionLogEntry> confirmeSug = requestInfo.getConfirmedSuggestions();
752  if(confirmeSug != null){
753  Iterator<SuggestionLogEntry> confirmeSugIt = confirmeSug.iterator();
754  while(confirmeSugIt.hasNext()){
755  SuggestionLogEntry sugLog = confirmeSugIt.next();
756  String tmpId = sugLog.getTmpId();
757  int tmpIdInt;
758  try{
759  tmpIdInt = Integer.parseInt(tmpId);
760  }
761  catch(NumberFormatException ex){
762  continue;
763  }
764  if(tmpIdInt == suggestion.getId()){
765  actualEntry = sugLog;
766  if(sugLog.getConfirmMethod() == Constants.CONFIRMED_MANUALLY_EDITED
767  || sugLog.getConfirmMethod() == Constants.CONFIRMED_MANUALLY)
768  {
769  setConfidence = 95;
770  }
771  else if(sugLog.getConfirmedVersion().getNestedInAnnot() != null){
772  if(sugLog.getConfirmMethod() == Constants.CONFIRMED_AUTOMATICALLY
773  || sugLog.getConfirmMethod() == Constants.CONFIRMED_AUTOMATICALLY_EDITED){
774  setConfidence = 95;
775  }
776  }
777  break;
778  }
779  }
780  }
781 
782  // update tmpId links in attributes
783  Suggestion sg = saveTmpIdRefSuggestions(suggestion, requestInfo, em);
784  if(sg != null && !touchedSuggestions.contains(sg)){
785  touchedSuggestions.add(sg);
786  }
787 
788  if(setConfidence != null && solveFeedback){
789  suggestion.setConfidence(setConfidence);
790  }
791  em.merge(suggestion);
792 
793  if(solveFeedback){
794  Annotation confirmedAnnot = actualEntry.getConfirmedVersion();
795  UserGroup userGroup = actualEntry.getConfirmedVersion().getAnnotType().getGroup();
796  createNewFeedback(em, confirmedAnnot, suggestion, userGroup);
797  }
798 
799  if(!touchedSuggestions.contains(suggestion)){
800  touchedSuggestions.add(suggestion);
801  }
802  } // updateSugg()
803 
804  /**
805  * Sets uri from value in annotation link parameters of given suggestion, also
806  * recursively in nested suggestions
807  *
808  * @param sugg Given suggestion
809  */
810  public static void setUriOfSuggParams(Suggestion sugg){
811  List attrList = sugg.getAttributes();
812  Iterator attrIt = attrList.iterator();
813  while(attrIt.hasNext()){ // For all attributes od Annotation
814  SugBaseAttribute attr = (SugBaseAttribute)attrIt.next();
815 
816  //for all annotation links which have uri set to null or empty value
817  if((attr.getSimpleType().equals("AnnotationLink") || attr.getSimpleType().equals("SuggestionLink"))
818  && attr instanceof SugLinkedAttribute && ((SugLinkedAttribute)attr).getValue() != null){
819  if(((SugLinkedAttribute)attr).getUri() == null || ((SugLinkedAttribute)attr).getUri().isEmpty()){
820  Suggestion value = (Suggestion) ((SugLinkedAttribute)attr).getValue();
821  ((SugLinkedAttribute)attr).setUri(value.getURI());
822  }
823  }
824 
825  //recursively update nested suggestions
826  if(attr.getSimpleType().equals("NestedAnnotation") && ((SugNestedAttribute)attr).getValue() != null){
827  Suggestion value = (Suggestion) ((SugNestedAttribute)attr).getValue();
828  setUriOfSuggParams(value);
829  }
830  }
831  } // setUriOfSuggParams()
832 
833 
834  /**
835  * Query database for type of annotation
836  *
837  * @param uri URI of type of annotation
838  * @param em Entity manager
839  * @return Returns type of annotation with given URI
840  * @throws RuntimeException If type of annotation was not found, throws exception
841  */
842  private static AnnotType queryType(String uri, EntityManager em) throws RuntimeException {
843  AnnotType annotType = null;
844  Object[] params = new Object[2];
845  params[0] = "uri";
846  params[1] = uri;
847  Query q = em.createNamedQuery("AnnotType.findByUri");
848  for (int p = 0; p < params.length; p = p + 2) {
849  q.setParameter((String) params[p], params[p + 1]);
850  }
851  List aList = q.getResultList();
852  if (aList != null && !aList.isEmpty()) { // if type was found
853  annotType = (AnnotType) aList.get(0);
854  } else { // if type was not found (merge failed)
856  String msg = "Queried type of annotation was not found by the Persister: " + uri;
857  Logger.getLogger(Persister.class.getName()).log(Level.SEVERE, msg);
858  }
859  throw new RuntimeException("New type not found!");
860  }
861  return annotType;
862  } // queryType()
863 
864  /**
865  * Method updates types in Suggestion
866  *
867  * @param sug suggestion for update
868  * @param em current EntityManager
869  * @param logMsg String with error
870  * @throws RuntimeException
871  */
872  private static void updateTypes(Suggestion sug, EntityManager em, String logMsg) throws RuntimeException {
873  // if it's a new type of suggestion, it must be set again after merge
874  if (sug.getAnnotType() == null) {
876  String msg = "The annotation has no type.";
877  Logger.getLogger(Persister.class.getName()).log(Level.SEVERE, msg);
878  }
879  return;
880  }
881  AnnotType annotType = null;
882  Object[] params = new Object[2];
883  params[0] = "uri";
884  params[1] = sug.getAnnotType().getUri();
885  Query q = em.createNamedQuery("AnnotType.findByUri");
886  for (int p = 0; p < params.length; p = p + 2) {
887  q.setParameter((String) params[p], params[p + 1]);
888  }
889  List aList = q.getResultList();
890  if (aList != null && !aList.isEmpty()) { // if type was found
891  annotType = (AnnotType) aList.get(0);
892  sug.setAnnotType(annotType);
893  // type of the nested and linked attribute also has to be updated
894  Iterator<SugBaseAttribute> attIt = sug.getAttributes().iterator();
895  while (attIt.hasNext()) {
896  SugBaseAttribute att = attIt.next();
897  // nested and linked attributes must have type set
898  if (att instanceof SugNestedAttribute || att instanceof SugLinkedAttribute) {
899  // second parameter of the query is the uri of the attribute type
900  if (att.getAttributeType() == null) {
901  continue;
902  }
903  params[1] = att.getAttributeType().getUri();
904  Query attTypeQuery = em.createNamedQuery("AnnotType.findByUri");
905  attTypeQuery.setParameter((String) params[0], params[1]);
906  // search in the database for the type with given uri
907  List typeList = attTypeQuery.getResultList();
908  // if the type's been found
909  if (typeList != null && !typeList.isEmpty()) {
910  // set attribute type to the one from the database
911  att.setAttributeType((AnnotType)typeList.get(0));
912  }
913  }
914  }
915  } else { // if type was not found (merge failed)
917  String msg = logMsg + sug.getAnnotType().getUri();
918  Logger.getLogger(Persister.class.getName()).log(Level.SEVERE, msg);
919  }
920  throw new RuntimeException("New type not found!");
921  }
922  } // updateTypes
923 
924  /**
925  * Updates types in nested suggestions (use types stored in DB)
926  *
927  * @param sug Suggestion, which attributes will be updated
928  * @param em Entity manager
929  */
930  private static void updateTypesInNested(Suggestion sug, EntityManager em) {
931  Iterator<SugBaseAttribute> aIt = sug.getAttributes().iterator();
932  while (aIt.hasNext()) { // for each attribute
933  SugBaseAttribute attribute = aIt.next();
934  Suggestion nested = attribute.getNestedSuggestion();
935  if (nested != null) { // if nested suggestions is included
936  if (nested.getAnnotType().getId() == null) { // if it is a new type
937  // if it's a new type of suggestion, it must be set again after merge
938  AnnotType annotType = queryType(nested.getAnnotType().getUri(), em);
939  nested.setAnnotType(annotType);
940  // type of the nested and linked attribute also has to be updated
941  Iterator<SugBaseAttribute> attIt = sug.getAttributes().iterator();
942  while (attIt.hasNext()) {
943  SugBaseAttribute att = attIt.next();
944  // nested and linked attributes must have type set
945  if (att instanceof SugNestedAttribute || att instanceof SugLinkedAttribute) {
946  // second parameter of the query is the uri of the attribute type
947  if (att.getAttributeType() == null) {
948  continue;
949  }
950  Object[] params = new Object[2];
951  params[0] = "uri";
952  params[1] = att.getAttributeType().getUri();
953  Query attTypeQuery = em.createNamedQuery("AnnotType.findByUri");
954  attTypeQuery.setParameter((String) params[0], params[1]);
955  // search in the database for the type with given uri
956  List typeList = attTypeQuery.getResultList();
957  // if the type's been found
958  if (typeList != null && !typeList.isEmpty()) {
959  // set attribute type to the one from the database
960  att.setAttributeType((AnnotType)typeList.get(0));
961  }
962  }
963  }
964  } // if it is a new type
965  updateTypesInNested(nested, em); // update nested suggestions recursively
966  } // if nested suggestion is included
967  } // for each attribute
968  } // updateTypesInNested()
969 
970  /**
971  * Recursively saves suggestions in attributes of given suggestion.
972  * @param sug Suggestion for reference update
973  * @param requestInfo Informations about client request
974  * @param em current EntityManager
975  * @return uri of the new saved suggestion
976  * @throws RuntimeException
977  */
978  private static Suggestion saveTmpIdRefSuggestions(Suggestion sug, RequestInfo requestInfo, EntityManager em) throws RuntimeException {
979  return saveTmpIdRefSuggestions(sug, requestInfo, em, null, null);
980  }
981 
982  /**
983  * Recursively saves suggestions in attributes of given suggestion.
984  * @param sug Suggestion for reference update
985  * @param requestInfo Informations about client request
986  * @param em current EntityManager
987  * @param justSaved List of just saved suggestions (versions before saving)
988  * @param nowSaved List of just saved suggestions (really saved versions)
989  * @return uri of the new saved suggestion
990  * @throws RuntimeException
991  */
992  private static Suggestion saveTmpIdRefSuggestions(Suggestion sug, RequestInfo requestInfo, EntityManager em, ArrayList<Suggestion> justSaved, ArrayList<Suggestion> nowSaved) throws RuntimeException {
993  Suggestion saved = null;
994 
995  List attrList = sug.getAttributes();
996  Iterator attrIt = attrList.iterator();
997  while(attrIt.hasNext()){ // For all attributes of Suggestion
998  SugBaseAttribute attr = (SugBaseAttribute)attrIt.next();
999  if((attr instanceof SugLinkedAttribute) && ((SugLinkedAttribute)attr).getValue() != null){
1000  Suggestion val = (Suggestion) ((SugLinkedAttribute)attr).getValue();
1001  if (val.getId() == null) {
1002  Iterator<Suggestion> nsIt = nowSaved.iterator();
1003  boolean found = false;
1004  while (nsIt.hasNext()) {
1005  Suggestion suggestion = nsIt.next();
1006  if (suggestion.getTmpId() != null && suggestion.getTmpId().equals(val.getTmpId())) {
1007  attr.setValue(suggestion);
1008  attr.setUri(suggestion.getURI());
1009  found = true;
1010  }
1011  }
1012  if (!found) {
1013  Suggestion annot4update = saveTmpIdRefSuggestions(val, requestInfo, em, justSaved, nowSaved);
1014  attr.setValue(annot4update);
1015  attr.setUri(annot4update.getURI());
1016  }
1017  }
1018  } else if (attr instanceof SugNestedAttribute && attr.getNestedSuggestion() != null) {
1019  saveTmpIdRefSuggestions(attr.getNestedSuggestion(), requestInfo, em, justSaved, nowSaved);
1020  }
1021  }
1022  if (sug.getId() == null && sug.getNestedInSuggestion() == null) {
1023  updateTypes(sug, em, "New type of suggestion for tmpId linked suggestion was not found by Persister: ");
1024  updateTypesInNested(sug, em);
1025  saved = em.merge(sug);
1026  em.flush();
1027  saved.setTmpId(sug.getTmpId());
1028  if (justSaved != null) {
1029  justSaved.add(sug);
1030  }
1031  if (nowSaved != null) {
1032  nowSaved.add(saved);
1033  }
1034  }
1035 
1036  return saved;
1037  } // saveTmpIdRefSuggestions()
1038 
1039  /**
1040  * Creates new feedback in DB
1041  *
1042  * @param em current EntityManager
1043  * @param annot Annotation
1044  * @param sugg Suggestion
1045  * @param group UserGroup
1046  */
1047  private static void createNewFeedback(EntityManager em, Annotation annot, Suggestion sugg, UserGroup group){
1049  feed.setAnnot(annot);
1050  if (annot != null) {
1051  feed.setAnnotationId(annot.getId());
1052  }
1053  feed.setSuggestion(sugg);
1054  feed.setSuggestionId(sugg.getId());
1055  feed.setGroup(group);
1056  feed.setGroupId(group.getId());
1057  em.merge(feed);
1058  }
1059 
1060  /**
1061  * Changes existing feedback in DB
1062  *
1063  * @param em current EntityManager
1064  * @param annot Annotation
1065  * @param oldSuggID Ols suggestion ID
1066  * @param newSugg New suggestion
1067  * @param group UserGroup
1068  */
1069  private static void changeFeedback(EntityManager em, Annotation annot, int oldSuggID, Suggestion newSugg, UserGroup group){
1070  try {
1071  Query query = em.createQuery("SELECT s FROM SuggestionFeedback s WHERE s.annotationId=?1 AND s.suggestionId=?2 AND s.groupId=?3");
1072  query.setParameter(1, annot.getId());
1073  query.setParameter(2, oldSuggID);
1074  query.setParameter(3, group.getId());
1075  List sList = query.getResultList();
1076  if (sList != null && !sList.isEmpty()) {
1077  SuggestionFeedback oldFeed = (SuggestionFeedback) sList.get(0);
1078  oldFeed.setAnnotationId(null);
1079  oldFeed.setAnnot(null);
1080  em.merge(oldFeed);
1081  em.flush();
1082  } else {
1083  Suggestion sg = getSuggestionByID(oldSuggID, em);
1084  if (sg != null) {
1085  createNewFeedback(em, null, sg, group);
1086  }
1087  }
1088  createNewFeedback(em, annot, newSugg, group);
1089  } catch (Exception e) {
1091  String msg = "Persisting of feedback failed.";
1092  Logger.getLogger(SugPersister.class.getName()).log(Level.SEVERE, msg, e);
1093  }
1094  }
1095  } // changeFeedback()
1096 
1097  /**
1098  * Gets suggestion from DB by ID
1099  *
1100  * @param id suggestion ID
1101  * @param em current EntityManager
1102  * @return Suggestion or null if not found
1103  */
1104  private static Suggestion getSuggestionByID(Integer id, EntityManager em) {
1105  if (id == null) {
1106  return null;
1107  }
1108  SuggestionFeedback fb = null;
1109  Query query = em.createNamedQuery("Suggestion.findById");
1110  query.setParameter("id", id);
1111  List sList = query.getResultList();
1112  if (sList == null || sList.isEmpty()) {
1113  return null;
1114  }
1115  return (Suggestion) sList.get(0);
1116  }
1117 
1118  /**
1119  * Select suggestion attribute from database
1120  *
1121  * @param id Id of attribute
1122  * @param em Entity manager
1123  * @return attribute or null
1124  */
1125  private static SugBaseAttribute getSugAttributeByID(Integer id, EntityManager em) {
1126  if (id == null) {
1127  return null;
1128  }
1129  SugBaseAttribute fb = null;
1130  Query query = em.createNamedQuery("SuggestionAttribute.findById");
1131  query.setParameter("id", id);
1132  List sList = query.getResultList();
1133  if (sList == null || sList.isEmpty()) {
1134  return null;
1135  }
1136  return (SugBaseAttribute) sList.get(0);
1137  }
1138 
1139  /**
1140  * Gets suggestion from suggestion feedback table by Annotation
1141  *
1142  * @param annot annotation ID
1143  * @param em Entity manager
1144  * @return Suggestion or null if not found
1145  */
1146  private static Suggestion getSuggestionByAnnotation(Annotation annot, EntityManager em) {
1147  if (annot == null || annot.getId() == null) {
1148  return null;
1149  }
1150  SuggestionFeedback fb = getFeedbackByAnnot(annot.getId(), em);
1151  if (fb == null || fb.getSuggestionId() == null) {
1152  return null;
1153  }
1154  return getSuggestionByID(fb.getSuggestionId(), em);
1155  }
1156 
1157  /**
1158  * Method loads suggestion positive feedback specified by annotation.
1159  *
1160  * @param annotId annotation id that specify suggestion feedback
1161  * @param em Entity manager
1162  * @return array list with founded negative suggestions feedback
1163  */
1164  public static SuggestionFeedback getFeedbackByAnnot(Integer annotId, EntityManager em){
1165  Query query2 = em.createQuery("SELECT s FROM SuggestionFeedback s WHERE s.annotationId=?1 AND s.suggestionId IS NOT NULL");
1166  query2.setParameter(1, annotId);
1167  List feedbackList = query2.getResultList();
1168  if(feedbackList != null && !feedbackList.isEmpty()){
1169  return (SuggestionFeedback) feedbackList.get(0);
1170  }
1171  return null;
1172  }
1173 
1174  /**
1175  * Method loads suggestion specified by id from the database.
1176  *
1177  * @param id id that specify suggestions
1178  * @param em Entity manager
1179  * @return querried suggestion or null if suggestion isn't found
1180  */
1181  public static Suggestion getSuggestionFromDB(Integer id, EntityManager em){
1182  Query query = em.createQuery("SELECT s FROM Suggestion s WHERE s.id=?1");
1183  query.setParameter(1, id);
1184  List suggestionList = query.getResultList();
1185  if(suggestionList != null && !suggestionList.isEmpty()){
1186  return (Suggestion) suggestionList.get(0);
1187  }
1188 
1189  return null;
1190  }
1191 
1192  /**
1193  * Sets Zero confidence to all nested Suggestions
1194  *
1195  * @param em Entity manager
1196  * @param an Refused annotation
1197  * @param touchedSuggestions modified suggestions
1198  */
1199  private static void setNestedZeroConfidence (EntityManager em, Annotation an, ArrayList<Suggestion> touchedSuggestions) {
1200  if (an.getAttributes() != null) { // if annotation have some attributes
1201  for (Iterator<BaseAttribute> it = an.getAttributes().iterator(); it.hasNext();) {
1202  BaseAttribute at = it.next();
1203  if (at.getNestedAnnotation() != null && at.getNestedAnnotation().getId() != null) {
1204  Query query2 = em.createQuery("SELECT s FROM SuggestionFeedback s WHERE s.annotationId=?1 AND s.suggestionId IS NOT NULL");
1205  query2.setParameter(1, at.getNestedAnnotation().getId());
1206  List sList = query2.getResultList();
1207  if (sList != null && !sList.isEmpty()) {
1208  SuggestionFeedback sugFeed = (SuggestionFeedback) sList.get(0);
1209  Suggestion s = sugFeed.getSuggestion();
1210  s.setConfidence(0);
1211  s = em.merge(s);
1212  sugFeed.setAnnot(null);
1213  em.merge(sugFeed);
1214  setNestedZeroConfidence (em, at.getNestedAnnotation(), touchedSuggestions);
1215  if (!touchedSuggestions.contains(s)) {
1216  touchedSuggestions.add(s);
1217  }
1218  }
1219  }
1220  }
1221  } // if annotation have some attributes
1222  } // setNestedZeroConfidence()
1223 
1224  /**
1225  * Sets Zero confidence to all nested Suggestions
1226  *
1227  * @param em Entity manager
1228  * @param sug Refused suggestion
1229  * @param touchedSuggestions modified suggestions
1230  */
1231  private static void setNestedSugZeroConfidence (EntityManager em, Suggestion sug, ArrayList<Suggestion> touchedSuggestions) {
1232  if (sug.getAttributes() != null) {
1233  for (Iterator<SugBaseAttribute> it = sug.getAttributes().iterator(); it.hasNext();) {
1234  SugBaseAttribute at = it.next();
1235  if (at.getNestedSuggestion() != null && at.getNestedSuggestion().getId() != null) {
1236  Suggestion s = at.getNestedSuggestion();
1237  s.setConfidence(0);
1238  s = em.merge(s);
1239  setNestedSugZeroConfidence(em, s, touchedSuggestions);
1240  if (!touchedSuggestions.contains(s)) {
1241  touchedSuggestions.add(s);
1242  }
1243  }
1244  }
1245  }
1246  } // setNestedSugZeroConfidence
1247 
1248 } // class SugPersister
Static class which persists data to the database.
Definition: Persister.java:49
Attribute manager provides a way how to create new attributes for prupose of suggestion.
Class representing attribute of type of annotation.
static AnnotType queryType(String uri, EntityManager em)
Class representing attribute of type NestedAnnotation for peupose of suggestion.
Singleton for storing global variables.
Definition: AppBean.java:47
static ArrayList< Suggestion > persist(RequestInfo requestInfo, ArrayList< Annotation > addedAnnotations, ArrayList< Annotation > editedAnnotations, ArrayList< Suggestion > NERsuggestions, boolean solveFeedbacks, ArrayList< AnnotType > addedTypes)
static SuggestionFeedback getFeedbackByAnnot(Integer annotId, EntityManager em)
static Suggestion saveTmpIdRefSuggestions(Suggestion sug, RequestInfo requestInfo, EntityManager em, ArrayList< Suggestion > justSaved, ArrayList< Suggestion > nowSaved)
static Suggestion getSuggestionByAnnotation(Annotation annot, EntityManager em)
static void updateTypes(Suggestion sug, EntityManager em, String logMsg)
static void updateTypesInNested(Suggestion sug, EntityManager em)
Suggested annotation with informations about suggestion.
static Suggestion saveSuggestionsWithoutLinks(RequestInfo requestInfo, EntityManager em, Annotation annot, HashMap< Integer, Suggestion > reqSuggs, ArrayList< Annotation > nestedInReqAnnotations, ArrayList< Suggestion > touchedSuggestions, HashMap< Annotation, Suggestion > anNewSugLinkMap)
Class representing user group.
Definition: UserGroup.java:47
static void changeFeedback(EntityManager em, Annotation annot, int oldSuggID, Suggestion newSugg, UserGroup group)
static Suggestion saveTmpIdRefSuggestions(Suggestion sug, RequestInfo requestInfo, EntityManager em)
static Suggestion getSuggestionFromDB(Integer id, EntityManager em)
static void updateSugg(EntityManager em, Suggestion suggestion, RequestInfo requestInfo, ArrayList< Suggestion > touchedSuggestions, boolean solveFeedback)
static void createNewFeedback(EntityManager em, Annotation annot, Suggestion sugg, UserGroup group)
Static class which persists suggestion data to the database.
Base class representing attribute of annotation.
static SugBaseAttribute getSugAttributeByID(Integer id, EntityManager em)
Class representing type of annotation.
Definition: AnnotType.java:58
Class representing user.
Definition: User.java:51
Class representing attribute of type AnnotationLink for prupose of suggestion.
static void updateLinksAndNested(Annotation annot, HashMap< Integer, Suggestion > reqSuggs, EntityManager em, ArrayList< Suggestion > touchedSuggestions)
static Suggestion getSuggestionByID(Integer id, EntityManager em)
static void setNestedZeroConfidence(EntityManager em, Annotation an, ArrayList< Suggestion > touchedSuggestions)
Processed informations about client request.
Class representing suggestion of annotation.
Definition: Suggestion.java:87
Represents database entitiy with suggestion feedback. feedback.
static void setNestedSugZeroConfidence(EntityManager em, Suggestion sug, ArrayList< Suggestion > touchedSuggestions)
static void updateNerSugg(EntityManager em, Suggestion sug, ArrayList< Suggestion > touchedSug, RequestInfo request)