Changeset 16:794077e6288c in AnnotationManagerN4J for src


Ignore:
Timestamp:
Sep 4, 2012, 6:02:59 PM (12 years ago)
Author:
casties
Branch:
default
Message:

CLOSED - # 252: Tags for Annotations
https://it-dev.mpiwg-berlin.mpg.de/tracs/mpdl-project-software/ticket/252

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

Legend:

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

    r15 r16  
    3232    public boolean isEquivalentWith(Person person, AnnotationStore store) {
    3333        if (person == null) return false;
    34         if (person.equals(getIdString())) {
    35             return true;
    36         }
     34        if (person.equals(this)) return true;
     35        if (person.getIdString().equals(this.getIdString())) return true;
    3736        if (isGroup() && store != null) {
    3837            // check if person in group
  • src/main/java/de/mpiwg/itgroup/annotations/Annotation.java

    r15 r16  
    33 */
    44package de.mpiwg.itgroup.annotations;
     5
     6import java.util.List;
     7import java.util.Set;
    58
    69import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
     
    8285     */
    8386    protected Actor readPermission;
    84    
     87       
     88    /**
     89     * List of tags on this Annotation.
     90     */
     91    protected Set<String> tags;
    8592   
    8693    /**
     
    318325        this.readPermission = readPermission;
    319326    }
     327
     328    /**
     329     * @return the tags
     330     */
     331    public Set<String> getTags() {
     332        return tags;
     333    }
     334
     335    /**
     336     * @param tags the tags to set
     337     */
     338    public void setTags(Set<String> tags) {
     339        this.tags = tags;
     340    }
    320341   
    321342   
  • src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java

    r15 r16  
    66import java.util.ArrayList;
    77import java.util.Calendar;
     8import java.util.HashSet;
    89import java.util.List;
     10import java.util.Set;
    911
    1012import org.apache.log4j.Logger;
     
    3537
    3638    public static enum NodeTypes {
    37         ANNOTATION, PERSON, TARGET, GROUP 
     39        ANNOTATION, PERSON, TARGET, GROUP, TAG
    3840    }
    3941
     
    4143
    4244    public static enum RelationTypes implements RelationshipType {
    43         ANNOTATES, CREATED, PERMITS_ADMIN, PERMITS_DELETE, PERMITS_UPDATE, PERMITS_READ, MEMBER_OF
     45        ANNOTATES, CREATED, PERMITS_ADMIN, PERMITS_DELETE, PERMITS_UPDATE, PERMITS_READ, MEMBER_OF, HAS_TAG
    4446    }
    4547
     
    4951        super();
    5052        this.graphDb = graphDb;
    51         nodeIndexes = new ArrayList<Index<Node>>(3);
     53        nodeIndexes = new ArrayList<Index<Node>>(5);
    5254        // List.set(enum.ordinal(), val) seems not to work.
    5355        nodeIndexes.add(NodeTypes.ANNOTATION.ordinal(), graphDb.index().forNodes("annotations"));
     
    5557        nodeIndexes.add(NodeTypes.TARGET.ordinal(), graphDb.index().forNodes("targets"));
    5658        nodeIndexes.add(NodeTypes.GROUP.ordinal(), graphDb.index().forNodes("groups"));
     59        nodeIndexes.add(NodeTypes.TAG.ordinal(), graphDb.index().forNodes("tags"));
    5760    }
    5861
     
    7174    }
    7275
    73    
    7476    /**
    7577     * Returns List of Groups the person is member of.
     
    8688            // make sure we're getting a group
    8789            if (!(group instanceof Group)) {
    88                 logger.error("target of MEMBER_OF is not GROUP! rel="+rel);
     90                logger.error("target of MEMBER_OF is not GROUP! rel=" + rel);
    8991                continue;
    9092            }
     
    9395        return groups;
    9496    }
    95    
     97
    9698    /**
    9799     * Returns if person with uri is in Group group.
     
    104106        Node pn = getPersonNodeByUri(person.getUriString());
    105107        if (pn == null) return false;
    106         // optimised version of getGroupsForPersonNode
     108        // optimized version of getGroupsForPersonNode
    107109        Iterable<Relationship> rels = pn.getRelationships(RelationTypes.MEMBER_OF);
    108110        for (Relationship rel : rels) {
     
    114116        return false;
    115117    }
    116    
     118
    117119    /**
    118120     * Returns the Annotation with the given id.
     
    138140        annot.setBodyText((String) annotNode.getProperty("bodyText", null));
    139141        annot.setBodyUri((String) annotNode.getProperty("bodyUri", null));
    140         // get annotation target from relation
     142        /*
     143         * get annotation target from relation
     144         */
    141145        Relationship targetRel = getRelation(annotNode, RelationTypes.ANNOTATES, null);
    142146        if (targetRel != null) {
     
    151155            annot.setFragmentType(FragmentTypes.valueOf(ft));
    152156        }
    153         // get creator from relation
     157        /*
     158         * get creator from relation
     159         */
    154160        Relationship creatorRel = getRelation(annotNode, RelationTypes.CREATED, null);
    155161        if (creatorRel != null) {
     
    160166            logger.error("annotation " + annotNode + " has no creator node!");
    161167        }
    162         // get creation date
     168        /*
     169         * get creation date
     170         */
    163171        annot.setCreated((String) annotNode.getProperty("created", null));
    164         // get permissions
     172        /*
     173         * get permissions
     174         */
    165175        Relationship adminRel = getRelation(annotNode, RelationTypes.PERMITS_ADMIN, null);
    166176        if (adminRel != null) {
     
    187197            annot.setReadPermission(read);
    188198        }
     199        /*
     200         * get tags
     201         */
     202        Set<String> tags = new HashSet<String>();
     203        for (Relationship rel : annotNode.getRelationships(RelationTypes.HAS_TAG)) {
     204            String tag = (String) rel.getEndNode().getProperty("name", null);
     205            if (tag != null) {
     206                tags.add(tag);
     207            }
     208        }
     209        annot.setTags(tags);
    189210
    190211        return annot;
     
    286307            setPermissionRelation(annotNode, RelationTypes.PERMITS_READ, annot.getReadPermission());
    287308
     309            /*
     310             * Tags on this annotation.
     311             */
     312            Set<String> newTags = annot.getTags();
     313            // we ignore existing tags if tags == null
     314            if (newTags != null) {
     315                List<Relationship> oldHasTags = new ArrayList<Relationship>();
     316                for (Relationship rel : annotNode.getRelationships(RelationTypes.HAS_TAG)) {
     317                    oldHasTags.add(rel);
     318                }
     319                // adjust to new tags
     320                if (newTags.isEmpty()) {
     321                    // remove old tags
     322                    if (!oldHasTags.isEmpty()) {
     323                        for (Relationship rel : oldHasTags) {
     324                            rel.delete();
     325                            // TODO: should we delete orphan nodes too?
     326                        }
     327                    }
     328                } else {
     329                    if (!oldHasTags.isEmpty()) {
     330                        // adjust old tags
     331                        for (Relationship rel : oldHasTags) {
     332                            String oldTag = (String) rel.getEndNode().getProperty("name", null);
     333                            if (newTags.contains(oldTag)) {
     334                                // tag exists
     335                                newTags.remove(oldTag);
     336                            } else {
     337                                // tag exists no longer
     338                                rel.delete();
     339                                // TODO: should we delete orphan nodes too?
     340                            }
     341                        }
     342                    }
     343                    if (!newTags.isEmpty()) {
     344                        // still tags to add
     345                        for (String tag : newTags) {
     346                            // create new tag
     347                            Node tagNode = getOrCreateTagNode(tag);
     348                            getOrCreateRelation(annotNode, RelationTypes.HAS_TAG, tagNode);
     349                        }
     350                    }
     351
     352                }
     353            }
    288354            tx.success();
    289355        } finally {
     
    371437            }
    372438        }
     439        // TODO: if both uri and user are given we should intersect
    373440        return annotations;
    374441    }
    375442
     443    /**
     444     * Returns Relationship of type from Node start to Node end. Creates one if
     445     * it doesn't exist.
     446     *
     447     * @param start
     448     * @param type
     449     * @param end
     450     * @return
     451     */
    376452    protected Relationship getOrCreateRelation(Node start, RelationshipType type, Node end) {
    377453        if (start.hasRelationship()) {
     
    458534                    person.setProperty("TYPE", NodeTypes.GROUP.name());
    459535                } else {
    460                     person.setProperty("TYPE", NodeTypes.PERSON.name());                   
     536                    person.setProperty("TYPE", NodeTypes.PERSON.name());
    461537                }
    462538                person.setProperty("uri", uri);
     
    474550        }
    475551        return person;
     552    }
     553
     554    protected Node getOrCreateTagNode(String tagname) {
     555        Index<Node> idx = getNodeIndex(NodeTypes.TAG);
     556        IndexHits<Node> tags = idx.get("name", tagname);
     557        Node tag = tags.getSingle();
     558        if (tag == null) {
     559            // does not exist yet
     560            Transaction tx = graphDb.beginTx();
     561            try {
     562                tag = graphDb.createNode();
     563                tag.setProperty("TYPE", NodeTypes.TAG.name());
     564                tag.setProperty("name", tagname);
     565                idx.add(tag, "name", tagname);
     566                tx.success();
     567            } finally {
     568                tx.finish();
     569            }
     570        }
     571        return tag;
    476572    }
    477573
     
    533629    }
    534630
    535     /** returns the (first) Relationship of RelationTypes type from Node start.
    536      * 
     631    /**
     632     * returns the (first) Relationship of RelationTypes type from Node start.
     633     *
    537634     * @param start
    538635     * @param type
     
    549646        }
    550647        for (Relationship rel : rels) {
     648            // just the first one
    551649            return rel;
    552650        }
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java

    r15 r16  
    5555        logger.debug("request authenticated=" + authUser);
    5656
    57         Annotation annot = getAnnotationStore().getAnnotationById(id);
     57        AnnotationStore store = getAnnotationStore();
     58        Annotation annot = store.getAnnotationById(id);
    5859        if (annot != null) {
    59             if (! annot.isActionAllowed("read", authUser, null)) {
     60            if (! annot.isActionAllowed("read", authUser, store)) {
    6061                setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
    6162                return null;
     
    160161                return null;
    161162            }
    162             if (! storedAnnot.isActionAllowed("update", authUser, null)) {
     163            if (! storedAnnot.isActionAllowed("update", authUser, store)) {
    163164                setStatus(Status.CLIENT_ERROR_FORBIDDEN);
    164165                return null;
     
    207208        Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
    208209        logger.debug("request authenticated=" + authUser);
    209         Annotation annot = getAnnotationStore().getAnnotationById(id);
     210        AnnotationStore store = getAnnotationStore();
     211        Annotation annot = store.getAnnotationById(id);
    210212        if (annot != null) {
    211             if (! annot.isActionAllowed("delete", authUser, null)) {
     213            if (! annot.isActionAllowed("delete", authUser, store)) {
    212214                setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
    213215                return null;
     
    216218       
    217219        // delete annotation
    218         getAnnotationStore().deleteById(id);
     220        store.deleteById(id);
    219221        setStatus(Status.SUCCESS_NO_CONTENT);
    220222        return null;
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java

    r15 r16  
    1010import java.util.ArrayList;
    1111import java.util.Calendar;
     12import java.util.HashSet;
    1213import java.util.List;
     14import java.util.Set;
    1315import java.util.regex.Matcher;
    1416import java.util.regex.Pattern;
     
    190192            jo.put("uri", annot.getTargetBaseUri());
    191193
     194            /*
     195             * user
     196             */
    192197            if (makeUserObject) {
    193198                // create user object
     
    214219            }
    215220
     221            /*
     222             * ranges
     223             */
    216224            if (annot.getTargetFragment() != null) {
    217225                // we only look at the first xpointer
     
    226234            }
    227235           
    228             // permissions
     236            /*
     237             * permissions
     238             */
    229239            JSONObject perms = new JSONObject();
    230240            jo.put("permissions", perms);
     
    267277            }
    268278           
     279            /*
     280             * tags
     281             */
     282            Set<String> tagset = annot.getTags();
     283            if (tagset != null) {
     284                JSONArray tags = new JSONArray();
     285                jo.put("tags", tags);
     286                for (String tag : tagset) {
     287                    tags.put(tag);
     288                }
     289            }
     290           
     291            /*
     292             * id
     293             */
    269294            // encode Annotation URL (=id) in base64
    270295            String annotUrl = annot.getUri();
     
    413438    public Annotation updateAnnotation(Annotation annot, JSONObject jo, Representation entity) throws JSONException,
    414439            UnsupportedEncodingException {
    415         // target uri
     440        /*
     441         * target uri
     442         */
    416443        if (jo.has("uri")) {
    417444            annot.setTargetBaseUri(jo.getString("uri"));
    418445        }
    419         // annotation text
     446        /*
     447         * annotation text
     448         */
    420449        if (jo.has("text")) {
    421450            annot.setBodyText(jo.getString("text"));
    422451        }
    423         // check authentication
     452        /*
     453         * check authentication
     454         */
    424455        String authUser = checkAuthToken(entity);
    425456        if (authUser == null) {
     
    434465             */
    435466        }
    436         // get or create creator object
     467        /*
     468         * get or create creator object
     469         */
    437470        Actor creator = annot.getCreator();
    438471        if (creator == null) {
     
    483516            creator.setUri(userUri);
    484517        }
    485 
     518        /*
     519         * creation date
     520         */
    486521        if (annot.getCreated() == null) {
    487522            // set creation date
     
    491526        }
    492527
    493         // create xpointer from the first range/area
     528        /*
     529         * create xpointer from the first range/area
     530         */
    494531        if (jo.has("ranges")) {
    495532            JSONObject ranges = jo.getJSONArray("ranges").getJSONObject(0);
     
    505542        }
    506543
    507         // permissions
     544        /*
     545         * permissions
     546         */
    508547        if (jo.has("permissions")) {
    509548            JSONObject permissions = jo.getJSONObject("permissions");
     
    530569        }
    531570
     571        /*
     572         * tags
     573         */
     574        if (jo.has("tags")) {
     575            HashSet<String> tagset = new HashSet<String>();
     576            JSONArray tags = jo.getJSONArray("tags");
     577            for (int i = 0; i < tags.length(); ++i) {
     578                tagset.add(tags.getString(i));
     579            }
     580            annot.setTags(tagset);
     581        }
     582
     583       
    532584        return annot;
    533585    }
Note: See TracChangeset for help on using the changeset viewer.