package jp.ac.nii.prl.mape.plan.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import jp.ac.nii.prl.mape.plan.model.Deployment;
import jp.ac.nii.prl.mape.plan.model.Instance;
import jp.ac.nii.prl.mape.plan.model.InstanceType;
import jp.ac.nii.prl.mape.plan.service.AdaptationService;
import jp.ac.nii.prl.mape.plan.service.DeploymentService;
import jp.ac.nii.prl.mape.plan.service.InstanceService;
import jp.ac.nii.prl.mape.plan.service.InstanceTypeService;

@RestController
@Component
@RequestMapping("/deployment")
public class DeploymentController {

	@Autowired
	private InstanceService instanceService;

	@Autowired
	private AdaptationService adaptationService;
	
	@Autowired
	private DeploymentService deploymentService;
	
	@Autowired
	private InstanceTypeService instanceTypeService;
	
	@RequestMapping(method=RequestMethod.POST)
	public ResponseEntity<?> createDeployment(@RequestBody Deployment deployment) {
		deploymentService.save(deployment);
		for (InstanceType instType:deployment.getInstanceTypes()) {
			instanceTypeService.save(instType);
		}
		for (Instance instance:deployment.getInstances()) {
			instance.setDeployment(deployment);
			instanceService.setInstanceType(instance);
			instanceService.save(instance);
		}
		adaptationService.save(deployment.getAdaptation());

		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.setLocation(ServletUriComponentsBuilder
				.fromCurrentRequest().path("/{id}")
				.buildAndExpand(deployment.getId()).toUri());
		return new ResponseEntity<>(null, httpHeaders, HttpStatus.CREATED);
	}
	
}

@@ -1326,16 +1326,231 @@
 vice;%0A%09%0A
+%09/**%0A%09 * Receives a deployment, with adaptation. Calculate plan, and save the resulting %0A%09 * deployment. Adaptation is saved for reference.%0A%09 * %0A%09 * @param deployment%0A%09 * @return the location of the deployment%0A%09 */%0A
 %09@Reques
@@ -1585,16 +1585,16 @@
 d.POST)%0A
-
 %09public 
@@ -1666,16 +1666,95 @@
 ment) %7B%0A
+%09%09// create plan%0A%09%09deploymentService.plan(deployment);%0A%09%09%0A%09%09// save deployment%0A
 %09%09deploy
@@ -2120,16 +2120,39 @@
 ion());%0A
+%09%09%0A%09%09// create response
 %0A%09%09HttpH

Include plan creation in deployment's POST