Search in sources :

Example 66 with TemplateModelException

use of freemarker.template.TemplateModelException in project ofbiz-framework by apache.

the class OfbizContentAltUrlTransforms method getWriter.

@Override
public Writer getWriter(final Writer out, final Map args) throws TemplateModelException, IOException {
    final StringBuilder buf = new StringBuilder();
    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");
                BeanModel res = (BeanModel) env.getVariable("response");
                if (req != null) {
                    String contentId = getStringArg(args, "contentId");
                    String viewContent = getStringArg(args, "viewContent");
                    HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
                    HttpServletResponse response = null;
                    if (res != null) {
                        response = (HttpServletResponse) res.getWrappedObject();
                    }
                    String url = "";
                    if (UtilValidate.isNotEmpty(contentId)) {
                        url = makeContentAltUrl(request, response, contentId, viewContent);
                    }
                    out.write(url);
                }
            } catch (TemplateModelException e) {
                throw new IOException(e.getMessage());
            }
        }
    };
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) TemplateModelException(freemarker.template.TemplateModelException) BeanModel(freemarker.ext.beans.BeanModel) Environment(freemarker.core.Environment) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Writer(java.io.Writer)

Example 67 with TemplateModelException

use of freemarker.template.TemplateModelException in project ofbiz-framework by apache.

the class OfbizCurrencyTransform method getArg.

private static String getArg(Map args, String key) {
    String result = "";
    Object o = args.get(key);
    if (o != null) {
        if (Debug.verboseOn())
            Debug.logVerbose("Arg Object : " + o.getClass().getName(), module);
        if (o instanceof TemplateScalarModel) {
            TemplateScalarModel s = (TemplateScalarModel) o;
            try {
                result = s.getAsString();
            } catch (TemplateModelException e) {
                Debug.logError(e, "Template Exception", module);
            }
        } else {
            result = o.toString();
        }
    }
    return result;
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) TemplateScalarModel(freemarker.template.TemplateScalarModel)

Example 68 with TemplateModelException

use of freemarker.template.TemplateModelException in project ofbiz-framework by apache.

the class CatalogUrlSeoTransform method getWriter.

@Override
public Writer getWriter(final Writer out, final Map args) throws TemplateModelException, IOException {
    final StringBuilder buf = new StringBuilder();
    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");
                if (req != null) {
                    String productId = getStringArg(args, "productId");
                    String currentCategoryId = getStringArg(args, "currentCategoryId");
                    String previousCategoryId = getStringArg(args, "previousCategoryId");
                    HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
                    if (!isCategoryMapInitialed()) {
                        initCategoryMap(request);
                    }
                    String catalogUrl = "";
                    if (SeoConfigUtil.isCategoryUrlEnabled(request.getContextPath())) {
                        if (UtilValidate.isEmpty(productId)) {
                            catalogUrl = makeCategoryUrl(request, currentCategoryId, previousCategoryId, null, null, null, null);
                        } else {
                            catalogUrl = makeProductUrl(request, productId, currentCategoryId, previousCategoryId);
                        }
                    } else {
                        catalogUrl = CatalogUrlServlet.makeCatalogUrl(request, productId, currentCategoryId, previousCategoryId);
                    }
                    out.write(catalogUrl);
                }
            } catch (TemplateModelException e) {
                throw new IOException(e.getMessage());
            }
        }
    };
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) TemplateModelException(freemarker.template.TemplateModelException) BeanModel(freemarker.ext.beans.BeanModel) Environment(freemarker.core.Environment) IOException(java.io.IOException) Writer(java.io.Writer)

Example 69 with TemplateModelException

use of freemarker.template.TemplateModelException in project ofbiz-framework by apache.

the class SeoTransform method getWriter.

