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());
}
}
};
}
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;
}
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());
}
}
};
}
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());
}
}
};
}
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());
}
}
};
}
Aggregations