Mercurial > hg > MetaDataManagerRestlet
comparison src/de/mpiwg/itgroup/nimanager/edit/Editor.java @ 4:aae47a713589
editing
author | dwinter |
---|---|
date | Mon, 02 Jan 2012 12:42:28 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3:dfd72ea936ff | 4:aae47a713589 |
---|---|
1 package de.mpiwg.itgroup.nimanager.edit; | |
2 | |
3 import java.io.UnsupportedEncodingException; | |
4 import java.net.URLDecoder; | |
5 import java.util.ArrayList; | |
6 import java.util.Collection; | |
7 import java.util.HashMap; | |
8 import java.util.Iterator; | |
9 import java.util.List; | |
10 import java.util.Map; | |
11 import java.util.Set; | |
12 | |
13 import org.apache.commons.lang.StringUtils; | |
14 import org.apache.log4j.Level; | |
15 import org.apache.log4j.Logger; | |
16 import org.apache.log4j.lf5.util.StreamUtils; | |
17 import org.openrdf.model.Statement; | |
18 import org.openrdf.repository.RepositoryException; | |
19 import org.openrdf.repository.RepositoryResult; | |
20 import org.restlet.Context; | |
21 import org.restlet.data.ClientInfo; | |
22 import org.restlet.data.Form; | |
23 import org.restlet.data.MediaType; | |
24 import org.restlet.data.Status; | |
25 import org.restlet.engine.component.ChildContext; | |
26 import org.restlet.representation.Representation; | |
27 import org.restlet.representation.StringRepresentation; | |
28 import org.restlet.resource.Get; | |
29 import org.restlet.resource.Post; | |
30 import org.restlet.resource.ServerResource; | |
31 import org.restlet.security.User; | |
32 | |
33 import de.mpiwg.itgroup.metaDataManagerRestlet.RestServer; | |
34 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException; | |
35 import de.mpiwg.itgroup.triplestoremanager.owl.MetaDataHandler; | |
36 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler; | |
37 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler.LiteralQuadruple; | |
38 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler.Quadruple; | |
39 import edu.stanford.smi.protegex.owl.model.impl.DefaultRDFProperty; | |
40 import edu.stanford.smi.protege.model.DefaultCls; | |
41 /** | |
42 * @author dwinter | |
43 * | |
44 * Edit an Entity defined by OWL modell | |
45 */ | |
46 public class Editor extends ServerResource { | |
47 | |
48 private String NAMED_ENTITIES_ONTOLOGIE_URL = "http://ontologies.mpiwg-berlin.mpg.de/authorities/namedIdentities"; | |
49 private String VIRTUOSO_PW; | |
50 private String VIRTUOSO_USER; | |
51 private String virtuoso_server_url; | |
52 private String TRIPLE_INDEX_PATH; | |
53 private Logger rl = Logger.getRootLogger(); | |
54 private MetaDataHandler mh; | |
55 private TripleStoreHandler th; | |
56 private String editGraph="file:///personEdits.rdf"; | |
57 | |
58 | |
59 public Editor (){ | |
60 ChildContext context = (ChildContext)Context.getCurrent(); | |
61 virtuoso_server_url = context.getParameters().getFirstValue("de.mpwig.itgroup.personSearch.virtuoso.url"); | |
62 VIRTUOSO_PW = context.getParameters().getFirstValue("de.mpwig.itgroup.personSearch.virtuoso.pw"); | |
63 VIRTUOSO_USER = context.getParameters().getFirstValue("de.mpwig.itgroup.personSearch.virtuoso.user"); | |
64 rl.setLevel(Level.DEBUG); | |
65 try { | |
66 th = TripleStoreHandler.getInstance(virtuoso_server_url, VIRTUOSO_USER, VIRTUOSO_PW); | |
67 | |
68 //th = new TripleStoreHandler(virtuoso_server_url, VIRTUOSO_USER, VIRTUOSO_PW); | |
69 } catch (TripleStoreHandlerException e) { | |
70 // TODO Auto-generated catch block | |
71 e.printStackTrace(); | |
72 } | |
73 | |
74 mh = MetaDataHandler.getInstance(); | |
75 } | |
76 | |
77 public Editor(String serverUrl,String User, String PW){ | |
78 virtuoso_server_url = serverUrl; | |
79 VIRTUOSO_PW = PW; | |
80 VIRTUOSO_USER = User; | |
81 | |
82 try { | |
83 th = TripleStoreHandler.getInstance(virtuoso_server_url, VIRTUOSO_USER, VIRTUOSO_PW); | |
84 //th = new TripleStoreHandler(virtuoso_server_url, VIRTUOSO_USER, VIRTUOSO_PW); | |
85 } catch (TripleStoreHandlerException e) { | |
86 // TODO Auto-generated catch block | |
87 e.printStackTrace(); | |
88 } | |
89 | |
90 mh = MetaDataHandler.getInstance(); | |
91 } | |
92 /** | |
93 * Hole alle Properties eines Instance wie definiert in OWL. | |
94 * @param instance | |
95 * @return | |
96 */ | |
97 private HashMap<String, Object> getProperties(String instance, List<String> NSFilter){ | |
98 RepositoryResult<Statement> stmts; | |
99 HashMap<String, Object> retMap = new HashMap<String, Object>(); | |
100 try { | |
101 stmts= th.getStatements(instance, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", null); | |
102 } catch (RepositoryException e) { | |
103 // TODO Auto-generated catch block | |
104 e.printStackTrace(); | |
105 return null; | |
106 } | |
107 try { | |
108 while(stmts.hasNext()){ | |
109 Statement stmt = stmts.next(); | |
110 addPropsOfType(retMap,stmt.getObject().stringValue(),instance, NSFilter); | |
111 } | |
112 } catch (RepositoryException e) { | |
113 // TODO Auto-generated catch block | |
114 e.printStackTrace(); | |
115 return null; | |
116 } | |
117 | |
118 | |
119 return retMap; | |
120 } | |
121 private void addPropsOfType(HashMap<String, Object> retMap, | |
122 String cls, String instance, List<String> NSfilter) { | |
123 | |
124 // Set props = mh.getAssociatedPropsForClass(cls); | |
125 // Iterator propsIt = props.iterator(); | |
126 // while (propsIt.hasNext()){ | |
127 // Object prop = propsIt.next(); | |
128 // | |
129 // | |
130 // } | |
131 Collection props = mh.getAssociatedPropsForClass(cls); | |
132 | |
133 // filter namespaces | |
134 if (NSfilter!=null){ | |
135 | |
136 Object[] propsCopy = props.toArray(); | |
137 for (Object prop:propsCopy){ | |
138 if (DefaultRDFProperty.class.isInstance(prop)){ | |
139 DefaultRDFProperty rdfProp = (DefaultRDFProperty)prop; | |
140 rl.debug(rdfProp.getNamespace()); | |
141 if (!NSfilter.contains(rdfProp.getNamespace())){ // nicht im filter | |
142 props.remove(prop); | |
143 } | |
144 } | |
145 } | |
146 } | |
147 | |
148 Map<String, Object> newProps; | |
149 try { | |
150 newProps = th.getJenaRDFValues(props, instance, mh.getOwlModel(),true); | |
151 } catch (RepositoryException e) { | |
152 // TODO Auto-generated catch block | |
153 e.printStackTrace(); | |
154 return; | |
155 } | |
156 | |
157 retMap.putAll(newProps); | |
158 | |
159 | |
160 } | |
161 | |
162 @Post | |
163 public Representation postHTML(Representation entity){ | |
164 User user = handleBasicAuthentification(entity); | |
165 if (user==null){ | |
166 setStatus(Status.CLIENT_ERROR_UNAUTHORIZED); | |
167 return null; | |
168 } | |
169 | |
170 Form form = new Form(entity); | |
171 | |
172 String id = form.getFirstValue("id"); | |
173 if (id==null){ | |
174 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); | |
175 return new StringRepresentation("Parameter ID missing"); | |
176 } | |
177 | |
178 String type = form.getFirstValue("type"); | |
179 if (type==null){ | |
180 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); | |
181 return new StringRepresentation("Parameter type missing"); | |
182 } | |
183 | |
184 | |
185 ArrayList<Quadruple> quads = new ArrayList<Quadruple>(); | |
186 ArrayList<Quadruple> deleteQuads = new ArrayList<Quadruple>(); | |
187 Map<String,String> formMap = form.getValuesMap(); | |
188 for (String key: formMap.keySet()){ | |
189 if (isDataTypeProperty(key,type)){ | |
190 String value=formMap.get(key); | |
191 | |
192 //remove white space | |
193 value=StringUtils.stripEnd(value, null); | |
194 value=StringUtils.stripStart(value, null); | |
195 | |
196 if (value!=null && !value.equals("")){ //not empty | |
197 deleteQuads.add(new LiteralQuadruple(id, key, null, editGraph)); | |
198 quads.add(new LiteralQuadruple(id, key, formMap.get(key), editGraph)); | |
199 } | |
200 } | |
201 } | |
202 try { | |
203 th.remove(deleteQuads); | |
204 th.write(quads); | |
205 } catch (RepositoryException e) { | |
206 setStatus(Status.SERVER_ERROR_INTERNAL); | |
207 e.printStackTrace(); | |
208 return null; | |
209 } catch (TripleStoreHandlerException e) { | |
210 setStatus(Status.SERVER_ERROR_INTERNAL); | |
211 e.printStackTrace(); | |
212 return null; | |
213 } | |
214 | |
215 return new StringRepresentation("written"); | |
216 | |
217 } | |
218 | |
219 /** | |
220 * Checks if the | |
221 * @param key | |
222 * @param value | |
223 * @return | |
224 */ | |
225 private boolean isDataTypeProperty(String key,String type) { | |
226 | |
227 | |
228 Collection props = mh.getAssociatedPropsForClass(type); | |
229 | |
230 | |
231 Object[] propsCopy = props.toArray(); | |
232 for (Object prop:propsCopy){ | |
233 if (DefaultRDFProperty.class.isInstance(prop)){ | |
234 DefaultRDFProperty rdfProp = (DefaultRDFProperty)prop; | |
235 rl.debug(rdfProp.getNamespace()); | |
236 if (rdfProp.getName().equals(key)){ | |
237 rl.debug(rdfProp); | |
238 return true; | |
239 } | |
240 } | |
241 } | |
242 return false; | |
243 } | |
244 | |
245 | |
246 @Get("html") | |
247 public Representation getHTML(Representation entity){ | |
248 Form form = getRequest().getResourceRef().getQueryAsForm(); | |
249 | |
250 | |
251 String id =form.getValuesMap().get("id"); | |
252 try { | |
253 id = URLDecoder.decode(id, "utf-8"); | |
254 } catch (UnsupportedEncodingException e) { | |
255 e.printStackTrace(); | |
256 e.printStackTrace(); | |
257 setStatus(Status.SERVER_ERROR_INTERNAL, "REPOSITORY ERROR"); | |
258 return new StringRepresentation( | |
259 "<xml><error>Unsupported encoding</error></xml>", | |
260 MediaType.TEXT_HTML); | |
261 | |
262 } | |
263 id=StringUtils.stripEnd(id, null); | |
264 id=StringUtils.stripStart(id, null); | |
265 | |
266 ArrayList<String> NSFilter = new ArrayList<String>(); | |
267 NSFilter.add("http://xmlns.com/foaf/0.1/"); // only foaf | |
268 HashMap<String, Object> props = getProperties(id, NSFilter); | |
269 | |
270 | |
271 ArrayList<String> NSFilterRDF = new ArrayList<String>(); | |
272 NSFilterRDF.add("http://www.w3.org/1999/02/22-rdf-syntax-ns#"); // only foaf | |
273 HashMap<String, Object> propsRDF = getProperties(id, NSFilterRDF); | |
274 | |
275 String RDFtype = ((DefaultCls)(propsRDF.get("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"))).getName(); | |
276 | |
277 | |
278 String retString="<html><body><form method=\"post\" class=\"editEntity\">"; | |
279 retString+=String.format("<input type=\"hidden\" name=\"id\" value=\"%s\" len=\"50\">", id); | |
280 retString+=String.format("<input type=\"hidden\" name=\"type\" value=\"%s\" len=\"50\">", RDFtype); | |
281 for (String prop: props.keySet()){ | |
282 | |
283 retString+=String.format("<div class=\"labelEntity\">%s<input type=\"text\" name=\"%s\" value=\"%s\" len=\"50\"></div>", | |
284 prop,prop,String.valueOf(props.get(prop))); | |
285 } | |
286 | |
287 retString+="<input type=\"submit\"/>"; | |
288 retString+="</form></body></html>"; | |
289 return new StringRepresentation(retString,MediaType.TEXT_HTML); | |
290 | |
291 } | |
292 | |
293 | |
294 private User handleBasicAuthentification(Representation entity) { | |
295 RestServer restServer = (RestServer) getApplication(); | |
296 if (!restServer.authenticate(getRequest(), getResponse())) { | |
297 // Not authenticated | |
298 return null; | |
299 } | |
300 | |
301 ClientInfo ci = getRequest().getClientInfo(); | |
302 rl.debug(ci); | |
303 return getRequest().getClientInfo().getUser(); | |
304 | |
305 } | |
306 | |
307 public static void main(String args[]) { | |
308 Logger.getRootLogger().setLevel(Level.DEBUG); | |
309 Editor ed = new Editor("jdbc:virtuoso://virtuoso.mpiwg-berlin.mpg.de:1111","dba","wa55er"); | |
310 ArrayList<String> NSFilter = new ArrayList<String>(); | |
311 NSFilter.add("http://xmlns.com/foaf/0.1/"); // only foaf | |
312 HashMap<String, Object> props = ed.getProperties("http://ontologies.mpiwg-berlin.mpg.de/tempObjects/person/370868",NSFilter); | |
313 System.out.println(props); | |
314 | |
315 } | |
316 } |