KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: WrapSubContentCacheTransform.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
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.content.webapp.ftl;
25
26 import java.io.IOException JavaDoc;
27 import java.io.Writer JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Locale JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34
35 import org.ofbiz.base.util.Debug;
36 import org.ofbiz.base.util.GeneralException;
37 import org.ofbiz.base.util.UtilValidate;
38 import org.ofbiz.base.util.template.FreeMarkerWorker;
39 import org.ofbiz.content.content.ContentWorker;
40 import org.ofbiz.entity.GenericDelegator;
41 import org.ofbiz.entity.GenericValue;
42
43 import freemarker.core.Environment;
44 import freemarker.template.TemplateTransformModel;
45
46 //import com.clarkware.profiler.Profiler;
47
/**
48  * WrapSubContentCacheTransform - Freemarker Transform for URLs (links)
49  *
50  * This is an interactive FreeMarker tranform that allows the user to modify the contents that are placed within it.
51  *
52  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
53  * @version $Rev: 5462 $
54  * @since 3.0
55  */

56 public class WrapSubContentCacheTransform implements TemplateTransformModel {
57
58     public static final String JavaDoc module = WrapSubContentCacheTransform.class.getName();
59     public static final String JavaDoc [] upSaveKeyNames = {"globalNodeTrail"};
60     public static final String JavaDoc [] saveKeyNames = {"contentId", "subContentId", "subDataResourceTypeId", "mimeTypeId", "whenMap", "locale", "wrapTemplateId", "encloseWrapText", "nullThruDatesOnly"};
61     
62     /**
63      * A wrapper for the FreeMarkerWorker version.
64      */

65     public static Object JavaDoc getWrappedObject(String JavaDoc varName, Environment env) {
66         return FreeMarkerWorker.getWrappedObject(varName, env);
67     }
68
69     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Environment env) {
70         return FreeMarkerWorker.getArg(args, key, env);
71     }
72
73     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Map JavaDoc ctx) {
74         return FreeMarkerWorker.getArg(args, key, ctx);
75     }
76
77     public Writer JavaDoc getWriter(final Writer JavaDoc out, Map JavaDoc args) {
78         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
79         final Environment env = Environment.getCurrentEnvironment();
80         Map JavaDoc envContext = (Map JavaDoc) FreeMarkerWorker.getWrappedObject("context", env);
81         final Map JavaDoc templateCtx;
82         if (envContext == null) {
83             templateCtx = FreeMarkerWorker.createEnvironmentMap(env);
84         } else {
85             templateCtx = envContext;
86         }
87         final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env);
88         final HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) FreeMarkerWorker.getWrappedObject("request", env);
89         FreeMarkerWorker.getSiteParameters(request, templateCtx);
90         final Map JavaDoc savedValuesUp = new HashMap JavaDoc();
91         FreeMarkerWorker.saveContextValues(templateCtx, upSaveKeyNames, savedValuesUp);
92         //if (Debug.infoOn()) Debug.logInfo("in Wrap(0a), savedValuesUp ." + savedValuesUp , module);
93
FreeMarkerWorker.overrideWithArgs(templateCtx, args);
94         //if (Debug.infoOn()) Debug.logInfo("in Wrap(0b), savedValuesUp ." + savedValuesUp , module);
95
final String JavaDoc wrapTemplateId = (String JavaDoc)templateCtx.get("wrapTemplateId");
96         //if (Debug.infoOn()) Debug.logInfo("in WrapSubContent, wrapTemplateId(1):" + wrapTemplateId, module);
97
final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env);
98         List JavaDoc trail = (List JavaDoc)templateCtx.get("globalNodeTrail");
99                 //if (Debug.infoOn()) Debug.logInfo("in WrapSubContent, trail(0):" + trail, "");
100
String JavaDoc contentAssocPredicateId = (String JavaDoc)templateCtx.get("contentAssocPredicateId");
101         String JavaDoc strNullThruDatesOnly = (String JavaDoc)templateCtx.get("nullThruDatesOnly");
102         Boolean JavaDoc nullThruDatesOnly = (strNullThruDatesOnly != null && strNullThruDatesOnly.equalsIgnoreCase("true")) ? new Boolean JavaDoc(true) :new Boolean JavaDoc(false);
103         GenericValue val = null;
104         try {
105             val = ContentWorker.getCurrentContent(delegator, trail, userLogin, templateCtx, nullThruDatesOnly, contentAssocPredicateId);
106         } catch(GeneralException e) {
107             throw new RuntimeException JavaDoc("Error getting current content. " + e.toString());
108         }
109                 //if (Debug.infoOn()) Debug.logInfo("in WrapSubContent, trail(1):" + trail, "");
110
final GenericValue view = val;
111
112         String JavaDoc dataResourceId = null;
113         try {
114             dataResourceId = (String JavaDoc) view.get("drDataResourceId");
115         } catch (Exception JavaDoc e) {
116             dataResourceId = (String JavaDoc) view.get("dataResourceId");
117         }
118         String JavaDoc subContentIdSub = (String JavaDoc) view.get("contentId");
119         // This order is taken so that the dataResourceType can be overridden in the transform arguments.
120
String JavaDoc subDataResourceTypeId = (String JavaDoc)templateCtx.get("subDataResourceTypeId");
121         if (UtilValidate.isEmpty(subDataResourceTypeId)) {
122             try {
123                 subDataResourceTypeId = (String JavaDoc) view.get("drDataResourceTypeId");
124             } catch (Exception JavaDoc e) {
125                 // view may be "Content"
126
}
127             // TODO: If this value is still empty then it is probably necessary to get a value from
128
// the parent context. But it will already have one and it is the same context that is
129
// being passed.
130
}
131         final Map JavaDoc savedValues = new HashMap JavaDoc();
132         FreeMarkerWorker.saveContextValues(templateCtx, saveKeyNames, savedValues);
133         //if (Debug.infoOn()) Debug.logInfo("in Wrap(1), savedValues ." + savedValues , module);
134
// This order is taken so that the mimeType can be overridden in the transform arguments.
135
String JavaDoc mimeTypeId = ContentWorker.getMimeTypeId(delegator, view, templateCtx);
136         templateCtx.put("drDataResourceId", dataResourceId);
137         templateCtx.put("mimeTypeId", mimeTypeId);
138         templateCtx.put("dataResourceId", dataResourceId);
139         templateCtx.put("subContentIdSub", subContentIdSub);
140         templateCtx.put("subDataResourceTypeId", subDataResourceTypeId);
141         templateCtx.put("wrapTemplateId", null); // Not something to pass on
142

