package ems.controller;

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.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
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;

    @RequestMapping( value = "/list-todos", method = RequestMethod.GET )
    public String showTodosList( ModelMap model ) {
        String user = (String) model.get( "name" );
        model.addAttribute( "todos", mTodoService.retrieveTodos( user ) );
        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( (String) model.get( "name" ), todo.getDesc(), new Date(), 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( (String) model.get( "name" ) );
        mTodoService.updateTodo( todo );
        model.clear();
        return "redirect:/list-todos";
    }
}

@@ -18,16 +18,51 @@
 oller;%0A%0A
+import java.text.SimpleDateFormat;%0A
 import j
@@ -76,16 +76,16 @@
 l.Date;%0A
-
 %0Aimport 
@@ -228,16 +228,83 @@
 owired;%0A
+import org.springframework.beans.propertyeditors.CustomDateEditor;%0A
 import o
@@ -438,16 +438,126 @@
 Result;%0A
+import org.springframework.web.bind.WebDataBinder;%0Aimport org.springframework.web.bind.annotation.InitBinder;%0A
 import o
@@ -926,16 +926,261 @@
 rvice;%0A%0A
+    @InitBinder%0A    protected void initBinder(WebDataBinder binder) %7B%0A        SimpleDateFormat dateFormat = new SimpleDateFormat(%22dd/MM/yyyy%22);%0A        binder.registerCustomEditor( Date.class, new CustomDateEditor( dateFormat, false ) );%0A    %7D%0A%0A
     @Req
@@ -1971,20 +1971,30 @@
 Desc(), 
-new 
+todo.getTarget
 Date(), 

Add init binder date to controller