comparison src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java @ 67:875a97f8b8da

more quick fixes for neo4j 2.0.
author casties
date Fri, 21 Feb 2014 22:19:23 +0100
parents 5b568de5ee0d
children 39bc52f9b102
comparison
equal deleted inserted replaced
66:5b568de5ee0d 67:875a97f8b8da
60 public AnnotationStore(GraphDatabaseService graphDb) { 60 public AnnotationStore(GraphDatabaseService graphDb) {
61 super(); 61 super();
62 this.graphDb = graphDb; 62 this.graphDb = graphDb;
63 nodeIndexes = new ArrayList<Index<Node>>(5); 63 nodeIndexes = new ArrayList<Index<Node>>(5);
64 // List.set(enum.ordinal(), val) seems not to work. 64 // List.set(enum.ordinal(), val) seems not to work.
65 try ( Transaction tx = graphDb.beginTx() ) 65 try ( Transaction tx = graphDb.beginTx() ) {
66 {
67 nodeIndexes.add(NodeTypes.ANNOTATION.ordinal(), graphDb.index().forNodes("annotations")); 66 nodeIndexes.add(NodeTypes.ANNOTATION.ordinal(), graphDb.index().forNodes("annotations"));
68 nodeIndexes.add(NodeTypes.PERSON.ordinal(), graphDb.index().forNodes("persons")); 67 nodeIndexes.add(NodeTypes.PERSON.ordinal(), graphDb.index().forNodes("persons"));
69 nodeIndexes.add(NodeTypes.TARGET.ordinal(), graphDb.index().forNodes("targets")); 68 nodeIndexes.add(NodeTypes.TARGET.ordinal(), graphDb.index().forNodes("targets"));
70 nodeIndexes.add(NodeTypes.GROUP.ordinal(), graphDb.index().forNodes("groups")); 69 nodeIndexes.add(NodeTypes.GROUP.ordinal(), graphDb.index().forNodes("groups"));
71 nodeIndexes.add(NodeTypes.TAG.ordinal(), graphDb.index().forNodes("tags")); 70 nodeIndexes.add(NodeTypes.TAG.ordinal(), graphDb.index().forNodes("tags"));
118 * @param type 117 * @param type
119 * @return 118 * @return
120 */ 119 */
121 public Node getNodeFromIndex(String key, String value, NodeTypes type) { 120 public Node getNodeFromIndex(String key, String value, NodeTypes type) {
122 if (key == null || value == null) return null; 121 if (key == null || value == null) return null;
123 Node node = getNodeIndex(type).get(key, value).getSingle(); 122 Node node = null;
123 try (Transaction tx = graphDb.beginTx()) {
124 node = getNodeIndex(type).get(key, value).getSingle();
125 tx.success();
126 }
124 return node; 127 return node;
125 } 128 }
126 129
127 /** 130 /**
128 * Returns list of Actors of given type (Group or Person). Key has to be 131 * Returns list of Actors of given type (Group or Person). Key has to be
309 */ 312 */
310 public boolean isPersonInGroup(Person person, Group group) { 313 public boolean isPersonInGroup(Person person, Group group) {
311 Node pn = getPersonNodeByUri(person.getUriString()); 314 Node pn = getPersonNodeByUri(person.getUriString());
312 if (pn == null) return false; 315 if (pn == null) return false;
313 // optimized version of getGroupsForPersonNode 316 // optimized version of getGroupsForPersonNode
317 try (Transaction tx = graphDb.beginTx()) {
314 Iterable<Relationship> rels = pn.getRelationships(RelationTypes.MEMBER_OF); 318 Iterable<Relationship> rels = pn.getRelationships(RelationTypes.MEMBER_OF);
315 for (Relationship rel : rels) { 319 for (Relationship rel : rels) {
316 Node gn = rel.getEndNode(); 320 Node gn = rel.getEndNode();
317 if (gn.getProperty("uri", "").equals(group.getUriString()) || gn.getProperty("id", "").equals(group.getId())) { 321 if (gn.getProperty("uri", "").equals(group.getUriString()) || gn.getProperty("id", "").equals(group.getId())) {
322 tx.success();
318 return true; 323 return true;
319 } 324 }
325 }
326 tx.success();
320 } 327 }
321 return false; 328 return false;
322 } 329 }
323 330
324 /** 331 /**
472 * 479 *
473 * @param id 480 * @param id
474 * @return 481 * @return
475 */ 482 */
476 public Annotation getAnnotationById(String id) { 483 public Annotation getAnnotationById(String id) {
484 Annotation annot = null;
485 try ( Transaction tx = graphDb.beginTx() ) {
477 Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle(); 486 Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle();
478 Annotation annot = createAnnotationFromNode(annotNode); 487 annot = createAnnotationFromNode(annotNode);
488 tx.success();
489 }
479 return annot; 490 return annot;
480 } 491 }
481 492
482 /** 493 /**
483 * Returns an Annotation object from an annotation-Node. 494 * Returns an Annotation object from an annotation-Node.
485 * @param annotNode 496 * @param annotNode
486 * @return 497 * @return
487 */ 498 */
488 public Annotation createAnnotationFromNode(Node annotNode) { 499 public Annotation createAnnotationFromNode(Node annotNode) {
489 Annotation annot = new Annotation(); 500 Annotation annot = new Annotation();
501 try ( Transaction tx = graphDb.beginTx() ) {
490 annot.setUri((String) annotNode.getProperty("id", null)); 502 annot.setUri((String) annotNode.getProperty("id", null));
491 annot.setBodyText((String) annotNode.getProperty("bodyText", null)); 503 annot.setBodyText((String) annotNode.getProperty("bodyText", null));
492 annot.setBodyUri((String) annotNode.getProperty("bodyUri", null)); 504 annot.setBodyUri((String) annotNode.getProperty("bodyUri", null));
493 /* 505 /*
494 * get annotation target and resource from relation 506 * get annotation target and resource from relation
562 if (tag != null) { 574 if (tag != null) {
563 tags.add(tag); 575 tags.add(tag);
564 } 576 }
565 } 577 }
566 annot.setTags(tags); 578 annot.setTags(tags);
567 579
580 tx.success();
581 }
568 return annot; 582 return annot;
569 } 583 }
570 584
571 /** 585 /**
572 * Returns an Actor object from a node. 586 * Returns an Actor object from a node.
811 List<Annotation> annotations = new ArrayList<Annotation>(); 825 List<Annotation> annotations = new ArrayList<Annotation>();
812 if (targetUri != null) { 826 if (targetUri != null) {
813 // there should be only one 827 // there should be only one
814 Node target = getNodeFromIndex("uri", targetUri, NodeTypes.TARGET); 828 Node target = getNodeFromIndex("uri", targetUri, NodeTypes.TARGET);
815 if (target != null) { 829 if (target != null) {
830 try ( Transaction tx = graphDb.beginTx() ) {
816 Iterable<Relationship> relations = target.getRelationships(RelationTypes.ANNOTATES); 831 Iterable<Relationship> relations = target.getRelationships(RelationTypes.ANNOTATES);
817 for (Relationship relation : relations) { 832 for (Relationship relation : relations) {
818 Node ann = relation.getStartNode(); 833 Node ann = relation.getStartNode();
819 if (ann.getProperty("TYPE", "").equals("ANNOTATION")) { 834 if (ann.getProperty("TYPE", "").equals("ANNOTATION")) {
820 Annotation annot = createAnnotationFromNode(ann); 835 Annotation annot = createAnnotationFromNode(ann);
821 annotations.add(annot); 836 annotations.add(annot);
822 } else { 837 } else {
823 logger.error("ANNOTATES relation does not start with ANNOTATION: " + ann); 838 logger.error("ANNOTATES relation does not start with ANNOTATION: " + ann);
824 } 839 }
840 }
841 tx.success();
825 } 842 }
826 } 843 }
827 } 844 }
828 if (userUri != null) { 845 if (userUri != null) {
829 // there should be only one 846 // there should be only one