FileDocCategorySizeDatePackage
AccountFormController.javaAPI DocExample4154Thu Dec 04 08:19:26 GMT 2003org.springframework.samples.jpetstore.web.spring

AccountFormController

public class AccountFormController extends org.springframework.web.servlet.mvc.SimpleFormController
author
Juergen Hoeller
since
01.12.2003

Fields Summary
public static final String[]
LANGUAGES
private org.springframework.samples.jpetstore.domain.logic.PetStoreFacade
petStore
Constructors Summary
public AccountFormController()


	  
		setSessionForm(true);
		setValidateOnBinding(false);
		setCommandName("accountForm");
		setFormView("EditAccountForm");
	
Methods Summary
protected java.lang.ObjectformBackingObject(javax.servlet.http.HttpServletRequest request)

		UserSession userSession = (UserSession) WebUtils.getSessionAttribute(request, "userSession");
		if (userSession != null) {
			return new AccountForm(this.petStore.getAccount(userSession.getAccount().getUsername()));
		}
		else {
			return new AccountForm();
		}
	
protected voidonBindAndValidate(javax.servlet.http.HttpServletRequest request, java.lang.Object command, org.springframework.validation.BindException errors)

		AccountForm accountForm = (AccountForm) command;
		Account account = accountForm.getAccount();

		if (request.getParameter("account.listOption") == null) {
			account.setListOption(false);
		}
		if (request.getParameter("account.bannerOption") == null) {
			account.setBannerOption(false);
		}

		errors.setNestedPath("account");
		getValidator().validate(account, errors);
		errors.setNestedPath("");

		if (accountForm.isNewAccount()) {
			account.setStatus("OK");
			rejectIfEmpty(errors, "account.username", "USER_ID_REQUIRED", "User ID is required.");
			if (account.getPassword() == null || account.getPassword().length() < 1 ||
					!account.getPassword().equals(accountForm.getRepeatedPassword())) {
			 errors.reject("PASSWORD_MISMATCH", "Passwords did not match or were not provided.  Matching passwords are required.");
			}
		}
		else if (account.getPassword() != null && account.getPassword().length() > 0) {
		  if (!account.getPassword().equals(accountForm.getRepeatedPassword())) {
				errors.reject("PASSWORD_MISMATCH", "Passwords did not match.  Matching passwords are required.");
		  }
	  }
 	
protected org.springframework.web.servlet.ModelAndViewonSubmit(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.Object command, org.springframework.validation.BindException errors)

		AccountForm accountForm = (AccountForm) command;
		if (accountForm.isNewAccount()) {
			this.petStore.insertAccount(accountForm.getAccount());
		}
		else {
			this.petStore.updateAccount(accountForm.getAccount());
		}
		UserSession userSession = new UserSession(this.petStore.getAccount(accountForm.getAccount().getUsername()));
		PagedListHolder myList = new PagedListHolder(this.petStore.getProductListByCategory(accountForm.getAccount().getFavouriteCategoryId()));
		myList.setPageSize(4);
		userSession.setMyList(myList);
		request.getSession().setAttribute("userSession", userSession);
		return super.onSubmit(request, response, command, errors);
	
protected java.util.MapreferenceData(javax.servlet.http.HttpServletRequest request)

		Map model = new HashMap();
		model.put("languages", LANGUAGES);
		model.put("categories", this.petStore.getCategoryList());
		return model;
	
protected voidrejectIfEmpty(org.springframework.validation.Errors errors, java.lang.String field, java.lang.String errorCode, java.lang.String defaultMessage)

		Object fieldValue = errors.getFieldValue(field);
		if (fieldValue == null || fieldValue.toString().length() == 0) {
			errors.rejectValue(field, errorCode, defaultMessage);
		}
	
public voidsetPetStore(org.springframework.samples.jpetstore.domain.logic.PetStoreFacade petStore)

		this.petStore = petStore;