KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: EditRenderSubContentCacheTransform.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.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Locale JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Set JavaDoc;
34
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36
37 import org.ofbiz.base.util.Debug;
38 import org.ofbiz.base.util.GeneralException;
39 import org.ofbiz.base.util.UtilValidate;
40 import org.ofbiz.base.util.template.FreeMarkerWorker;
41 import org.ofbiz.content.content.ContentWorker;
42 import org.ofbiz.entity.GenericDelegator;
43 import org.ofbiz.entity.GenericValue;
44
45 import freemarker.core.Environment;
46 import freemarker.template.TemplateTransformModel;
47
48 //import com.clarkware.profiler.Profiler;
49
/**
50  * EditRenderSubContentCacheTransform - Freemarker Transform for URLs (links)
51  *
52  * This is an interactive FreeMarker tranform that allows the user to modify the contents that are placed within it.
53  *
54  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
55  * @version $Rev: 5462 $
56  * @since 3.0
57  */

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

66     public static Object JavaDoc getWrappedObject(String JavaDoc varName, Environment env) {
67         return FreeMarkerWorker.getWrappedObject(varName, env);
68     }
69
70     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Environment env) {
71         return FreeMarkerWorker.getArg(args, key, env);
72     }
73
74     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Map JavaDoc ctx) {
75         return FreeMarkerWorker.getArg(args, key, ctx);
76     }
77
78     public Writer JavaDoc getWriter(final Writer JavaDoc out, Map JavaDoc args) {
79         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
80         final Environment env = Environment.getCurrentEnvironment();
81         final Map JavaDoc templateCtx = (Map JavaDoc) FreeMarkerWorker.getWrappedObject("context", env);
82         final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env);
83         final HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) FreeMarkerWorker.getWrappedObject("request", env);
84         FreeMarkerWorker.getSiteParameters(request, templateCtx);
85         FreeMarkerWorker.overrideWithArgs(templateCtx, args);
86         final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env);
87         List JavaDoc trail = (List JavaDoc)templateCtx.get("globalNodeTrail");
88         String JavaDoc contentAssocPredicateId = (String JavaDoc)templateCtx.get("contentAssocPredicateId");
89         String JavaDoc strNullThruDatesOnly = (String JavaDoc)templateCtx.get("nullThruDatesOnly");
90         Boolean JavaDoc nullThruDatesOnly = (strNullThruDatesOnly != null && strNullThruDatesOnly.equalsIgnoreCase("true")) ? new Boolean JavaDoc(true) :new Boolean JavaDoc(false);
91         GenericValue val = null;
92         try {
93             val = ContentWorker.getCurrentContent(delegator, trail, userLogin, templateCtx, nullThruDatesOnly, contentAssocPredicateId);
94         } catch(GeneralException e) {
95             throw new RuntimeException JavaDoc("Error getting current content. " + e.toString());
96         }
97         final GenericValue view = val;
98
99         String JavaDoc dataResourceId = null;
100         try {
101             dataResourceId = (String JavaDoc) view.get("drDataResourceId");
102         } catch (Exception JavaDoc e) {
103             dataResourceId = (String JavaDoc) view.get("dataResourceId");
104         }
105         String JavaDoc subContentIdSub = (String JavaDoc) view.get("contentId");
106         // This order is taken so that the dataResourceType can be overridden in the transform arguments.
107
String JavaDoc subDataResourceTypeId = (String JavaDoc)templateCtx.get("subDataResourceTypeId");
108         if (UtilValidate.isEmpty(subDataResourceTypeId)) {
109             try {
110                 subDataResourceTypeId = (String JavaDoc) view.get("drDataResourceTypeId");
111             } catch (Exception JavaDoc e) {
112                 // view may be "Content"
113
}
114             // TODO: If this value is still empty then it is probably necessary to get a value from
115
// the parent context. But it will already have one and it is the same context that is
116
// being passed.
117
}
118         // This order is taken so that the mimeType can be overridden in the transform arguments.
119
String JavaDoc mimeTypeId = ContentWorker.getMimeTypeId(delegator, view, templateCtx);
120         templateCtx.put("drDataResourceId", dataResourceId);
121         templateCtx.put("mimeTypeId", mimeTypeId);
122         templateCtx.put("dataResourceId", dataResourceId);
123         templateCtx.put("subContentIdSub", subContentIdSub);
124         templateCtx.put("subDataResourceTypeId", subDataResourceTypeId);
125         final Map JavaDoc savedValues = new HashMap JavaDoc();
126         FreeMarkerWorker.saveContextValues(templateCtx, saveKeyNames, savedValues);
127
128         return new Writer JavaDoc(out) {
129
130             public void write(char cbuf[], int off, int len) {
131                 buf.append(cbuf, off, len);
132             }
133
134             public void flush() throws IOException JavaDoc {
135                 out.flush();
136             }
137
138             public void close() throws IOException JavaDoc {
139                 FreeMarkerWorker.reloadValues(templateCtx, savedValues, env);
140                 String JavaDoc wrappedContent = buf.toString();
141                 String JavaDoc editTemplate = (String JavaDoc)templateCtx.get("editTemplate");
142 // if (editTemplate != null && editTemplate.equalsIgnoreCase("true")) {
143
String JavaDoc wrapTemplateId = (String JavaDoc)templateCtx.get("wrapTemplateId");
144                     if (UtilValidate.isNotEmpty(wrapTemplateId)) {
145                         templateCtx.put("wrappedContent", wrappedContent);
146                         
147                     //Map templateRoot = FreeMarkerWorker.createEnvironmentMap(env);
148
Map JavaDoc templateRoot = null;
149                     Map JavaDoc templateRootTemplate = (Map JavaDoc)templateCtx.get("templateRootTemplate");
150                     if (templateRootTemplate == null) {
151                         Map JavaDoc templateRootTmp = FreeMarkerWorker.createEnvironmentMap(env);
152                         templateRoot = new HashMap JavaDoc(templateRootTmp);
153                         templateCtx.put("templateRootTemplate", templateRootTmp);
154                     } else {
155                         templateRoot = new HashMap JavaDoc(templateRootTemplate);
156                     }
157                         
158                         templateRoot.put("context", templateCtx);
159         if (Debug.verboseOn()) {
160             Set JavaDoc kySet = templateCtx.keySet();
161             Iterator JavaDoc it = kySet.iterator();
162             while (it.hasNext()) {
163                 Object JavaDoc ky = it.next();
164                 Object JavaDoc val = templateCtx.get(ky);
165             }
166         }
167                         
168                         String JavaDoc mimeTypeId = (String JavaDoc)templateCtx.get("mimeTypeId");
169                         Locale JavaDoc locale = null;
170                         try {
171                            //if (Debug.infoOn()) Debug.logInfo("in Edit(0), before calling renderContentAsTextCache, wrapTemplateId: ." + wrapTemplateId , module);
172
ContentWorker.renderContentAsTextCache(delegator, wrapTemplateId, out, templateRoot, null, locale, mimeTypeId);
173                            //if (Debug.infoOn()) Debug.logInfo("in Edit(0), after calling renderContentAsTextCache, wrapTemplateId: ." + wrapTemplateId , module);
174
} catch (IOException JavaDoc e) {
175                             Debug.logError(e, "Error rendering content" + e.getMessage(), module);
176                             throw new IOException JavaDoc("Error rendering content" + e.toString());
177                         } catch (GeneralException e2) {
178                             Debug.logError(e2, "Error rendering content" + e2.getMessage(), module);
179                             throw new IOException JavaDoc("Error rendering content" + e2.toString());
180                         }
181                         
182                 } else {
183                     out.write(wrappedContent);
184                 }
185             }
186         };
187     }
188 }
189
Popular Tags