/*
 * 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 java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
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.ModelAndView;

import anyframe.sample.springmvc.annotation.service.Dept;
import anyframe.sample.springmvc.annotation.service.DeptService;

@Controller
@SessionAttributes("dept")
public class DeptController {

	@Resource(name = "deptService")
	DeptService deptService;

	@RequestMapping("/deptList.do")
	public ModelAndView getDeptList() {

		List<Dept> deptList = deptService.getDeptList();
		ModelAndView mnv = new ModelAndView("/jsp/dept/deptList.jsp");
		mnv.addObject("deptList", deptList);

		return mnv;
	}

	@RequestMapping("/getDept.do")
	public String getDept(@RequestParam("deptId")
	String deptId, Map<String, Dept> map) {

		Dept dept = deptService.getDept(deptId);
		map.put("dept", dept);

		return "/jsp/dept/getDept.jsp";
	}

	@RequestMapping("/addView.do")
	public ModelAndView addDeptView() {

		ModelAndView mnv = new ModelAndView("/jsp/dept/deptForm.jsp");
		mnv.addObject("dept", new Dept());

		return mnv;
	}

	@RequestMapping("/updateDept.do")
	public String updateDept(Dept dept, SessionStatus status) throws Exception {
		deptService.updateDept(dept);

		status.setComplete();

		return "/deptList.do";
	}

	@RequestMapping(value = { "/addDept.do", "/insertDept.do" })
	public String addDept(Dept dept, SessionStatus status) throws Exception {

		deptService.addDept(dept);

		status.setComplete();

		return "/deptList.do";
	}

	@RequestMapping("/deleteDept.do")
	public String deleteDept(@RequestParam("deptId")
	String deptId) throws Exception {

		deptService.deleteDept(deptId);

		return "/deptList.do";
	}
}

