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 org.training.storefront.breadcrumb.impl; import de.hybris.platform.catalog.model.classification.ClassificationClassModel; import de.hybris.platform.category.model.CategoryModel; import de.hybris.platform.commercefacades.product.data.ProductData; import de.hybris.platform.commerceservices.url.UrlResolver; import de.hybris.platform.core.model.product.ProductModel; import de.hybris.platform.variants.model.VariantProductModel; import org.training.storefront.breadcrumb.Breadcrumb; import org.training.storefront.history.BrowseHistory; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import org.springframework.beans.factory.annotation.Required; /** * ProductBreadcrumbBuilder implementation for {@link ProductData} */ public class ProductBreadcrumbBuilder { private static final String LAST_LINK_CLASS = "active"; private UrlResolver<ProductModel> productModelUrlResolver; private UrlResolver<CategoryModel> categoryModelUrlResolver; private BrowseHistory browseHistory; public List<Breadcrumb> getBreadcrumbs(final ProductModel productModel) throws IllegalArgumentException { final List<Breadcrumb> breadcrumbs = new ArrayList<>(); final Collection<CategoryModel> categoryModels = new ArrayList<>(); final Breadcrumb last; final ProductModel baseProductModel = getBaseProduct(productModel); last = getProductBreadcrumb(baseProductModel); categoryModels.addAll(baseProductModel.getSupercategories()); last.setLinkClass(LAST_LINK_CLASS); breadcrumbs.add(last); while (!categoryModels.isEmpty()) { CategoryModel toDisplay = null; for (final CategoryModel categoryModel : categoryModels) { if (!(categoryModel instanceof ClassificationClassModel)) { if (toDisplay == null) { toDisplay = categoryModel; } if (getBrowseHistory().findEntryMatchUrlEndsWith(categoryModel.getCode()) != null) { break; } } } categoryModels.clear(); if (toDisplay != null) { breadcrumbs.add(getCategoryBreadcrumb(toDisplay)); categoryModels.addAll(toDisplay.getSupercategories()); } } Collections.reverse(breadcrumbs); return breadcrumbs; } protected ProductModel getBaseProduct(final ProductModel product) { if (product instanceof VariantProductModel) { return getBaseProduct(((VariantProductModel) product).getBaseProduct()); } return product; } protected Breadcrumb getProductBreadcrumb(final ProductModel product) { final String productUrl = getProductModelUrlResolver().resolve(product); return new Breadcrumb(productUrl, product.getName(), null); } protected Breadcrumb getCategoryBreadcrumb(final CategoryModel category) { final String categoryUrl = getCategoryModelUrlResolver().resolve(category); return new Breadcrumb(categoryUrl, category.getName(), null, category.getCode()); } protected UrlResolver<ProductModel> getProductModelUrlResolver() { return productModelUrlResolver; } @Required public void setProductModelUrlResolver(final UrlResolver<ProductModel> productModelUrlResolver) { this.productModelUrlResolver = productModelUrlResolver; } protected UrlResolver<CategoryModel> getCategoryModelUrlResolver() { return categoryModelUrlResolver; } @Required public void setCategoryModelUrlResolver(final UrlResolver<CategoryModel> categoryModelUrlResolver) { this.categoryModelUrlResolver = categoryModelUrlResolver; } protected BrowseHistory getBrowseHistory() { return browseHistory; } @Required public void setBrowseHistory(final BrowseHistory browseHistory) { this.browseHistory = browseHistory; } }