Mercurial > hg > NamedIdentityManager
view src/de/mpiwg/itgroup/nimanager/persons/PersonListService.java @ 13:dacf1b7b3ad5 default tip
libs added
author | dwinter |
---|---|
date | Mon, 17 Sep 2012 10:46:36 +0200 |
parents | e0aca5ffc4df |
children |
line wrap: on
line source
package de.mpiwg.itgroup.nimanager.persons; 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; import java.util.Map; import org.apache.log4j.Logger; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; import org.json.JSONArray; 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; import org.restlet.representation.StringRepresentation; import org.restlet.resource.Get; import org.restlet.resource.Options; import org.restlet.resource.ServerResource; public class PersonListService extends ServerResource { //private String tripleIndexPath="/Users/dwinter/Documents/Projekte/ECHO-eSciDoc-MPDL/escidocMPIWG/NamedIdentityManager/data/tripleIndex"; private String tripleIndexPath; //private String tripleIndexPath="/usr/local/NamedIdentityManager/data/tripleIndex"; private Logger rl = Logger.getRootLogger(); private IndexSearcher searcher; private StandardAnalyzer analyzer; public PersonListService() throws CorruptIndexException, IOException{ ChildContext context = (ChildContext)Context.getCurrent(); tripleIndexPath = context.getParameters().getFirstValue("de.mpwig.itgroup.personSearch.index"); searcher = new IndexSearcher(FSDirectory.open(new File(tripleIndexPath))); analyzer = new StandardAnalyzer(Version.LUCENE_31); } @Options public void doOptions(Representation entity) { Form responseHeaders = (Form) getResponse().getAttributes().get( "org.restlet.http.headers"); if (responseHeaders == null) { responseHeaders = new Form(); getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders); } responseHeaders.add("Access-Control-Allow-Origin", "*"); responseHeaders.add("Access-Control-Allow-Methods", "POST,OPTIONS,GET"); responseHeaders.add("Access-Control-Allow-Headers", "Content-Type"); responseHeaders.add("Access-Control-Allow-Credentials", "false"); responseHeaders.add("Access-Control-Max-Age", "60"); } @Get("json") public Representation getJson() { Form responseHeaders = (Form) getResponse().getAttributes().get( "org.restlet.http.headers"); if (responseHeaders == null) { responseHeaders = new Form(); getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders); } responseHeaders.add("Access-Control-Allow-Origin", "*"); Form form = getRequest().getResourceRef().getQueryAsForm(); String term = form.getFirstValue("term"); String [] array = getPersonsAsArray(term); try { JSONArray jarray = new JSONArray(array); JsonRepresentation rp = new JsonRepresentation(jarray); //Map<String,String> persons = getPersonsAsMap(term); //JsonRepresentation rp = new JsonRepresentation(new JSONObject(persons)); return rp; } catch (Exception e) { // TODO: handle exception return null; } } @Get("html") public Representation getHTML() { //check json List<MediaType> supported=new ArrayList<MediaType>(); supported.add(MediaType.APPLICATION_JSON); MediaType types = getRequest().getClientInfo().getPreferredMediaType(supported); if ((types!=null) && types.equals(MediaType.APPLICATION_JSON)) return getJson(); Form responseHeaders = (Form) getResponse().getAttributes().get( "org.restlet.http.headers"); if (responseHeaders == null) { responseHeaders = new Form(); getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders); } responseHeaders.add("Access-Control-Allow-Origin", "*"); Form form = getRequest().getResourceRef().getQueryAsForm(); String [] array= null; String term=""; String ret="Search Name:<form method=\"get\" ><input name=\"term\" size=\"30\"/></form>"; 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); } private String[] getPersonsAsArray(String term) { QueryParser parser = new QueryParser(Version.LUCENE_31,"http://xmlns.com/foaf/0.1/name", analyzer); Query query; ScoreDoc[] hits; try { query = parser.parse(term+"*"); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); return new String[]{}; } try { hits=searcher.search(query, null, 5000).scoreDocs; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return new String[]{}; } String[] ret = new String[hits.length]; for (int i = 0; i < hits.length; i++) { Document doc; try { doc = searcher.doc(hits[i].doc); } catch (CorruptIndexException e) { // TODO Auto-generated catch block e.printStackTrace(); continue; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); continue; } ret[i]=doc.get("http://xmlns.com/foaf/0.1/name"); } return ret; } private Map<String,String> getPersonsAsMap(String term) { QueryParser parser = new QueryParser(Version.LUCENE_31,"http://xmlns.com/foaf/0.1/name", analyzer); Query query; ScoreDoc[] hits; Map<String,String> ret = new HashMap<String,String>(); try { query = parser.parse(term+"*"); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); return ret; } try { hits=searcher.search(query, null, 5000).scoreDocs; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return ret; } for (int i = 0; i < hits.length; i++) { Document doc; try { doc = searcher.doc(hits[i].doc); } catch (CorruptIndexException e) { // TODO Auto-generated catch block e.printStackTrace(); continue; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); continue; } ret.put(doc.get("http://xmlns.com/foaf/0.1/name"),doc.get("identifier")); } return ret; } }