0
|
1 package de.mpiwg.itgroup.nimanager.persons;
|
|
2
|
|
3 import java.io.UnsupportedEncodingException;
|
|
4 import java.net.URLDecoder;
|
|
5 import java.sql.SQLException;
|
|
6 import java.util.ArrayList;
|
|
7 import java.util.HashMap;
|
|
8 import java.util.List;
|
|
9 import java.util.Map;
|
|
10
|
|
11 import org.json.JSONArray;
|
|
12 import org.openrdf.model.Statement;
|
|
13 import org.openrdf.repository.RepositoryException;
|
|
14 import org.openrdf.repository.RepositoryResult;
|
|
15 import org.restlet.Context;
|
2
|
16 import org.restlet.data.ClientInfo;
|
0
|
17 import org.restlet.data.Form;
|
|
18 import org.restlet.data.MediaType;
|
|
19 import org.restlet.data.Status;
|
|
20 import org.restlet.engine.component.ChildContext;
|
|
21 import org.restlet.ext.json.JsonRepresentation;
|
|
22 import org.restlet.representation.Representation;
|
|
23 import org.restlet.representation.StringRepresentation;
|
|
24 import org.restlet.resource.Get;
|
|
25 import org.restlet.resource.Options;
|
|
26 import org.restlet.resource.ServerResource;
|
|
27
|
|
28 import virtuoso.jdbc4.VirtuosoResultSet;
|
|
29 import de.mpiwg.itgroup.nimanager.exceptions.TripleStoreHandlerException;
|
|
30 import de.mpiwg.itgroup.nimanager.owl.TripleStoreHandler;
|
|
31
|
|
32 public class PersonByNameService extends ServerResource {
|
|
33
|
|
34 private String VIRTUOSO_PW;
|
|
35 private String VIRTUOSO_USER;
|
|
36 private String virtuoso_server_url;
|
|
37 private String RELATION_FOR_NAME_SEARCH = "http://xmlns.com/foaf/0.1/name";
|
|
38 private String PERSONS_CONTEXT;
|
|
39 private TripleStoreHandler th;
|
|
40
|
|
41 public PersonByNameService() {
|
|
42 ChildContext context = (ChildContext)Context.getCurrent();
|
|
43 virtuoso_server_url = context.getParameters().getFirstValue("de.mpwig.itgroup.personSearch.virtuoso.url");
|
|
44 VIRTUOSO_PW = context.getParameters().getFirstValue("de.mpwig.itgroup.personSearch.virtuoso.pw");
|
|
45 VIRTUOSO_USER = context.getParameters().getFirstValue("de.mpwig.itgroup.personSearch.virtuoso.user");
|
|
46 PERSONS_CONTEXT = context.getParameters().getFirstValue("de.mpwig.itgroup.personSearch.virtuoso.personsGraphURI");
|
|
47 //th = TripleStoreHandler.getInstance("jdbc:virtuoso://virtuoso.mpiwg-berlin.mpg.de:1111", "dba", "dba");
|
|
48 try {
|
|
49 th = new TripleStoreHandler(virtuoso_server_url, VIRTUOSO_USER, VIRTUOSO_PW);
|
|
50 } catch (TripleStoreHandlerException e) {
|
|
51 // TODO Auto-generated catch block
|
|
52 e.printStackTrace();
|
|
53 }
|
|
54 }
|
|
55
|
|
56 @Options
|
|
57 public void doOptions(Representation entity) {
|
|
58 Form responseHeaders = (Form) getResponse().getAttributes().get(
|
|
59 "org.restlet.http.headers");
|
|
60 if (responseHeaders == null) {
|
|
61 responseHeaders = new Form();
|
|
62 getResponse().getAttributes().put("org.restlet.http.headers",
|
|
63 responseHeaders);
|
|
64 }
|
|
65 responseHeaders.add("Access-Control-Allow-Origin", "*");
|
|
66 responseHeaders.add("Access-Control-Allow-Methods", "POST,OPTIONS,GET");
|
|
67 responseHeaders.add("Access-Control-Allow-Headers", "Content-Type");
|
|
68 responseHeaders.add("Access-Control-Allow-Credentials", "false");
|
|
69 responseHeaders.add("Access-Control-Max-Age", "60");
|
|
70 }
|
|
71
|
|
72 public Representation getTest() {
|
|
73 return new StringRepresentation("TEST");
|
|
74 }
|
|
75
|
|
76 @Get("json")
|
|
77 public Representation getJson() {
|
|
78 Form responseHeaders = (Form) getResponse().getAttributes().get(
|
|
79 "org.restlet.http.headers");
|
|
80 if (responseHeaders == null) {
|
|
81 responseHeaders = new Form();
|
|
82 getResponse().getAttributes().put("org.restlet.http.headers",
|
|
83 responseHeaders);
|
|
84 }
|
|
85 responseHeaders.add("Access-Control-Allow-Origin", "*");
|
|
86
|
|
87 JSONArray resultsJsonArray = new JSONArray();
|
|
88 try {
|
|
89 String id = (String) getRequest().getAttributes().get("name");
|
|
90 id = URLDecoder.decode(id, "utf-8");
|
|
91 String queryPersonWPMD="sparql select DISTINCT * "+
|
2
|
92 "FROM <"+PERSONS_CONTEXT+"> "+
|
0
|
93 "FROM <file://newpersonsFromProjects>"+
|
|
94 " FROM <file://personendataWikipedia>"+
|
|
95 " where { " +
|
2
|
96 "{GRAPH <"+PERSONS_CONTEXT+">" +
|
0
|
97 "{"+
|
|
98 "?person <"+RELATION_FOR_NAME_SEARCH+"> \""+id+"\"@en."+
|
|
99 " ?ident <http://ontologies.mpiwg-berlin.mpg.de/authorities/namedIdentities/identifies_NamedEntity> ?person."+
|
|
100 " OPTIONAL {?wikipedia crm:P1_is_identified_by ?ident.}"+
|
|
101 " OPTIONAL {?wikipedia <http://dbpedia.org/ontology/birthDate> ?birthDate.}"+
|
|
102 " OPTIONAL {?wikipedia <http://dbpedia.org/ontology/deathDate> ?deathDate.}"
|
|
103 +"}" +
|
|
104 "} UNION {" +
|
|
105 "GRAPH <file://newpersonsFromProjects> {" +
|
|
106 "?person <"+RELATION_FOR_NAME_SEARCH+"> \""+id+"\""+
|
|
107 "}" +
|
|
108 "}" +
|
|
109 "}";
|
|
110 java.sql.Statement smt = th.sqlCon.createStatement();
|
|
111
|
|
112
|
|
113 smt.execute(queryPersonWPMD);
|
|
114 VirtuosoResultSet rs = (VirtuosoResultSet) smt.getResultSet();
|
|
115 Map<String,String> resultMap = new HashMap<String, String>();
|
|
116 while(rs.next()){
|
|
117 if (rs.getString("person")!=null){
|
|
118 resultMap.put("person",rs.getString("person"));
|
|
119 }else {
|
|
120 resultMap.put("person","");
|
|
121 }
|
|
122 if (rs.getString("wikipedia")!=null){
|
|
123 resultMap.put("wikipedia",rs.getString("wikipedia"));
|
|
124 } else {
|
|
125 resultMap.put("wikipedia","");
|
|
126 }
|
|
127 if (rs.getString("birthDate")!=null){
|
|
128 resultMap.put("birthDate",rs.getString("birthDate"));
|
|
129 } else {
|
|
130 resultMap.put("birthDate","");
|
|
131 }
|
|
132 if (rs.getString("deathDate")!=null){
|
|
133 resultMap.put("deathDate",rs.getString("deathDate"));
|
|
134 } else {
|
|
135 resultMap.put("deathDate","");
|
|
136 }
|
|
137 resultMap.put("name",id);
|
|
138 resultsJsonArray.put(resultMap);
|
|
139 }
|
|
140
|
|
141 return new JsonRepresentation(resultsJsonArray);
|
|
142
|
|
143 } catch (UnsupportedEncodingException e) {
|
|
144 e.printStackTrace();
|
|
145 setStatus(Status.SERVER_ERROR_INTERNAL, "REPOSITORY ERROR");
|
|
146 return new JsonRepresentation(new JSONArray());
|
|
147
|
|
148 } catch (SQLException e) {
|
|
149
|
|
150 e.printStackTrace();
|
|
151 setStatus(Status.SERVER_ERROR_INTERNAL, "REPOSITORY SQL ERROR");
|
|
152 return new JsonRepresentation(new JSONArray());
|
|
153
|
|
154 }
|
|
155
|
|
156
|
|
157 }
|
|
158
|
|
159 @Get("xml")
|
|
160 public Representation getXML() {
|
|
161 List<MediaType> supported=new ArrayList<MediaType>();
|
|
162 supported.add(MediaType.APPLICATION_JSON);
|
2
|
163
|
|
164 ClientInfo ci = getRequest().getClientInfo();
|
0
|
165
|
2
|
166 if(ci==null){ //don't know why this can happen, but sometimes does
|
|
167 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
168 return null;
|
|
169 }
|
0
|
170 MediaType types = getRequest().getClientInfo().getPreferredMediaType(supported);
|
|
171 if (types.equals(MediaType.APPLICATION_JSON))
|
|
172 return getJson();
|
|
173
|
|
174 try {
|
|
175 String id = (String) getRequest().getAttributes().get("name");
|
|
176 id = URLDecoder.decode(id, "utf-8");
|
|
177 RepositoryResult<Statement> statements = th.getStatements(null,
|
|
178 th.createUri(RELATION_FOR_NAME_SEARCH),
|
|
179 th.createLiteral(id, "en"), PERSONS_CONTEXT);
|
|
180 String ret = "<results>";
|
|
181 while (statements.hasNext()) {
|
|
182 Statement statement = statements.next();
|
|
183 ret += "<result subj=\"" + statement.getSubject().stringValue()
|
|
184 + "\"/>";
|
|
185 }
|
|
186 ret += "</results>";
|
|
187
|
|
188 return new StringRepresentation(ret, MediaType.TEXT_XML);
|
|
189
|
|
190 } catch (RepositoryException e) {
|
|
191
|
|
192 e.printStackTrace();
|
|
193 setStatus(Status.SERVER_ERROR_INTERNAL, "REPOSITORY ERROR");
|
|
194 return new StringRepresentation(
|
|
195 "<xml><error>Repository Error</error></xml>",
|
|
196 MediaType.TEXT_XML);
|
|
197 } catch (UnsupportedEncodingException e) {
|
|
198 e.printStackTrace();
|
|
199 setStatus(Status.SERVER_ERROR_INTERNAL, "REPOSITORY ERROR");
|
|
200 return new StringRepresentation(
|
|
201 "<xml><error>Unsupported encoding</error></xml>",
|
|
202 MediaType.TEXT_XML);
|
|
203 }
|
|
204 }
|
|
205
|
|
206 @Get("html")
|
|
207 public Representation getHTML() {
|
|
208
|
|
209 List<MediaType> supported=new ArrayList<MediaType>();
|
|
210 supported.add(MediaType.APPLICATION_JSON);
|
|
211
|
2
|
212 ClientInfo ci = getRequest().getClientInfo();
|
|
213
|
|
214 if(ci==null){ //don't know why this can happen, but sometimes does
|
|
215 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
216 return null;
|
|
217 }
|
|
218 MediaType types = ci.getPreferredMediaType(supported);
|
|
219 if (types!=null)
|
|
220 if (types.equals(MediaType.APPLICATION_JSON))
|
|
221 return getJson();
|
0
|
222
|
|
223 Form responseHeaders = (Form) getResponse().getAttributes().get(
|
|
224 "org.restlet.http.headers");
|
|
225 if (responseHeaders == null) {
|
|
226 responseHeaders = new Form();
|
|
227 getResponse().getAttributes().put("org.restlet.http.headers",
|
|
228 responseHeaders);
|
|
229 }
|
|
230 responseHeaders.add("Access-Control-Allow-Origin", "*");
|
|
231 try {
|
|
232 String id = (String) getRequest().getAttributes().get("name");
|
|
233 id = URLDecoder.decode(id, "utf-8");
|
|
234 // RepositoryResult<Statement> statements = th.getStatements(null,
|
|
235 // th.createUri(RELATION_FOR_NAME_SEARCH),
|
|
236 // th.createLiteral(id, "en"), PERSONS_CONTEXT);
|
|
237 String ret = "<html><body><div id=\"resultNames\">";
|
|
238 String queryPersonWPMD="sparql select * "+
|
2
|
239 "FROM <"+PERSONS_CONTEXT+"> "+
|
0
|
240 " FROM <file://personendataWikipedia>"+
|
|
241 " where { "+
|
|
242 " ?person <"+RELATION_FOR_NAME_SEARCH+"> \""+id+"\"@en."+
|
|
243 " ?ident <http://ontologies.mpiwg-berlin.mpg.de/authorities/namedIdentities/identifies_NamedEntity> ?person."+
|
|
244 " ?wikipedia crm:P1_is_identified_by ?ident."+
|
|
245 " ?wikipedia <http://dbpedia.org/ontology/birthDate> ?birthDate."+
|
|
246 " ?wikipedia <http://dbpedia.org/ontology/deathDate> ?deathDate."
|
|
247 +"}";
|
|
248 java.sql.Statement smt = th.sqlCon.createStatement();
|
|
249
|
|
250
|
|
251 smt.execute(queryPersonWPMD);
|
|
252 VirtuosoResultSet rs = (VirtuosoResultSet) smt.getResultSet();
|
|
253
|
|
254 while(rs.next()){
|
|
255 String person= rs.getString("person");
|
|
256 String wikipedia= rs.getString("wikipedia");
|
|
257 String bd= rs.getString("birthDate");
|
|
258 String dd= rs.getString("deathDate");
|
|
259
|
|
260 ret+=String.format("<div><a href=\"%s\">%s (%s-%s)</a></div>",wikipedia,id,bd,dd);
|
|
261 }
|
|
262
|
|
263
|
|
264 // while (statements.hasNext()) {
|
|
265 // Statement statement = statements.next();
|
|
266 //
|
|
267 //
|
|
268 //
|
|
269 //
|
|
270 //
|
|
271 // //ret +=String.format("<div><a href=\"../id/%s\">%s</a></div>",URLEncoder.encode(statement.getSubject().stringValue()),statement.getSubject().stringValue() );
|
|
272 // ret +=String.format("<div><a href=\"%s\">%s</a></div>",statement.getSubject().stringValue(),statement.getSubject().stringValue() );
|
|
273 //
|
|
274 // }
|
|
275
|
|
276 ret += "</div></body></html>";
|
|
277
|
|
278 return new StringRepresentation(ret, MediaType.TEXT_HTML);
|
|
279
|
|
280 // } catch (RepositoryException e) {
|
|
281 //
|
|
282 // e.printStackTrace();
|
|
283 // setStatus(Status.SERVER_ERROR_INTERNAL, "REPOSITORY ERROR");
|
|
284 // return new StringRepresentation(
|
|
285 // "<xml><error>Repository Error</error></xml>",
|
|
286 // MediaType.TEXT_HTML);
|
|
287 } catch (UnsupportedEncodingException e) {
|
|
288 e.printStackTrace();
|
|
289 setStatus(Status.SERVER_ERROR_INTERNAL, "REPOSITORY ERROR");
|
|
290 return new StringRepresentation(
|
|
291 "<xml><error>Unsupported encoding</error></xml>",
|
|
292 MediaType.TEXT_HTML);
|
|
293 } catch (SQLException e) {
|
|
294
|
|
295 e.printStackTrace();
|
|
296 setStatus(Status.SERVER_ERROR_INTERNAL, "REPOSITORY SQL ERROR");
|
|
297 return new StringRepresentation(
|
|
298 "<xml><error>Unsupported encoding</error></xml>",
|
|
299 MediaType.TEXT_HTML);
|
|
300 }
|
|
301 }
|
|
302
|
|
303 }
|