changeset 42:aa2bb7ac04d9

more work on resources and targets.
author casties
date Wed, 26 Sep 2012 16:12:46 +0200
parents 5d4260344db5
children d1bef7952bec
files src/main/java/de/mpiwg/itgroup/annotations/Resource.java src/main/java/de/mpiwg/itgroup/annotations/Target.java src/main/java/de/mpiwg/itgroup/annotations/Uri.java src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java
diffstat 4 files changed, 172 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/mpiwg/itgroup/annotations/Resource.java	Wed Sep 26 16:12:46 2012 +0200
@@ -0,0 +1,19 @@
+/**
+ * 
+ */
+package de.mpiwg.itgroup.annotations;
+
+/**
+ * @author casties
+ *
+ */
+public class Resource extends Uri {
+
+    /**
+     * @param uri
+     */
+    public Resource(String uri) {
+        super(uri);
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/mpiwg/itgroup/annotations/Target.java	Wed Sep 26 16:12:46 2012 +0200
@@ -0,0 +1,19 @@
+/**
+ * 
+ */
+package de.mpiwg.itgroup.annotations;
+
+/**
+ * @author casties
+ *
+ */
+public class Target extends Uri {
+
+    /**
+     * @param uri
+     */
+    public Target(String uri) {
+        super(uri);
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/mpiwg/itgroup/annotations/Uri.java	Wed Sep 26 16:12:46 2012 +0200
@@ -0,0 +1,22 @@
+package de.mpiwg.itgroup.annotations;
+
+/**
+ * @author casties
+ * 
+ */
+public class Uri {
+
+    public String uri;
+
+    public Uri(String uri) {
+        this.uri = uri;
+    }
+
+    public String getUri() {
+        return uri;
+    }
+
+    public void setUri(String uri) {
+        this.uri = uri;
+    }
+}
--- a/src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java	Wed Sep 26 14:59:00 2012 +0200
+++ b/src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java	Wed Sep 26 16:12:46 2012 +0200
@@ -25,7 +25,10 @@
 import de.mpiwg.itgroup.annotations.Annotation.FragmentTypes;
 import de.mpiwg.itgroup.annotations.Group;
 import de.mpiwg.itgroup.annotations.Person;
+import de.mpiwg.itgroup.annotations.Resource;
 import de.mpiwg.itgroup.annotations.Tag;
+import de.mpiwg.itgroup.annotations.Target;
+import de.mpiwg.itgroup.annotations.Uri;
 
 /**
  * Neo4J based Annotation store.
@@ -91,8 +94,33 @@
         return person;
     }
 
-    public List<Actor> getActors(String key, String query, NodeTypes type) {
-        ArrayList<Actor> actors = new ArrayList<Actor>();
+    /**
+     * Returns the Node with the given key and value.
+     * Key has to be indexed.
+     * 
+     * @param key
+     * @param value
+     * @param type
+     * @return
+     */
+    public Node getNodeFromIndex(String key, String value, NodeTypes type) {
+        if (key == null || value == null) return null;
+        Node person = getNodeIndex(type).get(key, value).getSingle();
+        return person;
+    }
+
+    /**
+     * Returns list of Actors of given type (Group or Person).
+     * Key has to be indexed.
+     * 
+     * @param key
+     * @param query
+     * @param type
+     * @return
+     */
+    @SuppressWarnings("unchecked")
+    public <T extends Actor> List<T> getActors(String key, String query, NodeTypes type) {
+        ArrayList<T> actors = new ArrayList<T>();
         Index<Node> idx = getNodeIndex(type);
         if (key == null) {
             key = "uri";
@@ -101,34 +129,81 @@
         IndexHits<Node> actorNodes = idx.query(key, query);
         for (Node actorNode : actorNodes) {
             Actor actor = createActorFromNode(actorNode);
-            actors.add(actor);
+            actors.add((T) actor);
         }
         return actors;
     }
-
+    
     /**
-     * Returns List of Groups.
-     * Key has to be indexed.
-     * 
+     * Returns list of groups. Key has to be indexed.
      * @param key
      * @param query
      * @return
      */
     public List<Group> getGroups(String key, String query) {
-        ArrayList<Group> groups = new ArrayList<Group>();
-        Index<Node> idx = getNodeIndex(NodeTypes.GROUP);
+        List<Group> groups = getActors(key, query, NodeTypes.GROUP);
+        return groups;
+    }
+    
+    /**
+     * Returns list of Persons. Key has to be indexed.
+     * @param key
+     * @param query
+     * @return
+     */
+    public List<Person> getPersons(String key, String query) {
+        List<Person> persons = getActors(key, query, NodeTypes.PERSON);
+        return persons;
+    }
+    
+    /**
+     * Returns list of uri-like objects of given type (Target or Resource).
+     * Key has to be indexed.
+     * 
+     * @param key
+     * @param query
+     * @param type
+     * @return
+     */
+    @SuppressWarnings("unchecked")
+    public <T extends Uri> List<T> getUris(String key, String query, NodeTypes type) {
+        ArrayList<T> uris = new ArrayList<T>();
+        Index<Node> idx = getNodeIndex(type);
         if (key == null) {
             key = "uri";
             query = "*";
         }
-        IndexHits<Node> groupNodes = idx.query(key, query);
-        for (Node groupNode : groupNodes) {
-            Actor group = createActorFromNode(groupNode);
-            groups.add((Group) group);
+        IndexHits<Node> actorNodes = idx.query(key, query);
+        for (Node actorNode : actorNodes) {
+            Uri uri = createUriFromNode(actorNode);
+            uris.add((T) uri);
         }
-        return groups;
+        return uris;
     }
 
+    
+    /**
+     * Returns list of Targets. Key has to be indexed.
+     * @param key
+     * @param query
+     * @return
+     */
+    public List<Target> getTargets(String key, String query) {
+        List<Target> targets = getUris(key, query, NodeTypes.TARGET);
+        return targets;
+    }
+    
+    /**
+     * Returns list of Resources. Key has to be indexed.
+     * @param key
+     * @param query
+     * @return
+     */
+    public List<Resource> getResources(String key, String query) {
+        List<Resource> targets = getUris(key, query, NodeTypes.RESOURCE);
+        return targets;
+    }
+    
     /**
      * Returns List of Annotations.
      * Key has to be indexed.
@@ -491,6 +566,18 @@
 
     }
 
+    protected Uri createUriFromNode(Node uriNode) {
+        if (uriNode == null) return null;
+        String uri = (String) uriNode.getProperty("uri", null);
+        String type = (String) uriNode.getProperty("TYPE", null);
+        if (type != null && type.equals("TARGET")) {
+            return new Target(uri);
+        } else if (type != null && type.equals("RESOURCE")) {
+            return new Resource(uri);
+        }
+        return null;
+    }
+
     /**
      * Store a new annotation in the store or update an existing one. Returns
      * the stored annotation.
@@ -980,4 +1067,15 @@
         return ret;
     }
 
+    public List<Annotation> getAnnotationsByResource(String resourceUri) {
+        ArrayList<Annotation> ret = new ArrayList<Annotation>();
+        Node tag = getNodeFromIndex("uri", resourceUri, NodeTypes.RESOURCE);
+        Iterable<Relationship> rels = tag.getRelationships(Direction.INCOMING, RelationTypes.ANNOTATES);
+        for (Relationship rel : rels) {
+            Node node = rel.getStartNode();
+            ret.add(createAnnotationFromNode(node));
+        }
+        return ret;
+    }
+
 }