Java tutorial
/* * [y] hybris Platform * * Copyright (c) 2000-2013 hybris AG * All rights reserved. * * This software is the confidential and proprietary information of hybris * ("Confidential Information"). You shall not disclose such Confidential * Information and shall use it only in accordance with the terms of the * license agreement you entered into with hybris. * * */ package com.acc.storefront.controllers.pages; import de.hybris.platform.acceleratorservices.controllers.page.PageType; import de.hybris.platform.cms2.exceptions.CMSItemNotFoundException; import de.hybris.platform.cms2.model.pages.AbstractPageModel; import de.hybris.platform.commercefacades.order.CheckoutFacade; import de.hybris.platform.commercefacades.order.OrderFacade; import de.hybris.platform.commercefacades.order.data.OrderData; import de.hybris.platform.commercefacades.order.data.OrderEntryData; import de.hybris.platform.commercefacades.product.ProductFacade; import de.hybris.platform.commercefacades.product.ProductOption; import de.hybris.platform.commercefacades.product.data.ProductData; import de.hybris.platform.commerceservices.customer.CustomerAccountService; import de.hybris.platform.commerceservices.customer.DuplicateUidException; import de.hybris.platform.commerceservices.strategies.CheckoutCustomerStrategy; import de.hybris.platform.core.model.order.OrderModel; import de.hybris.platform.servicelayer.exceptions.ModelNotFoundException; import de.hybris.platform.servicelayer.user.UserService; import de.hybris.platform.store.BaseStoreModel; import de.hybris.platform.store.services.BaseStoreService; import java.util.Arrays; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.acc.facades.flow.impl.SessionOverrideCheckoutFlowFacade; import com.acc.storefront.controllers.ControllerConstants; import de.hybris.platform.acceleratorstorefrontcommons.annotations.RequireHardLogIn; import de.hybris.platform.acceleratorstorefrontcommons.constants.WebConstants; import de.hybris.platform.acceleratorstorefrontcommons.controllers.pages.AbstractCheckoutController; import de.hybris.platform.acceleratorstorefrontcommons.forms.GuestRegisterForm; import de.hybris.platform.acceleratorstorefrontcommons.forms.validation.GuestRegisterValidator; import de.hybris.platform.acceleratorstorefrontcommons.security.AutoLoginStrategy; import de.hybris.platform.cms2.model.pages.AbstractPageModel; import de.hybris.platform.acceleratorstorefrontcommons.controllers.util.GlobalMessages; import de.hybris.platform.core.model.user.CustomerModel; /** * CheckoutController */ @Controller @Scope("tenant") @RequestMapping(value = "/checkout") public class CheckoutController extends AbstractCheckoutController { protected static final Logger LOG = Logger.getLogger(CheckoutController.class); /** * We use this suffix pattern because of an issue with Spring 3.1 where a Uri value is incorrectly extracted if it * contains on or more '.' characters. Please see https://jira.springsource.org/browse/SPR-6164 for a discussion on * the issue and future resolution. */ private static final String ORDER_CODE_PATH_VARIABLE_PATTERN = "{orderCode:.*}"; private static final String CHECKOUT_ORDER_CONFIRMATION_CMS_PAGE_LABEL = "orderConfirmation"; @Resource(name = "productFacade") private ProductFacade productFacade; @Resource(name = "orderFacade") private OrderFacade orderFacade; @Resource(name = "checkoutCustomerStrategy") private CheckoutCustomerStrategy checkoutCustomerStrategy; @Resource(name = "checkoutFacade") private CheckoutFacade checkoutFacade; private OrderModel orderModel; @Resource(name = "guestRegisterValidator") private GuestRegisterValidator guestRegisterValidator; @Resource(name = "autoLoginStrategy") private AutoLoginStrategy autoLoginStrategy; @Resource(name = "userService") private UserService userService; @Resource(name = "customerAccountService") private CustomerAccountService customerAccountService; @Resource(name = "baseStoreService") private BaseStoreService baseStoreService; @ExceptionHandler(ModelNotFoundException.class) public String handleModelNotFoundException(final ModelNotFoundException exception, final HttpServletRequest request) { request.setAttribute("message", exception.getMessage()); return FORWARD_PREFIX + "/404"; } @RequestMapping(method = RequestMethod.GET) public String checkout(final RedirectAttributes redirectModel) { if (hasValidCart()) { if (validateCart(redirectModel)) { return REDIRECT_PREFIX + "/cart"; } else { checkoutFacade.prepareCartForCheckout(); return getCheckoutRedirectUrl(); } } LOG.info("Missing, empty or unsupported cart"); // No session cart or empty session cart. Bounce back to the cart page. return REDIRECT_PREFIX + "/cart"; } @RequestMapping(value = "/orderConfirmation/" + ORDER_CODE_PATH_VARIABLE_PATTERN, method = RequestMethod.GET) @RequireHardLogIn public String orderConfirmation(@PathVariable("orderCode") final String orderCode, final HttpServletRequest request, final Model model) throws CMSItemNotFoundException { SessionOverrideCheckoutFlowFacade.resetSessionOverrides(); return processOrderCode(orderCode, model, request); } @RequestMapping(value = "/orderConfirmation/" + ORDER_CODE_PATH_VARIABLE_PATTERN, method = RequestMethod.POST) public String orderConfirmation(final GuestRegisterForm form, final BindingResult bindingResult, final Model model, final HttpServletRequest request, final HttpServletResponse response, final RedirectAttributes redirectModel) throws CMSItemNotFoundException { getGuestRegisterValidator().validate(form, bindingResult); return processRegisterGuestUserRequest(form, bindingResult, model, request, response, redirectModel); } /** * Method used to determine the checkout redirect URL that will handle the checkout process. * * @return A <code>String</code> object of the URL to redirect to. */ protected String getCheckoutRedirectUrl() { if (getUserFacade().isAnonymousUser()) { return REDIRECT_PREFIX + "/login/checkout"; } // Default to the multi-step checkout return REDIRECT_PREFIX + "/checkout/multi"; } protected String processRegisterGuestUserRequest(final GuestRegisterForm form, final BindingResult bindingResult, final Model model, final HttpServletRequest request, final HttpServletResponse response, final RedirectAttributes redirectModel) throws CMSItemNotFoundException { if (bindingResult.hasErrors()) { GlobalMessages.addErrorMessage(model, "form.global.error"); return processOrderCode(form.getOrderCode(), model, request); } try { getCustomerFacade().changeGuestToCustomer(form.getPwd(), form.getOrderCode()); getAutoLoginStrategy().login(getCustomerFacade().getCurrentCustomer().getUid(), form.getPwd(), request, response); getSessionService().removeAttribute(WebConstants.ANONYMOUS_CHECKOUT); } catch (final DuplicateUidException e) { // User already exists LOG.warn("guest registration failed: " + e); model.addAttribute(new GuestRegisterForm()); GlobalMessages.addFlashMessage(redirectModel, GlobalMessages.ERROR_MESSAGES_HOLDER, "guest.checkout.existingaccount.register.error", new Object[] { form.getUid() }); return REDIRECT_PREFIX + request.getHeader("Referer"); } return REDIRECT_PREFIX + "/my-account"; } protected String processOrderCode(final String orderCode, final Model model, final HttpServletRequest request) throws CMSItemNotFoundException { final OrderData orderDetails = orderFacade.getOrderDetailsForCode(orderCode); if (orderDetails.isGuestCustomer() && !StringUtils.substringBefore(orderDetails.getUser().getUid(), "|") .equals(getSessionService().getAttribute(WebConstants.ANONYMOUS_CHECKOUT_GUID))) { return getCheckoutRedirectUrl(); } if (orderDetails.getEntries() != null && !orderDetails.getEntries().isEmpty()) { for (final OrderEntryData entry : orderDetails.getEntries()) { final String productCode = entry.getProduct().getCode(); final ProductData product = productFacade.getProductForCodeAndOptions(productCode, Arrays.asList(ProductOption.BASIC, ProductOption.PRICE, ProductOption.CATEGORIES)); entry.setProduct(product); } final BaseStoreModel baseStoreModel = baseStoreService.getCurrentBaseStore(); final OrderModel orderModel = checkoutCustomerStrategy.isAnonymousCheckout() ? customerAccountService.getOrderDetailsForGUID(orderCode, baseStoreModel) : customerAccountService.getOrderForCode((CustomerModel) userService.getCurrentUser(), orderCode, baseStoreModel); if (StringUtils.isNotEmpty(orderModel.getUCOID())) { model.addAttribute("UCOID", orderModel.getUCOID()); System.out.println("ucoid" + orderModel.getUCOID()); } } model.addAttribute("orderCode", orderCode); model.addAttribute("orderData", orderDetails); model.addAttribute("allItems", orderDetails.getEntries()); model.addAttribute("deliveryAddress", orderDetails.getDeliveryAddress()); model.addAttribute("deliveryMode", orderDetails.getDeliveryMode()); model.addAttribute("paymentInfo", orderDetails.getPaymentInfo()); model.addAttribute("pageType", PageType.ORDERCONFIRMATION.name()); final String uid = orderDetails.getPaymentInfo().getBillingAddress().getEmail(); model.addAttribute("email", uid); if (orderDetails.isGuestCustomer() && !model.containsAttribute("guestRegisterForm")) { final GuestRegisterForm guestRegisterForm = new GuestRegisterForm(); guestRegisterForm.setOrderCode(orderDetails.getGuid()); guestRegisterForm.setUid(uid); model.addAttribute(guestRegisterForm); } final AbstractPageModel cmsPage = getContentPageForLabelOrId(CHECKOUT_ORDER_CONFIRMATION_CMS_PAGE_LABEL); storeCmsPageInModel(model, cmsPage); setUpMetaDataForContentPage(model, getContentPageForLabelOrId(CHECKOUT_ORDER_CONFIRMATION_CMS_PAGE_LABEL)); model.addAttribute("metaRobots", "no-index,no-follow"); return ControllerConstants.Views.Pages.Checkout.CheckoutConfirmationPage; } protected GuestRegisterValidator getGuestRegisterValidator() { return guestRegisterValidator; } protected AutoLoginStrategy getAutoLoginStrategy() { return autoLoginStrategy; } }