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 import org.ofbiz.widget.WidgetWorker; 41 42 import freemarker.core.Environment; 43 import freemarker.template.TemplateTransformModel; 44 54 public class RenderContentAsText implements TemplateTransformModel { 55 56 public static final String module = RenderContentAsText.class.getName(); 57 public static final String [] upSaveKeyNames = {"globalNodeTrail"}; 58 public static final String [] saveKeyNames = {"contentId", "subContentId", "subDataResourceTypeId", "mimeTypeId", "whenMap", "locale", "wrapTemplateId", "encloseWrapText", "nullThruDatesOnly", "globalNodeTrail"}; 59 60 public Writer getWriter(final Writer out, Map args) { 61 final StringBuffer buf = new StringBuffer (); 62 final Environment env = Environment.getCurrentEnvironment(); 63 final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env); 66 final HttpServletRequest request = (HttpServletRequest ) FreeMarkerWorker.getWrappedObject("request", env); 67 final HttpServletResponse response = (HttpServletResponse ) FreeMarkerWorker.getWrappedObject("response", env); 68 final Map templateRoot = FreeMarkerWorker.createEnvironmentMap(env); 69 if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, contentId(0):" + templateRoot.get( "contentId"), module); 70 FreeMarkerWorker.getSiteParameters(request, templateRoot); 71 final Map savedValuesUp = new HashMap (); 72 FreeMarkerWorker.saveContextValues(templateRoot, upSaveKeyNames, savedValuesUp); 73 FreeMarkerWorker.overrideWithArgs(templateRoot, args); 74 if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, contentId(2):" + templateRoot.get( "contentId"), module); 75 final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env); 76 List trail = (List )templateRoot.get( "globalNodeTrail"); 77 String contentAssocPredicateId = (String )templateRoot.get( "contentAssocPredicateId"); 79 String strNullThruDatesOnly = (String )templateRoot.get( "nullThruDatesOnly"); 80 Boolean nullThruDatesOnly = (strNullThruDatesOnly != null && strNullThruDatesOnly.equalsIgnoreCase("true")) ? new Boolean (true) :new Boolean (false); 81 final String thisContentId = (String )templateRoot.get("contentId"); 82 final String xmlEscape = (String )templateRoot.get("xmlEscape"); 83 final boolean directAssocMode = UtilValidate.isNotEmpty(thisContentId) ? true : false; 84 if (Debug.infoOn()) Debug.logInfo("in Render(0), directAssocMode ." + directAssocMode , module); 85 125 126 final Map savedValues = new HashMap (); 127 128 return new Writer (out) { 129 130 public void write(char cbuf[], int off, int len) { 131 } 132 133 public void flush() throws IOException { 134 out.flush(); 135 } 136 137 public void close() throws IOException { 138 List globalNodeTrail = (List )templateRoot.get( "globalNodeTrail"); 139 if (Debug.infoOn()) Debug.logInfo("Render close, globalNodeTrail(2a):" + ContentWorker.nodeTrailToCsv(globalNodeTrail), ""); 140 try { 141 renderSubContent(); 142 } catch (IOException e) { 144 throw new IOException (e.getMessage()); 145 } 146 } 147 148 public void renderSubContent() throws IOException { 149 150 Timestamp fromDate = UtilDateTime.nowTimestamp(); 152 List passedGlobalNodeTrail = (List )templateRoot.get( "globalNodeTrail"); 153 String editRequestName = (String )templateRoot.get( "editRequestName"); 154 if (Debug.infoOn()) Debug.logInfo("in Render(3), editRequestName ." + editRequestName , module); 155 168 169 170 171 String mimeTypeId = (String ) templateRoot.get( "mimeTypeId"); 172 Locale locale = (Locale ) templateRoot.get( "locale"); 173 if (locale == null) 174 locale = Locale.getDefault(); 175 176 if (UtilValidate.isNotEmpty(editRequestName)) { 177 String editStyle = getEditStyle(); 178 openEditWrap(out, editStyle); 179 } 180 181 if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, contentId(2):" + templateRoot.get( "contentId"), module); 182 if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, subContentId(2):" + templateRoot.get( "subContentId"), module); 183 FreeMarkerWorker.saveContextValues(templateRoot, saveKeyNames, savedValues); 184 try { 186 String txt = ContentWorker.renderContentAsTextCache(delegator, thisContentId, templateRoot, null, locale, mimeTypeId); 187 if ("true".equals(xmlEscape)) 188 txt = UtilFormatOut.encodeXmlValue(txt); 189 190 out.write(txt); 191 192 if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, after renderContentAsTextCache:", module); 193 } catch (GeneralException e) { 194 Debug.logError(e, "Error rendering content", module); 195 throw new IOException ("Error rendering thisContentId:" + thisContentId + " msg:" + e.toString()); 196 } 197 FreeMarkerWorker.reloadValues(templateRoot, savedValuesUp, env); 199 FreeMarkerWorker.reloadValues(templateRoot, savedValues, env); 200 if (UtilValidate.isNotEmpty(editRequestName)) { 201 closeEditWrap(out, editRequestName); 202 } 203 204 return; 206 } 207 208 public void openEditWrap(Writer out, String editStyle) throws IOException { 209 210 String divStr = "<div class=\"" + editStyle + "\">"; 211 out.write(divStr); 212 } 213 214 public void closeEditWrap(Writer out, String editRequestName) throws IOException { 215 216 if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, contentId(3):" + templateRoot.get( "contentId"), module); 217 if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, subContentId(3):" + templateRoot.get( "subContentId"), module); 218 StringBuffer sb = new StringBuffer (); 219 String fullRequest = editRequestName; 220 String contentId = null; 221 contentId = (String )templateRoot.get("subContentId"); 222 String delim = "?"; 223 if (UtilValidate.isNotEmpty(contentId)) { 224 fullRequest += delim + "contentId=" + contentId; 225 delim = "&"; 226 } 227 228 WidgetWorker.appendOfbizUrl(sb, fullRequest, request, response); 229 String url = sb.toString(); 230 String link = "<a HREF=\"" + url + "\">Edit</a>"; 231 out.write(link); 232 String divStr = "</div>"; 233 out.write(divStr); 234 } 235 236 public String getEditStyle() { 237 238 String editStyle = (String )templateRoot.get("editStyle"); 239 if (UtilValidate.isEmpty(editStyle)) { 240 editStyle = UtilProperties.getPropertyValue("content", "defaultEditStyle"); 241 } 242 if (UtilValidate.isEmpty(editStyle)) { 243 editStyle = "buttontext"; 244 } 245 return editStyle; 246 } 247 }; 248 249 } 250 251 } 252 | Popular Tags |