/*
 * 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.web.controller.basic;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import anyframe.sample.springmvc.services.UserService;
import anyframe.sample.springmvc.services.UserVO;
import anyframe.sample.springmvc.web.validator.UserValidator;

/**
 * This is sample code for controller class what extends
 * AbstractCommandController.
 * 
 * @author Heewon Jung
 * 
 */
public class UserController extends SimpleFormController {

	UserService userService = null;

	// setter injection of userSerivce
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	// setting command class for data binding
	public UserController() {
		setCommandClass(UserVO.class);
		setCommandName("users");
		setFormView("/jsp/user/userForm.jsp");
		setValidator(new UserValidator());
	}

	// override onSubmit() method.
	protected ModelAndView onSubmit(Object command) throws Exception {

		// data binding using command object
		UserVO userVO = (UserVO) command;

		// call business service
		userVO = userService.getUser(userVO);
		// setting view name
		ModelAndView mav = new ModelAndView("/jsp/user/getUser.jsp");
		mav.addObject(userVO);
		// return a ModelAndView object.
		return mav;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		Map address = new HashMap();
		address.put("seoul", "서울");
		address.put("daegu", "대구");
		address.put("busan", "부산");

		Map hobby = new HashMap();
		hobby.put("reading", "독서");
		hobby.put("listeningMusic", "음악감상");
		hobby.put("study", "공부");
		
		request.setAttribute("address", address);
		request.setAttribute("hobby", hobby);
		return new UserVO();
	}

}

