KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: RenderSubContentCacheTransform.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.UtilProperties;
34 import org.ofbiz.base.util.UtilValidate;
35 import org.ofbiz.base.util.template.FreeMarkerWorker;
36 import org.ofbiz.content.content.ContentWorker;
37 import org.ofbiz.entity.GenericDelegator;
38 import org.ofbiz.entity.GenericValue;
39 import org.ofbiz.entity.model.ModelEntity;
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 /**
47  * RenderSubContentCacheTransform - Freemarker Transform for Content rendering
48  *
49  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
50  * @version $Rev: 5462 $
51  * @since 3.0
52  *
53  * This transform cannot be called recursively (at this time).
54  */

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

126         return new Writer JavaDoc(out) {
127
128             public void write(char cbuf[], int off, int len) {
129             }
130
131             public void flush() throws IOException JavaDoc {
132                 out.flush();
133             }
134
135             public void close() throws IOException JavaDoc {
136                 List JavaDoc globalNodeTrail = (List JavaDoc)templateRoot.get("globalNodeTrail");
137                 //if (Debug.infoOn()) Debug.logInfo("Render close, globalNodeTrail(2a):" + FreeMarkerWorker.nodeTrailToCsv(globalNodeTrail), "");
138
try {
139                     renderSubContent();
140                 FreeMarkerWorker.reloadValues(templateRoot, savedValuesUp, env);
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
GenericValue thisView = null;
155                 if (view != null) {
156                     thisView = view;
157                 } else if (passedGlobalNodeTrail.size() > 0) {
158                     Map JavaDoc map = (Map JavaDoc)passedGlobalNodeTrail.get(passedGlobalNodeTrail.size() - 1);
159                     if (Debug.infoOn()) Debug.logInfo("in Render(3), map ." + map , module);
160                     if (map != null)
161                         thisView = (GenericValue)map.get("value");
162                 }
163                 //if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, subContentId:" + templateRoot.get( "subContentId"), module);
164
//if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, contentId:" + templateRoot.get( "contentId"), module);
165

166                 String JavaDoc mimeTypeId = (String JavaDoc) templateRoot.get("mimeTypeId");
167                 Locale JavaDoc locale = (Locale JavaDoc) templateRoot.get("locale");
168                 if (locale == null)
169                     locale = Locale.getDefault();
170
171                 if (UtilValidate.isNotEmpty(editRequestName)) {
172                     String JavaDoc editStyle = getEditStyle();
173                     openEditWrap(out, editStyle);
174                 }
175
176                 if (thisView != null) {
177                     try {
178                         ContentWorker.renderContentAsTextCache(delegator, null, out, templateRoot, thisView, locale, mimeTypeId);
179                         //if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, after renderContentAsTextCache:", module);
180
} catch (GeneralException e) {
181                         Debug.logError(e, "Error rendering content", module);
182                         throw new IOException JavaDoc("Error rendering thisView:" + thisView + " msg:" + e.toString());
183                     }
184                 }
185                 if (UtilValidate.isNotEmpty(editRequestName)) {
186                     closeEditWrap(out, editRequestName);
187                 }
188
189                 //if (Debug.infoOn()) Debug.logInfo("in Render(4), globalNodeTrail ." + getWrapped(env, "globalNodeTrail") , module);
190
return;
191             }
192
193             public void openEditWrap(Writer JavaDoc out, String JavaDoc editStyle) throws IOException JavaDoc {
194                 String JavaDoc divStr = "<div class=\"" + editStyle + "\">";
195                 out.write(divStr);
196             }
197
198             public void closeEditWrap(Writer JavaDoc out, String JavaDoc editRequestName) throws IOException JavaDoc {
199                 //if (Debug.infoOn()) Debug.logInfo("in RenderSubContent, contentId(1):" + templateRoot.get( "contentId"), module);
200
//if (Debug.infoOn()) Debug.logInfo("in Render(0), templateRoot ." + templateRoot , module);
201
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
202                 String JavaDoc fullRequest = editRequestName;
203                 String JavaDoc contentId = null;
204                 String JavaDoc contentIdTo = null;
205                 String JavaDoc contentAssocTypeId = null;
206                 String JavaDoc mapKey = null;
207                 String JavaDoc fromDate = null;
208              
209                 if (!directAssocMode) {
210                     contentIdTo = (String JavaDoc)templateRoot.get("contentId");
211                     contentAssocTypeId = (String JavaDoc)templateRoot.get("contentAssocTypeId");
212                     mapKey = (String JavaDoc)templateRoot.get("mapKey");
213                     fromDate = (String JavaDoc)templateRoot.get("fromDate");
214                     if (Debug.infoOn()) Debug.logInfo("in Render(0), view ." + view , module);
215                     if (view != null) {
216                         ModelEntity modelEntity = view.getModelEntity();
217                         if (UtilValidate.isEmpty(contentId) && modelEntity.getField("caContentId") != null )
218                             contentId = view.getString("caContentId");
219                         if (UtilValidate.isEmpty(contentId) && modelEntity.getField("contentId") != null )
220                             contentId = view.getString("contentId");
221                         if (UtilValidate.isEmpty(contentIdTo) && modelEntity.getField("caContentIdTo") != null )
222                             contentIdTo = view.getString("caContentIdTo");
223                         if (UtilValidate.isEmpty(contentIdTo) && modelEntity.getField("contentIdTo") != null )
224                             contentIdTo = view.getString("contentIdTo");
225                         if (UtilValidate.isEmpty(contentAssocTypeId) && modelEntity.getField("caContentAssocTypeId") != null )
226                             contentAssocTypeId = view.getString("caContentAssocTypeId");
227                         if (UtilValidate.isEmpty(contentAssocTypeId) && modelEntity.getField("contentAssocTypeId") != null )
228                             contentAssocTypeId = view.getString("contentAssocTypeId");
229                         if (UtilValidate.isEmpty(mapKey) && modelEntity.getField("caMapKey") != null )
230                             mapKey = view.getString("caMapKey");
231                         if (UtilValidate.isEmpty(mapKey) && modelEntity.getField("mapKey") != null )
232                             mapKey = view.getString("mapKey");
233                         if (UtilValidate.isEmpty(fromDate) && modelEntity.getField("caFromDate") != null )
234                             fromDate = view.getString("caFromDate");
235                         if (UtilValidate.isEmpty(fromDate) && modelEntity.getField("fromDate") != null )
236                             fromDate = view.getString("fromDate");
237                     }
238                 } else {
239                     contentId = (String JavaDoc)templateRoot.get("subContentId");
240                 }
241                 if (Debug.infoOn()) Debug.logInfo("in Render(0), contentIdTo ." + contentIdTo , module);
242                 String JavaDoc delim = "?";
243                 if (UtilValidate.isNotEmpty(contentId)) {
244                     fullRequest += delim + "contentId=" + contentId;
245                     delim = "&";
246                 }
247                 if (UtilValidate.isNotEmpty(contentIdTo)) {
248                     fullRequest += delim + "contentIdTo=" + contentIdTo;
249                     delim = "&";
250                 }
251                 if (UtilValidate.isNotEmpty(contentAssocTypeId)) {
252                     fullRequest += delim + "contentAssocTypeId=" + contentAssocTypeId;
253                     delim = "&";
254                 }
255                 if (UtilValidate.isNotEmpty(mapKey)) {
256                     fullRequest += delim + "mapKey=" + mapKey;
257                     delim = "&";
258                 }
259                 if (UtilValidate.isNotEmpty(fromDate)) {
260                     fullRequest += delim + "fromDate=" + fromDate;
261                     delim = "&";
262                 }
263               
264                 if (Debug.infoOn()) Debug.logInfo("in Render(2), contentIdTo ." + contentIdTo , module);
265                 WidgetWorker.appendOfbizUrl(sb, fullRequest, request, response);
266                 String JavaDoc url = sb.toString();
267                 String JavaDoc link = "<a HREF=\"" + url + "\">Edit</a>";
268                 out.write(link);
269                 String JavaDoc divStr = "</div>";
270                 out.write(divStr);
271             }
272
273             public String JavaDoc getEditStyle() {
274                 String JavaDoc editStyle = (String JavaDoc)templateRoot.get("editStyle");
275                 if (UtilValidate.isEmpty(editStyle)) {
276                     editStyle = UtilProperties.getPropertyValue("content", "defaultEditStyle");
277                 }
278                 if (UtilValidate.isEmpty(editStyle)) {
279                     editStyle = "buttontext";
280                 }
281                 return editStyle;
282             }
283         };
284         
285     }
286
287 }
288
Popular Tags