package com.dwalldorf.timetrack.rest.controller;

import com.dwalldorf.timetrack.document.User;
import com.dwalldorf.timetrack.event.PermissionFailureEvent;
import com.dwalldorf.timetrack.exception.AdminRequiredException;
import com.dwalldorf.timetrack.exception.LoginRequiredException;
import com.dwalldorf.timetrack.service.UserService;
import javax.inject.Inject;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class ErrorController {

    private final static String EMPTY_RESULT = "";

    private final ApplicationEventPublisher eventPublisher;

    private final UserService userService;

    @Inject
    public ErrorController(ApplicationEventPublisher eventPublisher, UserService userService) {
        this.eventPublisher = eventPublisher;
        this.userService = userService;
    }

    @ExceptionHandler(LoginRequiredException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public String handleLoginRequireException(LoginRequiredException e) {
        eventPublisher.publishEvent(PermissionFailureEvent.failureEvent(e.getMessage()));
        return EMPTY_RESULT;
    }

    @ExceptionHandler(AdminRequiredException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public String handleAdminRequiredException(AdminRequiredException e) {
        final String errorMessage = e.getMessage();
        final User user = userService.getCurrentUser();

        if (user != null) {
            eventPublisher.publishEvent(PermissionFailureEvent.failureEvent(user, errorMessage));
        } else {
            eventPublisher.publishEvent(PermissionFailureEvent.failureEvent(errorMessage));
        }
        return EMPTY_RESULT;
    }
}
@@ -43,16 +43,78 @@
 oller;%0A%0A
+import static org.springframework.http.HttpStatus.NOT_FOUND;%0A%0A
 import c
@@ -147,24 +147,24 @@
 ument.User;%0A
-
 import com.d
@@ -516,26 +516,30 @@
 rk.http.
-HttpStatus
+ResponseEntity
 ;%0Aimport
@@ -824,16 +824,18 @@
 SULT = %22
+no
 %22;%0A%0A    
@@ -1204,35 +1204,24 @@
 ponseStatus(
-HttpStatus.
 NOT_FOUND)%0A 
@@ -1226,30 +1226,38 @@
 %0A    public 
-String
+ResponseEntity
  handleLogin
@@ -1402,36 +1402,53 @@
      return 
-EMPTY_RESULT
+new ResponseEntity(NOT_FOUND)
 ;%0A    %7D%0A%0A   
@@ -1488,32 +1488,32 @@
 xception.class)%0A
+
     @ResponseSta
@@ -1516,27 +1516,16 @@
 eStatus(
-HttpStatus.
 NOT_FOUN

fix ErrorController