package at.ac.tuwien.inso.controller;

import at.ac.tuwien.inso.exception.BusinessObjectNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @Autowired
    private MessageSource messageSource;

    @ExceptionHandler(DataAccessException.class)
    public ModelAndView handleDataAccessExceptions(HttpServletRequest request, DataAccessException ex) {
        logger.info("DataAccessException: " + ex.getMessage() + "\n url="+request.getRequestURL());
        ModelAndView mav = new ModelAndView();
        mav.addObject("message", messageSource.getMessage("error.dataaccess", null, LocaleContextHolder.getLocale()));
        mav.setViewName("error");
        return mav;
    }

    @ExceptionHandler(BusinessObjectNotFoundException.class)
    public ModelAndView handleBusinessObjectNotFoundExceptions(HttpServletRequest request, BusinessObjectNotFoundException ex) {
        logger.info("BusinessObjectNotFoundException: " + ex.getMessage() + "\n url="+request.getRequestURL());
        ModelAndView mav = new ModelAndView();
        mav.addObject("message", ex.getMessage());
        mav.setViewName("error");
        return mav;
    }
}

@@ -157,16 +157,72 @@
 actory;%0A
+import org.springframework.beans.TypeMismatchException;%0A
 import o
@@ -276,16 +276,16 @@
 owired;%0A
-
 import o
@@ -431,32 +431,76 @@
 ccessException;%0A
+import org.springframework.http.HttpStatus;%0A
 import org.sprin
@@ -609,24 +609,87 @@
 ionHandler;%0A
+import org.springframework.web.bind.annotation.ResponseStatus;%0A
 import org.s
@@ -1029,32 +1029,79 @@
 xception.class)%0A
+    @ResponseStatus(value=HttpStatus.CONFLICT)%0A
     public Model
@@ -1573,24 +1573,74 @@
 tion.class)%0A
+    @ResponseStatus(value = HttpStatus.NOT_FOUND)%0A
     public M
@@ -1907,32 +1907,32 @@
 ModelAndView();%0A
-
         mav.addO
@@ -2025,11 +2025,550 @@
 v;%0A    %7D
+%0A%0A    @ExceptionHandler(TypeMismatchException.class)%0A    @ResponseStatus(value = HttpStatus.BAD_REQUEST)%0A    public ModelAndView handleTypeMismatchExceptions(HttpServletRequest request, TypeMismatchException ex) %7B%0A        logger.info(%22TypeMismatchRequest: %22 + ex.getMessage() + %22%5Cn url=%22+request.getRequestURL());%0A        ModelAndView mav = new ModelAndView();%0A        mav.addObject(%22message%22, messageSource.getMessage(%22error.badrequest%22, null, LocaleContextHolder.getLocale()));%0A        mav.setViewName(%22error%22);%0A        return mav;%0A    %7D
 %0A%7D%0A

add response status codes and handle type mismatches (for parameters) globally