/*
 * Copyright (C) 2015 Dominik Schadow, dominikschadow@gmail.com
 *
 * This file is part of the Application Intrusion Detection project.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.dominikschadow.duke.encounters.controllers;

import de.dominikschadow.duke.encounters.domain.Encounter;
import de.dominikschadow.duke.encounters.services.EncounterService;
import de.dominikschadow.duke.encounters.services.UserService;
import de.dominikschadow.duke.encounters.validators.EncounterValidator;
import org.owasp.security.logging.SecurityMarkers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Controller to handle all encounter related requests.
 */
@Controller
public class EncountersController {
    private static final Logger LOGGER = LoggerFactory.getLogger(EncountersController.class);

    private EncounterService encounterService;
    private EncounterValidator encounterValidator;
    private UserService userService;

    @Autowired
    public EncountersController(EncounterService encounterService, EncounterValidator encounterValidator, UserService
            userService) {
        this.encounterService = encounterService;
        this.encounterValidator = encounterValidator;
        this.userService = userService;
    }

    @RequestMapping(value = "/encounters", method = GET)
    public String allEncounters(Model model) {
        List<Encounter> encounters = encounterService.getAllEncounters();
        model.addAttribute("encounters", encounters);

        return "encounters";
    }

    @RequestMapping(value = "/encounter/create", method = GET)
    public String createEncounter(@ModelAttribute Encounter encounter) {
        return "user/createEncounter";
    }

    @RequestMapping(value = "/encounter/delete", method = POST)
    public ModelAndView deleteEncounter(long encounterId) {
        String username = userService.getUsername();

        LOGGER.info(SecurityMarkers.SECURITY_AUDIT, "User {} is trying to delete encounter {}", username, encounterId);

        // TODO AID react to not own encounter

        encounterService.deleteEncounter(username, encounterId);

        return new ModelAndView("redirect:/account");
    }

    @RequestMapping(value = "/encounters/{id}", method = GET)
    public String encounterById(@PathVariable("id") long id, Model model) {
        // TODO react on validation error

        Encounter encounter = encounterService.getEncounterById(id);
        model.addAttribute("encounter", encounter);

        return "user/encounterDetails";
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.setValidator(encounterValidator);
    }
}

@@ -1267,16 +1267,69 @@
 .Model;%0A
+import org.springframework.validation.BindingResult;%0A
 import o
@@ -1363,32 +1363,32 @@
 .WebDataBinder;%0A
-
 import org.sprin
@@ -1671,16 +1671,47 @@
 dView;%0A%0A
+import javax.validation.Valid;%0A
 import j
@@ -2971,32 +2971,796 @@
 ounter%22;%0A    %7D%0A%0A
+    @RequestMapping(value = %22/encounter/create%22, method = POST)%0A    public ModelAndView saveEncounter(@Valid Encounter newEncounter, BindingResult result) %7B%0A        if (result.hasErrors()) %7B%0A            return new ModelAndView(%22encounter/create%22, %22formErrors%22, result.getAllErrors());%0A        %7D%0A%0A        String username = userService.getUsername();%0A%0A        LOGGER.info(SecurityMarkers.SECURITY_AUDIT, %22User %7B%7D is trying to create a new encounter %7B%7D%22, username,%0A                newEncounter);%0A%0A        Encounter encounter = encounterService.createEncounter(newEncounter, username);%0A%0A        LOGGER.info(SecurityMarkers.SECURITY_AUDIT, %22User %7B%7D successfully created encounter %7B%7D%22, username, encounter);%0A%0A        return new ModelAndView(%22redirect:/account%22);%0A    %7D%0A%0A
     @RequestMapp
@@ -4090,16 +4090,16 @@
 ounter%0A%0A
-
         
@@ -4148,32 +4148,154 @@
  encounterId);%0A%0A
+        LOGGER.info(SecurityMarkers.SECURITY_AUDIT, %22User %7B%7D successfully deleted encounter %7B%7D%22, username, encounterId);%0A%0A
         return n

Create encounter