KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: TraverseSubContentTransform.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.Locale JavaDoc;
26 import java.util.Map JavaDoc;
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 /**
46  * TraverseSubContentTransform - Freemarker Transform for URLs (links)
47  *
48  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
49  * @version $Rev: 5462 $
50  * @since 3.0
51  */

52 public class TraverseSubContentTransform implements TemplateTransformModel {
53
54     public static final String JavaDoc module = TraverseSubContentTransform.class.getName();
55     public static final String JavaDoc [] saveKeyNames = {"contentId", "subContentId", "mimeType", "subContentDataResourceView", "wrapTemplateId", "templateContentId", "pickWhen", "followWhen", "returnAfterPickWhen", "returnBeforePickWhen", "indent"};
56     public static final String JavaDoc [] removeKeyNames = {"templateContentId", "subDataResourceTypeId", "mapKey", "wrappedFTL", "nodeTrail"};
57
58     /**
59      * A wrapper for the FreeMarkerWorker version.
60      */

61     public static Object JavaDoc getWrappedObject(String JavaDoc varName, Environment env) {
62         return FreeMarkerWorker.getWrappedObject(varName, env);
63     }
64
65     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Environment env) {
66         return FreeMarkerWorker.getArg(args, key, env);
67     }
68
69     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Map JavaDoc ctx) {
70         return FreeMarkerWorker.getArg(args, key, ctx);
71     }
72
73     public Writer JavaDoc getWriter(final Writer JavaDoc out, Map JavaDoc args) {
74         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
75         final Environment env = Environment.getCurrentEnvironment();
76         final Map JavaDoc templateCtx = (Map JavaDoc) FreeMarkerWorker.getWrappedObject("context", env);
77         //FreeMarkerWorker.convertContext(templateCtx);
78
final Map JavaDoc savedValues = FreeMarkerWorker.saveValues(templateCtx, saveKeyNames);
79         FreeMarkerWorker.overrideWithArgs(templateCtx, args);
80         final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env);
81 /*
82         final String editTemplate = getArg(args, "editTemplate", ctx);
83         final String wrapTemplateId = getArg(args, "wrapTemplateId", ctx);
84         //final String mapKey = getArg(args, "mapKey", ctx);
85         final String templateContentId = getArg(args, "templateContentId", ctx);
86         final String subDataResourceTypeId = getArg(args, "subDataResourceTypeId", ctx);
87         final String contentId = getArg(args, "contentId", ctx);
88         final String subContentId = getArg(args, "subContentId", ctx);
89         final String rootDir = getArg(args, "rootDir", ctx);
90         final String webSiteId = getArg(args, "webSiteId", ctx);
91         final String https = getArg(args, "https", ctx);
92         final String viewSize = getArg(args, "viewSize", ctx);
93         final String viewIndex = getArg(args, "viewIndex", ctx);
94         final String listSize = getArg(args, "listSize", ctx);
95         final String highIndex = getArg(args, "highIndex", ctx);
96         final String lowIndex = getArg(args, "lowIndex", ctx);
97         final String queryString = getArg(args, "queryString", ctx);
98         final Locale locale = (Locale) FreeMarkerWorker.getWrappedObject("locale", env);
99         final String mimeTypeId = getArg(args, "mimeTypeId", ctx);
100 */

