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.ServletContext ; 28 import javax.servlet.http.HttpServletRequest ; 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.UtilMisc; 34 import org.ofbiz.base.util.template.FreeMarkerWorker; 35 import org.ofbiz.content.content.ContentWorker; 36 import org.ofbiz.entity.GenericDelegator; 37 import org.ofbiz.entity.GenericValue; 38 39 import freemarker.core.Environment; 40 import freemarker.template.TemplateTransformModel; 41 42 51 public class RenderSubContentTransform implements TemplateTransformModel { 52 53 public static final String module = RenderSubContentTransform.class.getName(); 54 55 62 public static String getArg(Map args, String key, Environment env) { 63 return FreeMarkerWorker.getArg(args, key, env); 64 } 65 66 public static String getArg(Map args, String key, Map ctx) { 67 return FreeMarkerWorker.getArg(args, key, ctx); 68 } 69 70 public Writer getWriter(final Writer out, Map args) { 71 final Environment env = Environment.getCurrentEnvironment(); 73 Map ctx = (Map ) FreeMarkerWorker.getWrappedObject("context", env); 74 if (ctx == null) { 75 ctx = new HashMap (); 76 } 77 final String mapKey = getArg(args, "mapKey", ctx); 78 final String subContentId = getArg(args, "subContentId", ctx); 79 final String subDataResourceTypeId = getArg(args, "subDataResourceTypeId", ctx); 80 final String contentId = getArg(args, "contentId", ctx); 81 final String mimeTypeId = getArg(args, "mimeTypeId", ctx); 82 final Locale locale = (Locale ) FreeMarkerWorker.getWrappedObject("locale", env); 83 final HttpServletRequest request = (HttpServletRequest ) FreeMarkerWorker.getWrappedObject("request", env); 84 final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env); 85 final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env); 86 GenericValue subContentDataResourceViewTemp = (GenericValue) FreeMarkerWorker.getWrappedObject("subContentDataResourceView", env); 87 if (subContentDataResourceViewTemp == null) { 88 List assocTypes = UtilMisc.toList("SUB_CONTENT"); 89 Timestamp fromDate = UtilDateTime.nowTimestamp(); 90 try { 91 subContentDataResourceViewTemp = ContentWorker.getSubContent(delegator, contentId, mapKey, subContentId, userLogin, assocTypes, fromDate); 92 } catch (IOException e) { 93 throw new RuntimeException (e.getMessage()); 94 } 95 } 96 97 final GenericValue subContentDataResourceView = subContentDataResourceViewTemp; 98 99 100 final Map templateContext = ctx; 101 102 return new Writer (out) { 103 104 public void write(char cbuf[], int off, int len) { 105 } 106 107 public void flush() throws IOException { 108 out.flush(); 109 } 110 111 public void close() throws IOException { 112 try { 113 renderSubContent(); 114 } catch (IOException e) { 115 throw new IOException (e.getMessage()); 116 } 117 } 118 119 public void renderSubContent() throws IOException { 120 Timestamp fromDate = UtilDateTime.nowTimestamp(); 122 ServletContext servletContext = request.getSession().getServletContext(); 123 String rootDir = servletContext.getRealPath("/"); 124 String webSiteId = (String ) servletContext.getAttribute("webSiteId"); 125 String https = (String ) servletContext.getAttribute("https"); 126 templateContext.put("webSiteId", webSiteId); 127 templateContext.put("https", https); 128 templateContext.put("rootDir", rootDir); 129 130 Map templateRoot = FreeMarkerWorker.createEnvironmentMap(env); 131 132 templateRoot.put("context", templateContext); 133 if (subContentDataResourceView != null) { 134 } 135 try { 136 Map results = ContentWorker.renderSubContentAsText(delegator, contentId, out, mapKey, subContentId, subContentDataResourceView, templateRoot, locale, mimeTypeId, userLogin, fromDate); 137 } catch (GeneralException e) { 138 Debug.logError(e, "Error rendering content", module); 139 throw new IOException ("Error rendering content" + e.toString()); 140 } 141 142 Map resultCtx = (Map ) FreeMarkerWorker.getWrappedObject("context", env); 143 templateContext.put("mapKey", null); 144 templateContext.put("subContentId", null); 145 templateContext.put("subDataResourceTypeId", null); 146 templateContext.put("contentId", contentId); 147 templateContext.put("mimeTypeId", null); 148 templateContext.put("locale", locale); 149 150 return; 151 } 152 }; 153 } 154 155 } 156 | Popular Tags |