001: /*
002: * $Id: RenderSubContentTransform.java,v 1.5 2003/12/23 07:24:05 jonesde Exp $
003: *
004: * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
007: * in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
009: *
010: * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
011: *
012: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
013: * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
014: * 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.
015: *
016: */
017: package org.ofbiz.content.webapp.ftl;
018:
019: import java.io.IOException;
020: import java.io.Writer;
021: import java.sql.Timestamp;
022: import java.util.HashMap;
023: import java.util.List;
024: import java.util.Locale;
025: import java.util.Map;
026:
027: import javax.servlet.ServletContext;
028: import javax.servlet.http.HttpServletRequest;
029:
030: import org.ofbiz.base.util.Debug;
031: import org.ofbiz.base.util.GeneralException;
032: import org.ofbiz.base.util.UtilDateTime;
033: import org.ofbiz.base.util.UtilMisc;
034: import org.ofbiz.content.content.ContentWorker;
035: import org.ofbiz.entity.GenericDelegator;
036: import org.ofbiz.entity.GenericValue;
037:
038: import freemarker.template.Environment;
039: import freemarker.template.SimpleHash;
040: import freemarker.template.TemplateHashModel;
041: import freemarker.template.TemplateTransformModel;
042:
043: /**
044: * RenderSubContentTransform - Freemarker Transform for Content rendering
045: *
046: * @author <a href="mailto:byersa@automationgroups.com">Al Byers</a>
047: * @version $Revision: 1.5 $
048: * @since 3.0
049: *
050: * This transform cannot be called recursively (at this time).
051: */
052: public class RenderSubContentTransform implements
053: TemplateTransformModel {
054:
055: public static final String module = RenderSubContentTransform.class
056: .getName();
057:
058: /**
059: * 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.
060: * Secondly, it looks to see if it is passed as a parameter in the template context object.
061: * <p>
062: * Note that this is different from the getArg method of EditRenderDataResourceTransform, which checks the request object instead of the template context
063: * object.
064: */
065: public static String getArg(Map args, String key, Environment env) {
066: return FreeMarkerWorker.getArg(args, key, env);
067: }
068:
069: public static String getArg(Map args, String key, Map ctx) {
070: return FreeMarkerWorker.getArg(args, key, ctx);
071: }
072:
073: public Writer getWriter(final Writer out, Map args) {
074: //final StringBuffer buf = new StringBuffer();
075: final Environment env = Environment.getCurrentEnvironment();
076: Map ctx = (Map) FreeMarkerWorker.getWrappedObject("context",
077: env);
078: if (ctx == null) {
079: ctx = new HashMap();
080: }
081: final String mapKey = getArg(args, "mapKey", ctx);
082: final String subContentId = getArg(args, "subContentId", ctx);
083: final String subDataResourceTypeId = getArg(args,
084: "subDataResourceTypeId", ctx);
085: final String contentId = getArg(args, "contentId", ctx);
086: final String mimeTypeId = getArg(args, "mimeTypeId", ctx);
087: final Locale locale = (Locale) FreeMarkerWorker
088: .getWrappedObject("locale", env);
089: final HttpServletRequest request = (HttpServletRequest) FreeMarkerWorker
090: .getWrappedObject("request", env);
091: final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker
092: .getWrappedObject("delegator", env);
093: final GenericValue userLogin = (GenericValue) FreeMarkerWorker
094: .getWrappedObject("userLogin", env);
095: GenericValue subContentDataResourceViewTemp = (GenericValue) FreeMarkerWorker
096: .getWrappedObject("subContentDataResourceView", env);
097: if (subContentDataResourceViewTemp == null) {
098: List assocTypes = UtilMisc.toList("SUB_CONTENT");
099: Timestamp fromDate = UtilDateTime.nowTimestamp();
100: try {
101: subContentDataResourceViewTemp = ContentWorker
102: .getSubContent(delegator, contentId, mapKey,
103: subContentId, userLogin, assocTypes,
104: fromDate);
105: } catch (IOException e) {
106: throw new RuntimeException(e.getMessage());
107: }
108: }
109:
110: final GenericValue subContentDataResourceView = subContentDataResourceViewTemp;
111:
112: if (Debug.verboseOn())
113: Debug.logVerbose("in RenderSubContent, start.", module);
114: if (Debug.verboseOn())
115: Debug.logVerbose("in RenderSubContent, mapKey:" + mapKey,
116: module);
117: if (Debug.verboseOn())
118: Debug.logVerbose("in RenderSubContent, subContentId:"
119: + subContentId, module);
120: if (Debug.verboseOn())
121: Debug.logVerbose(
122: "in RenderSubContent, subDataResourceTypeId:"
123: + subDataResourceTypeId, module);
124: if (Debug.verboseOn())
125: Debug.logVerbose("in RenderSubContent, contentId:"
126: + contentId, module);
127: if (Debug.verboseOn())
128: Debug.logVerbose("in RenderSubContent, mimeTypeId:"
129: + mimeTypeId, module);
130: if (Debug.verboseOn())
131: Debug.logVerbose("in RenderSubContent, locale:" + locale,
132: module);
133:
134: final Map templateContext = ctx;
135:
136: return new Writer(out) {
137:
138: public void write(char cbuf[], int off, int len) {
139: }
140:
141: public void flush() throws IOException {
142: out.flush();
143: }
144:
145: public void close() throws IOException {
146: try {
147: if (Debug.verboseOn())
148: Debug.logVerbose("in RenderSubContent, close:",
149: module);
150: renderSubContent();
151: } catch (IOException e) {
152: throw new IOException(e.getMessage());
153: }
154: }
155:
156: public void renderSubContent() throws IOException {
157: //TemplateHashModel dataRoot = env.getDataModel();
158: Timestamp fromDate = UtilDateTime.nowTimestamp();
159: ServletContext servletContext = request.getSession()
160: .getServletContext();
161: String rootDir = servletContext.getRealPath("/");
162: String webSiteId = (String) servletContext
163: .getAttribute("webSiteId");
164: String https = (String) servletContext
165: .getAttribute("https");
166: if (Debug.verboseOn())
167: Debug.logVerbose("in RenderSubContent, rootDir:"
168: + rootDir, module);
169: templateContext.put("webSiteId", webSiteId);
170: templateContext.put("https", https);
171: templateContext.put("rootDir", rootDir);
172:
173: Map templateRoot = FreeMarkerWorker
174: .createEnvironmentMap(env);
175:
176: templateRoot.put("context", templateContext);
177: if (Debug.verboseOn())
178: Debug.logVerbose(
179: "in RenderSubContent, calling renderSubCasT:"
180: + " contentId:" + contentId
181: + " mapKey:" + mapKey
182: + " subContentId:" + subContentId,
183: module);
184: if (subContentDataResourceView != null) {
185: if (Debug.verboseOn())
186: Debug.logVerbose("in RenderSubContent, "
187: + " subContentDataResourceView:"
188: + subContentDataResourceView
189: .get("contentId")
190: + " / "
191: + subContentDataResourceView
192: .get("drDataResourceId"),
193: module);
194: }
195: try {
196: Map results = ContentWorker.renderSubContentAsText(
197: delegator, contentId, out, mapKey,
198: subContentId, subContentDataResourceView,
199: templateRoot, locale, mimeTypeId,
200: userLogin, fromDate);
201: } catch (GeneralException e) {
202: Debug
203: .logError(e, "Error rendering content",
204: module);
205: throw new IOException("Error rendering content"
206: + e.toString());
207: }
208:
209: Map resultCtx = (Map) FreeMarkerWorker
210: .getWrappedObject("context", env);
211: templateContext.put("mapKey", null);
212: templateContext.put("subContentId", null);
213: templateContext.put("subDataResourceTypeId", null);
214: templateContext.put("contentId", contentId);
215: templateContext.put("mimeTypeId", null);
216: templateContext.put("locale", locale);
217:
218: if (Debug.verboseOn())
219: Debug.logVerbose("in RenderSubContent, contentId."
220: + resultCtx.get("contentId"), module);
221: if (Debug.verboseOn())
222: Debug.logVerbose("in RenderSubContent, after.",
223: module);
224: if (Debug.verboseOn())
225: Debug.logVerbose("in RenderSubContent, mapKey:"
226: + mapKey, module);
227: if (Debug.verboseOn())
228: Debug.logVerbose(
229: "in RenderSubContent, subContentId:"
230: + subContentId, module);
231: if (Debug.verboseOn())
232: Debug.logVerbose(
233: "in RenderSubContent, subDataResourceTypeId:"
234: + subDataResourceTypeId, module);
235: if (Debug.verboseOn())
236: Debug.logVerbose("in RenderSubContent, contentId:"
237: + contentId, module);
238: if (Debug.verboseOn())
239: Debug.logVerbose("in RenderSubContent, mimeTypeId:"
240: + mimeTypeId, module);
241: if (Debug.verboseOn())
242: Debug.logVerbose("in RenderSubContent, locale:"
243: + locale, module);
244: if (Debug.verboseOn())
245: Debug.logVerbose("in RenderSubContent, contentId2."
246: + resultCtx.get("contentId"), module);
247:
248: return;
249: }
250: };
251: }
252:
253: }
|