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