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