package ems.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.validation.Valid;

import ems.model.Todo;
import ems.service.TodoService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
@SessionAttributes( "name" )
public class TodoController {
    @Autowired
    private TodoService mTodoService;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        binder.registerCustomEditor( Date.class, new CustomDateEditor( dateFormat, false ) );
    }

    @RequestMapping( value = {"/", "/list-todos"}, method = RequestMethod.GET )
    public String showTodosList( ModelMap model ) {
        model.addAttribute( "todos", mTodoService.retrieveTodos( getPrincipal() ) );
        return "list-todos";
    }

    @RequestMapping( value = "/add-todo", method = RequestMethod.GET )
    public String showAddTodoPage( ModelMap model ) {
        model.addAttribute( "todo", new Todo() );
        return "todo";
    }

    @RequestMapping( value = "/add-todo", method = RequestMethod.POST )
    public String addTodo( ModelMap model, @Valid Todo todo, BindingResult result ) {
        if ( result.hasErrors() ) {
            return "todo";
        }
        mTodoService.addTodo( getPrincipal(), todo.getDesc(), todo.getTargetDate(), false );
        model.clear();
        return "redirect:/list-todos";
    }

    @RequestMapping( value = "/delete-todo", method = RequestMethod.GET )
    public String deleteTodo( @RequestParam int id ) {
        mTodoService.deleteTodo( id );
        return "redirect:/list-todos";
    }

    @RequestMapping( value = "/update-todo", method = RequestMethod.GET )
    public String showUpdateTodoPage( ModelMap model, @RequestParam int id ) {
        model.addAttribute( "todo", mTodoService.retrieveTodo( id ) );
        return "todo";
    }

    @RequestMapping( value = "/update-todo", method = RequestMethod.POST )
    public String updateTodo( ModelMap model, @Valid Todo todo, BindingResult result ) {
        if ( result.hasErrors() ) {
            return "todo";
        }
        todo.setUser( getPrincipal() );
        mTodoService.updateTodo( todo );
        model.clear();
        return "redirect:/list-todos";
    }

    private String getPrincipal() {
        String userName = null;
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal instanceof UserDetails) {
            userName = ((UserDetails)principal).getUsername();
        } else {
            userName = principal.toString();
        }
        return userName;
    }
}

@@ -876,115 +876,20 @@
 am;%0A
-import org.springframework.web.bind.annotation.SessionAttributes;%0A%0A@Controller%0A@SessionAttributes( %22name%22 )
+%0A@Controller
 %0Apub
@@ -1246,14 +1246,8 @@
 ue =
- %7B%22/%22,
  %22/l
@@ -1256,17 +1256,16 @@
 t-todos%22
-%7D
 , method
@@ -1331,32 +1331,77 @@
 elMap model ) %7B%0A
+        String user = getLoggedInUsername();%0A
         model.ad
@@ -1448,32 +1448,20 @@
 veTodos(
- getPrincipal() 
+user
 ) );%0A   
@@ -1962,25 +1962,32 @@
 odo( get
-Principal
+LoggedInUsername
 (), todo
@@ -2828,25 +2828,32 @@
 ser( get
-Principal
+LoggedInUsername
 () );%0A  
@@ -2986,53 +2986,28 @@
  get
-Principal() %7B%0A        String userName = null;
+LoggedInUsername() %7B
 %0A   
@@ -3160,26 +3160,22 @@
         
-userName =
+return
  ((UserD
@@ -3220,15 +3220,8 @@
    %7D
- else %7B
 %0A   
@@ -3229,22 +3229,14 @@
     
-    userName =
+return
  pri
@@ -3258,43 +3258,8 @@
 ();%0A
-        %7D%0A        return userName;%0A
     

Change method getPrincipal to getLoggedUsername