KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: RenderSubContentTransform.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.ServletContext JavaDoc;
28 import javax.servlet.http.HttpServletRequest 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.UtilMisc;
34 import org.ofbiz.base.util.template.FreeMarkerWorker;
35 import org.ofbiz.content.content.ContentWorker;
36 import org.ofbiz.entity.GenericDelegator;
37 import org.ofbiz.entity.GenericValue;
38
39 import freemarker.core.Environment;
40 import freemarker.template.TemplateTransformModel;
41
42 /**
43  * RenderSubContentTransform - Freemarker Transform for Content rendering
44  *
45  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
46  * @version $Rev: 5462 $
47  * @since 3.0
48  *
49  * This transform cannot be called recursively (at this time).
50  */

51 public class RenderSubContentTransform implements TemplateTransformModel {
52
53     public static final String JavaDoc module = RenderSubContentTransform.class.getName();
54
55     /**
56      * Does a conditional search to return a value for a parameter with the passed name. Looks first to see if it was passed as an argument to the transform.
57      * Secondly, it looks to see if it is passed as a parameter in the template context object.
58      * <p/>
59      * Note that this is different from the getArg method of EditRenderDataResourceTransform, which checks the request object instead of the template context
60      * object.
61      */

62     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Environment env) {
63         return FreeMarkerWorker.getArg(args, key, env);
64     }
65
66     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Map JavaDoc ctx) {
67         return FreeMarkerWorker.getArg(args, key, ctx);
68     }
69
70     public Writer JavaDoc getWriter(final Writer JavaDoc out, Map JavaDoc args) {
71         //final StringBuffer buf = new StringBuffer();
72
final Environment env = Environment.getCurrentEnvironment();
73         Map JavaDoc ctx = (Map JavaDoc) FreeMarkerWorker.getWrappedObject("context", env);
74         if (ctx == null) {
75             ctx = new HashMap JavaDoc();
76         }
77         final String JavaDoc mapKey = getArg(args, "mapKey", ctx);
78         final String JavaDoc subContentId = getArg(args, "subContentId", ctx);
79         final String JavaDoc subDataResourceTypeId = getArg(args, "subDataResourceTypeId", ctx);
80         final String JavaDoc contentId = getArg(args, "contentId", ctx);
81         final String JavaDoc mimeTypeId = getArg(args, "mimeTypeId", ctx);
82         final Locale JavaDoc locale = (Locale JavaDoc) FreeMarkerWorker.getWrappedObject("locale", env);
83         final HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) FreeMarkerWorker.getWrappedObject("request", env);
84         final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env);
85         final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env);
86         GenericValue subContentDataResourceViewTemp = (GenericValue) FreeMarkerWorker.getWrappedObject("subContentDataResourceView", env);
87         if (subContentDataResourceViewTemp == null) {
88             List JavaDoc assocTypes = UtilMisc.toList("SUB_CONTENT");
89             Timestamp JavaDoc fromDate = UtilDateTime.nowTimestamp();
90             try {
91                 subContentDataResourceViewTemp = ContentWorker.getSubContent(delegator, contentId, mapKey, subContentId, userLogin, assocTypes, fromDate);
92             } catch (IOException JavaDoc e) {
93                 throw new RuntimeException JavaDoc(e.getMessage());
94             }
95         }
96
97         final GenericValue subContentDataResourceView = subContentDataResourceViewTemp;
98
99
100         final Map JavaDoc templateContext = ctx;
101
102         return new Writer JavaDoc(out) {
103
104             public void write(char cbuf[], int off, int len) {
105             }
106
107             public void flush() throws IOException JavaDoc {
108                 out.flush();
109             }
110
111             public void close() throws IOException JavaDoc {
112                 try {
113                     renderSubContent();
114                 } catch (IOException JavaDoc e) {
115                     throw new IOException JavaDoc(e.getMessage());
116                 }
117             }
118
119             public void renderSubContent() throws IOException JavaDoc {
120                 //TemplateHashModel dataRoot = env.getDataModel();
121
Timestamp JavaDoc fromDate = UtilDateTime.nowTimestamp();
122                 ServletContext JavaDoc servletContext = request.getSession().getServletContext();
123                 String JavaDoc rootDir = servletContext.getRealPath("/");
124                 String JavaDoc webSiteId = (String JavaDoc) servletContext.getAttribute("webSiteId");
125                 String JavaDoc https = (String JavaDoc) servletContext.getAttribute("https");
126                 templateContext.put("webSiteId", webSiteId);
127                 templateContext.put("https", https);
128                 templateContext.put("rootDir", rootDir);
129
130                 Map JavaDoc templateRoot = FreeMarkerWorker.createEnvironmentMap(env);
131
132                 templateRoot.put("context", templateContext);
133                 if (subContentDataResourceView != null) {
134                 }
135                 try {
136                     Map JavaDoc results = ContentWorker.renderSubContentAsText(delegator, contentId, out, mapKey, subContentId, subContentDataResourceView, templateRoot, locale, mimeTypeId, userLogin, fromDate);
137                 } catch (GeneralException e) {
138                     Debug.logError(e, "Error rendering content", module);
139                     throw new IOException JavaDoc("Error rendering content" + e.toString());
140                 }
141
142                 Map JavaDoc resultCtx = (Map JavaDoc) FreeMarkerWorker.getWrappedObject("context", env);
143                 templateContext.put("mapKey", null);
144                 templateContext.put("subContentId", null);
145                 templateContext.put("subDataResourceTypeId", null);
146                 templateContext.put("contentId", contentId);
147                 templateContext.put("mimeTypeId", null);
148                 templateContext.put("locale", locale);
149
150                 return;
151             }
152         };
153     }
154
155 }
156
Popular Tags