# HG changeset patch # User casties # Date 1348668766 -7200 # Node ID aa2bb7ac04d9130c2a12d6f01897748f51649f86 # Parent 5d4260344db581a98aa197f1c69e684796d5aad4 more work on resources and targets. diff -r 5d4260344db5 -r aa2bb7ac04d9 src/main/java/de/mpiwg/itgroup/annotations/Resource.java --- /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); + } + +} diff -r 5d4260344db5 -r aa2bb7ac04d9 src/main/java/de/mpiwg/itgroup/annotations/Target.java --- /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); + } + +} diff -r 5d4260344db5 -r aa2bb7ac04d9 src/main/java/de/mpiwg/itgroup/annotations/Uri.java --- /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; + } +} diff -r 5d4260344db5 -r aa2bb7ac04d9 src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java --- 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 getActors(String key, String query, NodeTypes type) { - ArrayList actors = new ArrayList(); + /** + * 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 List getActors(String key, String query, NodeTypes type) { + ArrayList actors = new ArrayList(); Index idx = getNodeIndex(type); if (key == null) { key = "uri"; @@ -101,34 +129,81 @@ IndexHits 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 getGroups(String key, String query) { - ArrayList groups = new ArrayList(); - Index idx = getNodeIndex(NodeTypes.GROUP); + List groups = getActors(key, query, NodeTypes.GROUP); + return groups; + } + + /** + * Returns list of Persons. Key has to be indexed. + * @param key + * @param query + * @return + */ + public List getPersons(String key, String query) { + List 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 List getUris(String key, String query, NodeTypes type) { + ArrayList uris = new ArrayList(); + Index idx = getNodeIndex(type); if (key == null) { key = "uri"; query = "*"; } - IndexHits groupNodes = idx.query(key, query); - for (Node groupNode : groupNodes) { - Actor group = createActorFromNode(groupNode); - groups.add((Group) group); + IndexHits 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 getTargets(String key, String query) { + List targets = getUris(key, query, NodeTypes.TARGET); + return targets; + } + + /** + * Returns list of Resources. Key has to be indexed. + * @param key + * @param query + * @return + */ + public List getResources(String key, String query) { + List 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 getAnnotationsByResource(String resourceUri) { + ArrayList ret = new ArrayList(); + Node tag = getNodeFromIndex("uri", resourceUri, NodeTypes.RESOURCE); + Iterable rels = tag.getRelationships(Direction.INCOMING, RelationTypes.ANNOTATES); + for (Relationship rel : rels) { + Node node = rel.getStartNode(); + ret.add(createAnnotationFromNode(node)); + } + return ret; + } + }