4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
AnnotTypeAttr.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: AnnotTypeAttr.java
5  * Description: Class representing attribute of type of annotation
6  */
7 
8 /**
9  * @file AnnotTypeAttr.java
10  *
11  * @brief Class representing attribute of type of annotation
12  */
13 
14 package cz.vutbr.fit.knot.annotations.entity;
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.OneToOne;
33 import javax.persistence.Table;
34 
35 /**
36  * Class representing attribute of type of annotation
37  *
38  * @brief Class representing attribute of type of annotation
39  * @author idytrych
40  */
41 @Entity
42 @Table(name = "annotTypeAttr")
43 @NamedQueries({
44  @NamedQuery(name = "AnnotTypeAttr.findAll", query = "SELECT a FROM AnnotTypeAttr a"),
45  @NamedQuery(name = "AnnotTypeAttr.findById", query = "SELECT a FROM AnnotTypeAttr a WHERE a.id = :id"),
46  @NamedQuery(name = "AnnotTypeAttr.findByAnnotType", query = "SELECT a FROM AnnotTypeAttr a WHERE a.annotType = :annotType"),
47  @NamedQuery(name = "AnnotTypeAttr.findByName", query = "SELECT a FROM AnnotTypeAttr a WHERE a.name = :name"),
48  @NamedQuery(name = "AnnotTypeAttr.findBySimpleType", query = "SELECT a FROM AnnotTypeAttr a WHERE a.simpleType = :simpleType"),
49  @NamedQuery(name = "AnnotTypeAttr.findByType", query = "SELECT a FROM AnnotTypeAttr a WHERE a.type = :type")})
50 public class AnnotTypeAttr implements Serializable, Comparable<Object> {
51  private static final long serialVersionUID = 1L;
52  /** Id of attribute of type of annotation */
53  @Id
54  @GeneratedValue(strategy = GenerationType.IDENTITY)
55  @Basic(optional = false)
56  @Column(name = "id")
57  private Integer id;
58  /** Id of type of annotation to which this attribute belongs */
59  @Basic(optional = false)
60  @Column(name = "annotType", nullable=false, insertable=false, updatable=false)
61  private int annotType;
62  /** Name of attribute */
63  @Basic(optional = false)
64  @Column(name = "name")
65  private String name;
66  /** Name of simple type of attribute */
67  @Column(name = "simpleType")
68  private String simpleType;
69  /** Id of type of attribute in case that this is attribute of structured type
70  * (type of annotation)
71  */
72  @Basic(optional = true)
73  @Column(name = "type", insertable=false, updatable=false)
74  private Integer type;
75  /** Indicates whether this attribute is required */
76  @Basic(optional = false)
77  @Column(name = "required")
78  private Boolean required;
79  /** URI in original ontology */
80  @Basic(optional = true)
81  @Column(name = "uriInOntology")
82  private String uriInOntology;
83  /** Comment */
84  @Basic(optional = true)
85  @Lob
86  @Column(name = "commentary")
87  private String comment;
88  /** Attribute priority (for order) */
89  @Column(name = "priority")
90  protected Integer priority;
91 
92  /** Type of annotation to which this attribute belongs */
93  @ManyToOne(optional = false)
94  @JoinColumn(name = "annotType", referencedColumnName = "id")
95  private AnnotType annotationType;
96  /** Type of attribute in case that this is attribute of structured type (type of annotation) */
97  @OneToOne(optional = true)
98  @JoinColumn(name = "type", referencedColumnName = "id")
99  private AnnotType attributeType;
100 
101  /**
102  * Constructor
103  */
104  public AnnotTypeAttr() {
105  }
106 
107  /**
108  * Constructor
109  *
110  * @param id id of attribute of type of annotation
111  */
112  public AnnotTypeAttr(Integer id) {
113  this.id = id;
114  }
115 
116  /**
117  * Constructor of attribute of simple type
118  *
119  * @param annotationType Type of annotation to which this attribute belongs
120  * @param name Name of attribute
121  * @param simpleType Name of simple type of attribute
122  * @param required Indicator whether this attribute is required
123  */
124  public AnnotTypeAttr(AnnotType annotationType, String name, String simpleType, Boolean required) {
125  this.annotationType = annotationType;
126  this.name = name;
127  this.simpleType = simpleType;
128  this.type = null;
129  this.attributeType = null;
130  this.required = required;
131  this.uriInOntology = null;
132  this.comment = null;
133  }
134 
135  /**
136  * Constructor of attribute of simple type
137  *
138  * @param annotationType Type of annotation to which this attribute belongs
139  * @param name Name of attribute
140  * @param simpleType Name of simple type of attribute
141  * @param required Indicator whether this attribute is required
142  * @param priority Priority of attribute
143  */
144  public AnnotTypeAttr(AnnotType annotationType, String name, String simpleType, Boolean required, Integer priority) {
145  this.annotationType = annotationType;
146  this.name = name;
147  this.simpleType = simpleType;
148  this.type = null;
149  this.attributeType = null;
150  this.required = required;
151  this.uriInOntology = null;
152  this.comment = null;
153  this.priority = priority;
154  }
155 
156  /**
157  * Constructor of attribute of structured type
158  *
159  * @param annotationType Type of annotation to which this attribute belongs
160  * @param name Name of attribute
161  * @param structuredType Type of structured attribute (type of annotation)
162  * @param required Indicator whether this attribute is required
163  */
164  public AnnotTypeAttr(AnnotType annotationType, String name, AnnotType structuredType, Boolean required) {
165  this.annotationType = annotationType;
166  this.name = name;
167  this.simpleType = null;
168  this.type = structuredType.getId();
169  this.attributeType = structuredType;
170  this.required = required;
171  this.uriInOntology = null;
172  this.comment = null;
173  }
174 
175  /**
176  * Constructor of attribute of structured type that is inicialized from another
177  * attribute.
178  *
179  * @param annotTypeAttr another attribute
180  */
181  public AnnotTypeAttr(AnnotTypeAttr annotTypeAttr) {
182  this.name = annotTypeAttr.getName();
183  this.simpleType = annotTypeAttr.getSimpleType();
184  this.type = annotTypeAttr.getType();
185  this.attributeType = annotTypeAttr.getAttributeType();
186  this.required = annotTypeAttr.getRequired();
187  this.uriInOntology = annotTypeAttr.getUriInOntology();
188  this.comment = annotTypeAttr.getComment();
189  }
190 
191  /**
192  * Gets id of attribute of type of annotation
193  *
194  * @return Returns id of attribute of type of annotation
195  */
196  public Integer getId() {
197  return id;
198  }
199 
200  /**
201  * Sets id of attribute of type of annotation
202  *
203  * @param id Id of attribute of type of annotation
204  */
205  public void setId(Integer id) {
206  this.id = id;
207  }
208 
209  /**
210  * Gets id of type of annotation to which this attribute belongs
211  *
212  * @return Returns id of type of annotation to which this attribute belongs
213  */
214  public int getAnnotType() {
215  return annotType;
216  }
217 
218  /**
219  * Sets id of type of annotation to which this attribute belongs
220  *
221  * @param annotType id of type of annotation to which this attribute belongs
222  */
223  public void setAnnotType(int annotType) {
224  this.annotType = annotType;
225  }
226 
227  /**
228  * Gets name of attribute of type of annotation
229  *
230  * @return Returns name of attribute of type of annotation
231  */
232  public String getName() {
233  return name;
234  }
235 
236  /**
237  * Sets name of attribute of type of annotation
238  *
239  * @param name Name of attribute of type of annotation
240  */
241  public void setName(String name) {
242  this.name = name;
243  }
244 
245  /**
246  * Gets name of simple type of annotation
247  *
248  * @return If type of attribute is simple, returns name of type, else returns ""
249  */
250  public String getSimpleType() {
251  return simpleType;
252  }
253 
254  /**
255  * Sets name of simple type of annotation (only for attributes of simple type)
256  *
257  * @param simpleType Name of simple type of annotation ("" for structured type)
258  */
259  public void setSimpleType(String simpleType) {
260  this.simpleType = simpleType;
261  }
262 
263  /**
264  * Gets id of type of attribute in case that this is attribute of structured type
265  * (type of annotation)
266  *
267  * @return If type of attribute is structured, returns id of type of annotation, else returns null
268  */
269  public Integer getType() {
270  return type;
271  }
272 
273  /**
274  * Sets id of type of attribute in case that this is attribute of structured type
275  * (type of annotation)
276  *
277  * @param type id of type of attribute in case that this is attribute of structured type
278  */
279  public void setType(Integer type) {
280  this.type = type;
281  }
282 
283  /**
284  * Gets type of annotation to which this attribute belongs
285  *
286  * @return Returns type of annotation to which this attribute belongs
287  */
289  return annotationType;
290  }
291 
292  /**
293  * Sets type of annotation to which this attribute belongs
294  *
295  * @param annotationType Type of annotation to which this attribute belongs
296  */
297  public void setAnnotationType(AnnotType annotationType) {
298  this.annotationType = annotationType;
299  }
300 
301  /**
302  * Gets type of attribute in case that this is attribute of structured type
303  * (type of annotation)
304  *
305  * @return If type of attribute is structured, returns type of annotation, else returns null
306  */
308  return attributeType;
309  }
310 
311  /**
312  * Sets structured type of attribute (only for attribute of structured type)
313  *
314  * @param attributeType Structured type of attribute (type of annotation), or null for simple type
315  */
316  public void setAttributeType(AnnotType attributeType) {
317  this.attributeType = attributeType;
318  }
319 
320  /**
321  * Gets indicator whether this attribute is required
322  *
323  * @return If this attribute is required, returns true, false otherwise
324  */
325  public Boolean getRequired() {
326  return required;
327  }
328 
329  /**
330  * Sets indicator whether this attribute is required
331  *
332  * @param required If this attribute is required, then true, false otherwise
333  */
334  public void setRequired(Boolean required) {
335  this.required = required;
336  }
337 
338  /**
339  * Gets URI in original ontology
340  *
341  * @return Returns URI in original ontology
342  */
343  public String getUriInOntology() {
344  return uriInOntology;
345  }
346 
347  /**
348  * Sets URI in original ontology
349  *
350  * @param uriInOntology URI in original ontology
351  */
352  public void setUriInOntology(String uriInOntology) {
353  this.uriInOntology = uriInOntology;
354  }
355 
356  /**
357  * Gets textual comment
358  *
359  * @return Returns textual comment
360  */
361  public String getComment() {
362  return comment;
363  }
364 
365  /**
366  * Sets textual comment
367  *
368  * @param comment Textual comment
369  */
370  public void setComment(String comment) {
371  this.comment = comment;
372  }
373 
374  /**
375  * Gets attribute priority
376  *
377  * @return Returns attribute priority
378  */
379  public Integer getPriority() {
380  return priority;
381  }
382 
383  /**
384  * Sets attribute priority
385  *
386  * @param priority Attribute priority
387  */
388  public void setPriority(Integer priority) {
389  this.priority = priority;
390  }
391 
392  /**
393  * Compares attributes according to priority
394  *
395  * @param o Object to compare with
396  * @return Returns a negative integer, zero, or a positive integer as this
397  * object is less than, equal to, or greater than the specified object.
398  */
399  @Override
400  public int compareTo(Object o) {
401  if (o == null) {
402  return 1;
403  }
404  if (!(o instanceof AnnotTypeAttr)) {
405  throw new UnsupportedOperationException("Not supported yet.");
406  }
407  AnnotTypeAttr other = (AnnotTypeAttr) o;
408  if (this.priority == null && other.getPriority() == null) {
409  return 0;
410  }
411  if (this.priority == null && other.getPriority() != null) {
412  return -1;
413  }
414  if (this.priority != null && other.getPriority() == null) {
415  return 1;
416  }
417  return this.priority.compareTo(other.getPriority());
418  }
419 
420  @Override
421  public int hashCode() {
422  int hash = 0;
423  hash += (id != null ? id.hashCode() : 0);
424  return hash;
425  }
426 
427  /**
428  * Compares this with other object and returns, whether objects are same type
429  * and have same id.
430  *
431  * @param object Object to compare with
432  * @return If object is same type and have same id, returns true, false otherwise
433  */
434  @Override
435  public boolean equals(Object object) {
436  if (!(object instanceof AnnotTypeAttr)) {
437  return false;
438  }
439  AnnotTypeAttr other = (AnnotTypeAttr) object;
440  if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
441  return false;
442  }
443  return true;
444  }
445 
446  /**
447  * Compares this with other object and returns, whether objects are same type
448  * and have same content (id is irelevant).
449  *
450  * @param obj Object to compare with
451  * @return If object is same type and have same content, returns true, false otherwise
452  */
453  public boolean contentEquals(Object obj) {
454  if (obj == null) {
455  return false;
456  }
457  if (getClass() != obj.getClass()) {
458  return false;
459  }
460  final AnnotTypeAttr other = (AnnotTypeAttr) obj;
461  if (this.annotType != other.annotType) {
462  return false;
463  }
464  if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
465  return false;
466  }
467  if ((this.simpleType == null) ? (other.simpleType != null) : !this.simpleType.equals(other.simpleType)) {
468  return false;
469  }
470  if (this.type != other.type && (this.type == null || !this.type.equals(other.type))) {
471  return false;
472  }
473  if (this.required != other.required && (this.required == null || !this.required.equals(other.required))) {
474  return false;
475  }
476  if (this.annotationType != other.annotationType && (this.annotationType == null || !this.annotationType.equals(other.annotationType))) {
477  return false;
478  }
479  if (this.attributeType != other.attributeType && (this.attributeType == null || !this.attributeType.equals(other.attributeType))) {
480  return false;
481  }
482  if (this.comment == null ? other.comment != null : !this.comment.equals(other.comment)) {
483  return false;
484  }
485  if (this.priority == null ? other.priority != null : !this.priority.equals(other.priority)) {
486  return false;
487  }
488  return true;
489  }
490 
491  @Override
492  public String toString() {
493  return "cz.vutbr.fit.knot.annotations.entity.AnnotTypeAttr[id=" + id + ",name=" + name + ",simpleType=" + simpleType + ",type=" + type + "]";
494  }
495 
496  /**
497  * Returns serialized informations about attribute of simple type in XML
498  *
499  * @param withComment If true, comment will be serialized, if false, it will be omitted
500  * @return Returns serialized informations about attribute of simple type in XML
501  */
502  private String simpleTypeToString(boolean withComment) {
503  String rq = "";
504  if (required) {
505  rq = " required=\"true\"";
506  }
507  String serializedCom = "/";
508  if (withComment && comment != null && !comment.isEmpty()) {
509  serializedCom = "><a:comment>"
510  + "<![CDATA["
511  + comment
512  + "]]>"
513  + "</a:comment></a:attribute";
514  }
515 
516  try {
517  BaseAttribute buf = AttributeManager.createAttribute(name, simpleType, null);
518  if (!(buf instanceof AnyAnnotationAttribute) || withComment)
519  return "<a:attribute name=\"" + name + "\" type=\"" + buf.getSimpleType() + "\"" + rq + serializedCom + ">";
520  return "";
521  } catch (ClassNotFoundException e) {
522  return "";
523  }
524  }
525 
526  /**
527  * Returns serialized informations about attribute of type of annotation in XML
528  *
529  * @param withComment If true, comment will be serialized, if false, it will be omitted
530  * @return Returns serialized informations about attribute of type of annotation in XML
531  */
532  public String toXMLString(boolean withComment) {
533  if (simpleType == null || simpleType.isEmpty()) {
534  String rq = "";
535  if (required) {
536  rq = " required=\"true\"";
537  }
538  String serializedCom = "/";
539  if (withComment && comment != null && !comment.isEmpty()) {
540  serializedCom = "><a:comment>"
541  + "<![CDATA["
542  + comment
543  + "]]>"
544  + "</a:comment></a:attribute";
545  }
546  return "<a:attribute name=\"" + name + "\" type=\"" + attributeType.getUri()
547  + "\"" + rq + serializedCom + ">";
548  }
549  return simpleTypeToString(withComment);
550  }
551 
552  /**
553  * Gets type of value of attribute for protocol v. 2.0
554  *
555  * @return Returns type of value of attribute for protocol v 2.0
556  */
557  public String getValueType() {
558  if (this.simpleType == null) {
559  return "linked";
560  }
561  if(this.simpleType.equalsIgnoreCase("annotationLink")){
562  return "linked";
563  } else if (this.simpleType.equalsIgnoreCase("nestedAnnotation")) {
564  return "nested";
565  } else {
566  return "simple";
567  }
568  }
569 
570  /**
571  * Gets URI of type of attribute for protocol v. 2.0
572  *
573  * @return REturns URI of type of attribute for protocol v. 2.0
574  */
575  public String getTypeUriV2() {
576  int typeIndex = Constants.SIMPLE_TYPES_NAMES_V2.indexOf(simpleType);
577  if (typeIndex != -1) {
578  return Constants.SIMPLE_TYPES_URI_V2.get(typeIndex);
579  } else {
580  return this.attributeType.getUri();
581  }
582  }
583 
584  /**
585  * Returns serialized informations about attribute of type of annotation in XML
586  *
587  * @param withComment If true, comment will be serialized, if false, it will be omitted
588  * @return Returns serialized informations about attribute of type of annotation in XML
589  */
590  public String toXMLStringV2(boolean withComment){
591  StringBuilder serializedAnnotTypeAttr = new StringBuilder();
592 
593  // make comment
594  StringBuilder serializedCom = null;
595  if (withComment && this.comment != null && !this.comment.isEmpty()) {
596  serializedCom = new StringBuilder();
597  serializedCom.append("<comment><![CDATA[");
598  serializedCom.append(this.comment);
599  serializedCom.append("]]></comment>");
600  }
601 
602  if (simpleType != null && !simpleType.isEmpty()) {
603  // is simple type
604  serializedAnnotTypeAttr.append("<attribute valueType=\"");
605  serializedAnnotTypeAttr.append(getValueType());
606  serializedAnnotTypeAttr.append("\" name=\"");
607  serializedAnnotTypeAttr.append(this.name);
608  serializedAnnotTypeAttr.append("\" typeUri=\"");
609  serializedAnnotTypeAttr.append(getTypeUriV2());
610  serializedAnnotTypeAttr.append("\" required=\"");
611  serializedAnnotTypeAttr.append(this.required.toString());
612  serializedAnnotTypeAttr.append("\"");
613  if(this.uriInOntology != null && !this.uriInOntology.isEmpty()){
614  serializedAnnotTypeAttr.append(" ontologyUri=\"");
615  serializedAnnotTypeAttr.append(this.uriInOntology);
616  serializedAnnotTypeAttr.append("\"");
617  }
618  if(serializedCom == null){
619  serializedAnnotTypeAttr.append("/>");
620  }else{
621  serializedAnnotTypeAttr.append(">");
622  serializedAnnotTypeAttr.append(serializedCom.toString());
623  serializedAnnotTypeAttr.append("</attribute>");
624  }
625 
626  return serializedAnnotTypeAttr.toString();
627  } else {
628  // is other annotation type
629  serializedAnnotTypeAttr.append("<attribute valueType=\"");
630  serializedAnnotTypeAttr.append(getValueType());
631  serializedAnnotTypeAttr.append("\" name=\"");
632  serializedAnnotTypeAttr.append(this.name);
633  serializedAnnotTypeAttr.append("\" typeUri=\"");
634  serializedAnnotTypeAttr.append(this.attributeType.getUri());
635  serializedAnnotTypeAttr.append("\" required=\"");
636  serializedAnnotTypeAttr.append(this.required.toString());
637  serializedAnnotTypeAttr.append("\"");
638  if(this.uriInOntology != null && !this.uriInOntology.isEmpty()){
639  serializedAnnotTypeAttr.append(" ontologyUri=\"");
640  serializedAnnotTypeAttr.append(this.uriInOntology);
641  serializedAnnotTypeAttr.append("\"");
642  }
643  if(serializedCom == null){
644  serializedAnnotTypeAttr.append("/>");
645  }else{
646  serializedAnnotTypeAttr.append(">");
647  serializedAnnotTypeAttr.append(serializedCom.toString());
648  serializedAnnotTypeAttr.append("</attribute>");
649  }
650 
651  return serializedAnnotTypeAttr.toString();
652  }
653  } // toXMLStringV2()
654 
655 } // class AnnotTypeAttr
Class representing attribute of type of annotation.
AnnotTypeAttr(AnnotType annotationType, String name, String simpleType, Boolean required, Integer priority)
Base class representing attribute of annotation.
Class representing type of annotation.
Definition: AnnotType.java:58
AnnotTypeAttr(AnnotType annotationType, String name, AnnotType structuredType, Boolean required)
void setAnnotationType(AnnotType annotationType)
Class attribute manager provides a way how to create new attributes.
AnnotTypeAttr(AnnotType annotationType, String name, String simpleType, Boolean required)