diff src/de/mpiwg/itgroup/annotationManager/restlet/AddAndSearchAnnotations.java @ 5:0be9d53a6967

editor for annotations
author dwinter
date Tue, 13 Dec 2011 17:43:46 +0100
parents c32080f364c6
children
line wrap: on
line diff
--- a/src/de/mpiwg/itgroup/annotationManager/restlet/AddAndSearchAnnotations.java	Fri Dec 02 09:25:37 2011 +0100
+++ b/src/de/mpiwg/itgroup/annotationManager/restlet/AddAndSearchAnnotations.java	Tue Dec 13 17:43:46 2011 +0100
@@ -15,6 +15,7 @@
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
+import org.restlet.Context;
 import org.restlet.data.ClientInfo;
 import org.restlet.data.Form;
 import org.restlet.data.MediaType;
@@ -34,7 +35,10 @@
 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert;
 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert.Annotation;
 import de.mpiwg.itgroup.annotationManager.RDFHandling.RDFSearcher;
-import de.mpiwg.itgroup.nimanager.exceptions.TripleStoreHandlerException;
+import de.mpiwg.itgroup.annotationManager.drupal.AnnotationHandler;
+import de.mpiwg.itgroup.annotationManager.drupal.UnknowUserException;
+import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException;
+
 
 public class AddAndSearchAnnotations extends ServerResource {
 
@@ -69,6 +73,15 @@
 		String limit=form.getFirstValue("limit");
 		String offset=form.getFirstValue("offset");
 
+		try {
+			if (uri!=null){
+			uri = URLDecoder.decode(uri, "utf-8");
+			}
+		} catch (UnsupportedEncodingException e1) {
+			e1.printStackTrace();
+			setStatus(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
+			return null;
+		}
 		
 //		
 		RDFSearcher searcher = new RDFSearcher("file:///annotations"); //TODO should ge into config file
@@ -331,10 +344,16 @@
 	@Post("html")
 	public Representation doPostHtml(Representation entity) {
 		Annotation retValAnnot = doPost(entity);
+		if (retValAnnot == null) {
+			return null;
+		}
+		
+		
 		String retVal=retValAnnot.annotationUri;
 		if (retVal == null) {
 			return null;
 		}
+		
 		String text = String.format(
 				"<html><body><a href=\"%s\">%s</a></body></html>", retVal
 				.replace(">", "").replace("<", ""),
@@ -446,9 +465,11 @@
 		Convert.Annotation annot;
 		Form form = new Form(entity);
 		String username = form.getValues("username");
+		String mode = form.getValues("mode");
 		String password = form.getValues("password");
 		String xpointer = form.getValues("xpointer");
 		String text = form.getValues("text");
+		String title = form.getValues("title");
 		String url = form.getValues("url");
 		String type = form.getValues("type");
 		RestServer restServer = (RestServer) getApplication();
@@ -479,10 +500,35 @@
 		}
 
 		//username should be a URI, if not it will set to the MPIWG namespace defined in de.mpiwg.itgroup.annotationManager.Constants.NS
+		String usernameOrig=username;
 		if (!username.startsWith("http"))
 			username=NS.MPIWG_PERSONS+username;
 		
-		annot = new Convert.Annotation(xpointer, username, null, text,
+		if (mode.equals("complexAnnotation")){// Annotation mit text in externer ressource
+			
+			Context context = getContext();
+			String drupalPath = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.drupalServer");
+			
+			
+			AnnotationHandler ah = new AnnotationHandler(drupalPath);
+			JSONObject newAnnot;
+			try {
+				newAnnot = ah.createAnnotation(title, text, usernameOrig, password);
+			} catch (UnknowUserException e1) {
+				setStatus(Status.CLIENT_ERROR_FORBIDDEN);
+				e1.printStackTrace();
+				return null;
+			}
+			try {
+				annot= new Convert.Annotation(xpointer, username, null, text, type, newAnnot.getString("node_uri"));
+			} catch (JSONException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+				setStatus(Status.SERVER_ERROR_INTERNAL);
+				return null;
+			}
+		} else
+			annot = new Convert.Annotation(xpointer, username, null, text,
 				type, url);
 		return annot;
 	}