KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: InjectNodeTrailCsvTransform.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 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.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25
26 import org.ofbiz.base.util.Debug;
27 import org.ofbiz.base.util.UtilValidate;
28 import org.ofbiz.base.util.template.FreeMarkerWorker;
29 import org.ofbiz.content.content.ContentWorker;
30 import org.ofbiz.entity.GenericDelegator;
31 import org.ofbiz.entity.GenericEntityException;
32 import org.ofbiz.entity.GenericValue;
33 import org.ofbiz.webapp.ftl.LoopWriter;
34
35 import freemarker.core.Environment;
36 import freemarker.template.TemplateModelException;
37 import freemarker.template.TemplateTransformModel;
38 import freemarker.template.TransformControl;
39
40 /**
41  * InjectNodeTrailCsvTransform - Freemarker Transform for URLs (links)
42  *
43  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
44  * @version $Rev: 5462 $
45  * @since 3.0
46  */

47 public class InjectNodeTrailCsvTransform implements TemplateTransformModel {
48
49     public static final String JavaDoc module = InjectNodeTrailCsvTransform.class.getName();
50
51     public static final String JavaDoc [] saveKeyNames = {"nodeTrailCsv","globalNodeTrail", "nodeTrail"};
52     public static final String JavaDoc [] removeKeyNames = {"nodeTrailCsv"};
53
54     /**
55      * A wrapper for the FreeMarkerWorker version.
56      */

57     public static Object JavaDoc getWrappedObject(String JavaDoc varName, Environment env) {
58         return FreeMarkerWorker.getWrappedObject(varName, env);
59     }
60
61     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Environment env) {
62         return FreeMarkerWorker.getArg(args, key, env);
63     }
64
65     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Map JavaDoc ctx) {
66         return FreeMarkerWorker.getArg(args, key, ctx);
67     }
68
69
70     public Writer JavaDoc getWriter(final Writer JavaDoc out, Map JavaDoc args) {
71         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
72         final Environment env = Environment.getCurrentEnvironment();
73         final Map JavaDoc templateCtx = (Map JavaDoc) FreeMarkerWorker.getWrappedObject("context", env);
74         //FreeMarkerWorker.convertContext(templateCtx);
75
final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env);
76         final HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) FreeMarkerWorker.getWrappedObject("request", env);
77         final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env);
78         FreeMarkerWorker.getSiteParameters(request, templateCtx);
79         FreeMarkerWorker.overrideWithArgs(templateCtx, args);
80
81         return new LoopWriter(out) {
82
83             final String JavaDoc passedCsv = (String JavaDoc)templateCtx.get("nodeTrailCsv");
84
85             public void write(char cbuf[], int off, int len) {
86                 buf.append(cbuf, off, len);
87             }
88
89             public void flush() throws IOException JavaDoc {
90                 out.flush();
91             }
92
93             public int onStart() throws TemplateModelException, IOException JavaDoc {
94                 String JavaDoc csvTrail = null;
95
96                 List JavaDoc trail = (List JavaDoc)templateCtx.get("globalNodeTrail");
97
98                 if (Debug.infoOn()) Debug.logInfo("in InjectNodeTrailCsv(0), trail:"+trail,module);
99                 // This will build a nodeTrail if none exists
100
// Maybe only contentId or subContentId are passed in
101
//GenericValue currentValue = getCurrentContent( delegator, trail, userLogin, templateCtx, nullThruDatesOnly, contentAssocPredicateId);
102
String JavaDoc redo = (String JavaDoc)templateCtx.get("redo");
103
104                 if (trail == null || trail.size() == 0 || (redo != null && redo.equalsIgnoreCase("true"))) {
105                     String JavaDoc thisContentId = null;
106                     String JavaDoc subContentId = (String JavaDoc)templateCtx.get("subContentId");
107                     if (Debug.infoOn()) Debug.logInfo("in InjectNodeTrailCsv(0), subContentId:"+subContentId,module);
108                     String JavaDoc contentId = (String JavaDoc)templateCtx.get("contentId");
109                     if (Debug.infoOn()) Debug.logInfo("in InjectNodeTrailCsv(0), contentId:"+contentId,module);
110                     String JavaDoc contentAssocTypeId = (String JavaDoc)templateCtx.get("contentAssocTypeId");
111                     if (Debug.infoOn()) Debug.logInfo("in InjectNodeTrailCsv(0), contentAssocTypeId:"+contentAssocTypeId,module);
112                     try {
113                         if (UtilValidate.isNotEmpty(subContentId)) {
114                             csvTrail = ContentWorker.getContentAncestryNodeTrailCsv(delegator, subContentId, contentAssocTypeId, "to");
115                             if (UtilValidate.isNotEmpty(csvTrail))
116                                 csvTrail += ",";
117                             csvTrail += subContentId;
118                         } else if (UtilValidate.isNotEmpty(contentId)) {
119                             csvTrail = ContentWorker.getContentAncestryNodeTrailCsv(delegator, contentId, contentAssocTypeId, "to");
120                             if (UtilValidate.isNotEmpty(csvTrail))
121                                 csvTrail += ",";
122                             csvTrail += contentId;
123                         }
124                     } catch (GenericEntityException e) {
125                         throw new RuntimeException JavaDoc("Error getting current content. " + e.toString());
126                     }
127                     if (Debug.infoOn()) Debug.logInfo("in InjectNodeTrailCsv(0), csvTrail:"+csvTrail,module);
128                 } else {
129                     // Build nodeTrail if one does not exist
130
//if (Debug.infoOn()) Debug.logInfo("in InjectNodeTrailCsv, trail:"+trail,module);
131
//if (Debug.infoOn()) Debug.logInfo("in InjectNodeTrailCsv, passedCsv:"+passedCsv,module);
132
if (UtilValidate.isNotEmpty(passedCsv)) {
133                         csvTrail = passedCsv;
134                         int lastComma = passedCsv.lastIndexOf(",");
135                         String JavaDoc lastPassedContentId = null;
136                         if (lastComma >= 0) {
137                             lastPassedContentId = passedCsv.substring(lastComma + 1);
138                         } else {
139                             lastPassedContentId = passedCsv;
140                         }
141     
142                         if (UtilValidate.isNotEmpty(lastPassedContentId)) {
143                             if (trail != null && trail.size() > 0) {
144                                 Map JavaDoc nd = (Map JavaDoc)trail.get(0);
145                                 String JavaDoc firstTrailContentId = (String JavaDoc)nd.get("contentId");
146                                 if (UtilValidate.isNotEmpty(firstTrailContentId)
147                                     && UtilValidate.isNotEmpty(lastPassedContentId)
148                                     && firstTrailContentId.equals(lastPassedContentId) ) {
149                                     csvTrail += "," + ContentWorker.nodeTrailToCsv(trail.subList(1, trail.size()));
150                                 } else {
151                                     csvTrail += "," + ContentWorker.nodeTrailToCsv(trail);
152                                 }
153                             }
154                         }
155                     } else {
156                         csvTrail = ContentWorker.nodeTrailToCsv(trail);
157                     }
158                 }
159                 //if (Debug.infoOn()) Debug.logInfo("in InjectNodeTrailCsv, csvTrail:"+csvTrail,module);
160
templateCtx.put("nodeTrailCsv", csvTrail);
161                 return TransformControl.EVALUATE_BODY;
162             }
163
164
165             public void close() throws IOException JavaDoc {
166                 templateCtx.put("nodeTrailCsv", passedCsv);
167                 String JavaDoc wrappedContent = buf.toString();
168                 out.write(wrappedContent);
169             }
170         };
171     }
172 }
173
Popular Tags