Search in sources :

Example 1 with MapStack

use of org.apache.ofbiz.base.util.collections.MapStack in project ofbiz-framework by apache.

the class RenderContentAndSubContent method getWriter.

@Override
@SuppressWarnings("unchecked")
public Writer getWriter(final Writer out, Map args) {
    final Environment env = Environment.getCurrentEnvironment();
    final LocalDispatcher dispatcher = FreeMarkerWorker.getWrappedObject("dispatcher", env);
    final HttpServletRequest request = FreeMarkerWorker.getWrappedObject("request", env);
    final Map<String, Object> envMap = FreeMarkerWorker.createEnvironmentMap(env);
    final MapStack<String> templateRoot = MapStack.create();
    ((MapStack) templateRoot).push(envMap);
    if (Debug.verboseOn()) {
        Debug.logVerbose("in RenderContentAndSubContent, contentId(0):" + templateRoot.get("contentId"), module);
    }
    FreeMarkerWorker.getSiteParameters(request, templateRoot);
    FreeMarkerWorker.overrideWithArgs(templateRoot, args);
    return new Writer(out) {

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

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

        @Override
        public void close() throws IOException {
            renderSubContent();
        }

        public void renderSubContent() throws IOException {
            String mimeTypeId = (String) templateRoot.get("mimeTypeId");
            Object localeObject = templateRoot.get("locale");
            Locale locale = null;
            if (localeObject == null) {
                locale = UtilHttp.getLocale(request);
            } else {
                locale = UtilMisc.ensureLocale(localeObject);
            }
            if (Debug.verboseOn()) {
                Debug.logVerbose("in RenderContentAndSubContent, contentId(2):" + templateRoot.get("contentId"), module);
            }
            if (Debug.verboseOn()) {
                Debug.logVerbose("in RenderContentAndSubContent, subContentId(2):" + templateRoot.get("subContentId"), module);
            }
            try {
                String contentId = (String) templateRoot.get("contentId");
                String mapKey = (String) templateRoot.get("mapKey");
                String contentAssocTypeId = (String) templateRoot.get("contentAssocTypeId");
                if (UtilValidate.isNotEmpty(mapKey) || UtilValidate.isNotEmpty(contentAssocTypeId)) {
                    String txt = ContentWorker.renderSubContentAsText(dispatcher, contentId, mapKey, templateRoot, locale, mimeTypeId, true);
                    out.write(txt);
                } else if (contentId != null) {
                    ContentWorker.renderContentAsText(dispatcher, contentId, out, templateRoot, locale, mimeTypeId, null, null, true);
                }
            } catch (GeneralException e) {
                String errMsg = "Error rendering thisContentId:" + (String) templateRoot.get("contentId") + " msg:" + e.toString();
                Debug.logError(e, errMsg, module);
            // just log a message and don't return anything: throw new IOException();
            }
        }
    };
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Locale(java.util.Locale) MapStack(org.apache.ofbiz.base.util.collections.MapStack) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) GeneralException(org.apache.ofbiz.base.util.GeneralException) Environment(freemarker.core.Environment) Writer(java.io.Writer)

Example 2 with MapStack

use of org.apache.ofbiz.base.util.collections.MapStack in project ofbiz-framework by apache.

the class RenderContentTransform method getWriter.

@Override
@SuppressWarnings("unchecked")
public Writer getWriter(final Writer out, Map args) {
    final Environment env = Environment.getCurrentEnvironment();
    final LocalDispatcher dispatcher = FreeMarkerWorker.getWrappedObject("dispatcher", env);
    final HttpServletRequest request = FreeMarkerWorker.getWrappedObject("request", env);
    final HttpServletResponse response = FreeMarkerWorker.getWrappedObject("response", env);
    final Map<String, Object> templateRoot = MapStack.create(FreeMarkerWorker.createEnvironmentMap(env));
    ((MapStack) templateRoot).push(args);
    final String xmlEscape = (String) templateRoot.get("xmlEscape");
    final String thisContentId = (String) templateRoot.get("contentId");
    return new Writer(out) {

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

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

        @Override
        public void close() throws IOException {
            renderSubContent();
        }

        public void renderSubContent() throws IOException {
            String mimeTypeId = (String) templateRoot.get("mimeTypeId");
            Object localeObject = templateRoot.get("locale");
            Locale locale = null;
            if (localeObject == null) {
                locale = UtilHttp.getLocale(request);
            } else {
                locale = UtilMisc.ensureLocale(localeObject);
            }
            String editRequestName = (String) templateRoot.get("editRequestName");
            if (UtilValidate.isNotEmpty(editRequestName)) {
                String editStyle = getEditStyle();
                openEditWrap(out, editStyle);
            }
            try {
                String txt = null;
                String mapKey = (String) templateRoot.get("mapKey");
                if (UtilValidate.isEmpty(mapKey)) {
                    txt = ContentWorker.renderContentAsText(dispatcher, thisContentId, templateRoot, locale, mimeTypeId, true);
                } else {
                    txt = ContentWorker.renderSubContentAsText(dispatcher, thisContentId, mapKey, templateRoot, locale, mimeTypeId, true);
                }
                if ("true".equals(xmlEscape)) {
                    txt = UtilFormatOut.encodeXmlValue(txt);
                }
                out.write(txt);
            } catch (GeneralException e) {
                String errMsg = "Error rendering thisContentId:" + thisContentId + " msg:" + e.toString();
                Debug.logError(e, errMsg, module);
            // just log a message and don't return anything: throw new IOException();
            }
            if (UtilValidate.isNotEmpty(editRequestName)) {
                closeEditWrap(out, editRequestName);
            }
        }

        public void openEditWrap(Writer out, String editStyle) throws IOException {
            String divStr = "<div class=\"" + editStyle + "\">";
            out.write(divStr);
        }

        public void closeEditWrap(Writer out, String editRequestName) throws IOException {
            String fullRequest = editRequestName;
            String delim = "?";
            if (UtilValidate.isNotEmpty(thisContentId)) {
                fullRequest += delim + "contentId=" + thisContentId;
                delim = "&";
            }
            out.write("<a href=\"");
            ServletContext servletContext = request.getSession().getServletContext();
            RequestHandler rh = (RequestHandler) servletContext.getAttribute("_REQUEST_HANDLER_");
            out.append(rh.makeLink(request, response, "/" + fullRequest, false, false, true));
            out.write("\">Edit</a>");
            out.write("</div>");
        }

        public String getEditStyle() {
            String editStyle = (String) templateRoot.get("editStyle");
            if (UtilValidate.isEmpty(editStyle)) {
                editStyle = UtilProperties.getPropertyValue("content", "defaultEditStyle");
            }
            if (UtilValidate.isEmpty(editStyle)) {
                editStyle = "buttontext";
            }
            return editStyle;
        }
    };
}
Also used : Locale(java.util.Locale) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) GeneralException(org.apache.ofbiz.base.util.GeneralException) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) MapStack(org.apache.ofbiz.base.util.collections.MapStack) RequestHandler(org.apache.ofbiz.webapp.control.RequestHandler) Environment(freemarker.core.Environment) ServletContext(javax.servlet.ServletContext) Writer(java.io.Writer)

Aggregations

Environment (freemarker.core.Environment)2 Writer (java.io.Writer)2 Locale (java.util.Locale)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 GeneralException (org.apache.ofbiz.base.util.GeneralException)2 MapStack (org.apache.ofbiz.base.util.collections.MapStack)2 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)2 ServletContext (javax.servlet.ServletContext)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 RequestHandler (org.apache.ofbiz.webapp.control.RequestHandler)1