KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: RenderSubContentAsText.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.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
41 import freemarker.core.Environment;
42 import freemarker.template.TemplateTransformModel;
43 //import com.clarkware.profiler.Profiler;
44
/**
45  * RenderSubContentAsText - Freemarker Transform for Content rendering
46  *
47  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
48  * @version $Rev: 5462 $
49  * @since 3.0
50  *
51  * This transform cannot be called recursively (at this time).
52  */

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

124
125         final Map JavaDoc savedValues = new HashMap JavaDoc();
126
127         return new Writer JavaDoc(out) {
128
129             public void write(char cbuf[], int off, int len) {
130             }
131
132             public void flush() throws IOException JavaDoc {
133                 out.flush();
134             }
135
136             public void close() throws IOException JavaDoc {
137                 List JavaDoc globalNodeTrail = (List JavaDoc)templateRoot.get( "globalNodeTrail");
138                 if (Debug.infoOn()) Debug.logInfo("Render close, globalNodeTrail(2a):" + ContentWorker.nodeTrailToCsv(globalNodeTrail), "");
139                 try {
140                     renderSubContent();
141                  //if (Debug.infoOn()) Debug.logInfo("in Render(2), globalNodeTrail ." + getWrapped(env, "globalNodeTrail") , module);
142
} catch (IOException JavaDoc e) {
143                     throw new IOException JavaDoc(e.getMessage());
144                 }
145             }
146
147             public void renderSubContent() throws IOException JavaDoc {
148
149                 //TemplateHashModel dataRoot = env.getDataModel();
150
Timestamp JavaDoc fromDate = UtilDateTime.nowTimestamp();
151                 List JavaDoc passedGlobalNodeTrail = (List JavaDoc)templateRoot.get( "globalNodeTrail");
152                 String JavaDoc editRequestName = (String JavaDoc)templateRoot.get( "editRequestName");
153                  if (Debug.infoOn()) Debug.logInfo("in Render(3), editRequestName ." + editRequestName , module);
154
155                 String JavaDoc mimeTypeId = (String JavaDoc) templateRoot.get( "mimeTypeId");
156                 Locale JavaDoc locale = (Locale JavaDoc) templateRoot.get( "locale");
157                 if (locale == null)
158                     locale = Locale.getDefault();
159
160                 if (UtilValidate.isNotEmpty(editRequestName)) {
161                     String JavaDoc editStyle = getEditStyle();
162                     openEditWrap(out, editStyle);
163                 }
164
165                 FreeMarkerWorker.saveContextValues(templateRoot, saveKeyNames, savedValues);
166                     try {
167                         String JavaDoc txt = ContentWorker.renderSubContentAsTextCache(delegator, thisContentId, thisMapKey, null, templateRoot, locale, mimeTypeId, null, fromDate);
168                         if ("true".equals(xmlEscape))
169                             txt = UtilFormatOut.encodeXmlValue(txt);
170                         
171                         out.write(txt);
172
173                     if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, after renderContentAsTextCache:", module);
174                     } catch (GeneralException e) {
175                         Debug.logError(e, "Error rendering content", module);
176                         throw new IOException JavaDoc("Error rendering thisContentId:" + thisContentId + " msg:" + e.toString());
177                     }
178                 FreeMarkerWorker.reloadValues(templateRoot, savedValues, env);
179                 FreeMarkerWorker.reloadValues(templateRoot, savedValuesUp, env);
180                 if (UtilValidate.isNotEmpty(editRequestName)) {
181                     closeEditWrap(out, editRequestName);
182                 }
183
184                 //if (Debug.infoOn()) Debug.logInfo("in Render(4), globalNodeTrail ." + getWrapped(env, "globalNodeTrail") , module);
185
return;
186             }
187
188             public void openEditWrap(Writer JavaDoc out, String JavaDoc editStyle) throws IOException JavaDoc {
189             
190                 String JavaDoc divStr = "<div class=\"" + editStyle + "\">";
191                 out.write(divStr);
192             }
193
194             public void closeEditWrap(Writer JavaDoc out, String JavaDoc editRequestName) throws IOException JavaDoc {
195            /*
196                 if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, contentId(5):" + templateRoot.get( "contentId"), module);
197                 if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, subContentId(5):" + templateRoot.get( "subContentId"), module);
198                 StringBuffer sb = new StringBuffer();
199                 String fullRequest = editRequestName;
200                 String contentId = null;
201                 String contentIdTo = null;
202                 String contentAssocTypeId = null;
203                 String mapKey = null;
204                 String fromDate = null;
205              
206                 contentIdTo = (String)templateRoot.get("contentId");
207                 contentAssocTypeId = (String)templateRoot.get("contentAssocTypeId");
208                 mapKey = (String)templateRoot.get("mapKey");
209                 fromDate = (String)templateRoot.get("fromDate");
210                 //if (Debug.infoOn()) Debug.logInfo("in Render(0), view ." + view , module);
211                 if (view != null) {
212                     ModelEntity modelEntity = view.getModelEntity();
213                     if (UtilValidate.isEmpty(contentId) && modelEntity.getField("caContentId") != null )
214                         contentId = view.getString("caContentId");
215                     if (UtilValidate.isEmpty(contentId) && modelEntity.getField("contentId") != null )
216                         contentId = view.getString("contentId");
217                     if (UtilValidate.isEmpty(contentIdTo) && modelEntity.getField("caContentIdTo") != null )
218                         contentIdTo = view.getString("caContentIdTo");
219                     if (UtilValidate.isEmpty(contentIdTo) && modelEntity.getField("contentIdTo") != null )
220                         contentIdTo = view.getString("contentIdTo");
221                     if (UtilValidate.isEmpty(contentAssocTypeId) && modelEntity.getField("caContentAssocTypeId") != null )
222                         contentAssocTypeId = view.getString("caContentAssocTypeId");
223                     if (UtilValidate.isEmpty(contentAssocTypeId) && modelEntity.getField("contentAssocTypeId") != null )
224                         contentAssocTypeId = view.getString("contentAssocTypeId");
225                     if (UtilValidate.isEmpty(mapKey) && modelEntity.getField("caMapKey") != null )
226                         mapKey = view.getString("caMapKey");
227                     if (UtilValidate.isEmpty(mapKey) && modelEntity.getField("mapKey") != null )
228                         mapKey = view.getString("mapKey");
229                     if (UtilValidate.isEmpty(fromDate) && modelEntity.getField("caFromDate") != null )
230                         fromDate = view.getString("caFromDate");
231                     if (UtilValidate.isEmpty(fromDate) && modelEntity.getField("fromDate") != null )
232                         fromDate = view.getString("fromDate");
233                 }
234                 if (Debug.infoOn()) Debug.logInfo("in Render(0), contentIdTo ." + contentIdTo , module);
235                 String delim = "?";
236                 if (UtilValidate.isNotEmpty(contentId)) {
237                     fullRequest += delim + "contentId=" + contentId;
238                     delim = "&";
239                 }
240                 if (UtilValidate.isNotEmpty(contentIdTo)) {
241                     fullRequest += delim + "contentIdTo=" + contentIdTo;
242                     delim = "&";
243                 }
244                 if (UtilValidate.isNotEmpty(contentAssocTypeId)) {
245                     fullRequest += delim + "contentAssocTypeId=" + contentAssocTypeId;
246                     delim = "&";
247                 }
248                 if (UtilValidate.isNotEmpty(mapKey)) {
249                     fullRequest += delim + "mapKey=" + mapKey;
250                     delim = "&";
251                 }
252                 if (UtilValidate.isNotEmpty(fromDate)) {
253                     fullRequest += delim + "fromDate=" + fromDate;
254                     delim = "&";
255                 }
256               
257                 if (Debug.infoOn()) Debug.logInfo("in Render(2), contentIdTo ." + contentIdTo , module);
258                 WidgetWorker.appendOfbizUrl(sb, fullRequest, request, response);
259                 String url = sb.toString();
260                 String link = "<a HREF=\"" + url + "\">Edit</a>";
261                 out.write(link);
262                 String divStr = "</div>";
263                 out.write(divStr);
264                 */

265             }
266
267             public String JavaDoc getEditStyle() {
268
269                 String JavaDoc editStyle = (String JavaDoc)templateRoot.get("editStyle");
270                 if (UtilValidate.isEmpty(editStyle)) {
271                     editStyle = UtilProperties.getPropertyValue("content", "defaultEditStyle");
272                 }
273                 if (UtilValidate.isEmpty(editStyle)) {
274                     editStyle = "buttontext";
275                 }
276                 return editStyle;
277             }
278         };
279         
280     }
281
282 }
283
Popular Tags