package at.ac.tuwien.inso.controller;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import at.ac.tuwien.inso.controller.forms.AccountActivationForm;
import at.ac.tuwien.inso.entity.UisUser;
import at.ac.tuwien.inso.service.AccountActivationService;

@Controller
@RequestMapping("/account_activation/{activationCode}")
public class AccountActivationController {

    @Autowired
    private AccountActivationService accountActivationService;

    @GetMapping
    public String accountActivationView(@PathVariable String activationCode,
                                        Model model,
                                        AccountActivationForm accountActivationForm) {
        model.addAttribute("user", accountActivationService.findOne(activationCode).getForUser());

        return "account-activation";
    }

    @PostMapping
    public String activateAccount(@PathVariable String activationCode,
                                  @Valid AccountActivationForm accountActivationForm,
                                  BindingResult bindingResult,
                                  Model model,
                                  RedirectAttributes attributes,
                                  HttpServletResponse response) {
        UisUser user = accountActivationService.findOne(activationCode).getForUser();

        if (bindingResult.hasErrors()) {
            model.addAttribute("user", user);

            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return "account-activation";
        }

        accountActivationService.activateAccount(activationCode, accountActivationForm.toUserAccount(user));
        attributes.addFlashAttribute("flashMessage", "account.activated");

        return "redirect:/login";
    }
}

@@ -111,16 +111,73 @@
 Valid;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -231,16 +231,16 @@
 owired;%0A
-
 import o
@@ -788,24 +788,148 @@
 ty.UisUser;%0A
+import at.ac.tuwien.inso.exception.BusinessObjectNotFoundException;%0Aimport at.ac.tuwien.inso.exception.UserFacingException;%0A
 import at.ac
@@ -1088,16 +1088,110 @@
 ller %7B%0A%0A
+    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);%0A%0A
     @Aut
@@ -1494,24 +1494,25 @@
 Form) %7B%0A
+%0A
         
 model.ad
@@ -1507,34 +1507,40 @@
     
-model.addAttribute(%22
+try %7B%0A            UisUser 
 user
-%22,
+ =
  acc
@@ -1597,18 +1597,261 @@
 orUser()
-)
 ;
+%0A            model.addAttribute(%22user%22, user);%0A%0A        %7D catch (BusinessObjectNotFoundException ex) %7B%0A            log.warn(%22Account activation failed%22, ex);%0A            throw new UserFacingException(%22error.activation_code.notfound%22);%0A        %7D
 %0A%0A      
@@ -2299,24 +2299,25 @@
 response) %7B%0A
+%0A
         UisU
@@ -2312,32 +2312,64 @@
         UisUser 
+user;%0A        try %7B%0A            
 user = accountAc
@@ -2421,16 +2421,213 @@
 rUser();
+%0A        %7D catch (BusinessObjectNotFoundException ex) %7B%0A            log.warn(%22Account activation failed%22, ex);%0A            throw new UserFacingException(%22error.activation_code.notfound%22);%0A        %7D
 %0A%0A      
@@ -2820,32 +2820,32 @@
 on%22;%0A        %7D%0A%0A
-
         accountA
@@ -2937,16 +2937,17 @@
 user));%0A
+%0A
         

Throw UserFacingException instead of BusinessObjectNotFoundException