Changeset 42:aa2bb7ac04d9 in AnnotationManagerN4J for src


Ignore:
Timestamp:
Sep 26, 2012, 2:12:46 PM (12 years ago)
Author:
casties
Branch:
default
Message:

more work on resources and targets.

Location:
src/main/java/de/mpiwg/itgroup/annotations
Files:
3 added
1 edited

Legend:

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

    r40 r42  
    2626import de.mpiwg.itgroup.annotations.Group;
    2727import de.mpiwg.itgroup.annotations.Person;
     28import de.mpiwg.itgroup.annotations.Resource;
    2829import de.mpiwg.itgroup.annotations.Tag;
     30import de.mpiwg.itgroup.annotations.Target;
     31import de.mpiwg.itgroup.annotations.Uri;
    2932
    3033/**
     
    9295    }
    9396
    94     public List<Actor> getActors(String key, String query, NodeTypes type) {
    95         ArrayList<Actor> actors = new ArrayList<Actor>();
     97    /**
     98     * Returns the Node with the given key and value.
     99     * Key has to be indexed.
     100     *
     101     * @param key
     102     * @param value
     103     * @param type
     104     * @return
     105     */
     106    public Node getNodeFromIndex(String key, String value, NodeTypes type) {
     107        if (key == null || value == null) return null;
     108        Node person = getNodeIndex(type).get(key, value).getSingle();
     109        return person;
     110    }
     111
     112    /**
     113     * Returns list of Actors of given type (Group or Person).
     114     * Key has to be indexed.
     115     *
     116     * @param key
     117     * @param query
     118     * @param type
     119     * @return
     120     */
     121    @SuppressWarnings("unchecked")
     122    public <T extends Actor> List<T> getActors(String key, String query, NodeTypes type) {
     123        ArrayList<T> actors = new ArrayList<T>();
    96124        Index<Node> idx = getNodeIndex(type);
    97125        if (key == null) {
     
    102130        for (Node actorNode : actorNodes) {
    103131            Actor actor = createActorFromNode(actorNode);
    104             actors.add(actor);
     132            actors.add((T) actor);
    105133        }
    106134        return actors;
    107135    }
    108 
    109     /**
    110      * Returns List of Groups.
    111      * Key has to be indexed.
    112      *
     136   
     137    /**
     138     * Returns list of groups. Key has to be indexed.
    113139     * @param key
    114140     * @param query
     
    116142     */
    117143    public List<Group> getGroups(String key, String query) {
    118         ArrayList<Group> groups = new ArrayList<Group>();
    119         Index<Node> idx = getNodeIndex(NodeTypes.GROUP);
     144        List<Group> groups = getActors(key, query, NodeTypes.GROUP);
     145        return groups;
     146    }
     147   
     148    /**
     149     * Returns list of Persons. Key has to be indexed.
     150     * @param key
     151     * @param query
     152     * @return
     153     */
     154    public List<Person> getPersons(String key, String query) {
     155        List<Person> persons = getActors(key, query, NodeTypes.PERSON);
     156        return persons;
     157    }
     158   
     159    /**
     160     * Returns list of uri-like objects of given type (Target or Resource).
     161     * Key has to be indexed.
     162     *
     163     * @param key
     164     * @param query
     165     * @param type
     166     * @return
     167     */
     168    @SuppressWarnings("unchecked")
     169    public <T extends Uri> List<T> getUris(String key, String query, NodeTypes type) {
     170        ArrayList<T> uris = new ArrayList<T>();
     171        Index<Node> idx = getNodeIndex(type);
    120172        if (key == null) {
    121173            key = "uri";
    122174            query = "*";
    123175        }
    124         IndexHits<Node> groupNodes = idx.query(key, query);
    125         for (Node groupNode : groupNodes) {
    126             Actor group = createActorFromNode(groupNode);
    127             groups.add((Group) group);
    128         }
    129         return groups;
    130     }
    131 
     176        IndexHits<Node> actorNodes = idx.query(key, query);
     177        for (Node actorNode : actorNodes) {
     178            Uri uri = createUriFromNode(actorNode);
     179            uris.add((T) uri);
     180        }
     181        return uris;
     182    }
     183
     184   
     185    /**
     186     * Returns list of Targets. Key has to be indexed.
     187     * @param key
     188     * @param query
     189     * @return
     190     */
     191    public List<Target> getTargets(String key, String query) {
     192        List<Target> targets = getUris(key, query, NodeTypes.TARGET);
     193        return targets;
     194    }
     195   
     196    /**
     197     * Returns list of Resources. Key has to be indexed.
     198     * @param key
     199     * @param query
     200     * @return
     201     */
     202    public List<Resource> getResources(String key, String query) {
     203        List<Resource> targets = getUris(key, query, NodeTypes.RESOURCE);
     204        return targets;
     205    }
     206   
    132207    /**
    133208     * Returns List of Annotations.
     
    490565        return new Tag(id, uri, name);
    491566
     567    }
     568
     569    protected Uri createUriFromNode(Node uriNode) {
     570        if (uriNode == null) return null;
     571        String uri = (String) uriNode.getProperty("uri", null);
     572        String type = (String) uriNode.getProperty("TYPE", null);
     573        if (type != null && type.equals("TARGET")) {
     574            return new Target(uri);
     575        } else if (type != null && type.equals("RESOURCE")) {
     576            return new Resource(uri);
     577        }
     578        return null;
    492579    }
    493580
     
    9811068    }
    9821069
     1070    public List<Annotation> getAnnotationsByResource(String resourceUri) {
     1071        ArrayList<Annotation> ret = new ArrayList<Annotation>();
     1072        Node tag = getNodeFromIndex("uri", resourceUri, NodeTypes.RESOURCE);
     1073        Iterable<Relationship> rels = tag.getRelationships(Direction.INCOMING, RelationTypes.ANNOTATES);
     1074        for (Relationship rel : rels) {
     1075            Node node = rel.getStartNode();
     1076            ret.add(createAnnotationFromNode(node));
     1077        }
     1078        return ret;
     1079    }
     1080
    9831081}
Note: See TracChangeset for help on using the changeset viewer.