1 24 package org.ofbiz.widget.menu; 25 26 import java.io.IOException ; 27 import java.io.Writer ; 28 import java.util.HashMap ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 34 import javax.servlet.http.HttpSession ; 35 36 import org.ofbiz.base.util.Debug; 37 import org.ofbiz.base.util.GeneralException; 38 import org.ofbiz.base.util.UtilValidate; 39 import org.ofbiz.base.util.template.FreeMarkerWorker; 40 import org.ofbiz.entity.GenericDelegator; 41 import org.ofbiz.entity.GenericValue; 42 import org.ofbiz.webapp.ftl.LoopWriter; 43 import org.ofbiz.widget.WidgetContentWorker; 44 import org.ofbiz.widget.html.HtmlMenuWrapper; 45 46 import freemarker.core.Environment; 47 import freemarker.template.TemplateModelException; 48 import freemarker.template.TemplateTransformModel; 49 import freemarker.template.TransformControl; 50 51 61 public class MenuWrapTransform implements TemplateTransformModel { 62 63 public static final String module = MenuWrapTransform.class.getName(); 64 public static final String [] upSaveKeyNames = {"globalNodeTrail"}; 65 public static final String [] saveKeyNames = {"contentId", "subContentId", "subDataResourceTypeId", "mimeTypeId", "whenMap", "locale", "wrapTemplateId", "encloseWrapText", "nullThruDatesOnly", "renderOnStart", "renderOnClose", "menuDefFile", "menuName", "associatedContentId", "wrapperClassName"}; 66 67 70 public static Object getWrappedObject(String varName, Environment env) { 71 return FreeMarkerWorker.getWrappedObject(varName, env); 72 } 73 74 public static String getArg(Map args, String key, Environment env) { 75 return FreeMarkerWorker.getArg(args, key, env); 76 } 77 78 public static String getArg(Map args, String key, Map ctx) { 79 return FreeMarkerWorker.getArg(args, key, ctx); 80 } 81 82 public Writer getWriter(final Writer out, Map args) { 83 final StringBuffer buf = new StringBuffer (); 84 final Environment env = Environment.getCurrentEnvironment(); 85 final Map templateCtx = (Map ) FreeMarkerWorker.getWrappedObject("context", env); 86 final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env); 87 final HttpServletRequest request = (HttpServletRequest ) FreeMarkerWorker.getWrappedObject("request", env); 88 final HttpServletResponse response = (HttpServletResponse ) FreeMarkerWorker.getWrappedObject("response", env); 89 final HttpSession session = (HttpSession ) FreeMarkerWorker.getWrappedObject("session", env); 90 FreeMarkerWorker.getSiteParameters(request, templateCtx); 91 final Map savedValuesUp = new HashMap (); 92 FreeMarkerWorker.saveContextValues(templateCtx, upSaveKeyNames, savedValuesUp); 93 FreeMarkerWorker.overrideWithArgs(templateCtx, args); 94 final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env); 98 List trail = (List )templateCtx.get("globalNodeTrail"); 99 String contentAssocPredicateId = (String )templateCtx.get("contentAssocPredicateId"); 100 String strNullThruDatesOnly = (String )templateCtx.get("nullThruDatesOnly"); 101 Boolean nullThruDatesOnly = (strNullThruDatesOnly != null && strNullThruDatesOnly.equalsIgnoreCase("true")) ? new Boolean (true) :new Boolean (false); 102 GenericValue val = null; 103 try { 104 if (WidgetContentWorker.contentWorker != null) { 105 val = WidgetContentWorker.contentWorker.getCurrentContentExt(delegator, trail, userLogin, templateCtx, nullThruDatesOnly, contentAssocPredicateId); 106 } else { 107 Debug.logError("Not rendering content, not ContentWorker found.", module); 108 } 109 } catch(GeneralException e) { 110 throw new RuntimeException ("Error getting current content. " + e.toString()); 111 } 112 final GenericValue view = val; 113 114 String dataResourceId = null; 115 try { 116 dataResourceId = (String ) view.get("drDataResourceId"); 117 } catch (Exception e) { 118 dataResourceId = (String ) view.get("dataResourceId"); 119 } 120 String subContentIdSub = (String ) view.get("contentId"); 121 String subDataResourceTypeId = (String )templateCtx.get("subDataResourceTypeId"); 123 if (UtilValidate.isEmpty(subDataResourceTypeId)) { 124 try { 125 subDataResourceTypeId = (String ) view.get("drDataResourceTypeId"); 126 } catch (Exception e) { 127 } 129 } 133 String mimeTypeId = null; 135 if (WidgetContentWorker.contentWorker != null) { 136 mimeTypeId = WidgetContentWorker.contentWorker.getMimeTypeIdExt(delegator, view, templateCtx); 137 } else { 138 Debug.logError("Not rendering content, not ContentWorker found.", module); 139 } 140 templateCtx.put("drDataResourceId", dataResourceId); 141 templateCtx.put("mimeTypeId", mimeTypeId); 142 templateCtx.put("dataResourceId", dataResourceId); 143 templateCtx.put("subContentIdSub", subContentIdSub); 144 templateCtx.put("subDataResourceTypeId", subDataResourceTypeId); 145 final Map savedValues = new HashMap (); 146 FreeMarkerWorker.saveContextValues(templateCtx, saveKeyNames, savedValues); 147 148 return new LoopWriter(out) { 149 150 public int onStart() throws TemplateModelException, IOException { 151 String renderOnStart = (String )templateCtx.get("renderOnStart"); 152 if (renderOnStart != null && renderOnStart.equalsIgnoreCase("true")) { 153 renderMenu(); 154 } 155 return TransformControl.EVALUATE_BODY; 156 } 157 158 public void write(char cbuf[], int off, int len) { 159 buf.append(cbuf, off, len); 160 } 161 162 public void flush() throws IOException { 163 out.flush(); 164 } 165 166 public void close() throws IOException { 167 FreeMarkerWorker.reloadValues(templateCtx, savedValues, env); 168 String wrappedContent = buf.toString(); 169 out.write(wrappedContent); 170 String renderOnClose = (String )templateCtx.get("renderOnClose"); 171 if (renderOnClose == null || !renderOnClose.equalsIgnoreCase("false")) { 172 renderMenu(); 173 } 174 FreeMarkerWorker.reloadValues(templateCtx, savedValuesUp, env); 175 } 176 177 public void renderMenu() throws IOException { 178 179 ModelMenu modelMenu = null; 180 String menuDefFile = (String )templateCtx.get("menuDefFile"); 181 String menuName = (String )templateCtx.get("menuName"); 182 String menuWrapperClassName = (String )templateCtx.get("menuWrapperClassName"); 183 HtmlMenuWrapper menuWrapper = HtmlMenuWrapper.getMenuWrapper(request, response, session, menuDefFile, menuName, menuWrapperClassName); 184 String associatedContentId = (String )templateCtx.get("associatedContentId"); 185 menuWrapper.putInContext("defaultAssociatedContentId", associatedContentId); 186 menuWrapper.putInContext("currentValue", view); 187 188 if (menuWrapper == null) { 189 throw new IOException ("HtmlMenuWrapper with def file:" + menuDefFile + " menuName:" + menuName + " and HtmlMenuWrapper class:" + menuWrapperClassName + " could not be instantiated."); 190 } 191 String menuStr = menuWrapper.renderMenuString(); 192 out.write(menuStr); 193 return; 194 } 195 196 }; 197 } 198 } 199 | Popular Tags |