001: /*
002: * $Id: EditRenderSubContentCacheTransform.java,v 1.3 2004/01/13 06:16:30 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: import java.util.ArrayList;
033:
034: import javax.servlet.http.HttpServletRequest;
035:
036: import org.ofbiz.base.util.Debug;
037: import org.ofbiz.base.util.GeneralException;
038: import org.ofbiz.base.util.UtilDateTime;
039: import org.ofbiz.base.util.UtilMisc;
040: import org.ofbiz.base.util.UtilValidate;
041: import org.ofbiz.content.content.ContentWorker;
042: import org.ofbiz.entity.GenericDelegator;
043: import org.ofbiz.entity.GenericEntityException;
044: import org.ofbiz.entity.GenericValue;
045: import org.ofbiz.minilang.MiniLangException;
046:
047: import freemarker.template.Environment;
048: import freemarker.template.TemplateTransformModel;
049:
050: /**
051: * EditRenderSubContentCacheTransform - Freemarker Transform for URLs (links)
052: *
053: * This is an interactive FreeMarker tranform that allows the user to modify the contents that are placed within it.
054: *
055: * @author <a href="mailto:byersa@automationgroups.com">Al Byers</a>
056: * @version $Revision: 1.3 $
057: * @since 3.0
058: */
059: public class EditRenderSubContentCacheTransform implements
060: TemplateTransformModel {
061:
062: public static final String module = EditRenderSubContentCacheTransform.class
063: .getName();
064: public static final String[] saveKeyNames = { "contentId",
065: "subContentId", "mimeTypeId", "subContentDataResourceView",
066: "wrapTemplateId", "templateContentId", "pickWhen",
067: "followWhen", "returnAfterPickWhen",
068: "returnBeforePickWhen", "globalNodeTrail", "nodeTrail",
069: "passedGlobalNodeTrail" };
070: public static final String[] removeKeyNames = {
071: "templateContentId", "subDataResourceTypeId", "mapKey",
072: "wrappedFTL" };
073:
074: /**
075: * A wrapper for the FreeMarkerWorker version.
076: */
077: public static Object getWrappedObject(String varName,
078: Environment env) {
079: return FreeMarkerWorker.getWrappedObject(varName, env);
080: }
081:
082: public static String getArg(Map args, String key, Environment env) {
083: return FreeMarkerWorker.getArg(args, key, env);
084: }
085:
086: public static String getArg(Map args, String key, Map ctx) {
087: return FreeMarkerWorker.getArg(args, key, ctx);
088: }
089:
090: public Writer getWriter(final Writer out, Map args) {
091: final StringBuffer buf = new StringBuffer();
092: final Environment env = Environment.getCurrentEnvironment();
093: final Map templateCtx = (Map) FreeMarkerWorker
094: .getWrappedObject("context", env);
095: final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker
096: .getWrappedObject("delegator", env);
097: final HttpServletRequest request = (HttpServletRequest) FreeMarkerWorker
098: .getWrappedObject("request", env);
099: FreeMarkerWorker.getSiteParameters(request, templateCtx);
100: if (Debug.verboseOn())
101: Debug.logVerbose(FreeMarkerWorker.logMap("(E)before save",
102: templateCtx, 0), module);
103: final Map savedValues = FreeMarkerWorker.saveValues(
104: templateCtx, saveKeyNames);
105: FreeMarkerWorker.overrideWithArgs(templateCtx, args);
106: if (Debug.verboseOn())
107: Debug.logVerbose(FreeMarkerWorker.logMap(
108: "(E)after overrride", templateCtx, 0), module);
109: final GenericValue userLogin = (GenericValue) FreeMarkerWorker
110: .getWrappedObject("userLogin", env);
111: String contentId = (String) templateCtx.get("contentId");
112: String subContentId = (String) templateCtx.get("subContentId");
113: String mapKey = (String) templateCtx.get("mapKey");
114: Timestamp fromDate = UtilDateTime.nowTimestamp();
115: List assocTypes = null;
116: List trail = (List) templateCtx.get("globalNodeTrail");
117: List passedGlobalNodeTrail = null;
118: if (trail != null && trail.size() > 0)
119: passedGlobalNodeTrail = new ArrayList(trail);
120: else
121: passedGlobalNodeTrail = new ArrayList();
122: Debug.logInfo("editREnder, contentId:" + contentId, "");
123: if (UtilValidate.isNotEmpty(subContentId)
124: || UtilValidate.isNotEmpty(contentId)) {
125: String this ContentId = subContentId;
126: if (UtilValidate.isEmpty(this ContentId))
127: this ContentId = contentId;
128:
129: // Don't want to duplicate a node value
130: String passedContentId = null;
131: if (passedGlobalNodeTrail.size() > 0) {
132: Map nd = (Map) passedGlobalNodeTrail
133: .get(passedGlobalNodeTrail.size() - 1);
134: passedContentId = (String) nd.get("contentId");
135: }
136: if (UtilValidate.isNotEmpty(this ContentId)
137: && (UtilValidate.isEmpty(passedContentId) || !this ContentId
138: .equals(passedContentId))) {
139:
140: try {
141: GenericValue view = ContentWorker
142: .getSubContentCache(delegator, contentId,
143: mapKey, subContentId, userLogin,
144: assocTypes, fromDate);
145: passedGlobalNodeTrail.add(FreeMarkerWorker
146: .makeNode(view));
147: } catch (GenericEntityException e) {
148: throw new RuntimeException(e.getMessage());
149: } catch (MiniLangException e2) {
150: throw new RuntimeException(e2.getMessage());
151: } catch (GeneralException e3) {
152: throw new RuntimeException(e3.getMessage());
153: }
154: }
155: }
156: templateCtx.put("globalNodeTrail", passedGlobalNodeTrail);
157: Debug.logInfo("editREnder, globalNodeTrail csv:"
158: + FreeMarkerWorker
159: .nodeTrailToCsv((List) passedGlobalNodeTrail),
160: "");
161: templateCtx.put("contentId", null);
162: templateCtx.put("subContentId", null);
163: int sz = passedGlobalNodeTrail.size();
164: if (Debug.verboseOn())
165: Debug.logVerbose("sz:" + sz, module);
166: if (Debug.verboseOn())
167: Debug.logVerbose(FreeMarkerWorker.logMap("(E)before get",
168: templateCtx, 0), module);
169: if (sz == 0)
170: throw new RuntimeException("No current subContent found.");
171: templateCtx.put("indent", new Integer(sz));
172: if (Debug.verboseOn())
173: Debug.logVerbose(FreeMarkerWorker.logList(
174: "(E)passedGlobalNodeTrail", passedGlobalNodeTrail,
175: 0), module);
176: final GenericValue view = (GenericValue) ((Map) passedGlobalNodeTrail
177: .get(sz - 1)).get("value");
178:
179: String dataResourceId = null;
180: try {
181: dataResourceId = (String) view.get("drDataResourceId");
182: } catch (Exception e) {
183: dataResourceId = (String) view.get("dataResourceId");
184: }
185: String subContentIdSub = (String) view.get("contentId");
186: if (Debug.verboseOn())
187: Debug.logVerbose(
188: "in LoopSubContentCache(0), subContentIdSub ."
189: + subContentIdSub, module);
190: // This order is taken so that the dataResourceType can be overridden in the transform arguments.
191: String subDataResourceTypeId = (String) templateCtx
192: .get("subDataResourceTypeId");
193: if (UtilValidate.isEmpty(subDataResourceTypeId)) {
194: try {
195: subDataResourceTypeId = (String) view
196: .get("drDataResourceTypeId");
197: } catch (Exception e) {
198: // view may be "Content"
199: }
200: // TODO: If this value is still empty then it is probably necessary to get a value from
201: // the parent context. But it will already have one and it is the same context that is
202: // being passed.
203: }
204: // This order is taken so that the mimeType can be overridden in the transform arguments.
205: String mimeTypeId = (String) templateCtx.get("mimeTypeId");
206: if (UtilValidate.isEmpty(mimeTypeId)) {
207: mimeTypeId = (String) view.get("mimeTypeId");
208: String parentContentId = (String) templateCtx
209: .get("contentId");
210: if (UtilValidate.isEmpty(mimeTypeId)
211: && UtilValidate.isNotEmpty(parentContentId)) { // will need these below
212: try {
213: GenericValue parentContent = delegator
214: .findByPrimaryKey("Content",
215: UtilMisc.toMap("contentId",
216: parentContentId));
217: if (parentContent != null) {
218: mimeTypeId = (String) parentContent
219: .get("mimeTypeId");
220: templateCtx.put("parentContent", parentContent);
221: if (Debug.verboseOn())
222: Debug.logVerbose(
223: "in LoopSubContentCache, parentContentId: "
224: + parentContent
225: .get("contentId"),
226: module);
227: }
228: } catch (GenericEntityException e) {
229: throw new RuntimeException(e.getMessage());
230: }
231: }
232:
233: }
234: templateCtx.put("drDataResourceId", dataResourceId);
235: templateCtx.put("mimeTypeId", mimeTypeId);
236: templateCtx.put("dataResourceId", dataResourceId);
237: templateCtx.put("subContentIdSub", subContentIdSub);
238: templateCtx.put("subDataResourceTypeId", subDataResourceTypeId);
239:
240: return new Writer(out) {
241:
242: public void write(char cbuf[], int off, int len) {
243: buf.append(cbuf, off, len);
244: if (Debug.verboseOn())
245: Debug.logVerbose("in EditRenderSubContent, buf:"
246: + buf.toString(), module);
247: }
248:
249: public void flush() throws IOException {
250: out.flush();
251: }
252:
253: public void close() throws IOException {
254: String wrappedFTL = buf.toString();
255: if (Debug.verboseOn())
256: Debug.logVerbose(
257: "in EditRenderSubContent, wrappedFTL:"
258: + wrappedFTL, module);
259: String editTemplate = (String) templateCtx
260: .get("editTemplate");
261: if (editTemplate != null
262: && editTemplate.equalsIgnoreCase("true")) {
263: String wrapTemplateId = (String) templateCtx
264: .get("wrapTemplateId");
265: if (UtilValidate.isNotEmpty(wrapTemplateId)) {
266: templateCtx.put("wrappedFTL", wrappedFTL);
267:
268: Map templateRoot = FreeMarkerWorker
269: .createEnvironmentMap(env);
270:
271: /*
272: templateRoot.put("wrapDataResourceId", dataResourceId);
273: templateRoot.put("wrapDataResourceTypeId", subDataResourceTypeId);
274: templateRoot.put("wrapContentIdTo", contentId);
275: templateRoot.put("wrapSubContentId", subContentIdSub);
276: templateRoot.put("wrapMimeTypeId", mimeTypeId);
277: templateRoot.put("wrapMapKey", mapKey);
278: */
279: templateRoot.put("context", templateCtx);
280:
281: String mimeTypeId = (String) templateCtx
282: .get("mimeTypeId");
283: Locale locale = null;
284: try {
285: ContentWorker.renderContentAsTextCache(
286: delegator, wrapTemplateId, out,
287: templateRoot, null, locale,
288: mimeTypeId);
289: } catch (IOException e) {
290: Debug.logError(e, "Error rendering content"
291: + e.getMessage(), module);
292: throw new IOException(
293: "Error rendering content"
294: + e.toString());
295: } catch (GeneralException e2) {
296: Debug.logError(e2,
297: "Error rendering content"
298: + e2.getMessage(), module);
299: throw new IOException(
300: "Error rendering content"
301: + e2.toString());
302: }
303: if (Debug.verboseOn())
304: Debug
305: .logVerbose(
306: "in ERSC, after renderContentAsText",
307: module);
308:
309: }
310: } else {
311: out.write(wrappedFTL);
312: }
313: if (Debug.verboseOn())
314: Debug
315: .logVerbose(
316: FreeMarkerWorker.logMap(
317: "(E)before remove",
318: templateCtx, 0), module);
319: FreeMarkerWorker.removeValues(templateCtx,
320: removeKeyNames);
321: if (Debug.verboseOn())
322: Debug.logVerbose(FreeMarkerWorker.logMap(
323: "(E)after remove", templateCtx, 0), module);
324: FreeMarkerWorker.reloadValues(templateCtx, savedValues);
325: if (Debug.verboseOn())
326: Debug.logVerbose(FreeMarkerWorker.logMap(
327: "(E)after reload", templateCtx, 0), module);
328: }
329: };
330: }
331: }
|