14 package cz.vutbr.fit.knot.annotations.documentCloner;
36 import java.io.BufferedReader;
37 import java.io.StringWriter;
38 import java.util.ArrayList;
39 import java.util.Date;
40 import java.util.Iterator;
41 import java.util.List;
42 import java.util.logging.Level;
43 import java.util.logging.Logger;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.xml.xpath.XPathExpressionException;
46 import org.json.simple.JSONObject;
47 import org.json.simple.JSONValue;
48 import org.w3c.dom.Document;
115 String msg =
"Parse JSON in document clone error: " + e.getMessage();
116 Logger.getLogger(DocumentClone.class.getName()).log(Level.ALL, msg);
122 String msg =
"Unauthorized document clone request from IP: " + request.getRemoteAddr();
123 Logger.getLogger(DocumentClone.class.getName()).log(Level.ALL, msg);
129 List<Annotation> oldAnnotList = null;
137 if(oldAnnotList != null && !oldAnnotList.isEmpty()){
146 String msg =
"Error while creating new document: " + e.getMessage();
147 System.out.println(msg);
148 Logger.getLogger(DocumentClone.class.getName()).log(Level.ALL, msg);
153 if(oldAnnotList != null && !oldAnnotList.isEmpty()){
155 ArrayList<Annotation> tmpArray =
new ArrayList<Annotation>(oldAnnotList.size());
156 tmpArray.addAll(oldAnnotList);
157 sender.setAddedAnnotations(tmpArray);
170 @SuppressWarnings(
"unchecked")
171 List<DocClonerServer> results = AppBean.getPersistenceManager().getEntitiesByName(
"DocClonerServer");
172 if(results == null || results.isEmpty()){
176 String address = params.getRequest().getRemoteAddr();
177 Iterator<DocClonerServer> resultsIt = results.iterator();
178 if(resultsIt.hasNext()){
179 if(resultsIt.next().getIpAddress().equals(address)){
205 throws RuntimeException{
207 if(requestMessage != null && !requestMessage.isEmpty()){
208 JSONObject jsonResponse = (JSONObject)JSONValue.parse(requestMessage);
209 if(jsonResponse != null){
220 throw new RuntimeException(e.getMessage());
224 throw new RuntimeException(
"Can't parse JSON string.");
228 throw new RuntimeException(
"Can't read JSON request string.");
242 throws RuntimeException{
245 if(checkDoc != null) {
246 throw new RuntimeException(
"Document with new document URI: " + params.getNewDocumentUri() +
" is already in DB.");
250 params.setOldDocument(oldDocument);
251 if(oldDocument != null){
254 newDocument.setUri(params.getNewDocumentUri());
255 newDocument.setContent(params.getNewDocumentContent());
256 newDocument.setModified(
new Date());
257 newDocument.setAdded(oldDocument.getAdded());
261 throw new RuntimeException(
"Persisting of new document failed.");
264 params.setNewDocument(newDocument);
268 throw new RuntimeException(
"Can't find document with uri: " + params.getOldDocumentUri() +
" in DB.");
283 throws RuntimeException {
285 Iterator<Annotation> oldAnnotIt = oldAnnotations.iterator();
287 while(oldAnnotIt.hasNext()){
293 currentAnnot.setId(null);
294 currentAnnot.setAttributes(null);
295 currentAnnot.setSourceDocument(params.getNewDocument());
296 currentAnnot.setSourceDocumentId(params.getNewDocument().getId());
298 Iterator<Fragment> fragmentsIt = currentAnnot.getFragments().iterator();
299 while(fragmentsIt.hasNext()){
300 fragmentsIt.next().setRefAnnotation(currentAnnot);
308 throw new RuntimeException(
"Can't persist annotation to DB.");
312 params.addCloneAnnotation(newItem);
316 Iterator<AnnotCloneItem> clonedAnnotIt = params.getCloneAnnotations().iterator();
317 while(clonedAnnotIt.hasNext()){
322 Iterator<BaseAttribute> attributeIt = currentItem.getNewAttributesOfAnnotation().iterator();
324 while(attributeIt.hasNext()){
329 LinkedAnnotationAttribute tmpAttribute = (LinkedAnnotationAttribute)currentAttribute;
331 if(linkedAnnotation != null){
332 AnnotCloneItem foundedAnnotation = params.findCloneAnnotation(linkedAnnotation.getId());
333 tmpAttribute.setValue(foundedAnnotation.getNewAnnotation());
335 tmpAttribute.setAttributeType(foundedAnnotation.getNewAnnotation().getAnnotType());
340 NestedAnnotationAttribute tmpAttribute = (NestedAnnotationAttribute)currentAttribute;
342 if(nestedAnnotation != null){
343 AnnotCloneItem foundedAnnotation = params.findCloneAnnotation(nestedAnnotation.getId());
344 tmpAttribute.setValue(foundedAnnotation.getNewAnnotation());
346 tmpAttribute.setAttributeType(foundedAnnotation.getNewAnnotation().getAnnotType());
351 currentAttribute.setAnnotation(currentItem.getNewAnnotation().getId());
355 throw new RuntimeException(
"Can't persist parameter of annotation to DB.");
371 StringWriter writer =
new StringWriter();
373 BufferedReader bufReader = params.getRequest().getReader();
375 while ((nextChar = bufReader.read()) != -1) {
376 writer.write(nextChar);
381 return writer.toString();
394 throws RuntimeException{
395 String result = null;
396 if(jsonResponse.containsKey(parameterName)){
398 result = (String)jsonResponse.get(parameterName);
399 if(result == null || result.isEmpty()){
401 throw new RuntimeException(
"Parameter " + parameterName +
" is empty.");
405 throw new RuntimeException(
"Can't find " + parameterName +
" parameter in JSON string.");
421 Document parsedSyncDocument = mp.getDocumentFromString(params.getNewDocumentContent(),
false,
true);
422 params.setParsedDoc(parsedSyncDocument);
423 }
catch (Exception ex) {
425 throw new RuntimeException(
"Can't parse document:" + ex.getMessage() +
".");
445 Document parsedDoc = params.getParsedDoc();
446 if (parsedDoc == null) {
453 int nowOrphFragCnt = 0;
454 int updatedFragCnt = 0;
455 Iterator<Fragment> fragIt = annotation.getFragments().iterator();
456 while (fragIt.hasNext()) {
460 UpdatableFragment uf = exactMatcherProvider.match(parsedDoc, fragment.toUpdatableFragment());
463 fragment.setIsGood(
true);
469 ArrayList<UpdatableFragment> ufl =
new ArrayList<UpdatableFragment>();
471 UpdatableFragment ufTemp = nearestMatcherProvider.matchInClosestNode(parsedDoc, fragment.toUpdatableFragment());
472 if (ufTemp != null) {
476 ufl = sequenceMatcherProvider.matchAllIncrementally(parsedDoc, fragment.toUpdatableFragment());
478 if (ufl.size() == 1) {
480 Iterator<UpdatableFragment> ufIt = ufl.iterator();
482 fragment.updateWithUpdatableFragment(uf);
485 fragment.setIsGood(
true);
487 }
else if (ufl.isEmpty()) {
489 fragment.setIsGood(
false);
495 int minBadness = Integer.MAX_VALUE;
497 String origPath = fragment.getPath();
498 String origText = fragment.getAnnotatedText();
499 int origOffset = fragment.getOffset();
500 for (Iterator<UpdatableFragment> uflIt = ufl.iterator(); uflIt.hasNext();) {
502 int lD = Util.levenshtein(origPath, ufr.getXPathString());
503 int lDT = Util.levenshtein(origText, ufr.getText());
504 int offDistance = Math.abs(origOffset - ufr.getOffset());
505 int badness = 10000 * lD + 1000 * lDT + offDistance;
506 if (badness < minBadness) {
507 minBadness = badness;
512 fragment.updateWithUpdatableFragment(uf);
515 fragment.setIsGood(
true);
519 }
catch (XPathExpressionException ex) {
521 fragment.setIsGood(
false);
525 }
catch (Exception e) {
533 if (badFragCnt == annotation.
getFragments().size() && nowOrphFragCnt > 0) {
535 }
else if (nowOrphFragCnt > 0) {
538 if (nowOrphFragCnt > 0 || updatedFragCnt > 0) {
552 Object[] params =
new Object[2];
556 @SuppressWarnings(
"unchecked")
558 if(results != null && !results.isEmpty()){
559 return results.get(0);
572 Object[] paramsDB =
new Object[2];
573 paramsDB[0] =
"sourceDocumentId";
574 paramsDB[1] = params.getOldDocument().getId();
576 @SuppressWarnings(
"unchecked")
577 List<Annotation> retList = AppBean.getPersistenceManager().queryDB(
"Annotation.findBySourceDocumentID", paramsDB);
589 Object[] paramsDB =
new Object[2];
590 paramsDB[0] =
"annotation";
593 @SuppressWarnings(
"unchecked")
594 List<BaseAttribute> retList = AppBean.getPersistenceManager().queryDB(
"Attribute.findByAnnotation", paramsDB);
Annotation getNewAnnotation()
boolean createNewDocument(CloneParams params)
String getJsonParameter(JSONObject jsonResponse, String parameterName)
MatcherProvider sequenceMatcherProvider
List< BaseAttribute > getAllAttributes(Integer id)
Class representing annotations for clone list item.
Class represent thread of Storyscope Interface that sends messages to StoryScopes.
List< Annotation > getAllAnnotations(CloneParams params)
MatcherProvider exactMatcherProvider
Class representing annotated copy of document.
boolean parseJSON(CloneParams params)
Class representing attribute of type NestedAnnotation.
Singleton for storing global variables.
Class providing access to available matchers.
static final String NEW_DOCUMENT_CONTENT_PARAM
boolean isAuthorized(CloneParams params)
Class representing attribute of type AnnotationLink.
static final String NEW_DOCUMENT_URI_PARAM
List< Fragment > getFragments()
Class provides document clonning with new content, iniciated from NLP.
static final String OLD_DOCUMENT_URI_PARAM
Class representing paramethers for document cloning.
List< BaseAttribute > getNewAttributesOfAnnotation()
Base class representing attribute of annotation.
Class for XML document fragment.
static final int LOG_LEVEL
Compare class using Levenshtein approximate string matching method.
static final int LOG_LEVEL_ALL
static PersistM getPersistenceManager()
Class consisting of traversing method and compare method.
AnnotDocument findDocumentInDB(String uri)
Exact XPath node iterator.
Node iterator which is gradually unnesting.
String cloneDocument(HttpServletRequest request)
Class for matcher consisting of comparator and node iterator.
MatcherProvider nearestMatcherProvider
Utility class (manipulates RFC 3339 dates)
boolean makeNewAnnotations(List< Annotation > oldAnnotations, CloneParams params)
Class representing annotated fragment.
Class representing annotation.
boolean updateAnnotation(Annotation annotation, CloneParams params)
String readMessage(CloneParams params)
void parseDocument(CloneParams params)