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