143         return new Writer JavaDoc(out) {
144
145             public void write(char cbuf[], int off, int len) {
146                 buf.append(cbuf, off, len);
147             }
148
149             public void flush() throws IOException JavaDoc {
150                 out.flush();
151             }
152
153             public void close() throws IOException JavaDoc {
154                 FreeMarkerWorker.reloadValues(templateCtx, savedValues, env);
155                 //if (Debug.infoOn()) Debug.logInfo("in Wrap(2), savedValues ." + savedValues , module);
156
String JavaDoc wrappedContent = buf.toString();
157                 
158                 //if (view != null && Debug.infoOn()) Debug.logInfo("in WrapSubContent, view(2):" + view.get("contentId"), module);
159
//if (Debug.infoOn()) Debug.logInfo("in WrapSubContent, wrappedContent:" + wrappedContent, module);
160
//if (Debug.infoOn()) Debug.logInfo("in WrapSubContent, wrapTemplateId(2):" + wrapTemplateId, module);
161
if (UtilValidate.isNotEmpty(wrapTemplateId)) {
162                     templateCtx.put("wrappedContent", wrappedContent);
163                     
164                     //Map templateRoot = FreeMarkerWorker.createEnvironmentMap(env);
165
Map JavaDoc templateRoot = null;
166                     Map JavaDoc templateRootTemplate = (Map JavaDoc)templateCtx.get("templateRootTemplate");
167                     if (templateRootTemplate == null) {
168                         Map JavaDoc templateRootTmp = FreeMarkerWorker.createEnvironmentMap(env);
169                         templateRoot = new HashMap JavaDoc(templateRootTmp);
170                         templateCtx.put("templateRootTemplate", templateRootTmp);
171                     } else {
172                         templateRoot = new HashMap JavaDoc(templateRootTemplate);
173                     }
174                     
175                     templateRoot.put("context", templateCtx);
176                     
177                     String JavaDoc mimeTypeId = (String JavaDoc)templateCtx.get("mimeTypeId");
178                     Locale JavaDoc locale = null;
179                     try {
180                         //if (Debug.infoOn()) Debug.logInfo("in Edit(0), before calling renderContentAsText ." , module);
181
ContentWorker.renderContentAsTextCache(delegator, wrapTemplateId, out, templateRoot, null, locale, mimeTypeId);
182                         //if (Debug.infoOn()) Debug.logInfo("in Edit(0), after calling renderContentAsText ." , module);
183
} catch (IOException JavaDoc e) {
184                         Debug.logError(e, "Error rendering content" + e.getMessage(), module);
185                         throw new IOException JavaDoc("Error rendering content" + e.toString());
186                     } catch (GeneralException e2) {
187                         Debug.logError(e2, "Error rendering content" + e2.getMessage(), module);
188                         throw new IOException JavaDoc("Error rendering content" + e2.toString());
189                     }
190                         
191                 FreeMarkerWorker.reloadValues(templateCtx, savedValuesUp, env);
192                         //if (Debug.infoOn()) Debug.logInfo("in Wrap(2), savedValuesUp ." + savedValuesUp , module);
193
}
194             }
195         };
196     }
197 }
198
Popular Tags