1 17 package org.ofbiz.content.webapp.ftl; 18 19 import java.io.IOException ; 20 import java.io.Writer ; 21 import java.sql.Timestamp ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 import java.util.Locale ; 25 import java.util.Map ; 26 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 import org.ofbiz.base.util.Debug; 31 import org.ofbiz.base.util.GeneralException; 32 import org.ofbiz.base.util.UtilDateTime; 33 import org.ofbiz.base.util.UtilFormatOut; 34 import org.ofbiz.base.util.UtilProperties; 35 import org.ofbiz.base.util.UtilValidate; 36 import org.ofbiz.base.util.template.FreeMarkerWorker; 37 import org.ofbiz.content.content.ContentWorker; 38 import org.ofbiz.entity.GenericDelegator; 39 import org.ofbiz.entity.GenericValue; 40 41 import freemarker.core.Environment; 42 import freemarker.template.TemplateTransformModel; 43 53 public class RenderSubContentAsText implements TemplateTransformModel { 54 55 public static final String module = RenderSubContentAsText.class.getName(); 56 public static final String [] upSaveKeyNames = {"globalNodeTrail"}; 57 public static final String [] saveKeyNames = {"contentId", "subContentId", "subDataResourceTypeId", "mimeTypeId", "whenMap", "locale", "wrapTemplateId", "encloseWrapText", "nullThruDatesOnly", "globalNodeTrail"}; 58 59 public Writer getWriter(final Writer out, Map args) { 60 final StringBuffer buf = new StringBuffer (); 61 final Environment env = Environment.getCurrentEnvironment(); 62 final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env); 65 final HttpServletRequest request = (HttpServletRequest ) FreeMarkerWorker.getWrappedObject("request", env); 66 final HttpServletResponse response = (HttpServletResponse ) FreeMarkerWorker.getWrappedObject("response", env); 67 final Map templateRoot = FreeMarkerWorker.createEnvironmentMap(env); 68 if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, contentId(0):" + templateRoot.get( "contentId"), module); 69 FreeMarkerWorker.getSiteParameters(request, templateRoot); 70 final Map savedValuesUp = new HashMap (); 71 FreeMarkerWorker.saveContextValues(templateRoot, upSaveKeyNames, savedValuesUp); 72 FreeMarkerWorker.overrideWithArgs(templateRoot, args); 73 if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, contentId(2):" + templateRoot.get( "contentId"), module); 74 final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env); 75 List trail = (List )templateRoot.get( "globalNodeTrail"); 76 String contentAssocPredicateId = (String )templateRoot.get( "contentAssocPredicateId"); 78 String strNullThruDatesOnly = (String )templateRoot.get( "nullThruDatesOnly"); 79 Boolean nullThruDatesOnly = (strNullThruDatesOnly != null && strNullThruDatesOnly.equalsIgnoreCase("true")) ? new Boolean (true) :new Boolean (false); 80 final String thisContentId = (String )templateRoot.get("contentId"); 81 final String thisMapKey = (String )templateRoot.get("mapKey"); 82 final String xmlEscape = (String )templateRoot.get("xmlEscape"); 83 if (Debug.infoOn()) Debug.logInfo("in Render(0), thisSubContentId ." + thisContentId , module); 84 final boolean directAssocMode = UtilValidate.isNotEmpty(thisContentId) ? true : false; 85 if (Debug.infoOn()) Debug.logInfo("in Render(0), directAssocMode ." + directAssocMode , module); 86 124 125 final Map savedValues = new HashMap (); 126 127 return new Writer (out) { 128 129 public void write(char cbuf[], int off, int len) { 130 } 131 132 public void flush() throws IOException { 133 out.flush(); 134 } 135 136 public void close() throws IOException { 137 List globalNodeTrail = (List )templateRoot.get( "globalNodeTrail"); 138 if (Debug.infoOn()) Debug.logInfo("Render close, globalNodeTrail(2a):" + ContentWorker.nodeTrailToCsv(globalNodeTrail), ""); 139 try { 140 renderSubContent(); 141 } catch (IOException e) { 143 throw new IOException (e.getMessage()); 144 } 145 } 146 147 public void renderSubContent() throws IOException { 148 149 Timestamp fromDate = UtilDateTime.nowTimestamp(); 151 List passedGlobalNodeTrail = (List )templateRoot.get( "globalNodeTrail"); 152 String editRequestName = (String )templateRoot.get( "editRequestName"); 153 if (Debug.infoOn()) Debug.logInfo("in Render(3), editRequestName ." + editRequestName , module); 154 155 String mimeTypeId = (String ) templateRoot.get( "mimeTypeId"); 156 Locale locale = (Locale ) templateRoot.get( "locale"); 157 if (locale == null) 158 locale = Locale.getDefault(); 159 160 if (UtilValidate.isNotEmpty(editRequestName)) { 161 String editStyle = getEditStyle(); 162 openEditWrap(out, editStyle); 163 } 164 165 FreeMarkerWorker.saveContextValues(templateRoot, saveKeyNames, savedValues); 166 try { 167 String txt = ContentWorker.renderSubContentAsTextCache(delegator, thisContentId, thisMapKey, null, templateRoot, locale, mimeTypeId, null, fromDate); 168 if ("true".equals(xmlEscape)) 169 txt = UtilFormatOut.encodeXmlValue(txt); 170 171 out.write(txt); 172 173 if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, after renderContentAsTextCache:", module); 174 } catch (GeneralException e) { 175 Debug.logError(e, "Error rendering content", module); 176 throw new IOException ("Error rendering thisContentId:" + thisContentId + " msg:" + e.toString()); 177 } 178 FreeMarkerWorker.reloadValues(templateRoot, savedValues, env); 179 FreeMarkerWorker.reloadValues(templateRoot, savedValuesUp, env); 180 if (UtilValidate.isNotEmpty(editRequestName)) { 181 closeEditWrap(out, editRequestName); 182 } 183 184 return; 186 } 187 188 public void openEditWrap(Writer out, String editStyle) throws IOException { 189 190 String divStr = "<div class=\"" + editStyle + "\">"; 191 out.write(divStr); 192 } 193 194 public void closeEditWrap(Writer out, String editRequestName) throws IOException { 195 265 } 266 267 public String getEditStyle() { 268 269 String editStyle = (String )templateRoot.get("editStyle"); 270 if (UtilValidate.isEmpty(editStyle)) { 271 editStyle = UtilProperties.getPropertyValue("content", "defaultEditStyle"); 272 } 273 if (UtilValidate.isEmpty(editStyle)) { 274 editStyle = "buttontext"; 275 } 276 return editStyle; 277 } 278 }; 279 280 } 281 282 } 283 | Popular Tags |