Changeset 6:6dfbe2400f64 in AnnotationManagerN4J
- Timestamp:
- Jul 11, 2012, 3:08:40 PM (13 years ago)
- Branch:
- default
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
.hgignore
r3 r6 10 10 syntax: regexp 11 11 ^\.shell_history$ 12 syntax: regexp 13 ^src/main/webapp/WEB-INF/consumerkeys\.property$ -
src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java
r5 r6 27 27 public class AnnotationStore { 28 28 29 protected static Logger logger = Logger.getLogger(AnnotationStore.class); 30 31 protected GraphDatabaseService graphDb; 32 33 public static enum RelationTypes implements RelationshipType { 34 ANNOTATES, CREATED 35 } 36 37 public static String ANNOTATION_URI_BASE = "http://entities.mpiwg-berlin.mpg.de/annotations/"; 38 39 public AnnotationStore(GraphDatabaseService graphDb) { 40 super(); 41 this.graphDb = graphDb; 42 } 43 44 public Annotation getAnnotationById(String id) { 45 Index<Node> idx = graphDb.index().forNodes("annotations"); 46 Node annotNode = idx.get("id", id).getSingle(); 47 Annotation annot = createAnnotation(annotNode); 48 return annot; 49 } 50 51 /** 52 * Returns an Annotation object from an annotation-Node. 53 * 54 * @param annotNode 55 * @return 56 */ 57 public Annotation createAnnotation(Node annotNode) { 58 Annotation annot = new Annotation(); 59 annot.setUri((String) annotNode.getProperty("id", null)); 60 annot.setBodyText((String) annotNode.getProperty("bodyText", null)); 61 annot.setBodyUri((String) annotNode.getProperty("bodyUri", null)); 62 // get annotation target from relation 63 Iterable<Relationship> targetRels = annotNode.getRelationships(RelationTypes.ANNOTATES); 64 for (Relationship targetRel : targetRels) { 65 Node target = targetRel.getEndNode(); 66 annot.setTargetBaseUri((String) target.getProperty("uri", null)); 67 // just the first one 68 break; 69 } 70 annot.setTargetFragment((String) annotNode.getProperty("targetFragment", null)); 71 String ft = (String) annotNode.getProperty("fragmentType", null); 72 if (ft != null) { 73 annot.setFragmentType(FragmentTypes.valueOf(ft)); 74 } 75 // get creator form relation 76 Iterable<Relationship> creatorRels = annotNode.getRelationships(RelationTypes.CREATED); 77 for (Relationship creatorRel : creatorRels) { 78 Node creator = creatorRel.getStartNode(); 79 annot.setCreatorUri((String) creator.getProperty("uri", null)); 80 annot.setCreatorName((String) creator.getProperty("name", null)); 81 // just the first one 82 break; 83 } 84 annot.setCreated((String) annotNode.getProperty("created", null)); 85 return annot; 86 } 87 88 /** 89 * Store a new annotation in the store or update an existing one. Returns 90 * the stored annotation. 91 * 92 * @param annot 93 * @return 94 */ 95 public Annotation storeAnnotation(Annotation annot) { 96 Node annotNode = null; 97 Transaction tx = graphDb.beginTx(); 98 try { 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 108 /* 109 * the annotation body 110 */ 111 String bodyText = annot.getBodyText(); 112 if (bodyText != null) { 113 annotNode.setProperty("bodyText", bodyText); 114 } 115 String bodyUri = annot.getBodyUri(); 116 if (bodyUri != null) { 117 annotNode.setProperty("bodyUri", bodyUri); 118 } 119 120 /* 121 * the annotation target 122 */ 123 String targetBaseUri = annot.getTargetBaseUri(); 124 if (targetBaseUri != null) { 125 Node target = getOrCreateTargetNode(targetBaseUri); 126 getOrCreateRelation(annotNode, RelationTypes.ANNOTATES, target); 127 } 128 129 /* 130 * The fragment part of the annotation target. 131 */ 132 String targetFragment = annot.getTargetFragment(); 133 FragmentTypes fragmentType = annot.getFragmentType(); 134 if (targetFragment != null) { 135 annotNode.setProperty("targetFragment", targetFragment); 136 annotNode.setProperty("fragmentType", fragmentType.name()); 137 } 138 139 /* 140 * The name of the creator of this annotation. 141 */ 142 String creatorName = annot.getCreatorName(); 143 144 /* 145 * The URI of the creator of this annotation. 146 */ 147 String creatorUri = annot.getCreatorUri(); 148 if (creatorUri != null) { 149 Node creator = getOrCreatePersonNode(creatorUri, creatorName); 150 getOrCreateRelation(creator, RelationTypes.CREATED, annotNode); 151 } 152 153 /* 154 * The creation date of this annotation. 155 */ 156 String created = annot.getCreated(); 157 if (created != null) { 158 annotNode.setProperty("created", created); 159 } 160 161 tx.success(); 162 } finally { 163 tx.finish(); 164 } 165 166 // re-read and return annotation 167 Annotation storedAnnot = createAnnotation(annotNode); 168 return storedAnnot; 169 } 170 171 /** 172 * Deletes the annotation with the id. 173 * 174 * @param id 175 */ 176 public void deleteById(String id) { 177 // TODO Auto-generated method stub 178 179 } 180 181 /** 182 * Returns all annotations with the given uri and/or user. 183 * 184 * @param uri 185 * @param userUri 186 * @param limit 187 * @param offset 188 * @return 189 */ 190 public List<Annotation> searchByUriUser(String targetUri, String userUri, String limit, String offset) { 191 List<Annotation> annotations = new ArrayList<Annotation>(); 192 if (targetUri != null) { 193 Index<Node> targetIdx = graphDb.index().forNodes("targets"); 194 // there should be only one 195 Node target = targetIdx.get("uri", targetUri).getSingle(); 196 if (target != null) { 197 Iterable<Relationship> relations = target.getRelationships(RelationTypes.ANNOTATES); 198 for (Relationship relation : relations) { 199 Node ann = relation.getStartNode(); 200 if (ann.getProperty("TYPE", "").equals("ANNOTATION")) { 201 Annotation annot = createAnnotation(ann); 202 annotations.add(annot); 203 } else { 204 logger.error("ANNOTATES relation does not start with ANNOTATION: " + ann); 205 } 206 } 207 } 208 } 209 if (userUri != null) { 210 Index<Node> persIdx = graphDb.index().forNodes("persons"); 211 // there should be only one 212 Node person = persIdx.get("uri", userUri).getSingle(); 213 if (person != null) { 214 Iterable<Relationship> relations = person.getRelationships(RelationTypes.CREATED); 215 for (Relationship relation : relations) { 216 Node ann = relation.getEndNode(); 217 if (ann.getProperty("TYPE", "").equals("ANNOTATION")) { 218 Annotation annot = createAnnotation(ann); 219 annotations.add(annot); 220 } else { 221 logger.error("CREATED relation does not end with ANNOTATION: " + ann); 222 } 223 } 224 } 225 } 226 return annotations; 227 } 228 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) { 253 Index<Node> idx = graphDb.index().forNodes("annotations"); 254 IndexHits<Node> annotations = idx.get("id", id); 255 Node annotation = annotations.getSingle(); 256 if (annotation == null) { 257 // does not exist yet 258 Transaction tx = graphDb.beginTx(); 259 try { 260 annotation = graphDb.createNode(); 261 annotation.setProperty("TYPE", "ANNOTATION"); 262 annotation.setProperty("id", id); 263 idx.add(annotation, "id", id); 264 tx.success(); 265 } finally { 266 tx.finish(); 267 } 268 } 269 return annotation; 270 } 271 272 protected Node getOrCreateTargetNode(String uri) { 273 Index<Node> idx = graphDb.index().forNodes("targets"); 274 IndexHits<Node> targets = idx.get("uri", uri); 275 Node target = targets.getSingle(); 276 if (target == null) { 277 // does not exist yet 278 Transaction tx = graphDb.beginTx(); 279 try { 280 target = graphDb.createNode(); 281 target.setProperty("TYPE", "TARGET"); 282 target.setProperty("uri", uri); 283 idx.add(target, "uri", uri); 284 tx.success(); 285 } finally { 286 tx.finish(); 287 } 288 } 289 return target; 290 } 291 292 protected Node getOrCreatePersonNode(String uri, String name) { 293 Index<Node> idx = graphDb.index().forNodes("persons"); 294 // Person is identified by URI 295 IndexHits<Node> persons = idx.get("uri", uri); 296 Node person = persons.getSingle(); 297 if (person == null) { 298 // does not exist yet 299 Transaction tx = graphDb.beginTx(); 300 try { 301 person = graphDb.createNode(); 302 person.setProperty("TYPE", "PERSON"); 303 person.setProperty("uri", uri); 304 idx.add(person, "uri", uri); 305 if (name != null) { 306 person.setProperty("name", name); 307 } 308 tx.success(); 309 } finally { 310 tx.finish(); 311 } 312 } 313 return person; 314 } 315 316 /** 317 * Erzeuge eine urn aus der aktuellen Zeit in millis 318 * 319 * @return 320 */ 321 private String createRessourceURI(String prefix) { 322 323 Calendar cal = Calendar.getInstance(); 324 325 long time = cal.getTimeInMillis(); 326 327 return String.format("%s%s%s", ANNOTATION_URI_BASE, prefix, time); 328 329 } 29 protected static Logger logger = Logger.getLogger(AnnotationStore.class); 30 31 protected GraphDatabaseService graphDb; 32 33 public static enum NodeTypes { 34 ANNOTATION, PERSON, TARGET 35 } 36 37 protected List<Index<Node>> nodeIndexes; 38 39 public static enum RelationTypes implements RelationshipType { 40 ANNOTATES, CREATED 41 } 42 43 public static String ANNOTATION_URI_BASE = "http://entities.mpiwg-berlin.mpg.de/annotations/"; 44 45 public AnnotationStore(GraphDatabaseService graphDb) { 46 super(); 47 this.graphDb = graphDb; 48 nodeIndexes = new ArrayList<Index<Node>>(3); 49 // List.set(enum.ordinal(), val) seems not to work. 50 nodeIndexes.add(NodeTypes.ANNOTATION.ordinal(), graphDb.index().forNodes("annotations")); 51 nodeIndexes.add(NodeTypes.PERSON.ordinal(), graphDb.index().forNodes("persons")); 52 nodeIndexes.add(NodeTypes.TARGET.ordinal(), graphDb.index().forNodes("targets")); 53 } 54 55 protected Index<Node> getNodeIndex(NodeTypes type) { 56 return nodeIndexes.get(type.ordinal()); 57 } 58 59 /** 60 * Returns the Annotation with the given id. 61 * 62 * @param id 63 * @return 64 */ 65 public Annotation getAnnotationById(String id) { 66 Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle(); 67 Annotation annot = createAnnotation(annotNode); 68 return annot; 69 } 70 71 /** 72 * Returns an Annotation object from an annotation-Node. 73 * 74 * @param annotNode 75 * @return 76 */ 77 public Annotation createAnnotation(Node annotNode) { 78 Annotation annot = new Annotation(); 79 annot.setUri((String) annotNode.getProperty("id", null)); 80 annot.setBodyText((String) annotNode.getProperty("bodyText", null)); 81 annot.setBodyUri((String) annotNode.getProperty("bodyUri", null)); 82 // get annotation target from relation 83 Iterable<Relationship> targetRels = annotNode 84 .getRelationships(RelationTypes.ANNOTATES); 85 for (Relationship targetRel : targetRels) { 86 Node target = targetRel.getEndNode(); 87 annot.setTargetBaseUri((String) target.getProperty("uri", null)); 88 // just the first one 89 break; 90 } 91 annot.setTargetFragment((String) annotNode.getProperty( 92 "targetFragment", null)); 93 String ft = (String) annotNode.getProperty("fragmentType", null); 94 if (ft != null) { 95 annot.setFragmentType(FragmentTypes.valueOf(ft)); 96 } 97 // get creator form relation 98 Iterable<Relationship> creatorRels = annotNode 99 .getRelationships(RelationTypes.CREATED); 100 for (Relationship creatorRel : creatorRels) { 101 Node creator = creatorRel.getStartNode(); 102 annot.setCreatorUri((String) creator.getProperty("uri", null)); 103 annot.setCreatorName((String) creator.getProperty("name", null)); 104 // just the first one 105 break; 106 } 107 annot.setCreated((String) annotNode.getProperty("created", null)); 108 return annot; 109 } 110 111 /** 112 * Store a new annotation in the store or update an existing one. Returns 113 * the stored annotation. 114 * 115 * @param annot 116 * @return 117 */ 118 public Annotation storeAnnotation(Annotation annot) { 119 Node annotNode = null; 120 Transaction tx = graphDb.beginTx(); 121 try { 122 /* 123 * create or get the annotation 124 */ 125 String id = annot.getUri(); 126 if (id == null) { 127 id = createRessourceURI("annot:"); 128 } 129 annotNode = getOrCreateAnnotationNode(id); 130 131 /* 132 * the annotation body 133 */ 134 String bodyText = annot.getBodyText(); 135 if (bodyText != null) { 136 annotNode.setProperty("bodyText", bodyText); 137 } 138 String bodyUri = annot.getBodyUri(); 139 if (bodyUri != null) { 140 annotNode.setProperty("bodyUri", bodyUri); 141 } 142 143 /* 144 * the annotation target 145 */ 146 String targetBaseUri = annot.getTargetBaseUri(); 147 if (targetBaseUri != null) { 148 Node target = getOrCreateTargetNode(targetBaseUri); 149 getOrCreateRelation(annotNode, RelationTypes.ANNOTATES, target); 150 } 151 152 /* 153 * The fragment part of the annotation target. 154 */ 155 String targetFragment = annot.getTargetFragment(); 156 FragmentTypes fragmentType = annot.getFragmentType(); 157 if (targetFragment != null) { 158 annotNode.setProperty("targetFragment", targetFragment); 159 annotNode.setProperty("fragmentType", fragmentType.name()); 160 } 161 162 /* 163 * The name of the creator of this annotation. 164 */ 165 String creatorName = annot.getCreatorName(); 166 167 /* 168 * The URI of the creator of this annotation. 169 */ 170 String creatorUri = annot.getCreatorUri(); 171 if (creatorUri != null) { 172 Node creator = getOrCreatePersonNode(creatorUri, creatorName); 173 getOrCreateRelation(creator, RelationTypes.CREATED, annotNode); 174 } 175 176 /* 177 * The creation date of this annotation. 178 */ 179 String created = annot.getCreated(); 180 if (created != null) { 181 annotNode.setProperty("created", created); 182 } 183 184 tx.success(); 185 } finally { 186 tx.finish(); 187 } 188 189 // re-read and return annotation 190 Annotation storedAnnot = createAnnotation(annotNode); 191 return storedAnnot; 192 } 193 194 /** 195 * Deletes the annotation with the given id. 196 * 197 * @param id 198 */ 199 public void deleteById(String id) { 200 Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle(); 201 if (annotNode != null) { 202 // delete related objects 203 Transaction tx = graphDb.beginTx(); 204 try { 205 for (Relationship rel : annotNode.getRelationships()) { 206 // delete relation and the related node if it has no other relations 207 Node other = rel.getOtherNode(annotNode); 208 rel.delete(); 209 if (! other.hasRelationship()) { 210 deleteNode(other); 211 } 212 } 213 if (! annotNode.hasRelationship()) { 214 deleteNode(annotNode); 215 } else { 216 logger.error("deleteById: unable to delete: Node still has relations."); 217 } 218 tx.success(); 219 } finally { 220 tx.finish(); 221 } 222 } 223 224 } 225 226 /** 227 * Returns all annotations with the given uri and/or user. 228 * 229 * @param uri 230 * @param userUri 231 * @param limit 232 * @param offset 233 * @return 234 */ 235 public List<Annotation> searchByUriUser(String targetUri, String userUri, 236 String limit, String offset) { 237 List<Annotation> annotations = new ArrayList<Annotation>(); 238 if (targetUri != null) { 239 // there should be only one 240 Node target = getNodeIndex(NodeTypes.TARGET).get("uri", targetUri).getSingle(); 241 if (target != null) { 242 Iterable<Relationship> relations = target 243 .getRelationships(RelationTypes.ANNOTATES); 244 for (Relationship relation : relations) { 245 Node ann = relation.getStartNode(); 246 if (ann.getProperty("TYPE", "").equals("ANNOTATION")) { 247 Annotation annot = createAnnotation(ann); 248 annotations.add(annot); 249 } else { 250 logger.error("ANNOTATES relation does not start with ANNOTATION: " 251 + ann); 252 } 253 } 254 } 255 } 256 if (userUri != null) { 257 // there should be only one 258 Node person = getNodeIndex(NodeTypes.PERSON).get("uri", userUri).getSingle(); 259 if (person != null) { 260 Iterable<Relationship> relations = person 261 .getRelationships(RelationTypes.CREATED); 262 for (Relationship relation : relations) { 263 Node ann = relation.getEndNode(); 264 if (ann.getProperty("TYPE", "").equals("ANNOTATION")) { 265 Annotation annot = createAnnotation(ann); 266 annotations.add(annot); 267 } else { 268 logger.error("CREATED relation does not end with ANNOTATION: " 269 + ann); 270 } 271 } 272 } 273 } 274 return annotations; 275 } 276 277 protected Relationship getOrCreateRelation(Node start, 278 RelationshipType type, Node end) { 279 if (start.hasRelationship()) { 280 // there are relations 281 Iterable<Relationship> rels = start.getRelationships(type, 282 Direction.OUTGOING); 283 for (Relationship rel : rels) { 284 if (rel.getEndNode().equals(end)) { 285 // relation exists 286 return rel; 287 } 288 } 289 } 290 // create new one 291 Relationship rel; 292 Transaction tx = graphDb.beginTx(); 293 try { 294 rel = start.createRelationshipTo(end, type); 295 tx.success(); 296 } finally { 297 tx.finish(); 298 } 299 return rel; 300 } 301 302 protected Node getOrCreateAnnotationNode(String id) { 303 Index<Node> idx = getNodeIndex(NodeTypes.ANNOTATION); 304 IndexHits<Node> annotations = idx.get("id", id); 305 Node annotation = annotations.getSingle(); 306 if (annotation == null) { 307 // does not exist yet 308 Transaction tx = graphDb.beginTx(); 309 try { 310 annotation = graphDb.createNode(); 311 annotation.setProperty("TYPE", NodeTypes.ANNOTATION.name()); 312 annotation.setProperty("id", id); 313 idx.add(annotation, "id", id); 314 tx.success(); 315 } finally { 316 tx.finish(); 317 } 318 } 319 return annotation; 320 } 321 322 protected Node getOrCreateTargetNode(String uri) { 323 Index<Node> idx = getNodeIndex(NodeTypes.TARGET); 324 IndexHits<Node> targets = idx.get("uri", uri); 325 Node target = targets.getSingle(); 326 if (target == null) { 327 // does not exist yet 328 Transaction tx = graphDb.beginTx(); 329 try { 330 target = graphDb.createNode(); 331 target.setProperty("TYPE", NodeTypes.TARGET.name()); 332 target.setProperty("uri", uri); 333 idx.add(target, "uri", uri); 334 tx.success(); 335 } finally { 336 tx.finish(); 337 } 338 } 339 return target; 340 } 341 342 protected Node getOrCreatePersonNode(String uri, String name) { 343 // Person is identified by URI 344 Index<Node> idx = getNodeIndex(NodeTypes.PERSON); 345 IndexHits<Node> persons = idx.get("uri", uri); 346 Node person = persons.getSingle(); 347 if (person == null) { 348 // does not exist yet 349 Transaction tx = graphDb.beginTx(); 350 try { 351 person = graphDb.createNode(); 352 person.setProperty("TYPE", NodeTypes.PERSON.name()); 353 person.setProperty("uri", uri); 354 idx.add(person, "uri", uri); 355 if (name != null) { 356 person.setProperty("name", name); 357 } 358 tx.success(); 359 } finally { 360 tx.finish(); 361 } 362 } 363 return person; 364 } 365 366 /** 367 * Unindexes and deletes given Node if it has no relations. 368 * @param node 369 */ 370 protected void deleteNode(Node node) { 371 Transaction tx = graphDb.beginTx(); 372 try { 373 if (node.hasRelationship()) { 374 logger.error("deleteNode: unable to delete: Node still has relations."); 375 } else { 376 String ts = (String) node.getProperty("TYPE", null); 377 try { 378 NodeTypes type = NodeTypes.valueOf(ts); 379 getNodeIndex(type).remove(node); 380 } catch (Exception e) { 381 logger.error("deleteNode: unable to get TYPE of node: "+node); 382 } 383 node.delete(); 384 } 385 tx.success(); 386 } finally { 387 tx.finish(); 388 } 389 } 390 391 /** 392 * Erzeuge eine urn aus der aktuellen Zeit in millis 393 * 394 * @return 395 */ 396 private String createRessourceURI(String prefix) { 397 398 Calendar cal = Calendar.getInstance(); 399 400 long time = cal.getTimeInMillis(); 401 402 return String.format("%s%s%s", ANNOTATION_URI_BASE, prefix, time); 403 404 } 330 405 331 406 } -
src/main/java/de/mpiwg/itgroup/annotations/restlet/RestServer.java
r5 r6 54 54 private AnnotationStore store; 55 55 public static final String ANNSTORE_KEY = "annotationmanager.store"; 56 57 private String ldapServerUrl; 58 public static final String LDAP_SERVER_KEY = "annotationmanager.ldapserver.url"; 56 59 57 60 /** … … 81 84 */ 82 85 graphdbPath = serverConfig.getProperty(GRAPHDB_PATH_KEY, graphdbPath); 86 ldapServerUrl = serverConfig.getProperty(LDAP_SERVER_KEY, null); 83 87 } catch (IOException e) { 84 88 logger.warn("Error loading server config: ", e); … … 182 186 String retString = creator; // falls nichts gefunden wird einfach den 183 187 // creator zurueckgeben 188 if (ldapServerUrl == null) { 189 return retString; 190 } 184 191 Hashtable<String, String> env = new Hashtable<String, String>(); 185 192 String sp = "com.sun.jndi.ldap.LdapCtxFactory"; 186 193 env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, sp); 187 194 188 // TODO: should go into config file 189 String ldapUrl = "ldap://ldap.mpiwg-berlin.mpg.de/dc=mpiwg-berlin,dc=mpg,dc=de"; 190 env.put(javax.naming.Context.PROVIDER_URL, ldapUrl); 195 env.put(javax.naming.Context.PROVIDER_URL, ldapServerUrl); 191 196 192 197 DirContext dctx;
Note: See TracChangeset
for help on using the changeset viewer.