0
|
1 package de.mpiwg.itgroup.annotationManager.RDFHandling;
|
|
2
|
|
3 import java.text.DateFormat;
|
|
4 import java.text.SimpleDateFormat;
|
|
5 import java.util.ArrayList;
|
|
6 import java.util.Calendar;
|
|
7 import java.util.Date;
|
|
8 import java.util.List;
|
|
9
|
|
10 import org.apache.log4j.Logger;
|
|
11 import org.openrdf.repository.RepositoryException;
|
|
12 import org.restlet.Context;
|
|
13 import org.restlet.engine.component.ChildContext;
|
|
14
|
|
15 import de.mpiwg.itgroup.annotationManager.Constants.NS;
|
|
16 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreStoreError;
|
|
17 import de.mpiwg.itgroup.annotationManager.Errors.XPointerError;
|
|
18 import de.mpiwg.itgroup.nimanager.exceptions.TripleStoreHandlerException;
|
|
19 import de.mpiwg.itgroup.nimanager.owl.TripleStoreHandler;
|
|
20 import de.mpiwg.itgroup.nimanager.owl.TripleStoreHandler.Quadruple;
|
|
21 import de.mpiwg.itgroup.nimanager.owl.TripleStoreHandler.LiteralQuadruple;
|
|
22
|
|
23 /**
|
|
24 * @author dwinter
|
|
25 *
|
|
26 * Klasse zum Konvertieren von Annotationen zum mpiwg RDF-Format:
|
|
27 * http://ontologies.mpiwg-berlin.mpg.de/annotations/
|
|
28 */
|
|
29
|
|
30 public class Convert {
|
|
31 private String ctx;
|
|
32 private Logger logger = Logger.getRootLogger();
|
|
33 private String urlBase="http://ontologies.mpiwg-berlin.mpg.de/annotations/"; //TODO should go into config
|
|
34 public Convert(String c){
|
|
35 ctx=c;
|
|
36 }
|
|
37
|
|
38 /**
|
|
39 * Fasst alle Parameter zusammen, die eine Annotation bilden
|
|
40 * @author dwinter
|
|
41 *
|
|
42 */
|
|
43 static public class Annotation {
|
|
44 public String xpointer=null; //if queried xpointer should contain the first xpointer in the xpointers list, if there is more than one.
|
|
45 public String creator=null;
|
|
46 public String time=null;
|
|
47 public String text=null;
|
|
48 public String type=null;
|
|
49 public String url;
|
|
50 public List<String> xpointers=null; // list of xpointers on the page url, can be empty or null if there is only one.
|
2
|
51 public String annotationUri=null; // uri der annotation im triplestore
|
0
|
52
|
2
|
53
|
|
54 /**
|
|
55 * @param xpointer Beschreibt die Annotation
|
|
56 * @param creator Username des Creators oder URI der Creators
|
|
57 * @param time Erstellungszeit, wenn null wird das aktuelle Datum verwenden beim Konvertieren
|
|
58 * @param text der Annotation
|
|
59 * @param type Annotationstype (Entsprechend den in http://www.w3.org/2000/10/annotationType# definierten.)
|
|
60 * @param url Url einer Annotation
|
|
61 * @param annotationUri Uri der Annotation im Triplestore
|
|
62 */
|
|
63 public Annotation(String xpointer, String creator, String time, String text, String type, String url,String annotationUri){
|
|
64 this.xpointer=xpointer;
|
|
65 this.creator=creator;
|
|
66 this.time=time;
|
|
67 this.text=text;
|
|
68 this.type=type;
|
|
69 this.url=url;
|
|
70 this.annotationUri=annotationUri;
|
|
71 }
|
0
|
72 /**
|
|
73 * @param xpointer Beschreibt die Annotation
|
|
74 * @param creator Username des Creators
|
|
75 * @param time Erstellungszeit, wenn null wird das aktuelle Datum verwenden beim Konvertieren
|
|
76 * @param text der Annotation
|
|
77 * @param type Annotationstype (Entsprechend den in http://www.w3.org/2000/10/annotationType# definierten.)
|
|
78 * @param url Url einer Annotation
|
|
79 */
|
|
80 public Annotation(String xpointer, String creator, String time, String text, String type, String url){
|
|
81 this.xpointer=xpointer;
|
|
82 this.creator=creator;
|
|
83 this.time=time;
|
|
84 this.text=text;
|
|
85 this.type=type;
|
|
86 this.url=url;
|
|
87 }
|
|
88
|
|
89 /**
|
|
90 * @param xpointer Beschreibt die Annotation
|
|
91 * @param creator Username des Creators
|
|
92 * @param time Erstellungszeit, wenn null wird das aktuelle Datum verwenden beim Konvertieren
|
|
93 * @param text der Annotation
|
|
94 * @param type Annotationstype (Entsprechend den in http://www.w3.org/2000/10/annotationType# definierten.)
|
|
95 */
|
|
96 public Annotation(String xpointer, String creator, String time, String annot, String type){
|
|
97 this.xpointer=xpointer;
|
|
98 this.creator=creator;
|
|
99 this.time=time;
|
|
100 this.text=annot;
|
|
101 this.type=type;
|
|
102 this.url=null;
|
|
103 }
|
|
104
|
|
105
|
|
106 }
|
|
107 private String annotUrl=null;
|
|
108
|
|
109 /**
|
|
110 *
|
|
111 * @param xpointer Beschreibt die Annotation
|
|
112 * @param creator Username des Creators
|
|
113 * @param time Erstellungszeit, wenn null wird das aktuelle Datum verwenden beim Konvertieren
|
|
114 * @param text der Annotation
|
|
115 * @param type Annotationstype (Entsprechend den in http://www.w3.org/2000/10/annotationType# definierten.)
|
|
116 * @return
|
|
117 */
|
|
118
|
|
119 private List<Quadruple> annot2quadruple(String xpointer, String creator, String time, String annot, String type){
|
|
120 return annot2quadruple(new Annotation(xpointer, creator, time, annot, type));
|
|
121 }
|
|
122
|
|
123
|
|
124 /**
|
|
125 * @param annot
|
|
126 * @return
|
|
127 */
|
|
128 public List<Quadruple> annot2quadruple(Annotation annot){
|
|
129 List<Quadruple> retQuad = new ArrayList<Quadruple>();
|
|
130
|
|
131 String annotation = createRessourceURL("annot:");
|
|
132
|
|
133 if (annot.time==null){
|
|
134 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
|
|
135 annot.time=format.format(Calendar.getInstance().getTime());
|
|
136
|
|
137 }
|
|
138
|
|
139 //TODO: check type
|
|
140 retQuad.add(new Quadruple(annotation, NS.RDF+"type", NS.ANNOTATION_TYPE+annot.type, ctx));
|
|
141
|
|
142 //add author
|
2
|
143
|
|
144
|
|
145 if (annot.creator.startsWith("http")){
|
|
146 retQuad.add(new Quadruple(annotation, NS.ANNOTATION_NS+"author", annot.creator, ctx));
|
|
147 } else {
|
|
148 retQuad.add(new LiteralQuadruple(annotation, NS.ANNOTATION_NS+"author", annot.creator, ctx));
|
|
149 }
|
0
|
150
|
|
151 // creation time
|
|
152 retQuad.add(new LiteralQuadruple(annotation, NS.ANNOTATION_NS+"created", annot.time, ctx));
|
|
153
|
|
154 String[] xpointerSplitted = annot.xpointer.split("#");
|
|
155
|
|
156 if (xpointerSplitted.length>2){
|
|
157 annotUrl=null;
|
|
158 throw new XPointerError();
|
|
159 }
|
|
160
|
|
161 // now add the xpointers
|
2
|
162 retQuad.add(new Quadruple(annotation, NS.MPIWG_annot+"annotatesDocuviewerText", xpointerSplitted[0], ctx));
|
0
|
163 retQuad.add(new Quadruple(annotation, NS.MPIWG_annot+"textSelection", annot.xpointer, ctx));
|
|
164
|
|
165 String annotationtext =createRessourceURL("annotText:");
|
|
166
|
|
167 retQuad.add(new Quadruple(annotation, NS.ANNOTATION_NS+"body", annotationtext, ctx));
|
|
168
|
|
169 retQuad.add(new Quadruple(annotationtext, NS.RDF+"type", NS.MPIWG_annot+"StandardTextNote", ctx));
|
|
170
|
|
171 retQuad.add(new LiteralQuadruple(annotationtext, NS.MPIWG_annot+"containsText", annot.text, ctx));
|
|
172
|
|
173 for (Quadruple ret:retQuad){
|
|
174 logger.debug(ret.toString());
|
|
175 }
|
|
176
|
|
177 annotUrl=annotation;
|
|
178 return retQuad;
|
|
179 }
|
|
180
|
|
181 /**
|
|
182 * Erzeuge eine urn aus der aktullen Zeit in millis
|
|
183 * @return
|
|
184 */
|
|
185 private String createRessourceURL(String prefix) {
|
|
186
|
|
187 Calendar cal = Calendar.getInstance();
|
|
188
|
|
189 long time = cal.getTimeInMillis();
|
|
190
|
|
191 return String.format("%s%s%s", urlBase,prefix,time);
|
|
192
|
|
193
|
|
194
|
|
195 }
|
|
196
|
|
197 /**
|
|
198 * gibt nach die nach aufruf eines Converters erzeuge aktuelle url der annotation zurueck
|
|
199 * @return
|
|
200 */
|
|
201 public String getAnnotUrl() {
|
|
202
|
|
203 return annotUrl;
|
|
204 }
|
|
205
|
|
206 /**
|
|
207 * @param args
|
|
208 */
|
|
209 public static void main(String[] args) {
|
|
210 Convert myConvert = new Convert("http://annotations.rdf");
|
|
211 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");
|
|
212 for (Quadruple ret:rets){
|
|
213 System.out.println(ret.toString());
|
|
214 }
|
|
215
|
|
216 }
|
|
217
|
|
218
|
|
219 public String storeAnnotation(Convert.Annotation annot) throws TripleStoreStoreError {
|
|
220
|
|
221 //Convert convert = new Convert("<file:///annotations>");
|
|
222
|
|
223
|
|
224 if ((annot.type==null) || annot.type.equals("")){
|
|
225 annot.type="Comment";
|
|
226 }
|
|
227
|
|
228 List<Quadruple> annots = new ArrayList<Quadruple>();
|
|
229 if (annot.text!=null && !annot.text.equals(""))
|
|
230 annots=annot2quadruple(annot);
|
|
231 if (annot.url!=null && !annot.url.equals(""))
|
|
232 annots.addAll(rel2quadruple(annot));
|
|
233
|
|
234
|
|
235
|
|
236 try {
|
|
237 ChildContext context = (ChildContext)Context.getCurrent();
|
|
238 String user = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreUser");
|
|
239 String pw = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreUserPassword");
|
|
240
|
|
241 TripleStoreHandler th = new TripleStoreHandler("jdbc:virtuoso://virtuoso.mpiwg-berlin.mpg.de:1111", user, pw);
|
|
242
|
|
243 th.write(annots);
|
|
244 } catch (RepositoryException e) {
|
|
245 // TODO Auto-generated catch block
|
|
246 e.printStackTrace();
|
|
247 throw new TripleStoreStoreError();
|
|
248 } catch (TripleStoreHandlerException e) {
|
|
249 // TODO Auto-generated catch block
|
|
250 e.printStackTrace();
|
|
251 throw new TripleStoreStoreError();
|
|
252 }
|
|
253
|
|
254
|
|
255
|
|
256 return getAnnotUrl();
|
|
257 }
|
|
258
|
|
259
|
|
260 private List<Quadruple> rel2quadruple(Annotation annot) {
|
|
261 // TODO Auto-generated method stub
|
|
262 return new ArrayList<TripleStoreHandler.Quadruple>();
|
|
263 }
|
|
264
|
|
265 }
|