Changeset 32:0731c4549065 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
r29 r32 5 5 6 6 import java.util.ArrayList; 7 import java.util.Arrays; 7 8 import java.util.Calendar; 8 9 import java.util.HashSet; … … 28 29 29 30 /** 31 * Neo4J based Annotation store. 32 * 30 33 * @author casties 31 34 * … … 40 43 ANNOTATION, PERSON, TARGET, GROUP, TAG 41 44 } 45 46 // types of nodes that should not be automatically deleted. 47 public Set<String> permanentNodeTypes = new HashSet<String>(Arrays.asList("PERSON", "GROUP", "TAG")); 42 48 43 49 protected List<Index<Node>> nodeIndexes; … … 101 107 102 108 /** 103 * Returns List of Groups. 104 * Key has to be indexed. 109 * Returns List of Groups. Key has to be indexed. 105 110 * 106 111 * @param key … … 122 127 return groups; 123 128 } 124 125 126 /** 127 * Returns List of Tags. 128 * Key has to be indexed. 129 130 /** 131 * Returns List of Tags. Key has to be indexed. 129 132 * 130 133 * @param key … … 147 150 } 148 151 149 150 /** 152 /** 151 153 * Returns List of Groups the person is member of. 152 154 * … … 213 215 return members; 214 216 } 215 217 216 218 /** 217 219 * Add Person newMember to Group group. … … 230 232 return addedMember; 231 233 } 232 234 233 235 /** 234 236 * Delete Person oldMember from Group group. … … 239 241 public void deleteGroupMember(Group group, Person member) { 240 242 Node gn = getActorNode(group); 243 Node pn = getActorNode(member); 241 244 Iterable<Relationship> rels = gn.getRelationships(RelationTypes.MEMBER_OF); 242 245 for (Relationship rel : rels) { 243 246 Node mn = rel.getStartNode(); 244 if (mn.equals(member)) { 245 rel.delete(); 247 if (mn.equals(pn)) { 248 Transaction tx = graphDb.beginTx(); 249 try { 250 rel.delete(); 251 tx.success(); 252 } finally { 253 tx.finish(); 254 } 246 255 // there should be only one 247 256 break; 248 257 } 249 } 250 } 251 258 } 259 } 260 252 261 /** 253 262 * Returns the stored Actor matching the given one. … … 257 266 */ 258 267 public Actor getActor(Actor actor) { 259 Node actorNode = getActorNode(actor); 260 Actor storedActor = createActorFromNode(actorNode); 261 return storedActor; 262 } 263 264 /** 265 * Stores an Actor (Person or Group). Creates a new actor Node or returns an existing one. 268 Node actorNode = getActorNode(actor); 269 Actor storedActor = createActorFromNode(actorNode); 270 return storedActor; 271 } 272 273 /** 274 * Stores an Actor (Person or Group). Creates a new actor Node or update an 275 * existing one. 266 276 * 267 277 * @param actor … … 269 279 */ 270 280 public Actor storeActor(Actor actor) { 271 Node actorNode = getOrCreateActorNode(actor); 272 Actor storedActor = createActorFromNode(actorNode); 273 return storedActor; 274 } 275 281 Node actorNode = getOrCreateActorNode(actor); 282 Transaction tx = graphDb.beginTx(); 283 try { 284 // id 285 String id = actor.getId(); 286 if (id != null) { 287 actorNode.setProperty("id", id); 288 } 289 // name 290 String name = actor.getName(); 291 if (name != null) { 292 actorNode.setProperty("name", name); 293 } 294 // uri 295 String uri = actor.getUri(); 296 if (uri != null) { 297 actorNode.setProperty("uri", uri); 298 } 299 tx.success(); 300 } finally { 301 tx.finish(); 302 } 303 Actor storedActor = createActorFromNode(actorNode); 304 return storedActor; 305 } 306 307 /** 308 * Deletes the given Actor. 309 * 310 * @param actor 311 */ 312 public void deleteActor(Actor actor) { 313 String uri = actor.getUriString(); 314 Index<Node> idx; 315 if (actor.isGroup()) { 316 idx = getNodeIndex(NodeTypes.GROUP); 317 } else { 318 idx = getNodeIndex(NodeTypes.PERSON); 319 } 320 Node actorNode = idx.get("uri", uri).getSingle(); 321 if (actorNode != null) { 322 // delete relations 323 Transaction tx = graphDb.beginTx(); 324 try { 325 for (Relationship rel : actorNode.getRelationships()) { 326 rel.delete(); 327 } 328 if (!actorNode.hasRelationship()) { 329 // this shouldn't happen 330 deleteNode(actorNode); 331 } else { 332 logger.error("deleteActor: unable to delete: Node still has relations."); 333 } 334 tx.success(); 335 } finally { 336 tx.finish(); 337 } 338 } 339 } 340 276 341 /** 277 342 * Returns the Annotation with the given id. … … 376 441 */ 377 442 protected Actor createActorFromNode(Node actorNode) { 443 if (actorNode == null) return null; 378 444 String id = (String) actorNode.getProperty("id", null); 379 445 String uri = (String) actorNode.getProperty("uri", null); … … 387 453 return null; 388 454 } 389 455 390 456 public Tag createTagFromNode(Node tagNode) { 391 String name = (String) tagNode.getProperty("name", null);392 String uri = (String) tagNode.getProperty("uri", null);393 String id = (String) tagNode.getProperty("id", null);394 395 return new Tag(id, uri, name); 396 397 } 398 457 if (tagNode == null) return null; 458 String name = (String) tagNode.getProperty("name", null); 459 String uri = (String) tagNode.getProperty("uri", null); 460 String id = (String) tagNode.getProperty("id", null); 461 462 return new Tag(id, uri, name); 463 464 } 399 465 400 466 /** … … 512 578 for (String tag : newTags) { 513 579 // create new tag 514 Node tagNode = getOrCreateTagNode(new Tag(null, null,tag));580 Node tagNode = getOrCreateTagNode(new Tag(null, null, tag)); 515 581 getOrCreateRelation(annotNode, RelationTypes.HAS_TAG, tagNode); 516 582 } … … 534 600 * @param id 535 601 */ 536 public void delete ById(String id) {602 public void deleteAnnotationById(String id) { 537 603 Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle(); 538 604 if (annotNode != null) { … … 542 608 for (Relationship rel : annotNode.getRelationships()) { 543 609 // delete relation and the related node if it has no other 544 // relations 610 // relations and is not permanent 545 611 Node other = rel.getOtherNode(annotNode); 546 612 rel.delete(); 547 if (! other.hasRelationship()) {613 if (!(other.hasRelationship() || permanentNodeTypes.contains(other.getProperty("TYPE", null)))) { 548 614 deleteNode(other); 549 615 } … … 570 636 * @return 571 637 */ 572 public List<Annotation> search ByUriUser(String targetUri, String userUri, String limit, String offset) {638 public List<Annotation> searchAnnotationByUriUser(String targetUri, String userUri, String limit, String offset) { 573 639 List<Annotation> annotations = new ArrayList<Annotation>(); 574 640 if (targetUri != null) { … … 693 759 return person; 694 760 } 695 761 696 762 protected Node getOrCreateActorNode(Actor actor) { 697 763 // Person/Group is identified by URI or id … … 746 812 tag.setProperty("name", tagname); 747 813 idx.add(tag, "name", tagname); 748 814 749 815 tag.setProperty("id", inTag.getId()); 750 816 tag.setProperty("uri", inTag.getUri()); 751 817 idx.add(tag, "uri", inTag.getUri()); 752 818 753 819 tx.success(); 754 820 } finally { … … 854 920 } 855 921 856 public List<Annotation> getAnnotationsByTag(String tagUri) { 857 858 ArrayList<Annotation> ret = new ArrayList<Annotation>(); 859 Node tag = getTagNodeByUri(tagUri); 860 861 862 Iterable<Relationship> rels = tag.getRelationships(Direction.INCOMING,RelationTypes.HAS_TAG); 863 864 for (Relationship rel:rels){ 865 Node node = rel.getStartNode(); 866 ret.add(createAnnotationFromNode(node)); 867 868 } 869 return ret; 870 } 922 public List<Annotation> getAnnotationsByTag(String tagUri) { 923 924 ArrayList<Annotation> ret = new ArrayList<Annotation>(); 925 Node tag = getTagNodeByUri(tagUri); 926 927 Iterable<Relationship> rels = tag.getRelationships(Direction.INCOMING, RelationTypes.HAS_TAG); 928 929 for (Relationship rel : rels) { 930 Node node = rel.getStartNode(); 931 ret.add(createAnnotationFromNode(node)); 932 933 } 934 return ret; 935 } 871 936 872 937 }
Note: See TracChangeset
for help on using the changeset viewer.