# HG changeset patch # User dwinter # Date 1324501838 -3600 # Node ID cdc4c12262b115344c19fa8ea85121ee505a8b2b # Parent f986e74583eba3c459f5b9a83b894b844a2c889d minor changes diff -r f986e74583eb -r cdc4c12262b1 src/de/mpiwg/itgroup/nimanager/luceneIndices/Importer.java --- 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(); diff -r f986e74583eb -r cdc4c12262b1 src/de/mpiwg/itgroup/nimanager/persons/PersonByNameService.java --- 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 "+ - " FROM "+ - " where { " + - "{GRAPH <"+PERSONS_CONTEXT+">" + - "{"+ - "?person <"+RELATION_FOR_NAME_SEARCH+"> \""+id+"\"@en."+ - " ?ident ?person."+ - " OPTIONAL {?wikipedia crm:P1_is_identified_by ?ident.}"+ - " OPTIONAL {?wikipedia ?birthDate.}"+ - " OPTIONAL {?wikipedia ?deathDate.}" - +"}" + - "} UNION {" + - "GRAPH {" + - "?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 resultMap = new HashMap(); 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 supported=new ArrayList(); 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 += ""; + + "\"/>"; } ret += ""; @@ -204,15 +208,15 @@ MediaType.TEXT_XML); } } - + @Get("html") public Representation getHTML() { List supported=new ArrayList(); 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 statements = th.getStatements(null, -// th.createUri(RELATION_FOR_NAME_SEARCH), -// th.createLiteral(id, "en"), PERSONS_CONTEXT); - String ret = "
"; - String queryPersonWPMD="sparql select * "+ -"FROM <"+PERSONS_CONTEXT+"> "+ -" FROM "+ -" where { "+ -" ?person <"+RELATION_FOR_NAME_SEARCH+"> \""+id+"\"@en."+ -" ?ident ?person."+ -" ?wikipedia crm:P1_is_identified_by ?ident."+ -" ?wikipedia ?birthDate."+ -" ?wikipedia ?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 = "
"; 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("",wikipedia,id,bd,dd); + + ret+=String.format("",gnd,id,bd,dd); + ret+=String.format("",person,person); } - - -// while (statements.hasNext()) { -// Statement statement = statements.next(); -// -// -// -// -// -// //ret +=String.format("",URLEncoder.encode(statement.getSubject().stringValue()),statement.getSubject().stringValue() ); -// ret +=String.format("",statement.getSubject().stringValue(),statement.getSubject().stringValue() ); -// -// } - + ret += "
"; return new StringRepresentation(ret, MediaType.TEXT_HTML); -// } catch (RepositoryException e) { -// -// e.printStackTrace(); -// setStatus(Status.SERVER_ERROR_INTERNAL, "REPOSITORY ERROR"); -// return new StringRepresentation( -// "Repository Error", -// MediaType.TEXT_HTML); } catch (UnsupportedEncodingException e) { e.printStackTrace(); setStatus(Status.SERVER_ERROR_INTERNAL, "REPOSITORY ERROR"); @@ -293,13 +272,89 @@ "Unsupported encoding", MediaType.TEXT_HTML); } catch (SQLException e) { - + e.printStackTrace(); setStatus(Status.SERVER_ERROR_INTERNAL, "REPOSITORY SQL ERROR"); return new StringRepresentation( "Unsupported encoding", MediaType.TEXT_HTML); + } catch (IOException e) { + e.printStackTrace(); + setStatus(Status.SERVER_ERROR_INTERNAL); + return null; } } + public List> getMaps(String id){ + ArrayList> retList = new ArrayList>(); + 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 resultMap = new HashMap(); + 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; + } + + + + + } } diff -r f986e74583eb -r cdc4c12262b1 src/de/mpiwg/itgroup/nimanager/persons/PersonListService.java --- 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:
"; - for (int i=0;i%s
", 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%s", array[i],array[i]); + } + } else if (mode.equals("long")){ + PersonByNameService ps = new PersonByNameService(); + for (int i=0;i> maps = ps.getMaps(URLDecoder.decode(array[i],"utf-8")); + for (Mapmap:maps){ + ret+=String.format("
%s (%s-%s)
", array[i],map.get("birthDate"),map.get("deathDate")); + ret+=String.format("",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); } diff -r f986e74583eb -r cdc4c12262b1 src/de/mpiwg/itgroup/nimanager/persons/personQueryString --- /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 +FROM +FROM +FROM +where { +{?person "%s". + +?ident ?person. +?gnd crm:P1_is_identified_by ?ident. + +optional {?person2 ?person. +?ident2 ?person2. +?objects crm:P1_is_identified_by ?ident2.} + +optional {?gnd ?birthDate.} +optional {?gnd ?deathDate.} +} +UNION {GRAPH {?person "Einstein, Albert"}}} \ No newline at end of file