1 24 package org.ofbiz.content.webapp.ftl; 25 26 import java.io.IOException ; 27 import java.io.Writer ; 28 import java.sql.Timestamp ; 29 import java.util.List ; 30 import java.util.Locale ; 31 import java.util.Map ; 32 33 import org.ofbiz.base.util.Debug; 34 import org.ofbiz.base.util.GeneralException; 35 import org.ofbiz.base.util.UtilDateTime; 36 import org.ofbiz.base.util.UtilMisc; 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.GenericEntityException; 42 import org.ofbiz.entity.GenericValue; 43 44 import freemarker.core.Environment; 45 import freemarker.template.TemplateTransformModel; 46 47 56 public class EditRenderSubContentTransform implements TemplateTransformModel { 57 58 public static final String module = EditRenderSubContentTransform.class.getName(); 59 60 63 public static Object getWrappedObject(String varName, Environment env) { 64 return FreeMarkerWorker.getWrappedObject(varName, env); 65 } 66 67 public static String getArg(Map args, String key, Environment env) { 68 return FreeMarkerWorker.getArg(args, key, env); 69 } 70 71 public static String getArg(Map args, String key, Map ctx) { 72 return FreeMarkerWorker.getArg(args, key, ctx); 73 } 74 75 public Writer getWriter(final Writer out, Map args) { 76 final StringBuffer buf = new StringBuffer (); 77 final Environment env = Environment.getCurrentEnvironment(); 78 Map ctx = (Map ) FreeMarkerWorker.getWrappedObject("context", env); 79 final String editTemplate = getArg(args, "editTemplate", ctx); 80 final String wrapTemplateId = getArg(args, "wrapTemplateId", ctx); 81 final String mapKey = getArg(args, "mapKey", ctx); 82 final String templateContentId = getArg(args, "templateContentId", ctx); 83 final String subContentId = getArg(args, "subContentId", ctx); 84 String subDataResourceTypeIdTemp = getArg(args, "subDataResourceTypeId", ctx); 85 final String contentId = getArg(args, "contentId", ctx); 86 87 88 final Locale locale = (Locale ) FreeMarkerWorker.getWrappedObject("locale", env); 89 String mimeTypeIdTemp = getArg(args, "mimeTypeId", ctx); 90 final String rootDir = getArg(args, "rootDir", ctx); 91 final String webSiteId = getArg(args, "webSiteId", ctx); 92 final String https = getArg(args, "https", ctx); 93 final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env); 95 final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env); 96 GenericValue subContentDataResourceViewTemp = (GenericValue) FreeMarkerWorker.getWrappedObject("subContentDataResourceView", env); 97 99 ctx.put("mapKey", mapKey); 100 ctx.put("subDataResourceTypeIdTemp", subDataResourceTypeIdTemp); 101 ctx.put("contentId", contentId); 102 ctx.put("templateContentId", templateContentId); 103 ctx.put("locale", locale); 104 105 GenericValue parentContent = null; 111 List assocTypes = UtilMisc.toList("SUB_CONTENT"); 113 Timestamp fromDate = UtilDateTime.nowTimestamp(); 114 if (subContentDataResourceViewTemp == null) { 115 try { 116 subContentDataResourceViewTemp = ContentWorker.getSubContent(delegator, contentId, mapKey, subContentId, userLogin, assocTypes, fromDate); 117 } catch (IOException e) { 118 Debug.logError(e, "Error getting sub-content", module); 119 throw new RuntimeException (e.getMessage()); 120 } 121 } 122 123 final GenericValue subContentDataResourceView = subContentDataResourceViewTemp; 124 125 String dataResourceIdTemp = null; 126 String subContentIdSubTemp = null; 127 if (subContentDataResourceView != null && subContentDataResourceView.get("contentId") != null) { 128 129 130 dataResourceIdTemp = (String ) subContentDataResourceView.get("drDataResourceId"); 131 subContentIdSubTemp = (String ) subContentDataResourceView.get("contentId"); 132 if (UtilValidate.isEmpty(subDataResourceTypeIdTemp)) { 133 subDataResourceTypeIdTemp = (String ) subContentDataResourceView.get("drDataResourceTypeId"); 134 } 135 if (UtilValidate.isEmpty(mimeTypeIdTemp)) { 136 mimeTypeIdTemp = (String ) subContentDataResourceView.get("mimeTypeId"); 137 if (UtilValidate.isEmpty(mimeTypeIdTemp) && UtilValidate.isNotEmpty(contentId)) { try { 139 parentContent = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", contentId)); 140 if (parentContent != null) { 141 mimeTypeIdTemp = (String ) parentContent.get("mimeTypeId"); 142 } 143 } catch (GenericEntityException e) { 144 throw new RuntimeException (e.getMessage()); 145 } 146 } 147 148 } 149 ctx.put("subContentId", subContentIdSubTemp); 150 ctx.put("drDataResourceId", dataResourceIdTemp); 151 ctx.put("subContentDataResourceView", subContentDataResourceView); 152 ctx.put("mimeTypeId", mimeTypeIdTemp); 153 } else { 155 ctx.put("subContentId", null); 156 ctx.put("drDataResourceId", null); 157 ctx.put("subContentDataResourceView", null); 158 ctx.put("mimeTypeId", null); 159 } 161 162 final String dataResourceId = dataResourceIdTemp; 163 final String subContentIdSub = subContentIdSubTemp; 164 final Map templateContext = ctx; 167 final String mimeTypeId = mimeTypeIdTemp; 168 final String subDataResourceTypeId = subDataResourceTypeIdTemp; 169 170 return new Writer (out) { 171 172 public void write(char cbuf[], int off, int len) { 173 buf.append(cbuf, off, len); 174 } 175 176 public void flush() throws IOException { 177 out.flush(); 178 } 179 180 public void close() throws IOException { 181 String wrappedFTL = buf.toString(); 182 if (editTemplate != null && editTemplate.equalsIgnoreCase("true")) { 183 if (UtilValidate.isNotEmpty(wrapTemplateId)) { 184 templateContext.put("wrappedFTL", wrappedFTL); 185 templateContext.put("webSiteId", webSiteId); 188 templateContext.put("https", https); 189 templateContext.put("rootDir", rootDir); 190 191 Map templateRoot = FreeMarkerWorker.createEnvironmentMap(env); 192 193 templateRoot.put("wrapDataResourceId", dataResourceId); 194 templateRoot.put("wrapDataResourceTypeId", subDataResourceTypeId); 195 templateRoot.put("wrapContentIdTo", contentId); 196 templateRoot.put("wrapSubContentId", subContentIdSub); 197 templateRoot.put("wrapMimeTypeId", mimeTypeId); 198 templateRoot.put("wrapMapKey", mapKey); 199 templateRoot.put("context", templateContext); 200 201 try { 202 ContentWorker.renderContentAsText(delegator, wrapTemplateId, out, templateRoot, null, locale, mimeTypeId); 203 } catch (IOException e) { 204 Debug.logError(e, "Error rendering content" + e.getMessage(), module); 205 throw new IOException ("Error rendering content" + e.toString()); 206 } catch (GeneralException e2) { 207 Debug.logError(e2, "Error rendering content" + e2.getMessage(), module); 208 throw new IOException ("Error rendering content" + e2.toString()); 209 } 210 211 Map ctx = (Map ) FreeMarkerWorker.getWrappedObject("context", env); 212 templateContext.put("contentId", contentId); 213 templateContext.put("locale", locale); 214 templateContext.put("mapKey", null); 215 templateContext.put("subContentId", null); 216 templateContext.put("templateContentId", null); 217 templateContext.put("subDataResourceTypeId", null); 218 templateContext.put("mimeTypeId", null); 219 templateContext.put("wrappedFTL", null); 220 } 221 } else { 222 out.write(wrappedFTL); 223 } 224 } 225 }; 226 } 227 } 228 | Popular Tags |