annotate src/main/java/de/mpiwg/itgroup/annotationManager/RDFHandling/Convert.java @ 27:b4a798d417fe

on the way to new annotations...
author casties
date Fri, 27 Apr 2012 09:55:39 +0200
parents 235b91ba8dff
children 185db3cac82c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dwinter
parents:
diff changeset
1 package de.mpiwg.itgroup.annotationManager.RDFHandling;
dwinter
parents:
diff changeset
2
dwinter
parents:
diff changeset
3 import java.text.SimpleDateFormat;
dwinter
parents:
diff changeset
4 import java.util.ArrayList;
dwinter
parents:
diff changeset
5 import java.util.Calendar;
dwinter
parents:
diff changeset
6 import java.util.List;
dwinter
parents:
diff changeset
7
dwinter
parents:
diff changeset
8 import org.apache.log4j.Logger;
dwinter
parents:
diff changeset
9 import org.openrdf.repository.RepositoryException;
dwinter
parents:
diff changeset
10
dwinter
parents:
diff changeset
11 import de.mpiwg.itgroup.annotationManager.Constants.NS;
dwinter
parents:
diff changeset
12 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreStoreError;
5
0be9d53a6967 editor for annotations
dwinter
parents: 3
diff changeset
13 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException;
0be9d53a6967 editor for annotations
dwinter
parents: 3
diff changeset
14 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler;
0be9d53a6967 editor for annotations
dwinter
parents: 3
diff changeset
15 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler.LiteralQuadruple;
0be9d53a6967 editor for annotations
dwinter
parents: 3
diff changeset
16 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler.Quadruple;
0
dwinter
parents:
diff changeset
17
dwinter
parents:
diff changeset
18 /**
dwinter
parents:
diff changeset
19 * @author dwinter
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
20 *
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
21 * Klasse zum Konvertieren von Annotationen zum mpiwg RDF-Format: http://ontologies.mpiwg-berlin.mpg.de/annotations/
0
dwinter
parents:
diff changeset
22 */
dwinter
parents:
diff changeset
23
dwinter
parents:
diff changeset
24 public class Convert {
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
25 private String context = "file:///annotations";
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
26 private static Logger logger = Logger.getRootLogger();
25
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
27 private String urlBase = "http://entities.mpiwg-berlin.mpg.de/annotations/"; // TODO should go into config
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
28
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
29 public Convert(String context) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
30 this.context = context;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
31 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
32
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
33 /**
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
34 *
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
35 * @param xpointer
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
36 * Beschreibt die Annotation
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
37 * @param creator
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
38 * Username des Creators
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
39 * @param time
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
40 * Erstellungszeit, wenn null wird das aktuelle Datum verwenden beim Konvertieren
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
41 * @param text
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
42 * der Annotation
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
43 * @param type
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
44 * Annotationstype (Entsprechend den in http://www.w3.org/2000/10/annotationType# definierten.)
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
45 * @return
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
46 */
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
47
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
48 private List<Quadruple> annot2quadruple(String xpointer, String creator, String time, String annot, String type) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
49 return annot2quadruple(new Annotation(xpointer, creator, time, annot, type));
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
50 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
51
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
52 /**
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
53 * @param annot
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
54 * @return
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
55 */
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
56 public List<Quadruple> annot2quadruple(Annotation annot) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
57 List<Quadruple> retQuad = new ArrayList<Quadruple>();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
58
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
59 // create new URL if annot has no annotationUrl
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
60 String annotationUrl = annot.getAnnotationUri();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
61 if (annotationUrl == null) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
62 annotationUrl = createRessourceURL("annot:");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
63 }
26
235b91ba8dff on the way to new annotations...
casties
parents: 25
diff changeset
64
235b91ba8dff on the way to new annotations...
casties
parents: 25
diff changeset
65 // annotation class
235b91ba8dff on the way to new annotations...
casties
parents: 25
diff changeset
66 retQuad.add(new Quadruple(annotationUrl, NS.RDF_NS + "type", NS.OAC_NS + "Annotation", context));
235b91ba8dff on the way to new annotations...
casties
parents: 25
diff changeset
67 // TODO: what types?
27
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
68 // retQuad.add(new LiteralQuadruple(annotationUrl, NS.RDF + "type", annot.type, context));
25
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
69
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
70 // author
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
71 if (annot.creator.startsWith("http")) {
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
72 retQuad.add(new Quadruple(annotationUrl, NS.DCTERMS_NS + "creator", annot.creator, context));
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
73 } else {
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
74 // TODO: this should not happen
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
75 retQuad.add(new LiteralQuadruple(annotationUrl, NS.DCTERMS_NS + "creator", annot.creator, context));
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
76 }
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
77 // creation time
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
78 if (annot.time == null) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
79 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
80 annot.time = format.format(Calendar.getInstance().getTime());
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
81 }
25
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
82 retQuad.add(new LiteralQuadruple(annotationUrl, NS.DCTERMS_NS + "created", annot.time, context));
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
83
26
235b91ba8dff on the way to new annotations...
casties
parents: 25
diff changeset
84 // target (full URL)
235b91ba8dff on the way to new annotations...
casties
parents: 25
diff changeset
85 retQuad.add(new Quadruple(annot.xpointer, NS.RDF_NS + "type", NS.OAC_NS + "Target", context));
235b91ba8dff on the way to new annotations...
casties
parents: 25
diff changeset
86 // is target of annotation
235b91ba8dff on the way to new annotations...
casties
parents: 25
diff changeset
87 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasTarget", annot.xpointer, context));
27
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
88
26
235b91ba8dff on the way to new annotations...
casties
parents: 25
diff changeset
89 // now add the xpointers
27
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
90 /*
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
91 * String[] xpointerSplitted = annot.xpointer.split("#"); if (xpointerSplitted.length > 2) { throw new XPointerError(); }
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
92 * retQuad.add(new Quadruple(annotationUrl, NS.MPIWG_ANNOT_URL + "annotatesDocuviewerText", xpointerSplitted[0], context));
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
93 * retQuad.add(new Quadruple(annotationUrl, NS.MPIWG_ANNOT_URL + "textSelection", annot.xpointer, context));
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
94 */
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
95
26
235b91ba8dff on the way to new annotations...
casties
parents: 25
diff changeset
96 // annotation body
27
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
97 if (annot.url != null && annot.url.startsWith("http://")) {
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
98 // body is resource
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
99 retQuad.add(new Quadruple(annot.url, NS.RDF_NS + "type", NS.OAC_NS + "Body", context));
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
100 // is body of annotation
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
101 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasBody", annot.url, context));
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
102 } else {
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
103 // body is literal text
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
104 String annotationtext = createRessourceURL("annotationBody:");
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
105 retQuad.add(new Quadruple(annotationtext, NS.RDF_NS + "type", NS.OAC_NS + "Body", context));
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
106 retQuad.add(new Quadruple(annotationtext, NS.RDF_NS + "type", NS.CNT_NS + "ContentAsText", context));
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
107 retQuad.add(new LiteralQuadruple(annotationtext, NS.CNT_NS + "chars", annot.text, context));
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
108 // is body of annotation
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
109 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasBody", annotationtext, context));
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
110 }
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
111
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
112 for (Quadruple ret : retQuad) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
113 logger.debug(ret.toString());
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
114 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
115
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
116 annot.setAnnotationUri(annotationUrl);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
117 return retQuad;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
118 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
119
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
120 /**
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
121 * Erzeuge eine urn aus der aktullen Zeit in millis
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
122 *
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
123 * @return
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
124 */
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
125 private String createRessourceURL(String prefix) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
126
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
127 Calendar cal = Calendar.getInstance();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
128
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
129 long time = cal.getTimeInMillis();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
130
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
131 return String.format("%s%s%s", urlBase, prefix, time);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
132
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
133 }
0
dwinter
parents:
diff changeset
134
27
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
135
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
136 /**
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
137 * Stores the Annotation in the TripleStore.
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
138 *
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
139 * @param annot
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
140 * @return
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
141 * @throws TripleStoreStoreError
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
142 */
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
143 public Annotation storeAnnotation(Annotation annot) throws TripleStoreStoreError {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
144 List<Quadruple> annotationRdf = new ArrayList<Quadruple>();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
145 if ((annot.type == null) || annot.type.equals("")) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
146 annot.type = "Comment";
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
147 }
27
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
148 annotationRdf = annot2quadruple(annot);
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
149 try {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
150 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
151 th.write(annotationRdf);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
152 } catch (RepositoryException e) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
153 // TODO Auto-generated catch block
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
154 e.printStackTrace();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
155 throw new TripleStoreStoreError();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
156 } catch (TripleStoreHandlerException e) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
157 // TODO Auto-generated catch block
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
158 e.printStackTrace();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
159 throw new TripleStoreStoreError();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
160 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
161 return annot;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
162 }
0
dwinter
parents:
diff changeset
163
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
164 /**
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
165 * @param args
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
166 */
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
167 public static void main(String[] args) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
168 Convert myConvert = new Convert("http://annotations.rdf");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
169 List<Quadruple> rets = myConvert
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
170 .annot2quadruple(
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
171 "http://mpdl-dev.mpiwg-berlin.mpg.de/ECHOdocuViewfullTest?url=/mpiwg/online/permanent/library/163127KK&amp;viewMode=text&amp;pn=7#xpointer(string-range(id(&quot;s1&quot;), &quot;&quot;, 66, 12))",
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
172 "mbuchman", null, "myannot", "Example");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
173 for (Quadruple ret : rets) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
174 System.out.println(ret.toString());
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
175 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
176 }
0
dwinter
parents:
diff changeset
177
dwinter
parents:
diff changeset
178 }