Search in sources :

Example 1 with ProductContentWrapper

use of org.apache.ofbiz.product.product.ProductContentWrapper in project ofbiz-framework by apache.

the class CatalogAltUrlSeoTransform method getWriter.

@Override
public Writer getWriter(final Writer out, final Map args) throws TemplateModelException, IOException {
    final StringBuilder buf = new StringBuilder();
    final boolean fullPath = checkArg(args, "fullPath", false);
    final boolean secure = checkArg(args, "secure", false);
    return new Writer(out) {

        public void write(char[] cbuf, int off, int len) throws IOException {
            buf.append(cbuf, off, len);
        }

        public void flush() throws IOException {
            out.flush();
        }

        public void close() throws IOException {
            try {
                Environment env = Environment.getCurrentEnvironment();
                BeanModel req = (BeanModel) env.getVariable("request");
                String previousCategoryId = getStringArg(args, "previousCategoryId");
                String productCategoryId = getStringArg(args, "productCategoryId");
                String productId = getStringArg(args, "productId");
                String url = "";
                Object prefix = env.getVariable("urlPrefix");
                String viewSize = getStringArg(args, "viewSize");
                String viewIndex = getStringArg(args, "viewIndex");
                String viewSort = getStringArg(args, "viewSort");
                String searchString = getStringArg(args, "searchString");
                if (req != null) {
                    HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
                    StringBuilder newURL = new StringBuilder();
                    if (UtilValidate.isNotEmpty(productId)) {
                        if (SeoConfigUtil.isCategoryUrlEnabled(request.getContextPath())) {
                            url = CatalogUrlSeoTransform.makeProductUrl(request, productId, productCategoryId, previousCategoryId);
                        } else {
                            url = CatalogUrlFilter.makeProductUrl(request, previousCategoryId, productCategoryId, productId);
                        }
                    } else {
                        if (SeoConfigUtil.isCategoryUrlEnabled(request.getContextPath())) {
                            url = CatalogUrlSeoTransform.makeCategoryUrl(request, productCategoryId, previousCategoryId, viewSize, viewIndex, viewSort, searchString);
                        } else {
                            url = CatalogUrlFilter.makeCategoryUrl(request, previousCategoryId, productCategoryId, productId, viewSize, viewIndex, viewSort, searchString);
                        }
                    }
                    // make the link
                    if (fullPath) {
                        try {
                            OfbizUrlBuilder builder = OfbizUrlBuilder.from(request);
                            builder.buildHostPart(newURL, "", secure);
                        } catch (WebAppConfigurationException e) {
                            Debug.logError(e.getMessage(), module);
                        }
                    }
                    newURL.append(url);
                    out.write(newURL.toString());
                } else if (prefix != null) {
                    Delegator delegator = FreeMarkerWorker.getWrappedObject("delegator", env);
                    LocalDispatcher dispatcher = FreeMarkerWorker.getWrappedObject("dispatcher", env);
                    Locale locale = (Locale) args.get("locale");
                    String prefixString = ((StringModel) prefix).getAsString();
                    prefixString = prefixString.replaceAll("/", "/");
                    String contextPath = prefixString;
                    int lastSlashIndex = prefixString.lastIndexOf('/');
                    if (lastSlashIndex > -1 && lastSlashIndex < prefixString.length()) {
                        contextPath = prefixString.substring(prefixString.lastIndexOf('/'));
                    }
                    if (UtilValidate.isNotEmpty(productId)) {
                        GenericValue product = EntityQuery.use(delegator).from("Product").where("productId", productId).queryOne();
                        ProductContentWrapper wrapper = new ProductContentWrapper(dispatcher, product, locale, EntityUtilProperties.getPropertyValue("content", "defaultMimeType", "text/html; charset=utf-8", delegator));
                        if (SeoConfigUtil.isCategoryUrlEnabled(contextPath)) {
                            url = CatalogUrlSeoTransform.makeProductUrl(delegator, wrapper, prefixString, contextPath, productCategoryId, previousCategoryId, productId);
                        } else {
                            url = CatalogUrlFilter.makeProductUrl(wrapper, null, prefixString, previousCategoryId, productCategoryId, productId);
                        }
                    } else {
                        GenericValue productCategory = EntityQuery.use(delegator).from("ProductCategory").where("productCategoryId", productCategoryId).queryOne();
                        CategoryContentWrapper wrapper = new CategoryContentWrapper(dispatcher, productCategory, locale, EntityUtilProperties.getPropertyValue("content", "defaultMimeType", "text/html; charset=utf-8", delegator));
                        if (SeoConfigUtil.isCategoryUrlEnabled(contextPath)) {
                            url = CatalogUrlSeoTransform.makeCategoryUrl(delegator, wrapper, prefixString, productCategoryId, previousCategoryId, productId, viewSize, viewIndex, viewSort, searchString);
                        } else {
                            url = CatalogUrlFilter.makeCategoryUrl(delegator, wrapper, null, prefixString, previousCategoryId, productCategoryId, productId, viewSize, viewIndex, viewSort, searchString);
                        }
                    }
                    out.write(url);
                } else {
                    out.write(buf.toString());
                }
            } catch (TemplateModelException | GenericEntityException e) {
                throw new IOException(e.getMessage());
            }
        }
    };
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) TemplateModelException(freemarker.template.TemplateModelException) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) BeanModel(freemarker.ext.beans.BeanModel) OfbizUrlBuilder(org.apache.ofbiz.webapp.OfbizUrlBuilder) CategoryContentWrapper(org.apache.ofbiz.product.category.CategoryContentWrapper) IOException(java.io.IOException) WebAppConfigurationException(org.apache.ofbiz.webapp.control.WebAppConfigurationException) HttpServletRequest(javax.servlet.http.HttpServletRequest) Delegator(org.apache.ofbiz.entity.Delegator) ProductContentWrapper(org.apache.ofbiz.product.product.ProductContentWrapper) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) Environment(freemarker.core.Environment) Writer(java.io.Writer)

