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.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.List ; 25 import java.util.Locale ; 26 import java.util.Map ; 27 28 import org.ofbiz.base.util.Debug; 29 import org.ofbiz.base.util.GeneralException; 30 import org.ofbiz.base.util.UtilDateTime; 31 import org.ofbiz.base.util.UtilMisc; 32 import org.ofbiz.base.util.UtilValidate; 33 import org.ofbiz.base.util.template.FreeMarkerWorker; 34 import org.ofbiz.content.content.ContentWorker; 35 import org.ofbiz.entity.GenericDelegator; 36 import org.ofbiz.entity.GenericEntityException; 37 import org.ofbiz.entity.GenericValue; 38 import org.ofbiz.webapp.ftl.LoopWriter; 39 40 import freemarker.core.Environment; 41 import freemarker.template.TemplateModelException; 42 import freemarker.template.TemplateTransformModel; 43 import freemarker.template.TransformControl; 44 45 52 public class TraverseSubContentTransform implements TemplateTransformModel { 53 54 public static final String module = TraverseSubContentTransform.class.getName(); 55 public static final String [] saveKeyNames = {"contentId", "subContentId", "mimeType", "subContentDataResourceView", "wrapTemplateId", "templateContentId", "pickWhen", "followWhen", "returnAfterPickWhen", "returnBeforePickWhen", "indent"}; 56 public static final String [] removeKeyNames = {"templateContentId", "subDataResourceTypeId", "mapKey", "wrappedFTL", "nodeTrail"}; 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 Writer getWriter(final Writer out, Map args) { 74 final StringBuffer buf = new StringBuffer (); 75 final Environment env = Environment.getCurrentEnvironment(); 76 final Map templateCtx = (Map ) FreeMarkerWorker.getWrappedObject("context", env); 77 final Map savedValues = FreeMarkerWorker.saveValues(templateCtx, saveKeyNames); 79 FreeMarkerWorker.overrideWithArgs(templateCtx, args); 80 final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env); 81 101 GenericValue view = (GenericValue) FreeMarkerWorker.getWrappedObject("subContentDataResourceView", env); 104 final Integer indent = (templateCtx.get("indent") == null) ? new Integer (0) : (Integer )templateCtx.get("indent"); 105 106 String contentId = (String )templateCtx.get("contentId"); 107 String subContentId = (String )templateCtx.get("subContentId"); 108 if (view == null) { 109 String thisContentId = subContentId; 110 if (UtilValidate.isEmpty(thisContentId)) 111 thisContentId = contentId; 112 113 if (UtilValidate.isNotEmpty(thisContentId)) { 114 115 try { 116 view = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", thisContentId)); 117 } catch (GenericEntityException e) { 118 Debug.logError(e, "Error getting sub-content", module); 119 throw new RuntimeException (e.getMessage()); 120 } 121 } 122 } 123 124 final GenericValue subContentDataResourceView = view; 125 126 final Map traverseContext = new HashMap (); 127 traverseContext.put("delegator", delegator); 128 Map whenMap = new HashMap (); 129 whenMap.put("followWhen", (String )templateCtx.get( "followWhen")); 130 whenMap.put("pickWhen", (String )templateCtx.get( "pickWhen")); 131 whenMap.put("returnBeforePickWhen", (String )templateCtx.get( "returnBeforePickWhen")); 132 whenMap.put("returnAfterPickWhen", (String )templateCtx.get( "returnAfterPickWhen")); 133 traverseContext.put("whenMap", whenMap); 134 String fromDateStr = (String )templateCtx.get( "fromDateStr"); 135 String thruDateStr = (String )templateCtx.get( "thruDateStr"); 136 Timestamp fromDate = null; 137 if (fromDateStr != null && fromDateStr.length() > 0) { 138 fromDate = UtilDateTime.toTimestamp(fromDateStr); 139 } 140 traverseContext.put("fromDate", fromDate); 141 Timestamp thruDate = null; 142 if (thruDateStr != null && thruDateStr.length() > 0) { 143 thruDate = UtilDateTime.toTimestamp(thruDateStr); 144 } 145 traverseContext.put("thruDate", thruDate); 146 String startContentAssocTypeId = (String )templateCtx.get( "contentAssocTypeId"); 147 if (startContentAssocTypeId != null) 148 startContentAssocTypeId = "SUB_CONTENT"; 149 traverseContext.put("contentAssocTypeId", startContentAssocTypeId); 150 String direction = (String )templateCtx.get( "direction"); 151 if (UtilValidate.isEmpty(direction)) 152 direction = "From"; 153 traverseContext.put("direction", direction); 154 155 156 return new LoopWriter(out) { 157 158 public void write(char cbuf[], int off, int len) { 159 buf.append(cbuf, off, len); 162 } 163 164 public void flush() throws IOException { 165 out.flush(); 166 } 167 168 public int onStart() throws TemplateModelException, IOException { 169 List nodeTrail = new ArrayList (); 171 traverseContext.put("nodeTrail", nodeTrail); 172 GenericValue content = null; 173 183 Map rootNode = ContentWorker.makeNode(subContentDataResourceView); 184 ContentWorker.traceNodeTrail("1",nodeTrail); 185 ContentWorker.selectKids(rootNode, traverseContext); 186 ContentWorker.traceNodeTrail("2",nodeTrail); 187 nodeTrail.add(rootNode); 188 boolean isPick = checkWhen(subContentDataResourceView, (String )traverseContext.get("contentAssocTypeId")); 189 rootNode.put("isPick", new Boolean (isPick)); 190 if (!isPick) { 191 ContentWorker.traceNodeTrail("3",nodeTrail); 192 isPick = ContentWorker.traverseSubContent(traverseContext); 193 ContentWorker.traceNodeTrail("4",nodeTrail); 194 } 195 if (isPick) { 196 populateContext(traverseContext, templateCtx); 197 ContentWorker.traceNodeTrail("5",nodeTrail); 198 return TransformControl.EVALUATE_BODY; 199 } else { 200 return TransformControl.SKIP_BODY; 201 } 202 } 203 204 public int afterBody() throws TemplateModelException, IOException { 205 List nodeTrail = (List )traverseContext.get("nodeTrail"); 209 ContentWorker.traceNodeTrail("6",nodeTrail); 210 boolean inProgress = ContentWorker.traverseSubContent(traverseContext); 211 ContentWorker.traceNodeTrail("7",nodeTrail); 212 if (inProgress) { 213 populateContext(traverseContext, templateCtx); 214 ContentWorker.traceNodeTrail("8",nodeTrail); 215 return TransformControl.REPEAT_EVALUATION; 216 } else 217 return TransformControl.END_EVALUATION; 218 } 219 220 public void close() throws IOException { 221 222 String wrappedFTL = buf.toString(); 223 String encloseWrappedText = (String )templateCtx.get("encloseWrappedText"); 224 if (UtilValidate.isEmpty(encloseWrappedText) || encloseWrappedText.equalsIgnoreCase("false")) { 225 226 out.write(wrappedFTL); 227 wrappedFTL = null; } 229 String wrapTemplateId = (String )templateCtx.get("wrapTemplateId"); 230 if (UtilValidate.isNotEmpty(wrapTemplateId)) { 231 templateCtx.put("wrappedFTL", wrappedFTL); 232 233 Map templateRoot = FreeMarkerWorker.createEnvironmentMap(env); 234 235 248 templateRoot.put("context", templateCtx); 249 String mimeTypeId = (String ) templateCtx.get("mimeTypeId"); 250 Locale locale = (Locale ) templateCtx.get("locale"); 251 if (locale == null) 252 locale = Locale.getDefault(); 253 try { 254 ContentWorker.renderContentAsText(delegator, wrapTemplateId, out, templateRoot, null, locale, mimeTypeId); 255 } catch (GeneralException e) { 256 Debug.logError(e, "Error rendering content", module); 257 throw new IOException ("Error rendering content" + e.toString()); 258 } 259 269 270 271 } else { 272 if (UtilValidate.isNotEmpty(wrappedFTL)) 273 out.write(wrappedFTL); 274 } 275 FreeMarkerWorker.removeValues(templateCtx, removeKeyNames); 276 FreeMarkerWorker.reloadValues(templateCtx, savedValues, env); 277 } 278 279 private boolean checkWhen (GenericValue thisContent, String contentAssocTypeId) { 280 281 boolean isPick = false; 282 Map assocContext = new HashMap (); 283 if (UtilValidate.isEmpty(contentAssocTypeId)) 284 contentAssocTypeId = ""; 285 assocContext.put("contentAssocTypeId", contentAssocTypeId); 286 String assocRelation = null; 288 String thisDirection = (String )templateCtx.get("direction"); 289 String thisContentId = (String )templateCtx.get("thisContentId"); 290 String relatedDirection = null; 291 if (thisDirection != null && thisDirection.equalsIgnoreCase("From")) { 292 assocContext.put("contentIdFrom", thisContentId); 293 assocRelation = "FromContent"; 294 relatedDirection = "From"; 295 } else { 296 assocContext.put("contentIdTo", thisContentId); 297 assocRelation = "ToContent"; 298 relatedDirection = "To"; 299 } 300 assocContext.put("content", thisContent); 301 List purposes = ContentWorker.getPurposes(thisContent); 302 assocContext.put("purposes", purposes); 303 List contentTypeAncestry = new ArrayList (); 304 String contentTypeId = (String )thisContent.get("contentTypeId"); 305 try { 306 ContentWorker.getContentTypeAncestry(delegator, contentTypeId, contentTypeAncestry); 307 } catch(GenericEntityException e) { 308 return false; 309 } 310 assocContext.put("typeAncestry", contentTypeAncestry); 311 Map whenMap = (Map )traverseContext.get("whenMap"); 312 String pickWhen = (String )whenMap.get("pickWhen"); 313 List nodeTrail = (List )traverseContext.get("nodeTrail"); 314 int indentSz = indent.intValue() + nodeTrail.size(); 315 assocContext.put("indentObj", new Integer (indentSz)); 316 isPick = ContentWorker.checkWhen(assocContext, (String )whenMap.get("pickWhen")); 317 return isPick; 318 } 319 320 321 public void populateContext(Map traverseContext, Map templateContext) { 322 323 List nodeTrail = (List )traverseContext.get("nodeTrail"); 324 int sz = nodeTrail.size(); 325 Map node = (Map )nodeTrail.get(sz - 1); 326 GenericValue content = (GenericValue)node.get("value"); 327 String contentId = (String )node.get("contentId"); 328 String subContentId = (String )node.get("subContentId"); 329 templateContext.put("subContentId", contentId); 330 templateContext.put("subContentDataResourceView", null); 331 int indentSz = indent.intValue() + nodeTrail.size(); 332 templateContext.put("indent", new Integer (indentSz)); 333 if (sz >= 2) { 334 Map parentNode = (Map )nodeTrail.get(sz - 2); 335 GenericValue parentContent = (GenericValue)parentNode.get("value"); 336 String parentContentId = (String )parentNode.get("contentId"); 337 templateContext.put("parentContentId", parentContentId); 338 templateContext.put("parentContent", parentContent); 339 templateContext.put("nodeTrail", nodeTrail); 340 } 341 return; 342 } 343 344 }; 345 } 346 } 347 | Popular Tags |