4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
P2NestedTrix.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: P2NestedTrix.java
5  * Description: Class that represents nested trix triple.
6  */
7 
8 /**
9  * @file P2NestedTrix.java
10  *
11  * @brief Class that represents nested trix triple.
12  */
13 
14 package cz.vutbr.fit.knot.annotations.comet.protocolV2_0.P2Trix;
15 
25 import java.util.ArrayList;
26 import java.util.Iterator;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 
30 /**
31  * Class that represents nested trix triple.
32  *
33  * @brief Class that represents nested trix triple.
34  */
35 public class P2NestedTrix extends P2BaseTrix{
36 
37  /**
38  * Constructs nested trix with specified destination
39  * @param destinationUri Uri of destination
40  */
41  public P2NestedTrix(String destinationUri){
42  super(destinationUri);
43  }
44 
45  public P2NestedTrix(P2BaseTrix trix){
46  super(trix.getDestinationUri());
47  this.name = trix.name;
48  this.annotType = trix.annotType;
49  this.priority = trix.priority;
50  this.fromOntology = trix.fromOntology;
51  this.uriInOntology = trix.uriInOntology;
52  }
53 
54  /** Uris of nested annotation */
55  private ArrayList<String> nestedAnnotUris;
56 
57  /** Uri of reference annotation */
58  private String refAnnotUri;
59 
60  /**
61  * Gets Uris of nested annotation
62  * @return The Uris of nested annotations
63  */
64  public ArrayList<String> getNestedAnnotUris() {
65  return nestedAnnotUris;
66  }
67 
68  /**
69  * Sets Uris of nested annotations
70  * @param nestedAnnotUris The Uris of nested annotations
71  */
72  public void setNestedAnnotUri(ArrayList<String> nestedAnnotUris) {
73  this.nestedAnnotUris = nestedAnnotUris;
74  }
75 
76  /**
77  * Add Uri of nested annotation
78  * @param nestedAnnotUri The Uri of nested annotation
79  */
80  public void addNestedAnnotUri(String nestedAnnotUri){
81  if(nestedAnnotUris == null){
82  nestedAnnotUris = new ArrayList<String>();
83  }
84 
85  nestedAnnotUris.add(nestedAnnotUri);
86  }
87 
88  /**
89  * Add all Uris of nested annotation
90  * @param allNestedAnnotUris The Uris of nested annotations
91  */
92  public void addAllNestedAnnotUris(ArrayList<String> allNestedAnnotUris){
93  if(nestedAnnotUris == null){
94  this.nestedAnnotUris = new ArrayList<String>();
95  }
96  nestedAnnotUris.addAll(allNestedAnnotUris);
97  }
98 
99  /**
100  * Gets Uri of reference annotation
101  * @return The Uri reference annotation
102  */
103  public String getRefAnnotUri() {
104  return refAnnotUri;
105  }
106 
107  /**
108  * Sets Uri of reference annotation
109  * @param refAnnotUri The Uri reference annotation
110  */
111  public void setRefAnnotUri(String refAnnotUri) {
112  this.refAnnotUri = refAnnotUri;
113  }
114 
115  @Override
116  public ArrayList<BaseAttribute> trixToAnnotAtt(Annotation annot, RequestInfo requestInfo, Integer sparePriority) {
117  ArrayList<BaseAttribute> atList = new ArrayList<BaseAttribute>();
118 
119 
120  /* Check if name is not empty */
121  if(name.isEmpty()){
122  int langNum = requestInfo.getSession().getLanguageNum();
123  int lod = requestInfo.getSession().getProtocolLOD();
124 
125  String info = "<attribute annotationUri=\"" + annot.getURIV2();
126  if(isFromOntology()){
127  info += "\" uri=\"" + uriInOntology;
128  }
129  else{
130  info += "\" name=\"" + name;
131  }
132  info += "\" invalidProperty=\"name\"/>";
133 
134  requestInfo.addError(lod, langNum, Localisation.ERROR_26_ATTRIBUTE_MALFORMED, info);
136  Logger.getLogger(P2NestedTrix.class.getName()).log(Level.SEVERE, null);
137  }
138  return null;
139  }
140 
141 
142  if (nestedAnnotUris == null) { // prevent NUllPointerException
143  nestedAnnotUris = new ArrayList<String>();
144  }
145  Iterator<String> nestedAnnUrisIt = nestedAnnotUris.iterator();
146  do { // attribute can be empty - it is necessary to create one even if there is no value
148 
149  try {
150  newAtt = (NestedAnnotationAttribute) AttributeManager.createAttribute(name, "NestedAnnotation", annot);
151  } catch (ClassNotFoundException ex) {
152  int langNum = requestInfo.getSession().getLanguageNum();
153  int lod = requestInfo.getSession().getProtocolLOD();
154 
155  String info = "<attribute annotationUri=\"" + annot.getURIV2();
156  if(isFromOntology()){
157  info += "\" uri=\"" + uriInOntology;
158  }
159  else{
160  info += "\" name=\"" + name;
161  }
162  info += "\" invalidProperty=\"type\"/>";
163 
164  requestInfo.addError(lod, langNum, Localisation.ERROR_26_ATTRIBUTE_MALFORMED, info);
166  Logger.getLogger(P2NestedTrix.class.getName()).log(Level.SEVERE, "Unable to create nested attribute", ex);
167  }
168  return null;
169  }
170 
171  /*
172  * Searching for attribute type
173  * - in Flier as it could be just created type
174  * - in DB
175  */
176  AnnotType type = P2AnnotTypeProcessor.searchAnnotType(annotType, requestInfo.getFlier().getAddedTypes());
177  if(type == null){
178  int langNum = requestInfo.getSession().getLanguageNum();
179  int lod = requestInfo.getSession().getProtocolLOD();
180 
181  String info = "<attribute annotationUri=\"" + annot.getURIV2()
182  + "\" uri=\"" + newAtt.getUriInOntology()
183  + "\" name=\"" + newAtt.getName()
184  + "\" valueType=\"" + newAtt.getValueType()
185  + "\" type=\"" + annotType + "\"/>";
186  requestInfo.addError(lod, langNum, Localisation.ERROR_15_ATTRIBUTE_TYPE_UNAVAILABLE, info);
188  Logger.getLogger(P2NestedTrix.class.getName()).log(Level.SEVERE, "Unable to search for attribute type");
189  }
190  return null;
191  }
192 
193  /* Sets uri in ontology */
194  newAtt.setUriInOntology(uriInOntology);
195 
196  // Set additional informations
197  if (nestedAnnUrisIt.hasNext()) {
198  newAtt.setUri(nestedAnnUrisIt.next());
199  }
200  newAtt.setAttributeType(type);
201 
202  /* Process priority */
203  Integer newPriority = null;
204  if(priority != null){
205  try{
206  newPriority = Integer.parseInt(priority);
207  }catch(NumberFormatException ex){
208  int langNum = requestInfo.getSession().getLanguageNum();
209  int lod = requestInfo.getSession().getProtocolLOD();
210 
211  String info = "<attribute annotationUri=\"" + annot.getURIV2();
212  if(isFromOntology()){
213  info += "\" uri=\"" + uriInOntology;
214  }
215  else{
216  info += "\" name=\"" + name;
217  }
218  info += "\" invalidProperty=\"priority\"/>";
219 
220  requestInfo.addError(lod, langNum, Localisation.ERROR_26_ATTRIBUTE_MALFORMED, info);
222  Logger.getLogger(P2NestedTrix.class.getName()).log(Level.SEVERE, null, ex);
223  }
224  return null;
225  }
226  }
227 
228  if(priority != null){
229  newAtt.setPriority(newPriority);
230  } else {
231  newAtt.setPriority(sparePriority);
232  }
233 
234  atList.add(newAtt);
235 
236  } while (nestedAnnUrisIt.hasNext());
237 
238  return atList;
239  } // trixToAnnotAtt()
240 } // public class P2NestedTrix
ArrayList< BaseAttribute > trixToAnnotAtt(Annotation annot, RequestInfo requestInfo, Integer sparePriority)
Class which parses and process XML with messages with types for protocol version 2.
void addAllNestedAnnotUris(ArrayList< String > allNestedAnnotUris)
Base class representing attribute of annotation.
Class representing type of annotation.
Definition: AnnotType.java:58
void setNestedAnnotUri(ArrayList< String > nestedAnnotUris)
Class attribute manager provides a way how to create new attributes.
Processed informations about client request.
Class responsible for localised strings.