/**
*
*/
package de.mpiwg.itgroup.annotations.restlet;
import java.util.List;
import org.apache.log4j.Logger;
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 org.restlet.resource.ServerResource;
import de.mpiwg.itgroup.annotations.Actor;
import de.mpiwg.itgroup.annotations.Group;
import de.mpiwg.itgroup.annotations.Person;
import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
/**
* @author casties
*
*/
public class AnnotationStoreGroups extends ServerResource {
public static Logger logger = Logger.getLogger(AnnotationStoreGroups.class);
private AnnotationStore store;
/**
* GET with HTML content type. Lists all groups.
*
* @param entity
* @return
*/
@Get("html")
public Representation doGetHTML(Representation entity) {
// id from URI /annotations/groups/{id}
String id = (String) getRequest().getAttributes().get("id");
logger.debug("group-id=" + id);
String result = null;
store = getAnnotationStore();
if (id == null) {
// list all groups
result = "
\nGroups
\n";
result += "id | name | uri |
";
List groups = store.getGroups("uri", "*");
for (Group group : groups) {
Reference groupUrl = this.getReference();
groupUrl.addSegment(group.getId());
result += String.format("%s | %s | %s |
\n", groupUrl,
group.getId(), group.getName(), group.getUri());
}
result += "
\n\n";
} else {
// just one group
Reference groupsUrl = this.getReference().getParentRef();
result = "\nGroup
\n";
result += String.format("All groups
", groupsUrl);
Group group = new Group(id);
group = (Group) store.getActor(group);
result += "";
result += String.format("id | %s |
\n", group.getId());
result += String.format("name | %s |
\n", group.getName());
result += String.format("uri | %s |
\n", group.getUri());
result += "members | ";
List members = store.getMembersOfGroup(group);
for (Person p : members) {
result += String.format("%s (%s)\n", p.getName(), p.getIdString());
}
result += " |
\n";
result += "
\n\n";
}
logger.debug("sending:");
logger.debug(result);
return new StringRepresentation(result, MediaType.TEXT_HTML);
}
/**
* POST with HTML content-type. Creates a new Group.
*
* @return
*/
@Post
public Representation doPostHTML(Representation entity) {
logger.debug("AnnotationStoreGroups doPostHTML!");
// TODO: do authentication
Form form = new Form(entity);
String id = form.getFirstValue("id");
String name = form.getFirstValue("name");
if (id == null || id.isEmpty()) {
// invalid id
setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return null;
}
String gid = makeGroupId(id);
Group newGroup = new Group(gid, null, name);
store = getAnnotationStore();
Actor storedGroup = store.storeActor(newGroup);
gid = storedGroup.getId();
// return 303: see other
setStatus(Status.REDIRECTION_SEE_OTHER);
// go GET URL for this group
Reference groupUrl = this.getReference();
groupUrl.addSegment(gid);
this.getResponse().setLocationRef(groupUrl);
return null;
}
/**
* Returns a group id based on the given id.
*
* @param id
* @return
*/
protected String makeGroupId(String id) {
// TODO: should we use different ids?
id = id.replaceAll("\\W", "_");
id = id.toLowerCase();
return id;
}
protected AnnotationStore getAnnotationStore() {
if (store == null) {
store = ((BaseRestlet) getApplication()).getAnnotationStore();
}
return store;
}
}