KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: RenderContentAsText.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.sql.Timestamp JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Locale JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29
30 import org.ofbiz.base.util.Debug;
31 import org.ofbiz.base.util.GeneralException;
32 import org.ofbiz.base.util.UtilDateTime;
33 import org.ofbiz.base.util.UtilFormatOut;
34 import org.ofbiz.base.util.UtilProperties;
35 import org.ofbiz.base.util.UtilValidate;
36 import org.ofbiz.base.util.template.FreeMarkerWorker;
37 import org.ofbiz.content.content.ContentWorker;
38 import org.ofbiz.entity.GenericDelegator;
39 import org.ofbiz.entity.GenericValue;
40 import org.ofbiz.widget.WidgetWorker;
41
42 import freemarker.core.Environment;
43 import freemarker.template.TemplateTransformModel;
44 //import com.clarkware.profiler.Profiler;
45
/**
46  * RenderContentAsText - Freemarker Transform for Content rendering
47  *
48  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
49  * @version $Rev: 5462 $
50  * @since 3.0
51  *
52  * This transform cannot be called recursively (at this time).
53  */

54 public class RenderContentAsText implements TemplateTransformModel {
55
56     public static final String JavaDoc module = RenderContentAsText.class.getName();
57     public static final String JavaDoc [] upSaveKeyNames = {"globalNodeTrail"};
58     public static final String JavaDoc [] saveKeyNames = {"contentId", "subContentId", "subDataResourceTypeId", "mimeTypeId", "whenMap", "locale", "wrapTemplateId", "encloseWrapText", "nullThruDatesOnly", "globalNodeTrail"};
59
60     public Writer JavaDoc getWriter(final Writer JavaDoc out, Map JavaDoc args) {
61         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
62         final Environment env = Environment.getCurrentEnvironment();
63         //final Map templateCtx = (Map) FreeMarkerWorker.getWrappedObject("context", env);
64
//final Map templateCtx = new HashMap();
65
final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env);
66         final HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) FreeMarkerWorker.getWrappedObject("request", env);
67         final HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) FreeMarkerWorker.getWrappedObject("response", env);
68         final Map JavaDoc templateRoot = FreeMarkerWorker.createEnvironmentMap(env);
69                 if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, contentId(0):" + templateRoot.get( "contentId"), module);
70         FreeMarkerWorker.getSiteParameters(request, templateRoot);
71         final Map JavaDoc savedValuesUp = new HashMap JavaDoc();
72         FreeMarkerWorker.saveContextValues(templateRoot, upSaveKeyNames, savedValuesUp);
73         FreeMarkerWorker.overrideWithArgs(templateRoot, args);
74                 if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, contentId(2):" + templateRoot.get( "contentId"), module);
75         final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env);
76         List JavaDoc trail = (List JavaDoc)templateRoot.get( "globalNodeTrail");
77         //if (Debug.infoOn()) Debug.logInfo("in Render(0), globalNodeTrail ." + trail , module);
78
String JavaDoc contentAssocPredicateId = (String JavaDoc)templateRoot.get( "contentAssocPredicateId");
79         String JavaDoc strNullThruDatesOnly = (String JavaDoc)templateRoot.get( "nullThruDatesOnly");
80         Boolean JavaDoc nullThruDatesOnly = (strNullThruDatesOnly != null && strNullThruDatesOnly.equalsIgnoreCase("true")) ? new Boolean JavaDoc(true) :new Boolean JavaDoc(false);
81         final String JavaDoc thisContentId = (String JavaDoc)templateRoot.get("contentId");
82         final String JavaDoc xmlEscape = (String JavaDoc)templateRoot.get("xmlEscape");
83         final boolean directAssocMode = UtilValidate.isNotEmpty(thisContentId) ? true : false;
84         if (Debug.infoOn()) Debug.logInfo("in Render(0), directAssocMode ." + directAssocMode , module);
85         /*
86         if (Debug.infoOn()) Debug.logInfo("in Render(0), thisSubContentId ." + thisSubContentId , module);
87         String thisSubContentId = (String)templateRoot.get("subContentId");
88         GenericValue val = null;
89         try {
90             val = FreeMarkerWorker.getCurrentContent(delegator, trail, userLogin, templateRoot, nullThruDatesOnly, contentAssocPredicateId);
91         } catch(GeneralException e) {
92             throw new RuntimeException("Error getting current content. " + e.toString());
93         }
94         final GenericValue view = val;
95
96         String dataResourceId = null;
97         String subContentIdSub = null;
98         if (view != null) {
99             try {
100                 dataResourceId = (String) view.get("drDataResourceId");
101             } catch (Exception e) {
102                 dataResourceId = (String) view.get("dataResourceId");
103             }
104             subContentIdSub = (String) view.get("contentId");
105         }
106         // This order is taken so that the dataResourceType can be overridden in the transform arguments.
107         String subDataResourceTypeId = (String)templateRoot.get( "subDataResourceTypeId");
108         if (UtilValidate.isEmpty(subDataResourceTypeId)) {
109             try {
110                 subDataResourceTypeId = (String) view.get("drDataResourceTypeId");
111             } catch (Exception 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         String mimeTypeId = FreeMarkerWorker.getMimeTypeId(delegator, view, templateRoot);
119         templateRoot.put( "drDataResourceId", dataResourceId);
120         templateRoot.put( "mimeTypeId", mimeTypeId);
121         templateRoot.put( "dataResourceId", dataResourceId);
122         templateRoot.put( "subContentId", subContentIdSub);
123         templateRoot.put( "subDataResourceTypeId", subDataResourceTypeId);
124         */