101         //final LocalDispatcher dispatcher = (LocalDispatcher) FreeMarkerWorker.getWrappedObject("dispatcher", env);
102
//final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env);
103
GenericValue view = (GenericValue) FreeMarkerWorker.getWrappedObject("subContentDataResourceView", env);
104         final Integer JavaDoc indent = (templateCtx.get("indent") == null) ? new Integer JavaDoc(0) : (Integer JavaDoc)templateCtx.get("indent");
105        
106         String JavaDoc contentId = (String JavaDoc)templateCtx.get("contentId");
107         String JavaDoc subContentId = (String JavaDoc)templateCtx.get("subContentId");
108         if (view == null) {
109             String JavaDoc 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 JavaDoc(e.getMessage());
120                 }
121             }
122         }
123
124         final GenericValue subContentDataResourceView = view;
125
126         final Map JavaDoc traverseContext = new HashMap JavaDoc();
127         traverseContext.put("delegator", delegator);
128         Map JavaDoc whenMap = new HashMap JavaDoc();
129         whenMap.put("followWhen", (String JavaDoc)templateCtx.get( "followWhen"));
130         whenMap.put("pickWhen", (String JavaDoc)templateCtx.get( "pickWhen"));
131         whenMap.put("returnBeforePickWhen", (String JavaDoc)templateCtx.get( "returnBeforePickWhen"));
132         whenMap.put("returnAfterPickWhen", (String JavaDoc)templateCtx.get( "returnAfterPickWhen"));
133         traverseContext.put("whenMap", whenMap);
134         String JavaDoc fromDateStr = (String JavaDoc)templateCtx.get( "fromDateStr");
135         String JavaDoc thruDateStr = (String JavaDoc)templateCtx.get( "thruDateStr");
136         Timestamp JavaDoc fromDate = null;
137         if (fromDateStr != null && fromDateStr.length() > 0) {
138             fromDate = UtilDateTime.toTimestamp(fromDateStr);
139         }
140         traverseContext.put("fromDate", fromDate);
141         Timestamp JavaDoc thruDate = null;
142         if (thruDateStr != null && thruDateStr.length() > 0) {
143             thruDate = UtilDateTime.toTimestamp(thruDateStr);
144         }
145         traverseContext.put("thruDate", thruDate);
146         String JavaDoc startContentAssocTypeId = (String JavaDoc)templateCtx.get( "contentAssocTypeId");
147         if (startContentAssocTypeId != null)
148             startContentAssocTypeId = "SUB_CONTENT";
149         traverseContext.put("contentAssocTypeId", startContentAssocTypeId);
150         String JavaDoc direction = (String JavaDoc)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                 //StringBuffer ctxBuf = (StringBuffer) templateContext.get("buf");
160
//ctxBuf.append(cbuf, off, len);
161
buf.append(cbuf, off, len);
162             }
163
164             public void flush() throws IOException JavaDoc {
165                 out.flush();
166             }
167
168             public int onStart() throws TemplateModelException, IOException JavaDoc {
169                 //templateContext.put("buf", new StringBuffer());
170
List JavaDoc nodeTrail = new ArrayList JavaDoc();
171                 traverseContext.put("nodeTrail", nodeTrail);
172                 GenericValue content = null;
173 /*
174                 if (UtilValidate.isNotEmpty(contentId)) {
175                     try {
176                         content = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", contentId));
177                     } catch(GenericEntityException e){
178                         // TODO: Not sure what to put here.
179                         throw new RuntimeException(e.getMessage());
180                     }
181                 }
182 */

183                 Map JavaDoc 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 JavaDoc)traverseContext.get("contentAssocTypeId"));
189                 rootNode.put("isPick", new Boolean JavaDoc(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 JavaDoc {
205                 //out.write(buf.toString());
206
//buf.setLength(0);
207
//templateContext.put("buf", new StringBuffer());
208
List JavaDoc nodeTrail = (List JavaDoc)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 JavaDoc {
221
222                 String JavaDoc wrappedFTL = buf.toString();
223                 String JavaDoc encloseWrappedText = (String JavaDoc)templateCtx.get("encloseWrappedText");
224                 if (UtilValidate.isEmpty(encloseWrappedText) || encloseWrappedText.equalsIgnoreCase("false")) {
225
226                     out.write(wrappedFTL);
227                     wrappedFTL = null; // So it won't get written again below.
228
}
229                 String JavaDoc wrapTemplateId = (String JavaDoc)templateCtx.get("wrapTemplateId");
230                 if (UtilValidate.isNotEmpty(wrapTemplateId)) {
231                     templateCtx.put("wrappedFTL", wrappedFTL);
232                     
233                     Map JavaDoc templateRoot = FreeMarkerWorker.createEnvironmentMap(env);
234                     
235 /*
236                     templateRoot.put("viewSize", viewSize);
237                     templateRoot.put("viewIndex", viewIndex);
238                     templateRoot.put("listSize", listSize);
239                     templateRoot.put("highIndex", highIndex);
240                     templateRoot.put("lowIndex", lowIndex);
241                     templateRoot.put("queryString", queryString);
242                     templateRoot.put("wrapDataResourceTypeId", subDataResourceTypeId);
243                     templateRoot.put("wrapContentIdTo", contentId);
244                     templateRoot.put("wrapMimeTypeId", mimeTypeId);
245                     //templateRoot.put("wrapMapKey", mapKey);
246                     
247 */

248                     templateRoot.put("context", templateCtx);
249                     String JavaDoc mimeTypeId = (String JavaDoc) templateCtx.get("mimeTypeId");
250                     Locale JavaDoc locale = (Locale JavaDoc) 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 JavaDoc("Error rendering content" + e.toString());
258                     }
259 /*
260                     Map resultsCtx = (Map) FreeMarkerWorker.getWrappedObject("context", env);
261                     templateContext.put("contentId", contentId);
262                     templateContext.put("locale", locale);
263                     templateContext.put("mapKey", null);
264                     templateContext.put("subContentId", null);
265                     templateContext.put("templateContentId", null);
266                     templateContext.put("subDataResourceTypeId", null);
267                     templateContext.put("mimeTypeId", null);
268 */

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 JavaDoc contentAssocTypeId) {
280         
281                 boolean isPick = false;
282                 Map JavaDoc assocContext = new HashMap JavaDoc();
283                 if (UtilValidate.isEmpty(contentAssocTypeId))
284                     contentAssocTypeId = "";
285                 assocContext.put("contentAssocTypeId", contentAssocTypeId);
286                 //assocContext.put("contentTypeId", assocValue.get("contentTypeId") );
287
String JavaDoc assocRelation = null;
288                 String JavaDoc thisDirection = (String JavaDoc)templateCtx.get("direction");
289                 String JavaDoc thisContentId = (String JavaDoc)templateCtx.get("thisContentId");
290                 String JavaDoc 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 JavaDoc purposes = ContentWorker.getPurposes(thisContent);
302                 assocContext.put("purposes", purposes);
303                 List JavaDoc contentTypeAncestry = new ArrayList JavaDoc();
304                 String JavaDoc contentTypeId = (String JavaDoc)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 JavaDoc whenMap = (Map JavaDoc)traverseContext.get("whenMap");
312                 String JavaDoc pickWhen = (String JavaDoc)whenMap.get("pickWhen");
313                 List JavaDoc nodeTrail = (List JavaDoc)traverseContext.get("nodeTrail");
314                 int indentSz = indent.intValue() + nodeTrail.size();
315                 assocContext.put("indentObj", new Integer JavaDoc(indentSz));
316                 isPick = ContentWorker.checkWhen(assocContext, (String JavaDoc)whenMap.get("pickWhen"));
317                 return isPick;
318            }
319
320
321             public void populateContext(Map JavaDoc traverseContext, Map JavaDoc templateContext) {
322
323                 List JavaDoc nodeTrail = (List JavaDoc)traverseContext.get("nodeTrail");
324                 int sz = nodeTrail.size();
325                 Map JavaDoc node = (Map JavaDoc)nodeTrail.get(sz - 1);
326                 GenericValue content = (GenericValue)node.get("value");
327                 String JavaDoc contentId = (String JavaDoc)node.get("contentId");
328                 String JavaDoc subContentId = (String JavaDoc)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 JavaDoc(indentSz));
333                 if (sz >= 2) {
334                     Map JavaDoc parentNode = (Map JavaDoc)nodeTrail.get(sz - 2);
335                     GenericValue parentContent = (GenericValue)parentNode.get("value");
336                     String JavaDoc parentContentId = (String JavaDoc)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