Mercurial > hg > AnnotationManagerN4J
comparison src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java @ 68:39bc52f9b102
(hopefully) fixed issues with neo4j 2.0 transactions.
author | casties |
---|---|
date | Sat, 22 Feb 2014 07:12:37 -0800 |
parents | 875a97f8b8da |
children | 2b1e6df5e21a |
comparison
equal
deleted
inserted
replaced
67:875a97f8b8da | 68:39bc52f9b102 |
---|---|
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 nodeIndexes.add(NodeTypes.ANNOTATION.ordinal(), graphDb.index().forNodes("annotations")); | 66 nodeIndexes.add(NodeTypes.ANNOTATION.ordinal(), graphDb.index().forNodes("annotations")); |
67 nodeIndexes.add(NodeTypes.PERSON.ordinal(), graphDb.index().forNodes("persons")); | 67 nodeIndexes.add(NodeTypes.PERSON.ordinal(), graphDb.index().forNodes("persons")); |
68 nodeIndexes.add(NodeTypes.TARGET.ordinal(), graphDb.index().forNodes("targets")); | 68 nodeIndexes.add(NodeTypes.TARGET.ordinal(), graphDb.index().forNodes("targets")); |
69 nodeIndexes.add(NodeTypes.GROUP.ordinal(), graphDb.index().forNodes("groups")); | 69 nodeIndexes.add(NodeTypes.GROUP.ordinal(), graphDb.index().forNodes("groups")); |
70 nodeIndexes.add(NodeTypes.TAG.ordinal(), graphDb.index().forNodes("tags")); | 70 nodeIndexes.add(NodeTypes.TAG.ordinal(), graphDb.index().forNodes("tags")); |
116 * @param value | 116 * @param value |
117 * @param type | 117 * @param type |
118 * @return | 118 * @return |
119 */ | 119 */ |
120 public Node getNodeFromIndex(String key, String value, NodeTypes type) { | 120 public Node getNodeFromIndex(String key, String value, NodeTypes type) { |
121 if (key == null || value == null) return null; | 121 if (key == null || value == null) |
122 return null; | |
122 Node node = null; | 123 Node node = null; |
123 try (Transaction tx = graphDb.beginTx()) { | 124 try (Transaction tx = graphDb.beginTx()) { |
124 node = getNodeIndex(type).get(key, value).getSingle(); | 125 node = getNodeIndex(type).get(key, value).getSingle(); |
125 tx.success(); | 126 tx.success(); |
126 } | 127 } |
148 IndexHits<Node> actorNodes = idx.query(key, query); | 149 IndexHits<Node> actorNodes = idx.query(key, query); |
149 for (Node actorNode : actorNodes) { | 150 for (Node actorNode : actorNodes) { |
150 Actor actor = createActorFromNode(actorNode); | 151 Actor actor = createActorFromNode(actorNode); |
151 actors.add((T) actor); | 152 actors.add((T) actor); |
152 } | 153 } |
153 tx.success(); | 154 tx.success(); |
154 } | 155 } |
155 return actors; | 156 return actors; |
156 } | 157 } |
157 | 158 |
158 /** | 159 /** |
310 * @param group | 311 * @param group |
311 * @return | 312 * @return |
312 */ | 313 */ |
313 public boolean isPersonInGroup(Person person, Group group) { | 314 public boolean isPersonInGroup(Person person, Group group) { |
314 Node pn = getPersonNodeByUri(person.getUriString()); | 315 Node pn = getPersonNodeByUri(person.getUriString()); |
315 if (pn == null) return false; | 316 if (pn == null) |
317 return false; | |
316 // optimized version of getGroupsForPersonNode | 318 // optimized version of getGroupsForPersonNode |
317 try (Transaction tx = graphDb.beginTx()) { | 319 try (Transaction tx = graphDb.beginTx()) { |
318 Iterable<Relationship> rels = pn.getRelationships(RelationTypes.MEMBER_OF); | 320 Iterable<Relationship> rels = pn.getRelationships(RelationTypes.MEMBER_OF); |
319 for (Relationship rel : rels) { | 321 for (Relationship rel : rels) { |
320 Node gn = rel.getEndNode(); | 322 Node gn = rel.getEndNode(); |
321 if (gn.getProperty("uri", "").equals(group.getUriString()) || gn.getProperty("id", "").equals(group.getId())) { | 323 if (gn.getProperty("uri", "").equals(group.getUriString()) |
322 tx.success(); | 324 || gn.getProperty("id", "").equals(group.getId())) { |
323 return true; | 325 tx.success(); |
324 } | 326 return true; |
325 } | 327 } |
326 tx.success(); | 328 } |
329 tx.success(); | |
327 } | 330 } |
328 return false; | 331 return false; |
329 } | 332 } |
330 | 333 |
331 /** | 334 /** |
379 Node pn = getActorNode(member); | 382 Node pn = getActorNode(member); |
380 Iterable<Relationship> rels = gn.getRelationships(RelationTypes.MEMBER_OF); | 383 Iterable<Relationship> rels = gn.getRelationships(RelationTypes.MEMBER_OF); |
381 for (Relationship rel : rels) { | 384 for (Relationship rel : rels) { |
382 Node mn = rel.getStartNode(); | 385 Node mn = rel.getStartNode(); |
383 if (mn.equals(pn)) { | 386 if (mn.equals(pn)) { |
384 Transaction tx = graphDb.beginTx(); | 387 try (Transaction tx = graphDb.beginTx()) { |
385 try { | |
386 rel.delete(); | 388 rel.delete(); |
387 tx.success(); | 389 tx.success(); |
388 } finally { | |
389 tx.finish(); | |
390 } | 390 } |
391 // there should be only one | 391 // there should be only one |
392 break; | 392 break; |
393 } | 393 } |
394 } | 394 } |
413 * @param actor | 413 * @param actor |
414 * @return | 414 * @return |
415 */ | 415 */ |
416 public Actor storeActor(Actor actor) { | 416 public Actor storeActor(Actor actor) { |
417 Node actorNode = getOrCreateActorNode(actor); | 417 Node actorNode = getOrCreateActorNode(actor); |
418 Transaction tx = graphDb.beginTx(); | 418 try (Transaction tx = graphDb.beginTx()) { |
419 try { | |
420 // id | 419 // id |
421 String id = actor.getId(); | 420 String id = actor.getId(); |
422 if (id != null) { | 421 if (id != null) { |
423 actorNode.setProperty("id", id); | 422 actorNode.setProperty("id", id); |
424 } | 423 } |
431 String uri = actor.getUri(); | 430 String uri = actor.getUri(); |
432 if (uri != null) { | 431 if (uri != null) { |
433 actorNode.setProperty("uri", uri); | 432 actorNode.setProperty("uri", uri); |
434 } | 433 } |
435 tx.success(); | 434 tx.success(); |
436 } finally { | |
437 tx.finish(); | |
438 } | 435 } |
439 Actor storedActor = createActorFromNode(actorNode); | 436 Actor storedActor = createActorFromNode(actorNode); |
440 return storedActor; | 437 return storedActor; |
441 } | 438 } |
442 | 439 |
454 idx = getNodeIndex(NodeTypes.PERSON); | 451 idx = getNodeIndex(NodeTypes.PERSON); |
455 } | 452 } |
456 Node actorNode = idx.get("uri", uri).getSingle(); | 453 Node actorNode = idx.get("uri", uri).getSingle(); |
457 if (actorNode != null) { | 454 if (actorNode != null) { |
458 // delete relations | 455 // delete relations |
459 Transaction tx = graphDb.beginTx(); | 456 try (Transaction tx = graphDb.beginTx()) { |
460 try { | |
461 for (Relationship rel : actorNode.getRelationships()) { | 457 for (Relationship rel : actorNode.getRelationships()) { |
462 rel.delete(); | 458 rel.delete(); |
463 } | 459 } |
464 if (!actorNode.hasRelationship()) { | 460 if (!actorNode.hasRelationship()) { |
465 // this shouldn't happen | 461 // this shouldn't happen |
466 deleteNode(actorNode); | 462 deleteNode(actorNode); |
467 } else { | 463 } else { |
468 logger.error("deleteActor: unable to delete: Node still has relations."); | 464 logger.error("deleteActor: unable to delete: Node still has relations."); |
469 } | 465 } |
470 tx.success(); | 466 tx.success(); |
471 } finally { | |
472 tx.finish(); | |
473 } | 467 } |
474 } | 468 } |
475 } | 469 } |
476 | 470 |
477 /** | 471 /** |
480 * @param id | 474 * @param id |
481 * @return | 475 * @return |
482 */ | 476 */ |
483 public Annotation getAnnotationById(String id) { | 477 public Annotation getAnnotationById(String id) { |
484 Annotation annot = null; | 478 Annotation annot = null; |
485 try ( Transaction tx = graphDb.beginTx() ) { | 479 try (Transaction tx = graphDb.beginTx()) { |
486 Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle(); | 480 Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle(); |
487 annot = createAnnotationFromNode(annotNode); | 481 annot = createAnnotationFromNode(annotNode); |
488 tx.success(); | 482 tx.success(); |
489 } | 483 } |
490 return annot; | 484 return annot; |
491 } | 485 } |
492 | 486 |
493 /** | 487 /** |
496 * @param annotNode | 490 * @param annotNode |
497 * @return | 491 * @return |
498 */ | 492 */ |
499 public Annotation createAnnotationFromNode(Node annotNode) { | 493 public Annotation createAnnotationFromNode(Node annotNode) { |
500 Annotation annot = new Annotation(); | 494 Annotation annot = new Annotation(); |
501 try ( Transaction tx = graphDb.beginTx() ) { | 495 try (Transaction tx = graphDb.beginTx()) { |
502 annot.setUri((String) annotNode.getProperty("id", null)); | 496 annot.setUri((String) annotNode.getProperty("id", null)); |
503 annot.setBodyText((String) annotNode.getProperty("bodyText", null)); | 497 annot.setBodyText((String) annotNode.getProperty("bodyText", null)); |
504 annot.setBodyUri((String) annotNode.getProperty("bodyUri", null)); | 498 annot.setBodyUri((String) annotNode.getProperty("bodyUri", null)); |
505 /* | 499 /* |
506 * get annotation target and resource from relation | 500 * get annotation target and resource from relation |
507 */ | 501 */ |
508 for (Relationship rel : annotNode.getRelationships(RelationTypes.ANNOTATES)) { | 502 for (Relationship rel : annotNode.getRelationships(RelationTypes.ANNOTATES)) { |
509 Node target = rel.getEndNode(); | 503 Node target = rel.getEndNode(); |
510 String type = (String) target.getProperty("TYPE"); | 504 String type = (String) target.getProperty("TYPE"); |
511 if (type.equals("TARGET")) { | 505 if (type.equals("TARGET")) { |
512 annot.setTarget(new Target((String) target.getProperty("uri", null))); | 506 annot.setTarget(new Target((String) target.getProperty("uri", null))); |
513 } else if (type.equals("RESOURCE")) { | 507 } else if (type.equals("RESOURCE")) { |
514 annot.setResource(new Resource((String) target.getProperty("uri", null))); | 508 annot.setResource(new Resource((String) target.getProperty("uri", null))); |
515 } | 509 } |
516 } | 510 } |
517 if (annot.getTarget() == null) { | 511 if (annot.getTarget() == null) { |
518 logger.error("annotation " + annotNode + " has no target node!"); | 512 logger.warn("annotation " + annotNode + " has no target node!"); |
519 } | 513 } |
520 // get fragment from attribute | 514 // get fragment from attribute |
521 annot.setTargetFragment((String) annotNode.getProperty("targetFragment", null)); | 515 annot.setTargetFragment((String) annotNode.getProperty("targetFragment", null)); |
522 String ft = (String) annotNode.getProperty("fragmentType", null); | 516 String ft = (String) annotNode.getProperty("fragmentType", null); |
523 if (ft != null) { | 517 if (ft != null) { |
524 annot.setFragmentType(FragmentTypes.valueOf(ft)); | 518 annot.setFragmentType(FragmentTypes.valueOf(ft)); |
525 } | 519 } |
526 /* | 520 /* |
527 * get creator from relation | 521 * get creator from relation |
528 */ | 522 */ |
529 Relationship creatorRel = getRelation(annotNode, RelationTypes.CREATED, null); | 523 Relationship creatorRel = getRelation(annotNode, RelationTypes.CREATED, null); |
530 if (creatorRel != null) { | 524 if (creatorRel != null) { |
531 Node creatorNode = creatorRel.getStartNode(); | 525 Node creatorNode = creatorRel.getStartNode(); |
532 Actor creator = createActorFromNode(creatorNode); | 526 Actor creator = createActorFromNode(creatorNode); |
533 annot.setCreator(creator); | 527 annot.setCreator(creator); |
534 } else { | 528 } else { |
535 logger.error("annotation " + annotNode + " has no creator node!"); | 529 logger.warn("annotation " + annotNode + " has no creator node!"); |
536 } | 530 } |
537 /* | 531 /* |
538 * get creation date | 532 * get creation date |
539 */ | 533 */ |
540 annot.setCreated((String) annotNode.getProperty("created", null)); | 534 annot.setCreated((String) annotNode.getProperty("created", null)); |
541 /* | 535 /* |
542 * get permissions | 536 * get permissions |
543 */ | 537 */ |
544 Relationship adminRel = getRelation(annotNode, RelationTypes.PERMITS_ADMIN, null); | 538 Relationship adminRel = getRelation(annotNode, RelationTypes.PERMITS_ADMIN, null); |
545 if (adminRel != null) { | 539 if (adminRel != null) { |
546 Node adminNode = adminRel.getEndNode(); | 540 Node adminNode = adminRel.getEndNode(); |
547 Actor admin = createActorFromNode(adminNode); | 541 Actor admin = createActorFromNode(adminNode); |
548 annot.setAdminPermission(admin); | 542 annot.setAdminPermission(admin); |
549 } | 543 } |
550 Relationship deleteRel = getRelation(annotNode, RelationTypes.PERMITS_DELETE, null); | 544 Relationship deleteRel = getRelation(annotNode, RelationTypes.PERMITS_DELETE, null); |
551 if (deleteRel != null) { | 545 if (deleteRel != null) { |
552 Node deleteNode = deleteRel.getEndNode(); | 546 Node deleteNode = deleteRel.getEndNode(); |
553 Actor delete = createActorFromNode(deleteNode); | 547 Actor delete = createActorFromNode(deleteNode); |
554 annot.setDeletePermission(delete); | 548 annot.setDeletePermission(delete); |
555 } | 549 } |
556 Relationship updateRel = getRelation(annotNode, RelationTypes.PERMITS_UPDATE, null); | 550 Relationship updateRel = getRelation(annotNode, RelationTypes.PERMITS_UPDATE, null); |
557 if (updateRel != null) { | 551 if (updateRel != null) { |
558 Node updateNode = updateRel.getEndNode(); | 552 Node updateNode = updateRel.getEndNode(); |
559 Actor update = createActorFromNode(updateNode); | 553 Actor update = createActorFromNode(updateNode); |
560 annot.setUpdatePermission(update); | 554 annot.setUpdatePermission(update); |
561 } | 555 } |
562 Relationship readRel = getRelation(annotNode, RelationTypes.PERMITS_READ, null); | 556 Relationship readRel = getRelation(annotNode, RelationTypes.PERMITS_READ, null); |
563 if (readRel != null) { | 557 if (readRel != null) { |
564 Node readNode = readRel.getEndNode(); | 558 Node readNode = readRel.getEndNode(); |
565 Actor read = createActorFromNode(readNode); | 559 Actor read = createActorFromNode(readNode); |
566 annot.setReadPermission(read); | 560 annot.setReadPermission(read); |
567 } | 561 } |
568 /* | 562 /* |
569 * get tags | 563 * get tags |
570 */ | 564 */ |
571 Set<String> tags = new HashSet<String>(); | 565 Set<String> tags = new HashSet<String>(); |
572 for (Relationship rel : annotNode.getRelationships(RelationTypes.HAS_TAG)) { | 566 for (Relationship rel : annotNode.getRelationships(RelationTypes.HAS_TAG)) { |
573 String tag = (String) rel.getEndNode().getProperty("name", null); | 567 String tag = (String) rel.getEndNode().getProperty("name", null); |
574 if (tag != null) { | 568 if (tag != null) { |
575 tags.add(tag); | 569 tags.add(tag); |
576 } | 570 } |
577 } | 571 } |
578 annot.setTags(tags); | 572 annot.setTags(tags); |
579 | 573 |
580 tx.success(); | 574 tx.success(); |
581 } | 575 } |
582 return annot; | 576 return annot; |
583 } | 577 } |
584 | 578 |
585 /** | 579 /** |
587 * | 581 * |
588 * @param actorNode | 582 * @param actorNode |
589 * @return | 583 * @return |
590 */ | 584 */ |
591 protected Actor createActorFromNode(Node actorNode) { | 585 protected Actor createActorFromNode(Node actorNode) { |
592 if (actorNode == null) return null; | 586 if (actorNode == null) |
593 String id = (String) actorNode.getProperty("id", null); | 587 return null; |
594 String uri = (String) actorNode.getProperty("uri", null); | 588 try (Transaction tx = graphDb.beginTx()) { |
595 String name = (String) actorNode.getProperty("name", null); | 589 String id = (String) actorNode.getProperty("id", null); |
596 String type = (String) actorNode.getProperty("TYPE", null); | 590 String uri = (String) actorNode.getProperty("uri", null); |
597 if (type != null && type.equals("PERSON")) { | 591 String name = (String) actorNode.getProperty("name", null); |
598 return new Person(id, uri, name); | 592 String type = (String) actorNode.getProperty("TYPE", null); |
599 } else if (type != null && type.equals("GROUP")) { | 593 tx.success(); |
600 return new Group(id, uri, name); | 594 if (type != null && type.equals("PERSON")) { |
595 return new Person(id, uri, name); | |
596 } else if (type != null && type.equals("GROUP")) { | |
597 return new Group(id, uri, name); | |
598 } | |
601 } | 599 } |
602 return null; | 600 return null; |
603 } | 601 } |
604 | 602 |
605 public Tag createTagFromNode(Node tagNode) { | 603 public Tag createTagFromNode(Node tagNode) { |
606 if (tagNode == null) return null; | 604 if (tagNode == null) |
607 String name = (String) tagNode.getProperty("name", null); | 605 return null; |
608 String uri = (String) tagNode.getProperty("uri", null); | 606 String id; |
609 String id = (String) tagNode.getProperty("id", null); | 607 String uri; |
610 | 608 String name; |
609 try (Transaction tx = graphDb.beginTx()) { | |
610 name = (String) tagNode.getProperty("name", null); | |
611 uri = (String) tagNode.getProperty("uri", null); | |
612 id = (String) tagNode.getProperty("id", null); | |
613 tx.success(); | |
614 } | |
611 return new Tag(id, uri, name); | 615 return new Tag(id, uri, name); |
612 | 616 |
613 } | 617 } |
614 | 618 |
615 /** | 619 /** |
627 public Target createTargetFromNode(Node targetNode) { | 631 public Target createTargetFromNode(Node targetNode) { |
628 return (Target) createUriFromNode(targetNode); | 632 return (Target) createUriFromNode(targetNode); |
629 } | 633 } |
630 | 634 |
631 protected Uri createUriFromNode(Node uriNode) { | 635 protected Uri createUriFromNode(Node uriNode) { |
632 if (uriNode == null) return null; | 636 if (uriNode == null) |
633 String uri = (String) uriNode.getProperty("uri", null); | 637 return null; |
634 String type = (String) uriNode.getProperty("TYPE", null); | 638 try (Transaction tx = graphDb.beginTx()) { |
635 if (type != null && type.equals("TARGET")) { | 639 String uri = (String) uriNode.getProperty("uri", null); |
636 return new Target(uri); | 640 String type = (String) uriNode.getProperty("TYPE", null); |
637 } else if (type != null && type.equals("RESOURCE")) { | 641 tx.success(); |
638 return new Resource(uri); | 642 if (type != null && type.equals("TARGET")) { |
643 return new Target(uri); | |
644 } else if (type != null && type.equals("RESOURCE")) { | |
645 return new Resource(uri); | |
646 } | |
639 } | 647 } |
640 return null; | 648 return null; |
641 } | 649 } |
642 | 650 |
643 /** | 651 /** |
647 * @param annot | 655 * @param annot |
648 * @return | 656 * @return |
649 */ | 657 */ |
650 public Annotation storeAnnotation(Annotation annot) { | 658 public Annotation storeAnnotation(Annotation annot) { |
651 Node annotNode = null; | 659 Node annotNode = null; |
652 Transaction tx = graphDb.beginTx(); | 660 try (Transaction tx = graphDb.beginTx()) { |
653 try { | |
654 /* | 661 /* |
655 * create or get the annotation | 662 * create or get the annotation |
656 */ | 663 */ |
657 String id = annot.getUri(); | 664 String id = annot.getUri(); |
658 if (id == null) { | 665 if (id == null) { |
669 } | 676 } |
670 String bodyUri = annot.getBodyUri(); | 677 String bodyUri = annot.getBodyUri(); |
671 if (bodyUri != null) { | 678 if (bodyUri != null) { |
672 annotNode.setProperty("bodyUri", bodyUri); | 679 annotNode.setProperty("bodyUri", bodyUri); |
673 } | 680 } |
674 | 681 |
675 /* | 682 /* |
676 * the annotation target | 683 * the annotation target |
677 */ | 684 */ |
678 Target target = annot.getTarget(); | 685 Target target = annot.getTarget(); |
679 Node targetNode = null; | 686 Node targetNode = null; |
771 } | 778 } |
772 | 779 |
773 } | 780 } |
774 } | 781 } |
775 tx.success(); | 782 tx.success(); |
776 } finally { | |
777 tx.finish(); | |
778 } | 783 } |
779 | 784 |
780 // re-read and return annotation | 785 // re-read and return annotation |
781 Annotation storedAnnot = createAnnotationFromNode(annotNode); | 786 Annotation storedAnnot = createAnnotationFromNode(annotNode); |
782 return storedAnnot; | 787 return storedAnnot; |
786 * Deletes the annotation with the given id. | 791 * Deletes the annotation with the given id. |
787 * | 792 * |
788 * @param id | 793 * @param id |
789 */ | 794 */ |
790 public void deleteAnnotationById(String id) { | 795 public void deleteAnnotationById(String id) { |
791 Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle(); | 796 try (Transaction tx = graphDb.beginTx()) { |
792 if (annotNode != null) { | 797 Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle(); |
793 // delete related objects | 798 if (annotNode != null) { |
794 Transaction tx = graphDb.beginTx(); | 799 // delete related objects |
795 try { | |
796 for (Relationship rel : annotNode.getRelationships()) { | 800 for (Relationship rel : annotNode.getRelationships()) { |
797 // delete relation and the related node if it has no other | 801 // delete relation and the related node if it has no other |
798 // relations and is not permanent | 802 // relations and is not permanent |
799 Node other = rel.getOtherNode(annotNode); | 803 Node other = rel.getOtherNode(annotNode); |
800 rel.delete(); | 804 rel.delete(); |
805 if (!annotNode.hasRelationship()) { | 809 if (!annotNode.hasRelationship()) { |
806 deleteNode(annotNode); | 810 deleteNode(annotNode); |
807 } else { | 811 } else { |
808 logger.error("deleteById: unable to delete: Node still has relations."); | 812 logger.error("deleteById: unable to delete: Node still has relations."); |
809 } | 813 } |
810 tx.success(); | 814 } |
811 } finally { | 815 tx.success(); |
812 tx.finish(); | |
813 } | |
814 } | 816 } |
815 } | 817 } |
816 | 818 |
817 /** | 819 /** |
818 * Returns all annotations with the given uri and/or user. | 820 * Returns all annotations with the given uri and/or user. |
823 */ | 825 */ |
824 public List<Annotation> searchAnnotationByUriUser(String targetUri, String userUri) { | 826 public List<Annotation> searchAnnotationByUriUser(String targetUri, String userUri) { |
825 List<Annotation> annotations = new ArrayList<Annotation>(); | 827 List<Annotation> annotations = new ArrayList<Annotation>(); |
826 if (targetUri != null) { | 828 if (targetUri != null) { |
827 // there should be only one | 829 // there should be only one |
828 Node target = getNodeFromIndex("uri", targetUri, NodeTypes.TARGET); | 830 Node target = getNodeFromIndex("uri", targetUri, NodeTypes.TARGET); |
829 if (target != null) { | 831 if (target != null) { |
830 try ( Transaction tx = graphDb.beginTx() ) { | 832 try (Transaction tx = graphDb.beginTx()) { |
831 Iterable<Relationship> relations = target.getRelationships(RelationTypes.ANNOTATES); | 833 Iterable<Relationship> relations = target.getRelationships(RelationTypes.ANNOTATES); |
832 for (Relationship relation : relations) { | 834 for (Relationship relation : relations) { |
833 Node ann = relation.getStartNode(); | 835 Node ann = relation.getStartNode(); |
834 if (ann.getProperty("TYPE", "").equals("ANNOTATION")) { | 836 if (ann.getProperty("TYPE", "").equals("ANNOTATION")) { |
835 Annotation annot = createAnnotationFromNode(ann); | 837 Annotation annot = createAnnotationFromNode(ann); |
836 annotations.add(annot); | 838 annotations.add(annot); |
837 } else { | 839 } else { |
838 logger.error("ANNOTATES relation does not start with ANNOTATION: " + ann); | 840 logger.error("ANNOTATES relation does not start with ANNOTATION: " + ann); |
841 } | |
839 } | 842 } |
840 } | 843 tx.success(); |
841 tx.success(); | |
842 } | 844 } |
843 } | 845 } |
844 } | 846 } |
845 if (userUri != null) { | 847 if (userUri != null) { |
846 // there should be only one | 848 // there should be only one |
847 Node person = getPersonNodeByUri(userUri); | 849 Node person = getPersonNodeByUri(userUri); |
848 if (person != null) { | 850 if (person != null) { |
849 Iterable<Relationship> relations = person.getRelationships(RelationTypes.CREATED); | 851 try (Transaction tx = graphDb.beginTx()) { |
850 for (Relationship relation : relations) { | 852 Iterable<Relationship> relations = person.getRelationships(RelationTypes.CREATED); |
851 Node ann = relation.getEndNode(); | 853 for (Relationship relation : relations) { |
852 if (ann.getProperty("TYPE", "").equals("ANNOTATION")) { | 854 Node ann = relation.getEndNode(); |
853 Annotation annot = createAnnotationFromNode(ann); | 855 if (ann.getProperty("TYPE", "").equals("ANNOTATION")) { |
854 annotations.add(annot); | 856 Annotation annot = createAnnotationFromNode(ann); |
855 } else { | 857 annotations.add(annot); |
856 logger.error("CREATED relation does not end with ANNOTATION: " + ann); | 858 } else { |
859 logger.error("CREATED relation does not end with ANNOTATION: " + ann); | |
860 } | |
857 } | 861 } |
862 tx.success(); | |
858 } | 863 } |
859 } | 864 } |
860 } | 865 } |
861 // TODO: if both uri and user are given we should intersect | 866 // TODO: if both uri and user are given we should intersect |
862 | 867 |
863 return annotations; | 868 return annotations; |
864 } | 869 } |
865 | 870 |
866 /** | 871 /** |
867 * Returns Relationship of type from Node start to Node end. Creates one if | 872 * Returns Relationship of type from Node start to Node end. Creates one if |
871 * @param type | 876 * @param type |
872 * @param end | 877 * @param end |
873 * @return | 878 * @return |
874 */ | 879 */ |
875 protected Relationship getOrCreateRelation(Node start, RelationshipType type, Node end) { | 880 protected Relationship getOrCreateRelation(Node start, RelationshipType type, Node end) { |
876 if (start == null || end == null) return null; | 881 if (start == null || end == null) |
882 return null; | |
877 if (start.hasRelationship()) { | 883 if (start.hasRelationship()) { |
878 // there are relations | 884 // there are relations |
879 Iterable<Relationship> rels = start.getRelationships(type, Direction.OUTGOING); | 885 try (Transaction tx = graphDb.beginTx()) { |
880 for (Relationship rel : rels) { | 886 Iterable<Relationship> rels = start.getRelationships(type, Direction.OUTGOING); |
881 if (rel.getEndNode().equals(end)) { | 887 for (Relationship rel : rels) { |
882 // relation exists | 888 if (rel.getEndNode().equals(end)) { |
883 return rel; | 889 // relation exists |
884 } | 890 tx.success(); |
891 return rel; | |
892 } | |
893 } | |
894 tx.success(); | |
885 } | 895 } |
886 } | 896 } |
887 // create new one | 897 // create new one |
888 Relationship rel; | 898 Relationship rel; |
889 Transaction tx = graphDb.beginTx(); | 899 try (Transaction tx = graphDb.beginTx()) { |
890 try { | |
891 rel = start.createRelationshipTo(end, type); | 900 rel = start.createRelationshipTo(end, type); |
892 tx.success(); | 901 tx.success(); |
893 } finally { | |
894 tx.finish(); | |
895 } | 902 } |
896 return rel; | 903 return rel; |
897 } | 904 } |
898 | 905 |
899 protected Node getOrCreateAnnotationNode(String id) { | 906 protected Node getOrCreateAnnotationNode(String id) { |
900 Index<Node> idx = getNodeIndex(NodeTypes.ANNOTATION); | 907 Index<Node> idx = getNodeIndex(NodeTypes.ANNOTATION); |
901 IndexHits<Node> annotations = idx.get("id", id); | 908 Node annotation; |
902 Node annotation = annotations.getSingle(); | 909 try (Transaction tx = graphDb.beginTx()) { |
903 if (annotation == null) { | 910 IndexHits<Node> annotations = idx.get("id", id); |
904 // does not exist yet | 911 annotation = annotations.getSingle(); |
905 Transaction tx = graphDb.beginTx(); | 912 if (annotation == null) { |
906 try { | 913 // does not exist yet |
907 annotation = graphDb.createNode(); | 914 annotation = graphDb.createNode(); |
908 annotation.setProperty("TYPE", NodeTypes.ANNOTATION.name()); | 915 annotation.setProperty("TYPE", NodeTypes.ANNOTATION.name()); |
909 annotation.setProperty("id", id); | 916 annotation.setProperty("id", id); |
910 idx.add(annotation, "id", id); | 917 idx.add(annotation, "id", id); |
911 tx.success(); | 918 } |
912 } finally { | 919 tx.success(); |
913 tx.finish(); | |
914 } | |
915 } | 920 } |
916 return annotation; | 921 return annotation; |
917 } | 922 } |
918 | 923 |
919 protected Node getOrCreateUriNode(String uri, NodeTypes type) { | 924 protected Node getOrCreateUriNode(String uri, NodeTypes type) { |
920 Index<Node> idx = getNodeIndex(type); | 925 Index<Node> idx = getNodeIndex(type); |
921 IndexHits<Node> targets = idx.get("uri", uri); | 926 Node target; |
922 Node target = targets.getSingle(); | 927 try (Transaction tx = graphDb.beginTx()) { |
923 if (target == null) { | 928 IndexHits<Node> targets = idx.get("uri", uri); |
924 // does not exist yet | 929 target = targets.getSingle(); |
925 Transaction tx = graphDb.beginTx(); | 930 if (target == null) { |
926 try { | 931 // does not exist yet |
927 target = graphDb.createNode(); | 932 target = graphDb.createNode(); |
928 target.setProperty("TYPE", type.name()); | 933 target.setProperty("TYPE", type.name()); |
929 target.setProperty("uri", uri); | 934 target.setProperty("uri", uri); |
930 idx.add(target, "uri", uri); | 935 idx.add(target, "uri", uri); |
931 tx.success(); | 936 } |
932 } finally { | 937 tx.success(); |
933 tx.finish(); | |
934 } | |
935 } | 938 } |
936 return target; | 939 return target; |
937 } | 940 } |
938 | 941 |
939 protected Node getActorNode(Actor actor) { | 942 protected Node getActorNode(Actor actor) { |
940 // Person/Group is identified by URI or id | 943 // Person/Group is identified by URI or id |
941 String uri = actor.getUriString(); | 944 String uri = actor.getUriString(); |
942 Index<Node> idx; | 945 Index<Node> idx; |
946 Node person; | |
943 if (actor.isGroup()) { | 947 if (actor.isGroup()) { |
944 idx = getNodeIndex(NodeTypes.GROUP); | 948 idx = getNodeIndex(NodeTypes.GROUP); |
945 } else { | 949 } else { |
946 idx = getNodeIndex(NodeTypes.PERSON); | 950 idx = getNodeIndex(NodeTypes.PERSON); |
947 } | 951 } |
948 IndexHits<Node> persons = idx.get("uri", uri); | 952 try (Transaction tx = graphDb.beginTx()) { |
949 Node person = persons.getSingle(); | 953 IndexHits<Node> persons = idx.get("uri", uri); |
954 person = persons.getSingle(); | |
955 tx.success(); | |
956 } | |
950 return person; | 957 return person; |
951 } | 958 } |
952 | 959 |
953 protected Node getOrCreateActorNode(Actor actor) { | 960 protected Node getOrCreateActorNode(Actor actor) { |
954 // Person/Group is identified by URI or id | 961 // Person/Group is identified by URI or id |
955 String uri = actor.getUriString(); | 962 String uri = actor.getUriString(); |
956 String name = actor.getName(); | 963 String name = actor.getName(); |
957 String id = actor.getId(); | 964 String id = actor.getId(); |
958 Index<Node> idx; | 965 Node person; |
959 if (actor.isGroup()) { | 966 try (Transaction tx = graphDb.beginTx()) { |
960 idx = getNodeIndex(NodeTypes.GROUP); | 967 Index<Node> idx; |
961 } else { | 968 if (actor.isGroup()) { |
962 idx = getNodeIndex(NodeTypes.PERSON); | 969 idx = getNodeIndex(NodeTypes.GROUP); |
963 } | 970 } else { |
964 IndexHits<Node> persons = idx.get("uri", uri); | 971 idx = getNodeIndex(NodeTypes.PERSON); |
965 Node person = persons.getSingle(); | 972 } |
966 if (person == null) { | 973 IndexHits<Node> persons = idx.get("uri", uri); |
967 // does not exist yet | 974 person = persons.getSingle(); |
968 Transaction tx = graphDb.beginTx(); | 975 if (person == null) { |
969 try { | 976 // does not exist yet |
970 person = graphDb.createNode(); | 977 person = graphDb.createNode(); |
971 if (actor.isGroup()) { | 978 if (actor.isGroup()) { |
972 person.setProperty("TYPE", NodeTypes.GROUP.name()); | 979 person.setProperty("TYPE", NodeTypes.GROUP.name()); |
973 } else { | 980 } else { |
974 person.setProperty("TYPE", NodeTypes.PERSON.name()); | 981 person.setProperty("TYPE", NodeTypes.PERSON.name()); |
979 person.setProperty("name", name); | 986 person.setProperty("name", name); |
980 } | 987 } |
981 if (id != null) { | 988 if (id != null) { |
982 person.setProperty("id", id); | 989 person.setProperty("id", id); |
983 } | 990 } |
984 tx.success(); | 991 } |
985 } finally { | 992 tx.success(); |
986 tx.finish(); | |
987 } | |
988 } | 993 } |
989 return person; | 994 return person; |
990 } | 995 } |
991 | 996 |
992 protected Node getOrCreateTagNode(Tag inTag) { | 997 protected Node getOrCreateTagNode(Tag inTag) { |
993 Index<Node> idx = getNodeIndex(NodeTypes.TAG); | 998 Index<Node> idx = getNodeIndex(NodeTypes.TAG); |
994 String tagname = inTag.getName(); | 999 String tagname = inTag.getName(); |
995 IndexHits<Node> tags = idx.get("name", tagname); | 1000 Node tag; |
996 Node tag = tags.getSingle(); | 1001 try (Transaction tx = graphDb.beginTx()) { |
997 if (tag == null) { | 1002 IndexHits<Node> tags = idx.get("name", tagname); |
998 // does not exist yet | 1003 tag = tags.getSingle(); |
999 Transaction tx = graphDb.beginTx(); | 1004 if (tag == null) { |
1000 try { | 1005 // does not exist yet |
1001 tag = graphDb.createNode(); | 1006 tag = graphDb.createNode(); |
1002 tag.setProperty("TYPE", NodeTypes.TAG.name()); | 1007 tag.setProperty("TYPE", NodeTypes.TAG.name()); |
1003 tag.setProperty("name", tagname); | 1008 tag.setProperty("name", tagname); |
1004 idx.add(tag, "name", tagname); | 1009 idx.add(tag, "name", tagname); |
1005 | 1010 |
1006 tag.setProperty("id", inTag.getId()); | 1011 tag.setProperty("id", inTag.getId()); |
1007 tag.setProperty("uri", inTag.getUri()); | 1012 tag.setProperty("uri", inTag.getUri()); |
1008 idx.add(tag, "uri", inTag.getUri()); | 1013 idx.add(tag, "uri", inTag.getUri()); |
1009 | 1014 } |
1010 tx.success(); | 1015 tx.success(); |
1011 } finally { | |
1012 tx.finish(); | |
1013 } | |
1014 } | 1016 } |
1015 return tag; | 1017 return tag; |
1016 } | 1018 } |
1017 | 1019 |
1018 /** | 1020 /** |
1028 newActorNode = getOrCreateActorNode(actor); | 1030 newActorNode = getOrCreateActorNode(actor); |
1029 } | 1031 } |
1030 Relationship rel = getRelation(annotNode, type, null); | 1032 Relationship rel = getRelation(annotNode, type, null); |
1031 if (rel != null) { | 1033 if (rel != null) { |
1032 // relation exists | 1034 // relation exists |
1033 Node oldActorNode = rel.getEndNode(); | 1035 try (Transaction tx = graphDb.beginTx()) { |
1034 if (!oldActorNode.equals(newActorNode)) { | 1036 Node oldActorNode = rel.getEndNode(); |
1035 // new admin is different | 1037 if (!oldActorNode.equals(newActorNode)) { |
1036 Transaction tx = graphDb.beginTx(); | 1038 // new admin is different |
1037 try { | |
1038 rel.delete(); | 1039 rel.delete(); |
1039 tx.success(); | 1040 tx.success(); |
1040 } finally { | |
1041 tx.finish(); | |
1042 } | 1041 } |
1043 if (newActorNode != null) { | 1042 if (newActorNode != null) { |
1044 rel = getOrCreateRelation(annotNode, type, newActorNode); | 1043 rel = getOrCreateRelation(annotNode, type, newActorNode); |
1045 } | 1044 } |
1045 tx.success(); | |
1046 } | 1046 } |
1047 } else { | 1047 } else { |
1048 // no relation yet | 1048 // no relation yet |
1049 if (newActorNode != null) { | 1049 if (newActorNode != null) { |
1050 rel = getOrCreateRelation(annotNode, type, newActorNode); | 1050 rel = getOrCreateRelation(annotNode, type, newActorNode); |
1056 * Unindexes and deletes given Node if it has no relations. | 1056 * Unindexes and deletes given Node if it has no relations. |
1057 * | 1057 * |
1058 * @param node | 1058 * @param node |
1059 */ | 1059 */ |
1060 protected void deleteNode(Node node) { | 1060 protected void deleteNode(Node node) { |
1061 Transaction tx = graphDb.beginTx(); | 1061 try (Transaction tx = graphDb.beginTx()) { |
1062 try { | |
1063 if (node.hasRelationship()) { | 1062 if (node.hasRelationship()) { |
1064 logger.error("deleteNode: unable to delete: Node still has relations."); | 1063 logger.error("deleteNode: unable to delete: Node still has relations."); |
1065 } else { | 1064 } else { |
1066 String ts = (String) node.getProperty("TYPE", null); | 1065 String ts = (String) node.getProperty("TYPE", null); |
1067 try { | 1066 try { |
1071 logger.error("deleteNode: unable to get TYPE of node: " + node); | 1070 logger.error("deleteNode: unable to get TYPE of node: " + node); |
1072 } | 1071 } |
1073 node.delete(); | 1072 node.delete(); |
1074 } | 1073 } |
1075 tx.success(); | 1074 tx.success(); |
1076 } finally { | |
1077 tx.finish(); | |
1078 } | 1075 } |
1079 } | 1076 } |
1080 | 1077 |
1081 /** | 1078 /** |
1082 * returns the (first) Relationship of RelationTypes type from Node start. | 1079 * returns the (first) Relationship of RelationTypes type from Node start. |
1086 * @param direction | 1083 * @param direction |
1087 * @return | 1084 * @return |
1088 */ | 1085 */ |
1089 protected Relationship getRelation(Node start, RelationTypes type, Direction direction) { | 1086 protected Relationship getRelation(Node start, RelationTypes type, Direction direction) { |
1090 Iterable<Relationship> rels; | 1087 Iterable<Relationship> rels; |
1091 if (direction == null) { | 1088 try (Transaction tx = graphDb.beginTx()) { |
1092 // ignore direction | 1089 if (direction == null) { |
1093 rels = start.getRelationships(type); | 1090 // ignore direction |
1094 } else { | 1091 rels = start.getRelationships(type); |
1095 rels = start.getRelationships(type, direction); | 1092 } else { |
1096 } | 1093 rels = start.getRelationships(type, direction); |
1097 for (Relationship rel : rels) { | 1094 } |
1098 // just the first one | 1095 tx.success(); |
1099 return rel; | 1096 for (Relationship rel : rels) { |
1097 // just the first one | |
1098 return rel; | |
1099 } | |
1100 } | 1100 } |
1101 return null; | 1101 return null; |
1102 } | 1102 } |
1103 | 1103 |
1104 /** | 1104 /** |
1114 | 1114 |
1115 public List<Annotation> getAnnotationsByTag(String tagUri) { | 1115 public List<Annotation> getAnnotationsByTag(String tagUri) { |
1116 ArrayList<Annotation> ret = new ArrayList<Annotation>(); | 1116 ArrayList<Annotation> ret = new ArrayList<Annotation>(); |
1117 Node tag = getTagNodeByUri(tagUri); | 1117 Node tag = getTagNodeByUri(tagUri); |
1118 if (tag != null) { | 1118 if (tag != null) { |
1119 Iterable<Relationship> rels = tag.getRelationships(Direction.INCOMING, RelationTypes.HAS_TAG); | 1119 try (Transaction tx = graphDb.beginTx()) { |
1120 for (Relationship rel : rels) { | 1120 Iterable<Relationship> rels = tag.getRelationships(Direction.INCOMING, RelationTypes.HAS_TAG); |
1121 Node node = rel.getStartNode(); | 1121 for (Relationship rel : rels) { |
1122 ret.add(createAnnotationFromNode(node)); | 1122 Node node = rel.getStartNode(); |
1123 ret.add(createAnnotationFromNode(node)); | |
1124 } | |
1125 tx.success(); | |
1123 } | 1126 } |
1124 } | 1127 } |
1125 return ret; | 1128 return ret; |
1126 } | 1129 } |
1127 | 1130 |
1128 public List<Annotation> getAnnotationsByResource(String resourceUri) { | 1131 public List<Annotation> getAnnotationsByResource(String resourceUri) { |
1129 ArrayList<Annotation> ret = new ArrayList<Annotation>(); | 1132 ArrayList<Annotation> ret = new ArrayList<Annotation>(); |
1130 Node res = getNodeFromIndex("uri", resourceUri, NodeTypes.RESOURCE); | 1133 Node res = getNodeFromIndex("uri", resourceUri, NodeTypes.RESOURCE); |
1131 if (res != null) { | 1134 if (res != null) { |
1132 Iterable<Relationship> rels = res.getRelationships(Direction.INCOMING, RelationTypes.ANNOTATES); | 1135 try (Transaction tx = graphDb.beginTx()) { |
1133 for (Relationship rel : rels) { | 1136 Iterable<Relationship> rels = res.getRelationships(Direction.INCOMING, RelationTypes.ANNOTATES); |
1134 Node an = rel.getStartNode(); | 1137 for (Relationship rel : rels) { |
1135 Node rn = rel.getEndNode(); | 1138 Node an = rel.getStartNode(); |
1136 if (rn.getProperty("TYPE", "").equals("RESOURCE")) { | 1139 Node rn = rel.getEndNode(); |
1137 logger.error("getAnnotationsByResource got ANNOTATES != RESOURCE"); | 1140 if (rn.getProperty("TYPE", "").equals("RESOURCE")) { |
1138 } | 1141 logger.error("getAnnotationsByResource got ANNOTATES != RESOURCE"); |
1139 ret.add(createAnnotationFromNode(an)); | 1142 } |
1143 ret.add(createAnnotationFromNode(an)); | |
1144 } | |
1145 tx.success(); | |
1140 } | 1146 } |
1141 } | 1147 } |
1142 return ret; | 1148 return ret; |
1143 } | 1149 } |
1144 | 1150 |