/*
 * Copyright 2002-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package anyframe.sample.springmvc.annotation.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ModelAndView;

import anyframe.sample.springmvc.annotation.service.Dept;
import anyframe.sample.springmvc.annotation.service.DeptService;
import anyframe.sample.springmvc.annotation.service.User;
import anyframe.sample.springmvc.annotation.service.UserService;
import anyframe.sample.springmvc.annotation.web.validator.UserValidator;

@Controller
@RequestMapping("/user.do")
@SessionAttributes("user")
public class EditUserController {
    @Resource(name = "userService")
    UserService userService;

    @Resource
    DeptService deptService;

    @Resource
    MessageSource messageSource;

    @Resource
    LocaleResolver localeResolver;

    @ModelAttribute("deptList")
    public List<Dept> populateDeptList() throws Exception {
        return deptService.getDeptList();
    }

    @RequestMapping(params = "param=addView")
    public ModelAndView addUserView() {
        ModelAndView mnv = new ModelAndView("/jsp/user/userForm.jsp");
        mnv.addObject("user", new User());
        return mnv;
    }

    @RequestMapping(params = "param=add")
    public String addUser(HttpServletRequest request, @ModelAttribute("user")
    User user, BindingResult result, SessionStatus status) throws Exception {

        new UserValidator().validate(user, result);

        if (result.hasErrors()) {
            return "/jsp/user/userForm.jsp";
        } else {
            try {
                userService.addUser(user);
                status.setComplete();
                return "/userList.do";

            } catch (Exception e) {
                throw new Exception(messageSource.getMessage(
                    "user.error.exist", new String[] {user.getUserId() },
                    localeResolver.resolveLocale(request)));
            }
        }
    }

    @RequestMapping(params = "param=get")
    public String getUser(@RequestParam("userId")
    String userId, ModelMap model) {
        User user = userService.getUser(userId);
        model.addAttribute("user", user);

        return "/jsp/user/getUser.jsp";
    }

    @RequestMapping(params = "param=update")
    public String updateUser(@ModelAttribute("user")
    User user, SessionStatus status) {
        userService.updateUser(user);
        status.setComplete();

        return "/userList.do";
    }

    @RequestMapping(params = "param=delete")
    public String deleteUser(@RequestParam("userId")
    String userId) {
        userService.deleteUser(userId);

        return "/userList.do";
    }

}

