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