diff src/de/mpiwg/itgroup/nimanager/persons/PersonByNameService.java @ 5:cdc4c12262b1

minor changes
author dwinter
date Wed, 21 Dec 2011 22:10:38 +0100
parents f986e74583eb
children 290d859f036b
line wrap: on
line diff
--- a/src/de/mpiwg/itgroup/nimanager/persons/PersonByNameService.java	Tue Dec 13 17:46:51 2011 +0100
+++ b/src/de/mpiwg/itgroup/nimanager/persons/PersonByNameService.java	Wed Dec 21 22:10:38 2011 +0100
@@ -1,6 +1,10 @@
 package de.mpiwg.itgroup.nimanager.persons;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringWriter;
 import java.io.UnsupportedEncodingException;
+import java.io.Writer;
 import java.net.URLDecoder;
 import java.sql.SQLException;
 import java.util.ArrayList;
@@ -8,6 +12,7 @@
 import java.util.List;
 import java.util.Map;
 
+import org.apache.commons.io.IOUtils;
 import org.json.JSONArray;
 import org.openrdf.model.Statement;
 import org.openrdf.repository.RepositoryException;
@@ -58,7 +63,7 @@
 	@Options
 	public void doOptions(Representation entity) {
 		Form responseHeaders = (Form) getResponse().getAttributes().get(
-				"org.restlet.http.headers");
+		"org.restlet.http.headers");
 		if (responseHeaders == null) {
 			responseHeaders = new Form();
 			getResponse().getAttributes().put("org.restlet.http.headers",
@@ -85,86 +90,85 @@
 					responseHeaders);
 		}
 		responseHeaders.add("Access-Control-Allow-Origin", "*");
-		
+
 		JSONArray resultsJsonArray = new JSONArray();
 		try {
 			String id = (String) getRequest().getAttributes().get("name");
 			id = URLDecoder.decode(id, "utf-8");
-			String queryPersonWPMD="sparql select DISTINCT * "+
-			"FROM <"+PERSONS_CONTEXT+"> "+
-			"FROM <file://newpersonsFromProjects>"+
-			" FROM <file://personendataWikipedia>"+
-			" where { " +
-			"{GRAPH <"+PERSONS_CONTEXT+">" +
-			"{"+
-			"?person <"+RELATION_FOR_NAME_SEARCH+"> \""+id+"\"@en."+
-			" ?ident <http://ontologies.mpiwg-berlin.mpg.de/authorities/namedIdentities/identifies_NamedEntity> ?person."+
-			" OPTIONAL {?wikipedia crm:P1_is_identified_by ?ident.}"+
-			" OPTIONAL {?wikipedia <http://dbpedia.org/ontology/birthDate> ?birthDate.}"+
-			" OPTIONAL {?wikipedia <http://dbpedia.org/ontology/deathDate> ?deathDate.}"
-			+"}" +
-			"} UNION {" +
-			"GRAPH <file://newpersonsFromProjects> {" +
-			"?person <"+RELATION_FOR_NAME_SEARCH+"> \""+id+"\""+
-			"}" +
-			"}" +
-			"}";
+
+			//InputStream is = getClass().getResourceAsStream("de/mpiwg/itgroup/nimanager/persons/personQueryString");
+			InputStream is = getClass().getResourceAsStream("personQueryString");
+			Writer sw = new StringWriter();
+			IOUtils.copy(is, sw,"utf-8");
+
+			String queryPersonWPMD=String.format(sw.toString(), id);
+
 			java.sql.Statement smt = th.sqlCon.createStatement();
-		
-			
+
+
 			smt.execute(queryPersonWPMD);
 			VirtuosoResultSet rs = (VirtuosoResultSet) smt.getResultSet();
 			Map<String,String> resultMap = new HashMap<String, String>();
 			while(rs.next()){
 				if (rs.getString("person")!=null){
-				resultMap.put("person",rs.getString("person"));
+					resultMap.put("person",rs.getString("person"));
 				}else  {
 					resultMap.put("person","");
 				}
-				if (rs.getString("wikipedia")!=null){
-				resultMap.put("wikipedia",rs.getString("wikipedia"));
+				if (rs.getString("objects")!=null){
+					resultMap.put("wikipedia",rs.getString("objects"));
 				} else  {
 					resultMap.put("wikipedia","");
 				}
+				if (rs.getString("gnd")!=null){
+					resultMap.put("gnd",rs.getString("gnd"));
+				} else  {
+					resultMap.put("gnd","");
+				}
 				if (rs.getString("birthDate")!=null){
-				resultMap.put("birthDate",rs.getString("birthDate"));
+					resultMap.put("birthDate",rs.getString("birthDate"));
 				} else {
 					resultMap.put("birthDate","");
 				}
 				if (rs.getString("deathDate")!=null){
-				resultMap.put("deathDate",rs.getString("deathDate"));
+					resultMap.put("deathDate",rs.getString("deathDate"));
 				} else {
 					resultMap.put("deathDate","");
 				}
 				resultMap.put("name",id);
 				resultsJsonArray.put(resultMap);
 			}
-			
+
 			return new JsonRepresentation(resultsJsonArray);
 
 		} catch (UnsupportedEncodingException e) {
 			e.printStackTrace();
 			setStatus(Status.SERVER_ERROR_INTERNAL, "REPOSITORY ERROR");
 			return new JsonRepresentation(new JSONArray());
-					
+
 		} catch (SQLException e) {
-			
+
 			e.printStackTrace();
 			setStatus(Status.SERVER_ERROR_INTERNAL, "REPOSITORY SQL ERROR");
 			return new JsonRepresentation(new JSONArray());
-			
+
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+			setStatus(Status.SERVER_ERROR_INTERNAL);
+			return new JsonRepresentation(new JSONArray());
 		}
-	
-	
+
+
 	}
