annotate src/main/java/de/mpiwg/itgroup/annotationManager/RDFHandling/Convert.java @ 33:e5f5848892a2

new annotation model basically works.
author casties
date Thu, 31 May 2012 19:08:48 +0200
parents b37487b756ac
children
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 {
33
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
25 private String context = NS.MPIWG_ANNOT_CTX;
17
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));
30
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
67 /*
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
68 * author
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
69 */
25
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
70 if (annot.creator.startsWith("http")) {
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
71 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
72 } else {
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
73 // TODO: this should not happen
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
74 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
75 }
30
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
76 /*
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
77 * creation time
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
78 */
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
79 if (annot.time == null) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
80 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
81 annot.time = format.format(Calendar.getInstance().getTime());
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
82 }
25
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
83 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
84
29
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
85 if (annot.page == null || annot.page == "") {
30
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
86 /*
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
87 * target without page number (full URL)
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
88 */
29
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
89 retQuad.add(new Quadruple(annot.xpointer, NS.RDF_NS + "type", NS.OAC_NS + "Target", context));
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
90 // is target of annotation
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
91 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasTarget", annot.xpointer, context));
33
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
92 if (annot.xpointer.contains("#")) {
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
93 // isPartOf base resource (without xpointer/fragment)
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
94 String baseUri = annot.xpointer.substring(0, annot.xpointer.indexOf("#"));
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
95 retQuad.add(new Quadruple(annot.xpointer, NS.DCTERMS_NS + "isPartOf", baseUri, context));
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
96 }
29
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
97 } else {
30
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
98 /*
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
99 * ConstrainedTarget with page number
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
100 */
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
101 String ctUrl = createRessourceURL("annotCT:");
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
102 // is ConstrainedTarget
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
103 retQuad.add(new Quadruple(ctUrl, NS.RDF_NS + "type", NS.OAC_NS + "ConstrainedTarget", context));
29
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
104 // is target of annotation
30
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
105 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasTarget", ctUrl, context));
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
106 // constrains Target (full URL)
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
107 retQuad.add(new Quadruple(ctUrl, NS.OAC_NS + "constrains", annot.xpointer, context));
29
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
108 // TextPageConstraint
30
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
109 String tpcUrl = createRessourceURL("annotC:");
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
110 // is TextPageConstraint
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
111 retQuad.add(new Quadruple(tpcUrl, NS.RDF_NS + "type", NS.MPIWG_ANNOT_NS + "TextPageConstraint", context));
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
112 // page number
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
113 retQuad.add(new LiteralQuadruple(tpcUrl, NS.MPIWG_ANNOT_NS + "textPage", annot.page, context));
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
114 // textPageDisplay
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
115 if (annot.displayUrl != null && annot.displayUrl != "") {
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
116 retQuad.add(new LiteralQuadruple(tpcUrl, NS.MPIWG_ANNOT_NS + "textPageDisplay", annot.displayUrl, context));
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
117 }
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
118 // ConstrainedTarget has this Constraint
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
119 retQuad.add(new Quadruple(ctUrl, NS.OAC_NS + "hasConstraint", tpcUrl, context));
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
120 //TODO: displayUrl
29
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
121 }
27
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
122
26
235b91ba8dff on the way to new annotations...
casties
parents: 25
diff changeset
123 // annotation body
27
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
124 if (annot.url != null && annot.url.startsWith("http://")) {
30
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
125 /*
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
126 * body is resource
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
127 */
29
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
128 retQuad.add(new Quadruple(annot.url, NS.RDF_NS + "type", NS.OAC_NS + "Body", context));
27
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
129 // is body of annotation
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
130 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasBody", annot.url, context));
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
131 } else {
30
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
132 /*
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
133 * body is literal text
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
134 */
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
135 String abUrl = createRessourceURL("annotB:");
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
136 // is Body and ContentAsText
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
137 retQuad.add(new Quadruple(abUrl, NS.RDF_NS + "type", NS.OAC_NS + "Body", context));
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
138 retQuad.add(new Quadruple(abUrl, NS.RDF_NS + "type", NS.CNT_NS + "ContentAsText", context));
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
139 // has text
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
140 retQuad.add(new LiteralQuadruple(abUrl, NS.CNT_NS + "chars", annot.text, context));
27
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
141 // is body of annotation
30
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
142 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasBody", abUrl, context));
27
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
143 }
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
144
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
145 for (Quadruple ret : retQuad) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
146 logger.debug(ret.toString());
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
147 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
148
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
149 annot.setAnnotationUri(annotationUrl);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
150 return retQuad;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
151 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
152
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
153 /**
30
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
154 * Erzeuge eine urn aus der aktuellen Zeit in millis
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
155 *
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
156 * @return
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
157 */
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
158 private String createRessourceURL(String prefix) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
159
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
160 Calendar cal = Calendar.getInstance();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
161
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
162 long time = cal.getTimeInMillis();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
163
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
164 return String.format("%s%s%s", urlBase, prefix, time);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
165
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
166 }
0
dwinter
parents:
diff changeset
167
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
168 /**
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
169 * Stores the Annotation in the TripleStore.
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
170 *
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
171 * @param annot
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
172 * @return
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
173 * @throws TripleStoreStoreError
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
174 */
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
175 public Annotation storeAnnotation(Annotation annot) throws TripleStoreStoreError {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
176 List<Quadruple> annotationRdf = new ArrayList<Quadruple>();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
177 if ((annot.type == null) || annot.type.equals("")) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
178 annot.type = "Comment";
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
179 }
27
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
180 annotationRdf = annot2quadruple(annot);
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
181 try {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
182 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
183 th.write(annotationRdf);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
184 } catch (RepositoryException e) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
185 // TODO Auto-generated catch block
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
186 e.printStackTrace();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
187 throw new TripleStoreStoreError();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
188 } catch (TripleStoreHandlerException e) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
189 // TODO Auto-generated catch block
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
190 e.printStackTrace();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
191 throw new TripleStoreStoreError();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
192 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
193 return annot;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
194 }
0
dwinter
parents:
diff changeset
195
dwinter
parents:
diff changeset
196 }