4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
BaseAttribute.java
Go to the documentation of this file.
1 /*
2  * Project: Server for annotations sharing
3  * Author: Ing. Jaroslav Dytrych idytrych@fit.vutbr.cz, Jan Planer xplane01@stud.fit.vutbr.cz
4  * File: BaseAttribute.java
5  * Description: Base class representing attribute of annotation
6  */
7 
8 /**
9  * @file BaseAttribute.java
10  *
11  * @brief Base class representing attribute of annotation
12  */
13 
14 /**
15  * @package cz.vutbr.fit.knot.annotations.entity.attribute
16  *
17  * @brief Classes for representing particular types of attributes of annotations
18  */
19 package cz.vutbr.fit.knot.annotations.entity.attribute;
20 
29 import java.io.Serializable;
30 import java.math.BigDecimal;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Date;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
38 import javax.persistence.*;
39 
40 /**
41  * Base class representing attribute of annotation
42  *
43  * @brief Base class representing attribute of annotation
44  */
45 @Entity(name="Attribute")
46 @Inheritance(strategy= InheritanceType.SINGLE_TABLE)
47 @Table(name = "attribute")
48 @DiscriminatorColumn(name="simpleType",discriminatorType=DiscriminatorType.STRING)
49 @NamedQueries({
50  @NamedQuery(name = "Attribute.findAll", query = "SELECT a FROM Attribute a"),
51  @NamedQuery(name = "Attribute.findById", query = "SELECT a FROM Attribute a WHERE a.id = :id"),
52  @NamedQuery(name = "Attribute.findByName", query = "SELECT a FROM Attribute a WHERE a.name = :name"),
53  @NamedQuery(name = "Attribute.findBySimpleType", query = "SELECT a FROM Attribute a WHERE a.simpleType = :simpleType"),
54  @NamedQuery(name = "Attribute.findByType", query = "SELECT a FROM Attribute a WHERE a.type = :type"),
55  @NamedQuery(name = "Attribute.findByAnnotation", query = "SELECT a FROM Attribute a WHERE a.annotation = :annotation"),
56 })
57 public abstract class BaseAttribute implements Serializable, Comparable<Object> {
58 
59  protected static final long serialVersionUID = 1L;
60 
61 
62  /** Id of attribute of annotation */
63  @Id
64  @GeneratedValue(strategy = GenerationType.IDENTITY)
65  @Basic(optional = false)
66  @Column(name = "id")
67  protected Integer id;
68 
69 
70  /** Id of annotation to which this attribute belongs */
71  @Basic(optional = false)
72  @Column(name = "annotation", nullable = false, insertable = false, updatable = false)
73  protected int annotation;
74 
75  /** Name of attribute */
76  @Basic(optional = false)
77  @Column(name = "name")
78  protected String name;
79 
80  /** Name of simple type of attribute (set in structured attributes too) */
81  @Column(name = "simpleType")
82  protected String simpleType;
83 
84  /** Id of structured type of attribute (type of annotation) */
85  @Column(name = "type", insertable = false, updatable = false)
86  protected Integer type;
87 
88  /** Value of attribute type URI or URI of linked annotation or Image */
89  @Column(name = "uri")
90  protected String uri;
91 
92  /** URI in ontology */
93  @Basic(optional = true)
94  @Column(name = "uriInOntology")
95  private String uriInOntology;
96 
97  /** Id of nested annotation (structured value) */
98  @Column(name = "nested", insertable = false, updatable = false)
99  protected String nested;
100 
101  /** Id of linked annotation (can be null in case of foreign annotation) */
102  @Column(name = "linked", insertable = false, updatable = false)
103  protected Integer linked;
104 
105  /** Attribute value of type String */
106  @Lob
107  @Column(name = "stringValue")
108  protected String stringValue;
109 
110  /** Attribute value of type Text */
111  @Lob
112  @Column(name = "textValue")
113  protected String textValue;
114 
115  /** Attribute value of type DateTime, Date or Time */
116  @Column(name = "dateValue")
117  @Temporal(TemporalType.TIMESTAMP)
118  protected Date dateValue;
119 
120  /** Attribute value of type integer */
121  @Column(name = "intValue")
122  protected Integer intValue;
123 
124  /** Attribute value of type Decimal */
125  @Column(name = "decValue")
126  protected BigDecimal decValue;
127 
128  /** Attribute value of type Boolean */
129  @Column(name = "boolVAlue")
130  protected Boolean boolVAlue;
131 
132  /** Attribute value of type Binary */
133  @Lob
134  @Column(name = "binValue")
135  protected byte[] binValue;
136 
137  /** Attribute value of type Person (id of user) */
138  @Column(name = "userValue", insertable = false, updatable = false)
139  protected Integer userValue;
140 
141  /** Attribute value of type GeoPoint - latitude */
142  @Column(name = "geoLat")
143  protected BigDecimal geoLat;
144  /** Attribute value of type GeoPoint - longitude */
145  @Column(name = "geoLong")
146  protected BigDecimal geoLong;
147 
148  /** type of vocalbulary entity */
149  @Column(name = "entityType")
150  protected String entityType;
151 
152  /** uri of image that entity represents */
153  @Column(name = "entityVisualURI")
154  protected String entityVisualURI;
155 
156  /** Comment */
157  @Basic(optional = true)
158  @Lob
159  @Column(name = "commentary")
160  protected String comment;
161 
162  /** Attribute priority (for order) */
163  @Column(name = "priority")
164  protected Integer priority;
165 
166  /** Annotation to which this attribute belongs */
167  @ManyToOne(optional = false)
168  @JoinColumn(name = "annotation", referencedColumnName = "id")
169  protected Annotation refAnnotation;
170 
171  /** Structured type of attribute (type of annotation) */
172  @OneToOne(optional = true)
173  @JoinColumn(name = "type", referencedColumnName = "id")
174  protected AnnotType attributeType;
175 
176  /** Nested annotation (structured value) */
177  @OneToOne(optional = true, cascade = CascadeType.ALL)
178  @JoinColumn(name = "nested", referencedColumnName = "id")
179  protected Annotation nestedAnnotation;
180 
181  /** Linked annotation (can be null in case of foreign annotation) */
182  @OneToOne(optional = true)
183  @JoinColumn(name = "linked", referencedColumnName = "id")
184  protected Annotation linkedAnnotation;
185 
186  /** Attribute value of type Person (user) */
187  @OneToOne(optional = true)
188  @JoinColumn(name = "userValue", referencedColumnName = "id")
189  protected User user;
190 
191  /** List of entity additional attribute */
192  @OneToMany(mappedBy = "refEntityAttribute", cascade = CascadeType.ALL,
193  targetEntity = EntityAdditionalAttribute.class)
194  protected List<EntityAdditionalAttribute> entityAdditionalAttributes;
195 
196  /**
197  * Constructor for base attribute. Note: every extended class MUST HAVE
198  * default constructor without parameters
199  */
200  public BaseAttribute() {
201  }
202 
203  /**
204  * Creates new attribute
205  * @param id id of new attribute
206  */
207  public BaseAttribute(Integer id) {
208  this.id = id;
209  }
210 
211 
212  /**
213  * Just a helper function,which returns XML part for comments.
214  *
215  * @param proto11 If true, protocol version is greater then 1.0
216  * @return XML part for comments
217  */
218  protected String getCommentXmlPart(boolean proto11) {
219  String sCom = "/";
220  if (proto11 && comment != null && !comment.isEmpty()) {
221  sCom = "><a:comment>"
222  + "<![CDATA["
223  + comment
224  + "]]>"
225  + "</a:comment></a:attribute";
226  }
227  return sCom;
228  }
229 
230  /**
231  * Formats attribute value for XML. Function should provide date
232  * converting for date attribute, string escaping for string based
233  * attributes...
234  *
235  * @return string representing attribute value
236  */
237  protected String xmlFormatValue() {
238  if (this.getValue() == null) {
239  return null;
240  }
241  return Util.escapeForXml(this.getValue().toString());
242  }
243 
244  /**
245  * Gets URI of type of attribute in ontology
246  *
247  * @return Returns URI of type of attribute in ontology
248  */
249  public String getTypeOntologyUri(){
250  if (this.attributeType != null) {
251  if (this.attributeType.getUriInOntology() != null) {
252  return this.attributeType.getUriInOntology();
253  }
254  }
255  return "";
256  }
257 
258  /**
259  * Returns serialized informations about attribute of annotation in XML
260  *
261  * @param proto11 If true, protocol version is greater then 1.0
262  * @param tmpIdForNested If true, nested annotations will have attribute tmpId
263  * @param withOntology If true, URI in ontology will be serialized, if false, it will be omitted
264  * @return Returns serialized informations about attribute of annotation in XML
265  */
266  public String toXMLString(boolean proto11, boolean tmpIdForNested, boolean withOntology) {
267  String sCom = this.getCommentXmlPart(proto11);
268  String escapedVal = xmlFormatValue();
269  String ontoString = "";
270 
271  if (escapedVal == null) {
272  escapedVal = "";
273  }
274 
275  if (withOntology) {
276  ontoString += " typeOntologyUri=\"" + getTypeOntologyUri() + "\"";
277  ontoString += " ontologyUri=\"" + getUriInOntology() + "\"";
278  }
279  return "<a:attribute name=\"" + name + "\"" + ontoString + " type=\"" + simpleType + "\" rdf:value=\"" + escapedVal + "\"" + sCom + ">";
280  }
281 
282 
283  /**
284  * Gets value type of attribute
285  *
286  * @return Returns value type of attribute
287  */
288  public String getValueType() {
289  if (this.simpleType != null && !this.simpleType.isEmpty()) {
290  if (this.simpleType.equals("annotationLink")) {
291  return "linked";
292  } else if (this.simpleType.equals("nestedAnnotation")) {
293  return "nested";
294  } else {
295  return "simple";
296  }
297  }
298  if (this.nestedAnnotation != null) {
299  return "nested";
300  }
301  return "linked";
302  }
303 
304  /**
305  * Gets URI of type of attribute for protocol v. 2.0
306  *
307  * @return Returns URI of type of attribute for protocol v. 2.0
308  */
309  public String getTypeUriV2() {
310  int typeIndex = Constants.SIMPLE_TYPES_NAMES_V2.indexOf(simpleType);
311  if (typeIndex != -1) {
312  return Constants.SIMPLE_TYPES_URI_V2.get(typeIndex);
313  } else {
314  if (this.attributeType != null) {
315  return this.attributeType.getUri();
316  }
317  typeIndex = Constants.SIMPLE_TYPES_NAMES_V2.indexOf("AnyAnnotation");
318  if (typeIndex == -1) {
319  return ""; // this should not happen
320  }
321  return Constants.SIMPLE_TYPES_URI_V2.get(typeIndex);
322  }
323  }
324 
325 
326  /**
327  * Returns serialized informations about attribute of annotation in XML for protocol V2
328  *
329  * @return Returns serialized informations about attribute of annotation in XML for protocol V2
330  */
331  public String toXMLStringV2(){
332  StringBuilder result = new StringBuilder();
333  String escapedVal = xmlFormatValue();
334  String ontoString = getUriInOntology();
335  String docUri = refAnnotation.getSource() + refAnnotation.getFragmentXpointersV2();
336 
337  // open triple
338  result.append("<trix:triple>");
339 
340  // make first two triples with document URI + xPointers
341  result.append("<trix:uri>");
342  result.append(docUri);
343  result.append("</trix:uri>");
344 
345  // add triple with name or name from ontology
346  if (ontoString == null || ontoString.isEmpty()) {
347  // if ontology name is not presented
348  result.append("<trix:name>");
349  result.append(Util.toHTMLString(name));
350  result.append("</trix:name>");
351  } else {
352  // if ontology string is avaible
353  result.append("<trix:uri>");
354  result.append(Util.toHTMLString(ontoString));
355  result.append("</trix:uri>");
356  }
357 
358  // make attributes with special pruposes (that is geoPoint and attributes with mor then one triple)
359  if (simpleType.equals("NestedAnnotation")) {
360  result.append(((NestedAnnotationAttribute) this).toXMLStringWHV2(ontoString));
361  } else if (simpleType.equals("AnnotationLink")) {
362  // Linked annotation
363  result.append(((LinkedAnnotationAttribute) this).toXMLStringWHV2(ontoString));
364  } else if (simpleType.equalsIgnoreCase("Entity")) {
365  // Entity
366  EntityAttribute entity = (EntityAttribute) this;
367  result.append(entity.toXMLStringWHV2());
368  } else if (simpleType.equalsIgnoreCase("GeoPoint")) {
369  // geo point
370  result.append("<trix:typedLiteral datatype=\"http://www.w3.org/2003/01/geo/wgs84_pos#Point\">");
371  result.append(((GeoPointAttribute) this).getXmlBody());
372  result.append("</trix:typedLiteral>");
373 
374  // close triple
375  result.append("</trix:triple>");
376  } else if (simpleType.equals("AnyAnnotation")) {
378  result.append(anyAnnot.toXMLStringWHV2(ontoString));
379  } else {
380  // All others simple attributes
381  int index = Constants.SIMPLE_TYPES_NAMES_V2.indexOf(simpleType);
382 
383  result.append("<trix:typedLiteral datatype=\"");
384  result.append(Constants.SIMPLE_TYPES_URI_V2.get(index));
385 
386  if (escapedVal == null) {
387  // attribute is empty
388  result.append("\"/>");
389  } else {
390  result.append("\">");
391  // value with escapet XML chars
392  result.append(escapedVal);
393  result.append("</trix:typedLiteral>");
394  }
395 
396  // close triple
397  result.append("</trix:triple>");
398  }
399 
400  if (priority != null) {
401  result.append("<trix:triple>");
402  result.append("<trix:uri>");
403  result.append(docUri);
404  result.append("</trix:uri>");
405  if (ontoString == null || ontoString.isEmpty()) {
406  // if ontology name is not presented
407  result.append("<trix:name>");
408  result.append(Util.toHTMLString(name));
409  result.append("</trix:name>");
410  } else {
411  // if ontology string is avaible
412  result.append("<trix:uri>");
413  result.append(Util.toHTMLString(ontoString));
414  result.append("</trix:uri>");
415  }
416  result.append("<trix:typedLiteral datatype=\"http://knot.fit.vutbr.cz/annotations/knotOAExtension#attributePriority\">");
417  result.append(priority);
418  result.append("</trix:typedLiteral>");
419  result.append("</trix:triple>");
420  }
421 
422  return result.toString();
423  }
424 
425  /**
426  * Returns serialized informations about attribute of annotation in SXML for protocol V2
427  *
428  * @return Returns serialized informations about attribute of annotation in SXML for protocol V2
429  */
430  public String toSXMLString(){
431  StringBuilder sb = new StringBuilder();
432  String escapedVal = xmlFormatValue();
433 
434  sb.append("<attribute name=\"").append(name);
435  sb.append("\" type=\"").append(simpleType).append("\">");
436 
437  if (simpleType.equalsIgnoreCase("GeoPoint")) {
438  sb.append(((GeoPointAttribute) this).getXmlBody());
439  } else if (simpleType.equalsIgnoreCase("NestedAnnotation")) {
440  //Remove '>'
441  sb.deleteCharAt(sb.length() - 1);
442  sb.append(" annotType=\"").append(attributeType.getLinearizedName()).append("\">");
443  if (nestedAnnotation != null) {
444  sb.append(nestedAnnotation.getURI());
445  }
446  } else if (simpleType.equalsIgnoreCase("LinkedAnnotation") || simpleType.equalsIgnoreCase("AnnotationLink")) {
447  //Remove '>'
448  sb.deleteCharAt(sb.length() - 1);
449  sb.append(" annotType=\"").append(attributeType.getLinearizedName()).append("\">");
450  if (linkedAnnotation != null) {
451  sb.append(linkedAnnotation.getURI());
452  }
453  } else if (simpleType.equalsIgnoreCase("Entity")) {
454  if (entityAdditionalAttributes != null) {
455  Iterator<EntityAdditionalAttribute> enIt = entityAdditionalAttributes.iterator();
456  while (enIt.hasNext()) {
457  EntityAdditionalAttribute tmpAtt = enIt.next();
458  sb.append("<entityAttribute name=\"").append(tmpAtt.getName()).append("\">");
459  if (tmpAtt.getStringValue() != null) {
460  sb.append(tmpAtt.getStringValue());
461  }
462  sb.append("</entityAttribute>");
463  }
464  }
465  } else if (simpleType.equalsIgnoreCase("Text")) {
466  if (this.textValue != null) {
467  sb.append("<![CDATA[").append(this.textValue).append("]]>");
468  }
469  } else {
470  if (escapedVal != null) {
471  sb.append(escapedVal);
472  }
473  }
474 
475  sb.append("</attribute>");
476 
477  return sb.toString();
478  } // toSXMLString()
479 
480  /**
481  * Converts this attribute to SugEntityAdditionalAttribute
482  * Some conversions are not possible as a values are complex
483  *
484  * @return Returns this attribute as SugEntityAdditionalAttribute or null if conversion is not possible
485  */
488 
489  result.setName(name);
491  result.setStringValue("");
492 
493  if (simpleType.equals("NestedAnnotation")) {
494  return null;
495  } else if (simpleType.equals("AnnotationLink") || simpleType.equals("LinkedAnnotation")) {
496  return null;
497  } else if (simpleType.equalsIgnoreCase("Entity")) {
498  return null;
499  } else if (simpleType.equalsIgnoreCase("GeoPoint")) {
500  if (this.getGeoLat() != null && this.getGeoLong() != null) {
501  result.setStringValue(this.getGeoLat().toString() + ", " + this.getGeoLong().toString());
502  }
503  } else if (simpleType.equals("AnyAnnotation")) {
504  return null;
505  } else if (simpleType.equals("Date")) {
506  if (this.getDateValue() != null) {
507  result.setStringValue(Util.toRFC3339DateOnlyWTZ(this.getDateValue()));
508  }
509  } else if (simpleType.equals("DateTime")) {
510  if (this.getDateValue() != null) {
511  result.setStringValue(Util.toRFC3339Date(this.getDateValue()));
512  }
513  } else if (simpleType.equals("Time")) {
514  if (this.getDateValue() != null) {
515  result.setStringValue(Util.toRFC3339Time(this.getDateValue()));
516  }
517  } else if (simpleType.equals("URI")) {
518  if (this.getValue() != null) {
519  result.setStringValue(this.getValue().toString());
520  result.setType("URI");
521  }
522  } else {
523  // All others simple attributes
524  int index = Constants.SIMPLE_TYPES_NAMES_V2.indexOf(simpleType);
525  if (index < 0) {
527  String msg = "Unknown type of attribute of suggestion: " + simpleType;
528  Logger.getLogger(BaseAttribute.class.getName()).log(Level.SEVERE, msg);
529  }
530  return null;
531  }
532 
533  Object val = this.getValue();
534  if (val != null) {
535  result.setStringValue(val.toString());
536  }
537  }
538 
539  return result;
540  } // toEntityAdditionalAttribute()
541 
542  /**
543  * Compares attributes according to priority
544  *
545  * @param o Object to compare with
546  * @return Returns a negative integer, zero, or a positive integer as this
547  * object is less than, equal to, or greater than the specified object.
548  */
549  @Override
550  public int compareTo(Object o) {
551  if (o == null) {
552  return 1;
553  }
554  if (!(o instanceof BaseAttribute)) {
555  throw new UnsupportedOperationException("Not supported yet.");
556  }
557  BaseAttribute other = (BaseAttribute) o;
558  if (this.priority == null && other.getPriority() == null) {
559  return 0;
560  }
561  if (this.priority == null && other.getPriority() != null) {
562  return -1;
563  }
564  if (this.priority != null && other.getPriority() == null) {
565  return 1;
566  }
567  return this.priority.compareTo(other.getPriority());
568  }
569 
570  /**
571  * Compares this with other object and returns, whether objects are same type
572  * and have same content (id is irrelevant). Contents of nested annotations
573  *
574  * are also compared.
575  * @param obj Object to compare with
576  * @return If object is same type and have same content, returns true, false otherwise
577  */
578  public boolean contentEquals(Object obj){
579  return contentEquals(obj,true);
580  }
581 
582  /**
583  * Compares this with other object and returns, whether objects are same type
584  * and have same content (id is irrelevant). Contents of nested annotations
585  * are also compared.
586  *
587  * @param obj Object to compare with
588  * @param refAnnot RefAnnotation will not be checked if the parameter is set to false
589  * @return If object is same type and have same content, returns true, false otherwise
590  */
591  public boolean contentEquals(Object obj, boolean refAnnot) {
592  if (obj == null) {
593  return false;
594  }
595  if (obj instanceof BaseAttribute) {
596  final BaseAttribute other = (BaseAttribute) obj;
597 
598  if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
599  return false;
600  }
601  if (this.userValue != other.userValue && (this.userValue == null || !this.userValue.equals(other.userValue))) {
602  return false;
603  }
604  if (this.geoLat != other.geoLat && (this.geoLat == null || !this.geoLat.equals(other.geoLat))) {
605  return false;
606  }
607  if (this.geoLong != other.geoLong && (this.geoLong == null || !this.geoLong.equals(other.geoLong))) {
608  return false;
609  }
610  if (this.attributeType != other.attributeType && (this.attributeType == null || !this.attributeType.equals(other.attributeType))) {
611  return false;
612  }
613  if (this.comment == null ? other.comment != null : !this.comment.equals(other.comment)) {
614  return false;
615  }
616  if (this.priority == null ? other.priority != null : !this.priority.equals(other.priority)) {
617  return false;
618  }
619 
620  if(refAnnot){
621  if (this.refAnnotation != other.refAnnotation && (this.refAnnotation == null || !this.refAnnotation.equals(other.refAnnotation))) {
622  return false;
623  }
624  }
625 
626  if ((this.simpleType == null) ? (other.simpleType != null) : !this.simpleType.equalsIgnoreCase(other.simpleType)) {
627  return false;
628  }
629  if(this.simpleType != null){
630  if(this.simpleType.equalsIgnoreCase("String") || this.simpleType.equalsIgnoreCase("Duration")){
631  if ((this.stringValue == null) ? (other.stringValue != null) : !this.stringValue.equals(other.stringValue)) {
632  return false;
633  }
634  }
635  else if(this.simpleType.equalsIgnoreCase("Binary")){
636  if (!Arrays.equals(this.binValue, other.binValue)) {
637  return false;
638  }
639  }
640  else if(this.simpleType.equalsIgnoreCase("Date") ||
641  this.simpleType.equalsIgnoreCase("DateTime") ||
642  this.simpleType.equalsIgnoreCase("Time") ||
643  this.simpleType.equalsIgnoreCase("Integer") ||
644  this.simpleType.equalsIgnoreCase("Decimal") ||
645  this.simpleType.equalsIgnoreCase("Boolean")){
646  if (this.getValue() != other.getValue() && (this.getValue() == null || !this.getValue().equals(other.getValue()))) {
647  return false;
648  }
649  }
650  else if(this.simpleType.equalsIgnoreCase("AnnotationLink")){
651  if (this.linkedAnnotation != other.linkedAnnotation &&
652  (this.linkedAnnotation == null || !this.linkedAnnotation.equals(other.linkedAnnotation))) {
653  return false;
654  }
655  }
656  else if(this.simpleType.equalsIgnoreCase("NestedAnnotation")){
657  if (this.nestedAnnotation != other.nestedAnnotation &&
658  (this.nestedAnnotation == null || !this.nestedAnnotation.equals(other.nestedAnnotation))) {
659  if (this.nestedAnnotation != null && other.nestedAnnotation != null) {
660  if (!this.nestedAnnotation.contentEquals(other.nestedAnnotation)) {
661  return false;
662  }
663  } else {
664  return false;
665  }
666  }
667  }
668  else if(this.simpleType.equalsIgnoreCase("Person")){
669  if (this.user != other.user && (this.user == null || !this.user.equals(other.user))) {
670  return false;
671  }
672  }
673  else if(this.simpleType.equalsIgnoreCase("GeoPoint")){
674  if (this.geoLat != other.geoLat && (this.geoLat == null || !this.geoLat.equals(other.geoLat))) {
675  return false;
676  }
677  if (this.geoLong != other.geoLong && (this.geoLong == null || !this.geoLong.equals(other.geoLong))) {
678  return false;
679  }
680  }
681  else if (this.simpleType.equalsIgnoreCase("Entity")) {
682  if (this.uri != other.uri && (this.uri == null || !this.uri.equals(other.uri))) {
683  return false;
684  }
685  if (this.entityType != other.entityType && (this.entityType == null || !this.entityType.equals(other.entityType))) {
686  return false;
687  }
688  if (this.stringValue != other.stringValue && (this.stringValue == null || !this.stringValue.equals(other.stringValue))) {
689  return false;
690  }
691  if (this.entityType != other.entityType && (this.entityType == null || !this.entityType.equals(other.entityType))) {
692  return false;
693  }
694  if (this.entityVisualURI != other.entityVisualURI && (this.entityVisualURI == null || !this.entityVisualURI.equals(other.entityVisualURI))) {
695  return false;
696  }
697  if (this.textValue != other.textValue && (this.textValue == null || !this.textValue.equals(other.textValue))) {
698  return false;
699  }
700  }
701  //AnyAnnotation, Text, URI, Image
702  else{
703  if ((this.getValue() == null) ? (other.getValue() != null) : (!this.getValue().equals(other.getValue()))) {
704  return false;
705  }
706  }
707  }
708  else{
709  if ((this.getValue() == null) ? (other.getValue() != null) : (!this.getValue().equals(other.getValue()))) {
710  return false;
711  }
712  }
713  return true;
714  }
715  return false;
716  }
717 
718  /**
719  * Gets value of the attribute
720  *
721  * @return value of the attribute
722  */
723  public abstract Object getValue();
724 
725 
726  /**
727  * Sets value of the attribute. Value should already be a native java value.
728  *
729  * For example, for attribute of type Integer value should be integer, not
730  * string. If you want to set value and parse that value, you should use
731  * setRawValue
732  *
733  * @param value new value of the attribute
734  */
735  public abstract void setValue(Object value);
736 
737 
738  /**
739  * Parses provided value and sets that value as a value of attribute
740  *
741  * @param value new value of the attribute in raw form from XML
742  */
743  public abstract void setRawValue(String value) throws IllegalArgumentException;
744 
745  /**
746  * Checks if attribute value is empty or default value
747  *
748  * @return true, if attribute value is empty
749  */
750  public boolean isEmpty() {
751  return this.getValue() == null;
752  }
753 
754  /**
755  * Compares this with other object and returns, whether objects are same type
756  * and have same id.
757  *
758  * @param object Object to compare with
759  * @return If object is same type and have same id, returns true, false otherwise
760  */
761  @Override
762  public boolean equals(Object object) {
763  if (!(object instanceof BaseAttribute)) {
764  return false;
765  }
766  BaseAttribute other = (BaseAttribute) object;
767  if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
768  return false;
769  }
770  return true;
771  }
772 
773  /**
774  * Gets textual comment
775  *
776  * @return Returns textual comment
777  */
778  public String getComment() {
779  return comment;
780  }
781 
782  /**
783  * Sets textual comment
784  *
785  * @param comment Textual comment
786  */
787  public void setComment(String comment) {
788  this.comment = comment;
789  }
790 
791  @Override
792  public String toString() {
793  return "cz.vutbr.fit.knot.annotations.entity.Attribute[id=" + id + "]";
794  }
795 
796  @Override
797  public int hashCode() {
798  int hash = 0;
799  hash += (id != null ? id.hashCode() : 0);
800  return hash;
801  }
802 
803  /**
804  * Gets name of attribute
805  *
806  * @return Returns name of attribute
807  */
808  public String getName() {
809  return name;
810  }
811 
812  /**
813  * Sets name of attribute
814  *
815  * @param name Name of attribute
816  */
817  public void setName(String name) {
818  this.name = name;
819  }
820 
821  /**
822  * Gets name of simple type of attribute
823  *
824  * @return Returns name of simple type of attribute
825  */
826  public String getSimpleType() {
827  return simpleType;
828  }
829 
830  /**
831  * Sets name of simple type of attribute
832  *
833  * @param simpleType Name of simple type of attribute
834  */
835  public void setSimpleType(String simpleType) {
836  this.simpleType = simpleType;
837  }
838 
839  /**
840  * Gets id of annotation to which this attribute belongs
841  *
842  * @return Returns id of annotation to which this attribute belongs
843  */
844  public int getAnnotation() {
845  return annotation;
846  }
847 
848  /**
849  * Sets id of annotation to which this attribute belongs
850  *
851  * @param annotation id of annotation to which this attribute belongs
852  */
853  public void setAnnotation(int annotation) {
854  this.annotation = annotation;
855  }
856 
857  /**
858  * Gets annotation to which this attribute belongs
859  *
860  * @return Returns annotation to which this attribute belongs
861  */
863  return refAnnotation;
864  }
865 
866  /**
867  * Sets annotation to which this attribute belongs
868  *
869  * @param value Annotation to which this attribute belongs
870  */
871  public void setRefAnnotation(Annotation value) {
872  this.refAnnotation = value;
873  }
874 
875  /**
876  * Gets structured type of attribute (type of annotation)
877  *
878  * @return Returns structured type of attribute (type of annotation)
879  */
881  return attributeType;
882  }
883 
884  /**
885  * Sets structured type of attribute (type of annotation)
886  *
887  * @param attributeType Structured type of attribute (type of annotation)
888  */
889  public void setAttributeType(AnnotType attributeType) {
890  this.attributeType = attributeType;
891  }
892 
893  /**
894  * Gets nested annotation
895  *
896  * @return Returns nested annotation
897  */
899  return nestedAnnotation;
900  }
901 
902  /**
903  * Gets tmpID of the nested annotation
904  *
905  * @return Returns tmpID of the nested annotation
906  */
907  public String getNestedID() {
908  return nested;
909  }
910 
911  /**
912  * Sets tmpID of the nested annotation
913  */
914  public void setNestedID(String nest) {
915  this.nested = nest;
916  }
917 
918  /**
919  * Sets tmpID of the nested annotation
920  */
921  public void setLinkedID(int link) {
922  this.linked = link;
923  }
924 
925  /**
926  * Sets nested annotation
927  *
928  * @param nestedAnnotation Nested annotation
929  */
930  public void setNestedAnnotation(Annotation nestedAnnotation) {
931  this.nestedAnnotation = nestedAnnotation;
932  }
933 
935  return linkedAnnotation;
936  }
937 
938  public void setLinkedAnnotation(Annotation linkedAnnotation) {
939  this.linkedAnnotation = linkedAnnotation;
940  }
941 
942  /**
943  * Gets id of attribute of annotation
944  *
945  * @return returns id of attribute of annotation
946  */
947  public Integer getId() {
948  return id;
949  }
950 
951  /**
952  * Sets id of attribute of annotation
953  *
954  * @param id Id of attribute of annotation
955  */
956  public void setId(Integer id) {
957  this.id = id;
958  }
959 
960  /**
961  * Gets id of structured type of attribute (type of annotation)
962  *
963  * @return Returns id of structured type of attribute (type of annotation)
964  */
965  public Integer getType() {
966  return type;
967  }
968 
969  /**
970  * Sets id of structured type of attribute (type of annotation)
971  *
972  * @param type id of structured type of attribute (type of annotation)
973  */
974  public void setType(Integer type) {
975  this.type = type;
976  }
977 
978  /**
979  * Checks if attribute is structured. Structured attribute is for example
980  * GeoPointAttribute
981  *
982  * @return true, if attribute is structured
983  */
984  public boolean isStructured() {
985  return false;
986  }
987 
988  /**
989  * Sets attribute value of type Person (user)
990  *
991  * @param user Attribute value of type Person (user)
992  */
993  public void setUser(User user) {
994  this.user = user;
995  }
996 
997  /**
998  * Sets attribute value of uri
999  *
1000  * @param uri Attribute value of uri
1001  */
1002  public void setUri(String uri){
1003  this.uri = uri;
1004  }
1005 
1006  /**
1007  * Gets attribute value of uri
1008  *
1009  * @return Attribute value of uri
1010  */
1011  public String getUri(){
1012  return uri;
1013  }
1014 
1015  /**
1016  * Sets attributes URI in ontology
1017  *
1018  * @param uriInOntology Uri in ontology
1019  */
1020  public void setUriInOntology(String uriInOntology){
1021  this.uriInOntology = uriInOntology;
1022  }
1023 
1024  /**
1025  * Gets attributes URI in ontology
1026  *
1027  * @return URI in ontology
1028  */
1029  public String getUriInOntology(){
1030  return uriInOntology;
1031  }
1032 
1033  /**
1034  * Gets attribute value of entityType
1035  *
1036  * @return Attribute value of entityType
1037  */
1038  public String getEntityType(){
1039  return this.entityType;
1040  }
1041 
1042  /**
1043  * Sets attribute value of entityType
1044  *
1045  * @param entityType Attribute value of entityType
1046  */
1047  public void setEntityType(String entityType) {
1048  this.entityType = entityType;
1049  }
1050 
1051  /**
1052  * Gets attribute value of entityVisualURI
1053  *
1054  * @return Attribute value of entityVisualURI
1055  */
1056  public String getEntityVisualURI(){
1057  return this.entityVisualURI;
1058  }
1059 
1060  /**
1061  * Sets attribute value of entityVisualURI
1062  *
1063  * @param entityVisualURI New value of entityVisualURI
1064  */
1065  public void setEntityVisualURI(String entityVisualURI) {
1066  this.entityVisualURI = entityVisualURI;
1067  }
1068 
1069  /**
1070  * Gets attribute value of boolean
1071  *
1072  * @return Attribute value of boolean
1073  */
1074  public Boolean getBoolValue(){
1075  return this.boolVAlue;
1076  }
1077 
1078  /**
1079  * Gets attribute value of binary
1080  *
1081  * @return Attribute value of binary
1082  */
1083  public byte[] getBinValue(){
1084  return this.binValue;
1085  }
1086 
1087  /**
1088  * Gets attribute value of date
1089  *
1090  * @return Attribute value of date
1091  */
1092  public Date getDateValue(){
1093  return this.dateValue;
1094  }
1095 
1096  /**
1097  * Gets attribute value of decimal
1098  *
1099  * @return Attribute value of decimal
1100  */
1101  public BigDecimal getDecValue(){
1102  return this.decValue;
1103  }
1104 
1105  /**
1106  * Gets attribute value of geo latitude
1107  *
1108  * @return Attribute value of geo latitude
1109  */
1110  public BigDecimal getGeoLat(){
1111  return this.geoLat;
1112  }
1113 
1114  /**
1115  * Gets attribute value of geo longitude
1116  *
1117  * @return Attribute value of geo longitude
1118  */
1119  public BigDecimal getGeoLong(){
1120  return this.geoLong;
1121  }
1122 
1123  /**
1124  * Gets attribute value of integer
1125  *
1126  * @return Attribute value of integer
1127  */
1128  public Integer getIntValue(){
1129  return this.intValue;
1130  }
1131 
1132  /**
1133  * Gets attribute value of string
1134  *
1135  * @return Attribute value of string
1136  */
1137  public String getStringValue(){
1138  return this.stringValue;
1139  }
1140 
1141  /**
1142  * Gets attribute value of text
1143  *
1144  * @return Attribute value of text
1145  */
1146  public String getTextValue(){
1147  return this.textValue;
1148  }
1149 
1150  public void setTextValue(String textValue) {
1151  this.textValue = textValue;
1152  }
1153 
1154  /**
1155  * Gets attribute value of user
1156  *
1157  * @return Attribute value of user
1158  */
1159  public Integer getUserValue(){
1160  return this.userValue;
1161  }
1162 
1163  /**
1164  * Gets attribute priority
1165  *
1166  * @return Returns attribute priority
1167  */
1168  public Integer getPriority() {
1169  return priority;
1170  }
1171 
1172  /**
1173  * Sets attribute priority
1174  *
1175  * @param priority Attribute priority
1176  */
1177  public void setPriority(Integer priority) {
1178  this.priority = priority;
1179  }
1180 
1181  public List<EntityAdditionalAttribute> getEntityAdditionalAttributes() {
1182  return entityAdditionalAttributes;
1183  }
1184  /**
1185  * Stores attribute data into the JSON string representation. String could be
1186  * processed on the NLP server. Method stores IDs of referenced structures, so
1187  * when creating an object from this representation, one must look up for
1188  * structures by IDs.
1189  *
1190  * @return String representation of JSON Attribute data
1191  */
1192  public String toJSONString() {
1193  StringBuilder sb = new StringBuilder("{\"attName\":\"");
1194  sb.append(this.name);
1195  sb.append("\",\"simpleType\":\"");
1196  sb.append(this.simpleType);
1197 
1198  sb.append("\",\"attValue\":\"");
1199  if (this.simpleType.equals("String")) {
1200  if (this.stringValue != null) {
1201  sb.append(this.stringValue);
1202  }
1203  } else if (this.simpleType.equals("Image") || this.simpleType.equals("URI")) {
1204  if (this.uri != null) {
1205  sb.append(this.uri);
1206  }
1207  } else if (this.simpleType.equals("Integer") && this.intValue != null) {
1208  sb.append(this.intValue.toString());
1209  } else if (this.simpleType.equals("Boolean") && this.boolVAlue != null) {
1210  sb.append(this.boolVAlue.toString());
1211  } else if (this.simpleType.equals("Text") && this.textValue != null) {
1212  sb.append(this.textValue);
1213  } else if (this.simpleType.equals("Decimal") && this.decValue != null) {
1214  sb.append(this.decValue.toString());
1215  } else if (this.dateValue != null) {
1216  if (simpleType.equals("DateTime")) {
1217  sb.append(Util.toRFC3339Date(dateValue));
1218  } else if (simpleType.equals("Date")) {
1219  sb.append(Util.toRFC3339DateOnly(dateValue));
1220  } else if (simpleType.equals("Time")) {
1221  sb.append(Util.toRFC3339Time(dateValue));
1222  }
1223  } else if (this.simpleType.equals("Binary") && this.binValue != null) {
1224  sb.append(org.apache.commons.codec.binary.Base64.encodeBase64String(this.binValue));
1225  } else if (this.simpleType.equals("AnyAnnotation")) {
1226  sb.append("null");
1227  } else if (this.simpleType.equals("GeoPoint") || this.simpleType.equals("geoPoint")) {
1228  // no value has been provided; send empty attribute value
1229  if (this.geoLat == null || this.geoLong == null) {
1230  sb.append("null");
1231  }
1232  else {
1233  sb.append(this.geoLat.toString());
1234  sb.append(' ');
1235  sb.append(this.geoLong.toString());
1236  }
1237  } else if(this.simpleType.equals("entity") || this.simpleType.equals("Entity")){
1238  if(this.uri.isEmpty() || this.uri == null){
1239  sb.append("null");
1240  }else{
1241  sb.append(((EntityAttribute.Entity)getValue()).toJSONString());
1242  }
1243  }else if (this.simpleType.equals("Duration")) {
1244  sb.append(this.stringValue);
1245  }
1246  sb.append("\",\"nestedURI\":\"");
1247  if (this.nestedAnnotation != null) {
1248  sb.append(this.nestedAnnotation.getURI());
1249  this.nested = this.nestedAnnotation.getId().toString();
1250  }
1251  else {
1252  sb.append("null");
1253  }
1254  sb.append("\",\"nestedID\":\"null\",\"linkedURI\":\"");
1255  if (this.linkedAnnotation != null) {
1256  sb.append(this.linkedAnnotation.getURI());
1257  this.linked = this.linkedAnnotation.getId();
1258  }
1259  else {
1260  sb.append("null");
1261  }
1262  sb.append("\",\"linkedID\":\"null\",\"structuredType\":");
1263  if (this.linkedAnnotation != null && this.linkedAnnotation.getAnnotType() != null) {
1264  sb.append("\"").append(this.linkedAnnotation.getAnnotType().getUri().split(AppBean.getBaseTypeUri())[1].split("g[0-9]*/", 2)[1]).append("\"");
1265  }
1266  else if (this.nestedAnnotation != null && this.nestedAnnotation.getAnnotType() != null) {
1267  sb.append("\"").append(this.nestedAnnotation.getAnnotType().getUri().split(AppBean.getBaseTypeUri())[1].split("g[0-9]*/", 2)[1]).append("\"");
1268  }
1269  else {
1270  sb.append("null");
1271  }
1272  sb.append("}");
1273  return sb.toString();
1274  } // toJSONString()
1275 
1276  /**
1277  * Copies Attribute class attributes to a new instance
1278  *
1279  * @param ba attribute of the annotation
1280  */
1282  this.annotation = ba.annotation;
1283  this.attributeType = ba.getAttributeType();
1284  this.binValue = ba.getBinValue();
1285  this.boolVAlue = ba.getBoolValue();
1286  this.comment = ba.getComment();
1287  this.dateValue = ba.getDateValue();
1288  this.decValue = ba.getDecValue();
1289  this.entityType = ba.getEntityType();
1290  this.entityVisualURI = ba.getEntityVisualURI();
1291  this.geoLat = ba.getGeoLat();
1292  this.geoLong = ba.getGeoLong();
1293  this.id = ba.getId();
1294  this.intValue = ba.getIntValue();
1295  this.linked= ba.linked;
1296  this.linkedAnnotation = ba.linkedAnnotation;
1297  this.name = ba.getName();
1298  this.nested = ba.nested;
1299  this.nestedAnnotation = ba.getNestedAnnotation();
1300  this.priority = ba.priority;
1301  this.refAnnotation = ba.refAnnotation;
1302  this.stringValue = ba.getStringValue();
1303  this.textValue = ba.getTextValue();
1304  this.type = ba.getType();
1305  this.uri = ba.getUri();
1306  this.uriInOntology = ba.getUriInOntology();
1307  this.user = ba.user;
1308  this.userValue = ba.getUserValue();
1309  this.entityAdditionalAttributes = ba.getEntityAdditionalAttributes();
1310  }
1311 
1312  /**
1313  * Copies SugBaseAttribute class attributes to a Attribute instance
1314  *
1315  * @param ba attribute of the suggestion
1316  */
1318  this.attributeType = ba.getAttributeType();
1319  this.binValue = ba.getBinValue();
1320  this.boolVAlue = ba.getBoolValue();
1321  this.comment = ba.getComment();
1322  this.dateValue = ba.getDateValue();
1323  this.decValue = ba.getDecValue();
1324  this.entityType = ba.getEntityType();
1325  this.entityVisualURI = ba.getEntityVisualURI();
1326  this.geoLat = ba.getGeoLat();
1327  this.geoLong = ba.getGeoLong();
1328  this.id = ba.getId();
1329  this.intValue = ba.getIntValue();
1330  if(ba.getLinkedAnnotation() != null){
1331  this.uri = ba.getLinkedSuggestion().getURIV2();
1332  }
1333  this.name = ba.getName();
1334  if(ba.getNestedAnnotation() != null){
1335  this.uri = ba.getNestedSuggestion().getURIV2();
1336  }
1337  this.priority = ba.getPriority();
1338  this.stringValue = ba.getStringValue();
1339  this.textValue = ba.getTextValue();
1340  this.type = ba.getType();
1341  this.uri = ba.getUri();
1342  this.uriInOntology = ba.getUriInOntology();
1343  this.user = ba.getUser();
1344  this.userValue = ba.getUserValue();
1345  this.entityAdditionalAttributes = new ArrayList<EntityAdditionalAttribute>();
1346  if (ba.getEntityAdditionalAttributes() != null) {
1347  Iterator<SugEntityAdditionalAttribute> seatit = ba.getEntityAdditionalAttributes().iterator();
1348  if (this.getClass().equals(EntityAttribute.class)) {
1349  while (seatit.hasNext()) {
1350  SugEntityAdditionalAttribute seat = seatit.next();
1351  EntityAdditionalAttribute nAt = new EntityAdditionalAttribute(seat.getName(), seat.getType(), seat.getStringValue(), (EntityAttribute) this);
1352  nAt.setPriority(seat.getPriority());
1353  this.entityAdditionalAttributes.add(nAt);
1354  }
1355  } else if (!ba.getEntityAdditionalAttributes().isEmpty()) {
1357  Logger.getLogger(BaseAttribute.class.getName()).log(Level.SEVERE, "Trying to update attribute using attribute with different type.");
1358  }
1359  }
1360  }
1361 
1362  }
1363 
1364 } // class BaseAttribute
List< EntityAdditionalAttribute > getEntityAdditionalAttributes()
Singleton for storing global variables.
Definition: AppBean.java:47
Class representing vocabulary entity attribute.
String toXMLString(boolean proto11, boolean tmpIdForNested, boolean withOntology)
Base class representing attribute of annotation.
Class representing type of annotation.
Definition: AnnotType.java:58
Class representing user.
Definition: User.java:51
static final String DEFAULT_SIMPLE_TYPE_FOR_ENT_AD_AT
Definition: Constants.java:225
Utility class (manipulates RFC 3339 dates)
Definition: Util.java:29