changeset 5:cdc4c12262b1

minor changes
author dwinter
date Wed, 21 Dec 2011 22:10:38 +0100
parents f986e74583eb
children 290d859f036b
files src/de/mpiwg/itgroup/nimanager/luceneIndices/Importer.java src/de/mpiwg/itgroup/nimanager/persons/PersonByNameService.java src/de/mpiwg/itgroup/nimanager/persons/PersonListService.java src/de/mpiwg/itgroup/nimanager/persons/personQueryString
diffstat 4 files changed, 217 insertions(+), 90 deletions(-) [+]
line wrap: on
line diff
--- a/src/de/mpiwg/itgroup/nimanager/luceneIndices/Importer.java	Tue Dec 13 17:46:51 2011 +0100
+++ b/src/de/mpiwg/itgroup/nimanager/luceneIndices/Importer.java	Wed Dec 21 22:10:38 2011 +0100
@@ -124,7 +124,7 @@
 
 		String[] indexFields=new String[]{"http://xmlns.com/foaf/0.1/name","http://xmlns.com/foaf/0.1/lastName","http://xmlns.com/foaf/0.1/firstName"};
 
-		Importer im = new Importer("file://mpiwg_persons.rdf", indexFields, th, "/tmp/tripleIndex");
+		Importer im = new Importer("file://mpiwg_persons_dnb.rdf", indexFields, th, "/tmp/tripleIndex");
 		im.initializeIndexer(true);
 		im.writeStatementsToIndex();
 		im.writer.optimize();
--- 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;
+		}
+
+
+	
+
+	}
 }
--- a/src/de/mpiwg/itgroup/nimanager/persons/PersonListService.java	Tue Dec 13 17:46:51 2011 +0100
+++ b/src/de/mpiwg/itgroup/nimanager/persons/PersonListService.java	Wed Dec 21 22:10:38 2011 +0100
@@ -2,6 +2,8 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -22,6 +24,7 @@
 import org.restlet.Context;
 import org.restlet.data.Form;
 import org.restlet.data.MediaType;
+import org.restlet.data.Status;
 import org.restlet.engine.component.ChildContext;
 import org.restlet.ext.json.JsonRepresentation;
 import org.restlet.representation.Representation;
@@ -109,15 +112,63 @@
 		}
 		responseHeaders.add("Access-Control-Allow-Origin", "*");
 		Form form = getRequest().getResourceRef().getQueryAsForm();
-		String term = form.getFirstValue("term");
+		
+		
+		String [] array= null;
+		String term="";
 		
-		String [] array = getPersonsAsArray(term);
-		String ret="";
+		String ret="Search Name:<form method=\"get\" ><input name=\"term\" size=\"30\"/></form>";
 		
-		for (int i=0;i<array.length;i++){
-			ret+=String.format("<div class=\"personName\"><a href=\"./person/name/%s\">%s</a></div>", array[i],array[i]);
+		if (form.getValuesMap().keySet().contains("term")){
+			term = form.getFirstValue("term");
+			
+		} else {
+			return new StringRepresentation(ret,MediaType.TEXT_HTML);
 		}
 		
+		array = getPersonsAsArray(term);
+//		String personId="";
+//		
+//		if (form.getValuesMap().keySet().contains("personID")){
+//			personId = form.getFirstValue("personID");
+//			array = new String[]{personId};
+//		}
+		
+		
+		
+		
+		
+		String mode="short";
+		if (form.getValuesMap().keySet().contains("mode")){
+			mode = form.getFirstValue("mode");
+		}
+		
+		
+		
+		if (mode.equals("short")){
+			for (int i=0;i<array.length;i++){
+				ret+=String.format("<div class=\"personName\"><a href=\"./person/name/%s\">%s</a></div>", array[i],array[i]);
+			}
+		} else if (mode.equals("long")){
+			PersonByNameService ps = new PersonByNameService();
+			for (int i=0;i<array.length;i++){
+				
+				try {
+					List<Map<String, String>> maps = ps.getMaps(URLDecoder.decode(array[i],"utf-8"));
+					for (Map<String,String>map:maps){
+						ret+=String.format("<div class=\"personName\">%s (%s-%s)</div>", array[i],map.get("birthDate"),map.get("deathDate"));
+						ret+=String.format("<div><a href=\"%s\">view in GND</a></div>",map.get("gnd"));
+					}
+				} catch (UnsupportedEncodingException e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
+				}
+				
+			}
+		} else {
+			setStatus(Status.SERVER_ERROR_NOT_IMPLEMENTED);
+			return new StringRepresentation("MODE "+mode+" unknown!",MediaType.TEXT_HTML);
+		}
 		
 		return new StringRepresentation(ret,MediaType.TEXT_HTML);
 	}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/de/mpiwg/itgroup/nimanager/persons/personQueryString	Wed Dec 21 22:10:38 2011 +0100
@@ -0,0 +1,21 @@
+sparql
+
+select * 
+FROM <file://mpiwg_persons_dnb.rdf>  
+FROM <file://mpiwg_persons_2.rdf> 
+FROM <file:///GND.rdf> 
+FROM <http://identifiedNames>
+where {
+{?person <http://xmlns.com/foaf/0.1/name> "%s". 
+
+?ident <http://ontologies.mpiwg-berlin.mpg.de/authorities/namedIdentities/identifies_NamedEntity> ?person. 
+?gnd crm:P1_is_identified_by ?ident.
+
+optional {?person2 <http://www.w3.org/2002/07/owl#sameAs> ?person.
+?ident2 <http://ontologies.mpiwg-berlin.mpg.de/authorities/namedIdentities/identifies_NamedEntity> ?person2. 
+?objects crm:P1_is_identified_by ?ident2.}
+
+optional {?gnd <http://RDVocab.info/ElementsGr2/dateOfBirth> ?birthDate.}
+optional {?gnd <http://RDVocab.info/ElementsGr2/dateOfDeath> ?deathDate.} 
+}
+UNION {GRAPH <file://newpersonsFromProjects> {?person <http://xmlns.com/foaf/0.1/name> "Einstein, Albert"}}}
\ No newline at end of file