Changeset 15:58357a4b86de 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
r14 r15 35 35 36 36 public static enum NodeTypes { 37 ANNOTATION, PERSON, TARGET 37 ANNOTATION, PERSON, TARGET, GROUP 38 38 } 39 39 … … 41 41 42 42 public static enum RelationTypes implements RelationshipType { 43 ANNOTATES, CREATED, PERMITS_ADMIN, PERMITS_DELETE, PERMITS_UPDATE, PERMITS_READ 43 ANNOTATES, CREATED, PERMITS_ADMIN, PERMITS_DELETE, PERMITS_UPDATE, PERMITS_READ, MEMBER_OF 44 44 } 45 45 … … 54 54 nodeIndexes.add(NodeTypes.PERSON.ordinal(), graphDb.index().forNodes("persons")); 55 55 nodeIndexes.add(NodeTypes.TARGET.ordinal(), graphDb.index().forNodes("targets")); 56 nodeIndexes.add(NodeTypes.GROUP.ordinal(), graphDb.index().forNodes("groups")); 56 57 } 57 58 … … 60 61 } 61 62 63 /** 64 * @param userUri 65 * @return 66 */ 67 public Node getPersonNodeByUri(String userUri) { 68 if (userUri == null) return null; 69 Node person = getNodeIndex(NodeTypes.PERSON).get("uri", userUri).getSingle(); 70 return person; 71 } 72 73 74 /** 75 * Returns List of Groups the person is member of. 76 * 77 * @param person 78 * @return 79 */ 80 public List<Group> getGroupsForPersonNode(Node person) { 81 ArrayList<Group> groups = new ArrayList<Group>(); 82 Iterable<Relationship> rels = person.getRelationships(RelationTypes.MEMBER_OF); 83 for (Relationship rel : rels) { 84 Node groupNode = rel.getEndNode(); 85 Actor group = createActorFromNode(groupNode); 86 // make sure we're getting a group 87 if (!(group instanceof Group)) { 88 logger.error("target of MEMBER_OF is not GROUP! rel="+rel); 89 continue; 90 } 91 groups.add((Group) group); 92 } 93 return groups; 94 } 95 96 /** 97 * Returns if person with uri is in Group group. 98 * 99 * @param person 100 * @param group 101 * @return 102 */ 103 public boolean isPersonInGroup(Person person, Group group) { 104 Node pn = getPersonNodeByUri(person.getUriString()); 105 if (pn == null) return false; 106 // optimised version of getGroupsForPersonNode 107 Iterable<Relationship> rels = pn.getRelationships(RelationTypes.MEMBER_OF); 108 for (Relationship rel : rels) { 109 Node gn = rel.getEndNode(); 110 if (gn.getProperty("uri", "").equals(group.getUriString()) || gn.getProperty("id", "").equals(group.getId())) { 111 return true; 112 } 113 } 114 return false; 115 } 116 62 117 /** 63 118 * Returns the Annotation with the given id. … … 143 198 */ 144 199 protected Actor createActorFromNode(Node actorNode) { 200 String id = (String) actorNode.getProperty("id", null); 145 201 String uri = (String) actorNode.getProperty("uri", null); 146 202 String name = (String) actorNode.getProperty("name", null); 147 203 String type = (String) actorNode.getProperty("TYPE", null); 148 204 if (type != null && type.equals("PERSON")) { 149 return new Person( uri, name);205 return new Person(id, uri, name); 150 206 } else if (type != null && type.equals("GROUP")) { 151 return new Group( uri, name);207 return new Group(id, uri, name); 152 208 } 153 209 return null; … … 210 266 Actor creator = annot.getCreator(); 211 267 if (creator != null) { 212 Node creatorNode = getOrCreate PersonNode(creator);268 Node creatorNode = getOrCreateActorNode(creator); 213 269 getOrCreateRelation(creatorNode, RelationTypes.CREATED, annotNode); 214 270 } … … 301 357 if (userUri != null) { 302 358 // there should be only one 303 Node person = get NodeIndex(NodeTypes.PERSON).get("uri", userUri).getSingle();359 Node person = getPersonNodeByUri(userUri); 304 360 if (person != null) { 305 361 Iterable<Relationship> relations = person.getRelationships(RelationTypes.CREATED); … … 381 437 } 382 438 383 protected Node getOrCreate PersonNode(Actor actor) {384 // Person is identified by URI439 protected Node getOrCreateActorNode(Actor actor) { 440 // Person/Group is identified by URI or id 385 441 String uri = actor.getUriString(); 386 442 String name = actor.getName(); 387 Index<Node> idx = getNodeIndex(NodeTypes.PERSON); 443 String id = actor.getId(); 444 Index<Node> idx; 445 if (actor.isGroup()) { 446 idx = getNodeIndex(NodeTypes.GROUP); 447 } else { 448 idx = getNodeIndex(NodeTypes.PERSON); 449 } 388 450 IndexHits<Node> persons = idx.get("uri", uri); 389 451 Node person = persons.getSingle(); … … 393 455 try { 394 456 person = graphDb.createNode(); 395 person.setProperty("TYPE", NodeTypes.PERSON.name()); 457 if (actor.isGroup()) { 458 person.setProperty("TYPE", NodeTypes.GROUP.name()); 459 } else { 460 person.setProperty("TYPE", NodeTypes.PERSON.name()); 461 } 396 462 person.setProperty("uri", uri); 397 463 idx.add(person, "uri", uri); … … 399 465 person.setProperty("name", name); 400 466 } 467 if (id != null) { 468 person.setProperty("id", id); 469 } 401 470 tx.success(); 402 471 } finally { … … 417 486 Node newActorNode = null; 418 487 if (actor != null) { 419 newActorNode = getOrCreate PersonNode(actor);488 newActorNode = getOrCreateActorNode(actor); 420 489 } 421 490 Relationship rel = getRelation(annotNode, type, null); … … 464 533 } 465 534 535 /** returns the (first) Relationship of RelationTypes type from Node start. 536 * 537 * @param start 538 * @param type 539 * @param direction 540 * @return 541 */ 466 542 protected Relationship getRelation(Node start, RelationTypes type, Direction direction) { 467 543 Iterable<Relationship> rels;
Note: See TracChangeset
for help on using the changeset viewer.