KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > content > webapp > ftl > TraverseSubContentCacheTransform


1 /*
2  * $Id: TraverseSubContentCacheTransform.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
13  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
14  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15  *
16  */

17 package org.ofbiz.content.webapp.ftl;
18
19 import java.io.IOException JavaDoc;
20 import java.io.Writer JavaDoc;
21 import java.sql.Timestamp JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
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 //import com.clarkware.profiler.Profiler;
44
/**
45  * TraverseSubContentCacheTransform - Freemarker Transform for URLs (links)
46  *
47  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
48  * @version $Rev: 5462 $
49  * @since 3.0
50  */

51 public class TraverseSubContentCacheTransform implements TemplateTransformModel {
52
53     public static final String JavaDoc module = TraverseSubContentCacheTransform.class.getName();
54     public static final String JavaDoc [] upSaveKeyNames = {"globalNodeTrail"};
55     public static final String JavaDoc [] saveKeyNames = {"contentId", "subContentId", "subDataResourceTypeId", "mimeTypeId", "whenMap", "locale", "wrapTemplateId", "encloseWrapText", "nullThruDatesOnly", "globalNodeTrail"};
56
57     /**
58      * A wrapper for the FreeMarkerWorker version.
59      */

60     public static Object JavaDoc getWrappedObject(String JavaDoc varName, Environment env) {
61         return FreeMarkerWorker.getWrappedObject(varName, env);
62     }
63
64     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Environment env) {
65         return FreeMarkerWorker.getArg(args, key, env);
66     }
67
68     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Map JavaDoc ctx) {
69         return FreeMarkerWorker.getArg(args, key, ctx);
70     }
71
72     public Writer JavaDoc getWriter(final Writer JavaDoc out, Map JavaDoc args) {
73         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
74         final Environment env = Environment.getCurrentEnvironment();
75         //final Map templateRoot = (Map) FreeMarkerWorker.getWrappedObject("context", env);
76
final Map JavaDoc templateRoot = FreeMarkerWorker.createEnvironmentMap(env);
77         //FreeMarkerWorker.convertContext(templateRoot);
78
final Map JavaDoc savedValuesUp = new HashMap JavaDoc();
79         FreeMarkerWorker.saveContextValues(templateRoot, upSaveKeyNames, savedValuesUp);
80         final Map JavaDoc savedValues = new HashMap JavaDoc();
81         FreeMarkerWorker.overrideWithArgs(templateRoot, args);
82         String JavaDoc startContentAssocTypeId = (String JavaDoc)templateRoot.get( "contentAssocTypeId");
83             //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContentCache, startContentAssocTypeId:" + startContentAssocTypeId, module);
84
final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env);
85         final HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) FreeMarkerWorker.getWrappedObject("request", env);
86         FreeMarkerWorker.getSiteParameters(request, templateRoot);
87         final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env);
88         Object JavaDoc obj = templateRoot.get("globalNodeTrail");
89         List JavaDoc globalNodeTrail = (List JavaDoc)obj;
90         //List globalNodeTrail = (List)templateRoot.get("globalNodeTrail");
91
String JavaDoc csvTrail = ContentWorker.nodeTrailToCsv(globalNodeTrail);
92         //if (Debug.infoOn()) Debug.logInfo("in Traverse(0), csvTrail:"+csvTrail,module);
93
String JavaDoc strNullThruDatesOnly = (String JavaDoc)templateRoot.get("nullThruDatesOnly");
94         String JavaDoc contentAssocPredicateId = (String JavaDoc)templateRoot.get("contentAssocPredicateId");
95         Boolean JavaDoc nullThruDatesOnly = (strNullThruDatesOnly != null && strNullThruDatesOnly.equalsIgnoreCase("true")) ? new Boolean JavaDoc(true) :new Boolean JavaDoc(false);
96         GenericValue val = null;
97         try {
98             // getCurrentContent puts the "current" node on the end of globalNodeTrail.
99
// It may have already been there, but getCurrentContent will compare its contentId
100
// to values in templateRoot.
101
val = ContentWorker.getCurrentContent(delegator, globalNodeTrail, userLogin, templateRoot, nullThruDatesOnly, contentAssocPredicateId);
102         } catch(GeneralException e) {
103             throw new RuntimeException JavaDoc("Error getting current content. " + e.toString());
104         }
105         final GenericValue view = val;
106
107
108         final Map JavaDoc traverseContext = new HashMap JavaDoc();
109         traverseContext.put("delegator", delegator);
110         Map JavaDoc whenMap = new HashMap JavaDoc();
111         whenMap.put("followWhen", (String JavaDoc)templateRoot.get( "followWhen"));
112         whenMap.put("pickWhen", (String JavaDoc)templateRoot.get( "pickWhen"));
113         whenMap.put("returnBeforePickWhen", (String JavaDoc)templateRoot.get( "returnBeforePickWhen"));
114         whenMap.put("returnAfterPickWhen", (String JavaDoc)templateRoot.get( "returnAfterPickWhen"));
115         traverseContext.put("whenMap", whenMap);
116         env.setVariable("whenMap", FreeMarkerWorker.autoWrap(whenMap, env));
117         String JavaDoc fromDateStr = (String JavaDoc)templateRoot.get( "fromDateStr");
118         String JavaDoc thruDateStr = (String JavaDoc)templateRoot.get( "thruDateStr");
119         Timestamp JavaDoc fromDate = null;
120         if (fromDateStr != null && fromDateStr.length() > 0) {
121             fromDate = UtilDateTime.toTimestamp(fromDateStr);
122         }
123         traverseContext.put("fromDate", fromDate);
124         Timestamp JavaDoc thruDate = null;
125         if (thruDateStr != null && thruDateStr.length() > 0) {
126             thruDate = UtilDateTime.toTimestamp(thruDateStr);
127         }
128         traverseContext.put("thruDate", thruDate);
129         //if (UtilValidate.isEmpty(startContentAssocTypeId)) {
130
//throw new RuntimeException("contentAssocTypeId is empty.");
131
//}
132
traverseContext.put("contentAssocTypeId", startContentAssocTypeId);
133         String JavaDoc direction = (String JavaDoc)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                 //StringBuffer ctxBuf = (StringBuffer) templateContext.get("buf");
144
//ctxBuf.append(cbuf, off, len);
145
buf.append(cbuf, off, len);
146             }
147
148             public void flush() throws IOException JavaDoc {
149                 out.flush();
150             }
151
152             public int onStart() throws TemplateModelException, IOException JavaDoc {
153                 //templateContext.put("buf", new StringBuffer());
154
List JavaDoc nodeTrail = null;
155                 Map JavaDoc node = null;
156                 GenericValue subContentDataResourceView = null;
157                 List JavaDoc globalNodeTrail = (List JavaDoc)templateRoot.get("globalNodeTrail");
158                 String JavaDoc trailCsv = ContentWorker.nodeTrailToCsv(globalNodeTrail);
159                 //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContentCache, onStart, trailCsv(1):" + trailCsv , module);
160
if (globalNodeTrail.size() > 0) {
161                     int sz = globalNodeTrail.size() ;
162                     nodeTrail = new ArrayList JavaDoc();
163                     //nodeTrail = passedGlobalNodeTrail.subList(sz - 1, sz);
164
node = (Map JavaDoc)globalNodeTrail.get(sz - 1);
165                     //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContentCache, onStart, node(1):" + node , module);
166
Boolean JavaDoc checkedObj = (Boolean JavaDoc)node.get("checked");
167                     Map JavaDoc whenMap = (Map JavaDoc)templateRoot.get("whenMap");
168                     //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContentCache, whenMap(2):" + whenMap , module);
169
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 JavaDoc("Empty node trail entries");
175                 }
176
177                 Boolean JavaDoc isReturnBeforePickBool = (Boolean JavaDoc)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                 //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContentCache, onStart, node(2):" + node , module);
184
nodeTrail.add(node);
185                 traverseContext.put("nodeTrail", nodeTrail);
186                 Boolean JavaDoc isPickBool = (Boolean JavaDoc)node.get("isPick");
187                 Boolean JavaDoc isFollowBool = (Boolean JavaDoc)node.get("isFollow");
188                 //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContentCache, onStart, isPickBool(1):" + isPickBool + " isFollowBool:" + isFollowBool, module);
189
boolean isPick = true;
190                 if ((isPickBool == null || !isPickBool.booleanValue())
191                    && (isFollowBool != null && isFollowBool.booleanValue())) {
192                     isPick = ContentWorker.traverseSubContent(traverseContext);
193                     //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContentCache, onStart, isPick(2):" + isPick, module);
194
}
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 JavaDoc {
205
206
207                 FreeMarkerWorker.reloadValues(templateRoot, savedValues, env);
208                 List JavaDoc globalNodeTrail = (List JavaDoc)templateRoot.get("globalNodeTrail");
209     //if (Debug.infoOn()) Debug.logInfo("populateContext, globalNodeTrail(2a):" + FreeMarkerWorker.nodeTrailToCsv(globalNodeTrail), "");
210
List JavaDoc nodeTrail = (List JavaDoc)traverseContext.get("nodeTrail");
211                 //List savedGlobalNodeTrail = (List)savedValues.get("globalNodeTrail");
212
//templateRoot.put("globalNodeTrail", savedGlobalNodeTrail);
213
boolean inProgress = ContentWorker.traverseSubContent(traverseContext);
214                 int sz = nodeTrail.size();
215                 if (inProgress) {
216                     populateContext(traverseContext, templateRoot);
217                     FreeMarkerWorker.saveContextValues(templateRoot, saveKeyNames, savedValues);
218                     //globalNodeTrail = (List)templateRoot.get("globalNodeTrail");
219
//globalNodeTrail.addAll(nodeTrail);
220
return TransformControl.REPEAT_EVALUATION;
221                 } else
222                     return TransformControl.END_EVALUATION;
223             }
224
225             public void close() throws IOException JavaDoc {
226
227                 FreeMarkerWorker.reloadValues(templateRoot, savedValuesUp, env);
228                 String JavaDoc wrappedContent = buf.toString();
229                 out.write(wrappedContent);
230                 //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContent, wrappedContent:" + wrappedContent, module);
231
}
232
233
234             public void populateContext(Map JavaDoc traverseContext, Map JavaDoc templateContext) {
235
236                 List JavaDoc nodeTrail = (List JavaDoc)traverseContext.get("nodeTrail");
237     //if (Debug.infoOn()) Debug.logInfo("populateContext, nodeTrail csv(a):" + FreeMarkerWorker.nodeTrailToCsv((List)nodeTrail), "");
238
int sz = nodeTrail.size();
239                 Map JavaDoc node = (Map JavaDoc)nodeTrail.get(sz - 1);
240                 GenericValue content = (GenericValue)node.get("value");
241                 String JavaDoc contentId = (String JavaDoc)node.get("contentId");
242                 String JavaDoc subContentId = (String JavaDoc)node.get("subContentId");
243                 String JavaDoc contentAssocTypeId = (String JavaDoc)node.get("contentAssocTypeId");
244                 envWrap("contentAssocTypeId", contentAssocTypeId);
245                 envWrap("contentId", contentId);
246                 envWrap("content", content);
247                 String JavaDoc mapKey = (String JavaDoc)node.get("mapKey");
248                 envWrap("mapKey", mapKey);
249                 envWrap("subContentDataResourceView", null);
250                 List JavaDoc globalNodeTrail = (List JavaDoc)templateContext.get("globalNodeTrail");
251                 String JavaDoc contentIdEnd = null;
252                 String JavaDoc contentIdStart = null;
253                 if (globalNodeTrail != null) {
254                     Map JavaDoc ndEnd = (Map JavaDoc)globalNodeTrail.get(globalNodeTrail.size() - 1);
255                     contentIdEnd = (String JavaDoc)ndEnd.get("contentId");
256                     Map JavaDoc ndStart = (Map JavaDoc)nodeTrail.get(0);
257                     contentIdStart = (String JavaDoc)ndStart.get("contentId");
258                 } else {
259                     globalNodeTrail = new ArrayList JavaDoc();
260                 }
261                 //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContentCache, populateContext, contentIdEnd(1):" + contentIdEnd + " contentIdStart:" + contentIdStart + " equals:" + (contentIdStart.equals(contentIdEnd)), module);
262
boolean bIdEnd = UtilValidate.isNotEmpty(contentIdEnd);
263                 boolean bIdStart = UtilValidate.isNotEmpty(contentIdStart);
264                 boolean bEquals = contentIdStart.equals(contentIdEnd);
265                 //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContentCache, populateContext, contentIdEnd(1):" + bIdEnd + " contentIdStart:" + bIdStart + " equals:" + bEquals, module);
266
//if (Debug.infoOn()) Debug.logInfo("populateContext, globalNodeTrail(1a):" + FreeMarkerWorker.nodeTrailToCsv(globalNodeTrail), "");
267
if (bIdEnd && bIdStart && bEquals) {
268                     List JavaDoc subList = nodeTrail.subList(1, sz);
269                     globalNodeTrail.addAll(subList);
270                 } else {
271                     globalNodeTrail.addAll(nodeTrail);
272                 }
273     //if (Debug.infoOn()) Debug.logInfo("populateContext, globalNodeTrail(1b):" + FreeMarkerWorker.nodeTrailToCsv(globalNodeTrail), "");
274
int indentSz = globalNodeTrail.size();
275                 envWrap("indent", new Integer JavaDoc(indentSz));
276                 String JavaDoc trailCsv = ContentWorker.nodeTrailToCsv(globalNodeTrail);
277                 //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContentCache, populateCtx, trailCsv(2):" + trailCsv , module);
278
envWrap("nodeTrailCsv", trailCsv);
279                 envWrap("globalNodeTrail", globalNodeTrail);
280                 return;
281             }
282
283             public void envWrap(String JavaDoc varName, Object JavaDoc obj) {
284
285                 templateRoot.put(varName, obj);
286                 env.setVariable(varName, FreeMarkerWorker.autoWrap(obj, env));
287             }
288
289         };
290     }
291 }
292
Popular Tags