4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
P2AnnotationProcessor.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: P2AnnotationProcessor.java
5  * Description: Class which parses and process XML with messages with annotations for
6  * protocol version 2.
7  */
8 
9 /**
10  * @file P2AnnotationProcessor.java
11  *
12  * @brief Class which parses and process XML with messages with annotations for protocol
13  * version 2.
14  */
15 
16 package cz.vutbr.fit.knot.annotations.comet.protocolV2_0;
17 
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
32 import org.w3c.dom.Element;
33 import org.w3c.dom.NodeList;
34 
35 /**
36  * Class which parses and process XML with messages with annotations for protocol
37  * version 2.
38  *
39  * @brief Class which parses and process XML with messages with annotations for
40  * protocol version 2
41  * @author Martin Petr (xpetrm05)
42  */
44  /** Informations about client request */
46 
47  /**
48  * Constructor
49  */
51 
52  }
53 
54  /**
55  * Method processes element with annotations to add.
56  *
57  * @param addAnnotationsEl Element with annotations to add
58  * @param requestInfo Informations about client request
59  */
60  public void processAddAnnotations(Element addAnnotationsEl, RequestInfo requestInfo) {
61  NodeList annotationsMsg = addAnnotationsEl.getElementsByTagName("oa:Annotation");
62  int annotCount = annotationsMsg.getLength();
63  ArrayList<Annotation> annotsToAdd = new ArrayList<Annotation>();
64 
65  this.requestInfo = requestInfo;
66 
67  for(int index = 0; index < annotCount; index++){
68  Element annotationEl = (Element) annotationsMsg.item(index);
69  if(annotationEl != null) {
70  Annotation newAnnotation = procesAnnotAdd(annotationEl,requestInfo);
71  if(newAnnotation != null){
72  annotsToAdd.add(newAnnotation);
73  }
74  }
75  }
76 
77  connectAnnotations(annotsToAdd,null);
78  requestInfo.getFlier().setAddedAnnotations(annotsToAdd);
79  }
80 
81  /**
82  * Method processes element with annotations to change.
83  *
84  * @param changeAnnotationsEl Element with annotations to change
85  * @param requestInfo Informations about client request
86  */
87  public void processChangeAnnotations(Element changeAnnotationsEl, RequestInfo requestInfo) {
88  NodeList annotationsMsg = changeAnnotationsEl.getElementsByTagName("oa:Annotation");
89  int annotCount = annotationsMsg.getLength();
90  ArrayList<Annotation> annotsToChange = new ArrayList<Annotation>();
91 
92  this.requestInfo = requestInfo;
93 
94  for(int index = 0; index < annotCount; index++){
95  Element annotationEl = (Element) annotationsMsg.item(index);
96  if(annotationEl != null) {
97  Annotation changedAnnotation = procesAnnotChange(annotationEl,requestInfo);
98  if(changedAnnotation != null){
99  annotsToChange.add(changedAnnotation);
100  }
101  }
102  }
103 
104  connectAnnotations(annotsToChange,this.requestInfo.getFlier().getAddedAnnotations());
105  requestInfo.getFlier().setEditedAnnotations(annotsToChange);
106  }
107 
108  /**
109  * Method processes element with annotations to delete.
110  *
111  * @param deleteAnnotationsEl Element with annotations to delete
112  * @param requestInfo Informations about client request
113  */
114  public void processDeleteAnnotations(Element deleteAnnotationsEl, RequestInfo requestInfo) {
115  NodeList annotationsMsg = deleteAnnotationsEl.getElementsByTagName("annotation");
116  int annotCount = annotationsMsg.getLength();
117  this.requestInfo = requestInfo;
118 
119  for(int index = 0; index < annotCount; index++){
120  Element annotationEl = (Element) annotationsMsg.item(index);
121  if(annotationEl != null) {
122  Annotation actualAnnot = procesAnnotDelete(annotationEl,requestInfo);
123  if(actualAnnot == null){
124  continue;
125  }
126 
127  requestInfo.getFlier().AddRemovedAnnotation(actualAnnot);
128  }
129  }
130  }
131 
132  /**
133  * Method which reconstruct links between annotation objects (references
134  * to appropriate objects) using URIs in attributes. For example if there
135  * is link to another annotation but only URI of annotation is set
136  * in the attribute, it will set also reference to linked annotation.
137  * It also reconstruct both sides of nesting relation.
138  *
139  * @param annotsToConnect Annotations in which links should be reconstructed
140  * @param added List of just added annotations
141  */
142  private void connectAnnotations(ArrayList<Annotation> annotsToConnect, ArrayList<Annotation> added){
143  Iterator<Annotation> connectIt = annotsToConnect.iterator();
144  while(connectIt.hasNext()){
145 
146  // go trough all added annotations
147  Annotation actualAnnot = connectIt.next();
148  if (actualAnnot.getAttributes() != null) {
149 
150  // go tough all attributes
151  Iterator<BaseAttribute> attributesIt = actualAnnot.getAttributes().iterator();
152  while (attributesIt.hasNext()) {
153 
154  // try find nested or linked attribute
155  BaseAttribute actualAttribute = attributesIt.next();
156  if ((actualAttribute instanceof LinkedAnnotationAttribute)) {
157  if (actualAttribute.getUri() != null && !actualAttribute.getUri().isEmpty()) {
158  // try find annotation uri in just added annotations
159  String annotUri = actualAttribute.getUri();
160  String uriType = getUriType(annotUri);
161 
162  if (actualAttribute.getLinkedAnnotation() == null) {
163  if (uriType.equals("temp")) {
164  Annotation findedAnnot = findAnnotByTempUri(annotsToConnect, actualAttribute.getUri());
165  if (findedAnnot == null) {
166  if (added != null) {
167  // try find it in
168  findedAnnot = findAnnotByTempUri(added, annotUri);
169  if(findedAnnot == null){
170  // error - cant find linked annotation
171  int langNum = requestInfo.getSession().getLanguageNum();
172  int lod = requestInfo.getSession().getProtocolLOD();
173  String info = "<attribute annotationUri=\"" + actualAnnot.getURIV2()
174  + "\" uri=\"" + actualAttribute.getUriInOntology()
175  + "\" name=\"" + actualAttribute.getName()
176  + "\" name=\"" + actualAttribute.getName()
177  + "\" valueType=\"" + actualAttribute.getValueType()
178  + "\" type=\"" + actualAttribute.getTypeUriV2() + "\"/>";
179  requestInfo.addError(lod, langNum, Localisation.ERROR_7_ATTRIBUTE_VALUE, info);
181  String msg = "Link to unknown annotation:" + actualAttribute.getUri() + "";
182  Logger.getLogger(P2AnnotationProcessor.class.getName()).log(Level.ALL, msg);
183  }
184  }
185  } else {
186  // error - cant find linked annotation
187  int langNum = requestInfo.getSession().getLanguageNum();
188  int lod = requestInfo.getSession().getProtocolLOD();
189  String info = "<attribute annotationUri=\"" + actualAnnot.getURIV2()
190  + "\" uri=\"" + actualAttribute.getUriInOntology()
191  + "\" name=\"" + actualAttribute.getName()
192  + "\" valueType=\"" + actualAttribute.getValueType()
193  + "\" type=\"" + actualAttribute.getTypeUriV2() + "\"/>";
194  requestInfo.addError(lod, langNum, Localisation.ERROR_7_ATTRIBUTE_VALUE, info);
196  String msg = "Link to unknown annotation:" + actualAttribute.getUri() + "";
197  Logger.getLogger(P2AnnotationProcessor.class.getName()).log(Level.ALL, msg);
198  }
199  }
200  }
201  actualAttribute.setLinkedAnnotation(findedAnnot);
202  } else if (uriType.equals("serv")) {
203  Annotation findedAnnot = findAnnotByServUri(annotsToConnect, annotUri);
204  if (findedAnnot == null) {
205  Integer id = null;
206  String idString = annotUri.replace(AppBean.getBaseUri() + "/serv/","");
207  try {
208  id = Integer.decode(idString);
209  } catch (NumberFormatException ex) {
210  // error - cant decode id
211  int langNum = requestInfo.getSession().getLanguageNum();
212  int lod = requestInfo.getSession().getProtocolLOD();
213  String info = "<attribute annotationUri=\"" + actualAnnot.getURIV2()
214  + "\" uri=\"" + actualAttribute.getUriInOntology()
215  + "\" name=\"" + actualAttribute.getName()
216  + "\" valueType=\"" + actualAttribute.getValueType()
217  + "\" type=\"" + actualAttribute.getTypeUriV2() + "\"/>";
218  requestInfo.addError(lod, langNum, Localisation.ERROR_7_ATTRIBUTE_VALUE, info);
220  String msg = "Cannot decode id of annotation:" + idString + "";
221  Logger.getLogger(P2AnnotationProcessor.class.getName()).log(Level.ALL, msg);
222  }
223  } finally {
224  PersistM persistMan = AppBean.getPersistenceManager();
225 
226  Object[] annotParams = new Object[2];
227  annotParams[0] = "id";
228  annotParams[1] = id;
229  @SuppressWarnings("unchecked")
230  List<Annotation> annotList = persistMan.queryDB("Annotation.findById", annotParams);
231  if(annotList == null || annotList.isEmpty()){
232  //error - cant find linked annot in DB
233  int langNum = requestInfo.getSession().getLanguageNum();
234  int lod = requestInfo.getSession().getProtocolLOD();
235  String info = "<attribute annotationUri=\"" + actualAnnot.getURIV2()
236  + "\" uri=\"" + actualAttribute.getUriInOntology()
237  + "\" name=\"" + actualAttribute.getName()
238  + "\" valueType=\"" + actualAttribute.getValueType()
239  + "\" type=\"" + actualAttribute.getTypeUriV2() + "\"/>";
240  requestInfo.addError(lod, langNum, Localisation.ERROR_7_ATTRIBUTE_VALUE, info);
242  String msg = "Linked annotation is't presented in DB. Annotation id:" + idString + "";
243  Logger.getLogger(P2AnnotationProcessor.class.getName()).log(Level.ALL, msg);
244  }
245  return;
246  }
247 
248  LinkedAnnotationAttribute actualLinked = (LinkedAnnotationAttribute)actualAttribute;
249  actualLinked.setValue(annotList.get(0));
250  actualLinked.setAttributeType(annotList.get(0).getAnnotType());
251  }
252  } else {
253  actualAttribute.setLinkedAnnotation(findedAnnot);
254  }
255  }
256  }
257  }
258  }
259 
260  if ((actualAttribute instanceof NestedAnnotationAttribute)) {
261  NestedAnnotationAttribute nestedAttr = (NestedAnnotationAttribute) actualAttribute;
262  if (actualAttribute.getUri() != null && !actualAttribute.getUri().isEmpty()) {
263 
264  // try find annotation uri in just added annotations
265  Annotation findedAnnot = findAnnotByTempUri(annotsToConnect, actualAttribute.getUri());
266  if (findedAnnot == null) {
267  findedAnnot = findAnnotByServUri(annotsToConnect, actualAttribute.getUri());
268  }
269  if (findedAnnot == null) {
270  findedAnnot = findAnnotByTempUri(added, actualAttribute.getUri());
271  }
272  if (findedAnnot != null) {
273  actualAttribute.setNestedAnnotation(findedAnnot);
274  findedAnnot.setNestedInAnnot(actualAnnot);
275  }
276  }
277  }
278  }
279  }
280  }
281  } // connectAnnotations()
282 
283  /**
284  * Method processes element with annotation to add.
285  *
286  * @param annotationEl Element with annotation to add
287  * @param requestInfo Informations about client request
288  * @return Returns annotation to add (in case of error it returns null)
289  */
290  private Annotation procesAnnotAdd(Element annotationEl,RequestInfo requestInfo){
291  Annotation newAnnot;
292  newAnnot = processAnnotation(annotationEl, requestInfo);
293  return newAnnot;
294  }
295 
296  /**
297  * Method processes element with annotation to change.
298  *
299  * @param annotationEl Element with annotation to change
300  * @param requestInfo Informations about client request
301  * @return Returns annotation to change (in case of error it returns null)
302  */
303  private Annotation procesAnnotChange(Element annotationEl, RequestInfo requestInfo) {
304  Annotation newAnnotation = processAnnotation(annotationEl, requestInfo);
305  if (newAnnotation != null) {
306  Integer annotID = newAnnotation.getId();
307 
308  // check if annotation in from server
309  if (annotID != null) {
310  // get annotation from DB
311  PersistM persistMan = AppBean.getPersistenceManager();
312  Annotation findedAnnot = null;
313 
314  Object[] params = new Object[2];
315  params[0] = "id";
316  params[1] = annotID;
317  @SuppressWarnings("unchecked")
318  List<Annotation> annotList = persistMan.queryDB("Annotation.findById", params);
319  if (annotList != null && !annotList.isEmpty()) {
320  findedAnnot = annotList.get(0);
321  } else {
322  // error - cant find annotation in DB
323  int langNum = requestInfo.getSession().getLanguageNum();
324  int lod = requestInfo.getSession().getProtocolLOD();
325  String info = "<annotation uri=\"" + newAnnotation.getURIV2() + "\"/>";
326  requestInfo.addError(lod, langNum, Localisation.ERROR_29_CHANGED_ANNOT_NOT_FOUND, info);
328  String msg = "Annotation is't presented in DB. Annotation id:" + annotID.toString() + "";
329  Logger.getLogger(P2AnnotationProcessor.class.getName()).log(Level.ALL, msg);
330  }
331  return null;
332  }
333 
334  newAnnotation.setId(findedAnnot.getId());
335  }
336  }
337 
338  return newAnnotation;
339  } // procesAnnotChange()
340 
341  /**
342  * Method processes element with annotation to delete.
343  *
344  * @param annotationEl Element with annotation to delete
345  * @param requestInfo Informations about client request
346  * @return Returns annotation to delete (in case of error it returns null)
347  */
348  private Annotation procesAnnotDelete(Element annotationEl, RequestInfo requestInfo) {
349  Annotation deleteAnnotation = null;
350  String uri = annotationEl.getAttribute("uri");
351  if(uri == null || uri.isEmpty()){
352  // error - bad annotation uri
353  int langNum = requestInfo.getSession().getLanguageNum();
354  int lod = requestInfo.getSession().getProtocolLOD();
355  String info = "<annotation uri=\"\"/>";
356  requestInfo.addError(lod, langNum, Localisation.ERROR_30_REM_ANNOT_NOT_FOUND, info);
358  String msg = "Uri of annotation is wrong.";
359  Logger.getLogger(P2AnnotationProcessor.class.getName()).log(Level.ALL, msg);
360  }
361  return null;
362  }
363 
364  Integer annotId = null;
365 
366  try{
367  annotId = Integer.decode(uri.replace(AppBean.getBaseUri() + "/serv/", ""));
368  } catch(NumberFormatException ex){
369  // error - bad annotation id
370  int langNum = requestInfo.getSession().getLanguageNum();
371  int lod = requestInfo.getSession().getProtocolLOD();
372  String info = "<annotation uri=\"" + uri + "\"/>";
373  requestInfo.addError(lod, langNum, Localisation.ERROR_30_REM_ANNOT_NOT_FOUND, info);
375  String msg = "Id of annotation is wrong.";
376  Logger.getLogger(P2AnnotationProcessor.class.getName()).log(Level.ALL, msg);
377  }
378  return null;
379  }
380 
381  // get document from DB
382  PersistM persistMan = AppBean.getPersistenceManager();
383 
384  Object[] params = new Object[2];
385  params[0] = "id";
386  params[1] = annotId;
387  @SuppressWarnings("unchecked")
388  List<Annotation> annotList = persistMan.queryDB("Annotation.findById", params);
389  if(annotList != null && !annotList.isEmpty()) {
390  deleteAnnotation = annotList.get(0);
391  }else{
392  // error - can't find annotation to delete
393  int langNum = requestInfo.getSession().getLanguageNum();
394  int lod = requestInfo.getSession().getProtocolLOD();
395  String info = "<annotation uri=\"" + uri + "\"/>";
396  requestInfo.addError(lod, langNum, Localisation.ERROR_30_REM_ANNOT_NOT_FOUND, info);
398  String msg = "Unable to find annotation to delete.";
399  Logger.getLogger(P2AnnotationProcessor.class.getName()).log(Level.ALL, msg);
400  }
401  return null;
402  }
403 
404  return deleteAnnotation;
405  } // procesAnnotDelete()
406 
407  /**
408  * Find annotation in the list by temporary
409  *
410  * @param annots List of annotations
411  * @param uri Uri of annotation which should be found
412  * @return If it was found returns given annotation, null otherwise
413  */
414  private Annotation findAnnotByTempUri(ArrayList<Annotation> annots, String uri){
415  Iterator<Annotation> annotsIt = annots.iterator();
416  while(annotsIt.hasNext()){
417  Annotation currentAnnot = annotsIt.next();
418  if(currentAnnot.getTmpId() != null && uri.equals(AppBean.getBaseUri() + "/temp/" + currentAnnot.getTmpId())){
419  return currentAnnot;
420  }
421  }
422  return null;
423  }
424 
425  /**
426  * Find annotation in the list by server uri
427  *
428  * @param annots List of annotations
429  * @param uri Uri of annotation which should be found
430  * @return If it was found returns given annotation, null otherwise
431  */
432  private Annotation findAnnotByServUri(ArrayList<Annotation> annots, String uri){
433  Iterator<Annotation> annotsIt = annots.iterator();
434  while(annotsIt.hasNext()){
435  Annotation currentAnnot = annotsIt.next();
436  if(currentAnnot.getId() != null && uri.equals(AppBean.getBaseUri() + "/serv/" + currentAnnot.getId())){
437  return currentAnnot;
438  }
439  }
440  return null;
441  }
442 
443 } // public class P2AnnotationProcessor
void connectAnnotations(ArrayList< Annotation > annotsToConnect, ArrayList< Annotation > added)
Persistence manager (database manipulator)
Definition: PersistM.java:35
Annotation procesAnnotDelete(Element annotationEl, RequestInfo requestInfo)
Singleton for storing global variables.
Definition: AppBean.java:47
Annotation procesAnnotAdd(Element annotationEl, RequestInfo requestInfo)
Abstract class provides a parser for the annotation of protocol version 2.
Annotation findAnnotByServUri(ArrayList< Annotation > annots, String uri)
Annotation procesAnnotChange(Element annotationEl, RequestInfo requestInfo)
Annotation processAnnotation(Element annotationEl, RequestInfo requestInfo)
Base class representing attribute of annotation.
void processDeleteAnnotations(Element deleteAnnotationsEl, RequestInfo requestInfo)
Class which parses and process XML with messages with annotations for protocol version 2...
void processChangeAnnotations(Element changeAnnotationsEl, RequestInfo requestInfo)
Processed informations about client request.
void processAddAnnotations(Element addAnnotationsEl, RequestInfo requestInfo)
Class responsible for localised strings.
Annotation findAnnotByTempUri(ArrayList< Annotation > annots, String uri)