/* ===================================================================
 * EditUserForm.java
 * 
 * Created Nov 18, 2004 9:31:45 PM
 * 
 * Copyright (c) 2004 Matt Magoffin (spamsqr@msqr.us)
 * 
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation; either version 2 of 
 * the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 
 * 02111-1307 USA
 * ===================================================================
 * $Id: EditUserForm.java 4 2009-05-01 03:27:53Z msqr $
 * ===================================================================
 */

package magoffin.matt.ieat.web;

import javax.servlet.http.HttpServletRequest;

import magoffin.matt.ieat.biz.BizContext;
import magoffin.matt.ieat.biz.UserBiz;
import magoffin.matt.ieat.domain.UiEdit;
import magoffin.matt.ieat.domain.UiSearchResults;
import magoffin.matt.ieat.domain.User;
import magoffin.matt.ieat.util.UserCommand;

import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.ServletRequestDataBinder;

/**
 * Form controller for editing an existing user.
 * 
 * @author Matt Magoffin (spamsqr@msqr.us)
 * @version $Revision: 4 $ $Date: 2009-05-01 15:27:53 +1200 (Fri, 01 May 2009) $
 */
public class EditUserForm extends AddUserForm {
	
	/**
	 * Get a UserCommand instance.
	 * 
	 * @param request the request
	 * @return the UserCommand
	 * @throws Exception if an error occurs
	 */
	protected UserCommand getEditUserCommand(HttpServletRequest request) throws Exception {
		// get the user ID to edit
		UserCommand uc = new UserCommand();
		ServletRequestDataBinder binder = createBinder(request, uc);
		binder.bind(request);
		return uc;
	}
	
	@Override
	protected Object formBackingObject(HttpServletRequest request)
		throws Exception 
	{
		BizContext context = getBizContext(request);
		UserCommand uc = getEditUserCommand(request);
		User user = userBiz.getUserById(uc.getUserId(), context);
		
		// create clone of user so we can edit it
		User copyUser = getDomainObjectFactory().getUserInstance();
		BeanUtils.copyProperties(user,copyUser);
		
		// set password to "special" value so we know if not to change it
		copyUser.setPassword(UserBiz.DO_NOT_CHANGE_VALUE);
		
		UiEdit edit = getDomainObjectFactory().getUiEditInstance();

		if ( uc.getIndex() != null ) {
			UiSearchResults sr = getDomainObjectFactory().getEmbeddedSearchResultsInstance();
			WebUtil.initSearchResults(sr,uc.getIndex(),null, getDomainObjectFactory());
			edit.setSearchResults(sr);
		}
		
		edit.setUser(copyUser);
		return edit;
	}
	
	@Override
	protected BizContext getBizContext(HttpServletRequest request) {
		// we don't require an admin, as users can edit themselves
		return WebUtil.getBizContext(request, true);
	}
}
