package com.temenos.interaction.core.state;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.Response.StatusType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.temenos.interaction.core.EntityResource;
import com.temenos.interaction.core.RESTResponse;
import com.temenos.interaction.core.command.CommandController;
import com.temenos.interaction.core.command.PutNotSupportedCommand;
import com.temenos.interaction.core.command.ResourcePostCommand;
import com.temenos.interaction.core.command.ResourcePutCommand;

/**
 * Defines a Resource Interaction Model for a resource with no state at all.
 * @author aphethean
 */
public class TRANSIENTResourceInteractionModel implements
		ResourceStateTransition {

	private final Logger logger = LoggerFactory.getLogger(TRANSIENTResourceInteractionModel.class);

	private String resourcePath;
	private CommandController commandController = new CommandController();
		
	public TRANSIENTResourceInteractionModel(String resourcePath) {
		this.resourcePath = resourcePath;
	}
	
	public String getResourcePath() {
		return resourcePath;
	}
	
	protected CommandController getCommandController() {
		return commandController;
	}

	/**
	 * POST a document to a resource.
	 * @precondition a valid POST command for this resourcePath + id must be registered with the command controller
	 * @postcondition a Response with non null Status must be returned
	 * @invariant resourcePath not null
	 */
    @POST
    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, com.temenos.interaction.core.media.hal.MediaType.APPLICATION_HAL_XML})
    public Response post( @Context HttpHeaders headers, @PathParam("id") String id, EntityResource resource ) {
    	logger.debug("POST " + resourcePath);
    	assert(resourcePath != null);
		ResourcePostCommand postCommand = (ResourcePostCommand) commandController.fetchStateTransitionCommand("POST", getResourcePath());
    	RESTResponse response = postCommand.post(id, resource);
    	assert (response != null);
    	StatusType status = response.getStatus();
    	assert (status != null);  // not a valid post command
		if (status.getFamily() == Response.Status.Family.SUCCESSFUL) {
			assert(response.getResource() != null);
			ResponseBuilder rb = Response.ok(response.getResource()).status(status);
			return HeaderHelper.allowHeader(rb, response).build();
		}
   		return Response.status(status).build();
    }
    
    @PUT
    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, com.temenos.interaction.core.media.hal.MediaType.APPLICATION_HAL_XML})
    public Response put( @Context HttpHeaders headers, @PathParam("id") String id, EntityResource resource ) {
    	logger.debug("PUT " + resourcePath);
		StatusType status = new PutNotSupportedCommand().put(id, resource);
   		return Response.status(status).build();
    }

	@Override
	public Response options(String id) {
    	logger.debug("OPTIONS " + resourcePath);
    	return Response.status(Status.NOT_FOUND).build();
	}

}

@@ -67,16 +67,67 @@
 nsumes;%0A
+import javax.ws.rs.DELETE;%0Aimport javax.ws.rs.GET;%0A
 import j
@@ -832,72 +832,8 @@
 and;
-%0Aimport com.temenos.interaction.core.command.ResourcePutCommand;
 %0A%0A/*
@@ -2727,152 +2727,283 @@
    @
-PU
+GE
 T%0A    
-@Consumes(%7BMediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, com.temenos.interaction.core.media.hal.MediaType.APPLICATION_HAL_XML%7D)
+public Response getNotImplemented( @Context HttpHeaders headers, @PathParam(%22id%22) String id, EntityResource resource ) %7B%0A    %09logger.debug(%22GET %22 + resourcePath);%0A   %09%09return Response.status(PutNotSupportedCommand.HTTP_STATUS_NOT_IMPLEMENTED).build();%0A    %7D%0A%0A    @PUT
 %0A   
@@ -3022,16 +3022,30 @@
 onse put
+NotImplemented
 ( @Conte
@@ -3170,112 +3170,367 @@
 h);%0A
-%09%09StatusType status = new PutNotSupportedCommand().put(id, resource);%0A   %09%09return Response.status(status
+   %09%09return Response.status(PutNotSupportedCommand.HTTP_STATUS_NOT_IMPLEMENTED).build();%0A    %7D%0A%0A    @DELETE%0A    public Response deleteNotImplemented( @Context HttpHeaders headers, @PathParam(%22id%22) String id, EntityResource resource ) %7B%0A    %09logger.debug(%22DELETE %22 + resourcePath);%0A   %09%09return Response.status(PutNotSupportedCommand.HTTP_STATUS_NOT_IMPLEMENTED
 ).bu

Handle GET, PUT, and DELETE as 501 not implemented on the TRANSIENT resources.