Mercurial > hg > AnnotationManagerN4J
diff src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java @ 15:58357a4b86de
ASSIGNED - # 249: Annotations shared in groups
https://it-dev.mpiwg-berlin.mpg.de/tracs/mpdl-project-software/ticket/249
author | casties |
---|---|
date | Tue, 28 Aug 2012 20:23:12 +0200 |
parents | 629e15b345aa |
children | 794077e6288c |
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java Fri Jul 13 20:41:02 2012 +0200 +++ b/src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java Tue Aug 28 20:23:12 2012 +0200 @@ -34,13 +34,13 @@ protected GraphDatabaseService graphDb; public static enum NodeTypes { - ANNOTATION, PERSON, TARGET + ANNOTATION, PERSON, TARGET, GROUP } protected List<Index<Node>> nodeIndexes; public static enum RelationTypes implements RelationshipType { - ANNOTATES, CREATED, PERMITS_ADMIN, PERMITS_DELETE, PERMITS_UPDATE, PERMITS_READ + ANNOTATES, CREATED, PERMITS_ADMIN, PERMITS_DELETE, PERMITS_UPDATE, PERMITS_READ, MEMBER_OF } public static String ANNOTATION_URI_BASE = "http://entities.mpiwg-berlin.mpg.de/annotations/"; @@ -53,6 +53,7 @@ nodeIndexes.add(NodeTypes.ANNOTATION.ordinal(), graphDb.index().forNodes("annotations")); nodeIndexes.add(NodeTypes.PERSON.ordinal(), graphDb.index().forNodes("persons")); nodeIndexes.add(NodeTypes.TARGET.ordinal(), graphDb.index().forNodes("targets")); + nodeIndexes.add(NodeTypes.GROUP.ordinal(), graphDb.index().forNodes("groups")); } protected Index<Node> getNodeIndex(NodeTypes type) { @@ -60,6 +61,60 @@ } /** + * @param userUri + * @return + */ + public Node getPersonNodeByUri(String userUri) { + if (userUri == null) return null; + Node person = getNodeIndex(NodeTypes.PERSON).get("uri", userUri).getSingle(); + return person; + } + + + /** + * Returns List of Groups the person is member of. + * + * @param person + * @return + */ + public List<Group> getGroupsForPersonNode(Node person) { + ArrayList<Group> groups = new ArrayList<Group>(); + Iterable<Relationship> rels = person.getRelationships(RelationTypes.MEMBER_OF); + for (Relationship rel : rels) { + Node groupNode = rel.getEndNode(); + Actor group = createActorFromNode(groupNode); + // make sure we're getting a group + if (!(group instanceof Group)) { + logger.error("target of MEMBER_OF is not GROUP! rel="+rel); + continue; + } + groups.add((Group) group); + } + return groups; + } + + /** + * Returns if person with uri is in Group group. + * + * @param person + * @param group + * @return + */ + public boolean isPersonInGroup(Person person, Group group) { + Node pn = getPersonNodeByUri(person.getUriString()); + if (pn == null) return false; + // optimised version of getGroupsForPersonNode + Iterable<Relationship> rels = pn.getRelationships(RelationTypes.MEMBER_OF); + for (Relationship rel : rels) { + Node gn = rel.getEndNode(); + if (gn.getProperty("uri", "").equals(group.getUriString()) || gn.getProperty("id", "").equals(group.getId())) { + return true; + } + } + return false; + } + + /** * Returns the Annotation with the given id. * * @param id @@ -142,13 +197,14 @@ * @return */ protected Actor createActorFromNode(Node actorNode) { + String id = (String) actorNode.getProperty("id", null); String uri = (String) actorNode.getProperty("uri", null); String name = (String) actorNode.getProperty("name", null); String type = (String) actorNode.getProperty("TYPE", null); if (type != null && type.equals("PERSON")) { - return new Person(uri, name); + return new Person(id, uri, name); } else if (type != null && type.equals("GROUP")) { - return new Group(uri, name); + return new Group(id, uri, name); } return null; } @@ -209,7 +265,7 @@ */ Actor creator = annot.getCreator(); if (creator != null) { - Node creatorNode = getOrCreatePersonNode(creator); + Node creatorNode = getOrCreateActorNode(creator); getOrCreateRelation(creatorNode, RelationTypes.CREATED, annotNode); } @@ -300,7 +356,7 @@ } if (userUri != null) { // there should be only one - Node person = getNodeIndex(NodeTypes.PERSON).get("uri", userUri).getSingle(); + Node person = getPersonNodeByUri(userUri); if (person != null) { Iterable<Relationship> relations = person.getRelationships(RelationTypes.CREATED); for (Relationship relation : relations) { @@ -380,11 +436,17 @@ return target; } - protected Node getOrCreatePersonNode(Actor actor) { - // Person is identified by URI + protected Node getOrCreateActorNode(Actor actor) { + // Person/Group is identified by URI or id String uri = actor.getUriString(); String name = actor.getName(); - Index<Node> idx = getNodeIndex(NodeTypes.PERSON); + String id = actor.getId(); + Index<Node> idx; + if (actor.isGroup()) { + idx = getNodeIndex(NodeTypes.GROUP); + } else { + idx = getNodeIndex(NodeTypes.PERSON); + } IndexHits<Node> persons = idx.get("uri", uri); Node person = persons.getSingle(); if (person == null) { @@ -392,12 +454,19 @@ Transaction tx = graphDb.beginTx(); try { person = graphDb.createNode(); - person.setProperty("TYPE", NodeTypes.PERSON.name()); + if (actor.isGroup()) { + person.setProperty("TYPE", NodeTypes.GROUP.name()); + } else { + person.setProperty("TYPE", NodeTypes.PERSON.name()); + } person.setProperty("uri", uri); idx.add(person, "uri", uri); if (name != null) { person.setProperty("name", name); } + if (id != null) { + person.setProperty("id", id); + } tx.success(); } finally { tx.finish(); @@ -416,7 +485,7 @@ protected void setPermissionRelation(Node annotNode, RelationTypes type, Actor actor) { Node newActorNode = null; if (actor != null) { - newActorNode = getOrCreatePersonNode(actor); + newActorNode = getOrCreateActorNode(actor); } Relationship rel = getRelation(annotNode, type, null); if (rel != null) { @@ -463,6 +532,13 @@ } } + /** returns the (first) Relationship of RelationTypes type from Node start. + * + * @param start + * @param type + * @param direction + * @return + */ protected Relationship getRelation(Node start, RelationTypes type, Direction direction) { Iterable<Relationship> rels; if (direction == null) {