Changeset 12:5928c5d9aae8 in AnnotationManagerN4J for src


Ignore:
Timestamp:
Jul 13, 2012, 1:02:06 PM (12 years ago)
Author:
casties
Branch:
default
Message:

more work on permissions...

Location:
src/main/java/de/mpiwg/itgroup/annotations
Files:
3 edited

Legend:

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

    r10 r12  
    1111
    1212    public Group(String id) {
     13        super();
    1314        this.id = id;
     15    }
     16
     17    public Group(String uri, String name) {
     18        super();
     19        this.uri = uri;
     20        this.name = name;
    1421    }
    1522
  • src/main/java/de/mpiwg/itgroup/annotations/Person.java

    r10 r12  
    1010public class Person extends Actor {
    1111
     12    public Person() {
     13    }
     14
     15    public Person(String id) {
     16        super();
     17        this.id = id;
     18    }
     19
    1220    public Person(String uri, String name) {
    1321        super();
    1422        this.uri = uri;
    1523        this.name = name;
    16     }
    17 
    18     public Person() {
    19     }
    20 
    21     public Person(String id) {
    22         this.id = id;
    2324    }
    2425
  • src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java

    r11 r12  
    2121import de.mpiwg.itgroup.annotations.Annotation;
    2222import de.mpiwg.itgroup.annotations.Annotation.FragmentTypes;
     23import de.mpiwg.itgroup.annotations.Group;
    2324import de.mpiwg.itgroup.annotations.Person;
    2425
     
    2930public class AnnotationStore {
    3031
    31         protected static Logger logger = Logger.getLogger(AnnotationStore.class);
    32 
    33         protected GraphDatabaseService graphDb;
    34        
    35         public static enum NodeTypes {
    36                 ANNOTATION, PERSON, TARGET
    37         }
    38 
    39         protected List<Index<Node>> nodeIndexes;
    40        
    41         public static enum RelationTypes implements RelationshipType {
    42                 ANNOTATES, CREATED, PERMITS
    43         }
    44 
    45         public static String ANNOTATION_URI_BASE = "http://entities.mpiwg-berlin.mpg.de/annotations/";
    46 
    47         public AnnotationStore(GraphDatabaseService graphDb) {
    48                 super();
    49                 this.graphDb = graphDb;
    50                 nodeIndexes = new ArrayList<Index<Node>>(3);
    51                 // List.set(enum.ordinal(), val) seems not to work.
    52                 nodeIndexes.add(NodeTypes.ANNOTATION.ordinal(), graphDb.index().forNodes("annotations"));
    53                 nodeIndexes.add(NodeTypes.PERSON.ordinal(), graphDb.index().forNodes("persons"));
    54                 nodeIndexes.add(NodeTypes.TARGET.ordinal(), graphDb.index().forNodes("targets"));
    55         }
    56 
    57         protected Index<Node> getNodeIndex(NodeTypes type) {
    58                 return nodeIndexes.get(type.ordinal());
    59         }
    60        
    61         /**
    62          * Returns the Annotation with the given id.
    63          *
    64          * @param id
    65          * @return
    66          */
    67         public Annotation getAnnotationById(String id) {
    68                 Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle();
    69                 Annotation annot = createAnnotationFromNode(annotNode);
    70                 return annot;
    71         }
    72 
    73         /**
    74          * Returns an Annotation object from an annotation-Node.
    75          *
    76          * @param annotNode
    77          * @return
    78          */
    79         public Annotation createAnnotationFromNode(Node annotNode) {
    80                 Annotation annot = new Annotation();
    81                 annot.setUri((String) annotNode.getProperty("id", null));
    82                 annot.setBodyText((String) annotNode.getProperty("bodyText", null));
    83                 annot.setBodyUri((String) annotNode.getProperty("bodyUri", null));
    84                 // get annotation target from relation
    85                 Iterable<Relationship> targetRels = annotNode
    86                                 .getRelationships(RelationTypes.ANNOTATES);
    87                 for (Relationship targetRel : targetRels) {
    88                         Node target = targetRel.getEndNode();
    89                         annot.setTargetBaseUri((String) target.getProperty("uri", null));
    90                         // just the first one
    91                         break;
    92                 }
    93                 annot.setTargetFragment((String) annotNode.getProperty(
    94                                 "targetFragment", null));
    95                 String ft = (String) annotNode.getProperty("fragmentType", null);
    96                 if (ft != null) {
    97                         annot.setFragmentType(FragmentTypes.valueOf(ft));
    98                 }
    99                 // get creator from relation
    100                 Iterable<Relationship> creatorRels = annotNode
    101                                 .getRelationships(RelationTypes.CREATED);
    102                 for (Relationship creatorRel : creatorRels) {
    103                         Node creatorNode = creatorRel.getStartNode();
    104                         String uri = (String) creatorNode.getProperty("uri", null);
    105                         String name = (String) creatorNode.getProperty("name", null);
    106             Actor creator = new Person(uri, name);
     32    protected static Logger logger = Logger.getLogger(AnnotationStore.class);
     33
     34    protected GraphDatabaseService graphDb;
     35
     36    public static enum NodeTypes {
     37        ANNOTATION, PERSON, TARGET
     38    }
     39
     40    protected List<Index<Node>> nodeIndexes;
     41
     42    public static enum RelationTypes implements RelationshipType {
     43        ANNOTATES, CREATED, PERMITS_ADMIN, PERMITS_DELETE, PERMITS_UPDATE, PERMITS_READ
     44    }
     45
     46    public static String ANNOTATION_URI_BASE = "http://entities.mpiwg-berlin.mpg.de/annotations/";
     47
     48    public AnnotationStore(GraphDatabaseService graphDb) {
     49        super();
     50        this.graphDb = graphDb;
     51        nodeIndexes = new ArrayList<Index<Node>>(3);
     52        // List.set(enum.ordinal(), val) seems not to work.
     53        nodeIndexes.add(NodeTypes.ANNOTATION.ordinal(), graphDb.index().forNodes("annotations"));
     54        nodeIndexes.add(NodeTypes.PERSON.ordinal(), graphDb.index().forNodes("persons"));
     55        nodeIndexes.add(NodeTypes.TARGET.ordinal(), graphDb.index().forNodes("targets"));
     56    }
     57
     58    protected Index<Node> getNodeIndex(NodeTypes type) {
     59        return nodeIndexes.get(type.ordinal());
     60    }
     61
     62    /**
     63     * Returns the Annotation with the given id.
     64     *
     65     * @param id
     66     * @return
     67     */
     68    public Annotation getAnnotationById(String id) {
     69        Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle();
     70        Annotation annot = createAnnotationFromNode(annotNode);
     71        return annot;
     72    }
     73
     74    /**
     75     * Returns an Annotation object from an annotation-Node.
     76     *
     77     * @param annotNode
     78     * @return
     79     */
     80    public Annotation createAnnotationFromNode(Node annotNode) {
     81        Annotation annot = new Annotation();
     82        annot.setUri((String) annotNode.getProperty("id", null));
     83        annot.setBodyText((String) annotNode.getProperty("bodyText", null));
     84        annot.setBodyUri((String) annotNode.getProperty("bodyUri", null));
     85        // get annotation target from relation
     86        Relationship targetRel = getRelation(annotNode, RelationTypes.ANNOTATES, null);
     87        if (targetRel != null) {
     88            Node target = targetRel.getEndNode();
     89            annot.setTargetBaseUri((String) target.getProperty("uri", null));
     90        } else {
     91            logger.error("annotation "+annotNode+" has no target node!");
     92        }
     93        annot.setTargetFragment((String) annotNode.getProperty("targetFragment", null));
     94        String ft = (String) annotNode.getProperty("fragmentType", null);
     95        if (ft != null) {
     96            annot.setFragmentType(FragmentTypes.valueOf(ft));
     97        }
     98        // get creator from relation
     99        Relationship creatorRel = getRelation(annotNode, RelationTypes.CREATED, null);
     100        if (creatorRel != null) {
     101            Node creatorNode = creatorRel.getStartNode();
     102            Actor creator = createActorFromNode(creatorNode);
    107103            annot.setCreator(creator);
    108                         // just the first one
    109                         break;
    110                 }
    111                 annot.setCreated((String) annotNode.getProperty("created", null));
    112                 return annot;
    113         }
    114 
    115         /**
    116          * Store a new annotation in the store or update an existing one. Returns
    117          * the stored annotation.
    118          *
    119          * @param annot
    120          * @return
    121          */
    122         public Annotation storeAnnotation(Annotation annot) {
    123                 Node annotNode = null;
    124                 Transaction tx = graphDb.beginTx();
    125                 try {
    126                         /*
    127                          * create or get the annotation
    128                          */
    129                         String id = annot.getUri();
    130                         if (id == null) {
    131                                 id = createRessourceURI("annot:");
    132                         }
    133                         annotNode = getOrCreateAnnotationNode(id);
    134 
    135                         /*
    136                          * the annotation body
    137                          */
    138                         String bodyText = annot.getBodyText();
    139                         if (bodyText != null) {
    140                                 annotNode.setProperty("bodyText", bodyText);
    141                         }
    142                         String bodyUri = annot.getBodyUri();
    143                         if (bodyUri != null) {
    144                                 annotNode.setProperty("bodyUri", bodyUri);
    145                         }
    146 
    147                         /*
    148                          * the annotation target
    149                          */
    150                         String targetBaseUri = annot.getTargetBaseUri();
    151                         if (targetBaseUri != null) {
    152                                 Node target = getOrCreateTargetNode(targetBaseUri);
    153                                 getOrCreateRelation(annotNode, RelationTypes.ANNOTATES, target);
    154                         }
    155 
    156                         /*
    157                          * The fragment part of the annotation target.
    158                          */
    159                         String targetFragment = annot.getTargetFragment();
    160                         FragmentTypes fragmentType = annot.getFragmentType();
    161                         if (targetFragment != null) {
    162                                 annotNode.setProperty("targetFragment", targetFragment);
    163                                 annotNode.setProperty("fragmentType", fragmentType.name());
    164                         }
    165 
    166                         /*
    167                          * The creator of this annotation.
    168                          */
    169                         Actor creator = annot.getCreator();
    170                         if (creator != null) {
    171                                 Node creatorNode = getOrCreatePersonNode(creator);
    172                                 getOrCreateRelation(creatorNode, RelationTypes.CREATED, annotNode);
    173                         }
    174 
    175                         /*
    176                          * The creation date of this annotation.
    177                          */
    178                         String created = annot.getCreated();
    179                         if (created != null) {
    180                                 annotNode.setProperty("created", created);
    181                         }
    182                        
    183                         /*
    184                          * Permissions for this annotation.
    185                          */
    186                         Actor admin = annot.getAdminPermission();
    187                         if (admin != null) {
    188                                
    189                         }
    190 
    191                         tx.success();
    192                 } finally {
    193                         tx.finish();
    194                 }
    195 
    196                 // re-read and return annotation
    197                 Annotation storedAnnot = createAnnotationFromNode(annotNode);
    198                 return storedAnnot;
    199         }
    200 
    201         /**
    202          * Deletes the annotation with the given id.
    203          *
    204          * @param id
    205          */
    206         public void deleteById(String id) {
    207                 Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle();
    208                 if (annotNode != null) {
    209                         // delete related objects
    210                         Transaction tx = graphDb.beginTx();
    211                         try {
    212                                 for (Relationship rel : annotNode.getRelationships()) {
    213                                         // delete relation and the related node if it has no other relations
    214                                         Node other = rel.getOtherNode(annotNode);                                       
    215                                         rel.delete();
    216                                         if (! other.hasRelationship()) {
    217                                                 deleteNode(other);
    218                                         }
    219                                 }
    220                                 if (! annotNode.hasRelationship()) {
    221                                         deleteNode(annotNode);
    222                                 } else {
    223                                         logger.error("deleteById: unable to delete: Node still has relations.");
    224                                 }
    225                                 tx.success();
    226                         } finally {
    227                                 tx.finish();
    228                         }
    229                 }
    230         }
    231 
    232         /**
    233          * Returns all annotations with the given uri and/or user.
    234          *
    235          * @param uri
    236          * @param userUri
    237          * @param limit
    238          * @param offset
    239          * @return
    240          */
    241         public List<Annotation> searchByUriUser(String targetUri, String userUri,
    242                         String limit, String offset) {
    243                 List<Annotation> annotations = new ArrayList<Annotation>();
    244                 if (targetUri != null) {
    245                         // there should be only one
    246                         Node target = getNodeIndex(NodeTypes.TARGET).get("uri", targetUri).getSingle();
    247                         if (target != null) {
    248                                 Iterable<Relationship> relations = target
    249                                                 .getRelationships(RelationTypes.ANNOTATES);
    250                                 for (Relationship relation : relations) {
    251                                         Node ann = relation.getStartNode();
    252                                         if (ann.getProperty("TYPE", "").equals("ANNOTATION")) {
    253                                                 Annotation annot = createAnnotationFromNode(ann);
    254                                                 annotations.add(annot);
    255                                         } else {
    256                                                 logger.error("ANNOTATES relation does not start with ANNOTATION: "
    257                                                                 + ann);
    258                                         }
    259                                 }
    260                         }
    261                 }
    262                 if (userUri != null) {
    263                         // there should be only one
    264                         Node person = getNodeIndex(NodeTypes.PERSON).get("uri", userUri).getSingle();
    265                         if (person != null) {
    266                                 Iterable<Relationship> relations = person
    267                                                 .getRelationships(RelationTypes.CREATED);
    268                                 for (Relationship relation : relations) {
    269                                         Node ann = relation.getEndNode();
    270                                         if (ann.getProperty("TYPE", "").equals("ANNOTATION")) {
    271                                                 Annotation annot = createAnnotationFromNode(ann);
    272                                                 annotations.add(annot);
    273                                         } else {
    274                                                 logger.error("CREATED relation does not end with ANNOTATION: "
    275                                                                 + ann);
    276                                         }
    277                                 }
    278                         }
    279                 }
    280                 return annotations;
    281         }
    282 
    283         protected Relationship getOrCreateRelation(Node start,
    284                         RelationshipType type, Node end) {
    285                 if (start.hasRelationship()) {
    286                         // there are relations
    287                         Iterable<Relationship> rels = start.getRelationships(type,
    288                                         Direction.OUTGOING);
    289                         for (Relationship rel : rels) {
    290                                 if (rel.getEndNode().equals(end)) {
    291                                         // relation exists
    292                                         return rel;
    293                                 }
    294                         }
    295                 }
    296                 // create new one
    297                 Relationship rel;
    298                 Transaction tx = graphDb.beginTx();
    299                 try {
    300                         rel = start.createRelationshipTo(end, type);
    301                         tx.success();
    302                 } finally {
    303                         tx.finish();
    304                 }
    305                 return rel;
    306         }
    307 
    308         protected Node getOrCreateAnnotationNode(String id) {
    309                 Index<Node> idx = getNodeIndex(NodeTypes.ANNOTATION);
    310                 IndexHits<Node> annotations = idx.get("id", id);
    311                 Node annotation = annotations.getSingle();
    312                 if (annotation == null) {
    313                         // does not exist yet
    314                         Transaction tx = graphDb.beginTx();
    315                         try {
    316                                 annotation = graphDb.createNode();
    317                                 annotation.setProperty("TYPE", NodeTypes.ANNOTATION.name());
    318                                 annotation.setProperty("id", id);
    319                                 idx.add(annotation, "id", id);
    320                                 tx.success();
    321                         } finally {
    322                                 tx.finish();
    323                         }
    324                 }
    325                 return annotation;
    326         }
    327 
    328         protected Node getOrCreateTargetNode(String uri) {
    329                 Index<Node> idx = getNodeIndex(NodeTypes.TARGET);
    330                 IndexHits<Node> targets = idx.get("uri", uri);
    331                 Node target = targets.getSingle();
    332                 if (target == null) {
    333                         // does not exist yet
    334                         Transaction tx = graphDb.beginTx();
    335                         try {
    336                                 target = graphDb.createNode();
    337                                 target.setProperty("TYPE", NodeTypes.TARGET.name());
    338                                 target.setProperty("uri", uri);
    339                                 idx.add(target, "uri", uri);
    340                                 tx.success();
    341                         } finally {
    342                                 tx.finish();
    343                         }
    344                 }
    345                 return target;
    346         }
    347 
    348         protected Node getOrCreatePersonNode(Actor actor) {
    349             /*
    350                 // Person is identified by URI
    351                 Index<Node> idx = getNodeIndex(NodeTypes.PERSON);
    352                 IndexHits<Node> persons = idx.get("uri", uri);
    353                 Node person = persons.getSingle();
    354                 if (person == null) {
    355                         // does not exist yet
    356                         Transaction tx = graphDb.beginTx();
    357                         try {
    358                                 person = graphDb.createNode();
    359                                 person.setProperty("TYPE", NodeTypes.PERSON.name());
    360                                 person.setProperty("uri", uri);
    361                                 idx.add(person, "uri", uri);
    362                                 if (name != null) {
    363                                         person.setProperty("name", name);
    364                                 }
    365                                 tx.success();
    366                         } finally {
    367                                 tx.finish();
    368                         }
    369                 }
    370                 return person;
    371                 */
    372             return null;
    373         }
    374 
    375         /**
    376          * Unindexes and deletes given Node if it has no relations.
    377          * @param node
    378          */
    379         protected void deleteNode(Node node) {
    380                 Transaction tx = graphDb.beginTx();
    381                 try {
    382                         if (node.hasRelationship()) {
    383                                 logger.error("deleteNode: unable to delete: Node still has relations.");
    384                         } else {
    385                                 String ts = (String) node.getProperty("TYPE", null);
    386                                 try {
    387                                         NodeTypes type = NodeTypes.valueOf(ts);
    388                                         getNodeIndex(type).remove(node);
    389                                 } catch (Exception e) {
    390                                         logger.error("deleteNode: unable to get TYPE of node: "+node);
    391                                 }
    392                                 node.delete();
    393                         }
    394                         tx.success();
    395                 } finally {
    396                         tx.finish();
    397                 }
    398         }
    399        
    400         /**
    401          * Erzeuge eine urn aus der aktuellen Zeit in millis
    402          *
    403          * @return
    404          */
    405         private String createRessourceURI(String prefix) {
    406 
    407                 Calendar cal = Calendar.getInstance();
    408 
    409                 long time = cal.getTimeInMillis();
    410 
    411                 return String.format("%s%s%s", ANNOTATION_URI_BASE, prefix, time);
    412 
    413         }
     104        } else {
     105            logger.error("annotation "+annotNode+" has no creator node!");
     106        }
     107        // get creation date
     108        annot.setCreated((String) annotNode.getProperty("created", null));
     109        // get permissions
     110        Relationship adminRel = getRelation(annotNode, RelationTypes.PERMITS_ADMIN, null);
     111        if (adminRel != null) {
     112            Node adminNode = adminRel.getEndNode();
     113            //FIXME
     114        }
     115       
     116        return annot;
     117    }
     118
     119    /**
     120     * Returns an Actor object from a node.
     121     *
     122     * @param actorNode
     123     * @return
     124     */
     125    protected Actor createActorFromNode(Node actorNode) {
     126        String uri = (String) actorNode.getProperty("uri", null);
     127        String name = (String) actorNode.getProperty("name", null);
     128        String type = (String) actorNode.getProperty("TYPE", null);
     129        Actor creator = null;
     130        if (type != null && type.equals("PERSON")) {
     131            creator = new Person(uri, name);
     132        } else if (type != null && type.equals("GROUP")) {
     133            creator = new Group(uri, name);
     134        }
     135        return creator;
     136    }
     137
     138    /**
     139     * Store a new annotation in the store or update an existing one. Returns
     140     * the stored annotation.
     141     *
     142     * @param annot
     143     * @return
     144     */
     145    public Annotation storeAnnotation(Annotation annot) {
     146        Node annotNode = null;
     147        Transaction tx = graphDb.beginTx();
     148        try {
     149            /*
     150             * create or get the annotation
     151             */
     152            String id = annot.getUri();
     153            if (id == null) {
     154                id = createRessourceURI("annot:");
     155            }
     156            annotNode = getOrCreateAnnotationNode(id);
     157
     158            /*
     159             * the annotation body
     160             */
     161            String bodyText = annot.getBodyText();
     162            if (bodyText != null) {
     163                annotNode.setProperty("bodyText", bodyText);
     164            }
     165            String bodyUri = annot.getBodyUri();
     166            if (bodyUri != null) {
     167                annotNode.setProperty("bodyUri", bodyUri);
     168            }
     169
     170            /*
     171             * the annotation target
     172             */
     173            String targetBaseUri = annot.getTargetBaseUri();
     174            if (targetBaseUri != null) {
     175                Node target = getOrCreateTargetNode(targetBaseUri);
     176                getOrCreateRelation(annotNode, RelationTypes.ANNOTATES, target);
     177            }
     178
     179            /*
     180             * The fragment part of the annotation target.
     181             */
     182            String targetFragment = annot.getTargetFragment();
     183            FragmentTypes fragmentType = annot.getFragmentType();
     184            if (targetFragment != null) {
     185                annotNode.setProperty("targetFragment", targetFragment);
     186                annotNode.setProperty("fragmentType", fragmentType.name());
     187            }
     188
     189            /*
     190             * The creator of this annotation.
     191             */
     192            Actor creator = annot.getCreator();
     193            if (creator != null) {
     194                Node creatorNode = getOrCreatePersonNode(creator);
     195                getOrCreateRelation(creatorNode, RelationTypes.CREATED, annotNode);
     196            }
     197
     198            /*
     199             * The creation date of this annotation.
     200             */
     201            String created = annot.getCreated();
     202            if (created != null) {
     203                annotNode.setProperty("created", created);
     204            }
     205
     206            /*
     207             * Permissions for this annotation.
     208             */
     209            setPermissionRelation(annotNode, RelationTypes.PERMITS_ADMIN, annot.getAdminPermission());
     210            setPermissionRelation(annotNode, RelationTypes.PERMITS_DELETE, annot.getDeletePermission());
     211            setPermissionRelation(annotNode, RelationTypes.PERMITS_UPDATE, annot.getUpdatePermission());
     212            setPermissionRelation(annotNode, RelationTypes.PERMITS_READ, annot.getReadPermission());
     213
     214            tx.success();
     215        } finally {
     216            tx.finish();
     217        }
     218
     219        // re-read and return annotation
     220        Annotation storedAnnot = createAnnotationFromNode(annotNode);
     221        return storedAnnot;
     222    }
     223
     224    /**
     225     * Deletes the annotation with the given id.
     226     *
     227     * @param id
     228     */
     229    public void deleteById(String id) {
     230        Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle();
     231        if (annotNode != null) {
     232            // delete related objects
     233            Transaction tx = graphDb.beginTx();
     234            try {
     235                for (Relationship rel : annotNode.getRelationships()) {
     236                    // delete relation and the related node if it has no other
     237                    // relations
     238                    Node other = rel.getOtherNode(annotNode);
     239                    rel.delete();
     240                    if (!other.hasRelationship()) {
     241                        deleteNode(other);
     242                    }
     243                }
     244                if (!annotNode.hasRelationship()) {
     245                    deleteNode(annotNode);
     246                } else {
     247                    logger.error("deleteById: unable to delete: Node still has relations.");
     248                }
     249                tx.success();
     250            } finally {
     251                tx.finish();
     252            }
     253        }
     254    }
     255
     256    /**
     257     * Returns all annotations with the given uri and/or user.
     258     *
     259     * @param uri
     260     * @param userUri
     261     * @param limit
     262     * @param offset
     263     * @return
     264     */
     265    public List<Annotation> searchByUriUser(String targetUri, String userUri, String limit, String offset) {
     266        List<Annotation> annotations = new ArrayList<Annotation>();
     267        if (targetUri != null) {
     268            // there should be only one
     269            Node target = getNodeIndex(NodeTypes.TARGET).get("uri", targetUri).getSingle();
     270            if (target != null) {
     271                Iterable<Relationship> relations = target.getRelationships(RelationTypes.ANNOTATES);
     272                for (Relationship relation : relations) {
     273                    Node ann = relation.getStartNode();
     274                    if (ann.getProperty("TYPE", "").equals("ANNOTATION")) {
     275                        Annotation annot = createAnnotationFromNode(ann);
     276                        annotations.add(annot);
     277                    } else {
     278                        logger.error("ANNOTATES relation does not start with ANNOTATION: " + ann);
     279                    }
     280                }
     281            }
     282        }
     283        if (userUri != null) {
     284            // there should be only one
     285            Node person = getNodeIndex(NodeTypes.PERSON).get("uri", userUri).getSingle();
     286            if (person != null) {
     287                Iterable<Relationship> relations = person.getRelationships(RelationTypes.CREATED);
     288                for (Relationship relation : relations) {
     289                    Node ann = relation.getEndNode();
     290                    if (ann.getProperty("TYPE", "").equals("ANNOTATION")) {
     291                        Annotation annot = createAnnotationFromNode(ann);
     292                        annotations.add(annot);
     293                    } else {
     294                        logger.error("CREATED relation does not end with ANNOTATION: " + ann);
     295                    }
     296                }
     297            }
     298        }
     299        return annotations;
     300    }
     301
     302    protected Relationship getOrCreateRelation(Node start, RelationshipType type, Node end) {
     303        if (start.hasRelationship()) {
     304            // there are relations
     305            Iterable<Relationship> rels = start.getRelationships(type, Direction.OUTGOING);
     306            for (Relationship rel : rels) {
     307                if (rel.getEndNode().equals(end)) {
     308                    // relation exists
     309                    return rel;
     310                }
     311            }
     312        }
     313        // create new one
     314        Relationship rel;
     315        Transaction tx = graphDb.beginTx();
     316        try {
     317            rel = start.createRelationshipTo(end, type);
     318            tx.success();
     319        } finally {
     320            tx.finish();
     321        }
     322        return rel;
     323    }
     324
     325    protected Node getOrCreateAnnotationNode(String id) {
     326        Index<Node> idx = getNodeIndex(NodeTypes.ANNOTATION);
     327        IndexHits<Node> annotations = idx.get("id", id);
     328        Node annotation = annotations.getSingle();
     329        if (annotation == null) {
     330            // does not exist yet
     331            Transaction tx = graphDb.beginTx();
     332            try {
     333                annotation = graphDb.createNode();
     334                annotation.setProperty("TYPE", NodeTypes.ANNOTATION.name());
     335                annotation.setProperty("id", id);
     336                idx.add(annotation, "id", id);
     337                tx.success();
     338            } finally {
     339                tx.finish();
     340            }
     341        }
     342        return annotation;
     343    }
     344
     345    protected Node getOrCreateTargetNode(String uri) {
     346        Index<Node> idx = getNodeIndex(NodeTypes.TARGET);
     347        IndexHits<Node> targets = idx.get("uri", uri);
     348        Node target = targets.getSingle();
     349        if (target == null) {
     350            // does not exist yet
     351            Transaction tx = graphDb.beginTx();
     352            try {
     353                target = graphDb.createNode();
     354                target.setProperty("TYPE", NodeTypes.TARGET.name());
     355                target.setProperty("uri", uri);
     356                idx.add(target, "uri", uri);
     357                tx.success();
     358            } finally {
     359                tx.finish();
     360            }
     361        }
     362        return target;
     363    }
     364
     365    protected Node getOrCreatePersonNode(Actor actor) {
     366        /*
     367         * // Person is identified by URI Index<Node> idx =
     368         * getNodeIndex(NodeTypes.PERSON); IndexHits<Node> persons =
     369         * idx.get("uri", uri); Node person = persons.getSingle(); if (person ==
     370         * null) { // does not exist yet Transaction tx = graphDb.beginTx(); try
     371         * { person = graphDb.createNode(); person.setProperty("TYPE",
     372         * NodeTypes.PERSON.name()); person.setProperty("uri", uri);
     373         * idx.add(person, "uri", uri); if (name != null) {
     374         * person.setProperty("name", name); } tx.success(); } finally {
     375         * tx.finish(); } } return person;
     376         */
     377        return null;
     378    }
     379
     380    /**
     381     * Create or update permissions relations for an annotation.
     382     *
     383     * @param annotNode
     384     * @param type
     385     * @param annot
     386     */
     387    protected void setPermissionRelation(Node annotNode, RelationTypes type, Actor actor) {
     388        Node newActorNode = null;
     389        if (actor != null) {
     390            newActorNode = getOrCreatePersonNode(actor);
     391        }
     392        Relationship rel = getRelation(annotNode, type, null);
     393        if (rel != null) {
     394            // relation exists
     395            Node oldActorNode = rel.getEndNode();
     396            if (!oldActorNode.equals(newActorNode)) {
     397                // new admin is different
     398                rel.delete();
     399                if (newActorNode != null) {
     400                    rel = getOrCreateRelation(annotNode, type, newActorNode);
     401                }
     402            }
     403        } else {
     404            // no relation yet
     405            if (newActorNode != null) {
     406                rel = getOrCreateRelation(annotNode, type, newActorNode);
     407            }
     408        }
     409    }
     410
     411    /**
     412     * Unindexes and deletes given Node if it has no relations.
     413     *
     414     * @param node
     415     */
     416    protected void deleteNode(Node node) {
     417        Transaction tx = graphDb.beginTx();
     418        try {
     419            if (node.hasRelationship()) {
     420                logger.error("deleteNode: unable to delete: Node still has relations.");
     421            } else {
     422                String ts = (String) node.getProperty("TYPE", null);
     423                try {
     424                    NodeTypes type = NodeTypes.valueOf(ts);
     425                    getNodeIndex(type).remove(node);
     426                } catch (Exception e) {
     427                    logger.error("deleteNode: unable to get TYPE of node: " + node);
     428                }
     429                node.delete();
     430            }
     431            tx.success();
     432        } finally {
     433            tx.finish();
     434        }
     435    }
     436
     437    protected Relationship getRelation(Node start, RelationTypes type, Direction direction) {
     438        Iterable<Relationship> rels;
     439        if (direction == null) {
     440            // ignore direction
     441            rels = start.getRelationships(type);
     442        } else {
     443            rels = start.getRelationships(type, direction);
     444        }
     445        for (Relationship rel : rels) {
     446            return rel;
     447        }
     448        return null;
     449    }
     450
     451    /**
     452     * Erzeuge eine urn aus der aktuellen Zeit in millis
     453     *
     454     * @return
     455     */
     456    private String createRessourceURI(String prefix) {
     457
     458        Calendar cal = Calendar.getInstance();
     459
     460        long time = cal.getTimeInMillis();
     461
     462        return String.format("%s%s%s", ANNOTATION_URI_BASE, prefix, time);
     463
     464    }
    414465
    415466}
Note: See TracChangeset for help on using the changeset viewer.