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 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.UtilValidate; 35 import org.ofbiz.base.util.template.FreeMarkerWorker; 36 import org.ofbiz.content.content.ContentServicesComplex; 37 import org.ofbiz.content.content.ContentWorker; 38 import org.ofbiz.entity.GenericDelegator; 39 import org.ofbiz.entity.GenericEntityException; 40 import org.ofbiz.entity.GenericValue; 41 import org.ofbiz.minilang.MiniLangException; 42 import org.ofbiz.webapp.ftl.LoopWriter; 43 44 import freemarker.core.Environment; 45 import freemarker.template.TemplateModelException; 46 import freemarker.template.TemplateTransformModel; 47 import freemarker.template.TransformControl; 48 49 56 public class LimitedSubContentCacheTransform implements TemplateTransformModel { 57 58 public static final String module = LimitedSubContentCacheTransform.class.getName(); 59 60 public static final String [] upSaveKeyNames = {"globalNodeTrail"}; 61 public static final String [] saveKeyNames = {"contentId", "subContentId", "entityList", "entityIndex", "subDataResourceTypeId", "mimeTypeId", "whenMap", "locale", "entityList", "viewSize", "viewIndex", "highIndex", "lowIndex", "listSize", "wrapTemplateId", "encloseWrapText", "nullThruDatesOnly", "globalNodeTrail", "outputIndex"}; 62 63 66 public static Object getWrappedObject(String varName, Environment env) { 67 return FreeMarkerWorker.getWrappedObject(varName, env); 68 } 69 70 public static String getArg(Map args, String key, Environment env) { 71 return FreeMarkerWorker.getArg(args, key, env); 72 } 73 74 public static String getArg(Map args, String key, Map ctx) { 75 return FreeMarkerWorker.getArg(args, key, ctx); 76 } 77 78 79 public Writer getWriter(final Writer out, Map args) { 80 final StringBuffer buf = new StringBuffer (); 82 final Environment env = Environment.getCurrentEnvironment(); 83 final Map templateRoot = FreeMarkerWorker.createEnvironmentMap(env); 85 final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env); 87 final HttpServletRequest request = (HttpServletRequest ) FreeMarkerWorker.getWrappedObject("request", env); 88 FreeMarkerWorker.getSiteParameters(request, templateRoot); 89 final Map savedValuesUp = new HashMap (); 91 FreeMarkerWorker.saveContextValues(templateRoot, upSaveKeyNames, savedValuesUp); 92 final Map savedValues = new HashMap (); 93 FreeMarkerWorker.overrideWithArgs(templateRoot, args); 94 String contentAssocTypeId = (String ) templateRoot.get("contentAssocTypeId"); 95 final Map pickedEntityIds = new HashMap (); 96 List assocTypes = StringUtil.split(contentAssocTypeId, "|"); 100 101 String contentPurposeTypeId = (String ) templateRoot.get("contentPurposeTypeId"); 102 List purposeTypes = StringUtil.split(contentPurposeTypeId, "|"); 103 templateRoot.put("purposeTypes", purposeTypes); 104 Locale locale = (Locale ) templateRoot.get("locale"); 105 if (locale == null) { 106 locale = Locale.getDefault(); 107 templateRoot.put("locale", locale); 108 } 109 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 templateRoot.put("whenMap", whenMap); 116 117 String fromDateStr = (String ) templateRoot.get("fromDateStr"); 118 Timestamp fromDate = null; 119 if (UtilValidate.isNotEmpty(fromDateStr)) { 120 fromDate = UtilDateTime.toTimestamp(fromDateStr); 121 } 122 if (fromDate == null) 123 fromDate = UtilDateTime.nowTimestamp(); 124 125 String limitSize = (String ) templateRoot.get("limitSize"); 126 final int returnLimit = Integer.parseInt(limitSize); 127 String limitMode = (String ) templateRoot.get("limitMode"); 129 final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env); 130 List globalNodeTrail = (List ) templateRoot.get("globalNodeTrail"); 131 String strNullThruDatesOnly = (String ) templateRoot.get("nullThruDatesOnly"); 132 String orderBy = (String ) templateRoot.get("orderBy"); 133 Boolean nullThruDatesOnly = (strNullThruDatesOnly != null && strNullThruDatesOnly.equalsIgnoreCase("true")) ? new Boolean (true) : new Boolean (false); 134 String contentId = (String ) templateRoot.get("subContentId"); 135 136 templateRoot.put("contentId", null); 137 templateRoot.put("subContentId", null); 138 139 final String contentIdTo = contentId; 140 141 Map results = null; 142 String contentAssocPredicateId = (String ) templateRoot.get("contentAssocPredicateId"); 144 try { 145 results = ContentServicesComplex.getAssocAndContentAndDataResourceCacheMethod(delegator, contentId, null, "From", fromDate, null, assocTypes, null, new Boolean (true), contentAssocPredicateId, orderBy); 146 } catch (MiniLangException e2) { 147 throw new RuntimeException (e2.getMessage()); 148 } catch (GenericEntityException e) { 149 throw new RuntimeException (e.getMessage()); 150 } 151 List longList = (List ) results.get("entityList"); 152 templateRoot.put("entityList", longList); 153 155 return new LoopWriter(out) { 156 157 public void write(char cbuf[], int off, int len) { 158 buf.append(cbuf, off, len); 159 } 162 163 public void flush() throws IOException { 164 out.flush(); 165 } 166 167 public int onStart() throws TemplateModelException, IOException { 168 169 List globalNodeTrail = (List ) templateRoot.get("globalNodeTrail"); 170 String trailCsv = ContentWorker.nodeTrailToCsv(globalNodeTrail); 171 boolean inProgress = false; 172 if (pickedEntityIds.size() < returnLimit) { 175 inProgress = getNextMatchingEntity(templateRoot, delegator, env); 176 } 177 FreeMarkerWorker.saveContextValues(templateRoot, saveKeyNames, savedValues); 178 if (inProgress) { 179 return TransformControl.EVALUATE_BODY; 180 } else { 181 return TransformControl.SKIP_BODY; 182 } 183 } 184 185 public int afterBody() throws TemplateModelException, IOException { 186 FreeMarkerWorker.reloadValues(templateRoot, savedValues, env); 187 List list = (List ) templateRoot.get("globalNodeTrail"); 188 List subList = list.subList(0, list.size() - 1); 189 templateRoot.put("globalNodeTrail", subList); 190 env.setVariable("globalNodeTrail", FreeMarkerWorker.autoWrap(subList, env)); 191 192 boolean inProgress = false; 196 if (pickedEntityIds.size() < returnLimit) { 197 inProgress = getNextMatchingEntity(templateRoot, delegator, env); 198 } 199 200 FreeMarkerWorker.saveContextValues(templateRoot, saveKeyNames, savedValues); 201 if (inProgress) { 202 return TransformControl.REPEAT_EVALUATION; 203 } else { 204 return TransformControl.END_EVALUATION; 205 } 206 } 207 208 public void close() throws IOException { 209 FreeMarkerWorker.reloadValues(templateRoot, savedValuesUp, env); 210 String wrappedContent = buf.toString(); 211 out.write(wrappedContent); 212 } 221 222 public boolean prepCtx(GenericDelegator delegator, Map ctx, Environment env, GenericValue view) throws GeneralException { 223 224 String dataResourceId = (String ) view.get("drDataResourceId"); 225 String subContentIdSub = (String ) view.get("contentId"); 226 String subDataResourceTypeId = (String ) ctx.get("subDataResourceTypeId"); 228 if (UtilValidate.isEmpty(subDataResourceTypeId)) { 229 subDataResourceTypeId = (String ) view.get("drDataResourceTypeId"); 230 } 234 235 String mimeTypeId = ContentWorker.getMimeTypeId(delegator, view, ctx); 236 237 238 Map trailNode = ContentWorker.makeNode(view); 239 Map whenMap = (Map ) ctx.get("whenMap"); 240 Locale locale = (Locale ) ctx.get("locale"); 241 if (locale == null) { 242 locale = Locale.getDefault(); 243 } 244 GenericValue assocContent = null; 245 ContentWorker.checkConditions(delegator, trailNode, assocContent, whenMap); 246 Boolean isReturnBeforeObj = (Boolean ) trailNode.get("isReturnBefore"); 247 Boolean isReturnAfterObj = (Boolean ) trailNode.get("isReturnAfter"); 248 Boolean isPickObj = (Boolean ) trailNode.get("isPick"); 249 Boolean isFollowObj = (Boolean ) trailNode.get("isFollow"); 250 if ((isReturnBeforeObj == null || !isReturnBeforeObj.booleanValue()) && ((isPickObj != null && 252 isPickObj.booleanValue()) || (isFollowObj != null && isFollowObj.booleanValue()))) { 253 List globalNodeTrail = (List ) ctx.get("globalNodeTrail"); 254 if (globalNodeTrail == null) { 255 globalNodeTrail = new ArrayList (); 256 } 257 globalNodeTrail.add(trailNode); 258 ctx.put("globalNodeTrail", globalNodeTrail); 259 String csvTrail = ContentWorker.nodeTrailToCsv(globalNodeTrail); 260 ctx.put("nodeTrailCsv", csvTrail); 261 int indentSz = globalNodeTrail.size(); 263 ctx.put("indent", new Integer (indentSz)); 264 265 ctx.put("subDataResourceTypeId", subDataResourceTypeId); 266 ctx.put("mimeTypeId", mimeTypeId); 267 ctx.put("subContentId", subContentIdSub); 268 ctx.put("content", view); 269 270 env.setVariable("subDataResourceTypeId", FreeMarkerWorker.autoWrap(subDataResourceTypeId, env)); 271 env.setVariable("indent", FreeMarkerWorker.autoWrap(new Integer (indentSz), env)); 272 env.setVariable("nodeTrailCsv", FreeMarkerWorker.autoWrap(csvTrail, env)); 273 env.setVariable("globalNodeTrail", FreeMarkerWorker.autoWrap(globalNodeTrail, env)); 274 env.setVariable("content", FreeMarkerWorker.autoWrap(view, env)); 275 env.setVariable("mimeTypeId", FreeMarkerWorker.autoWrap(mimeTypeId, env)); 276 env.setVariable("subContentId", FreeMarkerWorker.autoWrap(subContentIdSub, env)); 277 return true; 278 } else { 279 return false; 280 } 281 } 282 283 public GenericValue getRandomEntity() { 284 285 GenericValue pickEntity = null; 286 List lst = (List ) templateRoot.get("entityList"); 287 if (Debug.verboseOn()) Debug.logVerbose("in limited, lst:" + lst, ""); 288 289 while (pickEntity == null && lst.size() > 0) { 290 double randomValue = Math.random(); 291 int idx = (int) (lst.size() * randomValue); 293 pickEntity = (GenericValue) lst.get(idx); 295 String pickEntityId = pickEntity.getString("contentId"); 296 if (pickedEntityIds.get(pickEntityId) == null) { 297 pickedEntityIds.put(pickEntityId, pickEntity); 298 lst.remove(idx); 299 } else { 300 pickEntity = null; 301 } 302 } 303 return pickEntity; 304 } 305 306 public boolean getNextMatchingEntity(Map templateRoot, GenericDelegator delegator, Environment env) throws IOException { 307 boolean matchFound = false; 308 GenericValue pickEntity = getRandomEntity(); 309 310 while (pickEntity != null && !matchFound) { 311 try { 312 matchFound = prepCtx(delegator, templateRoot, env, pickEntity); 313 } catch (GeneralException e) { 314 throw new IOException (e.getMessage()); 315 } 316 if (!matchFound) 317 pickEntity = getRandomEntity(); 318 } 319 return matchFound; 320 } 321 }; 322 } 323 } 324 | Popular Tags |