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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.