1 24 package org.ofbiz.content.webapp.ftl; 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.Locale ; 31 import java.util.Map ; 32 33 import javax.servlet.http.HttpServletRequest ; 34 35 import org.ofbiz.base.util.Debug; 36 import org.ofbiz.base.util.GeneralException; 37 import org.ofbiz.base.util.UtilValidate; 38 import org.ofbiz.base.util.template.FreeMarkerWorker; 39 import org.ofbiz.content.content.ContentWorker; 40 import org.ofbiz.entity.GenericDelegator; 41 import org.ofbiz.entity.GenericValue; 42 43 import freemarker.core.Environment; 44 import freemarker.template.TemplateTransformModel; 45 46 56 public class WrapSubContentCacheTransform implements TemplateTransformModel { 57 58 public static final String module = WrapSubContentCacheTransform.class.getName(); 59 public static final String [] upSaveKeyNames = {"globalNodeTrail"}; 60 public static final String [] saveKeyNames = {"contentId", "subContentId", "subDataResourceTypeId", "mimeTypeId", "whenMap", "locale", "wrapTemplateId", "encloseWrapText", "nullThruDatesOnly"}; 61 62 65 public static Object getWrappedObject(String varName, Environment env) { 66 return FreeMarkerWorker.getWrappedObject(varName, env); 67 } 68 69 public static String getArg(Map args, String key, Environment env) { 70 return FreeMarkerWorker.getArg(args, key, env); 71 } 72 73 public static String getArg(Map args, String key, Map ctx) { 74 return FreeMarkerWorker.getArg(args, key, ctx); 75 } 76 77 public Writer getWriter(final Writer out, Map args) { 78 final StringBuffer buf = new StringBuffer (); 79 final Environment env = Environment.getCurrentEnvironment(); 80 Map envContext = (Map ) FreeMarkerWorker.getWrappedObject("context", env); 81 final Map templateCtx; 82 if (envContext == null) { 83 templateCtx = FreeMarkerWorker.createEnvironmentMap(env); 84 } else { 85 templateCtx = envContext; 86 } 87 final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env); 88 final HttpServletRequest request = (HttpServletRequest ) FreeMarkerWorker.getWrappedObject("request", env); 89 FreeMarkerWorker.getSiteParameters(request, templateCtx); 90 final Map savedValuesUp = new HashMap (); 91 FreeMarkerWorker.saveContextValues(templateCtx, upSaveKeyNames, savedValuesUp); 92 FreeMarkerWorker.overrideWithArgs(templateCtx, args); 94 final String wrapTemplateId = (String )templateCtx.get("wrapTemplateId"); 96 final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env); 98 List trail = (List )templateCtx.get("globalNodeTrail"); 99 String contentAssocPredicateId = (String )templateCtx.get("contentAssocPredicateId"); 101 String strNullThruDatesOnly = (String )templateCtx.get("nullThruDatesOnly"); 102 Boolean nullThruDatesOnly = (strNullThruDatesOnly != null && strNullThruDatesOnly.equalsIgnoreCase("true")) ? new Boolean (true) :new Boolean (false); 103 GenericValue val = null; 104 try { 105 val = ContentWorker.getCurrentContent(delegator, trail, userLogin, templateCtx, nullThruDatesOnly, contentAssocPredicateId); 106 } catch(GeneralException e) { 107 throw new RuntimeException ("Error getting current content. " + e.toString()); 108 } 109 final GenericValue view = val; 111 112 String dataResourceId = null; 113 try { 114 dataResourceId = (String ) view.get("drDataResourceId"); 115 } catch (Exception e) { 116 dataResourceId = (String ) view.get("dataResourceId"); 117 } 118 String subContentIdSub = (String ) view.get("contentId"); 119 String subDataResourceTypeId = (String )templateCtx.get("subDataResourceTypeId"); 121 if (UtilValidate.isEmpty(subDataResourceTypeId)) { 122 try { 123 subDataResourceTypeId = (String ) view.get("drDataResourceTypeId"); 124 } catch (Exception e) { 125 } 127 } 131 final Map savedValues = new HashMap (); 132 FreeMarkerWorker.saveContextValues(templateCtx, saveKeyNames, savedValues); 133 String mimeTypeId = ContentWorker.getMimeTypeId(delegator, view, templateCtx); 136 templateCtx.put("drDataResourceId", dataResourceId); 137 templateCtx.put("mimeTypeId", mimeTypeId); 138 templateCtx.put("dataResourceId", dataResourceId); 139 templateCtx.put("subContentIdSub", subContentIdSub); 140 templateCtx.put("subDataResourceTypeId", subDataResourceTypeId); 141 templateCtx.put("wrapTemplateId", null); 143 return new Writer (out) { 144 145 public void write(char cbuf[], int off, int len) { 146 buf.append(cbuf, off, len); 147 } 148 149 public void flush() throws IOException { 150 out.flush(); 151 } 152 153 public void close() throws IOException { 154 FreeMarkerWorker.reloadValues(templateCtx, savedValues, env); 155 String wrappedContent = buf.toString(); 157 158 if (UtilValidate.isNotEmpty(wrapTemplateId)) { 162 templateCtx.put("wrappedContent", wrappedContent); 163 164 Map templateRoot = null; 166 Map templateRootTemplate = (Map )templateCtx.get("templateRootTemplate"); 167 if (templateRootTemplate == null) { 168 Map templateRootTmp = FreeMarkerWorker.createEnvironmentMap(env); 169 templateRoot = new HashMap (templateRootTmp); 170 templateCtx.put("templateRootTemplate", templateRootTmp); 171 } else { 172 templateRoot = new HashMap (templateRootTemplate); 173 } 174 175 templateRoot.put("context", templateCtx); 176 177 String mimeTypeId = (String )templateCtx.get("mimeTypeId"); 178 Locale locale = null; 179 try { 180 ContentWorker.renderContentAsTextCache(delegator, wrapTemplateId, out, templateRoot, null, locale, mimeTypeId); 182 } catch (IOException e) { 184 Debug.logError(e, "Error rendering content" + e.getMessage(), module); 185 throw new IOException ("Error rendering content" + e.toString()); 186 } catch (GeneralException e2) { 187 Debug.logError(e2, "Error rendering content" + e2.getMessage(), module); 188 throw new IOException ("Error rendering content" + e2.toString()); 189 } 190 191 FreeMarkerWorker.reloadValues(templateCtx, savedValuesUp, env); 192 } 194 } 195 }; 196 } 197 } 198 | Popular Tags |