Example 2 with ProductContentWrapper

use of org.apache.ofbiz.product.product.ProductContentWrapper in project ofbiz-framework by apache.

the class OfbizCatalogAltUrlTransform method getWriter.

@Override
public Writer getWriter(final Writer out, final Map args) throws TemplateModelException, IOException {
    final StringBuilder buf = new StringBuilder();
    final boolean fullPath = checkArg(args, "fullPath", false);
    final boolean secure = checkArg(args, "secure", false);
    return new Writer(out) {

        @Override
        public void write(char[] cbuf, int off, int len) throws IOException {
            buf.append(cbuf, off, len);
        }

        @Override
        public void flush() throws IOException {
            out.flush();
        }

        @Override
        public void close() throws IOException {
            try {
                Environment env = Environment.getCurrentEnvironment();
                BeanModel req = (BeanModel) env.getVariable("request");
                String previousCategoryId = getStringArg(args, "previousCategoryId");
                String productCategoryId = getStringArg(args, "productCategoryId");
                String productId = getStringArg(args, "productId");
                String url = "";
                Object prefix = env.getVariable("urlPrefix");
                String viewSize = getStringArg(args, "viewSize");
                String viewIndex = getStringArg(args, "viewIndex");
                String viewSort = getStringArg(args, "viewSort");
                String searchString = getStringArg(args, "searchString");
                if (req != null) {
                    HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
                    StringBuilder newURL = new StringBuilder();
                    if (UtilValidate.isNotEmpty(productId)) {
                        url = CatalogUrlFilter.makeProductUrl(request, previousCategoryId, productCategoryId, productId);
                    } else {
                        url = CatalogUrlFilter.makeCategoryUrl(request, previousCategoryId, productCategoryId, productId, viewSize, viewIndex, viewSort, searchString);
                    }
                    // make the link
                    if (fullPath) {
                        OfbizUrlBuilder builder = OfbizUrlBuilder.from(request);
                        builder.buildHostPart(newURL, url, secure);
                    }
                    newURL.append(url);
                    out.write(newURL.toString());
                } else if (prefix != null) {
                    Delegator delegator = FreeMarkerWorker.getWrappedObject("delegator", env);
                    LocalDispatcher dispatcher = FreeMarkerWorker.getWrappedObject("dispatcher", env);
                    Locale locale = (Locale) args.get("locale");
                    if (UtilValidate.isNotEmpty(productId)) {
                        GenericValue product = EntityQuery.use(delegator).from("Product").where("productId", productId).queryOne();
                        ProductContentWrapper wrapper = new ProductContentWrapper(dispatcher, product, locale, EntityUtilProperties.getPropertyValue("content", "defaultMimeType", "text/html; charset=utf-8", delegator));
                        url = CatalogUrlFilter.makeProductUrl(wrapper, null, ((StringModel) prefix).getAsString(), previousCategoryId, productCategoryId, productId);
                    } else {
                        GenericValue productCategory = EntityQuery.use(delegator).from("ProductCategory").where("productCategoryId", productCategoryId).queryOne();
                        CategoryContentWrapper wrapper = new CategoryContentWrapper(dispatcher, productCategory, locale, EntityUtilProperties.getPropertyValue("content", "defaultMimeType", "text/html; charset=utf-8", delegator));
                        url = CatalogUrlFilter.makeCategoryUrl(delegator, wrapper, null, ((StringModel) prefix).getAsString(), previousCategoryId, productCategoryId, productId, viewSize, viewIndex, viewSort, searchString);
                    }
                    out.write(url);
                } else {
                    out.write(buf.toString());
                }
            } catch (TemplateModelException | GenericEntityException | WebAppConfigurationException e) {
                throw new IOException(e.getMessage());
            }
        }
    };
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) TemplateModelException(freemarker.template.TemplateModelException) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) BeanModel(freemarker.ext.beans.BeanModel) OfbizUrlBuilder(org.apache.ofbiz.webapp.OfbizUrlBuilder) CategoryContentWrapper(org.apache.ofbiz.product.category.CategoryContentWrapper) IOException(java.io.IOException) WebAppConfigurationException(org.apache.ofbiz.webapp.control.WebAppConfigurationException) HttpServletRequest(javax.servlet.http.HttpServletRequest) Delegator(org.apache.ofbiz.entity.Delegator) ProductContentWrapper(org.apache.ofbiz.product.product.ProductContentWrapper) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) Environment(freemarker.core.Environment) Writer(java.io.Writer)

