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.Iterator ; 24 import java.util.List ; 25 import java.util.Locale ; 26 import java.util.Map ; 27 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.StringUtil; 33 import org.ofbiz.base.util.UtilDateTime; 34 import org.ofbiz.base.util.UtilProperties; 35 import org.ofbiz.base.util.UtilValidate; 36 import org.ofbiz.base.util.template.FreeMarkerWorker; 37 import org.ofbiz.content.content.ContentServicesComplex; 38 import org.ofbiz.content.content.ContentWorker; 39 import org.ofbiz.entity.GenericDelegator; 40 import org.ofbiz.entity.GenericEntityException; 41 import org.ofbiz.entity.GenericValue; 42 import org.ofbiz.minilang.MiniLangException; 43 import org.ofbiz.webapp.ftl.LoopWriter; 44 45 import freemarker.core.Environment; 46 import freemarker.template.TemplateModelException; 47 import freemarker.template.TemplateTransformModel; 48 import freemarker.template.TransformControl; 49 50 57 public class LoopSubContentCacheTransform implements TemplateTransformModel { 58 59 public static final String module = LoopSubContentCacheTransform.class.getName(); 60 61 public static final String [] upSaveKeyNames = {"globalNodeTrail"}; 62 public static final String [] saveKeyNames = {"contentId", "subContentId", "entityList", "entityIndex", "subDataResourceTypeId", "mimeTypeId", "whenMap", "locale", "entityList", "viewSize", "viewIndex", "highIndex", "lowIndex", "listSize", "wrapTemplateId", "encloseWrapText", "nullThruDatesOnly", "globalNodeTrail", "outputIndex"}; 63 64 67 public static Object getWrappedObject(String varName, Environment env) { 68 return FreeMarkerWorker.getWrappedObject(varName, env); 69 } 70 71 public static String getArg(Map args, String key, Environment env) { 72 return FreeMarkerWorker.getArg(args, key, env); 73 } 74 75 public static String getArg(Map args, String key, Map ctx) { 76 return FreeMarkerWorker.getArg(args, key, ctx); 77 } 78 79 public static boolean prepCtx(GenericDelegator delegator, Map ctx, Environment env) throws GeneralException { 80 81 List lst = (List ) ctx.get("entityList"); 84 int entityIndex = ((Integer ) ctx.get("entityIndex")).intValue(); 85 if (entityIndex >= lst.size()) { 87 return false; 88 } 89 GenericValue view = (GenericValue) lst.get(entityIndex); 90 92 String dataResourceId = (String ) view.get("drDataResourceId"); 93 String subContentIdSub = (String ) view.get("contentId"); 94 String subDataResourceTypeId = (String ) ctx.get("subDataResourceTypeId"); 99 if (UtilValidate.isEmpty(subDataResourceTypeId)) { 100 subDataResourceTypeId = (String ) view.get("drDataResourceTypeId"); 101 } 105 106 String mimeTypeId = ContentWorker.getMimeTypeId(delegator, view, ctx); 107 Map trailNode = ContentWorker.makeNode(view); 108 Map whenMap = (Map ) ctx.get("whenMap"); 109 Locale locale = (Locale ) ctx.get("locale"); 110 if (locale == null) 111 locale = Locale.getDefault(); 112 GenericValue assocContent = null; 113 ContentWorker.checkConditions(delegator, trailNode, assocContent, whenMap); 114 Boolean isReturnBeforeObj = (Boolean ) trailNode.get("isReturnBefore"); 115 Boolean isReturnAfterObj = (Boolean ) trailNode.get("isReturnAfter"); 116 Boolean isPickObj = (Boolean ) trailNode.get("isPick"); 117 Boolean isFollowObj = (Boolean ) trailNode.get("isFollow"); 118 if ((isReturnBeforeObj == null || !isReturnBeforeObj.booleanValue()) && ((isPickObj != null && 120 isPickObj.booleanValue()) || (isFollowObj != null && isFollowObj.booleanValue()))) { 121 List globalNodeTrail = (List ) ctx.get("globalNodeTrail"); 122 globalNodeTrail.add(trailNode); 123 ctx.put("globalNodeTrail", globalNodeTrail); 124 String csvTrail = ContentWorker.nodeTrailToCsv(globalNodeTrail); 125 ctx.put("nodeTrailCsv", csvTrail); 126 int indentSz = globalNodeTrail.size(); 128 ctx.put("indent", new Integer (indentSz)); 129 132 144 145 ctx.put("subDataResourceTypeId", subDataResourceTypeId); 146 ctx.put("mimeTypeId", mimeTypeId); 147 ctx.put("subContentId", subContentIdSub); 148 ctx.put("content", view); 149 153 env.setVariable("subDataResourceTypeId", FreeMarkerWorker.autoWrap(subDataResourceTypeId, env)); 154 env.setVariable("indent", FreeMarkerWorker.autoWrap(new Integer (indentSz), env)); 155 env.setVariable("nodeTrailCsv", FreeMarkerWorker.autoWrap(csvTrail, env)); 156 env.setVariable("globalNodeTrail", FreeMarkerWorker.autoWrap(globalNodeTrail, env)); 157 env.setVariable("content", FreeMarkerWorker.autoWrap(view, env)); 158 env.setVariable("mimeTypeId", FreeMarkerWorker.autoWrap(mimeTypeId, env)); 159 env.setVariable("subContentId", FreeMarkerWorker.autoWrap(subContentIdSub, env)); 160 return true; 161 } else { 162 return false; 163 } 164 } 165 166 public static boolean getNextMatchingEntity(Map templateRoot, GenericDelegator delegator, Environment env) throws IOException { 167 int lowIndex = ((Integer ) templateRoot.get("lowIndex")).intValue(); 168 int entityIndex = ((Integer ) templateRoot.get("entityIndex")).intValue(); 169 int outputIndex = ((Integer ) templateRoot.get("outputIndex")).intValue(); 170 int listSize = ((Integer ) templateRoot.get("listSize")).intValue(); 171 boolean matchFound = false; 172 173 while (!matchFound && entityIndex < listSize) { 174 try { 175 matchFound = prepCtx(delegator, templateRoot, env); 176 } catch (GeneralException e) { 177 throw new IOException (e.getMessage()); 178 } 179 entityIndex++; 180 templateRoot.put("entityIndex", new Integer (entityIndex)); 181 if (matchFound) { 182 outputIndex++; 183 if (outputIndex >= lowIndex) { 184 break; 185 } else { 186 matchFound = false; 187 } 188 } 189 } 190 templateRoot.put("outputIndex", new Integer (outputIndex)); 192 env.setVariable("outputIndex", FreeMarkerWorker.autoWrap(new Integer (outputIndex), env)); 193 env.setVariable("entityIndex", FreeMarkerWorker.autoWrap(new Integer (entityIndex), env)); 194 return matchFound; 195 } 196 197 public Writer getWriter(final Writer out, Map args) { 198 final StringBuffer buf = new StringBuffer (); 200 final Environment env = Environment.getCurrentEnvironment(); 201 final Map templateRoot = FreeMarkerWorker.createEnvironmentMap(env); 203 final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env); 205 final HttpServletRequest request = (HttpServletRequest ) FreeMarkerWorker.getWrappedObject("request", env); 206 FreeMarkerWorker.getSiteParameters(request, templateRoot); 207 final Map savedValuesUp = new HashMap (); 209 FreeMarkerWorker.saveContextValues(templateRoot, upSaveKeyNames, savedValuesUp); 210 final Map savedValues = new HashMap (); 211 FreeMarkerWorker.overrideWithArgs(templateRoot, args); 212 String contentAssocTypeId = (String ) templateRoot.get("contentAssocTypeId"); 213 List assocTypes = StringUtil.split(contentAssocTypeId, "|"); 217 218 String contentPurposeTypeId = (String ) templateRoot.get("contentPurposeTypeId"); 219 List purposeTypes = StringUtil.split(contentPurposeTypeId, "|"); 220 templateRoot.put("purposeTypes", purposeTypes); 221 Locale locale = (Locale ) templateRoot.get("locale"); 222 if (locale == null) { 223 locale = Locale.getDefault(); 224 templateRoot.put("locale", locale); 225 } 226 227 Map whenMap = new HashMap (); 228 whenMap.put("followWhen", (String ) templateRoot.get("followWhen")); 229 whenMap.put("pickWhen", (String ) templateRoot.get("pickWhen")); 230 whenMap.put("returnBeforePickWhen", (String ) templateRoot.get("returnBeforePickWhen")); 231 whenMap.put("returnAfterPickWhen", (String ) templateRoot.get("returnAfterPickWhen")); 232 templateRoot.put("whenMap", whenMap); 233 234 String fromDateStr = (String ) templateRoot.get("fromDateStr"); 235 Timestamp fromDate = null; 236 if (UtilValidate.isNotEmpty(fromDateStr)) { 237 fromDate = UtilDateTime.toTimestamp(fromDateStr); 238 } 239 if (fromDate == null) 240 fromDate = UtilDateTime.nowTimestamp(); 241 242 final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env); 243 List globalNodeTrail = (List ) templateRoot.get("globalNodeTrail"); 244 String strNullThruDatesOnly = (String ) templateRoot.get("nullThruDatesOnly"); 246 String orderBy = (String ) templateRoot.get("orderBy"); 247 Boolean nullThruDatesOnly = (strNullThruDatesOnly != null && strNullThruDatesOnly.equalsIgnoreCase("true")) ? new Boolean (true) : new Boolean (false); 248 GenericValue val = null; 249 try { 250 val = ContentWorker.getCurrentContent(delegator, globalNodeTrail, userLogin, templateRoot, nullThruDatesOnly, null); 251 } catch (GeneralException e) { 252 Debug.logError(e, module); 253 } 254 final GenericValue view = val; 255 256 if (view != null) { 257 templateRoot.put("contentId", null); 258 templateRoot.put("subContentId", null); 259 260 String contentId = (String ) view.get("contentId"); 261 final String contentIdTo = contentId; 263 264 String thisMapKey = (String ) templateRoot.get("mapKey"); 265 Map results = null; 267 String contentAssocPredicateId = (String ) templateRoot.get("contentAssocPredicateId"); 269 try { 270 results = ContentServicesComplex.getAssocAndContentAndDataResourceCacheMethod(delegator, contentId, thisMapKey, "From", fromDate, null, assocTypes, null, new Boolean (true), contentAssocPredicateId, orderBy); 271 } catch (MiniLangException e2) { 272 throw new RuntimeException (e2.getMessage()); 273 } catch (GenericEntityException e) { 274 throw new RuntimeException (e.getMessage()); 275 } 276 List longList = (List ) results.get("entityList"); 277 String viewSizeStr = (String ) templateRoot.get("viewSize"); 279 if (UtilValidate.isEmpty(viewSizeStr)) 280 viewSizeStr = UtilProperties.getPropertyValue("content", "viewSize"); 281 if (UtilValidate.isEmpty(viewSizeStr)) 282 viewSizeStr = "10"; 283 int viewSize = Integer.parseInt(viewSizeStr); 284 String viewIndexStr = (String ) templateRoot.get("viewIndex"); 285 if (UtilValidate.isEmpty(viewIndexStr)) 286 viewIndexStr = "0"; 287 int viewIndex = Integer.parseInt(viewIndexStr); 288 int lowIndex = viewIndex * viewSize; 289 int listSize = longList.size(); 290 int highIndex = (viewIndex + 1) * viewSize; 291 if (highIndex > listSize) 292 highIndex = listSize; 293 Iterator it = longList.iterator(); 299 List entityList = longList; 301 templateRoot.put("entityList", entityList); 302 templateRoot.put("viewIndex", new Integer (viewIndex)); 303 templateRoot.put("viewSize", new Integer (viewSize)); 304 templateRoot.put("lowIndex", new Integer (lowIndex)); 305 templateRoot.put("highIndex", new Integer (highIndex)); 306 templateRoot.put("listSize", new Integer (listSize)); 307 308 env.setVariable("entityList", FreeMarkerWorker.autoWrap(entityList, env)); 309 env.setVariable("viewIndex", FreeMarkerWorker.autoWrap(new Integer (viewIndex), env)); 310 env.setVariable("viewSize", FreeMarkerWorker.autoWrap(new Integer (viewSize), env)); 311 env.setVariable("lowIndex", FreeMarkerWorker.autoWrap(new Integer (lowIndex), env)); 312 env.setVariable("highIndex", FreeMarkerWorker.autoWrap(new Integer (highIndex), env)); 313 env.setVariable("listSize", FreeMarkerWorker.autoWrap(new Integer (listSize), env)); 314 } 315 316 return new LoopWriter(out) { 317 318 public void write(char cbuf[], int off, int len) { 319 buf.append(cbuf, off, len); 320 } 323 324 public void flush() throws IOException { 325 out.flush(); 326 } 327 328 public int onStart() throws TemplateModelException, IOException { 329 330 if (view == null) { 331 return TransformControl.SKIP_BODY; 332 } 333 334 List globalNodeTrail = (List ) templateRoot.get("globalNodeTrail"); 335 String trailCsv = ContentWorker.nodeTrailToCsv(globalNodeTrail); 336 int viewIndex = ((Integer ) templateRoot.get("viewIndex")).intValue(); 338 int viewSize = ((Integer ) templateRoot.get("viewSize")).intValue(); 339 int listSize = ((Integer ) templateRoot.get("listSize")).intValue(); 340 int lowIndex = viewIndex * viewSize; 341 int highIndex = (viewIndex + 1) * viewSize; 342 if (highIndex > listSize) 343 highIndex = listSize; 344 int outputIndex = 0; 345 Integer highIndexInteger = new Integer (highIndex); 346 Integer lowIndexInteger = new Integer (lowIndex); 347 Integer outputIndexInteger = new Integer (outputIndex); 348 Integer entityIndexInteger = new Integer (0); 349 templateRoot.put("lowIndex", lowIndexInteger); 350 templateRoot.put("highIndex", highIndexInteger); 351 templateRoot.put("outputIndex", outputIndexInteger); 352 templateRoot.put("entityIndex", outputIndexInteger); 353 354 env.setVariable("lowIndex", FreeMarkerWorker.autoWrap(lowIndexInteger, env)); 355 env.setVariable("highIndex", FreeMarkerWorker.autoWrap(highIndexInteger, env)); 356 env.setVariable("outputIndex", FreeMarkerWorker.autoWrap(outputIndexInteger, env)); 357 env.setVariable("entityIndex", FreeMarkerWorker.autoWrap(outputIndexInteger, env)); 358 boolean inProgress = false; 364 if (outputIndex < highIndex) { 365 inProgress = getNextMatchingEntity(templateRoot, delegator, env); 366 } 367 FreeMarkerWorker.saveContextValues(templateRoot, saveKeyNames, savedValues); 368 if (inProgress) { 369 return TransformControl.EVALUATE_BODY; 370 } else { 371 return TransformControl.SKIP_BODY; 372 } 373 } 374 375 public int afterBody() throws TemplateModelException, IOException { 376 FreeMarkerWorker.reloadValues(templateRoot, savedValues, env); 377 List list = (List ) templateRoot.get("globalNodeTrail"); 378 List subList = list.subList(0, list.size() - 1); 379 templateRoot.put("globalNodeTrail", subList); 380 env.setVariable("globalNodeTrail", FreeMarkerWorker.autoWrap(subList, env)); 381 382 int outputIndex = ((Integer ) templateRoot.get("outputIndex")).intValue(); 383 int highIndex = ((Integer ) templateRoot.get("highIndex")).intValue(); 384 Integer highIndexInteger = new Integer (highIndex); 385 env.setVariable("highIndex", FreeMarkerWorker.autoWrap(highIndexInteger, env)); 386 boolean inProgress = false; 388 if (outputIndex < highIndex) { 389 inProgress = getNextMatchingEntity(templateRoot, delegator, env); 390 } 391 392 FreeMarkerWorker.saveContextValues(templateRoot, saveKeyNames, savedValues); 393 if (inProgress) { 394 return TransformControl.REPEAT_EVALUATION; 395 } else { 396 return TransformControl.END_EVALUATION; 397 } 398 } 399 400 public void close() throws IOException { 401 if (view == null) { 402 return; 403 } 404 FreeMarkerWorker.reloadValues(templateRoot, savedValuesUp, env); 405 int outputIndex = ((Integer ) templateRoot.get("outputIndex")).intValue(); 406 int highIndex = ((Integer ) templateRoot.get("highIndex")).intValue(); 408 Integer highIndexInteger = new Integer (highIndex); 409 env.setVariable("highIndex", FreeMarkerWorker.autoWrap(highIndexInteger, env)); 410 if (outputIndex < highIndex) { 412 templateRoot.put("highIndex", new Integer (outputIndex)); 413 templateRoot.put("listSize", new Integer (outputIndex)); 415 } 416 Object highIndexObj = FreeMarkerWorker.getWrappedObject("highIndex", env); 417 String wrappedContent = buf.toString(); 419 out.write(wrappedContent); 420 } 430 }; 431 } 432 } 433 | Popular Tags |