4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
LinkedAnnotationAttribute.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: LinkedAnnotationAttribute.java
5  * Description: Class representing attribute of type AnnotationLink
6  */
7 
8 package cz.vutbr.fit.knot.annotations.entity.attribute;
9 
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.logging.Level;
17 import java.util.logging.Logger;
18 import javax.persistence.DiscriminatorValue;
19 import javax.persistence.Entity;
20 import javax.persistence.NamedQueries;
21 import javax.persistence.NamedQuery;
22 import javax.persistence.Transient;
23 
24 /**
25  * @file LinkedAnnotationAttribute.java
26  *
27  * @brief Class representing attribute of type AnnotationLink
28  */
29 
30 /**
31  * Class representing attribute of type AnnotationLink
32  *
33  * @brief Class representing attribute of type AnnotationLink
34  */
35 @Entity
36 @DiscriminatorValue(value = "AnnotationLink")
37 @NamedQueries({
38  @NamedQuery(name = "Attribute.findByLinked", query = "SELECT a FROM Attribute a WHERE a.linked = :linked"),})
40 
41  /** Temporary id for creating links between annotations from client */
42  @Transient
43  private String tmpId = null;
44 
45  /**
46  * Sets value of the attribute. Value should already be a native java value.
47  *
48  * @param value New value of the attribute
49  */
50  @Override
51  public void setValue(Object value) {
52  if (value != null) {
53  this.linkedAnnotation = (Annotation) value;
54  this.uri = this.linkedAnnotation.getURI();
55  this.linked = linkedAnnotation.getId();
56  }else{
57  this.linkedAnnotation = null;
58  this.uri = "";
59  this.linked = null;
60  }
61  }
62 
63  /**
64  * Gets value of the attribute
65  *
66  * @return value of the attribute
67  */
68  @Override
69  public Object getValue() {
70  return this.linkedAnnotation;
71  }
72 
73  /**
74  * Returns serialized informations about attribute of annotation in XML
75  * for protocol v. 2.0 without header (target and attribute specification
76  * triples)
77  *
78  * @param ontologyUri URI of attribute in ontology
79  * @return Returns serialized informations about attribute of annotation in XML
80  */
81  public String toXMLStringWHV2(String ontologyUri){
82  StringBuilder result = new StringBuilder();
83 
84  if(linkedAnnotation != null){
85  // serialize triple with information about link
86  result.append("<trix:uri>");
87  result.append(linkedAnnotation.getURIV2());
88  result.append("</trix:uri>");
89  result.append("</trix:triple>");
90 
91  // <document uri><attr name><uri of type>
92  result.append("<trix:triple>");
93  result.append("<trix:uri>");
94  result.append(this.refAnnotation.getSource()).append(this.refAnnotation.getFragmentXpointersV2());
95  result.append("</trix:uri>");
96 
97  // serialize triple with name
98  if (ontologyUri == null || ontologyUri.isEmpty()) {
99  result.append("<trix:name>");
100  result.append(this.name);
101  result.append("</trix:name>");
102  } else {
103  result.append("<trix:uri>");
104  result.append(ontologyUri);
105  result.append("</trix:uri>");
106  }
107 
108  // serialize triple with information about link
109  result.append("<trix:uri>");
110  result.append("koae:linkedAnnotation");
111  result.append("</trix:uri>");
112  result.append("</trix:triple>");
113 
114  // <document uri><attr name><type uri>
115  result.append("<trix:triple>");
116  result.append("<trix:uri>");
117  result.append(this.refAnnotation.getSource()).append(this.refAnnotation.getFragmentXpointersV2());
118  result.append("</trix:uri>");
119 
120  // serialize triple with name
121  if (ontologyUri == null || ontologyUri.isEmpty()) {
122  result.append("<trix:name>");
123  result.append(this.name);
124  result.append("</trix:name>");
125  } else {
126  result.append("<trix:uri>");
127  result.append(ontologyUri);
128  result.append("</trix:uri>");
129  }
130 
131  // serialize triple with information about link
132  result.append("<trix:uri>");
133  result.append(linkedAnnotation.getAnnotType().getUri());
134  result.append("</trix:uri>");
135  result.append("</trix:triple>");
136 
137  }else{
138  result.append("<trix:uri>");
139  result.append(getAttributeType().getUri());
140  result.append("</trix:uri>");
141  // end of first trix
142  result.append("</trix:triple>");
143 
144  // serilize triple with document uri
145  result.append("<trix:triple>");
146  result.append("<trix:uri>");
147  result.append(this.refAnnotation.getSource()).append(this.refAnnotation.getFragmentXpointersV2());
148  result.append("</trix:uri>");
149 
150  // serialize triple with name
151  if(ontologyUri == null || ontologyUri.isEmpty()){
152  result.append("<trix:name>");
153  result.append(this.name);
154  result.append("</trix:name>");
155  }else{
156  result.append("<trix:uri>");
157  result.append(ontologyUri);
158  result.append("</trix:uri>");
159  }
160 
161 
162  // serialize triple with information about link
163  result.append("<trix:uri>");
164  result.append("koae:linkedAnnotation");
165  result.append("</trix:uri>");
166  result.append("</trix:triple>");
167  }
168 
169  return result.toString();
170  }
171 
172  /**
173  * Returns serialized informations about attribute of annotation in XML
174  *
175  * @param proto11 If true, protocol version is greater then 1.0
176  * @param tmpIdForNested If true, nested annotations will have attribute tmpId
177  * @param withOntology If true, URI in ontology will be serialized, if false, it will be omitted
178  * @return Returns serialized informations about attribute of annotation in
179  * XML
180  */
181  @Override
182  public String toXMLString(boolean proto11, boolean tmpIdForNested, boolean withOntology) {
183  String ontoString = "";
184 
185  if (withOntology) {
186  if (getTypeOntologyUri() != null && !getTypeOntologyUri().isEmpty()) {
187  ontoString += " typeOntologyUri=\"" + getTypeOntologyUri() + "\"";
188  }
189 
190  Iterator<AnnotTypeAttr> typeAttrIt = refAnnotation.getAnnotType().getAttributes().iterator();
191  while (typeAttrIt.hasNext()) {
192  AnnotTypeAttr actualAttr = typeAttrIt.next();
193  if (name.equals(actualAttr.getName())) {
194  if (actualAttr.getUriInOntology() != null) {
195  ontoString += " ontologyUri=\"" + actualAttr.getUriInOntology() + "\"";
196  }
197  }
198  }
199  }
200 
201  String uriString;
202 
203  if(uri == null || uri.isEmpty()){
204  uriString = "";
205  }else{
206  uriString = " uri=\"" + uri + "\"";
207  }
208 
209  if (this.getValue() == null && tmpId != null) {
210  return "<a:attribute name=\"" + name + "\"" + ontoString + " type=\"annotationLink\" tmpId=\""
211  + tmpId + "\"" + getCommentXmlPart(proto11) + ">";
212  } else if(this.getValue() == null){
213  if(attributeType != null){
214  return "<a:attribute name=\"" + name + "\"" + ontoString + " type=\"" + attributeType.getUri() + "\"" + uriString
215  + getCommentXmlPart(proto11) + ">";
216  }else{
218  String msg = "Unknown type of attribute created.";
219  Logger.getLogger(LinkedAnnotationAttribute.class.getName()).log(Level.SEVERE, msg);
220  }
221  return "<a:attribute name=\"" + name + "\"" + ontoString + " type=\"\"" + uriString
222  + getCommentXmlPart(proto11) + ">";
223  }
224  } else {
225  return "<a:attribute name=\"" + name + "\"" + ontoString + " type=\"annotationLink\"" + uriString
226  + getCommentXmlPart(proto11) + ">";
227  }
228  }
229 
230  /**
231  * Parses provided value and sets that value as a value of attribute
232  *
233  * @param value new value of the attribute in raw form from XML
234  */
235  @Override
236  public void setRawValue(String value) throws IllegalArgumentException {
237  if (value == null) {
238  value = "";
239  }
240  this.uri = value;
241 
242  // try getting id of annotation by stripping off base URI
243  String annotIdStr = value.replace(AppBean.getBaseAnnotUri(), "");
244  Integer annotId;
245  Annotation linkedAnnnot;
246 
247  // parse id of annotation
248  try {
249  annotId = Integer.parseInt(annotIdStr);
250  } // parsing failed - it can be foreign annotation
251  catch (NumberFormatException nfe) {
252  annotId = null;
253  }
254 
255  // if id of annotation is available
256  if (annotId != null) {
257  // query database for linked annotation
258  Object[] params = new Object[2];
259  params[0] = "id";
260  params[1] = annotId;
261  List aList = AppBean.getPersistenceManager().queryDB("Annotation.findById", params);
262 
263  // if annotation was found
264  if (aList != null && !aList.isEmpty()) {
265  linkedAnnnot = (Annotation) aList.get(0);
266  this.linkedAnnotation = linkedAnnnot;
267  this.setAttributeType(linkedAnnnot.getAnnotType());
268  this.uri = linkedAnnnot.getURI();
269  }
270  }
271  } // setRawValue()
272 
273  /**
274  * Gets tmpId of annotation
275  *
276  * @return Returns tmpId of annotation
277  */
278  public String getTmpId() {
279  return this.tmpId;
280  }
281 
282  /**
283  * Sets tmpId of annotation
284  *
285  * @param tmpId New tmpId of annotation.
286  */
287  public void setTmpId(String tmpId) {
288  this.tmpId = tmpId;
289  }
290 
291  /**
292  * Gets URI addres in ontology for this type of attribute
293  *
294  * @return Return URI addres in ontology for this type of attribute.
295  */
296  @Override
297  public String getTypeOntologyUri() {
298  if (this.attributeType != null) {
299  return this.attributeType.getUriInOntology();
300  } else {
301  return "";
302  }
303  }
304 } // class LinkedAnnotationAttribute
String toXMLString(boolean proto11, boolean tmpIdForNested, boolean withOntology)
Class representing attribute of type of annotation.
Singleton for storing global variables.
Definition: AppBean.java:47
Base class representing attribute of annotation.