Search in sources :

Example 1 with HtmlMenuWrapper

use of org.apache.ofbiz.widget.renderer.html.HtmlMenuWrapper in project ofbiz-framework by apache.

the class MenuWrapTransform method getWriter.

@SuppressWarnings("rawtypes")
public Writer getWriter(final Writer out, Map args) {
    final Environment env = Environment.getCurrentEnvironment();
    final Delegator delegator = FreeMarkerWorker.getWrappedObject("delegator", env);
    final HttpServletRequest request = FreeMarkerWorker.getWrappedObject("request", env);
    final HttpServletResponse response = FreeMarkerWorker.getWrappedObject("response", env);
    final HttpSession session = FreeMarkerWorker.getWrappedObject("session", env);
    final GenericValue userLogin = FreeMarkerWorker.getWrappedObject("userLogin", env);
    final Map<String, Object> templateCtx = FreeMarkerWorker.getWrappedObject("context", env);
    FreeMarkerWorker.getSiteParameters(request, templateCtx);
    final Map<String, Object> savedValuesUp = new HashMap<>();
    FreeMarkerWorker.saveContextValues(templateCtx, upSaveKeyNames, savedValuesUp);
    Map<String, Object> checkedArgs = UtilGenerics.checkMap(args);
    FreeMarkerWorker.overrideWithArgs(templateCtx, checkedArgs);
    List<Map<String, ? extends Object>> trail = UtilGenerics.checkList(templateCtx.get("globalNodeTrail"));
    String contentAssocPredicateId = (String) templateCtx.get("contentAssocPredicateId");
    String strNullThruDatesOnly = (String) templateCtx.get("nullThruDatesOnly");
    Boolean nullThruDatesOnly = (strNullThruDatesOnly != null && "true".equalsIgnoreCase(strNullThruDatesOnly)) ? Boolean.TRUE : Boolean.FALSE;
    GenericValue val = null;
    try {
        if (WidgetContentWorker.getContentWorker() != null) {
            val = WidgetContentWorker.getContentWorker().getCurrentContentExt(delegator, trail, userLogin, templateCtx, nullThruDatesOnly, contentAssocPredicateId);
        } else {
            Debug.logError("Not rendering content, not ContentWorker found.", module);
        }
    } catch (GeneralException e) {
        throw new RuntimeException("Error getting current content. " + e.toString());
    }
    final GenericValue view = val;
    String dataResourceId = null;
    try {
        dataResourceId = (String) view.get("drDataResourceId");
    } catch (IllegalArgumentException e) {
        dataResourceId = (String) view.get("dataResourceId");
    }
    String subContentIdSub = (String) view.get("contentId");
    // This order is taken so that the dataResourceType can be overridden in the transform arguments.
    String subDataResourceTypeId = (String) templateCtx.get("subDataResourceTypeId");
    if (UtilValidate.isEmpty(subDataResourceTypeId)) {
        try {
            subDataResourceTypeId = (String) view.get("drDataResourceTypeId");
        } catch (IllegalArgumentException e) {
        // view may be "Content"
        }
    // TODO: If this value is still empty then it is probably necessary to get a value from
    // the parent context. But it will already have one and it is the same context that is
    // being passed.
    }
    // This order is taken so that the mimeType can be overridden in the transform arguments.
    String mimeTypeId = null;
    if (WidgetContentWorker.getContentWorker() != null) {
        mimeTypeId = WidgetContentWorker.getContentWorker().getMimeTypeIdExt(delegator, view, templateCtx);
    } else {
        Debug.logError("Not rendering content, not ContentWorker found.", module);
    }
    templateCtx.put("drDataResourceId", dataResourceId);
    templateCtx.put("mimeTypeId", mimeTypeId);
    templateCtx.put("dataResourceId", dataResourceId);
    templateCtx.put("subContentIdSub", subContentIdSub);
    templateCtx.put("subDataResourceTypeId", subDataResourceTypeId);
    final Map<String, Object> savedValues = new HashMap<>();
    FreeMarkerWorker.saveContextValues(templateCtx, saveKeyNames, savedValues);
    final StringBuilder buf = new StringBuilder();
    return new LoopWriter(out) {

        @Override
        public int onStart() throws TemplateModelException, IOException {
            String renderOnStart = (String) templateCtx.get("renderOnStart");
            if (renderOnStart != null && "true".equalsIgnoreCase(renderOnStart)) {
                renderMenu();
            }
            return TransformControl.EVALUATE_BODY;
        }

        @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 {
            FreeMarkerWorker.reloadValues(templateCtx, savedValues, env);
            String wrappedContent = buf.toString();
            out.write(wrappedContent);
            String renderOnClose = (String) templateCtx.get("renderOnClose");
            if (renderOnClose == null || !"false".equalsIgnoreCase(renderOnClose)) {
                renderMenu();
            }
            FreeMarkerWorker.reloadValues(templateCtx, savedValuesUp, env);
        }

        public void renderMenu() throws IOException {
            String menuDefFile = (String) templateCtx.get("menuDefFile");
            String menuName = (String) templateCtx.get("menuName");
            String menuWrapperClassName = (String) templateCtx.get("menuWrapperClassName");
            HtmlMenuWrapper menuWrapper = HtmlMenuWrapper.getMenuWrapper(request, response, session, menuDefFile, menuName, menuWrapperClassName);
            if (menuWrapper == null) {
                throw new IOException("HtmlMenuWrapper with def file:" + menuDefFile + " menuName:" + menuName + " and HtmlMenuWrapper class:" + menuWrapperClassName + " could not be instantiated.");
            }
            String associatedContentId = (String) templateCtx.get("associatedContentId");
            menuWrapper.putInContext("defaultAssociatedContentId", associatedContentId);
            menuWrapper.putInContext("currentValue", view);
            String menuStr = menuWrapper.renderMenuString();
            out.write(menuStr);
        }
    };
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GeneralException(org.apache.ofbiz.base.util.GeneralException) HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) Delegator(org.apache.ofbiz.entity.Delegator) LoopWriter(org.apache.ofbiz.webapp.ftl.LoopWriter) Environment(freemarker.core.Environment) HashMap(java.util.HashMap) Map(java.util.Map) HtmlMenuWrapper(org.apache.ofbiz.widget.renderer.html.HtmlMenuWrapper)

Aggregations

Environment (freemarker.core.Environment)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1 GeneralException (org.apache.ofbiz.base.util.GeneralException)1 Delegator (org.apache.ofbiz.entity.Delegator)1 GenericValue (org.apache.ofbiz.entity.GenericValue)1 LoopWriter (org.apache.ofbiz.webapp.ftl.LoopWriter)1 HtmlMenuWrapper (org.apache.ofbiz.widget.renderer.html.HtmlMenuWrapper)1