package com.rhc.rest;

import java.net.URI;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;

import org.jbpm.services.api.model.ProcessInstanceDesc;
import org.joda.time.LocalDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.redhat.approval.entities.Employee;
import com.redhat.approval.entities.VacationRequest;
import com.redhat.bpm.approval.VacationRequestService;

@Component
@Path("/requests")
public class VacationRequestEndpoint {

	private static final Logger LOGGER = LoggerFactory.getLogger(VacationRequestEndpoint.class);

	@Autowired
	private VacationRequestService vacationRequestService;

	@Context
	private UriInfo uriInfo;

	@POST
	@Consumes({ "application/x-www-form-urlencoded" })
	public Response create(@FormParam("first-name") String firstName, @FormParam("last-name") String lastName,
			@FormParam("userid") String userId, @FormParam("start-date") String startDate,
			@FormParam("end-date") String endDate) {

		VacationRequest vacationRequest = new VacationRequest();

		String[] start = startDate.split("-");
		vacationRequest.setStartDate(new LocalDate(Integer.valueOf(start[0]), Integer.valueOf(start[1]), Integer
				.valueOf(start[2])));

		String[] end = endDate.split("-");
		vacationRequest.setEndDate(new LocalDate(Integer.valueOf(end[0]), Integer.valueOf(end[1]), Integer
				.valueOf(end[2])));

		Employee employee = new Employee(userId, firstName, lastName);
		vacationRequest.setEmployee(employee);

		Long processInstanceId = vacationRequestService.startProcess(vacationRequest);

		URI uri = UriBuilder.fromPath(uriInfo.getBaseUri() + "requests/" + processInstanceId.toString()).build();

		LOGGER.info(uri.toString());

		return Response.created(uri).build();
	}

	@GET
	@Path("/{id:[0-9][0-9]*}")
	@Produces({ "application/json" })
	public Response getProcess(@PathParam("id") final Long id) {
		ProcessInstanceDesc instance = vacationRequestService.getRuntimeDataService().getProcessInstanceById(id);
		if (instance == null) {
			return Response.status(Response.Status.NOT_FOUND).build();
		} else {
			return Response.ok(vacationRequestService.getRuntimeDataService().getProcessInstanceById(id)).build();
		}
	}

}

@@ -146,16 +146,40 @@
 s.POST;%0A
+import javax.ws.rs.PUT;%0A
 import j
@@ -2163,24 +2163,12 @@
 /%7Bid
-:%5B0-9%5D%5B0-9%5D*
 %7D%22)%0A
+
 %09@Pr
@@ -2581,10 +2581,533 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09@PUT%0A%09@Path(%22/%7Bid%7D/approve%22)%0A%09public Response approveRequest(@PathParam(%22id%22) final Long id) %7B%0A%09%09ProcessInstanceDesc instance = vacationRequestService.getRuntimeDataService().getProcessInstanceById(id);%0A%09%09if (instance == null) %7B%0A%09%09%09return Response.status(Response.Status.NOT_FOUND).build();%0A%09%09%7D else %7B%0A%09%09%09String managerId = (String) vacationRequestService.getProcessService().getProcessInstanceVariable(id,%0A%09%09%09%09%09%22managerId%22);%0A%09%09%09vacationRequestService.approveTheRequest(managerId);%0A%09%09%09return Response.ok().build();%0A%09%09%7D%0A%09%7D%0A
 %7D%0A

add approve rest endpoint