001: /*
002: * $Id: EditRenderSubContentTransform.java,v 1.9 2004/01/07 19:30:11 byersa Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.content.webapp.ftl;
025:
026: import java.io.IOException;
027: import java.io.Writer;
028: import java.sql.Timestamp;
029: import java.util.List;
030: import java.util.Locale;
031: import java.util.Map;
032:
033: import org.ofbiz.base.util.Debug;
034: import org.ofbiz.base.util.GeneralException;
035: import org.ofbiz.base.util.UtilDateTime;
036: import org.ofbiz.base.util.UtilMisc;
037: import org.ofbiz.base.util.UtilValidate;
038: import org.ofbiz.content.content.ContentWorker;
039: import org.ofbiz.entity.GenericDelegator;
040: import org.ofbiz.entity.GenericEntityException;
041: import org.ofbiz.entity.GenericValue;
042:
043: import freemarker.template.Environment;
044: import freemarker.template.TemplateTransformModel;
045:
046: /**
047: * EditRenderSubContentTransform - Freemarker Transform for URLs (links)
048: *
049: * This is an interactive FreeMarker tranform that allows the user to modify the contents that are placed within it.
050: *
051: * @author <a href="mailto:byersa@automationgroups.com">Al Byers</a>
052: * @version $Revision: 1.9 $
053: * @since 3.0
054: */
055: public class EditRenderSubContentTransform implements
056: TemplateTransformModel {
057:
058: public static final String module = EditRenderSubContentTransform.class
059: .getName();
060:
061: /**
062: * A wrapper for the FreeMarkerWorker version.
063: */
064: public static Object getWrappedObject(String varName,
065: Environment env) {
066: return FreeMarkerWorker.getWrappedObject(varName, env);
067: }
068:
069: public static String getArg(Map args, String key, Environment env) {
070: return FreeMarkerWorker.getArg(args, key, env);
071: }
072:
073: public static String getArg(Map args, String key, Map ctx) {
074: return FreeMarkerWorker.getArg(args, key, ctx);
075: }
076:
077: public Writer getWriter(final Writer out, Map args) {
078: final StringBuffer buf = new StringBuffer();
079: final Environment env = Environment.getCurrentEnvironment();
080: Map ctx = (Map) FreeMarkerWorker.getWrappedObject("context",
081: env);
082: final String editTemplate = getArg(args, "editTemplate", ctx);
083: final String wrapTemplateId = getArg(args, "wrapTemplateId",
084: ctx);
085: final String mapKey = getArg(args, "mapKey", ctx);
086: final String templateContentId = getArg(args,
087: "templateContentId", ctx);
088: final String subContentId = getArg(args, "subContentId", ctx);
089: String subDataResourceTypeIdTemp = getArg(args,
090: "subDataResourceTypeId", ctx);
091: final String contentId = getArg(args, "contentId", ctx);
092:
093: if (Debug.verboseOn())
094: Debug.logVerbose("in EditRenderSubContent, editTemplate:"
095: + editTemplate, module);
096: if (Debug.verboseOn())
097: Debug.logVerbose("in EditRenderSubContent, wrapTemplateId:"
098: + wrapTemplateId, module);
099: if (Debug.verboseOn())
100: Debug.logVerbose("in EditRenderSubContent, mapKey:"
101: + mapKey, module);
102: if (Debug.verboseOn())
103: Debug.logVerbose(
104: "in EditRenderSubContent, templateContentId:"
105: + templateContentId, module);
106: if (Debug.verboseOn())
107: Debug.logVerbose("in EditRenderSubContent, subContentId:"
108: + subContentId, module);
109: if (Debug.verboseOn())
110: Debug.logVerbose("in EditRenderSubContent, contentId:"
111: + contentId, module);
112:
113: final Locale locale = (Locale) FreeMarkerWorker
114: .getWrappedObject("locale", env);
115: String mimeTypeIdTemp = getArg(args, "mimeTypeId", ctx);
116: final String rootDir = getArg(args, "rootDir", ctx);
117: final String webSiteId = getArg(args, "webSiteId", ctx);
118: final String https = getArg(args, "https", ctx);
119: //final LocalDispatcher dispatcher = (LocalDispatcher) FreeMarkerWorker.getWrappedObject("dispatcher", env);
120: final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker
121: .getWrappedObject("delegator", env);
122: final GenericValue userLogin = (GenericValue) FreeMarkerWorker
123: .getWrappedObject("userLogin", env);
124: GenericValue subContentDataResourceViewTemp = (GenericValue) FreeMarkerWorker
125: .getWrappedObject("subContentDataResourceView", env);
126: //final HttpServletRequest request = (HttpServletRequest)FreeMarkerWorker.getWrappedObject("request", env);
127:
128: ctx.put("mapKey", mapKey);
129: ctx.put("subDataResourceTypeIdTemp", subDataResourceTypeIdTemp);
130: ctx.put("contentId", contentId);
131: ctx.put("templateContentId", templateContentId);
132: ctx.put("locale", locale);
133:
134: // This transform does not need information about the subContent until the
135: // close action, but any embedded RenderDataResourceTransformation will need it
136: // and since it cannot be passed back up from that transform, the subContent view
137: // is gotten here and made available to underlying transforms to save overall
138: // processing time.
139: GenericValue parentContent = null;
140: //ctx.put("userLogin", userLogin);
141: List assocTypes = UtilMisc.toList("SUB_CONTENT");
142: Timestamp fromDate = UtilDateTime.nowTimestamp();
143: if (subContentDataResourceViewTemp == null) {
144: try {
145: subContentDataResourceViewTemp = ContentWorker
146: .getSubContent(delegator, contentId, mapKey,
147: subContentId, userLogin, assocTypes,
148: fromDate);
149: } catch (IOException e) {
150: Debug.logError(e, "Error getting sub-content", module);
151: throw new RuntimeException(e.getMessage());
152: }
153: }
154:
155: final GenericValue subContentDataResourceView = subContentDataResourceViewTemp;
156:
157: String dataResourceIdTemp = null;
158: String subContentIdSubTemp = null;
159: if (subContentDataResourceView != null
160: && subContentDataResourceView.get("contentId") != null) {
161:
162: if (Debug.verboseOn())
163: Debug
164: .logVerbose(
165: "in EditRenderSubContent, subContentDataResourceView contentId/drDataResourceId:"
166: + subContentDataResourceView
167: .get("contentId")
168: + " / "
169: + subContentDataResourceView
170: .get("drDataResourceId"),
171: module);
172:
173: dataResourceIdTemp = (String) subContentDataResourceView
174: .get("drDataResourceId");
175: if (Debug.verboseOn())
176: Debug.logVerbose(
177: "in EditRenderSubContent(0), dataResourceIdTemp ."
178: + dataResourceIdTemp, module);
179: subContentIdSubTemp = (String) subContentDataResourceView
180: .get("contentId");
181: if (Debug.verboseOn())
182: Debug.logVerbose(
183: "in EditRenderSubContent(0), subContentIdSubTemp ."
184: + subContentIdSubTemp, module);
185: if (Debug.verboseOn())
186: Debug.logVerbose(
187: "in EditRenderSubContent(0), mimeTypeIdTemp."
188: + mimeTypeIdTemp, module);
189: if (UtilValidate.isEmpty(subDataResourceTypeIdTemp)) {
190: subDataResourceTypeIdTemp = (String) subContentDataResourceView
191: .get("drDataResourceTypeId");
192: }
193: if (UtilValidate.isEmpty(mimeTypeIdTemp)) {
194: mimeTypeIdTemp = (String) subContentDataResourceView
195: .get("mimeTypeId");
196: if (UtilValidate.isEmpty(mimeTypeIdTemp)
197: && UtilValidate.isNotEmpty(contentId)) { // will need these below
198: try {
199: parentContent = delegator.findByPrimaryKey(
200: "Content", UtilMisc.toMap("contentId",
201: contentId));
202: if (parentContent != null) {
203: mimeTypeIdTemp = (String) parentContent
204: .get("mimeTypeId");
205: if (Debug.verboseOn())
206: Debug
207: .logVerbose(
208: "in EditRenderSubContent, parentContentId: "
209: + parentContent
210: .get("contentId"),
211: module);
212: }
213: } catch (GenericEntityException e) {
214: throw new RuntimeException(e.getMessage());
215: }
216: }
217:
218: }
219: if (Debug.verboseOn())
220: Debug.logVerbose(
221: "in EditRenderSubContent(2), mimeTypeIdTemp."
222: + mimeTypeIdTemp, module);
223: if (Debug.verboseOn())
224: Debug.logVerbose(
225: "in EditRenderSubContent, subContentId/Sub."
226: + subContentIdSubTemp, module);
227: ctx.put("subContentId", subContentIdSubTemp);
228: ctx.put("drDataResourceId", dataResourceIdTemp);
229: ctx.put("subContentDataResourceView",
230: subContentDataResourceView);
231: ctx.put("mimeTypeId", mimeTypeIdTemp);
232: //request.setAttribute("drDataResourceId", subContentDataResourceView.get("drDataResourceId"));
233: } else {
234: ctx.put("subContentId", null);
235: ctx.put("drDataResourceId", null);
236: ctx.put("subContentDataResourceView", null);
237: ctx.put("mimeTypeId", null);
238: //request.setAttribute("drDataResourceId", null);
239: }
240:
241: final String dataResourceId = dataResourceIdTemp;
242: final String subContentIdSub = subContentIdSubTemp;
243: //final GenericValue finalSubContentView = subContentDataResourceView;
244: //final GenericValue content = parentContent;
245: final Map templateContext = ctx;
246: //if (Debug.verboseOn()) Debug.logVerbose("in EditRenderSubContent, templateContext:"+templateContext,module);
247: final String mimeTypeId = mimeTypeIdTemp;
248: final String subDataResourceTypeId = subDataResourceTypeIdTemp;
249:
250: return new Writer(out) {
251:
252: public void write(char cbuf[], int off, int len) {
253: buf.append(cbuf, off, len);
254: if (Debug.verboseOn())
255: Debug.logVerbose("in EditRenderSubContent, buf:"
256: + buf.toString(), module);
257: }
258:
259: public void flush() throws IOException {
260: out.flush();
261: }
262:
263: public void close() throws IOException {
264: String wrappedFTL = buf.toString();
265: if (Debug.verboseOn())
266: Debug.logVerbose(
267: "in EditRenderSubContent, wrappedFTL:"
268: + wrappedFTL, module);
269: if (editTemplate != null
270: && editTemplate.equalsIgnoreCase("true")) {
271: if (UtilValidate.isNotEmpty(wrapTemplateId)) {
272: templateContext.put("wrappedFTL", wrappedFTL);
273: //ServletContext servletContext = (ServletContext)request.getSession().getServletContext();
274: //String rootDir = servletContext.getRealPath("/");
275: if (Debug.verboseOn())
276: Debug.logVerbose(
277: "in EditRenderSubContent, rootDir:"
278: + rootDir, module);
279: templateContext.put("webSiteId", webSiteId);
280: templateContext.put("https", https);
281: templateContext.put("rootDir", rootDir);
282:
283: Map templateRoot = FreeMarkerWorker
284: .createEnvironmentMap(env);
285:
286: templateRoot.put("wrapDataResourceId",
287: dataResourceId);
288: templateRoot.put("wrapDataResourceTypeId",
289: subDataResourceTypeId);
290: templateRoot.put("wrapContentIdTo", contentId);
291: templateRoot.put("wrapSubContentId",
292: subContentIdSub);
293: templateRoot.put("wrapMimeTypeId", mimeTypeId);
294: templateRoot.put("wrapMapKey", mapKey);
295: templateRoot.put("context", templateContext);
296: if (Debug.verboseOn())
297: Debug.logVerbose(
298: "in ERSC, wrapDataResourceId:"
299: + dataResourceId, module);
300: if (Debug.verboseOn())
301: Debug.logVerbose(
302: "in ERSC, wrapDataResourceTypeId:"
303: + subDataResourceTypeId,
304: module);
305: if (Debug.verboseOn())
306: Debug.logVerbose(
307: "in ERSC, wrapContentIdTo:"
308: + contentId, module);
309: if (Debug.verboseOn())
310: Debug.logVerbose(
311: "in ERSC, wrapSubContentId:"
312: + subContentIdSub, module);
313: if (Debug.verboseOn())
314: Debug.logVerbose("in ERSC, wrapMimeTypeId:"
315: + mimeTypeId, module);
316: //if (Debug.verboseOn()) Debug.logVerbose("in ERSC, wrapMapKey:" + mapKey,module);
317: if (Debug.verboseOn())
318: Debug.logVerbose(
319: "in ERSC, calling renderContentAsText, wrapTemplateId:"
320: + wrapTemplateId, module);
321:
322: try {
323: ContentWorker.renderContentAsText(
324: delegator, wrapTemplateId, out,
325: templateRoot, null, locale,
326: mimeTypeId);
327: } catch (IOException e) {
328: Debug.logError(e, "Error rendering content"
329: + e.getMessage(), module);
330: throw new IOException(
331: "Error rendering content"
332: + e.toString());
333: } catch (GeneralException e2) {
334: Debug.logError(e2,
335: "Error rendering content"
336: + e2.getMessage(), module);
337: throw new IOException(
338: "Error rendering content"
339: + e2.toString());
340: }
341: if (Debug.verboseOn())
342: Debug
343: .logVerbose(
344: "in ERSC, after renderContentAsText",
345: module);
346:
347: Map ctx = (Map) FreeMarkerWorker
348: .getWrappedObject("context", env);
349: if (Debug.verboseOn())
350: Debug.logVerbose("in ERSC, contentId:"
351: + ctx.get("contentId"), module);
352: templateContext.put("contentId", contentId);
353: templateContext.put("locale", locale);
354: templateContext.put("mapKey", null);
355: templateContext.put("subContentId", null);
356: templateContext.put("templateContentId", null);
357: templateContext.put("subDataResourceTypeId",
358: null);
359: templateContext.put("mimeTypeId", null);
360: templateContext.put("wrappedFTL", null);
361: if (Debug.verboseOn())
362: Debug.logVerbose("in ERSC, after.", module);
363: if (Debug.verboseOn())
364: Debug.logVerbose("in ERSC, mapKey:"
365: + mapKey, module);
366: if (Debug.verboseOn())
367: Debug.logVerbose("in ERSC, subContentId:"
368: + subContentId, module);
369: if (Debug.verboseOn())
370: Debug.logVerbose(
371: "in ERSC, subDataResourceTypeId:"
372: + subDataResourceTypeId,
373: module);
374: if (Debug.verboseOn())
375: Debug.logVerbose("in ERSC, contentId:"
376: + contentId, module);
377: if (Debug.verboseOn())
378: Debug.logVerbose("in ERSC, mimeTypeId:"
379: + mimeTypeId, module);
380: if (Debug.verboseOn())
381: Debug.logVerbose("in ERSC, locale:"
382: + locale, module);
383: if (Debug.verboseOn())
384: Debug.logVerbose("in ERSC, contentId2."
385: + ctx.get("contentId"), module);
386: }
387: } else {
388: out.write(wrappedFTL);
389: }
390: }
391: };
392: }
393: }
|