Ignore:
Timestamp:
Jul 3, 2012, 7:23:17 PM (12 years ago)
Author:
casties
Branch:
default
Message:

version 0.2 really works now

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java

    r4 r5  
    99
    1010import org.apache.log4j.Logger;
     11import org.neo4j.graphdb.Direction;
    1112import org.neo4j.graphdb.GraphDatabaseService;
    1213import org.neo4j.graphdb.Node;
     
    5960        annot.setBodyText((String) annotNode.getProperty("bodyText", null));
    6061        annot.setBodyUri((String) annotNode.getProperty("bodyUri", null));
    61         Iterable<Relationship> targets = annotNode.getRelationships(RelationTypes.ANNOTATES);
    62         for (Relationship target : targets) {
     62        // get annotation target from relation
     63        Iterable<Relationship> targetRels = annotNode.getRelationships(RelationTypes.ANNOTATES);
     64        for (Relationship targetRel : targetRels) {
     65            Node target = targetRel.getEndNode();
    6366            annot.setTargetBaseUri((String) target.getProperty("uri", null));
     67            // just the first one
    6468            break;
    6569        }
     
    6973            annot.setFragmentType(FragmentTypes.valueOf(ft));
    7074        }
    71         Iterable<Relationship> creators = annotNode.getRelationships(RelationTypes.CREATED);
    72         for (Relationship creator : creators) {
     75        // get creator form relation
     76        Iterable<Relationship> creatorRels = annotNode.getRelationships(RelationTypes.CREATED);
     77        for (Relationship creatorRel : creatorRels) {
     78            Node creator = creatorRel.getStartNode();
    7379            annot.setCreatorUri((String) creator.getProperty("uri", null));
     80            annot.setCreatorName((String) creator.getProperty("name", null));
     81            // just the first one
    7482            break;
    7583        }
     
    8694     */
    8795    public Annotation storeAnnotation(Annotation annot) {
     96        Node annotNode = null;
    8897        Transaction tx = graphDb.beginTx();
    89         Node annotNode = null;
    90         String id = annot.getUri();
    91         if (id == null) {
    92             id = createRessourceURI("annot:");
    93         }
    94         annotNode = createAnnotationNode(id);
    95 
    9698        try {
    97             // Mutating operations go here
     99            /*
     100             * create or get the annotation
     101             */
     102            String id = annot.getUri();
     103            if (id == null) {
     104                id = createRessourceURI("annot:");
     105            }
     106            annotNode = getOrCreateAnnotationNode(id);
     107
    98108            /*
    99109             * the annotation body
     
    113123            String targetBaseUri = annot.getTargetBaseUri();
    114124            if (targetBaseUri != null) {
    115                 Node target = createTargetNode(targetBaseUri);
    116                 annotNode.createRelationshipTo(target, RelationTypes.ANNOTATES);
     125                Node target = getOrCreateTargetNode(targetBaseUri);
     126                getOrCreateRelation(annotNode, RelationTypes.ANNOTATES, target);
    117127            }
    118128
     
    128138
    129139            /*
     140             * The name of the creator of this annotation.
     141             */
     142            String creatorName = annot.getCreatorName();
     143
     144            /*
    130145             * The URI of the creator of this annotation.
    131146             */
    132147            String creatorUri = annot.getCreatorUri();
    133148            if (creatorUri != null) {
    134                 Node creator = createPersonNode(creatorUri, null);
    135                 creator.createRelationshipTo(annotNode, RelationTypes.CREATED);
     149                Node creator = getOrCreatePersonNode(creatorUri, creatorName);
     150                getOrCreateRelation(creator, RelationTypes.CREATED, annotNode);
    136151            }
    137152
     
    181196            if (target != null) {
    182197                Iterable<Relationship> relations = target.getRelationships(RelationTypes.ANNOTATES);
    183                 for (Relationship relation: relations) {
     198                for (Relationship relation : relations) {
    184199                    Node ann = relation.getStartNode();
    185200                    if (ann.getProperty("TYPE", "").equals("ANNOTATION")) {
     
    187202                        annotations.add(annot);
    188203                    } else {
    189                         logger.error("ANNOTATES relation does not start with ANNOTATION: "+ann);
     204                        logger.error("ANNOTATES relation does not start with ANNOTATION: " + ann);
    190205                    }
    191206                }
     
    198213            if (person != null) {
    199214                Iterable<Relationship> relations = person.getRelationships(RelationTypes.CREATED);
    200                 for (Relationship relation: relations) {
     215                for (Relationship relation : relations) {
    201216                    Node ann = relation.getEndNode();
    202217                    if (ann.getProperty("TYPE", "").equals("ANNOTATION")) {
     
    204219                        annotations.add(annot);
    205220                    } else {
    206                         logger.error("CREATED relation does not end with ANNOTATION: "+ann);
     221                        logger.error("CREATED relation does not end with ANNOTATION: " + ann);
    207222                    }
    208223                }
     
    212227    }
    213228
    214     protected Node createAnnotationNode(String id) {
     229    protected Relationship getOrCreateRelation(Node start, RelationshipType type, Node end) {
     230        if (start.hasRelationship()) {
     231            // there are relations
     232            Iterable<Relationship> rels = start.getRelationships(type, Direction.OUTGOING);
     233            for (Relationship rel : rels) {
     234                if (rel.getEndNode().equals(end)) {
     235                    // relation exists
     236                    return rel;
     237                }
     238            }
     239        }
     240        // create new one
     241        Relationship rel;
     242        Transaction tx = graphDb.beginTx();
     243        try {
     244            rel = start.createRelationshipTo(end, type);
     245            tx.success();
     246        } finally {
     247            tx.finish();
     248        }
     249        return rel;
     250    }
     251
     252    protected Node getOrCreateAnnotationNode(String id) {
    215253        Index<Node> idx = graphDb.index().forNodes("annotations");
    216254        IndexHits<Node> annotations = idx.get("id", id);
     
    232270    }
    233271
    234     protected Node createTargetNode(String uri) {
     272    protected Node getOrCreateTargetNode(String uri) {
    235273        Index<Node> idx = graphDb.index().forNodes("targets");
    236274        IndexHits<Node> targets = idx.get("uri", uri);
     
    252290    }
    253291
    254     protected Node createPersonNode(String uri, String name) {
     292    protected Node getOrCreatePersonNode(String uri, String name) {
    255293        Index<Node> idx = graphDb.index().forNodes("persons");
    256294        // Person is identified by URI
     
    291329    }
    292330
    293 
    294331}
Note: See TracChangeset for help on using the changeset viewer.