Changeset 16:794077e6288c 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
r15 r16 6 6 import java.util.ArrayList; 7 7 import java.util.Calendar; 8 import java.util.HashSet; 8 9 import java.util.List; 10 import java.util.Set; 9 11 10 12 import org.apache.log4j.Logger; … … 35 37 36 38 public static enum NodeTypes { 37 ANNOTATION, PERSON, TARGET, GROUP 39 ANNOTATION, PERSON, TARGET, GROUP, TAG 38 40 } 39 41 … … 41 43 42 44 public static enum RelationTypes implements RelationshipType { 43 ANNOTATES, CREATED, PERMITS_ADMIN, PERMITS_DELETE, PERMITS_UPDATE, PERMITS_READ, MEMBER_OF 45 ANNOTATES, CREATED, PERMITS_ADMIN, PERMITS_DELETE, PERMITS_UPDATE, PERMITS_READ, MEMBER_OF, HAS_TAG 44 46 } 45 47 … … 49 51 super(); 50 52 this.graphDb = graphDb; 51 nodeIndexes = new ArrayList<Index<Node>>( 3);53 nodeIndexes = new ArrayList<Index<Node>>(5); 52 54 // List.set(enum.ordinal(), val) seems not to work. 53 55 nodeIndexes.add(NodeTypes.ANNOTATION.ordinal(), graphDb.index().forNodes("annotations")); … … 55 57 nodeIndexes.add(NodeTypes.TARGET.ordinal(), graphDb.index().forNodes("targets")); 56 58 nodeIndexes.add(NodeTypes.GROUP.ordinal(), graphDb.index().forNodes("groups")); 59 nodeIndexes.add(NodeTypes.TAG.ordinal(), graphDb.index().forNodes("tags")); 57 60 } 58 61 … … 71 74 } 72 75 73 74 76 /** 75 77 * Returns List of Groups the person is member of. … … 86 88 // make sure we're getting a group 87 89 if (!(group instanceof Group)) { 88 logger.error("target of MEMBER_OF is not GROUP! rel=" +rel);90 logger.error("target of MEMBER_OF is not GROUP! rel=" + rel); 89 91 continue; 90 92 } … … 93 95 return groups; 94 96 } 95 97 96 98 /** 97 99 * Returns if person with uri is in Group group. … … 104 106 Node pn = getPersonNodeByUri(person.getUriString()); 105 107 if (pn == null) return false; 106 // optimi sed version of getGroupsForPersonNode108 // optimized version of getGroupsForPersonNode 107 109 Iterable<Relationship> rels = pn.getRelationships(RelationTypes.MEMBER_OF); 108 110 for (Relationship rel : rels) { … … 114 116 return false; 115 117 } 116 118 117 119 /** 118 120 * Returns the Annotation with the given id. … … 138 140 annot.setBodyText((String) annotNode.getProperty("bodyText", null)); 139 141 annot.setBodyUri((String) annotNode.getProperty("bodyUri", null)); 140 // get annotation target from relation 142 /* 143 * get annotation target from relation 144 */ 141 145 Relationship targetRel = getRelation(annotNode, RelationTypes.ANNOTATES, null); 142 146 if (targetRel != null) { … … 151 155 annot.setFragmentType(FragmentTypes.valueOf(ft)); 152 156 } 153 // get creator from relation 157 /* 158 * get creator from relation 159 */ 154 160 Relationship creatorRel = getRelation(annotNode, RelationTypes.CREATED, null); 155 161 if (creatorRel != null) { … … 160 166 logger.error("annotation " + annotNode + " has no creator node!"); 161 167 } 162 // get creation date 168 /* 169 * get creation date 170 */ 163 171 annot.setCreated((String) annotNode.getProperty("created", null)); 164 // get permissions 172 /* 173 * get permissions 174 */ 165 175 Relationship adminRel = getRelation(annotNode, RelationTypes.PERMITS_ADMIN, null); 166 176 if (adminRel != null) { … … 187 197 annot.setReadPermission(read); 188 198 } 199 /* 200 * get tags 201 */ 202 Set<String> tags = new HashSet<String>(); 203 for (Relationship rel : annotNode.getRelationships(RelationTypes.HAS_TAG)) { 204 String tag = (String) rel.getEndNode().getProperty("name", null); 205 if (tag != null) { 206 tags.add(tag); 207 } 208 } 209 annot.setTags(tags); 189 210 190 211 return annot; … … 286 307 setPermissionRelation(annotNode, RelationTypes.PERMITS_READ, annot.getReadPermission()); 287 308 309 /* 310 * Tags on this annotation. 311 */ 312 Set<String> newTags = annot.getTags(); 313 // we ignore existing tags if tags == null 314 if (newTags != null) { 315 List<Relationship> oldHasTags = new ArrayList<Relationship>(); 316 for (Relationship rel : annotNode.getRelationships(RelationTypes.HAS_TAG)) { 317 oldHasTags.add(rel); 318 } 319 // adjust to new tags 320 if (newTags.isEmpty()) { 321 // remove old tags 322 if (!oldHasTags.isEmpty()) { 323 for (Relationship rel : oldHasTags) { 324 rel.delete(); 325 // TODO: should we delete orphan nodes too? 326 } 327 } 328 } else { 329 if (!oldHasTags.isEmpty()) { 330 // adjust old tags 331 for (Relationship rel : oldHasTags) { 332 String oldTag = (String) rel.getEndNode().getProperty("name", null); 333 if (newTags.contains(oldTag)) { 334 // tag exists 335 newTags.remove(oldTag); 336 } else { 337 // tag exists no longer 338 rel.delete(); 339 // TODO: should we delete orphan nodes too? 340 } 341 } 342 } 343 if (!newTags.isEmpty()) { 344 // still tags to add 345 for (String tag : newTags) { 346 // create new tag 347 Node tagNode = getOrCreateTagNode(tag); 348 getOrCreateRelation(annotNode, RelationTypes.HAS_TAG, tagNode); 349 } 350 } 351 352 } 353 } 288 354 tx.success(); 289 355 } finally { … … 371 437 } 372 438 } 439 // TODO: if both uri and user are given we should intersect 373 440 return annotations; 374 441 } 375 442 443 /** 444 * Returns Relationship of type from Node start to Node end. Creates one if 445 * it doesn't exist. 446 * 447 * @param start 448 * @param type 449 * @param end 450 * @return 451 */ 376 452 protected Relationship getOrCreateRelation(Node start, RelationshipType type, Node end) { 377 453 if (start.hasRelationship()) { … … 458 534 person.setProperty("TYPE", NodeTypes.GROUP.name()); 459 535 } else { 460 person.setProperty("TYPE", NodeTypes.PERSON.name()); 536 person.setProperty("TYPE", NodeTypes.PERSON.name()); 461 537 } 462 538 person.setProperty("uri", uri); … … 474 550 } 475 551 return person; 552 } 553 554 protected Node getOrCreateTagNode(String tagname) { 555 Index<Node> idx = getNodeIndex(NodeTypes.TAG); 556 IndexHits<Node> tags = idx.get("name", tagname); 557 Node tag = tags.getSingle(); 558 if (tag == null) { 559 // does not exist yet 560 Transaction tx = graphDb.beginTx(); 561 try { 562 tag = graphDb.createNode(); 563 tag.setProperty("TYPE", NodeTypes.TAG.name()); 564 tag.setProperty("name", tagname); 565 idx.add(tag, "name", tagname); 566 tx.success(); 567 } finally { 568 tx.finish(); 569 } 570 } 571 return tag; 476 572 } 477 573 … … 533 629 } 534 630 535 /** returns the (first) Relationship of RelationTypes type from Node start. 536 * 631 /** 632 * returns the (first) Relationship of RelationTypes type from Node start. 633 * 537 634 * @param start 538 635 * @param type … … 549 646 } 550 647 for (Relationship rel : rels) { 648 // just the first one 551 649 return rel; 552 650 }
Note: See TracChangeset
for help on using the changeset viewer.