Example 3 with ProductContentWrapper

use of org.apache.ofbiz.product.product.ProductContentWrapper in project ofbiz-framework by apache.

the class CatalogUrlFilter method makeProductUrl.

public static String makeProductUrl(HttpServletRequest request, String previousCategoryId, String productCategoryId, String productId) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    String url = null;
    try {
        GenericValue product = EntityQuery.use(delegator).from("Product").where("productId", productId).cache().queryOne();
        ProductContentWrapper wrapper = new ProductContentWrapper(product, request);
        List<String> trail = CategoryWorker.getTrail(request);
        url = makeProductUrl(wrapper, trail, request.getContextPath(), previousCategoryId, productCategoryId, productId);
    } catch (GenericEntityException e) {
        Debug.logWarning(e, "Cannot create product's URL for: " + productId, module);
        return redirectUrl;
    }
    return url;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) ProductContentWrapper(org.apache.ofbiz.product.product.ProductContentWrapper) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 4 with ProductContentWrapper

use of org.apache.ofbiz.product.product.ProductContentWrapper in project ofbiz-framework by apache.

the class InvoiceWorker method getInvoiceItemDescription.

/**
 * Method to return the invoice item description with following step
 * 1. take the item description field
 * 2. if tax associate, resolve the taxAuthorityRateProduct description
 * 3. if product associate, call content wrapper to resolve PRODUCT_NAME or take the brandName
 * 4. take the item Type line description
 * @param dispatcher
 * @param invoiceItem
 * @param locale
 * @return the item description
 * @throws GenericEntityException
 */
public static String getInvoiceItemDescription(LocalDispatcher dispatcher, GenericValue invoiceItem, Locale locale) throws GenericEntityException {
    Delegator delegator = invoiceItem.getDelegator();
    String description = invoiceItem.getString("description");
    if (UtilValidate.isEmpty(description)) {
        String taxAuthorityRateSeqId = invoiceItem.getString("taxAuthorityRateSeqId");
        if (UtilValidate.isNotEmpty(taxAuthorityRateSeqId)) {
            GenericValue taxRate = invoiceItem.getRelatedOne("TaxAuthorityRateProduct", true);
            if (taxRate != null) {
                description = (String) taxRate.get("description", locale);
            }
        }
    }
    if (UtilValidate.isEmpty(description)) {
        String productId = invoiceItem.getString("productId");
        if (UtilValidate.isNotEmpty(productId)) {
            GenericValue product = EntityQuery.use(delegator).from("Product").where("productId", productId).cache().queryOne();
            ProductContentWrapper productContentWrapper = new ProductContentWrapper(dispatcher, product, locale, "text/html");
            StringUtil.StringWrapper stringWrapper = productContentWrapper.get("PRODUCT_NAME", "html");
            if (stringWrapper != null) {
                description = stringWrapper.toString();
            }
            if (UtilValidate.isEmpty(description)) {
                description = product.getString("brandName");
            }
        }
    }
    if (UtilValidate.isEmpty(description)) {
        description = (String) invoiceItem.getRelatedOne("InvoiceItemType", true).get("description", locale);
    }
    return description;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) ProductContentWrapper(org.apache.ofbiz.product.product.ProductContentWrapper) StringUtil(org.apache.ofbiz.base.util.StringUtil)

Example 5 with ProductContentWrapper

use of org.apache.ofbiz.product.product.ProductContentWrapper 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

Delegator (org.apache.ofbiz.entity.Delegator)5 GenericValue (org.apache.ofbiz.entity.GenericValue)5 ProductContentWrapper (org.apache.ofbiz.product.product.ProductContentWrapper)5 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)4 TemplateModelException (freemarker.template.TemplateModelException)3 IOException (java.io.IOException)3 Environment (freemarker.core.Environment)2 BeanModel (freemarker.ext.beans.BeanModel)2 Writer (java.io.Writer)2 Locale (java.util.Locale)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 CategoryContentWrapper (org.apache.ofbiz.product.category.CategoryContentWrapper)2 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)2 OfbizUrlBuilder (org.apache.ofbiz.webapp.OfbizUrlBuilder)2 WebAppConfigurationException (org.apache.ofbiz.webapp.control.WebAppConfigurationException)2 ServletException (javax.servlet.ServletException)1 StringUtil (org.apache.ofbiz.base.util.StringUtil)1 StringWrapper (org.apache.ofbiz.base.util.StringUtil.StringWrapper)1 MalformedPatternException (org.apache.oro.text.regex.MalformedPatternException)1