annotate src/main/java/de/mpiwg/itgroup/annotationManager/RDFHandling/Convert.java @ 32:40846c0b344d

working on image annotations...
author casties
date Thu, 31 May 2012 11:57:50 +0200
parents b37487b756ac
children e5f5848892a2
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));
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));
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
92 } else {
30
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
93 /*
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
94 * ConstrainedTarget with page number
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
95 */
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
96 String ctUrl = createRessourceURL("annotCT:");
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
97 // is ConstrainedTarget
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
98 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
99 // is target of annotation
30
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
100 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasTarget", ctUrl, context));
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
101 // constrains Target (full URL)
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
102 retQuad.add(new Quadruple(ctUrl, NS.OAC_NS + "constrains", annot.xpointer, context));
29
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
103 // TextPageConstraint
30
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
104 String tpcUrl = createRessourceURL("annotC:");
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
105 // is TextPageConstraint
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
106 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
107 // page number
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
108 retQuad.add(new LiteralQuadruple(tpcUrl, NS.MPIWG_ANNOT_NS + "textPage", annot.page, context));
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
109 // textPageDisplay
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
110 if (annot.displayUrl != null && annot.displayUrl != "") {
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
111 retQuad.add(new LiteralQuadruple(tpcUrl, NS.MPIWG_ANNOT_NS + "textPageDisplay", annot.displayUrl, context));
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
112 }
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
113 // ConstrainedTarget has this Constraint
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
114 retQuad.add(new Quadruple(ctUrl, NS.OAC_NS + "hasConstraint", tpcUrl, context));
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
115 //TODO: displayUrl
29
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
116 }
27
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
117
26
235b91ba8dff on the way to new annotations...
casties
parents: 25
diff changeset
118 // annotation body
27
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
119 if (annot.url != null && annot.url.startsWith("http://")) {
30
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
120 /*
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
121 * body is resource
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
122 */
29
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
123 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
124 // is body of annotation
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
125 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasBody", annot.url, context));
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
126 } else {
30
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
127 /*
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
128 * body is literal text
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
129 */
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
130 String abUrl = createRessourceURL("annotB:");
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
131 // is Body and ContentAsText
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
132 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
133 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
134 // has text
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
135 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
136 // is body of annotation
30
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
137 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasBody", abUrl, context));
27
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
138 }
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
139
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
140 for (Quadruple ret : retQuad) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
141 logger.debug(ret.toString());
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
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
144 annot.setAnnotationUri(annotationUrl);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
145 return retQuad;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
146 }
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 /**
30
b37487b756ac still working on new annotations...
casties
parents: 29
diff changeset
149 * Erzeuge eine urn aus der aktuellen Zeit in millis
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
150 *
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
151 * @return
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 private String createRessourceURL(String prefix) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
154
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
155 Calendar cal = Calendar.getInstance();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
156
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
157 long time = cal.getTimeInMillis();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
158
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
159 return String.format("%s%s%s", urlBase, prefix, time);
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 }
0
dwinter
parents:
diff changeset
162
17
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 * Stores the Annotation in the TripleStore.
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 * @param annot
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
167 * @return
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
168 * @throws TripleStoreStoreError
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
169 */
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
170 public Annotation storeAnnotation(Annotation annot) throws TripleStoreStoreError {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
171 List<Quadruple> annotationRdf = new ArrayList<Quadruple>();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
172 if ((annot.type == null) || annot.type.equals("")) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
173 annot.type = "Comment";
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
174 }
27
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
175 annotationRdf = annot2quadruple(annot);
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
176 try {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
177 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
178 th.write(annotationRdf);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
179 } catch (RepositoryException e) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
180 // TODO Auto-generated catch block
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
181 e.printStackTrace();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
182 throw new TripleStoreStoreError();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
183 } catch (TripleStoreHandlerException e) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
184 // TODO Auto-generated catch block
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
185 e.printStackTrace();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
186 throw new TripleStoreStoreError();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
187 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
188 return annot;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
189 }
0
dwinter
parents:
diff changeset
190
dwinter
parents:
diff changeset
191 }