4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
TypeAttrOnto.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: TypeAttrOnto.java
5  * Description: Class representing attribute of unknown type of annotation
6  * imported from ontology
7  */
8 
9 /**
10  * @file TypeAttrOnto.java
11  *
12  * @brief Class representing attribute of unknown type of annotation
13  */
14 
15 package cz.vutbr.fit.knot.annotations.entity;
16 
17 import com.hp.hpl.jena.graph.query.Expression;
21 import java.io.Serializable;
22 import javax.persistence.Basic;
23 import javax.persistence.Column;
24 import javax.persistence.Entity;
25 import javax.persistence.GeneratedValue;
26 import javax.persistence.GenerationType;
27 import javax.persistence.Id;
28 import javax.persistence.JoinColumn;
29 import javax.persistence.Lob;
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 unknown type of annotation imported from ontology
37  *
38  * @brief Class representing attribute of unknown type of annotation
39  * @author idytrych
40  */
41 @Entity
42 @Table(name = "typeAttrOnto")
43 @NamedQueries({
44  @NamedQuery(name = "TypeAttrOnto.findAll", query = "SELECT a FROM TypeAttrOnto a"),
45  @NamedQuery(name = "TypeAttrOnto.findById", query = "SELECT a FROM TypeAttrOnto a WHERE a.id = :id"),
46  @NamedQuery(name = "TypeAttrOnto.findByUserGroup", query = "SELECT a FROM TypeAttrOnto a WHERE a.groupId = :groupId"),
47  @NamedQuery(name = "TypeAttrOnto.findByName", query = "SELECT a FROM TypeAttrOnto a WHERE a.name = :name"),
48  @NamedQuery(name = "TypeAttrOnto.findByNameAndGroup", query = "SELECT a FROM TypeAttrOnto a WHERE a.name = :name AND a.groupId = :groupId"),
49  @NamedQuery(name = "TypeAttrOnto.findBySimpleType", query = "SELECT a FROM TypeAttrOnto a WHERE a.simpleType = :simpleType"),
50  @NamedQuery(name = "TypeAttrOnto.findByType", query = "SELECT a FROM TypeAttrOnto a WHERE a.type = :type")})
51 public class TypeAttrOnto implements Serializable {
52  private static final long serialVersionUID = 1L;
53  /** Id of attribute of type of annotation */
54  @Id
55  @GeneratedValue(strategy = GenerationType.IDENTITY)
56  @Basic(optional = false)
57  @Column(name = "id")
58  private Integer id;
59  /** Id of user group to which this attribute belongs */
60  @Basic(optional = true)
61  @Column(name = "groupId", insertable=false, updatable=false)
62  private Integer groupId;
63  /** Name of attribute */
64  @Basic(optional = false)
65  @Column(name = "name")
66  private String name;
67  /** Name of simple type of attribute */
68  @Column(name = "simpleType")
69  private String simpleType;
70  /** Id of type of attribute in case that this is attribute of structured type
71  * (type of annotation)
72  */
73  @Basic(optional = true)
74  @Column(name = "type", insertable=false, updatable=false)
75  private Integer type;
76  /** URI in original ontology */
77  @Basic(optional = true)
78  @Column(name = "uriInOntology")
79  private String uriInOntology;
80  /** Comment */
81  @Basic(optional = true)
82  @Lob
83  @Column(name = "commentary")
84  private String comment;
85 
86  /** Type of attribute in case that this is attribute of structured type (type of annotation) */
87  @OneToOne(optional = true)
88  @JoinColumn(name = "type", referencedColumnName = "id")
89  private AnnotType attributeType;
90  /** User group to which this attribute belongs */
91  @OneToOne(optional = true)
92  @JoinColumn(name = "groupId", referencedColumnName = "id")
93  private UserGroup group;
94 
95  /**
96  * Constructor
97  */
98  public TypeAttrOnto() {
99  }
100 
101  /**
102  * Constructor
103  *
104  * @param id id of attribute of type of annotation
105  */
106  public TypeAttrOnto(Integer id) {
107  this.id = id;
108  }
109 
110  /**
111  * Constructor of attribute of simple type
112  *
113  * @param group User group to which attribute belongs
114  * @param name Name of attribute
115  * @param simpleType Name of simple type of attribute
116  */
117  public TypeAttrOnto(UserGroup group, String name, String simpleType) {
118  this.group = group;
119  this.name = name;
120  this.simpleType = simpleType;
121  this.type = null;
122  this.attributeType = null;
123  this.uriInOntology = null;
124  this.comment = null;
125  }
126 
127  /**
128  * Constructor of attribute of structured type
129  *
130  * @param group User group to which attribute belongs
131  * @param name Name of attribute
132  * @param structuredType Type of structured attribute (type of annotation)
133  */
134  public TypeAttrOnto(UserGroup group, String name, AnnotType structuredType) {
135  this.group = group;
136  this.name = name;
137  this.simpleType = null;
138  this.type = structuredType.getId();
139  this.attributeType = structuredType;
140  this.uriInOntology = null;
141  this.comment = null;
142  }
143 
144  /**
145  * Gets id of attribute of type of annotation
146  *
147  * @return Returns id of attribute of type of annotation
148  */
149  public Integer getId() {
150  return id;
151  }
152 
153  /**
154  * Sets id of attribute of type of annotation
155  *
156  * @param id Id of attribute of type of annotation
157  */
158  public void setId(Integer id) {
159  this.id = id;
160  }
161 
162  /**
163  * Gets user group to which attribute belongs
164  *
165  * @return Returns user group to which attribute belongs
166  */
167  public UserGroup getGroup() {
168  return group;
169  }
170 
171  /**
172  * Sets user group to which attribute belongs
173  *
174  * @param group User group to which attribute belongs
175  */
176  public void setGroup(UserGroup group) {
177  this.group = group;
178  }
179 
180  /**
181  * Gets name of attribute of type of annotation
182  *
183  * @return Returns name of attribute of type of annotation
184  */
185  public String getName() {
186  return name;
187  }
188 
189  /**
190  * Sets name of attribute of type of annotation
191  *
192  * @param name Name of attribute of type of annotation
193  */
194  public void setName(String name) {
195  this.name = name;
196  }
197 
198  /**
199  * Gets name of simple type of annotation
200  *
201  * @return If type of attribute is simple, returns name of type, else returns ""
202  */
203  public String getSimpleType() {
204  return simpleType;
205  }
206 
207  /**
208  * Sets name of simple type of annotation (only for attributes of simple type)
209  *
210  * @param simpleType Name of simple type of annotation ("" for structured type)
211  */
212  public void setSimpleType(String simpleType) {
213  this.simpleType = simpleType;
214  }
215 
216  /**
217  * Gets id of type of attribute in case that this is attribute of structured type
218  * (type of annotation)
219  *
220  * @return If type of attribute is structured, returns id of type of annotation, else returns null
221  */
222  public Integer getType() {
223  return type;
224  }
225 
226  /**
227  * Sets id of type of attribute in case that this is attribute of structured type
228  * (type of annotation)
229  *
230  * @param type new type
231  */
232  public void setType(Integer type) {
233  this.type = type;
234  }
235 
236  /**
237  * Gets type of attribute in case that this is attribute of structured type
238  * (type of annotation)
239  *
240  * @return If type of attribute is structured, returns type of annotation, else returns null
241  */
243  return attributeType;
244  }
245 
246  /**
247  * Sets structured type of attribute (only for attribute of structured type)
248  *
249  * @param attributeType Structured type of attribute (type of annotation), or null for simple type
250  */
251  public void setAttributeType(AnnotType attributeType) {
252  this.attributeType = attributeType;
253  }
254 
255  /**
256  * Gets URI in original ontology
257  *
258  * @return Returns URI in original ontology
259  */
260  public String getUriInOntology() {
261  return uriInOntology;
262  }
263 
264  /**
265  * Sets URI in original ontology
266  *
267  * @param uriInOntology URI in original ontology
268  */
269  public void setUriInOntology(String uriInOntology) {
270  this.uriInOntology = uriInOntology;
271  }
272 
273  /**
274  * Gets textual comment
275  *
276  * @return Returns textual comment
277  */
278  public String getComment() {
279  return comment;
280  }
281 
282  /**
283  * Sets textual comment
284  *
285  * @param comment Textual comment
286  */
287  public void setComment(String comment) {
288  this.comment = comment;
289  }
290 
291  @Override
292  public int hashCode() {
293  int hash = 0;
294  hash += (id != null ? id.hashCode() : 0);
295  return hash;
296  }
297 
298  /**
299  * Compares this with other object and returns, whether objects are same type
300  * and have same id.
301  *
302  * @param object Object to compare with
303  * @return If object is same type and have same id, returns true, false otherwise
304  */
305  @Override
306  public boolean equals(Object object) {
307  if (!(object instanceof TypeAttrOnto)) {
308  return false;
309  }
310  TypeAttrOnto other = (TypeAttrOnto) object;
311  if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
312  return false;
313  }
314  return true;
315  }
316 
317  /**
318  * Compares this with other object and returns, whether objects are same type
319  * and have same content (id is irelevant).
320  *
321  * @param obj Object to compare with
322  * @return If object is same type and have same content, returns true, false otherwise
323  */
324  public boolean contentEquals(Object obj) {
325  if (obj == null) {
326  return false;
327  }
328  if (getClass() != obj.getClass()) {
329  return false;
330  }
331  final TypeAttrOnto other = (TypeAttrOnto) obj;
332  if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
333  return false;
334  }
335  if ((this.simpleType == null) ? (other.simpleType != null) : !this.simpleType.equals(other.simpleType)) {
336  return false;
337  }
338  if (this.type != other.type && (this.type == null || !this.type.equals(other.type))) {
339  return false;
340  }
341  if (this.attributeType != other.attributeType && (this.attributeType == null || !this.attributeType.equals(other.attributeType))) {
342  return false;
343  }
344  if (this.comment == null ? other.comment != null : !this.comment.equals(other.comment)) {
345  return false;
346  }
347  return true;
348  }
349 
350  @Override
351  public String toString() {
352  return "cz.vutbr.fit.knot.annotations.entity.TypeAttrOnto[id=" + id + ",name=" + name + ",simpleType=" + simpleType + ",type=" + type + "]";
353  }
354 
355  /**
356  * Returns serialized informations about attribute of simple type in XML
357  *
358  * @return Returns serialized informations about attribute of simple type in XML
359  */
360  private String simpleTypeToString() {
361  String serializedCom = "/";
362  if (comment != null && !comment.isEmpty()) {
363  serializedCom = "><a:comment>"
364  + "<![CDATA["
365  + comment
366  + "]]>"
367  + "</a:comment></a:attribute";
368  }
369  String grStr = "";
370  if (group != null) {
371  grStr = " group=\"" + group.getUri() + "\"";
372  }
373 
374  try {
375  BaseAttribute buf = AttributeManager.createAttribute(name, simpleType, null);
376  return "<a:attribute name=\"" + name + "\" type=\"" + buf.getSimpleType() + "\"" + grStr + serializedCom + ">";
377  }
378  // attribute not found
379  catch (ClassNotFoundException e) {
380  return "";
381  }
382  }
383 
384  /**
385  * Returns serialized informations about attribute of type of annotation in XML
386  *
387  * @return Returns serialized informations about attribute of type of annotation in XML
388  */
389  public String toXMLString() {
390  if (simpleType == null || simpleType.isEmpty()) {
391  String serializedCom = "/";
392  if (comment != null && !comment.isEmpty()) {
393  serializedCom = "><a:comment>"
394  + "<![CDATA["
395  + comment
396  + "]]>"
397  + "</a:comment></a:attribute";
398  }
399  String grStr = "";
400  if (group != null) {
401  grStr = " group=\"" + group.getUri() + "\"";
402  }
403  return "<a:attribute name=\"" + name + "\" type=\"" + attributeType.getUri()
404  + "\"" + grStr + serializedCom + ">";
405  }
406  return simpleTypeToString();
407  }
408 
409  /**
410  * Returns serialized informations about attribute of type of annotation in XML for protocol V2
411  *
412  * @return Returns serialized informations about attribute of type of annotation in XML for protocol V2
413  */
414  public String toXMLStringV2(){
415  StringBuilder result = new StringBuilder();
416 
417  result.append("<attribute name=\"");
418  result.append(name);
419  result.append("\" uri=\"");
420  result.append(uriInOntology);
421  result.append("\" typeUri=\"");
422 
423  if(simpleType != null && !simpleType.isEmpty()){
424  if(Constants.SIMPLE_TYPES_NAMES_V2.contains(simpleType)){
425  Integer post = Constants.SIMPLE_TYPES_NAMES_V2.indexOf(simpleType);
426  result.append(Constants.SIMPLE_TYPES_URI_V2.get(post));
427  }else{
428  result.append(simpleType);
429  }
430  }
431  else{
432  result.append(attributeType.getUri());
433  }
434 
435  result.append("\"");
436 
437  if (group != null) {
438  result.append(" groupUri=\"");
439  result.append(group.getUri());
440  result.append("\"");
441  }
442 
443  if (comment != null && !comment.isEmpty()) {
444  result.append("><comment><![CDATA[");
445  result.append(comment);
446  result.append("]]></comment></attribute>");
447  }else{
448  result.append("/>");
449  }
450 
451  return result.toString();
452  }
453 
454 } // class TypeAttrOnto
Class representing attribute of unknown type of annotation.
void setAttributeType(AnnotType attributeType)
TypeAttrOnto(UserGroup group, String name, String simpleType)
static final ArrayList< String > SIMPLE_TYPES_NAMES_V2
Definition: Constants.java:194
Class representing user group.
Definition: UserGroup.java:47
Base class representing attribute of annotation.
Class representing type of annotation.
Definition: AnnotType.java:58
TypeAttrOnto(UserGroup group, String name, AnnotType structuredType)
Class attribute manager provides a way how to create new attributes.