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.List ; 23 import java.util.Locale ; 24 import java.util.Map ; 25 26 import org.ofbiz.base.util.Debug; 27 import org.ofbiz.base.util.GeneralException; 28 import org.ofbiz.base.util.UtilDateTime; 29 import org.ofbiz.base.util.UtilMisc; 30 import org.ofbiz.base.util.UtilValidate; 31 import org.ofbiz.base.util.template.FreeMarkerWorker; 32 import org.ofbiz.content.content.ContentServicesComplex; 33 import org.ofbiz.content.content.ContentWorker; 34 import org.ofbiz.entity.GenericDelegator; 35 import org.ofbiz.entity.GenericEntityException; 36 import org.ofbiz.entity.GenericValue; 37 import org.ofbiz.webapp.ftl.LoopWriter; 38 39 import freemarker.core.Environment; 40 import freemarker.template.TemplateModelException; 41 import freemarker.template.TemplateTransformModel; 42 import freemarker.template.TransformControl; 43 44 51 public class LoopSubContentTransform implements TemplateTransformModel { 52 53 public static final String module = LoopSubContentTransform.class.getName(); 54 55 public static final String [] saveKeyNames = {"contentId", "subContentId", "mimeType", "subContentDataResourceView", "wrapTemplateId", "contentTemplateId"}; 56 public static final String [] removeKeyNames = {"wrapTemplateId", "entityList", "entityIndex", "textData", "dataResourceId","drDataResourceId", "subContentIdSub", "parentContent", "wrappedFTL"}; 57 58 61 public static Object getWrappedObject(String varName, Environment env) { 62 return FreeMarkerWorker.getWrappedObject(varName, env); 63 } 64 65 public static String getArg(Map args, String key, Environment env) { 66 return FreeMarkerWorker.getArg(args, key, env); 67 } 68 69 public static String getArg(Map args, String key, Map ctx) { 70 return FreeMarkerWorker.getArg(args, key, ctx); 71 } 72 73 public static boolean prepCtx(GenericDelegator delegator, Map ctx) { 74 75 List lst = (List ) ctx.get("entityList"); 78 Integer idx = (Integer ) ctx.get("entityIndex"); 79 if (idx == null) 80 idx = new Integer (0); 81 int i = idx.intValue(); 82 if (i >= lst.size()) { 83 return false; 84 } 85 GenericValue subContentDataResourceView = (GenericValue) lst.get(i); 86 ctx.put("subContentDataResourceView", subContentDataResourceView); 87 GenericValue electronicText = null; 88 try { 89 electronicText = subContentDataResourceView.getRelatedOne("ElectronicText"); 90 } catch (GenericEntityException e) { 91 throw new RuntimeException (e.getMessage()); 92 } 93 94 String dataResourceId = (String ) subContentDataResourceView.get("drDataResourceId"); 95 String subContentIdSub = (String ) subContentDataResourceView.get("contentId"); 96 String subDataResourceTypeId = (String )ctx.get("subDataResourceTypeId"); 98 if (UtilValidate.isEmpty(subDataResourceTypeId)) { 99 subDataResourceTypeId = (String ) subContentDataResourceView.get("drDataResourceTypeId"); 100 } 104 String mimeTypeId = (String )ctx.get("mimeTypeId"); 106 if (UtilValidate.isEmpty(mimeTypeId)) { 107 mimeTypeId = (String ) subContentDataResourceView.get("mimeTypeId"); 108 String parentContentId = (String )ctx.get("contentId"); 109 if (UtilValidate.isEmpty(mimeTypeId) && UtilValidate.isNotEmpty(parentContentId)) { try { 111 GenericValue parentContent = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", parentContentId)); 112 if (parentContent != null) { 113 mimeTypeId = (String ) parentContent.get("mimeTypeId"); 114 ctx.put("parentContent", parentContent); 115 } 116 } catch (GenericEntityException e) { 117 throw new RuntimeException (e.getMessage()); 118 } 119 } 120 121 } 122 123 ctx.put("subContentDataResourceView", subContentDataResourceView); 125 if (electronicText != null) 126 ctx.put("textData", electronicText.get("textData")); 127 else 128 ctx.put("textData", null); 129 ctx.put("entityIndex", new Integer (i + 1)); 130 ctx.put("subContentId", subContentIdSub); 131 ctx.put("drDataResourceId", dataResourceId); 132 ctx.put("mimeTypeId", mimeTypeId); 133 ctx.put("dataResourceId", dataResourceId); 134 ctx.put("subContentIdSub", subContentIdSub); 135 ctx.put("subDataResourceTypeId", subDataResourceTypeId); 136 return true; 137 } 138 139 public Writer getWriter(final Writer out, Map args) { 140 final StringBuffer buf = new StringBuffer (); 141 final Environment env = Environment.getCurrentEnvironment(); 142 final Map templateCtx = (Map ) FreeMarkerWorker.getWrappedObject("context", env); 143 final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env); 145 final Map savedValues = FreeMarkerWorker.saveValues(templateCtx, saveKeyNames); 147 FreeMarkerWorker.overrideWithArgs(templateCtx, args); 148 String contentAssocTypeId = (String )templateCtx.get("contentAssocTypeId"); 149 if (UtilValidate.isEmpty(contentAssocTypeId)) { 150 contentAssocTypeId = "SUB_CONTENT"; 151 templateCtx.put("contentAssocTypeId ", contentAssocTypeId ); 152 } 153 List assocTypes = UtilMisc.toList(contentAssocTypeId); 154 templateCtx.put("assocTypes", assocTypes); 155 Locale locale = (Locale ) templateCtx.get("locale"); 156 if (locale == null) { 157 locale = Locale.getDefault(); 158 templateCtx.put("locale", locale); 159 } 160 161 195 String fromDateStr = (String )templateCtx.get("fromDateStr"); 196 Timestamp fromDate = null; 197 if (UtilValidate.isNotEmpty(fromDateStr)) { 198 fromDate = UtilDateTime.toTimestamp(fromDateStr); 199 } 200 if (fromDate == null) 201 fromDate = UtilDateTime.nowTimestamp(); 202 String thisContentId = (String )templateCtx.get("contentId"); 203 if (UtilValidate.isEmpty(thisContentId)) { 204 thisContentId = (String )templateCtx.get("subContentId"); 205 } 206 String thisMapKey = (String )templateCtx.get("mapKey"); 207 Map results = 209 ContentServicesComplex.getAssocAndContentAndDataResourceMethod(delegator, thisContentId, thisMapKey, "From", fromDate, null, null, null, assocTypes, null); 210 List entityList = (List ) results.get("entityList"); 211 templateCtx.put("entityList", entityList); 212 213 return new LoopWriter(out) { 214 215 public void write(char cbuf[], int off, int len) { 216 buf.append(cbuf, off, len); 217 } 220 221 public void flush() throws IOException { 222 out.flush(); 223 } 224 225 public int onStart() throws TemplateModelException, IOException { 226 templateCtx.put("entityIndex", new Integer (0)); 227 boolean inProgress = prepCtx(delegator, templateCtx); 228 if (inProgress) { 229 return TransformControl.EVALUATE_BODY; 230 } else { 231 return TransformControl.SKIP_BODY; 232 } 233 } 234 235 public int afterBody() throws TemplateModelException, IOException { 236 Integer idx = (Integer ) templateCtx.get("entityIndex"); 237 int i = idx.intValue(); 238 boolean inProgress = prepCtx(delegator, templateCtx); 239 if (inProgress) 242 return TransformControl.REPEAT_EVALUATION; 243 else 244 return TransformControl.END_EVALUATION; 245 } 246 247 public void close() throws IOException { 248 249 String wrappedFTL = buf.toString(); 250 String encloseWrappedText = (String )templateCtx.get("encloseWrappedText"); 251 if (UtilValidate.isEmpty(encloseWrappedText) || encloseWrappedText.equalsIgnoreCase("false")) { 252 out.write(wrappedFTL); 253 wrappedFTL = ""; } 255 String wrapTemplateId = (String )templateCtx.get("wrapTemplateId"); 256 if (UtilValidate.isNotEmpty(wrapTemplateId)) { 257 templateCtx.put("wrappedFTL", wrappedFTL); 258 259 Map templateRoot = FreeMarkerWorker.createEnvironmentMap(env); 260 261 270 templateRoot.put("wrapDataResourceTypeId", templateCtx.get("subDataResourceTypeId")); 271 templateRoot.put("wrapContentIdTo", templateCtx.get("contentId")); 272 templateRoot.put("wrapMimeTypeId", templateCtx.get("mimeTypeId")); 273 templateRoot.put("context", templateCtx); 274 275 276 Locale locale = (Locale ) templateCtx.get("locale"); 277 if (locale == null) 278 locale = Locale.getDefault(); 279 String mimeTypeId = (String ) templateCtx.get("mimeTypeId"); 280 try { 281 ContentWorker.renderContentAsText(delegator, wrapTemplateId, out, templateRoot, null, locale, mimeTypeId); 282 } catch (GeneralException e) { 283 Debug.logError(e, "Error rendering content", module); 284 throw new IOException ("Error rendering content" + e.toString()); 285 } 286 } else { 287 if (UtilValidate.isNotEmpty(wrappedFTL)) 288 out.write(wrappedFTL); 289 } 290 FreeMarkerWorker.removeValues(templateCtx, removeKeyNames); 291 FreeMarkerWorker.reloadValues(templateCtx, savedValues, env); 292 } 293 }; 294 } 295 } 296 | Popular Tags |