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

OrderFormController

public class OrderFormController extends org.springframework.web.servlet.mvc.AbstractWizardFormController
author
Juergen Hoeller
since
01.12.2003

Fields Summary
private org.springframework.samples.jpetstore.domain.logic.PetStoreFacade
petStore
Constructors Summary
public OrderFormController()

		setCommandName("orderForm");
		setPages(new String[] {"NewOrderForm", "ShippingForm", "ConfirmOrder"});
	
Methods Summary
protected java.lang.ObjectformBackingObject(javax.servlet.http.HttpServletRequest request)

		UserSession userSession = (UserSession) request.getSession().getAttribute("userSession");
		Cart cart = (Cart) request.getSession().getAttribute("sessionCart");
		if (cart != null) {
			// Re-read account from DB at team's request.
			Account account = this.petStore.getAccount(userSession.getAccount().getUsername());
			OrderForm orderForm = new OrderForm();
			orderForm.getOrder().initOrder(account, cart);
			return orderForm;
		}
		else {
			ModelAndView modelAndView = new ModelAndView("Error", "message", "An order could not be created because a cart could not be found.");
			throw new ModelAndViewDefiningException(modelAndView);
		}
	
protected intgetTargetPage(javax.servlet.http.HttpServletRequest request, java.lang.Object command, org.springframework.validation.Errors errors, int currentPage)

		OrderForm orderForm = (OrderForm) command;
		if (currentPage == 0 && orderForm.isShippingAddressRequired()) {
			return 1;
		}
		else {
			return 2;
		}
	
protected booleanisFormSubmission(javax.servlet.http.HttpServletRequest request)

		return super.isFormSubmission(request) || request.getParameter(PARAM_FINISH) != null;
	
protected voidonBindAndValidate(javax.servlet.http.HttpServletRequest request, java.lang.Object command, org.springframework.validation.BindException errors, int page)

		if (page == 0 && request.getParameter("shippingAddressRequired") == null) {
			OrderForm orderForm = (OrderForm) command;
			orderForm.setShippingAddressRequired(false);
		}
	
protected org.springframework.web.servlet.ModelAndViewprocessCancel(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.Object command, org.springframework.validation.BindException errors)

		throw new UnsupportedOperationException();
	
protected org.springframework.web.servlet.ModelAndViewprocessFinish(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.Object command, org.springframework.validation.BindException errors)

		OrderForm orderForm = (OrderForm) command;
		this.petStore.insertOrder(orderForm.getOrder());
		request.getSession().removeAttribute("sessionCart");
		Map model = new HashMap();
		model.put("order", orderForm.getOrder());
		model.put("message", "Thank you, your order has been submitted.");
		return new ModelAndView("ViewOrder", model);
	
protected java.util.MapreferenceData(javax.servlet.http.HttpServletRequest request, int page)

		if (page == 0) {
			List creditCardTypes = new ArrayList();
			creditCardTypes.add("Visa");
			creditCardTypes.add("MasterCard");
			creditCardTypes.add("American Express");
			Map model = new HashMap();
			model.put("creditCardTypes", creditCardTypes);
			return model;
		}
		return null;
	
public voidsetPetStore(org.springframework.samples.jpetstore.domain.logic.PetStoreFacade petStore)

		this.petStore = petStore;
	
protected voidvalidatePage(java.lang.Object command, org.springframework.validation.Errors errors, int page)

		OrderForm orderForm = (OrderForm) command;
		OrderValidator orderValidator = (OrderValidator) getValidator();
		errors.setNestedPath("order");
		switch (page) {
			case 0:
				orderValidator.validateCreditCard(orderForm.getOrder(), errors);
				orderValidator.validateBillingAddress(orderForm.getOrder(), errors);
				break;
			case 1:
				orderValidator.validateShippingAddress(orderForm.getOrder(), errors);
		}
		errors.setNestedPath("");