4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
Suggestion.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: Annotation.java
5  * Description: Class representing suggestion of annotation
6  */
7 
8 /**
9  * @file Suggestion.java
10  *
11  * @brief Class representing suggestion of annotation
12  */
13 package cz.vutbr.fit.knot.annotations.modules.suggestionManager;
14 
29 import cz.vutbr.fit.knot.annotations.modules.suggestionManager.attributes.*;
30 import java.io.Serializable;
31 import java.text.SimpleDateFormat;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.Date;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.List;
38 import java.util.logging.Level;
39 import java.util.logging.Logger;
40 import javax.persistence.Basic;
41 import javax.persistence.CascadeType;
42 import javax.persistence.Column;
43 import javax.persistence.Entity;
44 import javax.persistence.GeneratedValue;
45 import javax.persistence.GenerationType;
46 import javax.persistence.Id;
47 import javax.persistence.JoinColumn;
48 import javax.persistence.Lob;
49 import javax.persistence.NamedQueries;
50 import javax.persistence.NamedQuery;
51 import javax.persistence.OneToMany;
52 import javax.persistence.OneToOne;
53 import javax.persistence.Table;
54 import javax.persistence.Temporal;
55 import javax.persistence.TemporalType;
56 import javax.persistence.Transient;
57 import javax.validation.constraints.NotNull;
58 import javax.validation.constraints.Size;
59 import javax.xml.bind.annotation.XmlRootElement;
60 
61 /**
62  * Class representing suggestion of annotation
63  *
64  * @brief Class representing suggestion of annotation
65  * @author Martin Petr (xpetrm05)
66  */
67 @Entity
68 @Table(name = "suggestion")
69 @XmlRootElement
70 @NamedQueries({
71  @NamedQuery(name = "Suggestion.findAll", query = "SELECT s FROM Suggestion s"),
72  @NamedQuery(name = "Suggestion.findById", query = "SELECT s FROM Suggestion s WHERE s.id = :id"),
73  @NamedQuery(name = "Suggestion.findByAlternativeOfAndNotSugg", query = "SELECT s FROM Suggestion s WHERE s.alternativeOf = :alternativeOf AND s.id <> :notThisSugId"),
74  @NamedQuery(name = "Suggestion.findByAlternativeOf", query = "SELECT s FROM Suggestion s WHERE s.alternativeOf = :alternativeOf"),
75  @NamedQuery(name = "Suggestion.findByType", query = "SELECT s FROM Suggestion s WHERE s.type = :type"),
76  @NamedQuery(name = "Suggestion.findByAuthorIdStr", query = "SELECT s FROM Suggestion s WHERE s.authorIdStr = :authorIdStr"),
77  @NamedQuery(name = "Suggestion.findByAuthorName", query = "SELECT s FROM Suggestion s WHERE s.authorName = :authorName"),
78  @NamedQuery(name = "Suggestion.findByAuthorAddress", query = "SELECT s FROM Suggestion s WHERE s.authorAddress = :authorAddress"),
79  @NamedQuery(name = "Suggestion.findBySource", query = "SELECT s FROM Suggestion s WHERE s.source = :source"),
80  @NamedQuery(name = "Suggestion.findBySourceDocId", query = "SELECT s FROM Suggestion s WHERE s.sourceDocumentId = :sourceDocId"),
81  @NamedQuery(name = "Suggestion.findForAlternatives", query = "SELECT s FROM Suggestion s WHERE s.sourceDocumentId = :sourceDocId AND s.confidence >= :minCon AND s.alternativeOf IS NULL AND s.isFromSECAPI = TRUE"),
82  @NamedQuery(name = "Suggestion.findBySourceDocIdNNConfid", query = "SELECT s FROM Suggestion s WHERE s.sourceDocumentId = :sourceDocId AND s.confidence > 0"),
83  @NamedQuery(name = "Suggestion.findBySourceDocument", query = "SELECT s FROM Suggestion s WHERE s.sourceDocument = :sourceDocument"),
84  @NamedQuery(name = "Suggestion.findByAutorId", query = "SELECT s FROM Suggestion s WHERE s.autorId = :autorId"),
85  @NamedQuery(name = "Suggestion.clearIsFromSECAPI", query = "UPDATE Suggestion s SET s.isFromSECAPI = FALSE WHERE s.id = :id")
86 })
87 public class Suggestion implements Serializable, SecSuggestion {
88  private static final long serialVersionUID = 1L;
89  /** Id of suggestion */
90  @Id
91  @GeneratedValue(strategy = GenerationType.IDENTITY)
92  @Basic(optional = false)
93  @Column(name = "id")
94  private Integer id;
95 
96  /** Id of type of annotation */
97  @Basic(optional = false)
98  @NotNull
99  @Column(name = "type", nullable=false, insertable=false, updatable=false)
100  private int type;
101 
102  /** Date of creation */
103  @Basic(optional = true)
104  @Column(name = "created")
105  @Temporal(TemporalType.TIMESTAMP)
106  private Date created;
107 
108  /** URI of suggestion author (user) */
109  @Basic(optional = true)
110  @Size(min = 1, max = 255)
111  @Column(name = "authorIdStr")
112  private String authorIdStr;
113 
114  /** Name of suggestion author (user) */
115  @Size(max = 40)
116  @Column(name = "authorName")
117  private String authorName;
118 
119  /** E-mail of suggestion author (user) */
120  @Size(max = 255)
121  @Column(name = "authorAddress")
122  private String authorAddress;
123 
124  /** URI of annotated copy of document to which this suggestion belongs */
125  @Basic(optional = false)
126  @NotNull
127  @Size(min = 1, max = 255)
128  @Column(name = "source")
129  private String source;
130 
131  /** Id of annotated copy of document to which this suggestion belongs */
132  @Basic(optional = false)
133  @NotNull
134  @Column(name = "sourceDocumentId", nullable=false, insertable=false, updatable=false)
135  private int sourceDocumentId;
136 
137  /** Textual content of suggestion */
138  @Basic(optional = false)
139  @Lob
140  @Column(name = "content")
141  private String content;
142 
143  /** Confidence of suggestion */
144  @Basic(optional = false)
145  @Column(name = "confidence")
146  private Integer confidence;
147 
148  /** Should be confidence displayed? */
149  @Basic(optional = false)
150  @NotNull
151  @Column(name = "displayConfidence")
152  private boolean displayConfidence;
153 
154  /** Id of suggestion author (user) */
155  @Column(name = "authorId", insertable=false, updatable=false)
156  private String autorId;
157 
158  /** If this suggestion is nested, here is id of parent suggestion */
159  @Column(name = "nestedIn", insertable=false, updatable=false)
160  private String nestedIn;
161 
162  /** If this suggestion was created from alternative, here is id of main suggestion */
163  @Column(name = "alternativeOf", insertable=false, updatable=false)
164  private String alternativeOf;
165 
166  /** Type of suggestion */
167  @OneToOne(optional = false)
168  @JoinColumn(name = "type", referencedColumnName = "id")
169  private AnnotType annotType;
170 
171  /** Annotated copy of document to which this suggestion belongs */
172  @OneToOne(optional = false)
173  @JoinColumn(name = "sourceDocumentId", referencedColumnName = "id")
174  private AnnotDocument sourceDocument;
175 
176  /** List of attributes of suggestion */
177  @OneToMany(mappedBy = "refSuggestion", cascade = CascadeType.ALL,
178  targetEntity = SugBaseAttribute.class)
179  private List<SugBaseAttribute> attributes;
180 
181  /** List of suggested fragments */
182  @OneToMany(mappedBy = "refSuggestion", cascade = CascadeType.ALL,
183  targetEntity = SuggestionFragment.class)
184  private List<SuggestionFragment> fragments;
185 
186  /** Author of suggestion (user) */
187  @OneToOne(optional = true)
188  @JoinColumn(name = "authorId", referencedColumnName = "id")
189  private User user;
190 
191  /** If this suggestion is nested, here is parent suggestion */
192  @OneToOne(optional = true)
193  @JoinColumn(name = "nestedIn", referencedColumnName = "id")
194  private Suggestion nestedInSuggestion;
195 
196  /** If this suggestion was created from alternative, here is id of main suggestion */
197  @OneToOne(optional = true)
198  @JoinColumn(name = "alternativeOf", referencedColumnName = "id")
199  private Suggestion alternativeOfSuggestion;
200 
201  /** Is this suggestion from SEC API? */
202  @Basic(optional = false)
203  @NotNull
204  @Column(name = "isFromSECAPI")
205  private boolean isFromSECAPI;
206 
207  /** Identifier of entity from SEC API (to detect same suggestions on more places) */
208  @Basic(optional = true)
209  @Column(name = "SAEntityIdentifier")
210  private String SAEntityIdentifier;
211 
212  /** Temporary id to identify targets of links in suggestions from client */
213  @Transient
214  private String tmpId;
215 
216  /** If this suggestion is nested, here is parent annotation */
217  @Transient
218  private Annotation nestedInAnnot;
219 
220  /**
221  * Constructor
222  */
223  public Suggestion() {
224  }
225 
226  /**
227  * Constructor of reference objects for searching purposes - no initialization
228  * needed
229  *
230  * @param id Id of suggestion
231  */
232  public Suggestion(Integer id) {
233  this.id = id;
234  }
235 
236  /**
237  * Constructor for creating suggestion from annotation
238  *
239  * @param a Annotation from which is the suggestion created
240  */
241  public Suggestion(Annotation a) {
242  this.updateWithAnnotationForDB(a);
243  isFromSECAPI = false; // annotation is created by user, not by SEC API
244  }
245 
246  /**
247  * Constructor for creating suggestion from alternative.
248  *
249  * @param a Alternative from which is the suggestion created
250  */
252  this.updateWithAlternativeForDB(a);
253  isFromSECAPI = true; // alternative is created by SEC API
254  }
255 
256  /**
257  * Constructor
258  *
259  * @param type Type of suggestion
260  * @param content Textual content of suggestion
261  * @param nestedIn If this suggestion is nested, then parent suggestion, null otherwise
262  */
263  public Suggestion(AnnotType type, String content, String nestedIn) {
264  this.annotType = type;
265  this.content = content;
266  this.nestedIn = nestedIn;
267  this.attributes = new ArrayList<SugBaseAttribute>();
268  this.fragments = new ArrayList<SuggestionFragment>();
269  isFromSECAPI = false;
270  displayConfidence = false;
271  }
272 
273  /**
274  * Constructor
275  *
276  * @param id Id of suggestion
277  * @param authorIdStr URI of suggestion author (user)
278  * @param authorName Name of suggestion author (user)
279  * @param authorAddress E-mail of suggestion author (user)
280  * @param sourceDocument Annotated copy of document to which this suggestion belongs
281  * @param content Textual content of suggestion
282  * @param nestedIn If this suggestion is nested, then parent suggestion, null otherwise
283  */
284  public Suggestion(Integer id, String authorIdStr, String authorName, String authorAddress, AnnotDocument sourceDocument, String content, String nestedIn) {
285  this.id = id;
286  this.authorIdStr = authorIdStr;
287  this.authorName = authorName;
288  this.authorAddress = authorAddress;
289  this.sourceDocument = sourceDocument;
290  if (sourceDocument != null) {
291  this.source = sourceDocument.getUriForAnnot();
292  }
293  this.content = content;
294  this.nestedIn = nestedIn;
295  this.nestedInSuggestion = null;
296  this.attributes = new ArrayList<SugBaseAttribute>();
297  this.fragments = new ArrayList<SuggestionFragment>();
298  isFromSECAPI = false;
299  displayConfidence = false;
300  }
301 
302  /**
303  * Constructor
304  *
305  * @param annotType Type of suggestion
306  * @param created Date of creation
307  * @param authorIdStr URI of suggestion author (user)
308  * @param authorName Name of suggestion author (user)
309  * @param authorAddress E-mail of suggestion author (user)
310  * @param sourceDocument Annotated copy of document to which this suggestion belongs
311  * @param content Textual content of suggestion
312  */
313  public Suggestion(AnnotType annotType, Date created, String authorIdStr, String authorName, String authorAddress, AnnotDocument sourceDocument, String content) {
314  this.annotType = annotType;
315  this.created = created;
316  this.authorIdStr = authorIdStr;
317  this.authorName = authorName;
318  this.authorAddress = authorAddress;
319  this.sourceDocument = sourceDocument;
320  if (sourceDocument != null) {
321  this.source = sourceDocument.getUriForAnnot();
322  }
323  this.content = content;
324  this.nestedInSuggestion = null;
325  this.attributes = new ArrayList<SugBaseAttribute>();
326  this.fragments = new ArrayList<SuggestionFragment>();
327  isFromSECAPI = false;
328  displayConfidence = false;
329  }
330 
331  /**
332  * Constructor
333  *
334  * @param annotType Type of suggestion
335  * @param created Date of creation
336  * @param authorIdStr URI of suggestion author (user)
337  * @param authorName Name of suggestion author (user)
338  * @param authorAddress E-mail of suggestion author (user)
339  * @param sourceDocument Annotated copy of document to which this suggestion belongs
340  * @param content Textual content of suggestion
341  * @param nestedInAnnot If this suggestion is nested, then parent suggestion, null otherwise
342  */
343  public Suggestion(AnnotType annotType, Date created, String authorIdStr, String authorName, String authorAddress, AnnotDocument sourceDocument, String content, Suggestion nestedInAnnot) {
344  this.annotType = annotType;
345  this.created = created;
346  this.authorIdStr = authorIdStr;
347  this.authorName = authorName;
348  this.authorAddress = authorAddress;
349  this.sourceDocument = sourceDocument;
350  if (sourceDocument != null) {
351  this.source = sourceDocument.getUriForAnnot();
352  }
353  this.content = content;
354  this.nestedInSuggestion = nestedInAnnot;
355  this.attributes = new ArrayList<SugBaseAttribute>();
356  this.fragments = new ArrayList<SuggestionFragment>();
357  isFromSECAPI = false;
358  displayConfidence = false;
359  }
360 
361  /**
362  * Updates data in this suggestion with data from another suggestion
363  *
364  * @param newData Suggestion with new data
365  */
366  public void updateWithSuggestion(Suggestion newData) {
367  this.type = newData.getAnnotType().getId();
368  this.created = newData.getCreated();
369  this.authorIdStr = newData.getAuthorIdStr();
370  this.authorName = newData.getAuthorName();
371  this.authorAddress = newData.getAuthorAddress();
372  this.source = newData.getSource();
373  this.sourceDocumentId = newData.getSourceDocument().getId();
374  this.content = newData.getContent();
375  if (newData.getNestedInSuggestion() != null) {
376  this.nestedIn = newData.getNestedInSuggestion().getId().toString();
377  }
378  this.annotType = newData.getAnnotType();
379  this.sourceDocument = newData.getSourceDocument();
380  this.attributes = newData.getAttributes();
381  this.fragments = newData.getFragments();
382  this.user = newData.getUser();
383  this.nestedInSuggestion = newData.getNestedInSuggestion();
384  this.displayConfidence = newData.displayConfidence;
385  }
386 
387  /**
388  * Update data of suggestion with annotation data
389  *
390  * @param newData Annotation data
391  */
392  public void updateWithAnnotation(Annotation newData){
393  this.type = newData.getAnnotType().getId();
394  this.created = newData.getCreated();
395  this.authorIdStr = newData.getAuthorIdStr();
396  this.authorName = newData.getAuthorName();
397  this.authorAddress = newData.getAuthorAddress();
398  this.source = newData.getSource();
399  this.sourceDocumentId = newData.getSourceDocument().getId();
400  this.content = newData.getContent();
401  this.annotType = newData.getAnnotType();
402  this.sourceDocument = newData.getSourceDocument();
403  this.attributes = new ArrayList<SugBaseAttribute>();
404  if (newData.getAttributes() != null) {
405  Iterator<BaseAttribute> atIt = newData.getAttributes().iterator();
406  while (atIt.hasNext()) {
407  BaseAttribute at = atIt.next();
408  SugBaseAttribute newAt;
409  try {
410  newAt = SugAttributeManager.createAttribute(at.getName(), at.getSimpleType(), this);
411  } catch (ClassNotFoundException ex) {
412  String msg = "Unable to create suggestion attribute with type = " + at.getSimpleType();
413  Logger.getLogger(Suggestion.class.getName()).log(Level.SEVERE, null, msg);
414  continue;
415  }
416  newAt.updateFromBaseAttribut(at);
417  this.attributes.add(newAt);
418  }
419  }
420  this.fragments = new ArrayList<SuggestionFragment>();
421  if (newData.getFragments() != null) {
422  Iterator<Fragment> fragIt = newData.getFragments().iterator();
423  while (fragIt.hasNext()) {
424  Fragment fr = fragIt.next();
425  SuggestionFragment newFrag = new SuggestionFragment(fr.getPath(), fr.getOffset(), fr.getLength(), fr.getAnnotatedText(), this);
426  this.fragments.add(newFrag);
427  }
428  }
429  this.user = newData.getUser();
430  displayConfidence = false;
431  } // updateWithAnnotation()
432 
433  /**
434  * Update data of suggestion with alternative data in correct way to save to db
435  *
436  * @param newData Alternative data
437  */
439  this.tmpId = newData.getTmpId();
440  this.type = newData.getAnnotType().getId();
441  this.source = newData.getSource();
442  this.sourceDocumentId = newData.getSourceDocument().getId();
443  this.content = newData.getContent();
444  this.annotType = newData.getAnnotType();
445  this.sourceDocument = newData.getSourceDocument();
446  this.confidence = newData.getConfidence();
447  this.alternativeOfSuggestion = newData.getAlternativeOfSuggestion();
448  this.SAEntityIdentifier = newData.getSAEntityIdentifier();
449  List newDataAtts = newData.getAttributes();
450  if (newDataAtts != null && !newDataAtts.isEmpty()) {
451  for (int i = 0; i < newDataAtts.size(); i++) {
452  if (!(newDataAtts.get(i) instanceof AlternativeAttribute)) {
453  continue;
454  }
455  AlternativeAttribute attr = (AlternativeAttribute) newDataAtts.get(i);
456  SugBaseAttribute sba;
457  try {
458  sba = SugAttributeManager.createAttribute("", attr.getSimpleType(), this);
459  } catch (Exception e) {
460  String msg = "Unable to create suggestion attribute with type = " + attr.getSimpleType();
461  Logger.getLogger(Suggestion.class.getName()).log(Level.SEVERE, msg, e);
462  continue;
463  }
464  if (sba == null) {
466  String msg = "Unable to create suggestion attribute with type = " + attr.getSimpleType();
467  Logger.getLogger(Annotation.class.getName()).log(Level.SEVERE, msg);
468  }
469  continue;
470  }
471  sba.updateFromAlternativeAttribut(attr);
472  if (this.attributes == null) {
473  this.attributes = new ArrayList<SugBaseAttribute>();
474  }
475  this.attributes.add(sba);
476  }
477  }
478  else{
479  this.attributes = new ArrayList<SugBaseAttribute>();
480  }
481 
482  Iterator frIt = newData.getFragments().iterator();
483  if (this.fragments == null) {
484  this.fragments = new ArrayList<SuggestionFragment>();
485  }
486  this.fragments.clear();
487  while (frIt.hasNext()) {
488  SuggestionFragment newSugFr = new SuggestionFragment();
489  newSugFr.updateFromAlternativeFragment((AlternativeFragment) frIt.next());
490  newSugFr.setRefSuggestion(this);
491  fragments.add(newSugFr);
492  }
493  displayConfidence = false;
494  } // updateWithAlternativeForDB()
495 
496  /**
497  * Update data of suggestion with annotation data in correct way to save to db
498  * @param newData Annotation data
499  */
500  public final void updateWithAnnotationForDB(Annotation newData){
501  this.type = newData.getAnnotType().getId();
502  this.source = newData.getSource();
503  this.sourceDocumentId = newData.getSourceDocument().getId();
504  this.content = newData.getContent();
505  this.annotType = newData.getAnnotType();
506  this.sourceDocument = newData.getSourceDocument();
507  List newDataAtts = newData.getAttributes();
508  if (newDataAtts != null && !newDataAtts.isEmpty()) {
509  for (int i = 0; i < newDataAtts.size(); i++) {
510  if (!(newDataAtts.get(i) instanceof BaseAttribute)) {
511  continue;
512  }
513  BaseAttribute ba = (BaseAttribute) newDataAtts.get(i);
514  SugBaseAttribute sba;
515  try {
516  sba = SugAttributeManager.createAttribute("", ba.getSimpleType(), this);
517  } catch (Exception e) {
519  String msg = "Unable to create suggestion attribute with type = " + ba.getSimpleType();
520  Logger.getLogger(Annotation.class.getName()).log(Level.SEVERE, msg, e);
521  }
522  continue;
523  }
524  sba.updateFromBaseAttribut(ba);
525  if (this.attributes == null) {
526  this.attributes = new ArrayList<SugBaseAttribute>();
527  }
528  this.attributes.add(sba);
529  }
530  }
531  Iterator<Fragment> frIt = newData.getFragments().iterator();
532  if (this.fragments == null) {
533  this.fragments = new ArrayList<SuggestionFragment>();
534  }
535  this.fragments.clear();
536  while (frIt.hasNext()) {
537  SuggestionFragment newSugFr = new SuggestionFragment();
538  newSugFr.updateFromFragment(frIt.next());
539  newSugFr.setRefSuggestion(this);
540  fragments.add(newSugFr);
541  }
542  displayConfidence = false;
543  } // updateWithAnnotationForDB()
544 
545  // <editor-fold defaultstate="collapsed" desc="Get and set function">
546  /**
547  * Gets id of suggestion
548  *
549  * @return Returns id of suggestion
550  */
551  public Integer getId() {
552  return id;
553  }
554 
555  /**
556  * Sets id of suggestion
557  *
558  * @param id Id of suggestion
559  */
560  public void setId(Integer id) {
561  this.id = id;
562  }
563 
564  /**
565  * Gets type of suggestion
566  *
567  * @return Returns type of suggestion
568  */
570  return annotType;
571  }
572 
573  /**
574  * Sets type of suggestion
575  *
576  * @param annotType Type of suggestion
577  */
578  public void setAnnotType(AnnotType annotType) {
579  this.annotType = annotType;
580  }
581 
582  /**
583  * Gets date of creation
584  *
585  * @return Returns date of creation
586  */
587  public Date getCreated() {
588  return created;
589  }
590 
591  /**
592  * Sets date of creation
593  *
594  * @param created Date of creation
595  */
596  public void setCreated(Date created) {
597  this.created = created;
598  }
599 
600  /**
601  * Gets author of suggestion (user)
602  *
603  * @return Returns author of suggestion (user)
604  */
605  public User getUser() {
606  return user;
607  }
608 
609  /**
610  * Sets author of suggestion (user)
611  * URI is also set and if available, full name and e-mail too
612  *
613  * @param user Author of suggestion (user)
614  */
615  public void setUser(User user) {
616  this.user = user;
617  if (user != null) {
618  authorIdStr = user.getURI();
619  if (user.getName() != null && !user.getName().contentEquals("")) {
620  authorName = user.getName();
621  }
622  if (user.getEmail() != null && !user.getEmail().contentEquals("")) {
623  authorAddress = user.getEmail();
624  }
625  }
626  }
627 
628  /**
629  * Gets URI of author of suggestion (user)
630  *
631  * @return Returns URI of author of suggestion (user)
632  */
633  public String getAuthorIdStr() {
634  return authorIdStr;
635  }
636 
637  /**
638  * Sets URI of author of suggestion (user)
639  *
640  * @param authorIdStr URI of author of suggestion (user)
641  */
642  public void setAuthorIdStr(String authorIdStr) {
643  this.authorIdStr = authorIdStr;
644  }
645 
646  /**
647  * Gets name of author of suggestion (user)
648  *
649  * @return Returns name of author of suggestion (user)
650  */
651  public String getAuthorName() {
652  return authorName;
653  }
654 
655  /**
656  * Sets name of author of suggestion (user)
657  *
658  * @param authorName Name of author of suggestion (user)
659  */
660  public void setAuthorName(String authorName) {
661  this.authorName = authorName;
662  }
663 
664  /**
665  * Gets e-mail of author of suggestion (user)
666  *
667  * @return Returns e-mail of author of suggestion (user)
668  */
669  public String getAuthorAddress() {
670  return authorAddress;
671  }
672 
673  /**
674  * Sets e-mail of author of suggestion (user)
675  *
676  * @param authorAddress E-mail of author of suggestion (user)
677  */
678  public void setAuthorAddress(String authorAddress) {
679  this.authorAddress = authorAddress;
680  }
681 
682  /**
683  * Gets annotated copy of document to which this suggestion belongs
684  *
685  * @return Returns annotated copy of document to which this suggestion belongs
686  */
687  public String getSource() {
688  return source;
689  }
690 
691  /**
692  * Sets annotated copy of document to which this suggestion belongs
693  *
694  * @param source Annotated copy of document to which this suggestion belongs
695  */
696  public void setSource(String source) {
697  this.source = source;
698  }
699 
700  /**
701  * Gets id of annotated copy of document to which this suggestion belongs
702  *
703  * @return Returns id of annotated copy of document to which this suggestion belongs
704  */
705  public Integer getSourceDocumentId() {
706  return sourceDocumentId;
707  }
708 
709  /**
710  * Sets id of annotated copy of document to which this suggestion belongs
711  *
712  * @param sourceDocumentId id of annotated copy of document to which this suggestion belongs
713  */
714  public void setSourceDocumentId(Integer sourceDocumentId) {
715  this.sourceDocumentId = sourceDocumentId;
716  }
717 
718  /**
719  * Gets id of type of suggestion
720  *
721  * @return Returns id of type of suggestion
722  */
723  public Integer getType() {
724  return type;
725  }
726 
727  /**
728  * Sets id of type of suggestion
729  *
730  * @param type id of type of suggestion
731  */
732  public void setType(Integer type) {
733  this.type = type;
734  }
735 
736  /**
737  * Gets textual content of suggestion
738  *
739  * @return Returns textual content of suggestion
740  */
741  public String getContent() {
742  return content;
743  }
744 
745  /**
746  * Sets textual content of suggestion
747  *
748  * @param content Textual content of suggestion
749  */
750  public void setContent(String content) {
751  this.content = content;
752  }
753 
754  /**
755  * Gets id of suggestion author (user)
756  *
757  * @return Returns id of suggestion author (user)
758  */
759  public String getAutorId() {
760  return autorId;
761  }
762 
763  /**
764  * Sets id of suggestion author (user)
765  *
766  * @param autorId id of suggestion author (user)
767  */
768  public void setAutorId(String autorId) {
769  this.autorId = autorId;
770  }
771 
772  /**
773  * Gets annotated copy of document to which this suggestion belongs
774  *
775  * @return Returns annotated copy of document to which this suggestion belongs
776  */
778  return sourceDocument;
779  }
780 
781  /**
782  * Sets annotated copy of document to which this suggestion belongs
783  * (if document is not null, his URI is also set)
784  *
785  * @param sourceDocument Annotated copy of document to which this suggestion belongs
786  */
787  public void setSourceDocument(AnnotDocument sourceDocument) {
788  this.sourceDocument = sourceDocument;
789  if (sourceDocument != null) {
790  this.source = sourceDocument.getUriForAnnot();
791  }
792  }
793 
794  /**
795  * Gets URI of suggestion in which is this suggestion nested
796  *
797  * @return Returns URI of suggestion in which is this suggestion nested
798  */
799  public String getNestedIn() {
800  return nestedIn;
801  }
802 
803  /**
804  * Sets URI of suggestion in which is this suggestion nested
805  *
806  * @param nestedIn URI of suggestion in which is this suggestion nested
807  */
808  public void setNestedIn(String nestedIn) {
809  this.nestedIn = nestedIn;
810  }
811 
812  /**
813  * Gets list of suggestion attributes
814  *
815  * @return Returns list of suggestion attributes
816  */
817  public List<SugBaseAttribute> getAttributes() {
818  return attributes;
819  }
820 
821  /**
822  * Sets list of suggestion attributes
823  *
824  * @param attributes List of suggestion attributes
825  */
826  public void setAttributes(ArrayList<SugBaseAttribute> attributes) {
827  this.attributes = attributes;
828  }
829 
830  /**
831  * Adds an attribute to list of suggestion attributes
832  *
833  * @param attribute Attribute to add
834  */
835  public void addAttribute(SugBaseAttribute attribute) {
836  this.attributes.add(attribute);
837  }
838 
839  /**
840  * Gets list of suggested fragments
841  *
842  * @return Returns list of suggested fragments
843  */
844  @Override
845  public List<SuggestionFragment> getFragments() {
846  return fragments;
847  }
848 
849  /**
850  * Gets list of suggested fragments as ArrayList
851  *
852  * @return Returns list of suggested fragments as ArrayList
853  */
854  public ArrayList<SuggestionFragment> getFragmentsAL() {
855  return new ArrayList<SuggestionFragment>(fragments);
856  }
857 
858  /**
859  * Sets list of suggested fragments
860  *
861  * @param fragments List of suggested fragments
862  */
863  public void setFragments(ArrayList<SuggestionFragment> fragments) {
864  this.fragments = fragments;
865  }
866 
867 
868  /**
869  * Gets parent suggestion of nested suggestion
870  *
871  * @return If this suggestion is nested, returns parent suggestion, null otherwise
872  */
874  return nestedInSuggestion;
875  }
876 
877  /**
878  * Sets parent suggestion of nested suggestion
879  *
880  * @param nestedInSuggestion If this suggestion is nested, then parent suggestion, null otherwise
881  */
882  public void setNestedInSuggestion(Suggestion nestedInSuggestion) {
883  this.nestedInSuggestion = nestedInSuggestion;
884  }
885 
886  /**
887  * Gets suggestion which alternative is this suggestion
888  *
889  * @return Gets suggestion which alternative is this suggestion or null
890  */
892  return alternativeOfSuggestion;
893  }
894 
895  /**
896  * Sets suggestion which alternative is this suggestion
897  *
898  * @param alternativeOfSuggestion Suggestion which alternative is this suggestion
899  */
900  public void setAlternativeOfSuggestion(Suggestion alternativeOfSuggestion) {
901  this.alternativeOfSuggestion = alternativeOfSuggestion;
902  }
903 
904  /**
905  * Gets annotation in which is this suggestion nested
906  *
907  * @return annotation in which is this suggestion nested
908  */
910  return nestedInAnnot;
911  }
912 
913  /**
914  * Sets annotation in which is this suggestion nested
915  *
916  * @param nestedInAnnot Annotation in which is this suggestion nested
917  */
918  public void setNestedInAnnot(Annotation nestedInAnnot) {
919  this.nestedInAnnot = nestedInAnnot;
920  }
921 
922  /**
923  * Gets confidence of this suggestion
924  *
925  * @return Returns confidence of this suggestion
926  */
927  public Integer getConfidence() {
928  return confidence;
929  }
930 
931  /**
932  * Sets confidence of this suggestion
933  *
934  * @param confidence Confidence of this suggestion
935  */
936  public void setConfidence(Integer confidence) {
937  this.confidence = confidence;
938  }
939 
940  /**
941  * Returns whether should be confidence displayed
942  *
943  * @return Returns whether should be confidence displayed
944  */
945  public boolean getDisplayConfidence() {
946  return displayConfidence;
947  }
948 
949  /**
950  * Sets whether should be confidence displayed
951  *
952  * @param displayConfidence Should be confidence displayed?
953  */
954  public void setDisplayConfidence(boolean displayConfidence) {
955  this.displayConfidence = displayConfidence;
956  }
957 
958  /**
959  * Gets tmpId of this suggestion
960  * (warning - tmpId is in id field for suggestion - this is an auxiliary value)
961  *
962  * @return Returns tmpId of this suggestion
963  */
964  public String getTmpId() {
965  return tmpId;
966  }
967 
968  /**
969  * Sets tmpId of this suggestion
970  * (warning - tmpId is in id field for suggestion - this is an auxiliary value)
971  *
972  * @param tmpId tmpId of this suggestion
973  */
974  public void setTmpId(String tmpId) {
975  this.tmpId = tmpId;
976  }
977 
978  /**
979  * Gets isFromSECAPI
980  *
981  * @return isFromSECAPI
982  */
983  public boolean getIsFromSECAPI() {
984  return isFromSECAPI;
985  }
986 
987  /**
988  * Sets isFromSECAPI
989  *
990  * @param isFromSECAPI new isFromSECAPI
991  */
992  public void setIsFromSECAPI(boolean isFromSECAPI) {
993  this.isFromSECAPI = isFromSECAPI;
994  }
995 
996  /**
997  * Gets SEC API Entity Identifier (to detect same suggestions)
998  *
999  * @return isFromSECAPI SEC API Entity Identifier
1000  */
1001  public String getSAEntityIdentifier() {
1002  return SAEntityIdentifier;
1003  }
1004 
1005  /**
1006  * Sets SEC API Entity Identifier
1007  *
1008  * @param SAEntityIdentifier SEC API Entity Identifier
1009  */
1010  @Override
1011  public void setSAEntityIdentifier(String SAEntityIdentifier) {
1012  this.SAEntityIdentifier = SAEntityIdentifier;
1013  }
1014 
1015  // </editor-fold>
1016 
1017  /**
1018  * Adds fragment to the list of suggested fragments
1019  *
1020  * @param fragment Suggested fragment
1021  */
1022  public void addFragment(SuggestionFragment fragment) {
1023  fragments.add(fragment);
1024  }
1025 
1026  /**
1027  * Adds fragment to the list of suggested fragments
1028  *
1029  * @param fragment Suggested fragment
1030  */
1031  @Override
1032  public void addSecFragment(SecFragment fragment) {
1033  if (fragment instanceof SuggestionFragment) {
1034  fragments.add((SuggestionFragment) fragment);
1035  }
1036  }
1037 
1038  @Override
1039  public int hashCode() {
1040  int hash = 0;
1041  hash += (id != null ? id.hashCode() : 0);
1042  return hash;
1043  }
1044 
1045  /**
1046  * Gets URI of suggestion for protocol V2
1047  *
1048  * @return URI of suggestion
1049  */
1050  public String getURIV2() {
1051  if (id == null) {
1052  return "";
1053  }
1054  return AppBean.getBaseUri() + "/sugg/" + id;
1055  }
1056 
1057  /**
1058  * Returns xpointers of all suggestion fragments
1059  *
1060  * @return Returns xpointers of all suggestion fragments
1061  */
1062  public String getFragmentXpointersV2(){
1063  String xpointers = "";
1064 
1065  Iterator<SuggestionFragment> fragIt = fragments.iterator();
1066  while(fragIt.hasNext()){
1067  xpointers += "#" + fragIt.next().getXpointerV2();
1068  }
1069 
1070  return xpointers;
1071  }
1072 
1073  /**
1074  * Returns serialized informations about suggestion in XML for protocol V2
1075  *
1076  * @param attFilter Filter for attributes
1077  * @param langNum Number of language
1078  * @param KBRefMode Is KB_Ref mode on?
1079  * @return Returns serialized informations about suggestion in XML for protocol V2
1080  */
1081  public String toXMLStringV2(ArrayList<String> attFilter, int langNum, Boolean KBRefMode){
1082  if (attFilter == null) {
1083  attFilter = new ArrayList<String>();
1084  attFilter.add(Constants.UPDATABLE_AN_AT_NAME);
1085  }
1086  if (KBRefMode == null) {
1087  KBRefMode = false;
1088  }
1089  StringBuilder result = new StringBuilder();
1090  String suggUri = AppBean.getBaseUri()+ "/sugg/" + id;
1091 
1092  // serialize suggestion + suggestion uri
1093  result.append("<suggestion confidence=\"");
1094  result.append(confidence);
1095  result.append("\" displayConfidence=\"");
1096  if (displayConfidence) {
1097  result.append("true");
1098  } else {
1099  result.append("false");
1100  }
1101  result.append("\">");
1102  result.append("<oa:Annotation rdf:about=\"");
1103  result.append(suggUri);
1104  result.append("\">");
1105 
1106  // serialize annotation type
1107  result.append("<oa:hasBody>");
1108  result.append("<oa:SemanticTag rdf:about=\"");
1109  result.append(annotType.getUri());
1110  result.append("\"/>");
1111  result.append("</oa:hasBody>");
1112 
1113  // serialize fragments
1114  if(fragments == null || fragments.isEmpty()){
1115  // suggestion of whole document
1116  result.append("<oa:hasTarget>");
1117 
1118  result.append("<dctypes:Text rdf:about=\"");
1119  result.append(source);
1120  result.append("\">");
1121  result.append("<dc:format>text/xml</dc:format>");
1122  result.append("</dctypes:Text>");
1123 
1124  result.append("</oa:hasTarget>");
1125  } else if (fragments.size() == 1) {
1126  // suggestion have only one fragment
1127  result.append("<oa:hasTarget>");
1128  result.append(((SuggestionFragment) fragments.get(0)).toXMLStringV2());
1129  result.append("</oa:hasTarget>");
1130  } else if (fragments.size() > 1) {
1131  // suggestion with multiple fragments
1132  result.append("<oa:hasTarget>");
1133  result.append("<oa:Composite rdf:about=\"");
1134  // make uri of composite
1135  result.append(source);
1136  result.append(getFragmentXpointersV2());
1137  result.append("\">");
1138 
1139  Iterator<SuggestionFragment> fragIt = fragments.iterator();
1140  while(fragIt.hasNext()){
1141  SuggestionFragment tmpFrag = fragIt.next();
1142  result.append("<oa:item>");
1143  result.append(tmpFrag.toXMLStringV2());
1144  result.append("</oa:item>");
1145  }
1146 
1147  result.append("</oa:Composite>");
1148  result.append("</oa:hasTarget>");
1149  }
1150 
1151  // serialize suggestion content
1152  result.append("<oa:hasBody>");
1153 
1154  result.append("<cnt:ContentAsText rdf:about=\"");
1155  result.append(suggUri);
1156  result.append("#body\">");
1157 
1158  result.append("<rdf:type rdf:resource=\"http://purl.org/dc/dcmitype/Text\"/>");
1159  result.append("<cnt:chars><![CDATA[");
1160  result.append(content);
1161  result.append("]]></cnt:chars><dc:format>text/plain</dc:format>");
1162  result.append("</cnt:ContentAsText>");
1163  result.append("</oa:hasBody>");
1164 
1165 
1166  // serialize attributes
1167  result.append("<oa:hasBody>");
1168 
1169  result.append("<cnt:ContentAsText rdf:about=\"");
1170  result.append(source);
1171  result.append("\">");
1172 
1173  result.append("<rdf:type rdf:resource=\"http://www.w3.org/2004/03/trix/rdfg-1/Graph\"/>");
1174 
1175  result.append("<trix:TriX>");
1176  result.append("<trix:graph>");
1177 
1178  if (KBRefMode
1179  && !Constants.FORBIDDEN_ENTITY_TYPES.contains(this.getAnnotType().getName().toLowerCase())) {
1180  // KB_Ref mode
1181  result.append(attributesToKBRefString(attFilter, langNum));
1182  } else {
1183  Iterator<SugBaseAttribute> attribIt = attributes.iterator();
1184  while (attribIt.hasNext()) {
1185  SugBaseAttribute tmpAttrib = attribIt.next();
1187  tmpAttrib = SugAttributeManager.changeAttributeInstance(tmpAttrib);
1188  }
1189  String aName = tmpAttrib.getName();
1190  if (attFilter.contains(aName)) {
1191  continue;
1192  }
1193  result.append(tmpAttrib.toXMLStringV2());
1194  }
1195  }
1196 
1197  result.append("</trix:graph>");
1198  result.append("</trix:TriX>");
1199 
1200  result.append("<dc:format>text/xml</dc:format>");
1201  result.append("</cnt:ContentAsText>");
1202  result.append("</oa:hasBody>");
1203 
1204  // serialize time and date of creation
1205  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
1206  String date;
1207  if(created == null){
1208  Date now = new Date();
1209  date = df.format(now.getTime());
1210  }else{
1211  date = df.format(created);
1212  }
1213 
1214  result.append("<oa:annotatedAt>");
1215  result.append(date);
1216  result.append("</oa:annotatedAt>");
1217 
1218  result.append("<oa:serializedAt>");
1219  result.append(date);
1220  result.append("</oa:serializedAt>");
1221 
1222  // end of annotation
1223  result.append("</oa:Annotation>");
1224  result.append("</suggestion>");
1225 
1226  // Serializing all nested suggestions and annotations
1227  Iterator<SugBaseAttribute> attrNestedIt = attributes.iterator();
1228  while(attrNestedIt.hasNext()){
1229  SugBaseAttribute tmpAttrib = attrNestedIt.next();
1230  String aName = tmpAttrib.getName();
1231  if (attFilter.contains(aName)) {
1232  continue;
1233  }
1234  if(tmpAttrib.getNestedAnnotation() != null){
1235  result.append(tmpAttrib.getNestedAnnotation().toXMLStringV2(attFilter, langNum, KBRefMode));
1236  }
1237  if(tmpAttrib.getNestedSuggestion() != null){
1238  result.append(tmpAttrib.getNestedSuggestion().toXMLStringV2(attFilter, langNum, KBRefMode));
1239  }
1240  }
1241 
1242  return result.toString();
1243  } // toXMLStringV2()
1244 
1245  /**
1246  * Returns serialized attributes in KB Ref mode
1247  *
1248  * @param attFilter Filter for attributes
1249  * @param langNum Number of language
1250  * @return Returns serialized attributes in KB Ref mode
1251  */
1252  private StringBuilder attributesToKBRefString(ArrayList<String> attFilter, int langNum) {
1253  StringBuilder result = new StringBuilder();
1254  StringBuilder tmpResult = new StringBuilder();
1255  Iterator<SugBaseAttribute> attribIt = attributes.iterator();
1256  SugEntityAttribute kRefAt = new SugEntityAttribute();
1257  kRefAt.setSimpleType("Entity");
1258  kRefAt.setEntityAdditionalAttributes(new ArrayList<SugEntityAdditionalAttribute>());
1259  kRefAt.setRefSuggestion(this);
1260  kRefAt.setName(Constants.KB_REF_ATT_NAME);
1261  kRefAt.setEntityType(this.getAnnotType().getName());
1262  Boolean nameSet = false;
1263  Boolean uriSet = false; // was URI set?
1264  Boolean vrSet = false;
1265  Boolean desSet = false;
1266  while (attribIt.hasNext()) {
1267  SugBaseAttribute tmpAttrib = attribIt.next();
1268  Boolean processed = false; // was attribute processed?
1270  tmpAttrib = SugAttributeManager.changeAttributeInstance(tmpAttrib);
1271  }
1272  if (tmpAttrib.getName().equalsIgnoreCase(Localisation.getKBStr(langNum, Localisation.KB_ARTWORK_NAME))) {
1273  if (tmpAttrib.getStringValue() != null && !nameSet) {
1274  kRefAt.setEntityName(tmpAttrib.getStringValue());
1275  nameSet = true;
1276  processed = true;
1277  }
1278  } else if (tmpAttrib.getName().equalsIgnoreCase(Localisation.getKBStr(langNum, Localisation.KB_DESCRIPTION))) {
1279  if (SugTextAttribute.class.isInstance(tmpAttrib)) {
1280  processed = true;
1281  if (tmpAttrib.getTextValue() != null) {
1282  if (desSet) {
1283  kRefAt.setDescription(kRefAt.getDescription() + tmpAttrib.getTextValue());
1284  } else {
1285  kRefAt.setDescription(tmpAttrib.getTextValue());
1286  desSet = true;
1287  }
1288  }
1289  } else if (SugStringAttribute.class.isInstance(tmpAttrib)) {
1290  processed = true;
1291  if (tmpAttrib.getStringValue() != null) {
1292  if (desSet) {
1293  kRefAt.setDescription(kRefAt.getDescription() + tmpAttrib.getStringValue());
1294  } else {
1295  kRefAt.setDescription(tmpAttrib.getStringValue());
1296  desSet = true;
1297  }
1298  }
1299  }
1300  } else if (tmpAttrib.getName().equalsIgnoreCase(Localisation.getKBStr(langNum, Localisation.KB_ENTITY_URI))) {
1301  if (tmpAttrib.getUri() != null && !uriSet) {
1302  kRefAt.setUri(tmpAttrib.getUri());
1303  uriSet = true;
1304  processed = true;
1305  }
1306  } else if (tmpAttrib.getName().equalsIgnoreCase(Localisation.getKBStr(langNum, Localisation.KB_VISUAL_REPRESENTATION))) {
1307  if (tmpAttrib.getUri() != null) {
1308  kRefAt.setEntityVisualURI(tmpAttrib.getUri());
1309  vrSet = true;
1310  processed = true;
1311  }
1312  }
1313  if (!processed) {
1314  String aName = tmpAttrib.getName();
1315  if (attFilter.contains(aName)) {
1316  continue;
1317  }
1318  if (tmpAttrib.getName().equalsIgnoreCase(Localisation.getKBStr(langNum, Localisation.KB_DISAMBIGUATION))) {
1319  tmpResult.append(tmpAttrib.toXMLStringV2());
1320  } else { // it is not disambiguation attribute
1321  SugEntityAdditionalAttribute adAt = tmpAttrib.toSugEntityAdditionalAttribute();
1322  if (adAt != null) {
1323  adAt.setRefEntityAttribute(kRefAt);
1324  kRefAt.getEntityAdditionalAttributes().add(adAt);
1325  } else {
1326  tmpResult.append(tmpAttrib.toXMLStringV2());
1327  }
1328  }
1329  } // if (!processed)
1330  } // while (attribIt.hasNext ...
1331  if (nameSet && desSet && uriSet) { // successfull conversion
1332  result.append(kRefAt.toXMLStringV2());
1333  result.append(tmpResult);
1334  } else { // unsuccessfull conversion
1335  attribIt = attributes.iterator();
1336  while (attribIt.hasNext()) {
1337  SugBaseAttribute tmpAttrib = attribIt.next();
1339  tmpAttrib = SugAttributeManager.changeAttributeInstance(tmpAttrib);
1340  }
1341  String aName = tmpAttrib.getName();
1342  if (attFilter.contains(aName)) {
1343  continue;
1344  }
1345  result.append(tmpAttrib.toXMLStringV2());
1346  }
1347  }
1348  return result;
1349  } // attributesToKBRefString()
1350 
1351  /**
1352  * Returns xpointers of all suggestion fragments
1353  *
1354  * @return Returns xpointers of all suggestion fragments
1355  */
1356  public String getFragmentXpointers(){
1357  String xpointers = "";
1358 
1359  Iterator<SuggestionFragment> fragIt = fragments.iterator();
1360  while(fragIt.hasNext()){
1361  xpointers += "#" + fragIt.next().getXpointer();
1362  }
1363 
1364  return xpointers;
1365  }
1366 
1367  /**
1368  * Returns serialized informations about suggestion in XML
1369  *
1370  * @param proto11 If true, protocol version is greather then 1.0
1371  * @return Returns serialized informations about suggestion in XML
1372  */
1373  public String toXMLString(boolean proto11) {
1374  return toXMLString(true, "", proto11);
1375  }
1376 
1377  /**
1378  * Returns serialized informations about suggestion in XML
1379  *
1380  * @param annotTag Denotes whether wrapping tag suggestion should be included
1381  * @param annotTagAttrs Attributes of created suggestion tag
1382  * @param proto11 If true, protocol version is greather then 1.0
1383  * @return Returns serialized informations about suggestion in XML
1384  */
1385  public String toXMLString(boolean annotTag, String annotTagAttrs, boolean proto11) {
1386  String authorAttr = "";
1387  if (authorName != null) {
1388  authorAttr = authorAttr + " name=\"" + authorName + "\"";
1389  }
1390  if (authorAddress != null) {
1391  authorAttr = authorAttr + " address=\"" + authorAddress + "\"";
1392  }
1393  String tIdForD = " tmpId=\"" + id + "\"";
1394  String authorIdString = "";
1395  if (authorIdStr != null) {
1396  authorIdString = authorIdStr;
1397  }
1398 
1399  String tmpContent = "";
1400  if(content != null){
1401  tmpContent = content;
1402  }
1403 
1404  if (annotTag) {
1405  return "<annotation" + annotTagAttrs + ">"
1406  + "<rdf:Description" + tIdForD + " rdf:about=\"\">"
1407  + "<rdf:type rdf:resource=\"" + annotType.getUri() + "\"/>"
1408  + "<a:author id=\"" + authorIdString + "\"" + authorAttr + "/>"
1409  + "<a:source rdf:resource=\"" + source + "\"/>"
1410  + fragmentsToXMLString()
1411  + "<a:content>"
1412  + "<![CDATA["
1413  + tmpContent
1414  + "]]>"
1415  + "</a:content>"
1416  + attributesToXMLString(proto11)
1417  + "</rdf:Description>"
1418  + "</annotation>";
1419  }
1420  return "<rdf:Description" + tIdForD + " rdf:about=\"\">"
1421  + "<rdf:type rdf:resource=\"" + annotType.getUri() + "\"/>"
1422  + "<a:author id=\"" + authorIdString + "\"" + authorAttr + "/>"
1423  + "<a:source rdf:resource=\"" + source + "\"/>"
1424  + fragmentsToXMLString()
1425  + "<a:content>"
1426  + "<![CDATA["
1427  + tmpContent
1428  + "]]>"
1429  + "</a:content>"
1430  + attributesToXMLString(proto11)
1431  + "</rdf:Description>";
1432  } // toXMLString()
1433 
1434  /**
1435  * Returns serialized informations about suggested fragments in XML
1436  *
1437  * @return Returns serialized informations about suggested fragments in XML
1438  */
1439  public String fragmentsToXMLString() {
1440  String fragmentsString = "";
1441  Iterator<SuggestionFragment> fragIt = fragments.iterator();
1442  SuggestionFragment fr;
1443  while (fragIt.hasNext()) {
1444  fr = fragIt.next();
1445  fragmentsString += fr.toXMLString();
1446  }
1447  return fragmentsString;
1448  }
1449 
1450  /**
1451  * Returns serialized informations about suggestion attributes in XML
1452  *
1453  * @param proto11 If true, protocol version is greather then 1.0
1454  * @return Returns serialized informations about suggestion attributes in XML
1455  */
1456  public String attributesToXMLString(boolean proto11) {
1457  String attrString = "";
1458  // a name of the attribute without additional number at the end
1459  String attrNameString;
1460  // the key consists of an attribute name and an attribute simple type
1461  String attrMapKey;
1462  // the value is used in case that the list contains more attributes with
1463  // the same name, the number is appended at the end of the name
1464  // represents number of identical names found so far, the value is obtained
1465  // from the map
1466  Integer attrNameIdx;
1467  HashMap<String, Integer> sameNameCntMap = new HashMap<String, Integer>();
1468 
1469  Collections.sort(attributes);
1470  Iterator<SugBaseAttribute> attrIt = attributes.iterator();
1471  SugBaseAttribute attr;
1472  while (attrIt.hasNext()) {
1473  attr = attrIt.next();
1475  SugAttributeManager.changeAttributeInstance(attr);
1476  }
1477  attrNameString = attr.getName();
1478  attrMapKey = attrNameString.concat(attr.getSimpleType());
1479  if (sameNameCntMap.containsKey(attrMapKey)) {
1480  // the same attribute name was found before, get the count of previously
1481  // found names
1482  attrNameIdx = sameNameCntMap.get(attrMapKey);
1483  attrNameIdx += 1;
1484  // append the number at the end of the name and make an XML string
1485  attr.setName(attrNameString.concat(attrNameIdx.toString()));
1486  attrString += attr.toXMLString(proto11, false);
1487  // restore original name
1488  attr.setName(attrNameString);
1489  // update the entry
1490  sameNameCntMap.put(attrMapKey, attrNameIdx);
1491  } else {
1492  // the attribute name wasn't fount until now, make a map entry
1493  attrString += attr.toXMLString(proto11, false);
1494  sameNameCntMap.put(attrMapKey, 1);
1495  }
1496  }
1497  return attrString;
1498  }
1499 
1500  /**
1501  * Compares this with other object and returns, whether objects are same type
1502  * and have same id.
1503  *
1504  * @param object Object to compare with
1505  * @return If object is same type and have same id, returns true, false otherwise
1506  */
1507  @Override
1508  public boolean equals(Object object) {
1509  if (!(object instanceof Suggestion)) {
1510  return false;
1511  }
1512  Suggestion other = (Suggestion) object;
1513  if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
1514  return false;
1515  }
1516 
1517  if(other.id == null && this.id == null){
1518  if(!contentEquals(object)){
1519  return false;
1520  }
1521  }
1522 
1523  return true;
1524  }
1525 
1526  /**
1527  * Compares this with other object and returns, whether objects are same type
1528  * and have same id, but not go to deeper comparation if noLinked is set to true.
1529  *
1530  * @param object Object to compare with
1531  * @param noLinked No trace links and compare them if is set to true
1532  * @return If object is same type and have same id, returns true, false otherwise
1533  */
1534  public boolean equals(Object object, boolean noLinked){
1535  if (!(object instanceof Suggestion)) {
1536  return false;
1537  }
1538  Suggestion other = (Suggestion) object;
1539  if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
1540  return false;
1541  }
1542 
1543  if(other.id == null && this.id == null){
1544  if(!contentEquals(object,false,noLinked)){
1545  return false;
1546  }
1547  }
1548 
1549  return true;
1550  }
1551 
1552  /**
1553  * Compares this with other object and returns, whether objects are same type
1554  * and have same content (id is irelevant). Contents of fragments
1555  * and attributes are also compared.
1556  *
1557  * @param obj Object to compare with
1558  * @return If object is same type and have same content, returns true, false otherwise
1559  */
1560  public boolean contentEquals(Object obj) {
1561  return contentEquals(obj,false,false);
1562  }
1563 
1564  /**
1565  * Compares this with other object and returns, whether objects are same type
1566  * and have same content (id is irrelevant). Contents of fragments
1567  * and attributes are also compared.
1568  *
1569  * @param obj Object to compare with
1570  * @param withCreated If true, dates of creation will be compared, if false, dates will be omitted
1571  * @param noLinked If true, links in attributes will be omitted, if false, links will be compared
1572  * @return If object is same type and have same content, returns true, false otherwise
1573  */
1574  public boolean contentEquals(Object obj, boolean withCreated, boolean noLinked) {
1575  if (obj == null) {
1576  return false;
1577  }
1578  if (getClass() != obj.getClass()) {
1579  return false;
1580  }
1581  final Suggestion other = (Suggestion) obj;
1582  if (withCreated) {
1583  if (this.created != other.created && (this.created == null || !this.created.equals(other.created))) {
1584  return false;
1585  }
1586  }
1587  if ((this.authorIdStr == null) ? (other.authorIdStr != null) : !this.authorIdStr.equals(other.authorIdStr)) {
1588  return false;
1589  }
1590  if ((this.authorName == null) ? (other.authorName != null) : !this.authorName.equals(other.authorName)) {
1591  return false;
1592  }
1593  if ((this.authorAddress == null) ? (other.authorAddress != null) : !this.authorAddress.equals(other.authorAddress)) {
1594  return false;
1595  }
1596  if ((this.source == null) ? (other.source != null) : !this.source.equals(other.source)) {
1597  return false;
1598  }
1599  if ((this.content == null) ? (other.content != null) : !this.content.equals(other.content)) {
1600  return false;
1601  }
1602  if (this.annotType != other.annotType && (this.annotType == null || !this.annotType.equals(other.annotType))) {
1603  return false;
1604  }
1605  if (this.sourceDocument != other.sourceDocument && (this.sourceDocument == null || !this.sourceDocument.equals(other.sourceDocument))) {
1606  return false;
1607  }
1608 
1609  if (this.attributes == null && other.attributes != null) {
1610  return false;
1611  } else if (this.attributes != null && other.attributes == null) {
1612  return false;
1613  } else if (this.attributes != other.attributes && this.attributes != null && other.attributes != null) {
1614  if (this.attributes.size() != other.attributes.size()) {
1615  return false;
1616  }
1617  Iterator<SugBaseAttribute> atIt = this.attributes.iterator();
1618  while (atIt.hasNext()) {
1619  SugBaseAttribute at = atIt.next();
1620  boolean found = false;
1621  Iterator<SugBaseAttribute> oAtIt = other.attributes.iterator();
1622  if(noLinked && at instanceof SugLinkedAttribute){
1623  continue;
1624  }
1625  while (oAtIt.hasNext()) {
1626  SugBaseAttribute oAt = oAtIt.next();
1627 
1628  if(at instanceof SugLinkedAttribute && oAt instanceof SugLinkedAttribute){
1629  if(at.getValue() != null && ((Suggestion)at.getValue()).equals(oAt,true)){
1630  found = true;
1631  break;
1632  }
1633  }
1634 
1635  if (at.contentEquals(oAt,false)) {
1636  found = true;
1637  break;
1638  }
1639  }
1640  if (!found) {
1641  return false;
1642  }
1643  }
1644  }
1645  if (this.fragments == null && other.fragments != null) {
1646  return false;
1647  } else if (this.fragments != null && other.fragments == null) {
1648  return false;
1649  } else if (this.fragments != other.fragments && this.fragments != null && other.fragments != null) {
1650  if (this.fragments.size() != other.fragments.size()) {
1651  return false;
1652  }
1653  Iterator<SuggestionFragment> frIt = this.fragments.iterator();
1654  while (frIt.hasNext()) {
1655  SuggestionFragment fr = frIt.next();
1656  boolean found = false;
1657  Iterator<SuggestionFragment> oFrIt = other.fragments.iterator();
1658  while (oFrIt.hasNext()) {
1659  SuggestionFragment oFr = oFrIt.next();
1660  if (fr.contentEquals(oFr)) {
1661  found = true;
1662  break;
1663  }
1664  }
1665  if (!found) {
1666  return false;
1667  }
1668  }
1669  }
1670  if (this.user != other.user && (this.user == null || !this.user.equals(other.user))) {
1671  return false;
1672  }
1673  if (this.nestedInSuggestion == null && other.nestedInSuggestion != null) {
1674  return false;
1675  } else if (this.nestedInSuggestion != null && other.nestedInSuggestion == null) {
1676  return false;
1677  }
1678  return true;
1679  } // contentEquals()
1680 
1681  @Override
1682  public String toString() {
1683  return "cz.vutbr.fit.knot.annotations.modules.SuggestionManager.Suggestion[ id=" + id + " ]";
1684  }
1685 
1686  /**
1687  * Gets URI of suggestion
1688  *
1689  * @return URI of suggestion
1690  */
1691  public String getURI() {
1692  if (id == null) {
1693  return "";
1694  }
1695  return AppBean.getBaseSugUri() + id;
1696  }
1697 } // class Suggestion
Suggestion(AnnotType annotType, Date created, String authorIdStr, String authorName, String authorAddress, AnnotDocument sourceDocument, String content)
Class representing attribute of type Text for prupose of suggestion.
Attribute manager provides a way how to create new attributes for prupose of suggestion.
static final ArrayList< String > FORBIDDEN_ENTITY_TYPES
Definition: Constants.java:432
Class representing annotated copy of document.
Singleton for storing global variables.
Definition: AppBean.java:47
Class representing vocabulary entity attribute for prupose of suggestion.
String toXMLStringV2(ArrayList< String > attFilter, int langNum, Boolean KBRefMode)
Suggestion(AnnotType type, String content, String nestedIn)
Suggestion(Integer id, String authorIdStr, String authorName, String authorAddress, AnnotDocument sourceDocument, String content, String nestedIn)
Class representing attribute of type String for prupose of suggestion.
boolean contentEquals(Object obj, boolean withCreated, boolean noLinked)
Base class representing attribute of annotation.
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.
String toXMLString(boolean annotTag, String annotTagAttrs, boolean proto11)
Suggestion(AnnotType annotType, Date created, String authorIdStr, String authorName, String authorAddress, AnnotDocument sourceDocument, String content, Suggestion nestedInAnnot)
void setFragments(ArrayList< SuggestionFragment > fragments)
void setAlternativeOfSuggestion(Suggestion alternativeOfSuggestion)
void setAttributes(ArrayList< SugBaseAttribute > attributes)
Class representing suggestion of annotation.
Definition: Suggestion.java:87
Class responsible for localised strings.
Class representing annotated fragment.
Definition: Fragment.java:48
StringBuilder attributesToKBRefString(ArrayList< String > attFilter, int langNum)
Interface for SuggestionFragment and AlternativeFragment.