-	
+
 	@Get("xml")
 	public Representation getXML() {
 		List<MediaType> supported=new ArrayList<MediaType>();
 		supported.add(MediaType.APPLICATION_JSON);
-	
+
 		ClientInfo ci = getRequest().getClientInfo();
-		
+
 		if(ci==null){ //don't know why this can happen, but sometimes does
 			setStatus(Status.SERVER_ERROR_INTERNAL);
 			return null;
@@ -172,7 +176,7 @@
 		MediaType types = getRequest().getClientInfo().getPreferredMediaType(supported);
 		if (types.equals(MediaType.APPLICATION_JSON))
 			return getJson();
-			
+
 		try {
 			String id = (String) getRequest().getAttributes().get("name");
 			id = URLDecoder.decode(id, "utf-8");
@@ -183,7 +187,7 @@
 			while (statements.hasNext()) {
 				Statement statement = statements.next();
 				ret += "<result subj=\"" + statement.getSubject().stringValue()
-						+ "\"/>";
+				+ "\"/>";
 			}
 			ret += "</results>";
 
@@ -204,15 +208,15 @@
 					MediaType.TEXT_XML);
 		}
 	}
-	
+
 	@Get("html")
 	public Representation getHTML() {
 
 		List<MediaType> supported=new ArrayList<MediaType>();
 		supported.add(MediaType.APPLICATION_JSON);
-		
+
 		ClientInfo ci = getRequest().getClientInfo();
-		
+
 		if(ci==null){ //don't know why this can happen, but sometimes does
 			setStatus(Status.SERVER_ERROR_INTERNAL);
 			return null;
@@ -221,7 +225,7 @@
 		if (types!=null)
 			if (types.equals(MediaType.APPLICATION_JSON))
 				return getJson();
-		
+
 		Form responseHeaders = (Form) getResponse().getAttributes().get(
 		"org.restlet.http.headers");
 		if (responseHeaders == null) {
@@ -233,59 +237,34 @@
 		try {
 			String id = (String) getRequest().getAttributes().get("name");
 			id = URLDecoder.decode(id, "utf-8");
-//			RepositoryResult<Statement> statements = th.getStatements(null,
-//					th.createUri(RELATION_FOR_NAME_SEARCH),
-//					th.createLiteral(id, "en"), PERSONS_CONTEXT);
-			String ret = "<html><body><div id=\"resultNames\">";
-			String queryPersonWPMD="sparql select * "+
-"FROM <"+PERSONS_CONTEXT+"> "+
-" FROM <file://personendataWikipedia>"+
-" where { "+
-" ?person <"+RELATION_FOR_NAME_SEARCH+"> \""+id+"\"@en."+
-" ?ident <http://ontologies.mpiwg-berlin.mpg.de/authorities/namedIdentities/identifies_NamedEntity> ?person."+
-" ?wikipedia crm:P1_is_identified_by ?ident."+
-" ?wikipedia <http://dbpedia.org/ontology/birthDate> ?birthDate."+
-" ?wikipedia <http://dbpedia.org/ontology/deathDate> ?deathDate."
-+"}";
+			
+			InputStream is = getClass().getResourceAsStream("personQueryString");
+			Writer sw = new StringWriter();
+			IOUtils.copy(is, sw,"utf-8");
+
+			String queryPersonWPMD=String.format(sw.toString(), id);
+
 			java.sql.Statement smt = th.sqlCon.createStatement();
-		
-			
+
+
 			smt.execute(queryPersonWPMD);
 			VirtuosoResultSet rs = (VirtuosoResultSet) smt.getResultSet();
 			
+			String ret = "<html><body><div id=\"resultNames\">";
 			while(rs.next()){
 				String person= rs.getString("person");
-				String wikipedia= rs.getString("wikipedia");
+				String gnd= rs.getString("gnd");
 				String bd= rs.getString("birthDate");
 				String dd= rs.getString("deathDate");
-				
-				ret+=String.format("<div><a href=\"%s\">%s (%s-%s)</a></div>",wikipedia,id,bd,dd);
+
+				ret+=String.format("<div><a href=\"%s\">%s (%s-%s)</a></div>",gnd,id,bd,dd);
+				ret+=String.format("<div><a href=\"%s\">%s </a></div>",person,person);
 			}
-				
-			
-//			while (statements.hasNext()) {
-//				Statement statement = statements.next();
-//				
-//				
-//				
-//				
-//				
-//				//ret +=String.format("<div><a href=\"../id/%s\">%s</a></div>",URLEncoder.encode(statement.getSubject().stringValue()),statement.getSubject().stringValue() );
-//				ret +=String.format("<div><a href=\"%s\">%s</a></div>",statement.getSubject().stringValue(),statement.getSubject().stringValue() );
-//			
-//			}
-			
+
 			ret += "</div></body></html>";
 
 			return new StringRepresentation(ret, MediaType.TEXT_HTML);
 
-//		} catch (RepositoryException e) {
-//
-//			e.printStackTrace();
-//			setStatus(Status.SERVER_ERROR_INTERNAL, "REPOSITORY ERROR");
-//			return new StringRepresentation(
-//					"<xml><error>Repository Error</error></xml>",
-//					MediaType.TEXT_HTML);
 		} catch (UnsupportedEncodingException e) {
 			e.printStackTrace();
 			setStatus(Status.SERVER_ERROR_INTERNAL, "REPOSITORY ERROR");
@@ -293,13 +272,89 @@
 					"<xml><error>Unsupported encoding</error></xml>",
 					MediaType.TEXT_HTML);
 		} catch (SQLException e) {
-			
+
 			e.printStackTrace();
 			setStatus(Status.SERVER_ERROR_INTERNAL, "REPOSITORY SQL ERROR");
 			return new StringRepresentation(
 					"<xml><error>Unsupported encoding</error></xml>",
 					MediaType.TEXT_HTML);
+		} catch (IOException e) {
+			e.printStackTrace();
+			setStatus(Status.SERVER_ERROR_INTERNAL);
+			return null;
 		}
 	}
 
+	public List<Map<String,String>> getMaps(String id){
+		ArrayList<Map<String, String>> retList = new ArrayList<Map<String,String>>();
+		try {
+
+
+			//InputStream is = getClass().getResourceAsStream("de/mpiwg/itgroup/nimanager/persons/personQueryString");
+			InputStream is = getClass().getResourceAsStream("personQueryString");
+			Writer sw = new StringWriter();
+			IOUtils.copy(is, sw,"utf-8");
+
+			String queryPersonWPMD=String.format(sw.toString(), id);
+
+			java.sql.Statement smt = th.sqlCon.createStatement();
+
+
+			smt.execute(queryPersonWPMD);
+			VirtuosoResultSet rs = (VirtuosoResultSet) smt.getResultSet();
+			Map<String,String> resultMap = new HashMap<String, String>();
+			while(rs.next()){
+				if (rs.getString("person")!=null){
+					resultMap.put("person",rs.getString("person"));
+				}else  {
+					resultMap.put("person","");
+				}
+				if (rs.getString("objects")!=null){
+					resultMap.put("wikipedia",rs.getString("objects"));
+				} else  {
+					resultMap.put("wikipedia","");
+				}
+				if (rs.getString("gnd")!=null){
+					resultMap.put("gnd",rs.getString("gnd"));
+				} else  {
+					resultMap.put("gnd","");
+				}
+				if (rs.getString("birthDate")!=null){
+					resultMap.put("birthDate",rs.getString("birthDate"));
+				} else {
+					resultMap.put("birthDate","");
+				}
+				if (rs.getString("deathDate")!=null){
+					resultMap.put("deathDate",rs.getString("deathDate"));
+				} else {
+					resultMap.put("deathDate","");
+				}
+				resultMap.put("name",id);
+			retList.add(resultMap);	
+			}
+
+		return retList;
+
+		} catch (UnsupportedEncodingException e) {
+			e.printStackTrace();
+			
+			return null;
+
+		} catch (SQLException e) {
+
+			e.printStackTrace();
+			
+			return null;
+
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+			setStatus(Status.SERVER_ERROR_INTERNAL);
+			return null;
+		}
+
+
+	
+
+	}
 }