package uk.ac.ox.oucs.vle;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.StreamingOutput;
import javax.ws.rs.ext.ContextResolver;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.map.type.TypeFactory;

@Path("/signup")
public class SignupResource {

	
	private CourseSignupService courseService;
	private JsonFactory jsonFactory;
	private ObjectMapper objectMapper;

	public SignupResource(@Context ContextResolver<Object> resolver) {
		this.courseService = (CourseSignupService) resolver.getContext(CourseSignupService.class);
		jsonFactory = new JsonFactory();
		objectMapper = new ObjectMapper();
		objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
		objectMapper.configure(SerializationConfig.Feature.USE_STATIC_TYPING, true);
		objectMapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
	}
	
	@Path("/all")
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public StreamingOutput getMySignups() {
		final List<CourseSignup> signups = courseService.getMySignups(null);
		return new StreamingOutput() {
			public void write(OutputStream output) throws IOException,
					WebApplicationException {
				objectMapper.typedWriter(TypeFactory.collectionType(List.class, CourseSignup.class)).writeValue(output, signups);
			}
		};
	}
}

@@ -105,30 +105,107 @@
 st;%0A
-%0Aimport javax.ws.rs.GE
+import java.util.Set;%0A%0Aimport javax.ws.rs.FormParam;%0Aimport javax.ws.rs.GET;%0Aimport javax.ws.rs.POS
 T;%0Ai
@@ -369,16 +369,50 @@
 iaType;%0A
+import javax.ws.rs.core.Response;%0A
 import j
@@ -1819,10 +1819,265 @@
 %09%09%7D;%0A%09%7D%0A
+%09%0A%09@Path(%22/new%22)%0A%09@POST%0A%09public Response signup(@FormParam(%22components%22)Set%3CString%3E components, @FormParam(%22email%22)String email, @FormParam(%22message%22)String message) %7B%0A%09%09courseService.signup(components, email, message);%0A%09%09return Response.ok().build();%0A%09%7D%0A
 %7D%0A

Support creating a new signup.