Ignore:
Timestamp:
Aug 28, 2012, 6:23:12 PM (12 years ago)
Author:
casties
Branch:
default
Message:

ASSIGNED - # 249: Annotations shared in groups
https://it-dev.mpiwg-berlin.mpg.de/tracs/mpdl-project-software/ticket/249

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java

    r14 r15  
    3535
    3636    public static enum NodeTypes {
    37         ANNOTATION, PERSON, TARGET
     37        ANNOTATION, PERSON, TARGET, GROUP
    3838    }
    3939
     
    4141
    4242    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
    4444    }
    4545
     
    5454        nodeIndexes.add(NodeTypes.PERSON.ordinal(), graphDb.index().forNodes("persons"));
    5555        nodeIndexes.add(NodeTypes.TARGET.ordinal(), graphDb.index().forNodes("targets"));
     56        nodeIndexes.add(NodeTypes.GROUP.ordinal(), graphDb.index().forNodes("groups"));
    5657    }
    5758
     
    6061    }
    6162
     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   
    62117    /**
    63118     * Returns the Annotation with the given id.
     
    143198     */
    144199    protected Actor createActorFromNode(Node actorNode) {
     200        String id = (String) actorNode.getProperty("id", null);
    145201        String uri = (String) actorNode.getProperty("uri", null);
    146202        String name = (String) actorNode.getProperty("name", null);
    147203        String type = (String) actorNode.getProperty("TYPE", null);
    148204        if (type != null && type.equals("PERSON")) {
    149             return new Person(uri, name);
     205            return new Person(id, uri, name);
    150206        } else if (type != null && type.equals("GROUP")) {
    151             return new Group(uri, name);
     207            return new Group(id, uri, name);
    152208        }
    153209        return null;
     
    210266            Actor creator = annot.getCreator();
    211267            if (creator != null) {
    212                 Node creatorNode = getOrCreatePersonNode(creator);
     268                Node creatorNode = getOrCreateActorNode(creator);
    213269                getOrCreateRelation(creatorNode, RelationTypes.CREATED, annotNode);
    214270            }
     
    301357        if (userUri != null) {
    302358            // there should be only one
    303             Node person = getNodeIndex(NodeTypes.PERSON).get("uri", userUri).getSingle();
     359            Node person = getPersonNodeByUri(userUri);
    304360            if (person != null) {
    305361                Iterable<Relationship> relations = person.getRelationships(RelationTypes.CREATED);
     
    381437    }
    382438
    383     protected Node getOrCreatePersonNode(Actor actor) {
    384         // Person is identified by URI
     439    protected Node getOrCreateActorNode(Actor actor) {
     440        // Person/Group is identified by URI or id
    385441        String uri = actor.getUriString();
    386442        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        }
    388450        IndexHits<Node> persons = idx.get("uri", uri);
    389451        Node person = persons.getSingle();
     
    393455            try {
    394456                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                }
    396462                person.setProperty("uri", uri);
    397463                idx.add(person, "uri", uri);
     
    399465                    person.setProperty("name", name);
    400466                }
     467                if (id != null) {
     468                    person.setProperty("id", id);
     469                }
    401470                tx.success();
    402471            } finally {
     
    417486        Node newActorNode = null;
    418487        if (actor != null) {
    419             newActorNode = getOrCreatePersonNode(actor);
     488            newActorNode = getOrCreateActorNode(actor);
    420489        }
    421490        Relationship rel = getRelation(annotNode, type, null);
     
    464533    }
    465534
     535    /** returns the (first) Relationship of RelationTypes type from Node start.
     536     * 
     537     * @param start
     538     * @param type
     539     * @param direction
     540     * @return
     541     */
    466542    protected Relationship getRelation(Node start, RelationTypes type, Direction direction) {
    467543        Iterable<Relationship> rels;
Note: See TracChangeset for help on using the changeset viewer.