Mercurial > hg > AnnotationManager
comparison src/de/mpiwg/itgroup/annotationManager/RDFHandling/Convert.java @ 17:b0ef5c860464
updating and deleting annotations works now!
more cleanup.
author | casties |
---|---|
date | Thu, 22 Mar 2012 21:37:16 +0100 |
parents | 9393c9c9b916 |
children |
comparison
equal
deleted
inserted
replaced
16:667d98fd28bd | 17:b0ef5c860464 |
---|---|
20 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler.LiteralQuadruple; | 20 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler.LiteralQuadruple; |
21 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler.Quadruple; | 21 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler.Quadruple; |
22 | 22 |
23 /** | 23 /** |
24 * @author dwinter | 24 * @author dwinter |
25 * | 25 * |
26 * Klasse zum Konvertieren von Annotationen zum mpiwg RDF-Format: | 26 * Klasse zum Konvertieren von Annotationen zum mpiwg RDF-Format: http://ontologies.mpiwg-berlin.mpg.de/annotations/ |
27 * http://ontologies.mpiwg-berlin.mpg.de/annotations/ | |
28 */ | 27 */ |
29 | 28 |
30 public class Convert { | 29 public class Convert { |
31 private String ctx; | 30 private String context = "file:///annotations"; |
32 private Logger logger = Logger.getRootLogger(); | 31 private static Logger logger = Logger.getRootLogger(); |
33 private String urlBase="http://ontologies.mpiwg-berlin.mpg.de/annotations/"; //TODO should go into config | 32 private String urlBase = "http://ontologies.mpiwg-berlin.mpg.de/annotations/"; // TODO should go into config |
34 public Convert(String c){ | 33 |
35 ctx=c; | 34 public Convert(String context) { |
36 } | 35 this.context = context; |
37 | 36 } |
38 /** | 37 |
39 * Fasst alle Parameter zusammen, die eine Annotation bilden | 38 /** |
40 * @author dwinter | 39 * |
41 * | 40 * @param xpointer |
42 */ | 41 * Beschreibt die Annotation |
43 static public class Annotation { | 42 * @param creator |
44 public String xpointer=null; //if queried xpointer should contain the first xpointer in the xpointers list, if there is more than one. | 43 * Username des Creators |
45 public String creator=null; | 44 * @param time |
46 public String time=null; | 45 * Erstellungszeit, wenn null wird das aktuelle Datum verwenden beim Konvertieren |
47 public String text=null; | 46 * @param text |
48 public String type=null; | 47 * der Annotation |
49 public String url; | 48 * @param type |
50 public List<String> xpointers=null; // list of xpointers on the page url, can be empty or null if there is only one. | 49 * Annotationstype (Entsprechend den in http://www.w3.org/2000/10/annotationType# definierten.) |
51 public String annotationUri=null; // uri der annotation im triplestore | 50 * @return |
52 | 51 */ |
53 | 52 |
54 /** | 53 private List<Quadruple> annot2quadruple(String xpointer, String creator, String time, String annot, String type) { |
55 * @param xpointer Beschreibt die Annotation | 54 return annot2quadruple(new Annotation(xpointer, creator, time, annot, type)); |
56 * @param creator Username des Creators oder URI der Creators | 55 } |
57 * @param time Erstellungszeit, wenn null wird das aktuelle Datum verwenden beim Konvertieren | 56 |
58 * @param text der Annotation | 57 /** |
59 * @param type Annotationstype (Entsprechend den in http://www.w3.org/2000/10/annotationType# definierten.) | 58 * @param annot |
60 * @param url Url einer Annotation | 59 * @return |
61 * @param annotationUri Uri der Annotation im Triplestore | 60 */ |
62 */ | 61 public List<Quadruple> annot2quadruple(Annotation annot) { |
63 public Annotation(String xpointer, String creator, String time, String text, String type, String url,String annotationUri){ | 62 List<Quadruple> retQuad = new ArrayList<Quadruple>(); |
64 this.xpointer=xpointer; | 63 |
65 this.creator=creator; | 64 // create new URL if annot has no annotationUrl |
66 this.time=time; | 65 String annotationUrl = annot.getAnnotationUri(); |
67 this.text=text; | 66 if (annotationUrl == null) { |
68 this.type=type; | 67 annotationUrl = createRessourceURL("annot:"); |
69 this.url=url; | 68 } |
70 this.annotationUri=annotationUri; | 69 |
71 } | 70 if (annot.time == null) { |
72 /** | 71 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); |
73 * @param xpointer Beschreibt die Annotation | 72 annot.time = format.format(Calendar.getInstance().getTime()); |
74 * @param creator Username des Creators | 73 } |
75 * @param time Erstellungszeit, wenn null wird das aktuelle Datum verwenden beim Konvertieren | 74 |
76 * @param text der Annotation | 75 // TODO: check type |
77 * @param type Annotationstype (Entsprechend den in http://www.w3.org/2000/10/annotationType# definierten.) | 76 retQuad.add(new Quadruple(annotationUrl, NS.RDF + "type", NS.ANNOTATION_TYPE + annot.type, context)); |
78 * @param url Url einer Annotation | 77 |
79 */ | 78 // add author |
80 public Annotation(String xpointer, String creator, String time, String text, String type, String url){ | 79 |
81 this.xpointer=xpointer; | 80 if (annot.creator.startsWith("http")) { |
82 this.creator=creator; | 81 retQuad.add(new Quadruple(annotationUrl, NS.ANNOTATION_NS + "author", annot.creator, context)); |
83 this.time=time; | 82 } else { |
84 this.text=text; | 83 retQuad.add(new LiteralQuadruple(annotationUrl, NS.ANNOTATION_NS + "author", annot.creator, context)); |
85 this.type=type; | 84 } |
86 this.url=url; | 85 |
87 } | 86 // creation time |
88 | 87 retQuad.add(new LiteralQuadruple(annotationUrl, NS.ANNOTATION_NS + "created", annot.time, context)); |
89 /** | 88 |
90 * @param xpointer Beschreibt die Annotation | 89 String[] xpointerSplitted = annot.xpointer.split("#"); |
91 * @param creator Username des Creators | 90 |
92 * @param time Erstellungszeit, wenn null wird das aktuelle Datum verwenden beim Konvertieren | 91 if (xpointerSplitted.length > 2) { |
93 * @param text der Annotation | 92 throw new XPointerError(); |
94 * @param type Annotationstype (Entsprechend den in http://www.w3.org/2000/10/annotationType# definierten.) | 93 } |
95 */ | 94 |
96 public Annotation(String xpointer, String creator, String time, String annot, String type){ | 95 // now add the xpointers |
97 this.xpointer=xpointer; | 96 retQuad.add(new Quadruple(annotationUrl, NS.MPIWG_annot + "annotatesDocuviewerText", xpointerSplitted[0], context)); |
98 this.creator=creator; | 97 retQuad.add(new Quadruple(annotationUrl, NS.MPIWG_annot + "textSelection", annot.xpointer, context)); |
99 this.time=time; | 98 |
100 this.text=annot; | 99 String annotationtext = createRessourceURL("annotText:"); |
101 this.type=type; | 100 |
102 this.url=null; | 101 retQuad.add(new Quadruple(annotationUrl, NS.ANNOTATION_NS + "body", annotationtext, context)); |
103 } | 102 |
104 | 103 retQuad.add(new Quadruple(annotationtext, NS.RDF + "type", NS.MPIWG_annot + "StandardTextNote", context)); |
105 | 104 |
106 } | 105 retQuad.add(new LiteralQuadruple(annotationtext, NS.MPIWG_annot + "containsText", annot.text, context)); |
107 private String annotUrl=null; | 106 |
108 | 107 for (Quadruple ret : retQuad) { |
109 /** | 108 logger.debug(ret.toString()); |
110 * | 109 } |
111 * @param xpointer Beschreibt die Annotation | 110 |
112 * @param creator Username des Creators | 111 annot.setAnnotationUri(annotationUrl); |
113 * @param time Erstellungszeit, wenn null wird das aktuelle Datum verwenden beim Konvertieren | 112 return retQuad; |
114 * @param text der Annotation | 113 } |
115 * @param type Annotationstype (Entsprechend den in http://www.w3.org/2000/10/annotationType# definierten.) | 114 |
116 * @return | 115 /** |
117 */ | 116 * Erzeuge eine urn aus der aktullen Zeit in millis |
118 | 117 * |
119 private List<Quadruple> annot2quadruple(String xpointer, String creator, String time, String annot, String type){ | 118 * @return |
120 return annot2quadruple(new Annotation(xpointer, creator, time, annot, type)); | 119 */ |
121 } | 120 private String createRessourceURL(String prefix) { |
122 | 121 |
123 | 122 Calendar cal = Calendar.getInstance(); |
124 | 123 |
125 /** | 124 long time = cal.getTimeInMillis(); |
126 * @param annot | 125 |
127 * @return | 126 return String.format("%s%s%s", urlBase, prefix, time); |
128 */ | 127 |
129 public List<Quadruple> annot2quadruple(Annotation annot){ | 128 } |
130 List<Quadruple> retQuad = new ArrayList<Quadruple>(); | 129 |
131 | 130 /** |
132 String annotation = createRessourceURL("annot:"); | 131 * Hier ist die uri der Annotation angegeben. |
133 | 132 * |
134 if (annot.time==null){ | 133 * @param annot |
135 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); | 134 * @return |
136 annot.time=format.format(Calendar.getInstance().getTime()); | 135 */ |
137 | 136 |
138 } | 137 private List<Quadruple> rel2quadruple(Annotation annot) { |
139 | 138 |
140 //TODO: check type | 139 List<Quadruple> retQuad = new ArrayList<Quadruple>(); |
141 retQuad.add(new Quadruple(annotation, NS.RDF+"type", NS.ANNOTATION_TYPE+annot.type, ctx)); | 140 |
142 | 141 String annotation = createRessourceURL("annot:"); |
143 //add author | 142 |
144 | 143 if (annot.time == null) { |
145 | 144 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'"); |
146 if (annot.creator.startsWith("http")){ | 145 annot.time = format.format(Calendar.getInstance().getTime()); |
147 retQuad.add(new Quadruple(annotation, NS.ANNOTATION_NS+"author", annot.creator, ctx)); | 146 |
148 } else { | 147 } |
149 retQuad.add(new LiteralQuadruple(annotation, NS.ANNOTATION_NS+"author", annot.creator, ctx)); | 148 |
150 } | 149 // TODO: check type |
151 | 150 retQuad.add(new Quadruple(annotation, NS.RDF + "type", NS.ANNOTATION_TYPE + annot.type, context)); |
152 // creation time | 151 |
153 retQuad.add(new LiteralQuadruple(annotation, NS.ANNOTATION_NS+"created", annot.time, ctx)); | 152 // add author |
154 | 153 if (annot.creator.startsWith("http")) { |
155 String[] xpointerSplitted = annot.xpointer.split("#"); | 154 retQuad.add(new Quadruple(annotation, NS.ANNOTATION_NS + "author", annot.creator, context)); |
156 | 155 } else { |
157 if (xpointerSplitted.length>2){ | 156 retQuad.add(new LiteralQuadruple(annotation, NS.ANNOTATION_NS + "author", annot.creator, context)); |
158 annotUrl=null; | 157 } |
159 throw new XPointerError(); | 158 |
160 } | 159 // creation time |
161 | 160 retQuad.add(new LiteralQuadruple(annotation, NS.ANNOTATION_NS + "created", annot.time, context)); |
162 // now add the xpointers | 161 |
163 retQuad.add(new Quadruple(annotation, NS.MPIWG_annot+"annotatesDocuviewerText", xpointerSplitted[0], ctx)); | 162 String[] xpointerSplitted = annot.xpointer.split("#"); |
164 retQuad.add(new Quadruple(annotation, NS.MPIWG_annot+"textSelection", annot.xpointer, ctx)); | 163 |
165 | 164 if (xpointerSplitted.length > 2) { |
166 String annotationtext =createRessourceURL("annotText:"); | 165 throw new XPointerError(); |
167 | 166 } |
168 retQuad.add(new Quadruple(annotation, NS.ANNOTATION_NS+"body", annotationtext, ctx)); | 167 |
169 | 168 // now add the xpointers |
170 retQuad.add(new Quadruple(annotationtext, NS.RDF+"type", NS.MPIWG_annot+"StandardTextNote", ctx)); | 169 retQuad.add(new Quadruple(annotation, NS.MPIWG_annot + "annotatesDocuviewerText", xpointerSplitted[0], context)); |
171 | 170 retQuad.add(new Quadruple(annotation, NS.MPIWG_annot + "textSelection", annot.xpointer, context)); |
172 retQuad.add(new LiteralQuadruple(annotationtext, NS.MPIWG_annot+"containsText", annot.text, ctx)); | 171 |
173 | 172 // String annotationtext =createRessourceURL("annotText:"); |
174 for (Quadruple ret:retQuad){ | 173 |
175 logger.debug(ret.toString()); | 174 retQuad.add(new Quadruple(annotation, NS.ANNOTATION_NS + "body", annot.url, context)); |
176 } | 175 |
177 | 176 retQuad.add(new Quadruple(annot.url, NS.RDF + "type", NS.MPIWG_annot + "ExtendedAnnotation", context)); |
178 annotUrl=annotation; | 177 |
179 return retQuad; | 178 for (Quadruple ret : retQuad) { |
180 } | 179 logger.debug(ret.toString()); |
181 | 180 } |
182 /** | 181 |
183 * Erzeuge eine urn aus der aktullen Zeit in millis | 182 // save new annotation url in annotation object |
184 * @return | 183 annot.setAnnotationUri(annotation); |
185 */ | 184 return retQuad; |
186 private String createRessourceURL(String prefix) { | 185 |
187 | 186 } |
188 Calendar cal = Calendar.getInstance(); | 187 |
189 | 188 /** |
190 long time = cal.getTimeInMillis(); | 189 * Stores the Annotation in the TripleStore. |
191 | 190 * |
192 return String.format("%s%s%s", urlBase,prefix,time); | 191 * @param annot |
193 | 192 * @return |
194 | 193 * @throws TripleStoreStoreError |
195 | 194 */ |
196 } | 195 public Annotation storeAnnotation(Annotation annot) throws TripleStoreStoreError { |
197 | 196 List<Quadruple> annotationRdf = new ArrayList<Quadruple>(); |
198 /** | 197 if ((annot.type == null) || annot.type.equals("")) { |
199 * Hier ist die uri der Annotation angegeben. | 198 annot.type = "Comment"; |
200 * @param annot | 199 } |
201 * @return | 200 if (annot.text != null && !annot.text.equals("")) { |
202 */ | 201 annotationRdf = annot2quadruple(annot); |
203 | 202 } |
204 private List<Quadruple> rel2quadruple(Annotation annot) { | 203 if (annot.url != null && !annot.url.equals("")) { |
205 | 204 annotationRdf.addAll(rel2quadruple(annot)); |
206 List<Quadruple> retQuad = new ArrayList<Quadruple>(); | 205 } |
207 | 206 try { |
208 String annotation = createRessourceURL("annot:"); | 207 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler(); |
209 | 208 th.write(annotationRdf); |
210 if (annot.time==null){ | 209 } catch (RepositoryException e) { |
211 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'"); | 210 // TODO Auto-generated catch block |
212 annot.time=format.format(Calendar.getInstance().getTime()); | 211 e.printStackTrace(); |
213 | 212 throw new TripleStoreStoreError(); |
214 } | 213 } catch (TripleStoreHandlerException e) { |
215 | 214 // TODO Auto-generated catch block |
216 //TODO: check type | 215 e.printStackTrace(); |
217 retQuad.add(new Quadruple(annotation, NS.RDF+"type", NS.ANNOTATION_TYPE+annot.type, ctx)); | 216 throw new TripleStoreStoreError(); |
218 | 217 } |
219 //add author | 218 return annot; |
220 | 219 } |
221 | 220 |
222 if (annot.creator.startsWith("http")){ | 221 /** |
223 retQuad.add(new Quadruple(annotation, NS.ANNOTATION_NS+"author", annot.creator, ctx)); | 222 * @param args |
224 } else { | 223 */ |
225 retQuad.add(new LiteralQuadruple(annotation, NS.ANNOTATION_NS+"author", annot.creator, ctx)); | 224 public static void main(String[] args) { |
226 } | 225 Convert myConvert = new Convert("http://annotations.rdf"); |
227 | 226 List<Quadruple> rets = myConvert |
228 // creation time | 227 .annot2quadruple( |
229 retQuad.add(new LiteralQuadruple(annotation, NS.ANNOTATION_NS+"created", annot.time, ctx)); | 228 "http://mpdl-dev.mpiwg-berlin.mpg.de/ECHOdocuViewfullTest?url=/mpiwg/online/permanent/library/163127KK&viewMode=text&pn=7#xpointer(string-range(id("s1"), "", 66, 12))", |
230 | 229 "mbuchman", null, "myannot", "Example"); |
231 String[] xpointerSplitted = annot.xpointer.split("#"); | 230 for (Quadruple ret : rets) { |
232 | 231 System.out.println(ret.toString()); |
233 if (xpointerSplitted.length>2){ | 232 } |
234 annotUrl=null; | 233 } |
235 throw new XPointerError(); | 234 |
236 } | |
237 | |
238 // now add the xpointers | |
239 retQuad.add(new Quadruple(annotation, NS.MPIWG_annot+"annotatesDocuviewerText", xpointerSplitted[0], ctx)); | |
240 retQuad.add(new Quadruple(annotation, NS.MPIWG_annot+"textSelection", annot.xpointer, ctx)); | |
241 | |
242 //String annotationtext =createRessourceURL("annotText:"); | |
243 | |
244 retQuad.add(new Quadruple(annotation, NS.ANNOTATION_NS+"body", annot.url, ctx)); | |
245 | |
246 retQuad.add(new Quadruple(annot.url, NS.RDF+"type", NS.MPIWG_annot+"ExtendedAnnotation", ctx)); | |
247 | |
248 | |
249 for (Quadruple ret:retQuad){ | |
250 logger.debug(ret.toString()); | |
251 } | |
252 | |
253 annotUrl=annotation; | |
254 return retQuad; | |
255 | |
256 } | |
257 /** | |
258 * gibt nach die nach aufruf eines Converters erzeuge aktuelle url der annotation zurueck | |
259 * @return | |
260 */ | |
261 public String getAnnotUrl() { | |
262 | |
263 return annotUrl; | |
264 } | |
265 | |
266 /** | |
267 * @param args | |
268 */ | |
269 public static void main(String[] args) { | |
270 Convert myConvert = new Convert("http://annotations.rdf"); | |
271 List<Quadruple> rets = myConvert.annot2quadruple("http://mpdl-dev.mpiwg-berlin.mpg.de/ECHOdocuViewfullTest?url=/mpiwg/online/permanent/library/163127KK&viewMode=text&pn=7#xpointer(string-range(id("s1"), "", 66, 12))", "mbuchman", null, "myannot", "Example"); | |
272 for (Quadruple ret:rets){ | |
273 System.out.println(ret.toString()); | |
274 } | |
275 | |
276 } | |
277 | |
278 | |
279 public Annotation storeAnnotation(Convert.Annotation annot) throws TripleStoreStoreError { | |
280 | |
281 //Convert convert = new Convert("<file:///annotations>"); | |
282 | |
283 | |
284 if ((annot.type==null) || annot.type.equals("")){ | |
285 annot.type="Comment"; | |
286 } | |
287 | |
288 List<Quadruple> annots = new ArrayList<Quadruple>(); | |
289 if (annot.text!=null && !annot.text.equals("")) | |
290 annots=annot2quadruple(annot); | |
291 if (annot.url!=null && !annot.url.equals("")) | |
292 annots.addAll(rel2quadruple(annot)); | |
293 | |
294 | |
295 | |
296 try { | |
297 ChildContext context = (ChildContext)Context.getCurrent(); | |
298 String user = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreUser"); | |
299 String pw = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreUserPassword"); | |
300 String connectionURL = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreConnectionURL"); | |
301 if (user == null || pw == null || connectionURL == null) { | |
302 logger.error(String.format("Missing triplestore parameters! (user=%s pw=%s url=%s)", user, pw, connectionURL)); | |
303 throw new TripleStoreStoreError(); | |
304 } | |
305 //TripleStoreHandler th = new TripleStoreHandler("jdbc:virtuoso://virtuoso.mpiwg-berlin.mpg.de:1111", user, pw); | |
306 TripleStoreHandler th = new TripleStoreHandler(connectionURL, user, pw); | |
307 | |
308 th.write(annots); | |
309 } catch (RepositoryException e) { | |
310 // TODO Auto-generated catch block | |
311 e.printStackTrace(); | |
312 throw new TripleStoreStoreError(); | |
313 } catch (TripleStoreHandlerException e) { | |
314 // TODO Auto-generated catch block | |
315 e.printStackTrace(); | |
316 throw new TripleStoreStoreError(); | |
317 } | |
318 | |
319 | |
320 annot.annotationUri=getAnnotUrl(); | |
321 return annot; | |
322 } | 235 } |
323 | |
324 | |
325 | |
326 | |
327 } |