diff src/main/java/de/mpiwg/monographs/servlet/AbstractMonographServlet.java @ 0:fcb8807fbd84

Fist commit!
author "jurzua <jurzua@mpiwg-berlin.mpg.de>"
date Tue, 10 Mar 2015 15:15:30 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/mpiwg/monographs/servlet/AbstractMonographServlet.java	Tue Mar 10 15:15:30 2015 +0100
@@ -0,0 +1,66 @@
+package de.mpiwg.monographs.servlet;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.servlet.http.HttpServlet;
+
+import org.codehaus.jettison.json.JSONArray;
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+import org.swordapp.server.SwordServerException;
+
+import edu.harvard.iq.dvn.core.study.EditStudyService;
+import edu.harvard.iq.dvn.core.study.FileMetadata;
+import edu.harvard.iq.dvn.core.study.Study;
+import edu.harvard.iq.dvn.core.study.StudyVersion;
+
+public class AbstractMonographServlet extends HttpServlet {
+	
+	private static final Logger logger = Logger.getLogger("monographs.AbstractMonographServlet");
+	
+	private Context ctx = null;
+	
+	protected JSONObject jsonStudyVersion(String studyGlobalId) throws SwordServerException{
+		
+		Study study0 = getStudy(studyGlobalId);
+		StudyVersion study = study0.getLatestVersion();
+		
+		return MonographUtils.jsonStudyVersion(study);
+	}
+	
+	protected Study getStudy(String studyGlobalId) throws SwordServerException{
+		
+		EditStudyService editStudyService;
+
+		try {
+			editStudyService = (EditStudyService) getCtx().lookup("java:comp/env/editStudy");
+		} catch (NamingException ex) {
+			logger.info("problem looking up editStudyService");
+			throw new SwordServerException("problem looking up editStudyService");
+		}
+		
+		Study study = editStudyService.getStudyByGlobalId(studyGlobalId);
+		return study;
+	}
+
+	protected Context getCtx() throws NamingException{
+		if(ctx == null)
+			ctx = new InitialContext();
+		return ctx;
+	}
+	
+	protected void error(JSONObject jsonResponse, Exception e){
+		logger.log(Level.SEVERE, e.getMessage());
+		try {
+			jsonResponse.put("status", "error");
+			jsonResponse.put("error", e.getMessage());
+		} catch (JSONException e1) {
+			e1.printStackTrace();
+		}
+	}
+	
+}