comparison src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java @ 40:03e0f7574224

saving and loading resource targets should work now (no searching yet)
author casties
date Wed, 26 Sep 2012 14:56:42 +0200
parents 0fdb05f35139
children aa2bb7ac04d9
comparison
equal deleted inserted replaced
36:0fdb05f35139 40:03e0f7574224
525 525
526 /* 526 /*
527 * the annotation target 527 * the annotation target
528 */ 528 */
529 String targetBaseUri = annot.getTargetBaseUri(); 529 String targetBaseUri = annot.getTargetBaseUri();
530 Node targetNode = null;
530 if (targetBaseUri != null) { 531 if (targetBaseUri != null) {
531 Node target = getOrCreateTargetNode(targetBaseUri); 532 targetNode = getOrCreateTargetNode(targetBaseUri, NodeTypes.TARGET);
532 getOrCreateRelation(annotNode, RelationTypes.ANNOTATES, target); 533 getOrCreateRelation(annotNode, RelationTypes.ANNOTATES, targetNode);
533 } 534 }
534 535
535 /* 536 /*
536 * The fragment part of the annotation target. 537 * The fragment part of the annotation target.
537 */ 538 */
538 String targetFragment = annot.getTargetFragment(); 539 String targetFragment = annot.getTargetFragment();
539 FragmentTypes fragmentType = annot.getFragmentType(); 540 FragmentTypes fragmentType = annot.getFragmentType();
540 if (targetFragment != null) { 541 if (targetFragment != null) {
541 annotNode.setProperty("targetFragment", targetFragment); 542 annotNode.setProperty("targetFragment", targetFragment);
542 annotNode.setProperty("fragmentType", fragmentType.name()); 543 annotNode.setProperty("fragmentType", fragmentType.name());
544 }
545
546 /*
547 * the annotation resource
548 */
549 String resourceUri = annot.getResourceUri();
550 if (resourceUri != null) {
551 Node resource = getOrCreateTargetNode(resourceUri, NodeTypes.RESOURCE);
552 getOrCreateRelation(annotNode, RelationTypes.ANNOTATES, resource);
553 getOrCreateRelation(targetNode, RelationTypes.PART_OF, resource);
543 } 554 }
544 555
545 /* 556 /*
546 * The creator of this annotation. 557 * The creator of this annotation.
547 */ 558 */
709 * @param type 720 * @param type
710 * @param end 721 * @param end
711 * @return 722 * @return
712 */ 723 */
713 protected Relationship getOrCreateRelation(Node start, RelationshipType type, Node end) { 724 protected Relationship getOrCreateRelation(Node start, RelationshipType type, Node end) {
725 if (start == null || end == null) return null;
714 if (start.hasRelationship()) { 726 if (start.hasRelationship()) {
715 // there are relations 727 // there are relations
716 Iterable<Relationship> rels = start.getRelationships(type, Direction.OUTGOING); 728 Iterable<Relationship> rels = start.getRelationships(type, Direction.OUTGOING);
717 for (Relationship rel : rels) { 729 for (Relationship rel : rels) {
718 if (rel.getEndNode().equals(end)) { 730 if (rel.getEndNode().equals(end)) {
751 } 763 }
752 } 764 }
753 return annotation; 765 return annotation;
754 } 766 }
755 767
756 protected Node getOrCreateTargetNode(String uri) { 768 protected Node getOrCreateTargetNode(String uri, NodeTypes type) {
757 Index<Node> idx = getNodeIndex(NodeTypes.TARGET); 769 Index<Node> idx = getNodeIndex(type);
758 IndexHits<Node> targets = idx.get("uri", uri); 770 IndexHits<Node> targets = idx.get("uri", uri);
759 Node target = targets.getSingle(); 771 Node target = targets.getSingle();
760 if (target == null) { 772 if (target == null) {
761 // does not exist yet 773 // does not exist yet
762 Transaction tx = graphDb.beginTx(); 774 Transaction tx = graphDb.beginTx();
763 try { 775 try {
764 target = graphDb.createNode(); 776 target = graphDb.createNode();
765 target.setProperty("TYPE", NodeTypes.TARGET.name()); 777 target.setProperty("TYPE", type.name());
766 target.setProperty("uri", uri); 778 target.setProperty("uri", uri);
767 idx.add(target, "uri", uri); 779 idx.add(target, "uri", uri);
768 tx.success(); 780 tx.success();
769 } finally { 781 } finally {
770 tx.finish(); 782 tx.finish();