Changeset 5:bbf0cc5bee29 in AnnotationManagerN4J
- Timestamp:
- Jul 3, 2012, 7:23:17 PM (13 years ago)
- Branch:
- default
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
pom.xml
r4 r5 4 4 <groupId>de.mpiwg.itgroup.annotations</groupId> 5 5 <artifactId>AnnotationManagerN4J</artifactId> 6 <version>0. 0.1-SNAPSHOT</version>6 <version>0.2.0-SNAPSHOT</version> 7 7 <properties> 8 8 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> … … 22 22 </snapshots> 23 23 </repository> 24 25 26 27 28 24 <repository> 25 <id>maven-restlet</id> 26 <name>Public online Restlet repository</name> 27 <url>http://maven.restlet.org</url> 28 </repository> 29 29 </repositories> 30 30 <dependencies> … … 71 71 </exclusions> 72 72 </dependency> 73 74 75 76 77 78 79 80 81 82 73 <dependency> 74 <groupId>log4j</groupId> 75 <artifactId>log4j</artifactId> 76 <version>1.2.14</version> 77 </dependency> 78 <dependency> 79 <groupId>commons-codec</groupId> 80 <artifactId>commons-codec</artifactId> 81 <version>1.4</version> 82 </dependency> 83 83 </dependencies> 84 84 <build> 85 <finalName>AnnotationManager</finalName> 85 86 <pluginManagement> 86 87 <plugins> -
src/main/java/de/mpiwg/itgroup/annotations/Annotation.java
r4 r5 51 51 */ 52 52 protected String creatorUri; 53 54 /** 55 * The full name of the creator of this annotation. 56 */ 57 protected String creatorName; 53 58 54 59 /** … … 156 161 157 162 /** 163 * @return the creatorName 164 */ 165 public String getCreatorName() { 166 return creatorName; 167 } 168 169 /** 170 * @param creatorName the creatorName to set 171 */ 172 public void setCreatorName(String creatorName) { 173 this.creatorName = creatorName; 174 } 175 176 /** 158 177 * @return the created 159 178 */ -
src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java
r4 r5 9 9 10 10 import org.apache.log4j.Logger; 11 import org.neo4j.graphdb.Direction; 11 12 import org.neo4j.graphdb.GraphDatabaseService; 12 13 import org.neo4j.graphdb.Node; … … 59 60 annot.setBodyText((String) annotNode.getProperty("bodyText", null)); 60 61 annot.setBodyUri((String) annotNode.getProperty("bodyUri", null)); 61 Iterable<Relationship> targets = annotNode.getRelationships(RelationTypes.ANNOTATES); 62 for (Relationship target : targets) { 62 // get annotation target from relation 63 Iterable<Relationship> targetRels = annotNode.getRelationships(RelationTypes.ANNOTATES); 64 for (Relationship targetRel : targetRels) { 65 Node target = targetRel.getEndNode(); 63 66 annot.setTargetBaseUri((String) target.getProperty("uri", null)); 67 // just the first one 64 68 break; 65 69 } … … 69 73 annot.setFragmentType(FragmentTypes.valueOf(ft)); 70 74 } 71 Iterable<Relationship> creators = annotNode.getRelationships(RelationTypes.CREATED); 72 for (Relationship creator : creators) { 75 // get creator form relation 76 Iterable<Relationship> creatorRels = annotNode.getRelationships(RelationTypes.CREATED); 77 for (Relationship creatorRel : creatorRels) { 78 Node creator = creatorRel.getStartNode(); 73 79 annot.setCreatorUri((String) creator.getProperty("uri", null)); 80 annot.setCreatorName((String) creator.getProperty("name", null)); 81 // just the first one 74 82 break; 75 83 } … … 86 94 */ 87 95 public Annotation storeAnnotation(Annotation annot) { 96 Node annotNode = null; 88 97 Transaction tx = graphDb.beginTx(); 89 Node annotNode = null;90 String id = annot.getUri();91 if (id == null) {92 id = createRessourceURI("annot:");93 }94 annotNode = createAnnotationNode(id);95 96 98 try { 97 // Mutating operations go here 99 /* 100 * create or get the annotation 101 */ 102 String id = annot.getUri(); 103 if (id == null) { 104 id = createRessourceURI("annot:"); 105 } 106 annotNode = getOrCreateAnnotationNode(id); 107 98 108 /* 99 109 * the annotation body … … 113 123 String targetBaseUri = annot.getTargetBaseUri(); 114 124 if (targetBaseUri != null) { 115 Node target = createTargetNode(targetBaseUri);116 annotNode.createRelationshipTo(target, RelationTypes.ANNOTATES);125 Node target = getOrCreateTargetNode(targetBaseUri); 126 getOrCreateRelation(annotNode, RelationTypes.ANNOTATES, target); 117 127 } 118 128 … … 128 138 129 139 /* 140 * The name of the creator of this annotation. 141 */ 142 String creatorName = annot.getCreatorName(); 143 144 /* 130 145 * The URI of the creator of this annotation. 131 146 */ 132 147 String creatorUri = annot.getCreatorUri(); 133 148 if (creatorUri != null) { 134 Node creator = createPersonNode(creatorUri, null);135 creator.createRelationshipTo(annotNode, RelationTypes.CREATED);149 Node creator = getOrCreatePersonNode(creatorUri, creatorName); 150 getOrCreateRelation(creator, RelationTypes.CREATED, annotNode); 136 151 } 137 152 … … 181 196 if (target != null) { 182 197 Iterable<Relationship> relations = target.getRelationships(RelationTypes.ANNOTATES); 183 for (Relationship relation : relations) {198 for (Relationship relation : relations) { 184 199 Node ann = relation.getStartNode(); 185 200 if (ann.getProperty("TYPE", "").equals("ANNOTATION")) { … … 187 202 annotations.add(annot); 188 203 } else { 189 logger.error("ANNOTATES relation does not start with ANNOTATION: " +ann);204 logger.error("ANNOTATES relation does not start with ANNOTATION: " + ann); 190 205 } 191 206 } … … 198 213 if (person != null) { 199 214 Iterable<Relationship> relations = person.getRelationships(RelationTypes.CREATED); 200 for (Relationship relation : relations) {215 for (Relationship relation : relations) { 201 216 Node ann = relation.getEndNode(); 202 217 if (ann.getProperty("TYPE", "").equals("ANNOTATION")) { … … 204 219 annotations.add(annot); 205 220 } else { 206 logger.error("CREATED relation does not end with ANNOTATION: " +ann);221 logger.error("CREATED relation does not end with ANNOTATION: " + ann); 207 222 } 208 223 } … … 212 227 } 213 228 214 protected Node createAnnotationNode(String id) { 229 protected Relationship getOrCreateRelation(Node start, RelationshipType type, Node end) { 230 if (start.hasRelationship()) { 231 // there are relations 232 Iterable<Relationship> rels = start.getRelationships(type, Direction.OUTGOING); 233 for (Relationship rel : rels) { 234 if (rel.getEndNode().equals(end)) { 235 // relation exists 236 return rel; 237 } 238 } 239 } 240 // create new one 241 Relationship rel; 242 Transaction tx = graphDb.beginTx(); 243 try { 244 rel = start.createRelationshipTo(end, type); 245 tx.success(); 246 } finally { 247 tx.finish(); 248 } 249 return rel; 250 } 251 252 protected Node getOrCreateAnnotationNode(String id) { 215 253 Index<Node> idx = graphDb.index().forNodes("annotations"); 216 254 IndexHits<Node> annotations = idx.get("id", id); … … 232 270 } 233 271 234 protected Node createTargetNode(String uri) {272 protected Node getOrCreateTargetNode(String uri) { 235 273 Index<Node> idx = graphDb.index().forNodes("targets"); 236 274 IndexHits<Node> targets = idx.get("uri", uri); … … 252 290 } 253 291 254 protected Node createPersonNode(String uri, String name) {292 protected Node getOrCreatePersonNode(String uri, String name) { 255 293 Index<Node> idx = graphDb.index().forNodes("persons"); 256 294 // Person is identified by URI … … 291 329 } 292 330 293 294 331 } -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java
r4 r5 9 9 import java.security.InvalidKeyException; 10 10 import java.security.SignatureException; 11 import java.text.SimpleDateFormat; 11 12 import java.util.ArrayList; 13 import java.util.Calendar; 12 14 import java.util.List; 13 15 import java.util.regex.Matcher; … … 178 180 */ 179 181 public JSONObject createAnnotatorJson(Annotation annot) { 182 // return user as a JSON object (otherwise just as string) 180 183 boolean makeUserObject = true; 181 184 JSONObject jo = new JSONObject(); … … 198 201 userObject.put("id", userId); 199 202 // get full name 200 RestServer restServer = (RestServer) getApplication(); 201 String userName = restServer.getUserNameFromLdap(userId); 203 String userName = annot.getCreatorName(); 204 if (userName == null) { 205 RestServer restServer = (RestServer) getApplication(); 206 userName = restServer.getFullNameFromLdap(userId); 207 } 202 208 userObject.put("name", userName); 203 209 // save user object … … 241 247 try { 242 248 for (String xpointer : xpointers) { 243 String decoded = URLDecoder.decode(xpointer, "utf-8"); 249 //String decoded = URLDecoder.decode(xpointer, "utf-8"); 250 String decoded = xpointer; 244 251 Matcher m = rg.matcher(decoded); 245 252 … … 266 273 // TODO Auto-generated catch block 267 274 e.printStackTrace(); 268 } catch (UnsupportedEncodingException e) { 269 // TODO Auto-generated catch block 270 e.printStackTrace(); 271 } 272 275 } 273 276 return ja; 274 277 } … … 282 285 try { 283 286 for (String xpointer : xpointers) { 284 String decoded = URLDecoder.decode(xpointer, "utf-8"); 287 //String decoded = URLDecoder.decode(xpointer, "utf-8"); 288 String decoded = xpointer; 285 289 Matcher m = rg.matcher(decoded); 286 290 … … 300 304 // TODO Auto-generated catch block 301 305 e.printStackTrace(); 302 } catch (UnsupportedEncodingException e) { 303 // TODO Auto-generated catch block 304 e.printStackTrace(); 305 } 306 306 } 307 307 return ja; 308 308 } 309 309 310 protected String parseArea(JSONObject area) throws JSONException , UnsupportedEncodingException{310 protected String parseArea(JSONObject area) throws JSONException { 311 311 String x = area.getString("x"); 312 312 String y = area.getString("y"); … … 317 317 height = area.getString("height"); 318 318 } 319 String fragment = URLEncoder.encode(String.format("xywh=fraction:%s,%s,%s,%s", x, y, width, height), "utf-8");319 String fragment = String.format("xywh=fraction:%s,%s,%s,%s", x, y, width, height); 320 320 return fragment; 321 321 } 322 322 323 protected String parseRange(JSONObject range) throws JSONException , UnsupportedEncodingException{323 protected String parseRange(JSONObject range) throws JSONException { 324 324 String start = range.getString("start"); 325 325 String end = range.getString("end"); … … 327 327 String endOffset = range.getString("endOffset"); 328 328 329 String fragment = URLEncoder.encode(String.format(329 String fragment = String.format( 330 330 "xpointer(start-point(string-range(\"%s\",%s,1))/range-to(end-point(string-range(\"%s\",%s,1))))", start, 331 startOffset, end, endOffset) , "utf-8");331 startOffset, end, endOffset); 332 332 return fragment; 333 333 } 334 334 335 335 /** 336 * creates an Annotation object with data from JSON.336 * Creates an Annotation object with data from JSON. 337 337 * 338 338 * uses the specification from the annotator project: {@link https … … 352 352 } 353 353 354 /** 355 * Updates an Annotation object with data from JSON. 356 * 357 * uses the specification from the annotator project: {@link https 358 * ://github.com/okfn/annotator/wiki/Annotation-format} 359 * 360 * The username will be transformed to an URI if not given already as URI, 361 * if not it will set to the MPIWG namespace defined in 362 * de.mpiwg.itgroup.annotationManager.Constants.NS 363 * 364 * @param annot 365 * @param jo 366 * @return 367 * @throws JSONException 368 * @throws UnsupportedEncodingException 369 */ 354 370 public Annotation updateAnnotation(Annotation annot, JSONObject jo, Representation entity) throws JSONException, 355 371 UnsupportedEncodingException { 356 // annotateduri372 // target uri 357 373 if (jo.has("uri")) { 358 374 annot.setTargetBaseUri(jo.getString("uri")); … … 397 413 username = authUser; 398 414 } 399 // username should be a URI, if not it will set to the MPIWG namespace 400 // defined in 401 // de.mpiwg.itgroup.annotationManager.Constants.NS 415 // try to get full name 416 if (username != null) { 417 RestServer restServer = (RestServer) getApplication(); 418 String fullName = restServer.getFullNameFromLdap(username); 419 annot.setCreatorName(fullName); 420 } 421 // userUri should be a URI, if not it will set to the MPIWG namespace 402 422 if (userUri == null) { 403 423 if (username.startsWith("http")) { … … 408 428 } 409 429 // TODO: should we overwrite the creator? 430 if (annot.getCreatorUri() == null) { 431 annot.setCreatorUri(userUri); 432 } 433 434 if (annot.getCreated() == null) { 435 // set creation date 436 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); 437 String ct = format.format(Calendar.getInstance().getTime()); 438 annot.setCreated(ct); 439 } 410 440 411 441 // create xpointer from the first range/area -
src/main/java/de/mpiwg/itgroup/annotations/restlet/RestServer.java
r4 r5 41 41 public String CONSUMER_KEYS_PATH = "WEB-INF/consumerkeys.property"; 42 42 43 private Properties serverConfig; 44 public String CONFIG_PROPS_PATH = "WEB-INF/serverconfig.property"; 45 43 46 private GraphDatabaseService graphDb; 44 47 public static final String GRAPHDB_KEY = "annotationmanager.graphdb"; 45 public String DB_PATH = "WEB-INF/neo4j-annotation-db"; 48 public static final String GRAPHDB_PATH_KEY = "annotationmanager.graphdb.path"; 49 public String graphdbPath = "WEB-INF/neo4j-annotation-db"; 46 50 47 51 private WrappingNeoServerBootstrapper srv; … … 64 68 .get("org.restlet.ext.servlet.ServletContext"); 65 69 if (sc != null) { 70 /* 71 * read config from webapp 72 */ 73 serverConfig = new Properties(); 74 InputStream ps1 = getResourceAsStream(sc, CONFIG_PROPS_PATH); 75 if (ps1 != null) { 76 logger.debug("loading config from " + CONFIG_PROPS_PATH); 77 try { 78 serverConfig.load(ps1); 79 /* 80 * read serverconfig options 81 */ 82 graphdbPath = serverConfig.getProperty(GRAPHDB_PATH_KEY, graphdbPath); 83 } catch (IOException e) { 84 logger.warn("Error loading server config: ", e); 85 } 86 logger.debug("config: " + serverConfig); 87 } else { 88 logger.error("Unable to get resource " + CONFIG_PROPS_PATH); 89 } 66 90 // look for database service in context 67 91 graphDb = (GraphDatabaseService) sc.getAttribute(GRAPHDB_KEY); … … 70 94 * open database 71 95 */ 72 String dbFn = getResourcePath(sc, DB_PATH);96 String dbFn = getResourcePath(sc, graphdbPath); 73 97 if (dbFn != null) { 74 98 logger.debug("opening DB " + dbFn); … … 87 111 srv.start(); 88 112 } else { 89 logger.error("Unable to get resource " + DB_PATH);113 logger.error("Unable to get resource " + dbFn); 90 114 } 91 115 } … … 94 118 */ 95 119 consumerKeys = new Properties(); 96 InputStream ps = getResourceAsStream(sc, CONSUMER_KEYS_PATH);97 if (ps != null) {120 InputStream ps2 = getResourceAsStream(sc, CONSUMER_KEYS_PATH); 121 if (ps2 != null) { 98 122 logger.debug("loading consumer keys from " + CONSUMER_KEYS_PATH); 99 123 try { 100 consumerKeys.load(ps );124 consumerKeys.load(ps2); 101 125 } catch (IOException e) { 102 126 // TODO Auto-generated catch block … … 155 179 * @return 156 180 */ 157 public String get UserNameFromLdap(String creator) {181 public String getFullNameFromLdap(String creator) { 158 182 String retString = creator; // falls nichts gefunden wird einfach den 159 183 // creator zurueckgeben … … 163 187 164 188 // TODO: should go into config file 165 String ldapUrl = "ldap://ldap replik.mpiwg-berlin.mpg.de/dc=mpiwg-berlin,dc=mpg,dc=de";189 String ldapUrl = "ldap://ldap.mpiwg-berlin.mpg.de/dc=mpiwg-berlin,dc=mpg,dc=de"; 166 190 env.put(javax.naming.Context.PROVIDER_URL, ldapUrl); 167 191 … … 175 199 } 176 200 177 String base = "ou= People";201 String base = "ou=people"; 178 202 179 203 SearchControls sc = new SearchControls();
Note: See TracChangeset
for help on using the changeset viewer.