changeset 68:39bc52f9b102

(hopefully) fixed issues with neo4j 2.0 transactions.
author casties
date Sat, 22 Feb 2014 07:12:37 -0800
parents 875a97f8b8da
children 9d3885d1681e
files src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorRestlet.java src/main/webapp/annotationBrowser/js/annotation.js
diffstat 3 files changed, 255 insertions(+), 249 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java	Fri Feb 21 22:19:23 2014 +0100
+++ b/src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java	Sat Feb 22 07:12:37 2014 -0800
@@ -62,7 +62,7 @@
         this.graphDb = graphDb;
         nodeIndexes = new ArrayList<Index<Node>>(5);
         // List.set(enum.ordinal(), val) seems not to work.
-        try ( Transaction tx = graphDb.beginTx() ) {
+        try (Transaction tx = graphDb.beginTx()) {
             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"));
@@ -118,7 +118,8 @@
      * @return
      */
     public Node getNodeFromIndex(String key, String value, NodeTypes type) {
-        if (key == null || value == null) return null;
+        if (key == null || value == null)
+            return null;
         Node node = null;
         try (Transaction tx = graphDb.beginTx()) {
             node = getNodeIndex(type).get(key, value).getSingle();
@@ -150,7 +151,7 @@
                 Actor actor = createActorFromNode(actorNode);
                 actors.add((T) actor);
             }
-            tx.success();
+            tx.success();            
         }
         return actors;
     }
@@ -312,18 +313,20 @@
      */
     public boolean isPersonInGroup(Person person, Group group) {
         Node pn = getPersonNodeByUri(person.getUriString());
-        if (pn == null) return false;
+        if (pn == null)
+            return false;
         // optimized version of getGroupsForPersonNode
         try (Transaction tx = graphDb.beginTx()) {
-        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())) {
-                tx.success();
-                return true;
+            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())) {
+                    tx.success();
+                    return true;
+                }
             }
-        }
-        tx.success();
+            tx.success();
         }
         return false;
     }
