Search code examples
javaspringspring-mvcsap-commerce-cloud

Custom Product Url-Resolver in Hybris


I am trying to write URL Resolver in Hybris but I am facing with couple of errors;

The method getBaseProduct(ProductModel) is undefined for the type TrialProductModelUrlResolver

The method getCategoryPath(CategoryModel) in the type DefaultCategoryModelUrlResolver is not applicable for the arguments (ProductModel)

The method getProductForCode(String) in the type ProductService is not applicable for the arguments (ProductModel)

public class TrailerProductModelUrlResolver extends DefaultCategoryModelUrlResolver {

private TrailerURLNormalizationStrategy trailerURLNormalizationStrategy;

@Autowired
private ProductService productService;

@Override
protected String resolveInternal(final ProductModel source) {
    final ProductModel baseProduct = getBaseProduct(source);
    final BaseSiteModel currentBaseSite = getBaseSiteService().getCurrentBaseSite();
    String url = getPattern();

    if ((currentBaseSite != null) && (url.contains("{baseSite-uid}"))) {
        url = url.replace("{baseSite-uid}", currentBaseSite.getUid());
    }

    if (url.contains("{category-path}")) {
        url = url.replace("{category-path}", trailerURLNormalizationStrategy.normalizeString(buildPathString(getCategoryPath(source))));
    }

    if (url.contains("{product-name}")) {
        String productName = source.getName(TrailerCoreConstants.LOCALE_TR);

        if (StringUtils.isEmpty(productName)) {
            productName = baseProduct.getName(TrailerCoreConstants.LOCALE_TR);
        }

        CategoryModel categoryModel = productService.getProductForCode(baseProduct);
        if (categoryModel != null) {
            if (categoryModel.getName() != null) {
                if (!categoryModel.getName(TrailerCoreConstants.LOCALE_TR).isEmpty()) {
                    productName = categoryModel.getName(TrailerCoreConstants.LOCALE_TR) + " " + productName;
                }
            }
        }
        url = url.replace("{product-name}", trailerURLNormalizationStrategy.normalizeString(productName));
    }

    if (url.contains("{product-code}")) {
        url = url.replace("{product-code}", trailerURLNormalizationStrategy.normalizeString(source.getCode()));
    }

    return url;
}

@Override
protected String buildPathString(final List<CategoryModel> path) {
    if ((path == null) || (path.isEmpty())) {
        return "c";
    }

    final StringBuilder result = new StringBuilder();

    for (int i = 0; i < path.size(); ++i) {
        if (i != 0) {
            result.append('/');
        }
        result.append(path.get(i).getName());
    }

    return result.toString();
}

public TrailerURLNormalizationStrategy getTrailerURLNormalizationStrategy() {
    return trailerURLNormalizationStrategy;
}

public void setTrailerURLNormalizationStrategy(TrailerURLNormalizationStrategy trailerURLNormalizationStrategy) {
    this.trailerURLNormalizationStrategy = trailerURLNormalizationStrategy;
}
}

any help

Regards


Solution

  • Just change DefaultCategoryModelUrlResolver to DefaultProductModelUrlResolver

    For the Product UrlResolver, extend DefaultProductModelUrlResolver

    public class TrailerProductModelUrlResolver extends DefaultProductModelUrlResolver
    {
    
        private static final Logger LOG = Logger.getLogger(TrailerProductModelUrlResolver.class);
        @Override
        protected String resolveInternal(final ProductModel source)
        {
            final ProductModel baseProduct = getBaseProduct(source);
            ...
        }
    }
    

    For Category UrlResolver, extend DefaultCategoryModelUrlResolver

    public class TrailerCategoryModelUrlResolver extends DefaultCategoryModelUrlResolver
    {
        @Override
        protected String resolveInternal(CategoryModel source)
        {
    
        }
    }