001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.content.webapp.ftl;
019:
020: import java.io.IOException;
021: import java.io.Writer;
022: import java.sql.Timestamp;
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.List;
026: import java.util.Map;
027:
028: import javax.servlet.http.HttpServletRequest;
029:
030: import org.ofbiz.base.util.GeneralException;
031: import org.ofbiz.base.util.UtilDateTime;
032: import org.ofbiz.base.util.UtilValidate;
033: import org.ofbiz.base.util.template.FreeMarkerWorker;
034: import org.ofbiz.content.content.ContentWorker;
035: import org.ofbiz.entity.GenericDelegator;
036: import org.ofbiz.entity.GenericValue;
037: import org.ofbiz.webapp.ftl.LoopWriter;
038:
039: import freemarker.core.Environment;
040: import freemarker.template.TemplateModelException;
041: import freemarker.template.TemplateTransformModel;
042: import freemarker.template.TransformControl;
043:
044: //import com.clarkware.profiler.Profiler;
045: /**
046: * TraverseSubContentCacheTransform - Freemarker Transform for URLs (links)
047: */
048: public class TraverseSubContentCacheTransform implements
049: TemplateTransformModel {
050:
051: public static final String module = TraverseSubContentCacheTransform.class
052: .getName();
053: public static final String[] upSaveKeyNames = { "globalNodeTrail" };
054: public static final String[] saveKeyNames = { "contentId",
055: "subContentId", "subDataResourceTypeId", "mimeTypeId",
056: "whenMap", "locale", "wrapTemplateId", "encloseWrapText",
057: "nullThruDatesOnly", "globalNodeTrail" };
058:
059: /**
060: * A wrapper for the FreeMarkerWorker version.
061: */
062: public static Object getWrappedObject(String varName,
063: Environment env) {
064: return FreeMarkerWorker.getWrappedObject(varName, env);
065: }
066:
067: public static String getArg(Map args, String key, Environment env) {
068: return FreeMarkerWorker.getArg(args, key, env);
069: }
070:
071: public static String getArg(Map args, String key, Map ctx) {
072: return FreeMarkerWorker.getArg(args, key, ctx);
073: }
074:
075: public Writer getWriter(final Writer out, Map args) {
076: final StringBuffer buf = new StringBuffer();
077: final Environment env = Environment.getCurrentEnvironment();
078: //final Map templateRoot = (Map) FreeMarkerWorker.getWrappedObject("context", env);
079: final Map templateRoot = FreeMarkerWorker
080: .createEnvironmentMap(env);
081: //FreeMarkerWorker.convertContext(templateRoot);
082: final Map savedValuesUp = new HashMap();
083: FreeMarkerWorker.saveContextValues(templateRoot,
084: upSaveKeyNames, savedValuesUp);
085: final Map savedValues = new HashMap();
086: FreeMarkerWorker.overrideWithArgs(templateRoot, args);
087: String startContentAssocTypeId = (String) templateRoot
088: .get("contentAssocTypeId");
089: //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContentCache, startContentAssocTypeId:" + startContentAssocTypeId, module);
090: final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker
091: .getWrappedObject("delegator", env);
092: final HttpServletRequest request = (HttpServletRequest) FreeMarkerWorker
093: .getWrappedObject("request", env);
094: FreeMarkerWorker.getSiteParameters(request, templateRoot);
095: final GenericValue userLogin = (GenericValue) FreeMarkerWorker
096: .getWrappedObject("userLogin", env);
097: Object obj = templateRoot.get("globalNodeTrail");
098: List globalNodeTrail = (List) obj;
099: //List globalNodeTrail = (List)templateRoot.get("globalNodeTrail");
100: String csvTrail = ContentWorker.nodeTrailToCsv(globalNodeTrail);
101: //if (Debug.infoOn()) Debug.logInfo("in Traverse(0), csvTrail:"+csvTrail,module);
102: String strNullThruDatesOnly = (String) templateRoot
103: .get("nullThruDatesOnly");
104: String contentAssocPredicateId = (String) templateRoot
105: .get("contentAssocPredicateId");
106: Boolean nullThruDatesOnly = (strNullThruDatesOnly != null && strNullThruDatesOnly
107: .equalsIgnoreCase("true")) ? Boolean.TRUE
108: : Boolean.FALSE;
109: GenericValue val = null;
110: try {
111: // getCurrentContent puts the "current" node on the end of globalNodeTrail.
112: // It may have already been there, but getCurrentContent will compare its contentId
113: // to values in templateRoot.
114: val = ContentWorker.getCurrentContent(delegator,
115: globalNodeTrail, userLogin, templateRoot,
116: nullThruDatesOnly, contentAssocPredicateId);
117: } catch (GeneralException e) {
118: throw new RuntimeException(
119: "Error getting current content. " + e.toString());
120: }
121: final GenericValue view = val;
122:
123: final Map traverseContext = new HashMap();
124: traverseContext.put("delegator", delegator);
125: Map whenMap = new HashMap();
126: whenMap.put("followWhen", (String) templateRoot
127: .get("followWhen"));
128: whenMap.put("pickWhen", (String) templateRoot.get("pickWhen"));
129: whenMap.put("returnBeforePickWhen", (String) templateRoot
130: .get("returnBeforePickWhen"));
131: whenMap.put("returnAfterPickWhen", (String) templateRoot
132: .get("returnAfterPickWhen"));
133: traverseContext.put("whenMap", whenMap);
134: env.setVariable("whenMap", FreeMarkerWorker.autoWrap(whenMap,
135: env));
136: String fromDateStr = (String) templateRoot.get("fromDateStr");
137: String thruDateStr = (String) templateRoot.get("thruDateStr");
138: Timestamp fromDate = null;
139: if (fromDateStr != null && fromDateStr.length() > 0) {
140: fromDate = UtilDateTime.toTimestamp(fromDateStr);
141: }
142: traverseContext.put("fromDate", fromDate);
143: Timestamp thruDate = null;
144: if (thruDateStr != null && thruDateStr.length() > 0) {
145: thruDate = UtilDateTime.toTimestamp(thruDateStr);
146: }
147: traverseContext.put("thruDate", thruDate);
148: //if (UtilValidate.isEmpty(startContentAssocTypeId)) {
149: //throw new RuntimeException("contentAssocTypeId is empty.");
150: //}
151: traverseContext.put("contentAssocTypeId",
152: startContentAssocTypeId);
153: String direction = (String) templateRoot.get("direction");
154: if (UtilValidate.isEmpty(direction)) {
155: direction = "From";
156: }
157: traverseContext.put("direction", direction);
158:
159: return new LoopWriter(out) {
160:
161: public void write(char cbuf[], int off, int len) {
162: //StringBuffer ctxBuf = (StringBuffer) templateContext.get("buf");
163: //ctxBuf.append(cbuf, off, len);
164: buf.append(cbuf, off, len);
165: }
166:
167: public void flush() throws IOException {
168: out.flush();
169: }
170:
171: public int onStart() throws TemplateModelException,
172: IOException {
173: //templateContext.put("buf", new StringBuffer());
174: List nodeTrail = null;
175: Map node = null;
176: GenericValue subContentDataResourceView = null;
177: List globalNodeTrail = (List) templateRoot
178: .get("globalNodeTrail");
179: String trailCsv = ContentWorker
180: .nodeTrailToCsv(globalNodeTrail);
181: //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContentCache, onStart, trailCsv(1):" + trailCsv , module);
182: if (globalNodeTrail.size() > 0) {
183: int sz = globalNodeTrail.size();
184: nodeTrail = new ArrayList();
185: //nodeTrail = passedGlobalNodeTrail.subList(sz - 1, sz);
186: node = (Map) globalNodeTrail.get(sz - 1);
187: //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContentCache, onStart, node(1):" + node , module);
188: Boolean checkedObj = (Boolean) node.get("checked");
189: Map whenMap = (Map) templateRoot.get("whenMap");
190: //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContentCache, whenMap(2):" + whenMap , module);
191: if (checkedObj == null
192: || !checkedObj.booleanValue()) {
193: ContentWorker.checkConditions(delegator, node,
194: null, whenMap);
195: }
196: subContentDataResourceView = (GenericValue) node
197: .get("value");
198: } else {
199: throw new IOException("Empty node trail entries");
200: }
201:
202: Boolean isReturnBeforePickBool = (Boolean) node
203: .get("isReturnBeforePick");
204: if (isReturnBeforePickBool != null
205: && isReturnBeforePickBool.booleanValue())
206: return TransformControl.SKIP_BODY;
207:
208: GenericValue content = null;
209: ContentWorker.selectKids(node, traverseContext);
210: //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContentCache, onStart, node(2):" + node , module);
211: nodeTrail.add(node);
212: traverseContext.put("nodeTrail", nodeTrail);
213: Boolean isPickBool = (Boolean) node.get("isPick");
214: Boolean isFollowBool = (Boolean) node.get("isFollow");
215: //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContentCache, onStart, isPickBool(1):" + isPickBool + " isFollowBool:" + isFollowBool, module);
216: boolean isPick = true;
217: if ((isPickBool == null || !isPickBool.booleanValue())
218: && (isFollowBool != null && isFollowBool
219: .booleanValue())) {
220: isPick = ContentWorker
221: .traverseSubContent(traverseContext);
222: //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContentCache, onStart, isPick(2):" + isPick, module);
223: }
224: if (isPick) {
225: populateContext(traverseContext, templateRoot);
226: FreeMarkerWorker.saveContextValues(templateRoot,
227: saveKeyNames, savedValues);
228: return TransformControl.EVALUATE_BODY;
229: } else {
230: return TransformControl.SKIP_BODY;
231: }
232: }
233:
234: public int afterBody() throws TemplateModelException,
235: IOException {
236:
237: FreeMarkerWorker.reloadValues(templateRoot,
238: savedValues, env);
239: List globalNodeTrail = (List) templateRoot
240: .get("globalNodeTrail");
241: //if (Debug.infoOn()) Debug.logInfo("populateContext, globalNodeTrail(2a):" + FreeMarkerWorker.nodeTrailToCsv(globalNodeTrail), "");
242: List nodeTrail = (List) traverseContext
243: .get("nodeTrail");
244: //List savedGlobalNodeTrail = (List)savedValues.get("globalNodeTrail");
245: //templateRoot.put("globalNodeTrail", savedGlobalNodeTrail);
246: boolean inProgress = ContentWorker
247: .traverseSubContent(traverseContext);
248: int sz = nodeTrail.size();
249: if (inProgress) {
250: populateContext(traverseContext, templateRoot);
251: FreeMarkerWorker.saveContextValues(templateRoot,
252: saveKeyNames, savedValues);
253: //globalNodeTrail = (List)templateRoot.get("globalNodeTrail");
254: //globalNodeTrail.addAll(nodeTrail);
255: return TransformControl.REPEAT_EVALUATION;
256: } else
257: return TransformControl.END_EVALUATION;
258: }
259:
260: public void close() throws IOException {
261:
262: FreeMarkerWorker.reloadValues(templateRoot,
263: savedValuesUp, env);
264: String wrappedContent = buf.toString();
265: out.write(wrappedContent);
266: //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContent, wrappedContent:" + wrappedContent, module);
267: }
268:
269: public void populateContext(Map traverseContext,
270: Map templateContext) {
271:
272: List nodeTrail = (List) traverseContext
273: .get("nodeTrail");
274: //if (Debug.infoOn()) Debug.logInfo("populateContext, nodeTrail csv(a):" + FreeMarkerWorker.nodeTrailToCsv((List)nodeTrail), "");
275: int sz = nodeTrail.size();
276: Map node = (Map) nodeTrail.get(sz - 1);
277: GenericValue content = (GenericValue) node.get("value");
278: String contentId = (String) node.get("contentId");
279: String subContentId = (String) node.get("subContentId");
280: String contentAssocTypeId = (String) node
281: .get("contentAssocTypeId");
282: envWrap("contentAssocTypeId", contentAssocTypeId);
283: envWrap("contentId", contentId);
284: envWrap("content", content);
285: String mapKey = (String) node.get("mapKey");
286: envWrap("mapKey", mapKey);
287: envWrap("subContentDataResourceView", null);
288: List globalNodeTrail = (List) templateContext
289: .get("globalNodeTrail");
290: String contentIdEnd = null;
291: String contentIdStart = null;
292: if (globalNodeTrail != null) {
293: Map ndEnd = (Map) globalNodeTrail
294: .get(globalNodeTrail.size() - 1);
295: contentIdEnd = (String) ndEnd.get("contentId");
296: Map ndStart = (Map) nodeTrail.get(0);
297: contentIdStart = (String) ndStart.get("contentId");
298: } else {
299: globalNodeTrail = new ArrayList();
300: }
301: //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContentCache, populateContext, contentIdEnd(1):" + contentIdEnd + " contentIdStart:" + contentIdStart + " equals:" + (contentIdStart.equals(contentIdEnd)), module);
302: boolean bIdEnd = UtilValidate.isNotEmpty(contentIdEnd);
303: boolean bIdStart = UtilValidate
304: .isNotEmpty(contentIdStart);
305: boolean bEquals = contentIdStart.equals(contentIdEnd);
306: //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContentCache, populateContext, contentIdEnd(1):" + bIdEnd + " contentIdStart:" + bIdStart + " equals:" + bEquals, module);
307: //if (Debug.infoOn()) Debug.logInfo("populateContext, globalNodeTrail(1a):" + FreeMarkerWorker.nodeTrailToCsv(globalNodeTrail), "");
308: if (bIdEnd && bIdStart && bEquals) {
309: List subList = nodeTrail.subList(1, sz);
310: globalNodeTrail.addAll(subList);
311: } else {
312: globalNodeTrail.addAll(nodeTrail);
313: }
314: //if (Debug.infoOn()) Debug.logInfo("populateContext, globalNodeTrail(1b):" + FreeMarkerWorker.nodeTrailToCsv(globalNodeTrail), "");
315: int indentSz = globalNodeTrail.size();
316: envWrap("indent", new Integer(indentSz));
317: String trailCsv = ContentWorker
318: .nodeTrailToCsv(globalNodeTrail);
319: //if (Debug.infoOn()) Debug.logInfo("in TraverseSubContentCache, populateCtx, trailCsv(2):" + trailCsv , module);
320: envWrap("nodeTrailCsv", trailCsv);
321: envWrap("globalNodeTrail", globalNodeTrail);
322: }
323:
324: public void envWrap(String varName, Object obj) {
325:
326: templateRoot.put(varName, obj);
327: env.setVariable(varName, FreeMarkerWorker.autoWrap(obj,
328: env));
329: }
330:
331: };
332: }
333: }
|