Mercurial > hg > AnnotationManagerN4J
diff src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java @ 5:bbf0cc5bee29
version 0.2 really works now
author | casties |
---|---|
date | Tue, 03 Jul 2012 21:23:17 +0200 |
parents | 3599b29c393f |
children | b2bfc3bc9ba8 |
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java Mon Jul 02 22:39:46 2012 +0200 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java Tue Jul 03 21:23:17 2012 +0200 @@ -8,7 +8,9 @@ import java.net.URLEncoder; import java.security.InvalidKeyException; import java.security.SignatureException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Calendar; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -177,6 +179,7 @@ * @return */ public JSONObject createAnnotatorJson(Annotation annot) { + // return user as a JSON object (otherwise just as string) boolean makeUserObject = true; JSONObject jo = new JSONObject(); try { @@ -197,8 +200,11 @@ // save as id userObject.put("id", userId); // get full name - RestServer restServer = (RestServer) getApplication(); - String userName = restServer.getUserNameFromLdap(userId); + String userName = annot.getCreatorName(); + if (userName == null) { + RestServer restServer = (RestServer) getApplication(); + userName = restServer.getFullNameFromLdap(userId); + } userObject.put("name", userName); // save user object jo.put("user", userObject); @@ -240,7 +246,8 @@ try { for (String xpointer : xpointers) { - String decoded = URLDecoder.decode(xpointer, "utf-8"); + //String decoded = URLDecoder.decode(xpointer, "utf-8"); + String decoded = xpointer; Matcher m = rg.matcher(decoded); if (m.find()) { @@ -265,11 +272,7 @@ } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); - } catch (UnsupportedEncodingException e) { - // TODO Auto-generated catch block - e.printStackTrace(); } - return ja; } @@ -281,7 +284,8 @@ try { for (String xpointer : xpointers) { - String decoded = URLDecoder.decode(xpointer, "utf-8"); + //String decoded = URLDecoder.decode(xpointer, "utf-8"); + String decoded = xpointer; Matcher m = rg.matcher(decoded); if (m.find()) { @@ -299,15 +303,11 @@ } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); - } catch (UnsupportedEncodingException e) { - // TODO Auto-generated catch block - e.printStackTrace(); } - return ja; } - protected String parseArea(JSONObject area) throws JSONException, UnsupportedEncodingException { + protected String parseArea(JSONObject area) throws JSONException { String x = area.getString("x"); String y = area.getString("y"); String width = "0"; @@ -316,24 +316,24 @@ width = area.getString("width"); height = area.getString("height"); } - String fragment = URLEncoder.encode(String.format("xywh=fraction:%s,%s,%s,%s", x, y, width, height), "utf-8"); + String fragment = String.format("xywh=fraction:%s,%s,%s,%s", x, y, width, height); return fragment; } - protected String parseRange(JSONObject range) throws JSONException, UnsupportedEncodingException { + protected String parseRange(JSONObject range) throws JSONException { String start = range.getString("start"); String end = range.getString("end"); String startOffset = range.getString("startOffset"); String endOffset = range.getString("endOffset"); - String fragment = URLEncoder.encode(String.format( + String fragment = String.format( "xpointer(start-point(string-range(\"%s\",%s,1))/range-to(end-point(string-range(\"%s\",%s,1))))", start, - startOffset, end, endOffset), "utf-8"); + startOffset, end, endOffset); return fragment; } /** - * creates an Annotation object with data from JSON. + * Creates an Annotation object with data from JSON. * * uses the specification from the annotator project: {@link https * ://github.com/okfn/annotator/wiki/Annotation-format} @@ -351,9 +351,25 @@ return updateAnnotation(new Annotation(), jo, entity); } + /** + * Updates an Annotation object with data from JSON. + * + * 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 + * de.mpiwg.itgroup.annotationManager.Constants.NS + * + * @param annot + * @param jo + * @return + * @throws JSONException + * @throws UnsupportedEncodingException + */ public Annotation updateAnnotation(Annotation annot, JSONObject jo, Representation entity) throws JSONException, UnsupportedEncodingException { - // annotated uri + // target uri if (jo.has("uri")) { annot.setTargetBaseUri(jo.getString("uri")); } @@ -396,9 +412,13 @@ if (username == null) { username = authUser; } - // username should be a URI, if not it will set to the MPIWG namespace - // defined in - // de.mpiwg.itgroup.annotationManager.Constants.NS + // try to get full name + if (username != null) { + RestServer restServer = (RestServer) getApplication(); + String fullName = restServer.getFullNameFromLdap(username); + annot.setCreatorName(fullName); + } + // userUri should be a URI, if not it will set to the MPIWG namespace if (userUri == null) { if (username.startsWith("http")) { userUri = username; @@ -407,6 +427,16 @@ } } // TODO: should we overwrite the creator? + if (annot.getCreatorUri() == null) { + annot.setCreatorUri(userUri); + } + + if (annot.getCreated() == null) { + // set creation date + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + String ct = format.format(Calendar.getInstance().getTime()); + annot.setCreated(ct); + } // create xpointer from the first range/area if (jo.has("ranges")) {