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