125
126         final Map JavaDoc savedValues = new HashMap JavaDoc();
127
128         return new Writer JavaDoc(out) {
129
130             public void write(char cbuf[], int off, int len) {
131             }
132
133             public void flush() throws IOException JavaDoc {
134                 out.flush();
135             }
136
137             public void close() throws IOException JavaDoc {
138                 List JavaDoc globalNodeTrail = (List JavaDoc)templateRoot.get( "globalNodeTrail");
139                 if (Debug.infoOn()) Debug.logInfo("Render close, globalNodeTrail(2a):" + ContentWorker.nodeTrailToCsv(globalNodeTrail), "");
140                 try {
141                     renderSubContent();
142                  //if (Debug.infoOn()) Debug.logInfo("in Render(2), globalNodeTrail ." + getWrapped(env, "globalNodeTrail") , module);
143
} catch (IOException JavaDoc e) {
144                     throw new IOException JavaDoc(e.getMessage());
145                 }
146             }
147
148             public void renderSubContent() throws IOException JavaDoc {
149
150                 //TemplateHashModel dataRoot = env.getDataModel();
151
Timestamp JavaDoc fromDate = UtilDateTime.nowTimestamp();
152                 List JavaDoc passedGlobalNodeTrail = (List JavaDoc)templateRoot.get( "globalNodeTrail");
153                 String JavaDoc editRequestName = (String JavaDoc)templateRoot.get( "editRequestName");
154                  if (Debug.infoOn()) Debug.logInfo("in Render(3), editRequestName ." + editRequestName , module);
155                  /*
156                 GenericValue thisView = null;
157                 if (view != null) {
158                     thisView = view;
159                 } else if (passedGlobalNodeTrail.size() > 0) {
160                     Map map = (Map)passedGlobalNodeTrail.get(passedGlobalNodeTrail.size() - 1);
161                     if (Debug.infoOn()) Debug.logInfo("in Render(3), map ." + map , module);
162                     if (map != null)
163                         thisView = (GenericValue)map.get("value");
164                 }
165                 if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, subContentId:" + templateRoot.get( "subContentId"), module);
166                 if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, contentId:" + templateRoot.get( "contentId"), module);
167                 */

168
169
170
171                 String JavaDoc mimeTypeId = (String JavaDoc) templateRoot.get( "mimeTypeId");
172                 Locale JavaDoc locale = (Locale JavaDoc) templateRoot.get( "locale");
173                 if (locale == null)
174                     locale = Locale.getDefault();
175
176                 if (UtilValidate.isNotEmpty(editRequestName)) {
177                     String JavaDoc editStyle = getEditStyle();
178                     openEditWrap(out, editStyle);
179                 }
180
181                 if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, contentId(2):" + templateRoot.get( "contentId"), module);
182                 if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, subContentId(2):" + templateRoot.get( "subContentId"), module);
183                 FreeMarkerWorker.saveContextValues(templateRoot, saveKeyNames, savedValues);
184                 //if (thisView != null) {
185
try {
186                         String JavaDoc txt = ContentWorker.renderContentAsTextCache(delegator, thisContentId, templateRoot, null, locale, mimeTypeId);
187                         if ("true".equals(xmlEscape))
188                             txt = UtilFormatOut.encodeXmlValue(txt);
189                         
190                         out.write(txt);
191                         
192                     if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, after renderContentAsTextCache:", module);
193                     } catch (GeneralException e) {
194                         Debug.logError(e, "Error rendering content", module);
195                         throw new IOException JavaDoc("Error rendering thisContentId:" + thisContentId + " msg:" + e.toString());
196                     }
197                 //}
198
FreeMarkerWorker.reloadValues(templateRoot, savedValuesUp, env);
199                 FreeMarkerWorker.reloadValues(templateRoot, savedValues, env);
200                 if (UtilValidate.isNotEmpty(editRequestName)) {
201                     closeEditWrap(out, editRequestName);
202                 }
203
204                 //if (Debug.infoOn()) Debug.logInfo("in Render(4), globalNodeTrail ." + getWrapped(env, "globalNodeTrail") , module);
205
return;
206             }
207
208             public void openEditWrap(Writer JavaDoc out, String JavaDoc editStyle) throws IOException JavaDoc {
209             
210                 String JavaDoc divStr = "<div class=\"" + editStyle + "\">";
211                 out.write(divStr);
212             }
213
214             public void closeEditWrap(Writer JavaDoc out, String JavaDoc editRequestName) throws IOException JavaDoc {
215             
216                 if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, contentId(3):" + templateRoot.get( "contentId"), module);
217                 if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, subContentId(3):" + templateRoot.get( "subContentId"), module);
218                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
219                 String JavaDoc fullRequest = editRequestName;
220                 String JavaDoc contentId = null;
221                 contentId = (String JavaDoc)templateRoot.get("subContentId");
222                 String JavaDoc delim = "?";
223                 if (UtilValidate.isNotEmpty(contentId)) {
224                     fullRequest += delim + "contentId=" + contentId;
225                     delim = "&";
226                 }
227               
228                 WidgetWorker.appendOfbizUrl(sb, fullRequest, request, response);
229                 String JavaDoc url = sb.toString();
230                 String JavaDoc link = "<a HREF=\"" + url + "\">Edit</a>";
231                 out.write(link);
232                 String JavaDoc divStr = "</div>";
233                 out.write(divStr);
234             }
235
236             public String JavaDoc getEditStyle() {
237
238                 String JavaDoc editStyle = (String JavaDoc)templateRoot.get("editStyle");
239                 if (UtilValidate.isEmpty(editStyle)) {
240                     editStyle = UtilProperties.getPropertyValue("content", "defaultEditStyle");
241                 }
242                 if (UtilValidate.isEmpty(editStyle)) {
243                     editStyle = "buttontext";
244                 }
245                 return editStyle;
246             }
247         };
248         
249     }
250
251 }
252
Popular Tags