annotate src/main/java/de/mpiwg/itgroup/annotationManager/RDFHandling/Convert.java @ 29:cf8166ee8918

still working on new annotations...
author casties
date Mon, 07 May 2012 19:50:39 +0200
parents 185db3cac82c
children b37487b756ac
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));
25
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
67
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
68 // author
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
69 if (annot.creator.startsWith("http")) {
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
70 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
71 } else {
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
72 // TODO: this should not happen
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
73 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
74 }
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
75 // creation time
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
76 if (annot.time == null) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
77 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
78 annot.time = format.format(Calendar.getInstance().getTime());
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
79 }
25
52dff477e45f on the way to a new ontology...
casties
parents: 23
diff changeset
80 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
81
29
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
82 if (annot.page == null || annot.page == "") {
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
83 // target without page number (full URL)
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
84 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
85 // is target of annotation
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
86 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasTarget", annot.xpointer, context));
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
87 } else {
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
88 // ConstrainedTarget with page number (full URL)
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 + "ConstrainedTarget", 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 // constrains Target
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
93 //retQuad.add(new Quadruple(constraint, NS.OAC_NS + "constrains", annot.xpointer, context));
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
94 // TextPageConstraint
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
95 String constraint = createRessourceURL("annotC:");
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
96 retQuad.add(new Quadruple(constraint, NS.RDF_NS + "type", NS.MPIWG_ANNOT_NS + "TextPageConstraint", context));
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
97 retQuad.add(new LiteralQuadruple(constraint, NS.MPIWG_ANNOT_NS + "textPage", annot.page, context));
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
98 // constrains Target
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
99 retQuad.add(new Quadruple(constraint, NS.OAC_NS + "constrains", annot.xpointer, context));
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
100
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
101 }
27
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
102
26
235b91ba8dff on the way to new annotations...
casties
parents: 25
diff changeset
103 // annotation body
27
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
104 if (annot.url != null && annot.url.startsWith("http://")) {
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
105 // body is resource
29
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
106 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
107 // is body of annotation
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
108 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasBody", annot.url, context));
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
109 } else {
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
110 // body is literal text
29
cf8166ee8918 still working on new annotations...
casties
parents: 28
diff changeset
111 String annotationtext = createRessourceURL("annotB:");
27
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
112 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
113 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
114 retQuad.add(new LiteralQuadruple(annotationtext, NS.CNT_NS + "chars", annot.text, context));
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
115 // is body of annotation
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
116 retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasBody", annotationtext, context));
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
117 }
17
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 for (Quadruple ret : retQuad) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
120 logger.debug(ret.toString());
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
121 }
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 annot.setAnnotationUri(annotationUrl);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
124 return retQuad;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
125 }
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 /**
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
128 * Erzeuge eine urn aus der aktullen Zeit in millis
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
129 *
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
130 * @return
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
131 */
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
132 private String createRessourceURL(String prefix) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
133
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
134 Calendar cal = Calendar.getInstance();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
135
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
136 long time = cal.getTimeInMillis();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
137
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
138 return String.format("%s%s%s", urlBase, prefix, time);
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 }
0
dwinter
parents:
diff changeset
141
17
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 * Stores the Annotation in the TripleStore.
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 * @param annot
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
146 * @return
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
147 * @throws TripleStoreStoreError
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 public Annotation storeAnnotation(Annotation annot) throws TripleStoreStoreError {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
150 List<Quadruple> annotationRdf = new ArrayList<Quadruple>();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
151 if ((annot.type == null) || annot.type.equals("")) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
152 annot.type = "Comment";
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
153 }
27
b4a798d417fe on the way to new annotations...
casties
parents: 26
diff changeset
154 annotationRdf = annot2quadruple(annot);
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
155 try {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
156 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
157 th.write(annotationRdf);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
158 } catch (RepositoryException e) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
159 // TODO Auto-generated catch block
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
160 e.printStackTrace();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
161 throw new TripleStoreStoreError();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
162 } catch (TripleStoreHandlerException e) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
163 // TODO Auto-generated catch block
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
164 e.printStackTrace();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
165 throw new TripleStoreStoreError();
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 return annot;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
168 }
0
dwinter
parents:
diff changeset
169
17
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 args
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
172 */
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
173 public static void main(String[] args) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
174 Convert myConvert = new Convert("http://annotations.rdf");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
175 List<Quadruple> rets = myConvert
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
176 .annot2quadruple(
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
177 "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
178 "mbuchman", null, "myannot", "Example");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
179 for (Quadruple ret : rets) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
180 System.out.println(ret.toString());
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
181 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 13
diff changeset
182 }
0
dwinter
parents:
diff changeset
183
dwinter
parents:
diff changeset
184 }