Search in sources :

Example 1 with CategoryContentWrapper

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

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

the class CatalogUrlSeoTransform method initCategoryMap.

public static synchronized void initCategoryMap(HttpServletRequest request, Delegator delegator) {
    if (SeoConfigUtil.checkCategoryUrl()) {
        categoryNameIdMap = new Hashtable<>();
        categoryIdNameMap = new Hashtable<>();
        Perl5Matcher matcher = new Perl5Matcher();
        try {
            Collection<GenericValue> allCategories = delegator.findList("ProductCategory", null, UtilMisc.toSet("productCategoryId", "categoryName"), null, null, false);
            for (GenericValue category : allCategories) {
                String categoryName = category.getString("categoryName");
                String categoryNameId = null;
                String categoryIdName = null;
                String categoryId = category.getString("productCategoryId");
                if (UtilValidate.isNotEmpty(categoryName)) {
                    categoryName = SeoUrlUtil.replaceSpecialCharsUrl(categoryName.trim());
                    if (matcher.matches(categoryName, asciiPattern)) {
                        categoryIdName = categoryName.replaceAll(" ", URL_HYPHEN);
                        categoryNameId = categoryIdName + URL_HYPHEN + categoryId.trim().replaceAll(" ", URL_HYPHEN);
                    } else {
                        categoryIdName = categoryId.trim().replaceAll(" ", URL_HYPHEN);
                        categoryNameId = categoryIdName;
                    }
                } else {
                    GenericValue productCategory = EntityQuery.use(delegator).from("ProductCategory").where("productCategoryId", categoryId).cache().queryOne();
                    CategoryContentWrapper wrapper = new CategoryContentWrapper(productCategory, request);
                    StringWrapper alternativeUrl = wrapper.get("ALTERNATIVE_URL", "url");
                    if (UtilValidate.isNotEmpty(alternativeUrl) && UtilValidate.isNotEmpty(alternativeUrl.toString())) {
                        categoryIdName = SeoUrlUtil.replaceSpecialCharsUrl(alternativeUrl.toString());
                        categoryNameId = categoryIdName + URL_HYPHEN + categoryId.trim().replaceAll(" ", URL_HYPHEN);
                    } else {
                        categoryNameId = categoryId.trim().replaceAll(" ", URL_HYPHEN);
                        categoryIdName = categoryNameId;
                    }
                }
                if (categoryNameIdMap.containsKey(categoryNameId)) {
                    categoryNameId = categoryId.trim().replaceAll(" ", URL_HYPHEN);
                    categoryIdName = categoryNameId;
                }
                if (!matcher.matches(categoryNameId, asciiPattern) || categoryNameIdMap.containsKey(categoryNameId)) {
                    continue;
                }
                categoryNameIdMap.put(categoryNameId, categoryId);
                categoryIdNameMap.put(categoryId, categoryIdName);
            }
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
        }
    }
    categoryMapInitialed = true;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) StringWrapper(org.apache.ofbiz.base.util.StringUtil.StringWrapper) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) CategoryContentWrapper(org.apache.ofbiz.product.category.CategoryContentWrapper)

Example 3 with CategoryContentWrapper

use of org.apache.ofbiz.product.category.CategoryContentWrapper 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)

Aggregations

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