Changeset 34:8427930c5f88 in AnnotationManagerN4J for src


Ignore:
Timestamp:
Sep 25, 2012, 8:20:17 PM (12 years ago)
Author:
casties
Branch:
default
Parents:
33:86bb29132ba6 (diff), 31:9f653697437e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge with 8ad8596a570af38aacfaecdb68b7e06145624181

Location:
src/main
Files:
7 edited

Legend:

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

    r31 r34  
    55
    66import java.util.ArrayList;
     7import java.util.Arrays;
    78import java.util.Calendar;
    89import java.util.HashSet;
     
    2829
    2930/**
     31 * Neo4J based Annotation store.
     32 *
    3033 * @author casties
    3134 *
     
    4043        ANNOTATION, PERSON, TARGET, GROUP, TAG
    4144    }
     45
     46    // types of nodes that should not be automatically deleted.
     47    public Set<String> permanentNodeTypes = new HashSet<String>(Arrays.asList("PERSON", "GROUP", "TAG"));
    4248
    4349    protected List<Index<Node>> nodeIndexes;
     
    8591    }
    8692
    87    
    88    
    8993    public List<Actor> getActors(String key, String query, NodeTypes type) {
    9094        ArrayList<Actor> actors = new ArrayList<Actor>();
     
    124128        return groups;
    125129    }
    126    
    127    
     130
    128131    /**
    129132     * Returns List of Annotations.
     
    174177    }
    175178
    176  
    177    /**
     179    /**
    178180     * Returns List of Groups the person is member of.
    179181     *
     
    240242        return members;
    241243    }
    242    
     244
    243245    /**
    244246     * Add Person newMember to Group group.
     
    257259        return addedMember;
    258260    }
    259    
     261
    260262    /**
    261263     * Delete Person oldMember from Group group.
     
    266268    public void deleteGroupMember(Group group, Person member) {
    267269        Node gn = getActorNode(group);
     270        Node pn = getActorNode(member);
    268271        Iterable<Relationship> rels = gn.getRelationships(RelationTypes.MEMBER_OF);
    269272        for (Relationship rel : rels) {
    270273            Node mn = rel.getStartNode();
    271             if (mn.equals(member)) {
    272                 rel.delete();
     274            if (mn.equals(pn)) {
     275                Transaction tx = graphDb.beginTx();
     276                try {
     277                    rel.delete();
     278                    tx.success();
     279                } finally {
     280                    tx.finish();
     281                }
    273282                // there should be only one
    274283                break;
    275284            }
    276         }       
    277     }
    278    
     285        }
     286    }
     287
    279288    /**
    280289     * Returns the stored Actor matching the given one.
     
    284293     */
    285294    public Actor getActor(Actor actor) {
    286         Node actorNode = getActorNode(actor);
    287         Actor storedActor = createActorFromNode(actorNode);
    288         return storedActor;
    289     }
    290    
    291     /**
    292      * Stores an Actor (Person or Group). Creates a new actor Node or returns an existing one.
     295        Node actorNode = getActorNode(actor);
     296        Actor storedActor = createActorFromNode(actorNode);
     297        return storedActor;
     298    }
     299
     300    /**
     301     * Stores an Actor (Person or Group). Creates a new actor Node or update an
     302     * existing one.
    293303     *
    294304     * @param actor
     
    296306     */
    297307    public Actor storeActor(Actor actor) {
    298         Node actorNode = getOrCreateActorNode(actor);
    299         Actor storedActor = createActorFromNode(actorNode);
    300         return storedActor;
    301     }
    302    
     308        Node actorNode = getOrCreateActorNode(actor);
     309        Transaction tx = graphDb.beginTx();
     310        try {
     311            // id
     312            String id = actor.getId();
     313            if (id != null) {
     314                actorNode.setProperty("id", id);
     315            }
     316            // name
     317            String name = actor.getName();
     318            if (name != null) {
     319                actorNode.setProperty("name", name);
     320            }
     321            // uri
     322            String uri = actor.getUri();
     323            if (uri != null) {
     324                actorNode.setProperty("uri", uri);
     325            }
     326            tx.success();
     327        } finally {
     328            tx.finish();
     329        }
     330        Actor storedActor = createActorFromNode(actorNode);
     331        return storedActor;
     332    }
     333
     334    /**
     335     * Deletes the given Actor.
     336     *
     337     * @param actor
     338     */
     339    public void deleteActor(Actor actor) {
     340        String uri = actor.getUriString();
     341        Index<Node> idx;
     342        if (actor.isGroup()) {
     343            idx = getNodeIndex(NodeTypes.GROUP);
     344        } else {
     345            idx = getNodeIndex(NodeTypes.PERSON);
     346        }
     347        Node actorNode = idx.get("uri", uri).getSingle();
     348        if (actorNode != null) {
     349            // delete relations
     350            Transaction tx = graphDb.beginTx();
     351            try {
     352                for (Relationship rel : actorNode.getRelationships()) {
     353                    rel.delete();
     354                }
     355                if (!actorNode.hasRelationship()) {
     356                    // this shouldn't happen
     357                    deleteNode(actorNode);
     358                } else {
     359                    logger.error("deleteActor: unable to delete: Node still has relations.");
     360                }
     361                tx.success();
     362            } finally {
     363                tx.finish();
     364            }
     365        }
     366    }
     367
    303368    /**
    304369     * Returns the Annotation with the given id.
     
    403468     */
    404469    protected Actor createActorFromNode(Node actorNode) {
     470        if (actorNode == null) return null;
    405471        String id = (String) actorNode.getProperty("id", null);
    406472        String uri = (String) actorNode.getProperty("uri", null);
     
    414480        return null;
    415481    }
    416    
     482
    417483    public Tag createTagFromNode(Node tagNode) {
    418         String name = (String) tagNode.getProperty("name", null);
    419         String uri = (String) tagNode.getProperty("uri", null);
    420         String id = (String) tagNode.getProperty("id", null);
    421        
    422         return new Tag(id, uri, name);
    423                
    424         }
    425 
     484        if (tagNode == null) return null;
     485        String name = (String) tagNode.getProperty("name", null);
     486        String uri = (String) tagNode.getProperty("uri", null);
     487        String id = (String) tagNode.getProperty("id", null);
     488
     489        return new Tag(id, uri, name);
     490
     491    }
    426492
    427493    /**
     
    539605                        for (String tag : newTags) {
    540606                            // create new tag
    541                             Node tagNode = getOrCreateTagNode(new Tag(null,null,tag));
     607                            Node tagNode = getOrCreateTagNode(new Tag(null, null, tag));
    542608                            getOrCreateRelation(annotNode, RelationTypes.HAS_TAG, tagNode);
    543609                        }
     
    561627     * @param id
    562628     */
    563     public void deleteById(String id) {
     629    public void deleteAnnotationById(String id) {
    564630        Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle();
    565631        if (annotNode != null) {
     
    569635                for (Relationship rel : annotNode.getRelationships()) {
    570636                    // delete relation and the related node if it has no other
    571                     // relations
     637                    // relations and is not permanent
    572638                    Node other = rel.getOtherNode(annotNode);
    573639                    rel.delete();
    574                     if (!other.hasRelationship()) {
     640                    if (!(other.hasRelationship() || permanentNodeTypes.contains(other.getProperty("TYPE", null)))) {
    575641                        deleteNode(other);
    576642                    }
     
    597663     * @return
    598664     */
    599     public List<Annotation> searchByUriUser(String targetUri, String userUri, String limit, String offset) {
     665    public List<Annotation> searchAnnotationByUriUser(String targetUri, String userUri, String limit, String offset) {
    600666        List<Annotation> annotations = new ArrayList<Annotation>();
    601667        if (targetUri != null) {
     
    720786        return person;
    721787    }
    722    
     788
    723789    protected Node getOrCreateActorNode(Actor actor) {
    724790        // Person/Group is identified by URI or id
     
    773839                tag.setProperty("name", tagname);
    774840                idx.add(tag, "name", tagname);
    775                
     841
    776842                tag.setProperty("id", inTag.getId());
    777843                tag.setProperty("uri", inTag.getUri());
    778844                idx.add(tag, "uri", inTag.getUri());
    779                
     845
    780846                tx.success();
    781847            } finally {
     
    804870            if (!oldActorNode.equals(newActorNode)) {
    805871                // new admin is different
    806                 rel.delete();
     872                Transaction tx = graphDb.beginTx();
     873                try {
     874                    rel.delete();
     875                    tx.success();
     876                } finally {
     877                    tx.finish();
     878                }
    807879                if (newActorNode != null) {
    808880                    rel = getOrCreateRelation(annotNode, type, newActorNode);
     
    881953    }
    882954
    883         public List<Annotation> getAnnotationsByTag(String tagUri) {
    884                
    885                 ArrayList<Annotation> ret = new  ArrayList<Annotation>();
    886                 Node tag = getTagNodeByUri(tagUri);
    887                
    888                
    889                 Iterable<Relationship> rels = tag.getRelationships(Direction.INCOMING,RelationTypes.HAS_TAG);
    890                
    891                 for (Relationship rel:rels){
    892                         Node node = rel.getStartNode();
    893                         ret.add(createAnnotationFromNode(node));
    894                        
    895                 }
    896                 return ret;
    897         }
     955    public List<Annotation> getAnnotationsByTag(String tagUri) {
     956
     957        ArrayList<Annotation> ret = new ArrayList<Annotation>();
     958        Node tag = getTagNodeByUri(tagUri);
     959
     960        Iterable<Relationship> rels = tag.getRelationships(Direction.INCOMING, RelationTypes.HAS_TAG);
     961
     962        for (Relationship rel : rels) {
     963            Node node = rel.getStartNode();
     964            ret.add(createAnnotationFromNode(node));
     965
     966        }
     967        return ret;
     968    }
    898969
    899970}
  • src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java

    r33 r34  
    107107
    108108    /**
    109      * Returns List of Groups. Key has to be indexed.
     109     * Returns List of Groups.
     110     * Key has to be indexed.
    110111     *
    111112     * @param key
     
    129130
    130131    /**
    131      * Returns List of Tags. Key has to be indexed.
     132     * Returns List of Annotations.
     133     * Key has to be indexed.
     134     *
     135     * @param key
     136     * @param query
     137     * @return
     138     */
     139    public List<Annotation> getAnnotations(String key, String query) {
     140        ArrayList<Annotation> annotations = new ArrayList<Annotation>();
     141        Index<Node> idx = getNodeIndex(NodeTypes.ANNOTATION);
     142        if (key == null) {
     143            key = "id";
     144            query = "*";
     145        }
     146        IndexHits<Node> annotNodes = idx.query(key, query);
     147        for (Node annotNode : annotNodes) {
     148            Annotation annotation = createAnnotationFromNode(annotNode);
     149            annotations.add(annotation);
     150        }
     151        return annotations;
     152    }
     153
     154 
     155   
     156    /**
     157     * Returns List of Tags.
     158     * Key has to be indexed.
    132159     *
    133160     * @param key
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java

    r31 r34  
    276276       
    277277        // delete annotation
    278         store.deleteById(id);
     278        store.deleteAnnotationById(id);
    279279        setStatus(Status.SUCCESS_NO_CONTENT);
    280280        return null;
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java

    r32 r34  
    66
    77import java.io.IOException;
    8 
     8import java.util.ArrayList;
     9import java.util.List;
     10
     11import org.json.JSONArray;
    912import org.json.JSONException;
    1013import org.json.JSONObject;
     14import org.restlet.data.Form;
     15import org.restlet.data.Parameter;
    1116import org.restlet.data.Status;
    1217import org.restlet.ext.json.JsonRepresentation;
     
    1924import de.mpiwg.itgroup.annotations.Annotation;
    2025import de.mpiwg.itgroup.annotations.Person;
     26import de.mpiwg.itgroup.annotations.Tag;
    2127import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
     28import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator;
    2229
    2330/**
     
    5057
    5158        if (id == null) {
    52             // TODO: what to return without id - list all annotations?
    53             setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
    54             return null;
     59           
     60            return getAllAnnotations();
    5561        }
    5662
     
    7783    }
    7884
    79     /**
     85    private Representation getAllAnnotations() {
     86       
     87         Form form = getRequest().getResourceRef().getQueryAsForm();
     88           String sortBy=null;
     89           for (Parameter parameter : form) {
     90             if (parameter.getName().equals("sortBy")){
     91             sortBy =  parameter.getValue();
     92             }
     93           }
     94       
     95        AnnotationStore store = getAnnotationStore();
     96        ArrayList<JSONObject> results = new ArrayList<JSONObject>();
     97       
     98        List<Annotation> annotations = store.getAnnotations(null, null);
     99        for (Annotation annotation : annotations) {
     100               
     101                 JSONObject jo = createAnnotatorJson(annotation,false);
     102             results.add(jo);
     103           
     104            }
     105       
     106        if (sortBy!=null){
     107                JSONObjectComparator.sortAnnotations(results,sortBy);
     108        }
     109       
     110        JSONArray resultsJa = new JSONArray();
     111        for (JSONObject result:results){
     112                resultsJa.put(result);
     113        }
     114       
     115        // assemble result object
     116        JSONObject result = new JSONObject();
     117        try {
     118            result.put("rows", resultsJa);
     119            result.put("total", resultsJa.length());
     120        } catch (JSONException e) {
     121            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
     122            return null;
     123        }
     124        logger.debug("sending:");
     125        logger.debug(result);
     126        return new JsonRepresentation(result);
     127    }
     128
     129       
     130       
     131       
     132
     133        /**
    80134     * POST with JSON content-type. Creates a new Annotation.
    81135     *
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsUiRestlet.java

    r31 r34  
    1717public class AnnotationsUiRestlet extends BaseRestlet {
    1818
    19     public final String version = "AnnotationManagerN4J/AnnotationStore 0.1";
     19    public final String version = "AnnotationManagerN4J/AnnotationsUI 0.2";
    2020
    2121    public static Logger logger = Logger.getLogger(AnnotationsUiRestlet.class);
     
    4242        router.attach("/groups/{id}/", GroupResource.class);
    4343        router.attach("/groups/{id}/members", GroupMembersResource.class);
    44              
     44        router.attach("/persons", PersonsResource.class);
     45        router.attach("/persons/", PersonsResource.class);
     46        router.attach("/persons/{id}", PersonResource.class);
     47        router.attach("/persons/{id}/", PersonResource.class);
     48
    4549        router.attach("/", InfoResource.class);
    4650        // authenticator.setNext(router);
  • src/main/webapp/index.html

    r31 r34  
    1111
    1212<body>
    13         <h1>Annotation Server</h1>
     13  <h1>Annotation Server</h1>
     14  <h2>View</h2>
     15  <ul>
     16    <li><a href="tags">View tags</a></li>
     17    <li><a href="annotationBrowser">All Annotations</a></li>
     18  </ul>
     19  <h2>Admin</h2>
    1420        <ul>
    15                 <li><a href="groups">View and edit groups</a></li>
    16                 <li><a href="tags">Browse by Tags</a></li>
    17                 <li><a href="annotationBrowser">All Annotations</a></li>
    18                
     21                <li><a href="annotations/groups">View and edit groups</a></li>
     22    <li><a href="annotations/persons">View and edit persons</a></li>
    1923        </ul>
    2024</body>
  • src/main/webapp/index.html

    r32 r34  
    1111
    1212<body>
    13         <h1>Annotation Server</h1>
    14         <h2>View</h2>
     13  <h1>Annotation Server</h1>
     14  <h2>View</h2>
    1515  <ul>
    1616    <li><a href="tags">View tags</a></li>
     17    <li><a href="annotationBrowser">All Annotations</a></li>
    1718  </ul>
    1819  <h2>Admin</h2>
Note: See TracChangeset for help on using the changeset viewer.