Changeset 5:bbf0cc5bee29 in AnnotationManagerN4J for src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note: See TracChangeset
for help on using the changeset viewer.