Search in sources :

Example 6 with StringWrapper

use of org.apache.ofbiz.base.util.StringUtil.StringWrapper in project ofbiz-framework by apache.

the class CatalogUrlSeoTransform method makeProductUrl.

/**
 * Make product url according to the configurations.
 *
 * @return String a catalog url
 */
public static String makeProductUrl(HttpServletRequest request, String productId, String currentCategoryId, String previousCategoryId) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    if (!isCategoryMapInitialed()) {
        initCategoryMap(request);
    }
    String contextPath = request.getContextPath();
    StringBuilder urlBuilder = new StringBuilder();
    GenericValue product = null;
    urlBuilder.append((request.getSession().getServletContext()).getContextPath());
    if (urlBuilder.length() == 0 || urlBuilder.charAt(urlBuilder.length() - 1) != '/') {
        urlBuilder.append("/");
    }
    if (UtilValidate.isNotEmpty(productId)) {
        try {
            product = EntityQuery.use(delegator).from("Product").where("productId", productId).cache().queryOne();
        } catch (GenericEntityException e) {
            Debug.logError(e, "Error looking up product info for productId [" + productId + "]: " + e.toString(), module);
        }
    }
    if (product != null) {
        urlBuilder.append(CatalogUrlServlet.PRODUCT_REQUEST + "/");
    }
    if (UtilValidate.isNotEmpty(currentCategoryId)) {
        List<String> trail = CategoryWorker.getTrail(request);
        trail = CategoryWorker.adjustTrail(trail, currentCategoryId, previousCategoryId);
        if (!SeoConfigUtil.isCategoryUrlEnabled(contextPath)) {
            for (String trailCategoryId : trail) {
                if ("TOP".equals(trailCategoryId)) {
                    continue;
                }
                urlBuilder.append("/");
                urlBuilder.append(trailCategoryId);
            }
        } else {
            if (trail.size() > 1) {
                String lastCategoryId = trail.get(trail.size() - 1);
                if (!"TOP".equals(lastCategoryId)) {
                    if (SeoConfigUtil.isCategoryNameEnabled()) {
                        String categoryName = CatalogUrlSeoTransform.getCategoryIdNameMap().get(lastCategoryId);
                        if (UtilValidate.isNotEmpty(categoryName)) {
                            urlBuilder.append(categoryName);
                            if (product != null) {
                                urlBuilder.append(URL_HYPHEN);
                            }
                        }
                    }
                }
            }
        }
    }
    if (UtilValidate.isNotEmpty(productId)) {
        if (product != null) {
            String productName = product.getString("productName");
            productName = SeoUrlUtil.replaceSpecialCharsUrl(productName);
            if (UtilValidate.isNotEmpty(productName)) {
                urlBuilder.append(productName + URL_HYPHEN);
            } else {
                ProductContentWrapper wrapper = new ProductContentWrapper(product, request);
                StringWrapper alternativeUrl = wrapper.get("ALTERNATIVE_URL", "url");
                if (UtilValidate.isNotEmpty(alternativeUrl) && UtilValidate.isNotEmpty(alternativeUrl.toString())) {
                    productName = SeoUrlUtil.replaceSpecialCharsUrl(alternativeUrl.toString());
                    if (UtilValidate.isNotEmpty(productName)) {
                        urlBuilder.append(productName + URL_HYPHEN);
                    }
                }
            }
        }
        try {
            urlBuilder.append(productId);
        } catch (Exception e) {
            urlBuilder.append(productId);
        }
    }
    if (!urlBuilder.toString().endsWith("/") && UtilValidate.isNotEmpty(SeoConfigUtil.getCategoryUrlSuffix())) {
        urlBuilder.append(SeoConfigUtil.getCategoryUrlSuffix());
    }
    return urlBuilder.toString();
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) StringWrapper(org.apache.ofbiz.base.util.StringUtil.StringWrapper) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ProductContentWrapper(org.apache.ofbiz.product.product.ProductContentWrapper) ServletException(javax.servlet.ServletException) TemplateModelException(freemarker.template.TemplateModelException) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) IOException(java.io.IOException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Aggregations

StringWrapper (org.apache.ofbiz.base.util.StringUtil.StringWrapper)6 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)4 GenericValue (org.apache.ofbiz.entity.GenericValue)4 IOException (java.io.IOException)3 TemplateModelException (freemarker.template.TemplateModelException)2 ServletException (javax.servlet.ServletException)2 MalformedPatternException (org.apache.oro.text.regex.MalformedPatternException)2 TemplateException (freemarker.template.TemplateException)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 Timestamp (java.sql.Timestamp)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1 GeneralException (org.apache.ofbiz.base.util.GeneralException)1