# HG changeset patch # User casties # Date 1338484128 -7200 # Node ID e5f5848892a2f438c3ea48040b641633a907f5b5 # Parent 40846c0b344d267030a4f58580bcb314a016e64e new annotation model basically works. diff -r 40846c0b344d -r e5f5848892a2 .settings/org.eclipse.wst.common.component --- a/.settings/org.eclipse.wst.common.component Thu May 31 11:57:50 2012 +0200 +++ b/.settings/org.eclipse.wst.common.component Thu May 31 19:08:48 2012 +0200 @@ -7,6 +7,9 @@ uses + + uses + diff -r 40846c0b344d -r e5f5848892a2 src/main/java/de/mpiwg/itgroup/annotationManager/Constants/NS.java --- a/src/main/java/de/mpiwg/itgroup/annotationManager/Constants/NS.java Thu May 31 11:57:50 2012 +0200 +++ b/src/main/java/de/mpiwg/itgroup/annotationManager/Constants/NS.java Thu May 31 19:08:48 2012 +0200 @@ -10,5 +10,6 @@ public static final String RDF_NS = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; public static final String MPIWG_ANNOT_NS = "http://ontologies.mpiwg-berlin.mpg.de/annotations/"; public static final String MPIWG_ANNOT_URL = "http://entities.mpiwg-berlin.mpg.de/annotations/"; + public static final String MPIWG_ANNOT_CTX = "file:///annotations2"; } diff -r 40846c0b344d -r e5f5848892a2 src/main/java/de/mpiwg/itgroup/annotationManager/RDFHandling/Convert.java --- a/src/main/java/de/mpiwg/itgroup/annotationManager/RDFHandling/Convert.java Thu May 31 11:57:50 2012 +0200 +++ b/src/main/java/de/mpiwg/itgroup/annotationManager/RDFHandling/Convert.java Thu May 31 19:08:48 2012 +0200 @@ -22,7 +22,7 @@ */ public class Convert { - private String context = "file:///annotations"; + private String context = NS.MPIWG_ANNOT_CTX; private static Logger logger = Logger.getRootLogger(); private String urlBase = "http://entities.mpiwg-berlin.mpg.de/annotations/"; // TODO should go into config @@ -89,6 +89,11 @@ retQuad.add(new Quadruple(annot.xpointer, NS.RDF_NS + "type", NS.OAC_NS + "Target", context)); // is target of annotation retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasTarget", annot.xpointer, context)); + if (annot.xpointer.contains("#")) { + // isPartOf base resource (without xpointer/fragment) + String baseUri = annot.xpointer.substring(0, annot.xpointer.indexOf("#")); + retQuad.add(new Quadruple(annot.xpointer, NS.DCTERMS_NS + "isPartOf", baseUri, context)); + } } else { /* * ConstrainedTarget with page number diff -r 40846c0b344d -r e5f5848892a2 src/main/java/de/mpiwg/itgroup/annotationManager/RDFHandling/RDFSearcher.java --- a/src/main/java/de/mpiwg/itgroup/annotationManager/RDFHandling/RDFSearcher.java Thu May 31 11:57:50 2012 +0200 +++ b/src/main/java/de/mpiwg/itgroup/annotationManager/RDFHandling/RDFSearcher.java Thu May 31 19:08:48 2012 +0200 @@ -7,6 +7,7 @@ import org.openrdf.query.BindingSet; import org.openrdf.query.TupleQueryResult; +import de.mpiwg.itgroup.annotationManager.Constants.NS; import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError; import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException; import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler; @@ -38,23 +39,27 @@ TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler(); String queryString = ""; // query for tuples with id as subject - StringBuilder whereString = new StringBuilder(String.format("<%s> ?p .", id)); - whereString.append(String.format("<%s> ?uri.", id)); - whereString.append(String.format("<%s> ?xpointer.", id)); - whereString.append(String.format("<%s> ?annotText.", id)); - whereString.append(String.format("<%s> ?author.", id)); - whereString.append(String.format("<%s> ?created.", id)); - whereString.append(" OPTIONAL {?annotText ?text.}"); + StringBuilder whereString = new StringBuilder(); + whereString.append(String.format("<%s> ?target.", id)); + whereString.append("?target ?uri."); + whereString.append(String.format("<%s> ?body.", id)); + whereString.append("?body ?bodyText."); + whereString.append(String.format("<%s> ?userUrl.", id)); + whereString.append(String.format("<%s> ?creationDate", id)); + + /* + * StringBuilder whereString = new + * StringBuilder(String.format("<%s> ?p .", id)); + * whereString.append(String + * .format("<%s> ?uri.", id)); + * whereString.append(String.format("<%s> ?xpointer.", + * id)); whereString.append(String.format("<%s> ?annotText.", id)); + * whereString.append(String.format("<%s> ?author.", id)); + * whereString.append(String.format("<%s> ?created.", id)); + * whereString.append(" OPTIONAL {?annotText ?text.}"); + */ - /* StringBuilder whereString = new StringBuilder(String.format("<%s> ?p .", id)); - whereString.append(String.format("<%s> ?uri.", id)); - whereString.append(String.format("<%s> ?xpointer.", id)); - whereString.append(String.format("<%s> ?annotText.", id)); - whereString.append(String.format("<%s> ?author.", id)); - whereString.append(String.format("<%s> ?created.", id)); - whereString.append(" OPTIONAL {?annotText ?text.}"); */ - - queryString = String.format("select distinct * where {%s}", whereString); + queryString = String.format("select * from <%s> where {%s}", context, whereString); logger.debug("RDFSearcher:" + queryString); @@ -63,15 +68,14 @@ while (results.hasNext()) { BindingSet result = results.next(); String annotUri = result.getBinding("uri").getValue().stringValue(); - String annotUser = result.getBinding("author").getValue().stringValue(); + String annotUser = result.getBinding("userUrl").getValue().stringValue(); String textString = ""; - if (result.getBinding("text") != null) { - textString = result.getBinding("text").getValue().stringValue(); + if (result.getBinding("bodyText") != null) { + textString = result.getBinding("bodyText").getValue().stringValue(); } - - String xpointer = result.getBinding("xpointer").getValue().stringValue(); - String created = result - .getBinding("created").getValue().stringValue(); + + String xpointer = result.getBinding("target").getValue().stringValue(); + String created = result.getBinding("creationDate").getValue().stringValue(); Annotation annot = new Annotation(xpointer, annotUser, created, textString, null, annotUri, id); retAnnots.add(annot); } @@ -79,10 +83,122 @@ e.printStackTrace(); throw new TripleStoreSearchError(); } + return retAnnots; + } + + /** + * Sucht im Triplestore nach Annotationen + * + * select * from where { ?target + * . ?ann + * ?target. ?ann ?body. ?body + * ?bodyText. } + * + * @param uri + * Adresse der Annotierten Ressource, in der Regel nicht mit dem xpointer, sonder die URI der kompletten Ressource + * oder NULL oder leer + * @param user + * Author der Annotationen, entweder als uri der Person oder ein Username, je nachdem wie die Annotatinen angelegt + * wurden. + * @param limit + * @param offset + * @return + * @throws TripleStoreHandlerException + * @throws TripleStoreSearchError + */ + public List searchByUriUser(String uri, String user, String limit, String offset) + throws TripleStoreHandlerException, TripleStoreSearchError { + + List retAnnots = new ArrayList(); + TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler(); + String queryString = ""; + + String whereString = "?s ?p ."; + // whereString +="?s ?link."; + + if (uri != null && !uri.equals("")) { + whereString = String.format("?target <%s>.", uri); + } else { + whereString = "?target ?uri."; + } + whereString += "?annotation ?target."; + whereString += "?annotation ?body."; + whereString += "?body ?bodyText."; + + if (user != null && !user.equals("")) { + if (user.startsWith("http")) { + whereString += String.format("?annotation <%s>", user); + } else { + whereString += String.format("?annotation \"%s\".", user); + } + } else { + whereString += "?annotation ?userUrl."; + } + + whereString += "?annotation ?creationDate"; + + queryString = String.format("select * from <%s> where {%s}", context, whereString); + + logger.debug("RDFSearcher:" + queryString); + + try { + TupleQueryResult results = th.querySPARQL(queryString); + + while (results.hasNext()) { + BindingSet result = results.next(); + String annotatedUri; + if (uri != null && !uri.equals("")) { + annotatedUri = uri; + } else { + annotatedUri = result.getBinding("uri").getValue().stringValue(); + } + + String annotUser; + if (user != null && !user.equals("")) { + annotUser = user; + } else { + annotUser = result.getBinding("userUrl").getValue().stringValue(); + } + + String textString = ""; + if (result.getBinding("bodyText") != null) { + textString = result.getBinding("bodyText").getValue().stringValue(); + } + String xpointer = result.getBinding("target").getValue().stringValue(); + String time = result.getBinding("creationDate").getValue().stringValue(); + String annotationUri = result.getBinding("annotation").getValue().stringValue(); + Annotation annot = new Annotation(xpointer, annotUser, time, textString, null, annotatedUri, annotationUri); + retAnnots.add(annot); + } + } catch (Exception e) { + e.printStackTrace(); + throw new TripleStoreSearchError(); + } // TODO Auto-generated method stub return retAnnots; } + public void deleteById(String id) throws TripleStoreHandlerException, TripleStoreSearchError { + if (id == null) { + return; + } + TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler(); + // delete triples with id as subject + /* + * wish Virtuoso would speak SparQL1.1... String queryString = + * String.format("WITH <%s> DELETE { <%s> ?p ?o } WHERE { <%s> ?p ?o }", context, id, id); + */ + String queryString = String.format("DELETE FROM <%s> { <%s> ?p ?o } WHERE { <%s> ?p ?o }", context, id, id); + + logger.debug("RDFSearcher:" + queryString); + + try { + th.querySPARQL(queryString); + } catch (Exception e) { + e.printStackTrace(); + throw new TripleStoreSearchError(); + } + } /** * Sucht im Triplestore nach Annotationen * @@ -98,7 +214,7 @@ * @throws TripleStoreHandlerException * @throws TripleStoreSearchError */ - public List searchByUriUser(String uri, String user, String limit, String offset) + public List searchByUriUserAlt(String uri, String user, String limit, String offset) throws TripleStoreHandlerException, TripleStoreSearchError { List retAnnots = new ArrayList(); @@ -172,24 +288,4 @@ return retAnnots; } - public void deleteById(String id) throws TripleStoreHandlerException, TripleStoreSearchError { - if (id == null) { - return; - } - TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler(); - // delete triples with id as subject - /* wish Virtuoso would speak SparQL1.1... - String queryString = String.format("WITH <%s> DELETE { <%s> ?p ?o } WHERE { <%s> ?p ?o }", context, id, id); - */ - String queryString = String.format("DELETE FROM <%s> { <%s> ?p ?o } WHERE { <%s> ?p ?o }", context, id, id); - - logger.debug("RDFSearcher:" + queryString); - - try { - th.querySPARQL(queryString); - } catch (Exception e) { - e.printStackTrace(); - throw new TripleStoreSearchError(); - } - } } diff -r 40846c0b344d -r e5f5848892a2 src/main/java/de/mpiwg/itgroup/annotationManager/restlet/AddAndReadAnnotations.java --- a/src/main/java/de/mpiwg/itgroup/annotationManager/restlet/AddAndReadAnnotations.java Thu May 31 11:57:50 2012 +0200 +++ b/src/main/java/de/mpiwg/itgroup/annotationManager/restlet/AddAndReadAnnotations.java Thu May 31 19:08:48 2012 +0200 @@ -93,7 +93,7 @@ } // - RDFSearcher searcher = new RDFSearcher("file:///annotations"); //TODO should ge into config file + RDFSearcher searcher = new RDFSearcher(NS.MPIWG_ANNOT_CTX); //TODO should ge into config file String retString=""; String lineFormat="" + @@ -195,7 +195,7 @@ // - RDFSearcher searcher = new RDFSearcher("file:///annotations"); //TODO should ge into config file + RDFSearcher searcher = new RDFSearcher(NS.MPIWG_ANNOT_CTX); //TODO should ge into config file JSONArray ja; try { @@ -440,7 +440,7 @@ try { - return new Convert("file:///annotations").storeAnnotation(annot); + return new Convert(NS.MPIWG_ANNOT_CTX).storeAnnotation(annot); } catch (TripleStoreStoreError e) { e.printStackTrace(); setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStore Error"); diff -r 40846c0b344d -r e5f5848892a2 src/main/java/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorAnnotations.java --- a/src/main/java/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorAnnotations.java Thu May 31 11:57:50 2012 +0200 +++ b/src/main/java/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorAnnotations.java Thu May 31 19:08:48 2012 +0200 @@ -68,7 +68,7 @@ boolean authenticated = isAuthenticated(entity); logger.debug("request authenticated=" + authenticated); - RDFSearcher searcher = new RDFSearcher("file:///annotations"); // TODO should ge into config file + RDFSearcher searcher = new RDFSearcher(NS.MPIWG_ANNOT_CTX); // TODO should ge into config file try { List annots = searcher.searchById(id); @@ -145,7 +145,7 @@ Annotation storedAnnot; try { // store Annotation - storedAnnot = new Convert("file:///annotations").storeAnnotation(annot); + storedAnnot = new Convert(NS.MPIWG_ANNOT_CTX).storeAnnotation(annot); } catch (TripleStoreStoreError e) { e.printStackTrace(); setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStore Error"); @@ -179,7 +179,7 @@ Annotation retValAnnot; try { - retValAnnot = new Convert("file:///annotations").storeAnnotation(annot); + retValAnnot = new Convert(NS.MPIWG_ANNOT_CTX).storeAnnotation(annot); } catch (TripleStoreStoreError e) { e.printStackTrace(); setStatus(Status.SERVER_ERROR_INTERNAL, "TripleStore Error"); @@ -314,7 +314,7 @@ setStatus(Status.CLIENT_ERROR_BAD_REQUEST); return null; } - RDFSearcher searcher = new RDFSearcher("file:///annotations"); // TODO should ge into config file + RDFSearcher searcher = new RDFSearcher(NS.MPIWG_ANNOT_CTX); // TODO should ge into config file // get stored Annotation List annots = searcher.searchById(id); if (annots.size() < 1) { @@ -327,7 +327,7 @@ // update from posted JSON annot = updateAnnotation(storedAnnot, jo, entity); // store Annotation - storedAnnot = new Convert("file:///annotations").storeAnnotation(annot); + storedAnnot = new Convert(NS.MPIWG_ANNOT_CTX).storeAnnotation(annot); /* according to https://github.com/okfn/annotator/wiki/Storage * we should return 303: see other. * but the client doesn't like it @@ -378,7 +378,7 @@ return null; } - RDFSearcher searcher = new RDFSearcher("file:///annotations"); // TODO should ge into config file + RDFSearcher searcher = new RDFSearcher(NS.MPIWG_ANNOT_CTX); // TODO should ge into config file try { // delete annotation diff -r 40846c0b344d -r e5f5848892a2 src/main/java/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorResourceImpl.java --- a/src/main/java/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorResourceImpl.java Thu May 31 11:57:50 2012 +0200 +++ b/src/main/java/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorResourceImpl.java Thu May 31 19:08:48 2012 +0200 @@ -114,8 +114,7 @@ } /** - * checks Annotator Auth plugin authentication information from headers. - * returns userId if successful. + * checks Annotator Auth plugin authentication information from headers. returns userId if successful. * * @param entity * @return @@ -201,7 +200,15 @@ xpointers.add(xpointerString); } } - jo.put("ranges", transformToRanges(xpointers)); + if (!xpointers.isEmpty()) { + // we only look at the first xpointer + String xt = getXpointerType(xpointers.get(0)); + if (xt == "range") { + jo.put("ranges", transformToRanges(xpointers)); + } else if (xt == "area") { + jo.put("areas", transformToAreas(xpointers)); + } + } // encode Annotation URL (=id) in base64 String annotUrl = annot.getAnnotationUri(); String annotId = encodeJsonId(annotUrl); @@ -214,7 +221,6 @@ return null; } - private String getXpointerType(String xpointer) { if (xpointer.contains("#xpointer")) { return "range"; @@ -223,7 +229,7 @@ } return null; } - + private JSONArray transformToRanges(List xpointers) { JSONArray ja = new JSONArray(); @@ -267,14 +273,46 @@ return ja; } + private JSONArray transformToAreas(List xpointers) { + + JSONArray ja = new JSONArray(); + + Pattern rg = Pattern.compile("#xywh=(\\w*:)([\\d\\.]+),([\\d\\.]+),([\\d\\.]+),([\\d\\.]+)"); + + try { + for (String xpointer : xpointers) { + String decoded = URLDecoder.decode(xpointer, "utf-8"); + Matcher m = rg.matcher(decoded); + + if (m.find()) { + { + JSONObject jo = new JSONObject(); + String unit = m.group(1); + jo.put("x", m.group(2)); + jo.put("y", m.group(3)); + jo.put("width", m.group(4)); + jo.put("height", m.group(5)); + ja.put(jo); + } + } + } + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return ja; + } + /** * creates an Annotation object with data from JSON. * - * uses the specification from the annotator project: {@link https - * ://github.com/okfn/annotator/wiki/Annotation-format} + * uses the specification from the annotator project: {@link https ://github.com/okfn/annotator/wiki/Annotation-format} * - * The username will be transformed to an URI if not given already as URI, - * if not it will set to the MPIWG namespace defined in + * The username will be transformed to an URI if not given already as URI, if not it will set to the MPIWG namespace defined in * de.mpiwg.itgroup.annotationManager.Constants.NS * * @param jo @@ -365,7 +403,7 @@ if (jo.has("areas")) { JSONObject area = jo.getJSONArray("areas").getJSONObject(0); String x = area.getString("x"); - String y = area.getString("end"); + String y = area.getString("y"); String width = "0"; String height = "0"; if (area.has("width")) { diff -r 40846c0b344d -r e5f5848892a2 src/main/java/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorSearch.java --- a/src/main/java/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorSearch.java Thu May 31 11:57:50 2012 +0200 +++ b/src/main/java/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorSearch.java Thu May 31 19:08:48 2012 +0200 @@ -19,6 +19,7 @@ import org.restlet.representation.StringRepresentation; import org.restlet.resource.Get; +import de.mpiwg.itgroup.annotationManager.Constants.NS; import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError; import de.mpiwg.itgroup.annotationManager.RDFHandling.Annotation; import de.mpiwg.itgroup.annotationManager.RDFHandling.RDFSearcher; @@ -59,7 +60,7 @@ String limit = form.getFirstValue("limit"); String offset = form.getFirstValue("offset"); - RDFSearcher searcher = new RDFSearcher("file:///annotations"); // TODO should go into config file + RDFSearcher searcher = new RDFSearcher(NS.MPIWG_ANNOT_CTX); // TODO should go into config file JSONArray ja; try { @@ -128,7 +129,7 @@ return null; } - RDFSearcher searcher = new RDFSearcher("file:///annotations"); // TODO + RDFSearcher searcher = new RDFSearcher(NS.MPIWG_ANNOT_CTX); // TODO // should // ge // into diff -r 40846c0b344d -r e5f5848892a2 src/main/java/de/mpiwg/itgroup/annotationManager/restlet/SearchAnnotations.java --- a/src/main/java/de/mpiwg/itgroup/annotationManager/restlet/SearchAnnotations.java Thu May 31 11:57:50 2012 +0200 +++ b/src/main/java/de/mpiwg/itgroup/annotationManager/restlet/SearchAnnotations.java Thu May 31 19:08:48 2012 +0200 @@ -89,7 +89,7 @@ return null; } - RDFSearcher searcher = new RDFSearcher("file:///annotations"); //TODO should ge into config file + RDFSearcher searcher = new RDFSearcher(NS.MPIWG_ANNOT_CTX); //TODO should ge into config file String retString="
%s
"; String lineFormat="" + @@ -190,7 +190,7 @@ // - RDFSearcher searcher = new RDFSearcher("file:///annotations"); //TODO should ge into config file + RDFSearcher searcher = new RDFSearcher(NS.MPIWG_ANNOT_CTX); //TODO should ge into config file JSONArray ja; try {
%s