Changeset 22:b1fb0d117877 in AnnotationManagerN4J


Ignore:
Timestamp:
Sep 20, 2012, 3:42:26 PM (12 years ago)
Author:
casties
Branch:
default
Message:

adding and listing groups via html works now.
no editing of group membership yet.
no authentication yet.

Location:
src/main
Files:
3 added
6 edited

Legend:

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

    r19 r22  
    140140    }
    141141
     142    /**
     143     * Returns the members of the group.
     144     *
     145     * @param group
     146     * @return
     147     */
     148    public List<Person> getMembersOfGroup(Group group) {
     149        ArrayList<Person> members = new ArrayList<Person>();
     150        Node gn = getActorNode(group);
     151        Iterable<Relationship> rels = gn.getRelationships(RelationTypes.MEMBER_OF);
     152        for (Relationship rel : rels) {
     153            Node memberNode = rel.getStartNode();
     154            Actor member = createActorFromNode(memberNode);
     155            // make sure we're getting a group
     156            if (!(member instanceof Person)) {
     157                logger.error("source of MEMBER_OF is not PERSON! rel=" + rel);
     158                continue;
     159            }
     160            members.add((Person) member);
     161        }
     162        return members;
     163    }
     164   
     165    /**
     166     * Returns the stored Actor matching the given one.
     167     *
     168     * @param actor
     169     * @return
     170     */
     171    public Actor getActor(Actor actor) {
     172        Node actorNode = getActorNode(actor);
     173        Actor storedActor = createActorFromNode(actorNode);
     174        return storedActor;
     175    }
     176   
     177    /**
     178     * Stores an Actor (Person or Group). Creates a new actor Node or returns an existing one.
     179     *
     180     * @param actor
     181     * @return
     182     */
     183    public Actor storeActor(Actor actor) {
     184        Node actorNode = getOrCreateActorNode(actor);
     185        Actor storedActor = createActorFromNode(actorNode);
     186        return storedActor;
     187    }
     188   
    142189    /**
    143190     * Returns the Annotation with the given id.
     
    536583    }
    537584
     585    protected Node getActorNode(Actor actor) {
     586        // Person/Group is identified by URI or id
     587        String uri = actor.getUriString();
     588        Index<Node> idx;
     589        if (actor.isGroup()) {
     590            idx = getNodeIndex(NodeTypes.GROUP);
     591        } else {
     592            idx = getNodeIndex(NodeTypes.PERSON);
     593        }
     594        IndexHits<Node> persons = idx.get("uri", uri);
     595        Node person = persons.getSingle();
     596        return person;
     597    }
     598   
    538599    protected Node getOrCreateActorNode(Actor actor) {
    539600        // Person/Group is identified by URI or id
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotationStoreGroups.java

    r21 r22  
    66import java.util.List;
    77
    8 import javax.servlet.ServletContext;
    9 
    108import org.apache.log4j.Logger;
    119import org.restlet.data.Form;
    1210import org.restlet.data.MediaType;
     11import org.restlet.data.Reference;
     12import org.restlet.data.Status;
    1313import org.restlet.representation.Representation;
    1414import org.restlet.representation.StringRepresentation;
    1515import org.restlet.resource.Get;
     16import org.restlet.resource.Post;
    1617import org.restlet.resource.ServerResource;
    1718
     19import de.mpiwg.itgroup.annotations.Actor;
    1820import de.mpiwg.itgroup.annotations.Group;
     21import de.mpiwg.itgroup.annotations.Person;
    1922import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
    2023
    2124/**
    2225 * @author casties
    23  *
     26 * 
    2427 */
    2528public class AnnotationStoreGroups extends ServerResource {
     
    2932    private AnnotationStore store;
    3033
     34    /**
     35     * GET with HTML content type. Lists all groups.
     36     *
     37     * @param entity
     38     * @return
     39     */
    3140    @Get("html")
    32     public Representation doGetHTML(Representation entity){
    33         Form form = getRequest().getResourceRef().getQueryAsForm();
     41    public Representation doGetHTML(Representation entity) {
    3442        // id from URI /annotations/groups/{id}
    3543        String id = (String) getRequest().getAttributes().get("id");
    3644        logger.debug("group-id=" + id);
    37         String result="<html><body>\n<h1>Groups</h1>\n<table>";
    38         result += "<tr><th>id</th><th>name</th><th>uri</th></tr>";
     45        String result = null;
    3946        store = getAnnotationStore();
    4047        if (id == null) {
    4148            // list all groups
     49            result = "<html><body>\n<h1>Groups</h1>\n<table>";
     50            result += "<tr><th>id</th><th>name</th><th>uri</th></tr>";
    4251            List<Group> groups = store.getGroups("uri", "*");
    4352            for (Group group : groups) {
    44                 String groupLink = group.getId();
    45                 result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupLink, group.getId(), group.getName(), group.getUri());
     53                Reference groupUrl = this.getReference();
     54                groupUrl.addSegment(group.getId());
     55                result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupUrl,
     56                        group.getId(), group.getName(), group.getUri());
    4657            }
     58            result += "</table>\n</body>\n</html>";
    4759        } else {
    4860            // just one group
    49             List<Group> groups = store.getGroups("uri", "*");
    50             for (Group group : groups) {
    51                 String groupLink = group.getId();
    52                 result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupLink, group.getId(), group.getName(), group.getUri());
     61            Reference groupsUrl = this.getReference().getParentRef();
     62            result = "<html><body>\n<h1>Group</h1>\n";
     63            result += String.format("<p><a href=\"%s\">All groups</a></p>", groupsUrl);
     64            Group group = new Group(id);
     65            group = (Group) store.getActor(group);
     66            result += "<table>";
     67            result += String.format("<tr><td><b>id</b></td><td>%s</td></tr>\n", group.getId());
     68            result += String.format("<tr><td><b>name</b></td><td>%s</td></tr>\n", group.getName());
     69            result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", group.getUri());
     70            result += "<tr><td><b>members</b></td><td>";
     71            List<Person> members = store.getMembersOfGroup(group);
     72            for (Person p : members) {
     73                result += String.format("%s (%s)\n", p.getName(), p.getIdString());               
    5374            }
     75            result += "</td></tr>\n";
     76            result += "</table>\n</body>\n</html>";
    5477        }
    55         result += "</table>\n</body>\n</html>";
    56        
     78
    5779        logger.debug("sending:");
    5880        logger.debug(result);
    59         return new StringRepresentation(result,MediaType.TEXT_HTML);
     81        return new StringRepresentation(result, MediaType.TEXT_HTML);
     82    }
     83
     84    /**
     85     * POST with HTML content-type. Creates a new Group.
     86     *
     87     * @return
     88     */
     89    @Post
     90    public Representation doPostHTML(Representation entity) {
     91        logger.debug("AnnotationStoreGroups doPostHTML!");
     92        // TODO: do authentication
     93        Form form = new Form(entity);
     94        String id = form.getFirstValue("id");
     95        String name = form.getFirstValue("name");
     96        if (id == null || id.isEmpty()) {
     97            // invalid id
     98            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
     99            return null;
     100        }
     101        String gid = makeGroupId(id);
     102        Group newGroup = new Group(gid, null, name);
     103        store = getAnnotationStore();
     104        Actor storedGroup = store.storeActor(newGroup);
     105        gid = storedGroup.getId();
     106        // return 303: see other
     107        setStatus(Status.REDIRECTION_SEE_OTHER);
     108        // go GET URL for this group
     109        Reference groupUrl = this.getReference();
     110        groupUrl.addSegment(gid);
     111        this.getResponse().setLocationRef(groupUrl);
     112        return null;
     113    }
     114
     115    /**
     116     * Returns a group id based on the given id.
     117     *
     118     * @param id
     119     * @return
     120     */
     121    protected String makeGroupId(String id) {
     122        // TODO: should we use different ids?
     123        id = id.replaceAll("\\W", "_");
     124        id = id.toLowerCase();
     125        return id;
    60126    }
    61127
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotationStoreRestlet.java

    r18 r22  
    3636
    3737        router.attach("/groups", AnnotationStoreGroups.class);
     38        router.attach("/groups/", AnnotationStoreGroups.class);
    3839        router.attach("/groups/{id}", AnnotationStoreGroups.class);
    3940
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java

    r20 r22  
    4949        logger.debug("annotation-id=" + id);
    5050
    51         // TODO: what to return without id - list of all annotations?
     51        if (id == null) {
     52            // TODO: what to return without id - list all annotations?
     53            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
     54            return null;
     55        }
    5256
    5357        // do authentication
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java

    r19 r22  
    6666
    6767    public String encodeJsonId(String id) {
     68        if (id == null) return null;
    6869        try {
    6970            return Base64.encodeBase64URLSafeString(id.getBytes("UTF-8"));
     
    7475
    7576    public String decodeJsonId(String id) {
     77        if (id == null) return null;
    7678        try {
    7779            return new String(Base64.decodeBase64(id), "UTF-8");
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/BaseRestlet.java

    r19 r22  
    135135                    logger.error("Unable to get resource " + dbFn);
    136136                }
     137            } else {
     138                // get existing AnnotationStore
     139                store = (AnnotationStore) sc.getAttribute(ANNSTORE_KEY);
    137140            }
    138141            /*
Note: See TracChangeset for help on using the changeset viewer.