/** * */ 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 * . * #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 = "\n

Group members

\n"; result += String.format("

Group: %s (%s)

\n", group.getName(), groupsUrl, group.getId()); result += "

Members:

\n"; result += String.format("
", thisUrl); result += ""; List members = store.getMembersOfGroup(group); for (Person p : members) { result += String.format("", p.getName(), p.getIdString()); result += String.format("\n", p.getIdString()); } result += "
%s(%s)
\n"; result += "
\n"; result += String.format("
\n", thisUrl); result += "

Add new member: \n"; result += "\n"; result += "

\n"; result += "\n"; 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; } }