Changeset 32:0731c4549065 in AnnotationManagerN4J for src


Ignore:
Timestamp:
Sep 25, 2012, 7:59:21 PM (12 years ago)
Author:
casties
Branch:
default
Message:

UI for editing groups and persons works now. (still no authorisation!)

Location:
src/main
Files:
2 added
2 deleted
8 edited

Legend:

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

    r15 r32  
    33 */
    44package de.mpiwg.itgroup.annotations;
     5
     6import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;
    57
    68/**
     
    5557        return null;
    5658    }
     59
     60    /**
     61     * Sets the name from the id using getFullNameFromLdap of the Application.
     62     * 
     63     * @param application
     64     * @return
     65     */
     66    public String updateName(BaseRestlet application) {
     67        if (id != null) {
     68            name = application.getFullNameFromLdap(id);
     69        }
     70        return name;
     71    }
    5772}
  • src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java

    r29 r32  
    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;
     
    101107
    102108    /**
    103      * Returns List of Groups.
    104      * Key has to be indexed.
     109     * Returns List of Groups. Key has to be indexed.
    105110     *
    106111     * @param key
     
    122127        return groups;
    123128    }
    124    
    125    
    126     /**
    127      * Returns List of Tags.
    128      * Key has to be indexed.
     129
     130    /**
     131     * Returns List of Tags. Key has to be indexed.
    129132     *
    130133     * @param key
     
    147150    }
    148151
    149  
    150    /**
     152    /**
    151153     * Returns List of Groups the person is member of.
    152154     *
     
    213215        return members;
    214216    }
    215    
     217
    216218    /**
    217219     * Add Person newMember to Group group.
     
    230232        return addedMember;
    231233    }
    232    
     234
    233235    /**
    234236     * Delete Person oldMember from Group group.
     
    239241    public void deleteGroupMember(Group group, Person member) {
    240242        Node gn = getActorNode(group);
     243        Node pn = getActorNode(member);
    241244        Iterable<Relationship> rels = gn.getRelationships(RelationTypes.MEMBER_OF);
    242245        for (Relationship rel : rels) {
    243246            Node mn = rel.getStartNode();
    244             if (mn.equals(member)) {
    245                 rel.delete();
     247            if (mn.equals(pn)) {
     248                Transaction tx = graphDb.beginTx();
     249                try {
     250                    rel.delete();
     251                    tx.success();
     252                } finally {
     253                    tx.finish();
     254                }
    246255                // there should be only one
    247256                break;
    248257            }
    249         }       
    250     }
    251    
     258        }
     259    }
     260
    252261    /**
    253262     * Returns the stored Actor matching the given one.
     
    257266     */
    258267    public Actor getActor(Actor actor) {
    259         Node actorNode = getActorNode(actor);
    260         Actor storedActor = createActorFromNode(actorNode);
    261         return storedActor;
    262     }
    263    
    264     /**
    265      * Stores an Actor (Person or Group). Creates a new actor Node or returns an existing one.
     268        Node actorNode = getActorNode(actor);
     269        Actor storedActor = createActorFromNode(actorNode);
     270        return storedActor;
     271    }
     272
     273    /**
     274     * Stores an Actor (Person or Group). Creates a new actor Node or update an
     275     * existing one.
    266276     *
    267277     * @param actor
     
    269279     */
    270280    public Actor storeActor(Actor actor) {
    271         Node actorNode = getOrCreateActorNode(actor);
    272         Actor storedActor = createActorFromNode(actorNode);
    273         return storedActor;
    274     }
    275    
     281        Node actorNode = getOrCreateActorNode(actor);
     282        Transaction tx = graphDb.beginTx();
     283        try {
     284            // id
     285            String id = actor.getId();
     286            if (id != null) {
     287                actorNode.setProperty("id", id);
     288            }
     289            // name
     290            String name = actor.getName();
     291            if (name != null) {
     292                actorNode.setProperty("name", name);
     293            }
     294            // uri
     295            String uri = actor.getUri();
     296            if (uri != null) {
     297                actorNode.setProperty("uri", uri);
     298            }
     299            tx.success();
     300        } finally {
     301            tx.finish();
     302        }
     303        Actor storedActor = createActorFromNode(actorNode);
     304        return storedActor;
     305    }
     306
     307    /**
     308     * Deletes the given Actor.
     309     *
     310     * @param actor
     311     */
     312    public void deleteActor(Actor actor) {
     313        String uri = actor.getUriString();
     314        Index<Node> idx;
     315        if (actor.isGroup()) {
     316            idx = getNodeIndex(NodeTypes.GROUP);
     317        } else {
     318            idx = getNodeIndex(NodeTypes.PERSON);
     319        }
     320        Node actorNode = idx.get("uri", uri).getSingle();
     321        if (actorNode != null) {
     322            // delete relations
     323            Transaction tx = graphDb.beginTx();
     324            try {
     325                for (Relationship rel : actorNode.getRelationships()) {
     326                    rel.delete();
     327                }
     328                if (!actorNode.hasRelationship()) {
     329                    // this shouldn't happen
     330                    deleteNode(actorNode);
     331                } else {
     332                    logger.error("deleteActor: unable to delete: Node still has relations.");
     333                }
     334                tx.success();
     335            } finally {
     336                tx.finish();
     337            }
     338        }
     339    }
     340
    276341    /**
    277342     * Returns the Annotation with the given id.
     
    376441     */
    377442    protected Actor createActorFromNode(Node actorNode) {
     443        if (actorNode == null) return null;
    378444        String id = (String) actorNode.getProperty("id", null);
    379445        String uri = (String) actorNode.getProperty("uri", null);
     
    387453        return null;
    388454    }
    389    
     455
    390456    public Tag createTagFromNode(Node tagNode) {
    391         String name = (String) tagNode.getProperty("name", null);
    392         String uri = (String) tagNode.getProperty("uri", null);
    393         String id = (String) tagNode.getProperty("id", null);
    394        
    395         return new Tag(id, uri, name);
    396                
    397         }
    398 
     457        if (tagNode == null) return null;
     458        String name = (String) tagNode.getProperty("name", null);
     459        String uri = (String) tagNode.getProperty("uri", null);
     460        String id = (String) tagNode.getProperty("id", null);
     461
     462        return new Tag(id, uri, name);
     463
     464    }
    399465
    400466    /**
     
    512578                        for (String tag : newTags) {
    513579                            // create new tag
    514                             Node tagNode = getOrCreateTagNode(new Tag(null,null,tag));
     580                            Node tagNode = getOrCreateTagNode(new Tag(null, null, tag));
    515581                            getOrCreateRelation(annotNode, RelationTypes.HAS_TAG, tagNode);
    516582                        }
     
    534600     * @param id
    535601     */
    536     public void deleteById(String id) {
     602    public void deleteAnnotationById(String id) {
    537603        Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle();
    538604        if (annotNode != null) {
     
    542608                for (Relationship rel : annotNode.getRelationships()) {
    543609                    // delete relation and the related node if it has no other
    544                     // relations
     610                    // relations and is not permanent
    545611                    Node other = rel.getOtherNode(annotNode);
    546612                    rel.delete();
    547                     if (!other.hasRelationship()) {
     613                    if (!(other.hasRelationship() || permanentNodeTypes.contains(other.getProperty("TYPE", null)))) {
    548614                        deleteNode(other);
    549615                    }
     
    570636     * @return
    571637     */
    572     public List<Annotation> searchByUriUser(String targetUri, String userUri, String limit, String offset) {
     638    public List<Annotation> searchAnnotationByUriUser(String targetUri, String userUri, String limit, String offset) {
    573639        List<Annotation> annotations = new ArrayList<Annotation>();
    574640        if (targetUri != null) {
     
    693759        return person;
    694760    }
    695    
     761
    696762    protected Node getOrCreateActorNode(Actor actor) {
    697763        // Person/Group is identified by URI or id
     
    746812                tag.setProperty("name", tagname);
    747813                idx.add(tag, "name", tagname);
    748                
     814
    749815                tag.setProperty("id", inTag.getId());
    750816                tag.setProperty("uri", inTag.getUri());
    751817                idx.add(tag, "uri", inTag.getUri());
    752                
     818
    753819                tx.success();
    754820            } finally {
     
    854920    }
    855921
    856         public List<Annotation> getAnnotationsByTag(String tagUri) {
    857                
    858                 ArrayList<Annotation> ret = new  ArrayList<Annotation>();
    859                 Node tag = getTagNodeByUri(tagUri);
    860                
    861                
    862                 Iterable<Relationship> rels = tag.getRelationships(Direction.INCOMING,RelationTypes.HAS_TAG);
    863                
    864                 for (Relationship rel:rels){
    865                         Node node = rel.getStartNode();
    866                         ret.add(createAnnotationFromNode(node));
    867                        
    868                 }
    869                 return ret;
    870         }
     922    public List<Annotation> getAnnotationsByTag(String tagUri) {
     923
     924        ArrayList<Annotation> ret = new ArrayList<Annotation>();
     925        Node tag = getTagNodeByUri(tagUri);
     926
     927        Iterable<Relationship> rels = tag.getRelationships(Direction.INCOMING, RelationTypes.HAS_TAG);
     928
     929        for (Relationship rel : rels) {
     930            Node node = rel.getStartNode();
     931            ret.add(createAnnotationFromNode(node));
     932
     933        }
     934        return ret;
     935    }
    871936
    872937}
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java

    r22 r32  
    222222       
    223223        // delete annotation
    224         store.deleteById(id);
     224        store.deleteAnnotationById(id);
    225225        setStatus(Status.SUCCESS_NO_CONTENT);
    226226        return null;
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java

    r15 r32  
    5757        logger.debug(String.format("searching for uri=%s user=%s", uri, user));
    5858        AnnotationStore store = getAnnotationStore();
    59         List<Annotation> annots = store.searchByUriUser(uri, user, limit, offset);
     59        List<Annotation> annots = store.searchAnnotationByUriUser(uri, user, limit, offset);
    6060        for (Annotation annot : annots) {
    6161            // check permission
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsUiRestlet.java

    r23 r32  
    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        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);
    4448
    4549        router.attach("/", InfoResource.class);
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupResource.java

    r24 r32  
    55
    66import org.apache.log4j.Logger;
     7import org.restlet.data.Form;
    78import org.restlet.data.MediaType;
    89import org.restlet.data.Reference;
     
    1011import org.restlet.representation.Representation;
    1112import org.restlet.representation.StringRepresentation;
     13import org.restlet.resource.Delete;
    1214import org.restlet.resource.Get;
     15import org.restlet.resource.Put;
    1316import org.restlet.resource.ResourceException;
    1417import org.restlet.resource.ServerResource;
     
    2932
    3033    protected AnnotationStore store;
    31    
     34
    3235    protected String requestId;
    33    
     36
    3437    protected Group group;
    35    
     38
    3639    @Override
    3740    protected void doInit() throws ResourceException {
     
    5861    @Get("html")
    5962    public Representation doGetHTML(Representation entity) {
    60         if (requestId == null || requestId.isEmpty()) {
     63        if (group == null) {
     64            // invalid id
     65            setStatus(Status.CLIENT_ERROR_NOT_FOUND);
     66            return null;
     67        }
     68        String result = null;
     69        // get form parameter
     70        Form f = this.getQuery();
     71        String form = f.getFirstValue("form");
     72        if (form != null && form.equals("edit")) {
     73            // output edit form
     74            result = "<html><body>\n";
     75            result += String.format("<h1>Edit group %s</h1>\n", group.getId());
     76            result += String.format("<p><a href=\"%s\">All groups</a></p>", this.getReference().getParentRef());
     77            // tunnel PUT method through POST
     78            result += String.format("<form method=\"post\" action=\"%s?method=PUT\">\n", this.getReference().getHierarchicalPart());
     79            result += "<table>";
     80            result += String.format("<tr><td><b>name</b></td><td><input type=\"text\" name=\"name\" value=\"%s\"/></td></tr>\n",
     81                    group.getName());
     82            result += String.format("<tr><td><b>uri</b></td><td><input type=\"text\" name=\"uri\" value=\"%s\"/></td></tr>\n",
     83                    group.getUriString());
     84            result += "</table>\n";
     85            result += "<p><input type=\"submit\"/></p>";
     86            result += "</table>\n</form>\n</body>\n</html>";
     87        } else {
     88            // output group content
     89            result = "<html><body>\n<h1>Group</h1>\n";
     90            result += String.format("<p><a href=\"%s\">All groups</a></p>", this.getReference().getParentRef());
     91            result += "<table>";
     92            result += String.format("<tr><td><b>id</b></td><td>%s</td></tr>\n", group.getId());
     93            result += String.format("<tr><td><b>name</b></td><td>%s</td></tr>\n", group.getName());
     94            result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", group.getUri());
     95            result += String.format("<tr><td><b>members</b></td><td><a href=\"%s\">view members</a></td></tr>\n", this
     96                    .getReference().addSegment("members"));
     97            result += "</table>\n";
     98            result += "<p><a href=\"?form=edit\">Edit group</a></p>\n";
     99            // tunnel POST as DELETE
     100            result += String.format(
     101                    "<form method=\"post\" action=\"%s?method=DELETE\"><input type=\"submit\" value=\"Delete group\"/></form>\n",
     102                    this.getReference().getHierarchicalPart());
     103            result += "</body>\n</html>";
     104        }
     105        return new StringRepresentation(result, MediaType.TEXT_HTML);
     106    }
     107
     108    /**
     109     * PUT updates the group.
     110     *
     111     * @param entity
     112     * @return
     113     */
     114    @Put
     115    public Representation doPut(Representation entity) {
     116        logger.debug("GroupResource.doPut!");
     117        if (group == null) {
    61118            // invalid id
    62119            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
    63120            return null;
    64121        }
    65         String result = null;
    66         Reference groupsUrl = this.getReference().getParentRef();
    67         result = "<html><body>\n<h1>Group</h1>\n";
    68         result += String.format("<p><a href=\"%s\">All groups</a></p>", groupsUrl);
    69         result += "<table>";
    70         result += String.format("<tr><td><b>id</b></td><td>%s</td></tr>\n", group.getId());
    71         result += String.format("<tr><td><b>name</b></td><td>%s</td></tr>\n", group.getName());
    72         result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", group.getUri());
    73         result += String.format("<tr><td><b>members</b></td><td><a href=\"%s\">view members</a></td></tr>\n", this.getReference()
    74                 .addSegment("members"));
    75         result += "</table>\n</body>\n</html>";
    76 
    77         logger.debug("sending:");
    78         logger.debug(result);
    79         return new StringRepresentation(result, MediaType.TEXT_HTML);
     122        // TODO: do authentication
     123        Form form = new Form(entity);
     124        String name = form.getFirstValue("name");
     125        String uri = form.getFirstValue("uri");
     126        if (name != null && !name.isEmpty()) {
     127            group.setName(name);
     128        }
     129        if (uri != null && !uri.isEmpty()) {
     130            group.setUri(uri);
     131        }
     132        store.storeActor(group);
     133        // return 303: see other
     134        setStatus(Status.REDIRECTION_SEE_OTHER);
     135        // go GET same URL
     136        Reference url = this.getReference();
     137        this.getResponse().setLocationRef(url);
     138        return null;
    80139    }
    81140
     141    /**
     142     * DELETE deletes the group.
     143     *
     144     * @param entity
     145     * @return
     146     */
     147    @Delete
     148    public Representation doDelete(Representation entity) {
     149        logger.debug("GroupResource.doDelete!");
     150        if (group == null) {
     151            // invalid id
     152            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
     153            return null;
     154        }
     155        // TODO: do authentication
     156        store.deleteActor(group);
     157        // return 303: see other
     158        setStatus(Status.REDIRECTION_SEE_OTHER);
     159        // go GET parent URL
     160        Reference url = this.getReference().getParentRef();
     161        this.getResponse().setLocationRef(url);
     162        return null;
     163    }
    82164}
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupsResource.java

    r23 r32  
    1515import org.restlet.resource.Get;
    1616import org.restlet.resource.Post;
     17import org.restlet.resource.ResourceException;
    1718import org.restlet.resource.ServerResource;
    1819
     
    3435    private AnnotationStore store;
    3536
     37    @Override
     38    protected void doInit() throws ResourceException {
     39        super.doInit();
     40        // get store instance
     41        if (store == null) {
     42            store = ((BaseRestlet) getApplication()).getAnnotationStore();
     43        }
     44    }
     45
    3646    /**
    3747     * GET with HTML content type. Lists all groups.
     
    4353    public Representation doGetHTML(Representation entity) {
    4454        String result = null;
    45         store = getAnnotationStore();
    46         // list all groups
    47         result = "<html><body>\n<h1>Groups</h1>\n<table>";
    48         result += "<tr><th>id</th><th>name</th><th>uri</th></tr>";
    49         List<Group> groups = store.getGroups("uri", "*");
    50         for (Group group : groups) {
    51             Reference groupUrl = this.getReference();
    52             groupUrl.addSegment(group.getId());
    53             result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupUrl, group.getId(),
    54                     group.getName(), group.getUri());
     55        // get form parameter
     56        Form f = this.getQuery();
     57        String form = f.getFirstValue("form");
     58        if (form != null && form.equals("new_group")) {
     59            // output new group form
     60            result = "<html><body>\n";
     61            result += "<h1>New group</h1>\n";
     62            result += String.format("<p><a href=\"%s\">All groups</a></p>", this.getReference());
     63            result += String.format("<form method=\"post\" action=\"%s\">\n", this.getReference().getHierarchicalPart());
     64            result += "<table>";
     65            result += "<tr><td><b>id</b></td><td><input type=\"text\" name=\"id\"/></td></tr>\n";
     66            result += "<tr><td><b>name</b></td><td><input type=\"text\" name=\"name\"/></td></tr>\n";
     67            result += "</table>\n";
     68            result += "<p><input type=\"submit\"/></p>\n";
     69            result += "</form>\n</body>\n</html>";
     70        } else {
     71            // list all groups
     72            result = "<html><body>\n<h1>Groups</h1>\n";
     73            result += "<table>\n";
     74            result += "<tr><th>id</th><th>name</th><th>uri</th></tr>";
     75            List<Group> groups = store.getGroups("uri", "*");
     76            for (Group group : groups) {
     77                Reference groupUrl = this.getReference().clone();
     78                groupUrl.addSegment(group.getId());
     79                result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupUrl, group.getId(),
     80                        group.getName(), group.getUri());
     81            }
     82            result += "</table>\n";
     83            result += "<p><a href=\"?form=new_group\">Add new group</a></p>\n";
     84            result += "</body>\n</html>";
    5585        }
    56         result += "</table>\n</body>\n</html>";
    57         logger.debug("sending:");
    58         logger.debug(result);
    5986        return new StringRepresentation(result, MediaType.TEXT_HTML);
    6087    }
     
    79106        String gid = makeGroupId(id);
    80107        Group newGroup = new Group(gid, null, name);
    81         store = getAnnotationStore();
    82108        Actor storedGroup = store.storeActor(newGroup);
    83109        gid = storedGroup.getId();
  • src/main/webapp/index.html

    r22 r32  
    1212<body>
    1313        <h1>Annotation Server</h1>
     14        <h2>View</h2>
     15  <ul>
     16    <li><a href="tags">View tags</a></li>
     17  </ul>
     18  <h2>Admin</h2>
    1419        <ul>
    15                 <li><a href="groups">View and edit groups</a></li>
     20                <li><a href="annotations/groups">View and edit groups</a></li>
     21    <li><a href="annotations/persons">View and edit persons</a></li>
    1622        </ul>
    1723</body>
Note: See TracChangeset for help on using the changeset viewer.