package com.aemreunal.controller.scenario;

/*
 ***************************
 * Copyright (c) 2014      *
 *                         *
 * This code belongs to:   *
 *                         *
 * @author Ahmet Emre Ünal *
 * S001974                 *
 *                         *
 * aemreunal@gmail.com     *
 * emre.unal@ozu.edu.tr    *
 *                         *
 * aemreunal.com           *
 ***************************
 */

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.mvc.ControllerLinkBuilder;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.aemreunal.config.GlobalSettings;
import com.aemreunal.controller.project.ProjectController;
import com.aemreunal.controller.user.UserController;
import com.aemreunal.domain.Scenario;
import com.aemreunal.service.ScenarioService;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

@Controller
@RequestMapping(GlobalSettings.SCENARIO_PATH_MAPPING)
public class ScenarioController {
    @Autowired
    private ScenarioService scenarioService;

    // TODO add search by attributes
    @RequestMapping(method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public ResponseEntity<List<Scenario>> getAllScenariosOfUser(@PathVariable String username,
                                                                @PathVariable Long projectId) {
        List<Scenario> scenarios = scenarioService.getScenariosOfProject(username, projectId);
        return new ResponseEntity<List<Scenario>>(scenarios, HttpStatus.OK);
    }

    @RequestMapping(method = RequestMethod.GET, value = GlobalSettings.SCENARIO_ID_MAPPING, produces = "application/json;charset=UTF-8")
    public ResponseEntity<Scenario> getScenario(@PathVariable String username,
                                                @PathVariable Long projectId,
                                                @PathVariable Long scenarioId) {
        Scenario scenario = scenarioService.getScenario(username, projectId, scenarioId);
        addLinks(username, projectId, scenarioId, scenario);
        return new ResponseEntity<Scenario>(scenario, HttpStatus.OK);
    }

    private void addLinks(String username, Long projectId, Long scenarioId, Scenario scenario) {
        scenario.add(ControllerLinkBuilder.linkTo(methodOn(ScenarioController.class).getScenario(username, projectId, scenarioId)).withSelfRel());
        scenario.add(ControllerLinkBuilder.linkTo(methodOn(UserController.class).getUserByUsername(username)).withRel("owner"));
        scenario.add(ControllerLinkBuilder.linkTo(methodOn(ProjectController.class).getProjectById(username, projectId)).withRel("project"));
    }

    @RequestMapping(method = RequestMethod.DELETE, value = GlobalSettings.SCENARIO_ID_MAPPING, produces = "application/json;charset=UTF-8")
    public ResponseEntity<Scenario> deleteScenario(@PathVariable String username,
                                                   @PathVariable Long projectId,
                                                   @PathVariable Long scenarioId,
                                                   @RequestParam(value = "confirm", required = true) String confirmation) {
        if (confirmation.toLowerCase().equals("yes")) {
            Scenario deletedScenario = scenarioService.delete(username, projectId, scenarioId);
            return new ResponseEntity<Scenario>(deletedScenario, HttpStatus.OK);
        } else {
            return new ResponseEntity<Scenario>(HttpStatus.PRECONDITION_FAILED);
        }
    }
}

@@ -566,24 +566,69 @@
 inkBuilder;%0A
+import org.springframework.http.HttpHeaders;%0A
 import org.s
@@ -659,16 +659,16 @@
 Status;%0A
-
 import o
@@ -804,28 +804,17 @@
 otation.
-PathVariable
+*
 ;%0Aimport
@@ -842,161 +842,33 @@
 web.
-bind.annotation.RequestMapping;%0Aimport org.springframework.web.bind.annotation.RequestMethod;%0Aimport org.springframework.web.bind.annotation.RequestParam
+util.UriComponentsBuilder
 ;%0Aim
@@ -2963,32 +2963,1585 @@
 ject%22));%0A    %7D%0A%0A
+    @RequestMapping(method = RequestMethod.POST, produces = %22application/json;charset=UTF-8%22)%0A    public ResponseEntity%3CScenario%3E createScenario(@PathVariable String username,%0A                                                     @PathVariable Long projectId,%0A                                                     @RequestBody Scenario scenarioFromJson,%0A                                                     UriComponentsBuilder builder) %7B%0A        Scenario savedScenario = scenarioService.save(username, projectId, scenarioFromJson);%0A        if (GlobalSettings.DEBUGGING) %7B%0A            System.out.println(%22Saved scenario with Name = %5C'%22 + savedScenario.getName() + %22%5C' ID = %5C'%22 + savedScenario.getScenarioId() + %22%5C'%22);%0A        %7D%0A        return buildCreateResponse(username, builder, savedScenario);%0A    %7D%0A%0A    private ResponseEntity%3CScenario%3E buildCreateResponse(String username, UriComponentsBuilder builder, Scenario savedScenario) %7B%0A        HttpHeaders headers = new HttpHeaders();%0A        headers.setLocation(builder.path(GlobalSettings.SCENARIO_SPECIFIC_MAPPING)%0A                                   .buildAndExpand(%0A                                       username,%0A                                       savedScenario.getProject().getProjectId(),%0A                                       savedScenario.getScenarioId()%0A                                   )%0A                                   .toUri());%0A        return new ResponseEntity%3CScenario%3E(savedScenario, headers, HttpStatus.CREATED);%0A    %7D%0A%0A%0A                                                        
     @RequestMapp

Add scenario creation method