4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
P2SimpleTrix.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: P2SimpleTrix.java
5  * Description: Class that represents simple trix triple.
6  */
7 
8 /**
9  * @file P2SimpleTrix.java
10  *
11  * @brief Class that represents simple trix triple.
12  */
13 
14 package cz.vutbr.fit.knot.annotations.comet.protocolV2_0.P2Trix;
15 
23 import java.util.ArrayList;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 
27 /**
28  * Class that represents simple trix triple.
29  *
30  * @brief Class that represents simple trix triple.
31  */
32 public class P2SimpleTrix extends P2BaseTrix{
33 
34  /**
35  * Constructs simple trix with specified destination
36  * @param destinationUri Uri of destination
37  */
38  public P2SimpleTrix(String destinationUri){
39  super(destinationUri);
40  }
41 
42  /** Uri of type of attribute */
43  private String typeUri;
44 
45  /** Values of attribute */
46  private ArrayList<String> values;
47 
48  /** Latitudes of GeoPoint attribute */
49  private ArrayList<String> latitudes;
50 
51  /** Longitude of GeoPoint attribute */
52  private ArrayList<String> longitudes;
53 
54  /** Type of attribute is GeoPoint | default value: false */
55  private boolean geoPoint = false;
56 
57  /**
58  * Gets URI of type of attribute
59  * @return Uri of type of attribute
60  */
61  public String getTypeUri(){
62  return typeUri;
63  }
64 
65  /**
66  * Sets Uri of type of attribute
67  * @param typeUri Uri of type of attribute
68  */
69  public void setTypeUri(String typeUri){
70  this.typeUri = typeUri;
71  }
72 
73  /**
74  * Gets boolean value that indicates if attribute is GeoPoint
75  * @return true if attribute is GeoPoint, false otherwise
76  */
77  public boolean isGeoPoint() {
78  return geoPoint;
79  }
80 
81  /**
82  * Sets boolean value that indicates that attribute is GeoPoint
83  * @param geoPoint Boolean value that indicates that attribute is GeoPoint
84  */
85  public void setGeoPoint(boolean geoPoint) {
86  this.geoPoint = geoPoint;
87  }
88 
89  /**
90  * Gets values of attribute in String form
91  * @return The values of attribute in String form
92  */
93  public ArrayList<String> getValues() {
94  return values;
95  }
96 
97  /**
98  * Sets values of attribute in String form
99  * @param values The values of attribute to set
100  */
101  public void setValue(ArrayList<String> values) {
102  this.values = values;
103  }
104 
105  /**
106  * Adds value of attribute
107  * @param value The value of attribute to set
108  */
109  public void addValue(String value){
110  if(values == null){
111  values = new ArrayList<String>();
112  }
113  values.add(value);
114  }
115 
116  /**
117  * Gets latitudes of GeoPoint attribute in String form
118  * @return The latitude of GeoPoint attribute in String form
119  */
120  public ArrayList<String> getLatitudes() {
121  return latitudes;
122  }
123 
124  /**
125  * Sets latitudes of GeoPoint attribute
126  * @param latitudes The latitudes to set
127  */
128  public void setLatitudes(ArrayList<String> latitudes) {
129  this.latitudes = latitudes;
130  }
131 
132  /**
133  * Add latitude of GeoPoint attribute in String form
134  * @param latitude The latitude to set
135  */
136  public void addLatitude(String latitude){
137  if(latitudes == null){
138  this.latitudes = new ArrayList<String>();
139  }
140 
141  latitudes.add(latitude);
142  }
143 
144  /**
145  * Gets longitudes of GeoPoint attribute in String form
146  * @return The longitudes of GeoPoint attribute in String form
147  */
148  public ArrayList<String> getLongitudes() {
149  return longitudes;
150  }
151 
152  /**
153  * Sets longitudes of GeoPoint attribute
154  * @param longitudes The longitudes to set
155  */
156  public void setLongitudes(ArrayList<String> longitudes) {
157  this.longitudes = longitudes;
158  }
159 
160  /**
161  * Add longitude of GeoPoint attribute in String form
162  * @param longitude The longitude to set
163  */
164  public void addLongitude(String longitude){
165  if(longitudes == null){
166  this.longitudes = new ArrayList<String>();
167  }
168 
169  longitudes.add(longitude);
170  }
171 
172  @Override
173  public ArrayList<BaseAttribute> trixToAnnotAtt(Annotation annot, RequestInfo requestInfo, Integer sparePriority) {
174  ArrayList<BaseAttribute> atList = new ArrayList<BaseAttribute>();
175 
176  /* Check if name is not empty */
177  if(name.isEmpty()){
178  int langNum = requestInfo.getSession().getLanguageNum();
179  int lod = requestInfo.getSession().getProtocolLOD();
180 
181  String info = "<attribute annotationUri=\"" + annot.getURIV2();
182  if(isFromOntology()){
183  info += "\" uri=\"" + uriInOntology;
184  }
185  else{
186  info += "\" name=\"" + name;
187  }
188  info += "\" invalidProperty=\"name\"/>";
189 
190  requestInfo.addError(lod, langNum, Localisation.ERROR_26_ATTRIBUTE_MALFORMED, info);
192  Logger.getLogger(P2SimpleTrix.class.getName()).log(Level.SEVERE, null);
193  }
194  return null;
195  }
196 
197 
198  String simpleType = "";
199  int typeIndex = Constants.SIMPLE_TYPES_URI_V2.indexOf(typeUri);
200  if (typeIndex < 0) {
201  if (Constants.EMPTY_ENTITY.contentEquals(typeUri)) {
202  simpleType = "entity";
203  } else {
204  int langNum = requestInfo.getSession().getLanguageNum();
205  int lod = requestInfo.getSession().getProtocolLOD();
206 
207  String info = "<attribute annotationUri=\"" + annot.getURIV2();
208  if(isFromOntology()){
209  info += "\" uri=\"" + uriInOntology;
210  }
211  else{
212  info += "\" name=\"" + name;
213  }
214  info += "\" invalidProperty=\"type\"/>";
215 
216  requestInfo.addError(lod, langNum, Localisation.ERROR_26_ATTRIBUTE_MALFORMED, info);
218  Logger.getLogger(P2SimpleTrix.class.getName()).log(Level.SEVERE, null);
219  }
220  return null;
221  }
222  } else {
223  simpleType = Constants.SIMPLE_TYPES_NAMES_V2.get(typeIndex);
224  }
225 
226 
227  int objNum = getNumberOfAttributes();
228 
229  for(int i = 0; i < objNum; i++){
230  BaseAttribute newAtt;
231  try {
232  newAtt = AttributeManager.createAttribute(name, simpleType, annot);
233 
234  } catch (ClassNotFoundException ex) {
235  int langNum = requestInfo.getSession().getLanguageNum();
236  int lod = requestInfo.getSession().getProtocolLOD();
237 
238  String info = "<attribute annotationUri=\"" + annot.getURIV2();
239  if(isFromOntology()){
240  info += "\" uri=\"" + uriInOntology;
241  }
242  else{
243  info += "\" name=\"" + name;
244  }
245  info += "\" invalidProperty=\"type\"/>";
246 
247  requestInfo.addError(lod, langNum, Localisation.ERROR_26_ATTRIBUTE_MALFORMED, info);
249  Logger.getLogger(P2SimpleTrix.class.getName()).log(Level.SEVERE, null, ex);
250  }
251  return null;
252  }
253 
254  /* Set uri in ontology */
255  newAtt.setUriInOntology(uriInOntology);
256 
257  /* Process priority */
258  Integer newPriority = null;
259  if(priority != null){
260  try{
261  newPriority = Integer.parseInt(priority);
262  }catch(NumberFormatException ex){
263  int langNum = requestInfo.getSession().getLanguageNum();
264  int lod = requestInfo.getSession().getProtocolLOD();
265 
266  String info = "<attribute annotationUri=\"" + annot.getURIV2();
267  if(isFromOntology()){
268  info += "\" uri=\"" + uriInOntology;
269  }
270  else{
271  info += "\" name=\"" + name;
272  }
273  info += "\" invalidProperty=\"priority\"/>";
274 
275  requestInfo.addError(lod, langNum, Localisation.ERROR_26_ATTRIBUTE_MALFORMED, info);
277  Logger.getLogger(P2SimpleTrix.class.getName()).log(Level.SEVERE, null, ex);
278  }
279  return null;
280  }
281  }
282 
283  if(priority != null){
284  newAtt.setPriority(newPriority);
285  } else {
286  newAtt.setPriority(sparePriority);
287  }
288 
289  /* If its anyAnnotation or anyEntity attribute, process of creating is done */
290  if(simpleType.equals("anyAnnotation") || simpleType.equals("anyEntity")){
291  atList.add(newAtt);
292  }
293  /* If its GeoPoint attribute, set its values */
294  else if(simpleType.equals("GeoPoint")){
295  try{
296  if(latitudes != null){
297  ((GeoPointAttribute)newAtt).setStringValue(latitudes.get(i), longitudes.get(i));
298  }
299  atList.add(newAtt);
300  } catch (IllegalArgumentException ex) {
301  int langNum = requestInfo.getSession().getLanguageNum();
302  int lod = requestInfo.getSession().getProtocolLOD();
303 
304  String info = "<attribute annotationUri=\"" + annot.getURIV2();
305  if(isFromOntology()){
306  info += "\" uri=\"" + uriInOntology;
307  }
308  else{
309  info += "\" name=\"" + name;
310  }
311  info += "\" valueType=\"" + simpleType + "\" type=\"" + typeUri + "\"/>";
312 
313  requestInfo.addError(lod, langNum, Localisation.ERROR_7_ATTRIBUTE_VALUE, info);
315  Logger.getLogger(P2SimpleTrix.class.getName()).log(Level.SEVERE, null, ex);
316  }
317  return null;
318  }
319  }
320  else{
321  try{
322  if(values != null){
323  newAtt.setRawValue(values.get(i));
324  }
325  atList.add(newAtt);
326  }
327  catch (IllegalArgumentException ex) {
328  int langNum = requestInfo.getSession().getLanguageNum();
329  int lod = requestInfo.getSession().getProtocolLOD();
330 
331  String info = "<attribute annotationUri=\"" + annot.getURIV2();
332  if(isFromOntology()){
333  info += "\" uri=\"" + uriInOntology;
334  }
335  else{
336  info += "\" name=\"" + name;
337  }
338  info += "\" valueType=\"" + simpleType + "\" type=\"" + typeUri + "\"/>";
339 
340  requestInfo.addError(lod, langNum, Localisation.ERROR_7_ATTRIBUTE_VALUE, info);
341 
342  String loggerInfo = "Attribute name = " + name + " value = " + values.get(i);
344  Logger.getLogger(P2SimpleTrix.class.getName()).log(Level.SEVERE, loggerInfo, ex);
345  }
346  return null;
347  }
348  }
349  }
350 
351  return atList;
352  }
353 
354  /**
355  * Gets number of attributes that will be created from this trix
356  * @return Number of attributes that will be create from this trix
357  */
358  private int getNumberOfAttributes(){
359  if(values != null){
360  return values.size();
361  }
362  else if(latitudes != null){
363  return latitudes.size();
364  }
365  else{
366  //Any annotation or anyEntity
367  return 1;
368  }
369  }
370 }
ArrayList< BaseAttribute > trixToAnnotAtt(Annotation annot, RequestInfo requestInfo, Integer sparePriority)
Base class representing attribute of annotation.
Class attribute manager provides a way how to create new attributes.
Processed informations about client request.
Class responsible for localised strings.