public Writer getWriter(final Writer out, Map args) {
    final StringBuffer buf = new StringBuffer();
    final boolean fullPath = checkArg(args, "fullPath", false);
    final boolean secure = checkArg(args, "secure", false);
    final boolean encode = checkArg(args, "encode", true);
    return new Writer(out) {

        public void write(char[] cbuf, int off, int len) {
            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");
                BeanModel res = (BeanModel) env.getVariable("response");
                Object prefix = env.getVariable("urlPrefix");
                if (req != null) {
                    HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
                    ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
                    HttpServletResponse response = null;
                    if (res != null) {
                        response = (HttpServletResponse) res.getWrappedObject();
                    }
                    HttpSession session = request.getSession();
                    GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
                    // anonymous shoppers are not logged in
                    if (userLogin != null && "anonymous".equals(userLogin.getString("userLoginId"))) {
                        userLogin = null;
                    }
                    RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
                    out.write(seoUrl(rh.makeLink(request, response, buf.toString(), fullPath, secure, encode), userLogin == null));
                } else if (prefix != null) {
                    if (prefix instanceof TemplateScalarModel) {
                        TemplateScalarModel s = (TemplateScalarModel) prefix;
                        String prefixString = s.getAsString();
                        String bufString = buf.toString();
                        boolean prefixSlash = prefixString.endsWith("/");
                        boolean bufSlash = bufString.startsWith("/");
                        if (prefixSlash && bufSlash) {
                            bufString = bufString.substring(1);
                        } else if (!prefixSlash && !bufSlash) {
                            bufString = "/" + bufString;
                        }
                        out.write(prefixString + bufString);
                    }
                } else {
                    out.write(buf.toString());
                }
            } catch (IOException | TemplateModelException e) {
                throw new IOException(e.getMessage());
            }
        }
    };
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) TemplateModelException(freemarker.template.TemplateModelException) BeanModel(freemarker.ext.beans.BeanModel) HttpSession(javax.servlet.http.HttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestHandler(org.apache.ofbiz.webapp.control.RequestHandler) Environment(freemarker.core.Environment) ServletContext(javax.servlet.ServletContext) TemplateScalarModel(freemarker.template.TemplateScalarModel) Writer(java.io.Writer)

Example 70 with TemplateModelException

use of freemarker.template.TemplateModelException in project ofbiz-framework by apache.

the class OfbizCurrencyTransform method getWriter.

public Writer getWriter(final Writer out, Map args) {
    final StringBuilder buf = new StringBuilder();
    final BigDecimal amount = OfbizCurrencyTransform.getAmount(args, "amount");
    final String isoCode = OfbizCurrencyTransform.getArg(args, "isoCode");
    final String locale = OfbizCurrencyTransform.getArg(args, "locale");
    // check the rounding -- DEFAULT is 10 to not round for display, only use this when necessary
    // rounding should be handled by the code, however some times the numbers are coming from
    // someplace else (i.e. an integration)
    Integer roundingNumber = getInteger(args, "rounding");
    String scaleEnabled = "N";
    Environment env = Environment.getCurrentEnvironment();
    BeanModel req = null;
    try {
        req = (BeanModel) env.getVariable("request");
    } catch (TemplateModelException e) {
        Debug.logError(e.getMessage(), module);
    }
    if (req != null) {
        HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
        Delegator delegator = (Delegator) request.getAttribute("delegator");
        // Get rounding from SystemProperty
        if (UtilValidate.isNotEmpty(delegator)) {
            scaleEnabled = EntityUtilProperties.getPropertyValue("general", "currency.scale.enabled", "N", delegator);
            if (UtilValidate.isEmpty(roundingNumber)) {
                String roundingString = EntityUtilProperties.getPropertyValue("general", "currency.rounding.default", "10", delegator);
                if (UtilValidate.isInteger(roundingString))
                    roundingNumber = Integer.parseInt(roundingString);
            }
        }
    }
    if (roundingNumber == null)
        roundingNumber = 10;
    if ("Y".equals(scaleEnabled)) {
        if (amount.stripTrailingZeros().scale() <= 0) {
            roundingNumber = 0;
        }
    }
    final int rounding = roundingNumber;
    return new Writer(out) {

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

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

        @Override
        public void close() throws IOException {
            try {
                if (Debug.verboseOn())
                    Debug.logVerbose("parms: " + amount + " " + isoCode + " " + locale, module);
                if (locale.length() < 1) {
                    // Load the locale from the session
                    Environment env = Environment.getCurrentEnvironment();
                    BeanModel req = (BeanModel) env.getVariable("request");
                    if (req != null) {
                        HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
                        // we set the max to 10 digits as an hack to not round numbers in the ui
                        out.write(UtilFormatOut.formatCurrency(amount, isoCode, UtilHttp.getLocale(request), rounding));
                    } else {
                        // we set the max to 10 digits as an hack to not round numbers in the ui
                        out.write(UtilFormatOut.formatCurrency(amount, isoCode, env.getLocale(), rounding));
                    }
                } else {
                    // we set the max to 10 digits as an hack to not round numbers in the ui
                    out.write(UtilFormatOut.formatCurrency(amount, isoCode, new Locale(locale), rounding));
                }
            } catch (TemplateModelException e) {
                throw new IOException(e.getMessage());
            }
        }
    };
}
Also used : Locale(java.util.Locale) TemplateModelException(freemarker.template.TemplateModelException) BeanModel(freemarker.ext.beans.BeanModel) IOException(java.io.IOException) BigDecimal(java.math.BigDecimal) HttpServletRequest(javax.servlet.http.HttpServletRequest) Delegator(org.apache.ofbiz.entity.Delegator) Environment(freemarker.core.Environment) Writer(java.io.Writer)

Aggregations

TemplateModelException (freemarker.template.TemplateModelException)87 TemplateModel (freemarker.template.TemplateModel)24 IOException (java.io.IOException)21 TemplateScalarModel (freemarker.template.TemplateScalarModel)18 SimpleScalar (freemarker.template.SimpleScalar)17 BeanModel (freemarker.ext.beans.BeanModel)14 Environment (freemarker.core.Environment)13 Writer (java.io.Writer)13 ArrayList (java.util.ArrayList)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 Map (java.util.Map)10 List (java.util.List)9 SimpleNumber (freemarker.template.SimpleNumber)7 TemplateHashModel (freemarker.template.TemplateHashModel)7 Iterator (java.util.Iterator)6 freemarker.core._TemplateModelException (freemarker.core._TemplateModelException)5 TemplateHashModelEx (freemarker.template.TemplateHashModelEx)5 TemplateMethodModelEx (freemarker.template.TemplateMethodModelEx)5 TemplateModelIterator (freemarker.template.TemplateModelIterator)5 UMAException (com.ibm.uma.UMAException)4