@@ -381,12 +384,9 @@
         for (Relationship rel : rels) {
             Node mn = rel.getStartNode();
             if (mn.equals(pn)) {
-                Transaction tx = graphDb.beginTx();
-                try {
+                try (Transaction tx = graphDb.beginTx()) {
                     rel.delete();
                     tx.success();
-                } finally {
-                    tx.finish();
                 }
                 // there should be only one
                 break;
@@ -415,8 +415,7 @@
      */
     public Actor storeActor(Actor actor) {
         Node actorNode = getOrCreateActorNode(actor);
-        Transaction tx = graphDb.beginTx();
-        try {
+        try (Transaction tx = graphDb.beginTx()) {
             // id
             String id = actor.getId();
             if (id != null) {
@@ -433,8 +432,6 @@
                 actorNode.setProperty("uri", uri);
             }
             tx.success();
-        } finally {
-            tx.finish();
         }
         Actor storedActor = createActorFromNode(actorNode);
         return storedActor;
@@ -456,8 +453,7 @@
         Node actorNode = idx.get("uri", uri).getSingle();
         if (actorNode != null) {
             // delete relations
-            Transaction tx = graphDb.beginTx();
-            try {
+            try (Transaction tx = graphDb.beginTx()) {
                 for (Relationship rel : actorNode.getRelationships()) {
                     rel.delete();
                 }
@@ -468,8 +464,6 @@
                     logger.error("deleteActor: unable to delete: Node still has relations.");
                 }
                 tx.success();
-            } finally {
-                tx.finish();
             }
         }
     }
@@ -482,10 +476,10 @@
      */
     public Annotation getAnnotationById(String id) {
         Annotation annot = null;
-        try ( Transaction tx = graphDb.beginTx() ) {
-        Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle();
-        annot = createAnnotationFromNode(annotNode);
-        tx.success();
+        try (Transaction tx = graphDb.beginTx()) {
+            Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle();
+            annot = createAnnotationFromNode(annotNode);
+            tx.success();
         }
         return annot;
     }
@@ -498,86 +492,86 @@
      */
     public Annotation createAnnotationFromNode(Node annotNode) {
         Annotation annot = new Annotation();
-        try ( Transaction tx = graphDb.beginTx() ) {        
-        annot.setUri((String) annotNode.getProperty("id", null));
-        annot.setBodyText((String) annotNode.getProperty("bodyText", null));
-        annot.setBodyUri((String) annotNode.getProperty("bodyUri", null));
-        /*
-         * get annotation target and resource from relation
-         */
-        for (Relationship rel : annotNode.getRelationships(RelationTypes.ANNOTATES)) {
-            Node target = rel.getEndNode();
-            String type = (String) target.getProperty("TYPE");
-            if (type.equals("TARGET")) {
-                annot.setTarget(new Target((String) target.getProperty("uri", null)));
-            } else if (type.equals("RESOURCE")) {
-                annot.setResource(new Resource((String) target.getProperty("uri", null)));
+        try (Transaction tx = graphDb.beginTx()) {
+            annot.setUri((String) annotNode.getProperty("id", null));
+            annot.setBodyText((String) annotNode.getProperty("bodyText", null));
+            annot.setBodyUri((String) annotNode.getProperty("bodyUri", null));
+            /*
+             * get annotation target and resource from relation
+             */
+            for (Relationship rel : annotNode.getRelationships(RelationTypes.ANNOTATES)) {
+                Node target = rel.getEndNode();
+                String type = (String) target.getProperty("TYPE");
+                if (type.equals("TARGET")) {
+                    annot.setTarget(new Target((String) target.getProperty("uri", null)));
+                } else if (type.equals("RESOURCE")) {
+                    annot.setResource(new Resource((String) target.getProperty("uri", null)));
+                }
+            }
+            if (annot.getTarget() == null) {
+                logger.warn("annotation " + annotNode + " has no target node!");
+            }
+            // get fragment from attribute
+            annot.setTargetFragment((String) annotNode.getProperty("targetFragment", null));
+            String ft = (String) annotNode.getProperty("fragmentType", null);
+            if (ft != null) {
+                annot.setFragmentType(FragmentTypes.valueOf(ft));
+            }
+            /*
+             * get creator from relation
+             */
+            Relationship creatorRel = getRelation(annotNode, RelationTypes.CREATED, null);
+            if (creatorRel != null) {
+                Node creatorNode = creatorRel.getStartNode();
+                Actor creator = createActorFromNode(creatorNode);
+                annot.setCreator(creator);
+            } else {
+                logger.warn("annotation " + annotNode + " has no creator node!");
             }
-        }
-        if (annot.getTarget() == null) {
-            logger.error("annotation " + annotNode + " has no target node!");
-        }
-        // get fragment from attribute
-        annot.setTargetFragment((String) annotNode.getProperty("targetFragment", null));
-        String ft = (String) annotNode.getProperty("fragmentType", null);
-        if (ft != null) {
-            annot.setFragmentType(FragmentTypes.valueOf(ft));
-        }
-        /*
-         * get creator from relation
-         */
-        Relationship creatorRel = getRelation(annotNode, RelationTypes.CREATED, null);
-        if (creatorRel != null) {
-            Node creatorNode = creatorRel.getStartNode();
-            Actor creator = createActorFromNode(creatorNode);
-            annot.setCreator(creator);
-        } else {
-            logger.error("annotation " + annotNode + " has no creator node!");
-        }
-        /*
-         * get creation date
-         */
-        annot.setCreated((String) annotNode.getProperty("created", null));
-        /*
-         * get permissions
-         */
-        Relationship adminRel = getRelation(annotNode, RelationTypes.PERMITS_ADMIN, null);
-        if (adminRel != null) {
-            Node adminNode = adminRel.getEndNode();
-            Actor admin = createActorFromNode(adminNode);
-            annot.setAdminPermission(admin);
-        }
-        Relationship deleteRel = getRelation(annotNode, RelationTypes.PERMITS_DELETE, null);
-        if (deleteRel != null) {
-            Node deleteNode = deleteRel.getEndNode();
-            Actor delete = createActorFromNode(deleteNode);
-            annot.setDeletePermission(delete);
-        }
-        Relationship updateRel = getRelation(annotNode, RelationTypes.PERMITS_UPDATE, null);
-        if (updateRel != null) {
-            Node updateNode = updateRel.getEndNode();
-            Actor update = createActorFromNode(updateNode);
-            annot.setUpdatePermission(update);
-        }
-        Relationship readRel = getRelation(annotNode, RelationTypes.PERMITS_READ, null);
-        if (readRel != null) {
-            Node readNode = readRel.getEndNode();
-            Actor read = createActorFromNode(readNode);
-            annot.setReadPermission(read);
-        }
-        /*
-         * get tags
-         */
-        Set<String> tags = new HashSet<String>();
-        for (Relationship rel : annotNode.getRelationships(RelationTypes.HAS_TAG)) {
-            String tag = (String) rel.getEndNode().getProperty("name", null);
-            if (tag != null) {
-                tags.add(tag);
+            /*
+             * get creation date
+             */
+            annot.setCreated((String) annotNode.getProperty("created", null));
+            /*
+             * get permissions
+             */
+            Relationship adminRel = getRelation(annotNode, RelationTypes.PERMITS_ADMIN, null);
+            if (adminRel != null) {
+                Node adminNode = adminRel.getEndNode();
+                Actor admin = createActorFromNode(adminNode);
+                annot.setAdminPermission(admin);
+            }
+            Relationship deleteRel = getRelation(annotNode, RelationTypes.PERMITS_DELETE, null);
+            if (deleteRel != null) {
+                Node deleteNode = deleteRel.getEndNode();
+                Actor delete = createActorFromNode(deleteNode);
+                annot.setDeletePermission(delete);
             }
-        }
-        annot.setTags(tags);
-        
-        tx.success();
+            Relationship updateRel = getRelation(annotNode, RelationTypes.PERMITS_UPDATE, null);
+            if (updateRel != null) {
+                Node updateNode = updateRel.getEndNode();
+                Actor update = createActorFromNode(updateNode);
+                annot.setUpdatePermission(update);
+            }
+            Relationship readRel = getRelation(annotNode, RelationTypes.PERMITS_READ, null);
+            if (readRel != null) {
+                Node readNode = readRel.getEndNode();
+                Actor read = createActorFromNode(readNode);
+                annot.setReadPermission(read);
+            }
+            /*
+             * get tags
+             */
+            Set<String> tags = new HashSet<String>();
+            for (Relationship rel : annotNode.getRelationships(RelationTypes.HAS_TAG)) {
+                String tag = (String) rel.getEndNode().getProperty("name", null);
+                if (tag != null) {
+                    tags.add(tag);
+                }
+            }
+            annot.setTags(tags);
+
+            tx.success();
         }
         return annot;
     }
@@ -589,25 +583,35 @@
      * @return
      */
     protected Actor createActorFromNode(Node actorNode) {
-        if (actorNode == null) return null;
-        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(id, uri, name);
-        } else if (type != null && type.equals("GROUP")) {
-            return new Group(id, uri, name);
+        if (actorNode == null)
+            return null;
+        try (Transaction tx = graphDb.beginTx()) {
+            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);
+            tx.success();
+            if (type != null && type.equals("PERSON")) {
+                return new Person(id, uri, name);
+            } else if (type != null && type.equals("GROUP")) {
+                return new Group(id, uri, name);
+            }
         }
         return null;
     }
 
     public Tag createTagFromNode(Node tagNode) {
-        if (tagNode == null) return null;
-        String name = (String) tagNode.getProperty("name", null);
-        String uri = (String) tagNode.getProperty("uri", null);
-        String id = (String) tagNode.getProperty("id", null);
-
+        if (tagNode == null)
+            return null;
+        String id;
+        String uri;
+        String name;
+        try (Transaction tx = graphDb.beginTx()) {
+            name = (String) tagNode.getProperty("name", null);
+            uri = (String) tagNode.getProperty("uri", null);
+            id = (String) tagNode.getProperty("id", null);
+            tx.success();
+        }
         return new Tag(id, uri, name);
 
     }
@@ -629,13 +633,17 @@
     }
 
     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);
+        if (uriNode == null)
+            return null;
+        try (Transaction tx = graphDb.beginTx()) {
+            String uri = (String) uriNode.getProperty("uri", null);
+            String type = (String) uriNode.getProperty("TYPE", null);
+            tx.success();
+            if (type != null && type.equals("TARGET")) {
+                return new Target(uri);
+            } else if (type != null && type.equals("RESOURCE")) {
+                return new Resource(uri);
+            }
         }
         return null;
     }
@@ -649,8 +657,7 @@
      */
     public Annotation storeAnnotation(Annotation annot) {
         Node annotNode = null;
-        Transaction tx = graphDb.beginTx();
-        try {
+        try (Transaction tx = graphDb.beginTx()) {
             /*
              * create or get the annotation
              */
@@ -671,7 +678,7 @@
             if (bodyUri != null) {
                 annotNode.setProperty("bodyUri", bodyUri);
             }
-
+                
             /*
              * the annotation target
              */
@@ -773,8 +780,6 @@
                 }
             }
             tx.success();
-        } finally {
-            tx.finish();
         }
 
         // re-read and return annotation
@@ -788,11 +793,10 @@
      * @param id
      */
     public void deleteAnnotationById(String id) {
-        Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle();
-        if (annotNode != null) {
-            // delete related objects
-            Transaction tx = graphDb.beginTx();
-            try {
+        try (Transaction tx = graphDb.beginTx()) {
+            Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle();
+            if (annotNode != null) {
+                // delete related objects
                 for (Relationship rel : annotNode.getRelationships()) {
                     // delete relation and the related node if it has no other
                     // relations and is not permanent
@@ -807,10 +811,8 @@
                 } else {
                     logger.error("deleteById: unable to delete: Node still has relations.");
                 }
-                tx.success();
-            } finally {
-                tx.finish();
             }
+            tx.success();
         }
     }
 
@@ -825,20 +827,20 @@
         List<Annotation> annotations = new ArrayList<Annotation>();
         if (targetUri != null) {
             // there should be only one
-            Node target = getNodeFromIndex("uri", targetUri, NodeTypes.TARGET); 
+            Node target = getNodeFromIndex("uri", targetUri, NodeTypes.TARGET);
             if (target != null) {
-                try ( Transaction tx = graphDb.beginTx() ) {
-                Iterable<Relationship> relations = target.getRelationships(RelationTypes.ANNOTATES);
-                for (Relationship relation : relations) {
-                    Node ann = relation.getStartNode();
-                    if (ann.getProperty("TYPE", "").equals("ANNOTATION")) {
-                        Annotation annot = createAnnotationFromNode(ann);
-                        annotations.add(annot);
-                    } else {
-                        logger.error("ANNOTATES relation does not start with ANNOTATION: " + ann);
+                try (Transaction tx = graphDb.beginTx()) {
+                    Iterable<Relationship> relations = target.getRelationships(RelationTypes.ANNOTATES);
+                    for (Relationship relation : relations) {
+                        Node ann = relation.getStartNode();
+                        if (ann.getProperty("TYPE", "").equals("ANNOTATION")) {
+                            Annotation annot = createAnnotationFromNode(ann);
+                            annotations.add(annot);
+                        } else {
+                            logger.error("ANNOTATES relation does not start with ANNOTATION: " + ann);
+                        }
                     }
-                }
-                tx.success();
+                    tx.success();
                 }
             }
         }
@@ -846,20 +848,23 @@
             // there should be only one
             Node person = getPersonNodeByUri(userUri);
             if (person != null) {
-                Iterable<Relationship> relations = person.getRelationships(RelationTypes.CREATED);
-                for (Relationship relation : relations) {
-                    Node ann = relation.getEndNode();
-                    if (ann.getProperty("TYPE", "").equals("ANNOTATION")) {
-                        Annotation annot = createAnnotationFromNode(ann);
-                        annotations.add(annot);
-                    } else {
-                        logger.error("CREATED relation does not end with ANNOTATION: " + ann);
+                try (Transaction tx = graphDb.beginTx()) {
+                    Iterable<Relationship> relations = person.getRelationships(RelationTypes.CREATED);
+                    for (Relationship relation : relations) {
+                        Node ann = relation.getEndNode();
+                        if (ann.getProperty("TYPE", "").equals("ANNOTATION")) {
+                            Annotation annot = createAnnotationFromNode(ann);
+                            annotations.add(annot);
+                        } else {
+                            logger.error("CREATED relation does not end with ANNOTATION: " + ann);
+                        }
                     }
+                    tx.success();
                 }
             }
         }
         // TODO: if both uri and user are given we should intersect
-        
+
         return annotations;
     }
 
@@ -873,65 +878,63 @@
      * @return
      */
     protected Relationship getOrCreateRelation(Node start, RelationshipType type, Node end) {
-        if (start == null || end == null) return null;
+        if (start == null || end == null)
+            return null;
         if (start.hasRelationship()) {
             // there are relations
-            Iterable<Relationship> rels = start.getRelationships(type, Direction.OUTGOING);
-            for (Relationship rel : rels) {
-                if (rel.getEndNode().equals(end)) {
-                    // relation exists
-                    return rel;
+            try (Transaction tx = graphDb.beginTx()) {
+                Iterable<Relationship> rels = start.getRelationships(type, Direction.OUTGOING);
+                for (Relationship rel : rels) {
+                    if (rel.getEndNode().equals(end)) {
+                        // relation exists
+                        tx.success();
+                        return rel;
+                    }
                 }
+                tx.success();
             }
         }
         // create new one
         Relationship rel;
-        Transaction tx = graphDb.beginTx();
-        try {
+        try (Transaction tx = graphDb.beginTx()) {
             rel = start.createRelationshipTo(end, type);
             tx.success();
-        } finally {
-            tx.finish();
         }
         return rel;
     }
 
     protected Node getOrCreateAnnotationNode(String id) {
         Index<Node> idx = getNodeIndex(NodeTypes.ANNOTATION);
-        IndexHits<Node> annotations = idx.get("id", id);
-        Node annotation = annotations.getSingle();
-        if (annotation == null) {
-            // does not exist yet
-            Transaction tx = graphDb.beginTx();
-            try {
+        Node annotation;
+        try (Transaction tx = graphDb.beginTx()) {
+            IndexHits<Node> annotations = idx.get("id", id);
+            annotation = annotations.getSingle();
+            if (annotation == null) {
+                // does not exist yet
                 annotation = graphDb.createNode();
                 annotation.setProperty("TYPE", NodeTypes.ANNOTATION.name());
                 annotation.setProperty("id", id);
                 idx.add(annotation, "id", id);
-                tx.success();
-            } finally {
-                tx.finish();
             }
+            tx.success();
         }
         return annotation;
     }
 
     protected Node getOrCreateUriNode(String uri, NodeTypes type) {
         Index<Node> idx = getNodeIndex(type);
-        IndexHits<Node> targets = idx.get("uri", uri);
-        Node target = targets.getSingle();
-        if (target == null) {
-            // does not exist yet
-            Transaction tx = graphDb.beginTx();
-            try {
+        Node target;
+        try (Transaction tx = graphDb.beginTx()) {
+            IndexHits<Node> targets = idx.get("uri", uri);
+            target = targets.getSingle();
+            if (target == null) {
+                // does not exist yet
                 target = graphDb.createNode();
                 target.setProperty("TYPE", type.name());
                 target.setProperty("uri", uri);
                 idx.add(target, "uri", uri);
-                tx.success();
-            } finally {
-                tx.finish();
             }
+            tx.success();
         }
         return target;
     }
@@ -940,13 +943,17 @@
         // Person/Group is identified by URI or id
         String uri = actor.getUriString();
         Index<Node> idx;
+        Node person;
         if (actor.isGroup()) {
             idx = getNodeIndex(NodeTypes.GROUP);
         } else {
             idx = getNodeIndex(NodeTypes.PERSON);
         }
-        IndexHits<Node> persons = idx.get("uri", uri);
-        Node person = persons.getSingle();
+        try (Transaction tx = graphDb.beginTx()) {
+            IndexHits<Node> persons = idx.get("uri", uri);
+            person = persons.getSingle();
+            tx.success();
+        }
         return person;
     }
 
@@ -955,18 +962,18 @@
         String uri = actor.getUriString();
         String name = actor.getName();
         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) {
-            // does not exist yet
-            Transaction tx = graphDb.beginTx();
-            try {
+        Node person;
+        try (Transaction tx = graphDb.beginTx()) {
+            Index<Node> idx;
+            if (actor.isGroup()) {
+                idx = getNodeIndex(NodeTypes.GROUP);
+            } else {
+                idx = getNodeIndex(NodeTypes.PERSON);
+            }
+            IndexHits<Node> persons = idx.get("uri", uri);
+            person = persons.getSingle();
+            if (person == null) {
+                // does not exist yet
                 person = graphDb.createNode();
                 if (actor.isGroup()) {
                     person.setProperty("TYPE", NodeTypes.GROUP.name());
@@ -981,10 +988,8 @@
                 if (id != null) {
                     person.setProperty("id", id);
                 }
-                tx.success();
-            } finally {
-                tx.finish();
             }
+            tx.success();
         }
         return person;
     }
@@ -992,12 +997,12 @@
     protected Node getOrCreateTagNode(Tag inTag) {
         Index<Node> idx = getNodeIndex(NodeTypes.TAG);
         String tagname = inTag.getName();
-        IndexHits<Node> tags = idx.get("name", tagname);
-        Node tag = tags.getSingle();
-        if (tag == null) {
-            // does not exist yet
-            Transaction tx = graphDb.beginTx();
-            try {
+        Node tag;
+        try (Transaction tx = graphDb.beginTx()) {
+            IndexHits<Node> tags = idx.get("name", tagname);
+            tag = tags.getSingle();
+            if (tag == null) {
+                // does not exist yet
                 tag = graphDb.createNode();
                 tag.setProperty("TYPE", NodeTypes.TAG.name());
                 tag.setProperty("name", tagname);
@@ -1006,11 +1011,8 @@
                 tag.setProperty("id", inTag.getId());
                 tag.setProperty("uri", inTag.getUri());
                 idx.add(tag, "uri", inTag.getUri());
-
-                tx.success();
-            } finally {
-                tx.finish();
             }
+            tx.success();
         }
         return tag;
     }
@@ -1030,19 +1032,17 @@
         Relationship rel = getRelation(annotNode, type, null);
         if (rel != null) {
             // relation exists
-            Node oldActorNode = rel.getEndNode();
-            if (!oldActorNode.equals(newActorNode)) {
-                // new admin is different
-                Transaction tx = graphDb.beginTx();
-                try {
+            try (Transaction tx = graphDb.beginTx()) {
+                Node oldActorNode = rel.getEndNode();
+                if (!oldActorNode.equals(newActorNode)) {
+                    // new admin is different
                     rel.delete();
                     tx.success();
-                } finally {
-                    tx.finish();
                 }
                 if (newActorNode != null) {
                     rel = getOrCreateRelation(annotNode, type, newActorNode);
                 }
+                tx.success();
             }
         } else {
             // no relation yet
@@ -1058,8 +1058,7 @@
      * @param node
      */
     protected void deleteNode(Node node) {
-        Transaction tx = graphDb.beginTx();
-        try {
+        try (Transaction tx = graphDb.beginTx()) {
             if (node.hasRelationship()) {
                 logger.error("deleteNode: unable to delete: Node still has relations.");
             } else {
@@ -1073,8 +1072,6 @@
                 node.delete();
             }
             tx.success();
-        } finally {
-            tx.finish();
         }
     }
 
@@ -1088,15 +1085,18 @@
      */
     protected Relationship getRelation(Node start, RelationTypes type, Direction direction) {
         Iterable<Relationship> rels;
-        if (direction == null) {
-            // ignore direction
-            rels = start.getRelationships(type);
-        } else {
-            rels = start.getRelationships(type, direction);
-        }
-        for (Relationship rel : rels) {
-            // just the first one
-            return rel;
+        try (Transaction tx = graphDb.beginTx()) {
+            if (direction == null) {
+                // ignore direction
+                rels = start.getRelationships(type);
+            } else {
+                rels = start.getRelationships(type, direction);
+            }
+            tx.success();
+            for (Relationship rel : rels) {
+                // just the first one
+                return rel;
+            }
         }
         return null;
     }
@@ -1116,10 +1116,13 @@
         ArrayList<Annotation> ret = new ArrayList<Annotation>();
         Node tag = getTagNodeByUri(tagUri);
         if (tag != null) {
-            Iterable<Relationship> rels = tag.getRelationships(Direction.INCOMING, RelationTypes.HAS_TAG);
-            for (Relationship rel : rels) {
-                Node node = rel.getStartNode();
-                ret.add(createAnnotationFromNode(node));
+            try (Transaction tx = graphDb.beginTx()) {
+                Iterable<Relationship> rels = tag.getRelationships(Direction.INCOMING, RelationTypes.HAS_TAG);
+                for (Relationship rel : rels) {
+                    Node node = rel.getStartNode();
+                    ret.add(createAnnotationFromNode(node));
+                }
+                tx.success();
             }
         }
         return ret;
@@ -1129,14 +1132,17 @@
         ArrayList<Annotation> ret = new ArrayList<Annotation>();
         Node res = getNodeFromIndex("uri", resourceUri, NodeTypes.RESOURCE);
         if (res != null) {
-            Iterable<Relationship> rels = res.getRelationships(Direction.INCOMING, RelationTypes.ANNOTATES);
-            for (Relationship rel : rels) {
-                Node an = rel.getStartNode();
-                Node rn = rel.getEndNode();
-                if (rn.getProperty("TYPE", "").equals("RESOURCE")) {
-                    logger.error("getAnnotationsByResource got ANNOTATES != RESOURCE");
+            try (Transaction tx = graphDb.beginTx()) {
+                Iterable<Relationship> rels = res.getRelationships(Direction.INCOMING, RelationTypes.ANNOTATES);
+                for (Relationship rel : rels) {
+                    Node an = rel.getStartNode();
+                    Node rn = rel.getEndNode();
+                    if (rn.getProperty("TYPE", "").equals("RESOURCE")) {
+                        logger.error("getAnnotationsByResource got ANNOTATES != RESOURCE");
+                    }
+                    ret.add(createAnnotationFromNode(an));
                 }
-                ret.add(createAnnotationFromNode(an));
+                tx.success();
             }
         }
         return ret;
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorRestlet.java	Fri Feb 21 22:19:23 2014 +0100
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorRestlet.java	Sat Feb 22 07:12:37 2014 -0800
@@ -14,7 +14,7 @@
  */
 public class AnnotatorRestlet extends BaseRestlet {
 
-    public final String version = "AnnotationManagerN4J/Annotator 0.3.1";
+    public final String version = "AnnotationManagerN4J/Annotator 0.3.2";
 
     public static Logger logger = Logger.getLogger(AnnotatorRestlet.class);
 
--- a/src/main/webapp/annotationBrowser/js/annotation.js	Fri Feb 21 22:19:23 2014 +0100
+++ b/src/main/webapp/annotationBrowser/js/annotation.js	Sat Feb 22 07:12:37 2014 -0800
@@ -19,7 +19,7 @@
 		  $('#annotations').html("");
 		  for (var i=0;i<rows.length;i++){
 			  var text=rows[i]['text'];
-			  var author=rows[i]['user']['name'];
+			  var author= (rows[i]['user'] != null) ? rows[i]['user']['name'] : 'NO USER';
 			  var docUri=createLinkFromURI(rows[i]['uri']);
 			  
 			  var ret = '<div id="an_'+i+'" class="annotationRow">';