Java tutorial
/* * #%L * BroadleafCommerce Framework Web * %% * Copyright (C) 2009 - 2013 Broadleaf Commerce * %% * 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. * #L% */ package org.broadleafcommerce.core.web.controller.checkout; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.broadleafcommerce.common.i18n.domain.ISOCountry; import org.broadleafcommerce.common.i18n.service.ISOService; import org.broadleafcommerce.common.payment.service.PaymentGatewayCheckoutService; import org.broadleafcommerce.common.web.controller.BroadleafAbstractController; import org.broadleafcommerce.core.checkout.service.CheckoutService; import org.broadleafcommerce.core.order.service.FulfillmentGroupService; import org.broadleafcommerce.core.order.service.FulfillmentOptionService; import org.broadleafcommerce.core.order.service.OrderMultishipOptionService; import org.broadleafcommerce.core.order.service.OrderService; import org.broadleafcommerce.core.payment.service.OrderPaymentService; import org.broadleafcommerce.core.payment.service.OrderToPaymentRequestDTOService; import org.broadleafcommerce.core.web.checkout.validator.*; import org.broadleafcommerce.profile.core.domain.Country; import org.broadleafcommerce.profile.core.domain.Phone; import org.broadleafcommerce.profile.core.domain.PhoneImpl; import org.broadleafcommerce.profile.core.domain.State; import org.broadleafcommerce.profile.core.service.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.ui.Model; import org.springframework.web.bind.ServletRequestDataBinder; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.beans.PropertyEditorSupport; /** * An abstract controller that provides convenience methods and resource declarations for its * children. Operations that are shared between controllers that deal with checkout belong here. * * @author Andre Azzolini (apazzolini) * @author Elbert Bautista (elbertbautista) * @author Joshua Skorton (jskorton) */ public abstract class AbstractCheckoutController extends BroadleafAbstractController { private static final Log LOG = LogFactory.getLog(AbstractCheckoutController.class); protected static String cartPageRedirect = "redirect:/cart"; protected static String checkoutView = "checkout/checkout"; protected static String checkoutPageRedirect = "redirect:/checkout"; protected static String baseConfirmationView = "ajaxredirect:/confirmation"; /* Optional Service */ @Autowired(required = false) @Qualifier("blPaymentGatewayCheckoutService") protected PaymentGatewayCheckoutService paymentGatewayCheckoutService; /* Services */ @Resource(name = "blOrderService") protected OrderService orderService; @Resource(name = "blOrderPaymentService") protected OrderPaymentService orderPaymentService; @Resource(name = "blOrderToPaymentRequestDTOService") protected OrderToPaymentRequestDTOService dtoTranslationService; @Resource(name = "blFulfillmentGroupService") protected FulfillmentGroupService fulfillmentGroupService; @Resource(name = "blFulfillmentOptionService") protected FulfillmentOptionService fulfillmentOptionService; @Resource(name = "blCheckoutService") protected CheckoutService checkoutService; @Resource(name = "blCustomerService") protected CustomerService customerService; @Resource(name = "blStateService") protected StateService stateService; @Resource(name = "blCountryService") protected CountryService countryService; @Resource(name = "blCountrySubdivisionService") protected CountrySubdivisionService countrySubdivisionService; @Resource(name = "blISOService") protected ISOService isoService; @Resource(name = "blCustomerAddressService") protected CustomerAddressService customerAddressService; @Resource(name = "blAddressService") protected AddressService addressService; @Resource(name = "blPhoneService") protected PhoneService phoneService; @Resource(name = "blOrderMultishipOptionService") protected OrderMultishipOptionService orderMultishipOptionService; /* Validators */ @Resource(name = "blShippingInfoFormValidator") protected ShippingInfoFormValidator shippingInfoFormValidator; @Resource(name = "blBillingInfoFormValidator") protected BillingInfoFormValidator billingInfoFormValidator; @Resource(name = "blGiftCardInfoFormValidator") protected GiftCardInfoFormValidator giftCardInfoFormValidator; @Resource(name = "blMultishipAddAddressFormValidator") protected MultishipAddAddressFormValidator multishipAddAddressFormValidator; @Resource(name = "blOrderInfoFormValidator") protected OrderInfoFormValidator orderInfoFormValidator; /* Extension Managers */ @Resource(name = "blCheckoutControllerExtensionManager") protected BroadleafCheckoutControllerExtensionManager checkoutControllerExtensionManager; /* Views and Redirects */ public String getCartPageRedirect() { return cartPageRedirect; } public String getCheckoutView() { return checkoutView; } public String getCheckoutPageRedirect() { return checkoutPageRedirect; } public String getBaseConfirmationView() { return baseConfirmationView; } protected String getConfirmationView(String orderNumber) { return getBaseConfirmationView() + "/" + orderNumber; } protected void populateModelWithReferenceData(HttpServletRequest request, Model model) { //Add module specific model variables checkoutControllerExtensionManager.getProxy().addAdditionalModelVariables(model); } /** * Initializes some custom binding operations for the checkout flow. * More specifically, this method will attempt to bind state and country * abbreviations to actual State and Country objects when the String * representation of the abbreviation is submitted. * * @param request * @param binder * @throws Exception */ protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { /** * @deprecated - address.setState() is deprecated in favor of ISO standardization * This is here for legacy compatibility */ binder.registerCustomEditor(State.class, "address.state", new PropertyEditorSupport() { @Override public void setAsText(String text) { if (StringUtils.isNotEmpty(text)) { State state = stateService.findStateByAbbreviation(text); setValue(state); } else { setValue(null); } } }); /** * @deprecated - address.setCountry() is deprecated in favor of ISO standardization * This is here for legacy compatibility */ binder.registerCustomEditor(Country.class, "address.country", new PropertyEditorSupport() { @Override public void setAsText(String text) { if (StringUtils.isNotEmpty(text)) { Country country = countryService.findCountryByAbbreviation(text); setValue(country); } else { setValue(null); } } }); binder.registerCustomEditor(ISOCountry.class, "address.isoCountryAlpha2", new PropertyEditorSupport() { @Override public void setAsText(String text) { if (StringUtils.isNotEmpty(text)) { ISOCountry isoCountry = isoService.findISOCountryByAlpha2Code(text); setValue(isoCountry); } else { setValue(null); } } }); binder.registerCustomEditor(Phone.class, "address.phonePrimary", new PropertyEditorSupport() { @Override public void setAsText(String text) { Phone phone = new PhoneImpl(); phone.setPhoneNumber(text); setValue(phone); } }); binder.registerCustomEditor(Phone.class, "address.phoneSecondary", new PropertyEditorSupport() { @Override public void setAsText(String text) { Phone phone = new PhoneImpl(); phone.setPhoneNumber(text); setValue(phone); } }); binder.registerCustomEditor(Phone.class, "address.phoneFax", new PropertyEditorSupport() { @Override public void setAsText(String text) { Phone phone = new PhoneImpl(); phone.setPhoneNumber(text); setValue(phone); } }); } }