4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
AlternativeFragment.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: AlternativeFragment.java
5  * Description: Class representing fragment for suggestion alternative
6  */
7 
8 /**
9  * @file AlternativeFragment.java
10  *
11  * @brief Class representing fragment for suggestion alternative
12  */
13 
14 package cz.vutbr.fit.knot.annotations.modules.suggestionManager.alternative;
15 
20 import java.io.Serializable;
21 import javax.persistence.Basic;
22 import javax.persistence.Column;
23 import javax.persistence.Entity;
24 import javax.persistence.GeneratedValue;
25 import javax.persistence.GenerationType;
26 import javax.persistence.Id;
27 import javax.persistence.JoinColumn;
28 import javax.persistence.Lob;
29 import javax.persistence.ManyToOne;
30 import javax.persistence.NamedQueries;
31 import javax.persistence.NamedQuery;
32 import javax.persistence.Table;
33 import javax.validation.constraints.NotNull;
34 import javax.validation.constraints.Size;
35 import javax.xml.bind.annotation.XmlRootElement;
36 import javax.xml.xpath.XPathExpressionException;
37 
38 /**
39  * Class representing fragment for suggestion alternative
40  *
41  * @brief Class representing fragment for suggestion alternative
42  *
43  * @author Marek Kopecky
44  */
45 @Entity
46 @Table(name = "alternativeFragment")
47 @XmlRootElement
48 @NamedQueries({
49  @NamedQuery(name = "AlternativeFragment.findAll", query = "SELECT a FROM AlternativeFragment a"),
50  @NamedQuery(name = "AlternativeFragment.findById", query = "SELECT a FROM AlternativeFragment a WHERE a.id = :id"),
51  @NamedQuery(name = "AlternativeFragment.findByAlternative", query = "SELECT a FROM AlternativeFragment a WHERE a.alternative = :alternative"),
52  @NamedQuery(name = "AlternativeFragment.findByOffset", query = "SELECT a FROM AlternativeFragment a WHERE a.offset = :frOffset"),
53  @NamedQuery(name = "AlternativeFragment.findByLength", query = "SELECT a FROM AlternativeFragment a WHERE a.length = :frLength"),
54  @NamedQuery(name = "AlternativeFragment.findByIsGood", query = "SELECT a FROM AlternativeFragment a WHERE a.isGood = :isGood"),
55  @NamedQuery(name = "AlternativeFragment.deleteByAltId", query = "DELETE FROM AlternativeFragment a WHERE a.alternative = :altId")
56 })
57 public class AlternativeFragment implements Serializable, Comparable<Object>, SecFragment {
58  private static final long serialVersionUID = 1L;
59 
60  /** Id of fragment */
61  @Id
62  @GeneratedValue(strategy = GenerationType.IDENTITY)
63  @Basic(optional = false)
64  @Column(name = "id")
65  private Integer id;
66 
67  /** Id of alternative to which this fragment belongs */
68  @Basic(optional = false)
69  @NotNull
70  @Column(name = "alternative", nullable = false, insertable = false, updatable = false)
71  private int alternative;
72 
73  /** XPath of element with alternative fragment */
74  @Lob
75  @Size(max = 2147483647)
76  @Column(name = "path")
77  private String path;
78 
79  /** Offset of alternative fragment in given element */
80  @Column(name = "offset")
81  private Integer offset;
82 
83  /** Length of alternative fragment */
84  @Column(name = "fLength")
85  private Integer length;
86 
87  /** Textual content of alternative fragment */
88  @Basic(optional = false)
89  @NotNull
90  @Lob
91  @Size(min = 0, max = 16777215)
92  @Column(name = "annotatedText")
93  private String annotatedText;
94 
95  /**
96  * Indicator, whether fragment is good (not orphaned)
97  */
98  @Basic(optional = false)
99  @NotNull
100  @Column(name = "isGood")
101  private boolean isGood;
102 
103  /**
104  * Alternative to which this fragment belongs
105  */
106  @ManyToOne(optional = false)
107  @JoinColumn(name = "alternative", referencedColumnName = "id")
108  private Alternative refAlternative;
109 
110  /**
111  * Constructor
112  */
114  }
115 
116  /**
117  * Constructor
118  *
119  * @param path XPath of element with alternative fragment
120  * @param offset Offset of alternative fragment in given element
121  * @param length Length of alternative fragment
122  * @param annotatedText Textual content of alternative fragment
123  * @param refAlternative Alternative to which this fragment belongs
124  */
125  public AlternativeFragment(String path, Integer offset, Integer length, String annotatedText, Alternative refAlternative) {
126  this.path = path;
127  this.offset = offset;
128  this.length = length;
129  this.annotatedText = annotatedText;
130  this.refAlternative = refAlternative;
131  this.isGood = true;
132  }
133 
134  /**
135  * Constructor
136  *
137  * @param path XPath of element with annotated fragment
138  * @param offset Offset of annotated fragment in given element
139  * @param length Length of annotated fragment
140  * @param annotatedText Textual content of annotated fragment
141  * @param refAlternative Alternative to which this fragment belongs
142  * @param isGood Indicator, whether fragment is good (not orphaned)
143  */
144  public AlternativeFragment(String path, Integer offset, Integer length, String annotatedText, Alternative refAlternative, Boolean isGood) {
145  this.path = path;
146  this.offset = offset;
147  this.length = length;
148  this.annotatedText = annotatedText;
149  this.refAlternative = refAlternative;
150  this.isGood = isGood;
151  }
152 
153  /**
154  * Constructor
155  *
156  * @param path XPath of element with annotated fragment
157  * @param offset Offset of annotated fragment in given element
158  * @param length Length of annotated fragment
159  */
160  public AlternativeFragment(String path, Integer offset, Integer length) {
161  this.path = path;
162  this.offset = offset;
163  this.length = length;
164  this.isGood = true;
165  }
166 
167  /**
168  * Gets ID of this object
169  *
170  * @return id
171  */
172  public Integer getId() {
173  return id;
174  }
175 
176  /**
177  * Sets ID of this object
178  *
179  * @param id new ID
180  */
181  public void setId(Integer id) {
182  this.id = id;
183  }
184 
185  /**
186  * Gets ID of alternative
187  *
188  * @return ID
189  */
190  public int getAlternative() {
191  return alternative;
192  }
193 
194  /**
195  * Sets ID of alternative
196  *
197  * @param alternative new id
198  */
199  public void setAlternative(int alternative) {
200  this.alternative = alternative;
201  }
202 
203  /**
204  * Gets XPath
205  *
206  * @return XPath
207  */
208  public String getPath() {
209  return path;
210  }
211 
212  /**
213  * Sets XPath
214  * @param path new XPath
215  */
216  public void setPath(String path) {
217  this.path = path;
218  }
219 
220  /**
221  * Gets offset
222  *
223  * @return offset
224  */
225  @Override
226  public Integer getOffset() {
227  return offset;
228  }
229 
230  /**
231  * Sets offset
232  *
233  * @param offset new offset
234  */
235  public void setOffset(Integer offset) {
236  this.offset = offset;
237  }
238 
239  /**
240  * Gets length
241  *
242  * @return length
243  */
244  @Override
245  public Integer getLength() {
246  return length;
247  }
248 
249  /**
250  * Sets length
251  *
252  * @param length new length
253  */
254  public void setLength(Integer length) {
255  this.length = length;
256  }
257 
258  /**
259  * Gets annotated text
260  *
261  * @return annotated text
262  */
263  @Override
264  public String getAnnotatedText() {
265  return annotatedText;
266  }
267 
268  /**
269  * Sets annotated text
270  *
271  * @param annotatedText new annotated text
272  */
273  public void setAnnotatedText(String annotatedText) {
274  this.annotatedText = annotatedText;
275  }
276 
277  /**
278  * Get information about fragment status (whether fragment is good or
279  * orphaned))
280  *
281  * @return If fragment is good, returns true, if it's orphaned, returns false
282  */
283  public boolean getIsGood() {
284  return isGood;
285  }
286 
287  /**
288  * Sets fragment status
289  *
290  * @param isGood If fragment is good then true, if it's orphaned then false
291  */
292  public void setIsGood(boolean isGood) {
293  this.isGood = isGood;
294  }
295 
296  /**
297  * Gets alternative to which this fragment belongs
298  *
299  * @return Returns alternative to which this fragment belongs
300  */
302  return refAlternative;
303  }
304 
305  /**
306  * Gets alternative to which this fragment belongs
307  *
308  * @return Returns alternative to which this fragment belongs
309  */
310  @Override
312  return refAlternative;
313  }
314 
315  /**
316  * Sets alternative to which this fragment belongs
317  *
318  * @param refAlternative Alternative to which this fragment belongs
319  */
320  public void setRefAlternative(Alternative refAlternative) {
321  this.refAlternative = refAlternative;
322  }
323 
324  /**
325  * Sets alternative to which this fragment belongs
326  *
327  * @param value Alternative to which this fragment belongs
328  */
329  @Override
330  public void setRefSecSuggestion(SecSuggestion value) {
331  if (value instanceof Alternative) {
332  this.refAlternative = (Alternative) value;
333  }
334  }
335 
336  /**
337  * Creates UpdatableFragment from this fragment and returns it
338  *
339  * @return UpdatableFragment created from this fragment
340  */
341  public UpdatableFragment toUpdatableFragment() throws XPathExpressionException {
342  UpdatableFragment retUF = new UpdatableFragment(path, offset, length, annotatedText);
343  return retUF;
344  }
345 
346  /**
347  * Updates data in this fragment with data from UpdatableFragment
348  *
349  * @param uf UpdatableFragment with new (updated) data
350  */
352  if (uf.getXPathString().contains("/text()")) {
353  this.path = uf.getXPathString();
354  } else {
355  this.path = uf.getXPathString() + "/text()";
356  }
357  this.offset = uf.getOffset();
358  this.length = uf.getLength();
359  this.annotatedText = uf.getText();
360  }
361 
362  /**
363  * Copies all values of the annotation fragment into the suggestion fragment
364  *
365  * @param fr fragment of the annotation
366  */
367  public void updateFromFragment(Fragment fr){
368  this.path = fr.getPath();
369  this.length = fr.getLength();
370  this.offset = fr.getOffset();
371  this.annotatedText = fr.getAnnotatedText();
372  this.isGood = fr.getIsGood();
373  }
374 
375  /**
376  * Compares this with other object and returns, whether objects are same type
377  * and have same content (id is irrelevant).
378  *
379  * @param object Object to compare with
380  * @return If object is same type and have same id, returns true, false
381  * otherwise
382  */
383  @Override
384  public boolean equals(Object object) {
385  if (!(object instanceof AlternativeFragment)) {
386  return false;
387  }
388  AlternativeFragment other = (AlternativeFragment) object;
389 
390  if (this.path != other.path && (this.path == null || !this.path.equals(other.path))) {
391  return false;
392  }
393  if (this.offset != other.offset && (this.offset == null || !this.offset.equals(other.offset))) {
394  return false;
395  }
396  if (this.length != other.length && (this.length == null || !this.length.equals(other.length))) {
397  return false;
398  }
399  if ((this.annotatedText == null) ? (other.annotatedText != null) : !this.annotatedText.equals(other.annotatedText)) {
400  return false;
401  }
402 
403  return true;
404  }
405 
406  /**
407  * Compares this with other object and returns, whether objects are same type
408  * and have same content (id is irrelevant).
409  *
410  * @param other Object to compare with
411  * @return If object is same type and have same content, returns true, false
412  * otherwise
413  */
414  public boolean contentEqualsForSec(SuggestionFragment other) {
415  if (other == null) {
416  return false;
417  }
418  if ((this.path == null) ? (other.getPath() != null) : !this.path.equals(other.getPath())) {
419  if ((this.path == null) ? (other.getPath() != null) : !this.path.equals(other.getPath().concat("/text()"))) {
420  return false;
421  }
422  }
423  if (this.offset != other.getOffset() && (this.offset == null || !this.offset.equals(other.getOffset()))) {
424  return false;
425  }
426  if (this.length != other.getLength() && (this.length == null || !this.length.equals(other.getLength()))) {
427  return false;
428  }
429  if ((this.annotatedText == null) ? (other.getAnnotatedText() != null) : !this.annotatedText.equals(other.getAnnotatedText())) {
430  return false;
431  }
432  return true;
433  } // contentEqualsWithSuggestion()
434 
435  /**
436  * Compares this with other object and returns, whether objects are same type
437  * and have same content (id is irrelevant).
438  *
439  * @param obj Object to compare with
440  * @return If object is same type and have same content, returns true, false
441  * otherwise
442  */
443  public boolean contentEquals(Object obj) {
444  if (obj == null) {
445  return false;
446  }
447  if (getClass() != obj.getClass()) {
448  return false;
449  }
450  final AlternativeFragment other = (AlternativeFragment) obj;
451  if ((this.path == null) ? (other.path != null) : !this.path.equals(other.path)) {
452  if ((this.path == null) ? (other.path != null) : !this.path.equals(other.path.concat("/text()"))) {
453  return false;
454  }
455  }
456  if (this.offset != other.offset && (this.offset == null || !this.offset.equals(other.offset))) {
457  return false;
458  }
459  if (this.length != other.length && (this.length == null || !this.length.equals(other.length))) {
460  return false;
461  }
462  if ((this.annotatedText == null) ? (other.annotatedText != null) : !this.annotatedText.equals(other.annotatedText)) {
463  return false;
464  }
465  return true;
466  } // contentEquals()
467 
468  /**
469  * Compares this with instance of UpdatableFragment and returns, whether
470  * fragments have same location (XPath, offset and length) and textual
471  * content.
472  *
473  * @param uf Instance of UpdatableFragment to compare with
474  * @return If fragments have same location and textual content, returns true,
475  * false otherwise
476  */
478  if (uf == null) {
479  return false;
480  }
481  if ((this.path == null) ? (uf.getXPathString() != null) : !this.path.equals(uf.getXPathString())) {
482  return false;
483  }
484  if (this.offset != uf.getOffset() && (this.offset == null || !this.offset.equals(uf.getOffset()))) {
485  return false;
486  }
487  if (this.length != uf.getLength() && (this.length == null || !this.length.equals(uf.getLength()))) {
488  return false;
489  }
490  if ((this.annotatedText == null) ? (uf.getText() != null) : !this.annotatedText.equals(uf.getText())) {
491  return false;
492  }
493  return true;
494  } // fragmentEqualsWUF()
495 
496  /**
497  * Returns serialized informations about fragment in XML
498  *
499  * @return Returns serialized informations about fragment in XML
500  */
501  public String toXMLString() {
502  String invalid = "";
503  if (!isGood) {
504  invalid = " valid=\"false\"";
505  }
506  return "<a:fragment" + invalid + ">"
507  + "<a:path>" + path + "</a:path>"
508  + "<a:offset>" + offset + "</a:offset>"
509  + "<a:length>" + length + "</a:length>"
510  + "<a:annotatedText>" + Util.toHTMLString(annotatedText) + "</a:annotatedText>"
511  + "</a:fragment>";
512  }
513 
514  @Override
515  public int hashCode() {
516  int hash = 0;
517  hash += (id != null ? id.hashCode() : 0);
518  hash += (offset != null ? offset.hashCode() : 0);
519  hash += (length != null ? length.hashCode() : 0);
520  hash += (annotatedText != null ? annotatedText.hashCode() : 0);
521  return hash;
522  }
523 
524  @Override
525  public String toString() {
526  return "cz.vutbr.fit.knot.annotations.modules.suggestionManager.SuggestionFragment[ id=" + id + " ]";
527  }
528 
529  /**
530  * Compares this fragment with another fragment according to offset and length
531  *
532  * @param object Fragment to compare
533  * @return Returns a negative integer, zero, or a positive integer as this
534  * object is less than, equal to, or greater than the specified object.
535  */
536  @Override
537  public int compareTo(Object object) {
538  if (!(object instanceof AlternativeFragment)) {
539  throw new UnsupportedOperationException("Not supported yet.");
540  }
541  AlternativeFragment other = (AlternativeFragment) object;
542  int compareResult = offset.compareTo(other.getOffset());
543  if (compareResult == 0) {
544  compareResult = length.compareTo(other.getLength());
545  }
546 
547  return compareResult;
548  }
549 } // public class AlternativeFragment
Utility class (manipulates RFC 3339 dates)
Definition: Util.java:29
Class representing annotated fragment.
Definition: Fragment.java:48
AlternativeFragment(String path, Integer offset, Integer length, String annotatedText, Alternative refAlternative)
AlternativeFragment(String path, Integer offset, Integer length, String annotatedText, Alternative refAlternative, Boolean isGood)
Interface for SuggestionFragment and AlternativeFragment.