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.Map ; 26 27 import javax.servlet.http.HttpServletRequest ; 28 29 import org.ofbiz.base.util.GeneralException; 30 import org.ofbiz.base.util.UtilDateTime; 31 import org.ofbiz.base.util.UtilValidate; 32 import org.ofbiz.base.util.template.FreeMarkerWorker; 33 import org.ofbiz.content.content.ContentWorker; 34 import org.ofbiz.entity.GenericDelegator; 35 import org.ofbiz.entity.GenericValue; 36 import org.ofbiz.webapp.ftl.LoopWriter; 37 38 import freemarker.core.Environment; 39 import freemarker.template.TemplateModelException; 40 import freemarker.template.TemplateTransformModel; 41 import freemarker.template.TransformControl; 42 43 51 public class TraverseSubContentCacheTransform implements TemplateTransformModel { 52 53 public static final String module = TraverseSubContentCacheTransform.class.getName(); 54 public static final String [] upSaveKeyNames = {"globalNodeTrail"}; 55 public static final String [] saveKeyNames = {"contentId", "subContentId", "subDataResourceTypeId", "mimeTypeId", "whenMap", "locale", "wrapTemplateId", "encloseWrapText", "nullThruDatesOnly", "globalNodeTrail"}; 56 57 60 public static Object getWrappedObject(String varName, Environment env) { 61 return FreeMarkerWorker.getWrappedObject(varName, env); 62 } 63 64 public static String getArg(Map args, String key, Environment env) { 65 return FreeMarkerWorker.getArg(args, key, env); 66 } 67 68 public static String getArg(Map args, String key, Map ctx) { 69 return FreeMarkerWorker.getArg(args, key, ctx); 70 } 71 72 public Writer getWriter(final Writer out, Map args) { 73 final StringBuffer buf = new StringBuffer (); 74 final Environment env = Environment.getCurrentEnvironment(); 75 final Map templateRoot = FreeMarkerWorker.createEnvironmentMap(env); 77 final Map savedValuesUp = new HashMap (); 79 FreeMarkerWorker.saveContextValues(templateRoot, upSaveKeyNames, savedValuesUp); 80 final Map savedValues = new HashMap (); 81 FreeMarkerWorker.overrideWithArgs(templateRoot, args); 82 String startContentAssocTypeId = (String )templateRoot.get( "contentAssocTypeId"); 83 final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env); 85 final HttpServletRequest request = (HttpServletRequest ) FreeMarkerWorker.getWrappedObject("request", env); 86 FreeMarkerWorker.getSiteParameters(request, templateRoot); 87 final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env); 88 Object obj = templateRoot.get("globalNodeTrail"); 89 List globalNodeTrail = (List )obj; 90 String csvTrail = ContentWorker.nodeTrailToCsv(globalNodeTrail); 92 String strNullThruDatesOnly = (String )templateRoot.get("nullThruDatesOnly"); 94 String contentAssocPredicateId = (String )templateRoot.get("contentAssocPredicateId"); 95 Boolean nullThruDatesOnly = (strNullThruDatesOnly != null && strNullThruDatesOnly.equalsIgnoreCase("true")) ? new Boolean (true) :new Boolean (false); 96 GenericValue val = null; 97 try { 98 val = ContentWorker.getCurrentContent(delegator, globalNodeTrail, userLogin, templateRoot, nullThruDatesOnly, contentAssocPredicateId); 102 } catch(GeneralException e) { 103 throw new RuntimeException ("Error getting current content. " + e.toString()); 104 } 105 final GenericValue view = val; 106 107 108 final Map traverseContext = new HashMap (); 109 traverseContext.put("delegator", delegator); 110 Map whenMap = new HashMap (); 111 whenMap.put("followWhen", (String )templateRoot.get( "followWhen")); 112 whenMap.put("pickWhen", (String )templateRoot.get( "pickWhen")); 113 whenMap.put("returnBeforePickWhen", (String )templateRoot.get( "returnBeforePickWhen")); 114 whenMap.put("returnAfterPickWhen", (String )templateRoot.get( "returnAfterPickWhen")); 115 traverseContext.put("whenMap", whenMap); 116 env.setVariable("whenMap", FreeMarkerWorker.autoWrap(whenMap, env)); 117 String fromDateStr = (String )templateRoot.get( "fromDateStr"); 118 String thruDateStr = (String )templateRoot.get( "thruDateStr"); 119 Timestamp fromDate = null; 120 if (fromDateStr != null && fromDateStr.length() > 0) { 121 fromDate = UtilDateTime.toTimestamp(fromDateStr); 122 } 123 traverseContext.put("fromDate", fromDate); 124 Timestamp thruDate = null; 125 if (thruDateStr != null && thruDateStr.length() > 0) { 126 thruDate = UtilDateTime.toTimestamp(thruDateStr); 127 } 128 traverseContext.put("thruDate", thruDate); 129 traverseContext.put("contentAssocTypeId", startContentAssocTypeId); 133 String direction = (String )templateRoot.get( "direction"); 134 if (UtilValidate.isEmpty(direction)) { 135 direction = "From"; 136 } 137 traverseContext.put("direction", direction); 138 139 140 return new LoopWriter(out) { 141 142 public void write(char cbuf[], int off, int len) { 143 buf.append(cbuf, off, len); 146 } 147 148 public void flush() throws IOException { 149 out.flush(); 150 } 151 152 public int onStart() throws TemplateModelException, IOException { 153 List nodeTrail = null; 155 Map node = null; 156 GenericValue subContentDataResourceView = null; 157 List globalNodeTrail = (List )templateRoot.get("globalNodeTrail"); 158 String trailCsv = ContentWorker.nodeTrailToCsv(globalNodeTrail); 159 if (globalNodeTrail.size() > 0) { 161 int sz = globalNodeTrail.size() ; 162 nodeTrail = new ArrayList (); 163 node = (Map )globalNodeTrail.get(sz - 1); 165 Boolean checkedObj = (Boolean )node.get("checked"); 167 Map whenMap = (Map )templateRoot.get("whenMap"); 168 if (checkedObj == null || !checkedObj.booleanValue()) { 170 ContentWorker.checkConditions(delegator, node, null, whenMap); 171 } 172 subContentDataResourceView = (GenericValue)node.get("value"); 173 } else { 174 throw new IOException ("Empty node trail entries"); 175 } 176 177 Boolean isReturnBeforePickBool = (Boolean )node.get("isReturnBeforePick"); 178 if (isReturnBeforePickBool != null && isReturnBeforePickBool.booleanValue()) 179 return TransformControl.SKIP_BODY; 180 181 GenericValue content = null; 182 ContentWorker.selectKids(node, traverseContext); 183 nodeTrail.add(node); 185 traverseContext.put("nodeTrail", nodeTrail); 186 Boolean isPickBool = (Boolean )node.get("isPick"); 187 Boolean isFollowBool = (Boolean )node.get("isFollow"); 188 boolean isPick = true; 190 if ((isPickBool == null || !isPickBool.booleanValue()) 191 && (isFollowBool != null && isFollowBool.booleanValue())) { 192 isPick = ContentWorker.traverseSubContent(traverseContext); 193 } 195 if (isPick) { 196 populateContext(traverseContext, templateRoot); 197 FreeMarkerWorker.saveContextValues(templateRoot, saveKeyNames, savedValues); 198 return TransformControl.EVALUATE_BODY; 199 } else { 200 return TransformControl.SKIP_BODY; 201 } 202 } 203 204 public int afterBody() throws TemplateModelException, IOException { 205 206 207 FreeMarkerWorker.reloadValues(templateRoot, savedValues, env); 208 List globalNodeTrail = (List )templateRoot.get("globalNodeTrail"); 209 List nodeTrail = (List )traverseContext.get("nodeTrail"); 211 boolean inProgress = ContentWorker.traverseSubContent(traverseContext); 214 int sz = nodeTrail.size(); 215 if (inProgress) { 216 populateContext(traverseContext, templateRoot); 217 FreeMarkerWorker.saveContextValues(templateRoot, saveKeyNames, savedValues); 218 return TransformControl.REPEAT_EVALUATION; 221 } else 222 return TransformControl.END_EVALUATION; 223 } 224 225 public void close() throws IOException { 226 227 FreeMarkerWorker.reloadValues(templateRoot, savedValuesUp, env); 228 String wrappedContent = buf.toString(); 229 out.write(wrappedContent); 230 } 232 233 234 public void populateContext(Map traverseContext, Map templateContext) { 235 236 List nodeTrail = (List )traverseContext.get("nodeTrail"); 237 int sz = nodeTrail.size(); 239 Map node = (Map )nodeTrail.get(sz - 1); 240 GenericValue content = (GenericValue)node.get("value"); 241 String contentId = (String )node.get("contentId"); 242 String subContentId = (String )node.get("subContentId"); 243 String contentAssocTypeId = (String )node.get("contentAssocTypeId"); 244 envWrap("contentAssocTypeId", contentAssocTypeId); 245 envWrap("contentId", contentId); 246 envWrap("content", content); 247 String mapKey = (String )node.get("mapKey"); 248 envWrap("mapKey", mapKey); 249 envWrap("subContentDataResourceView", null); 250 List globalNodeTrail = (List )templateContext.get("globalNodeTrail"); 251 String contentIdEnd = null; 252 String contentIdStart = null; 253 if (globalNodeTrail != null) { 254 Map ndEnd = (Map )globalNodeTrail.get(globalNodeTrail.size() - 1); 255 contentIdEnd = (String )ndEnd.get("contentId"); 256 Map ndStart = (Map )nodeTrail.get(0); 257 contentIdStart = (String )ndStart.get("contentId"); 258 } else { 259 globalNodeTrail = new ArrayList (); 260 } 261 boolean bIdEnd = UtilValidate.isNotEmpty(contentIdEnd); 263 boolean bIdStart = UtilValidate.isNotEmpty(contentIdStart); 264 boolean bEquals = contentIdStart.equals(contentIdEnd); 265 if (bIdEnd && bIdStart && bEquals) { 268 List subList = nodeTrail.subList(1, sz); 269 globalNodeTrail.addAll(subList); 270 } else { 271 globalNodeTrail.addAll(nodeTrail); 272 } 273 int indentSz = globalNodeTrail.size(); 275 envWrap("indent", new Integer (indentSz)); 276 String trailCsv = ContentWorker.nodeTrailToCsv(globalNodeTrail); 277 envWrap("nodeTrailCsv", trailCsv); 279 envWrap("globalNodeTrail", globalNodeTrail); 280 return; 281 } 282 283 public void envWrap(String varName, Object obj) { 284 285 templateRoot.put(varName, obj); 286 env.setVariable(varName, FreeMarkerWorker.autoWrap(obj, env)); 287 } 288 289 }; 290 } 291 } 292 | Popular Tags |