view src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupMembersResource.java @ 70:2b1e6df5e21a

added lgpl_v3 license information.
author casties
date Thu, 06 Mar 2014 15:09:04 +0100
parents 64aa756c60cc
children 25eb2e1df106
line wrap: on
line source

/**
 * 
 */
package de.mpiwg.itgroup.annotations.restlet.annotations_ui;

/*
 * #%L
 * AnnotationManager
 * %%
 * Copyright (C) 2012 - 2014 MPIWG Berlin
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
 * #L%
 */

import java.util.List;

import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Reference;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.Post;

import de.mpiwg.itgroup.annotations.Person;

/**
 * Resource class for the members of an annotation group.
 * 
 * @author casties
 * 
 */
public class GroupMembersResource extends GroupResource {

    /**
     * GET with HTML content type. Shows the members of the group.
     * 
     * @param entity
     * @return
     */
    @Get("html")
    public Representation doGetHTML(Representation entity) {
        // id from URI /annotations/groups/{id}/members
        if (requestId == null || requestId.isEmpty()) {
            // invalid id
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            return null;
        }
        String result = null;
        Reference thisUrl = this.getReference();
        Reference groupsUrl = thisUrl.getParentRef();
        result = "<html><body>\n<h1>Group members</h1>\n";
        result += String.format("<p>Group: %s <a href=\"%s\">(%s)</a></p>\n", group.getName(), groupsUrl, group.getId());
        result += "<p>Members:</p>\n";
        result += String.format("<form method=\"post\" action=\"%s\">", thisUrl);
        result += "<table>";
        List<Person> members = store.getMembersOfGroup(group);
        for (Person p : members) {
            result += String.format("<tr><td>%s</td><td>(%s)</td>", p.getName(), p.getIdString());
            result += String.format("<td><input type=\"submit\" name=\"del_member\" value=\"delete:%s\"></td></tr>\n", p.getIdString());
        }
        result += "</table>\n";
        result += "</form>\n";
        result += String.format("<form method=\"post\" action=\"%s\">\n", thisUrl);
        result += "<p>Add new member: <select name=\"add_member\">\n";
        for (Person p : store.getPersons("uri", "*")) {
            result += String.format("<option value=\"%s\">%s</option>\n", p.getIdString(), p.getName());
        }
        result += "</select>\n";
        result += "<input type=\"submit\"/>\n";
        result += "</form>\n";
        result += "</body>\n</html>";

        return new StringRepresentation(result, MediaType.TEXT_HTML);
    }

    /**
     * POST adds or deletes members of the Group.
     * 
     * @return
     */
    @Post
    public Representation doPost(Representation entity) {
        logger.debug("GroupMembersResource doPost!");
        // TODO: do authentication
        Form form = new Form(entity);
        String addMemberId = form.getFirstValue("add_member");
        String delMemberId = form.getFirstValue("del_member");
        if (group == null || ((addMemberId == null || addMemberId.isEmpty()) 
                && (delMemberId == null || delMemberId.isEmpty()))) {
            // no id
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            return null;
        }
        if (addMemberId != null) {
            logger.debug("adding member: "+addMemberId);
            Person member = new Person(addMemberId);
            store.addGroupMember(group, member);
        } else if (delMemberId != null) {
            if (delMemberId.startsWith("delete:")) {
                delMemberId = delMemberId.substring(7);
            }
            logger.debug("deleting member: "+delMemberId);
            Person member = new Person(delMemberId);
            store.deleteGroupMember(group, member);
        }
        // return 303: see other
        setStatus(Status.REDIRECTION_SEE_OTHER);
        // go get same URL
        Reference thisUrl = this.getReference();
        this.getResponse().setLocationRef(thisUrl);
        return null;
    }

}