Java tutorial
/* * [y] hybris Platform * * Copyright (c) 2000-2015 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 cec.easyshop.storefront.controllers.pages; import de.hybris.platform.acceleratorfacades.futurestock.FutureStockFacade; import de.hybris.platform.acceleratorservices.controllers.page.PageType; import de.hybris.platform.acceleratorstorefrontcommons.breadcrumb.impl.ProductBreadcrumbBuilder; import de.hybris.platform.acceleratorstorefrontcommons.constants.WebConstants; import de.hybris.platform.acceleratorstorefrontcommons.controllers.pages.AbstractPageController; import de.hybris.platform.acceleratorstorefrontcommons.controllers.util.GlobalMessages; import de.hybris.platform.acceleratorstorefrontcommons.forms.FutureStockForm; import de.hybris.platform.acceleratorstorefrontcommons.forms.ReviewForm; import de.hybris.platform.acceleratorstorefrontcommons.forms.validation.ReviewValidator; import de.hybris.platform.acceleratorstorefrontcommons.util.XSSFilterUtil; import de.hybris.platform.acceleratorstorefrontcommons.variants.VariantSortStrategy; import de.hybris.platform.catalog.enums.ProductReferenceTypeEnum; import de.hybris.platform.cms2.exceptions.CMSItemNotFoundException; import de.hybris.platform.cms2.model.pages.AbstractPageModel; import de.hybris.platform.cms2.servicelayer.services.CMSPageService; import de.hybris.platform.commercefacades.product.ProductFacade; import de.hybris.platform.commercefacades.product.ProductOption; import de.hybris.platform.commercefacades.product.data.*; import de.hybris.platform.commerceservices.url.UrlResolver; import de.hybris.platform.core.model.product.ProductModel; import de.hybris.platform.product.ProductService; import de.hybris.platform.servicelayer.exceptions.UnknownIdentifierException; import de.hybris.platform.util.Config; import cec.easyshop.storefront.controllers.ControllerConstants; import de.hybris.platform.acceleratorstorefrontcommons.util.MetaSanitizerUtil; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.springframework.context.annotation.Scope; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.google.common.collect.Maps; /** * Controller for product details page */ @Controller @Scope("tenant") @RequestMapping(value = "/**/p") public class ProductPageController extends AbstractPageController { @SuppressWarnings("unused") private static final Logger LOG = Logger.getLogger(ProductPageController.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 PRODUCT_CODE_PATH_VARIABLE_PATTERN = "/{productCode:.*}"; private static final String REVIEWS_PATH_VARIABLE_PATTERN = "{numberOfReviews:.*}"; private static final String FUTURE_STOCK_ENABLED = "storefront.products.futurestock.enabled"; private static final String STOCK_SERVICE_UNAVAILABLE = "basket.page.viewFuture.unavailable"; private static final String NOT_MULTISKU_ITEM_ERROR = "basket.page.viewFuture.not.multisku"; @Resource(name = "productModelUrlResolver") private UrlResolver<ProductModel> productModelUrlResolver; @Resource(name = "accProductFacade") private ProductFacade productFacade; @Resource(name = "productService") private ProductService productService; @Resource(name = "productBreadcrumbBuilder") private ProductBreadcrumbBuilder productBreadcrumbBuilder; @Resource(name = "cmsPageService") private CMSPageService cmsPageService; @Resource(name = "variantSortStrategy") private VariantSortStrategy variantSortStrategy; @Resource(name = "reviewValidator") private ReviewValidator reviewValidator; @Resource(name = "futureStockFacade") private FutureStockFacade futureStockFacade; @RequestMapping(value = PRODUCT_CODE_PATH_VARIABLE_PATTERN, method = RequestMethod.GET) public String productDetail(@PathVariable("productCode") final String productCode, final Model model, final HttpServletRequest request, final HttpServletResponse response) throws CMSItemNotFoundException, UnsupportedEncodingException { final ProductModel productModel = productService.getProductForCode(productCode); final String redirection = checkRequestUrl(request, response, productModelUrlResolver.resolve(productModel)); if (StringUtils.isNotEmpty(redirection)) { return redirection; } updatePageTitle(productModel, model); final List<ProductOption> extraOptions = Arrays.asList(ProductOption.VARIANT_MATRIX_BASE, ProductOption.VARIANT_MATRIX_URL, ProductOption.VARIANT_MATRIX_MEDIA); populateProductDetailForDisplay(productModel, model, request, extraOptions); model.addAttribute(new ReviewForm()); final List<ProductReferenceData> productReferences = productFacade.getProductReferencesForCode(productCode, Arrays.asList(ProductReferenceTypeEnum.SIMILAR, ProductReferenceTypeEnum.ACCESSORIES), Arrays.asList(ProductOption.BASIC, ProductOption.PRICE), null); model.addAttribute("productReferences", productReferences); model.addAttribute("pageType", PageType.PRODUCT.name()); model.addAttribute("futureStockEnabled", Boolean.valueOf(Config.getBoolean(FUTURE_STOCK_ENABLED, false))); final String metaKeywords = MetaSanitizerUtil.sanitizeKeywords(productModel.getKeywords()); final String metaDescription = MetaSanitizerUtil.sanitizeDescription(productModel.getDescription()); setUpMetaData(model, metaKeywords, metaDescription); return getViewForPage(model); } @RequestMapping(value = PRODUCT_CODE_PATH_VARIABLE_PATTERN + "/orderForm", method = RequestMethod.GET) public String productOrderForm(@PathVariable("productCode") final String productCode, final Model model, final HttpServletRequest request, final HttpServletResponse response) throws CMSItemNotFoundException { final ProductModel productModel = productService.getProductForCode(productCode); updatePageTitle(productModel, model); final List<ProductOption> extraOptions = Arrays.asList(ProductOption.VARIANT_MATRIX_BASE, ProductOption.VARIANT_MATRIX_PRICE, ProductOption.VARIANT_MATRIX_MEDIA, ProductOption.VARIANT_MATRIX_STOCK, ProductOption.URL); populateProductDetailForDisplay(productModel, model, request, extraOptions); if (!model.containsAttribute(WebConstants.MULTI_DIMENSIONAL_PRODUCT)) { return REDIRECT_PREFIX + productModelUrlResolver.resolve(productModel); } return ControllerConstants.Views.Pages.Product.OrderForm; } @RequestMapping(value = PRODUCT_CODE_PATH_VARIABLE_PATTERN + "/zoomImages", method = RequestMethod.GET) public String showZoomImages(@PathVariable("productCode") final String productCode, @RequestParam(value = "galleryPosition", required = false) final String galleryPosition, final Model model) { final ProductModel productModel = productService.getProductForCode(productCode); final ProductData productData = productFacade.getProductForOptions(productModel, Collections.singleton(ProductOption.GALLERY)); final List<Map<String, ImageData>> images = getGalleryImages(productData); populateProductData(productData, model); if (galleryPosition != null) { try { model.addAttribute("zoomImageUrl", images.get(Integer.parseInt(galleryPosition)).get("zoom").getUrl()); } catch (final IndexOutOfBoundsException | NumberFormatException ioebe) { model.addAttribute("zoomImageUrl", ""); } } return ControllerConstants.Views.Fragments.Product.ZoomImagesPopup; } @RequestMapping(value = PRODUCT_CODE_PATH_VARIABLE_PATTERN + "/quickView", method = RequestMethod.GET) public String showQuickView(@PathVariable("productCode") final String productCode, final Model model, final HttpServletRequest request) { final ProductModel productModel = productService.getProductForCode(productCode); final ProductData productData = productFacade.getProductForOptions(productModel, Arrays.asList(ProductOption.BASIC, ProductOption.PRICE, ProductOption.SUMMARY, ProductOption.DESCRIPTION, ProductOption.CATEGORIES, ProductOption.PROMOTIONS, ProductOption.STOCK, ProductOption.REVIEW, ProductOption.VARIANT_FULL, ProductOption.DELIVERY_MODE_AVAILABILITY)); sortVariantOptionData(productData); populateProductData(productData, model); getRequestContextData(request).setProduct(productModel); return ControllerConstants.Views.Fragments.Product.QuickViewPopup; } @RequestMapping(value = PRODUCT_CODE_PATH_VARIABLE_PATTERN + "/review", method = { RequestMethod.GET, RequestMethod.POST }) public String postReview(@PathVariable final String productCode, final ReviewForm form, final BindingResult result, final Model model, final HttpServletRequest request, final RedirectAttributes redirectAttrs) throws CMSItemNotFoundException { getReviewValidator().validate(form, result); final ProductModel productModel = productService.getProductForCode(productCode); if (result.hasErrors()) { updatePageTitle(productModel, model); GlobalMessages.addErrorMessage(model, "review.general.error"); model.addAttribute("showReviewForm", Boolean.TRUE); populateProductDetailForDisplay(productModel, model, request, Collections.EMPTY_LIST); storeCmsPageInModel(model, getPageForProduct(productModel)); return getViewForPage(model); } final ReviewData review = new ReviewData(); review.setHeadline(XSSFilterUtil.filter(form.getHeadline())); review.setComment(XSSFilterUtil.filter(form.getComment())); review.setRating(form.getRating()); review.setAlias(XSSFilterUtil.filter(form.getAlias())); productFacade.postReview(productCode, review); GlobalMessages.addFlashMessage(redirectAttrs, GlobalMessages.CONF_MESSAGES_HOLDER, "review.confirmation.thank.you.title"); return REDIRECT_PREFIX + productModelUrlResolver.resolve(productModel); } @RequestMapping(value = PRODUCT_CODE_PATH_VARIABLE_PATTERN + "/reviewhtml/" + REVIEWS_PATH_VARIABLE_PATTERN, method = RequestMethod.GET) public String reviewHtml(@PathVariable("productCode") final String productCode, @PathVariable("numberOfReviews") final String numberOfReviews, final Model model, final HttpServletRequest request) { final ProductModel productModel = productService.getProductForCode(productCode); final List<ReviewData> reviews; final ProductData productData = productFacade.getProductForOptions(productModel, Arrays.asList(ProductOption.BASIC, ProductOption.REVIEW)); if ("all".equals(numberOfReviews)) { reviews = productFacade.getReviews(productCode); } else { final int reviewCount = Math.min(Integer.parseInt(numberOfReviews), (productData.getNumberOfReviews() == null ? 0 : productData.getNumberOfReviews().intValue())); reviews = productFacade.getReviews(productCode, Integer.valueOf(reviewCount)); } getRequestContextData(request).setProduct(productModel); model.addAttribute("reviews", reviews); model.addAttribute("reviewsTotal", productData.getNumberOfReviews()); model.addAttribute(new ReviewForm()); return ControllerConstants.Views.Fragments.Product.ReviewsTab; } @RequestMapping(value = PRODUCT_CODE_PATH_VARIABLE_PATTERN + "/writeReview", method = RequestMethod.GET) public String writeReview(@PathVariable final String productCode, final Model model) throws CMSItemNotFoundException { final ProductModel productModel = productService.getProductForCode(productCode); model.addAttribute(new ReviewForm()); setUpReviewPage(model, productModel); return ControllerConstants.Views.Pages.Product.WriteReview; } protected void setUpReviewPage(final Model model, final ProductModel productModel) throws CMSItemNotFoundException { final String metaKeywords = MetaSanitizerUtil.sanitizeKeywords(productModel.getKeywords()); final String metaDescription = MetaSanitizerUtil.sanitizeDescription(productModel.getDescription()); setUpMetaData(model, metaKeywords, metaDescription); storeCmsPageInModel(model, getPageForProduct(productModel)); model.addAttribute("product", productFacade.getProductForOptions(productModel, Arrays.asList(ProductOption.BASIC))); updatePageTitle(productModel, model); } @RequestMapping(value = PRODUCT_CODE_PATH_VARIABLE_PATTERN + "/writeReview", method = RequestMethod.POST) public String writeReview(@PathVariable final String productCode, final ReviewForm form, final BindingResult result, final Model model, final HttpServletRequest request, final RedirectAttributes redirectAttrs) throws CMSItemNotFoundException { getReviewValidator().validate(form, result); final ProductModel productModel = productService.getProductForCode(productCode); if (result.hasErrors()) { GlobalMessages.addErrorMessage(model, "review.general.error"); populateProductDetailForDisplay(productModel, model, request, Collections.EMPTY_LIST); setUpReviewPage(model, productModel); return ControllerConstants.Views.Pages.Product.WriteReview; } final ReviewData review = new ReviewData(); review.setHeadline(XSSFilterUtil.filter(form.getHeadline())); review.setComment(XSSFilterUtil.filter(form.getComment())); review.setRating(form.getRating()); review.setAlias(XSSFilterUtil.filter(form.getAlias())); productFacade.postReview(productCode, review); GlobalMessages.addFlashMessage(redirectAttrs, GlobalMessages.CONF_MESSAGES_HOLDER, "review.confirmation.thank.you.title"); return REDIRECT_PREFIX + productModelUrlResolver.resolve(productModel); } @RequestMapping(value = PRODUCT_CODE_PATH_VARIABLE_PATTERN + "/futureStock", method = RequestMethod.GET) public String productFutureStock(@PathVariable("productCode") final String productCode, final Model model, final HttpServletRequest request, final HttpServletResponse response) throws CMSItemNotFoundException { final boolean futureStockEnabled = Config.getBoolean(FUTURE_STOCK_ENABLED, false); if (futureStockEnabled) { final List<FutureStockData> futureStockList = futureStockFacade.getFutureAvailability(productCode); if (futureStockList == null) { GlobalMessages.addErrorMessage(model, STOCK_SERVICE_UNAVAILABLE); } else if (futureStockList.isEmpty()) { GlobalMessages.addInfoMessage(model, "product.product.details.future.nostock"); } final ProductModel productModel = productService.getProductForCode(productCode); populateProductDetailForDisplay(productModel, model, request, Collections.EMPTY_LIST); model.addAttribute("futureStocks", futureStockList); return ControllerConstants.Views.Fragments.Product.FutureStockPopup; } else { return ControllerConstants.Views.Pages.Error.ErrorNotFoundPage; } } @ResponseBody @RequestMapping(value = PRODUCT_CODE_PATH_VARIABLE_PATTERN + "/grid/skusFutureStock", method = { RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_VALUE) public final Map<String, Object> productSkusFutureStock(final FutureStockForm form, final Model model) { final String productCode = form.getProductCode(); final List<String> skus = form.getSkus(); final boolean futureStockEnabled = Config.getBoolean(FUTURE_STOCK_ENABLED, false); Map<String, Object> result = new HashMap<>(); if (futureStockEnabled && CollectionUtils.isNotEmpty(skus) && StringUtils.isNotBlank(productCode)) { final Map<String, List<FutureStockData>> futureStockData = futureStockFacade .getFutureAvailabilityForSelectedVariants(productCode, skus); if (futureStockData == null) { // future availability service is down, we show this to the user result = Maps.newHashMap(); final String errorMessage = getMessageSource().getMessage(NOT_MULTISKU_ITEM_ERROR, null, getI18nService().getCurrentLocale()); result.put(NOT_MULTISKU_ITEM_ERROR, errorMessage); } else { for (final Map.Entry<String, List<FutureStockData>> entry : futureStockData.entrySet()) { result.put(entry.getKey(), entry.getValue()); } } } return result; } @ExceptionHandler(UnknownIdentifierException.class) public String handleUnknownIdentifierException(final UnknownIdentifierException exception, final HttpServletRequest request) { request.setAttribute("message", exception.getMessage()); return FORWARD_PREFIX + "/404"; } protected void updatePageTitle(final ProductModel productModel, final Model model) { storeContentPageTitleInModel(model, getPageTitleResolver().resolveProductPageTitle(productModel)); } protected void populateProductDetailForDisplay(final ProductModel productModel, final Model model, final HttpServletRequest request, final List<ProductOption> extraOptions) throws CMSItemNotFoundException { getRequestContextData(request).setProduct(productModel); final List<ProductOption> options = new ArrayList<>(Arrays.asList(ProductOption.VARIANT_FIRST_VARIANT, ProductOption.BASIC, ProductOption.URL, ProductOption.PRICE, ProductOption.SUMMARY, ProductOption.DESCRIPTION, ProductOption.GALLERY, ProductOption.CATEGORIES, ProductOption.REVIEW, ProductOption.PROMOTIONS, ProductOption.CLASSIFICATION, ProductOption.VARIANT_FULL, ProductOption.STOCK, ProductOption.VOLUME_PRICES, ProductOption.PRICE_RANGE, ProductOption.DELIVERY_MODE_AVAILABILITY)); options.addAll(extraOptions); final ProductData productData = productFacade.getProductForOptions(productModel, options); sortVariantOptionData(productData); storeCmsPageInModel(model, getPageForProduct(productModel)); populateProductData(productData, model); model.addAttribute(WebConstants.BREADCRUMBS_KEY, productBreadcrumbBuilder.getBreadcrumbs(productModel)); if (CollectionUtils.isNotEmpty(productData.getVariantMatrix())) { model.addAttribute(WebConstants.MULTI_DIMENSIONAL_PRODUCT, Boolean.valueOf(CollectionUtils.isNotEmpty(productData.getVariantMatrix()))); } } protected void populateProductData(final ProductData productData, final Model model) { model.addAttribute("galleryImages", getGalleryImages(productData)); model.addAttribute("product", productData); } protected void sortVariantOptionData(final ProductData productData) { if (CollectionUtils.isNotEmpty(productData.getBaseOptions())) { for (final BaseOptionData baseOptionData : productData.getBaseOptions()) { if (CollectionUtils.isNotEmpty(baseOptionData.getOptions())) { Collections.sort(baseOptionData.getOptions(), variantSortStrategy); } } } if (CollectionUtils.isNotEmpty(productData.getVariantOptions())) { Collections.sort(productData.getVariantOptions(), variantSortStrategy); } } protected List<Map<String, ImageData>> getGalleryImages(final ProductData productData) { final List<Map<String, ImageData>> galleryImages = new ArrayList<>(); if (CollectionUtils.isNotEmpty(productData.getImages())) { final List<ImageData> images = new ArrayList<>(); for (final ImageData image : productData.getImages()) { if (ImageDataType.GALLERY.equals(image.getImageType())) { images.add(image); } } Collections.sort(images, new Comparator<ImageData>() { @Override public int compare(final ImageData image1, final ImageData image2) { return image1.getGalleryIndex().compareTo(image2.getGalleryIndex()); } }); if (CollectionUtils.isNotEmpty(images)) { int currentIndex = images.get(0).getGalleryIndex().intValue(); Map<String, ImageData> formats = new HashMap<String, ImageData>(); for (final ImageData image : images) { if (currentIndex != image.getGalleryIndex().intValue()) { galleryImages.add(formats); formats = new HashMap<>(); currentIndex = image.getGalleryIndex().intValue(); } formats.put(image.getFormat(), image); } if (!formats.isEmpty()) { galleryImages.add(formats); } } } return galleryImages; } protected ReviewValidator getReviewValidator() { return reviewValidator; } protected AbstractPageModel getPageForProduct(final ProductModel product) throws CMSItemNotFoundException { return cmsPageService.getPageForProduct(product); } }