001: /*
002: * $Id: InjectNodeTrailCsvTransform.java,v 1.1 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.List;
023: import java.util.Locale;
024: import java.util.Map;
025: import java.util.ArrayList;
026: import javax.servlet.http.HttpServletRequest;
027:
028: import org.ofbiz.base.util.Debug;
029: import org.ofbiz.base.util.GeneralException;
030: import org.ofbiz.base.util.UtilDateTime;
031: import org.ofbiz.base.util.UtilMisc;
032: import org.ofbiz.base.util.UtilValidate;
033: import org.ofbiz.content.content.ContentServicesComplex;
034: import org.ofbiz.content.content.ContentPermissionServices;
035: import org.ofbiz.content.content.ContentWorker;
036: import org.ofbiz.entity.GenericDelegator;
037: import org.ofbiz.entity.GenericEntityException;
038: import org.ofbiz.entity.GenericValue;
039: import org.ofbiz.minilang.MiniLangException;
040: import org.ofbiz.service.ModelService;
041: import org.ofbiz.security.Security;
042:
043: import freemarker.template.Environment;
044: import freemarker.template.SimpleHash;
045: import freemarker.template.TemplateHashModel;
046: import freemarker.template.TemplateTransformModel;
047: import freemarker.template.TransformControl;
048: import freemarker.template.TemplateModelException;
049:
050: /**
051: * InjectNodeTrailCsvTransform - Freemarker Transform for URLs (links)
052: *
053: * @author <a href="mailto:byersa@automationgroups.com">Al Byers</a>
054: * @version $Revision: 1.1 $
055: * @since 3.0
056: */
057: public class InjectNodeTrailCsvTransform implements
058: TemplateTransformModel {
059:
060: public static final String module = InjectNodeTrailCsvTransform.class
061: .getName();
062:
063: public static final String[] saveKeyNames = { "nodeTrailCsv",
064: "globalNodeTrail", "nodeTrail" };
065: public static final String[] removeKeyNames = { "nodeTrailCsv" };
066:
067: /**
068: * A wrapper for the FreeMarkerWorker version.
069: */
070: public static Object getWrappedObject(String varName,
071: Environment env) {
072: return FreeMarkerWorker.getWrappedObject(varName, env);
073: }
074:
075: public static String getArg(Map args, String key, Environment env) {
076: return FreeMarkerWorker.getArg(args, key, env);
077: }
078:
079: public static String getArg(Map args, String key, Map ctx) {
080: return FreeMarkerWorker.getArg(args, key, ctx);
081: }
082:
083: public Writer getWriter(final Writer out, Map args) {
084: final StringBuffer buf = new StringBuffer();
085: final Environment env = Environment.getCurrentEnvironment();
086: final Map templateCtx = (Map) FreeMarkerWorker
087: .getWrappedObject("context", env);
088: //FreeMarkerWorker.convertContext(templateCtx);
089: final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker
090: .getWrappedObject("delegator", env);
091: final HttpServletRequest request = (HttpServletRequest) FreeMarkerWorker
092: .getWrappedObject("request", env);
093: FreeMarkerWorker.getSiteParameters(request, templateCtx);
094: //templateCtx.put("buf", buf);
095: if (Debug.verboseOn())
096: Debug.logVerbose(FreeMarkerWorker.logMap("(I)before save",
097: templateCtx, 0), module);
098: final Map savedValues = FreeMarkerWorker.saveValues(
099: templateCtx, saveKeyNames);
100: if (Debug.verboseOn())
101: Debug
102: .logVerbose("(I-0)savedValues: " + savedValues,
103: module);
104: FreeMarkerWorker.overrideWithArgs(templateCtx, args);
105: if (Debug.verboseOn())
106: Debug.logVerbose(FreeMarkerWorker.logMap(
107: "(I)after overrride", templateCtx, 0), module);
108:
109: return new LoopWriter(out) {
110:
111: public void write(char cbuf[], int off, int len) {
112: buf.append(cbuf, off, len);
113: if (Debug.verboseOn())
114: Debug.logVerbose("in InjectNodeTrailCsv, buf:"
115: + buf.toString(), module);
116: }
117:
118: public void flush() throws IOException {
119: out.flush();
120: }
121:
122: public int onStart() throws TemplateModelException,
123: IOException {
124: String passedCsv = (String) templateCtx
125: .get("nodeTrailCsv");
126: String csvTrail = null;
127: List trail = (List) templateCtx.get("globalNodeTrail");
128: if (UtilValidate.isNotEmpty(passedCsv)) {
129: csvTrail = passedCsv;
130: int lastComma = passedCsv.lastIndexOf(",");
131: String lastPassedContentId = null;
132: if (lastComma >= 0) {
133: lastPassedContentId = passedCsv
134: .substring(lastComma + 1);
135: } else {
136: lastPassedContentId = passedCsv;
137: }
138:
139: if (UtilValidate.isNotEmpty(lastPassedContentId)) {
140: if (trail != null && trail.size() > 0) {
141: Map nd = (Map) trail.get(0);
142: String firstTrailContentId = (String) nd
143: .get("contentId");
144: if (UtilValidate
145: .isNotEmpty(firstTrailContentId)
146: && UtilValidate
147: .isNotEmpty(lastPassedContentId)
148: && firstTrailContentId
149: .equals(lastPassedContentId)) {
150: csvTrail += ","
151: + FreeMarkerWorker
152: .nodeTrailToCsv(trail
153: .subList(
154: 1,
155: trail
156: .size()));
157: } else {
158: csvTrail += ","
159: + FreeMarkerWorker
160: .nodeTrailToCsv(trail);
161: }
162: }
163: }
164: } else {
165: csvTrail = FreeMarkerWorker.nodeTrailToCsv(trail);
166: }
167: if (Debug.infoOn())
168: Debug.logInfo("in InjectNodeTrailCsv, csvTrail:"
169: + csvTrail, module);
170: templateCtx.put("nodeTrailCsv", csvTrail);
171: return TransformControl.EVALUATE_BODY;
172: }
173:
174: public void close() throws IOException {
175: String wrappedFTL = buf.toString();
176: if (Debug.verboseOn())
177: Debug.logVerbose(
178: "in InjectNodeTrailCsv, wrappedFTL:"
179: + wrappedFTL, module);
180: out.write(wrappedFTL);
181: if (Debug.verboseOn())
182: Debug
183: .logVerbose(
184: FreeMarkerWorker.logMap(
185: "(I)before remove",
186: templateCtx, 0), module);
187: FreeMarkerWorker.removeValues(templateCtx,
188: removeKeyNames);
189: if (Debug.verboseOn())
190: Debug.logVerbose(FreeMarkerWorker.logMap(
191: "(I)after remove", templateCtx, 0), module);
192: FreeMarkerWorker.reloadValues(templateCtx, savedValues);
193: if (Debug.verboseOn())
194: Debug.logVerbose(FreeMarkerWorker.logMap(
195: "(I)after reload", templateCtx, 0), module);
196: }
197: };
198: }
199: }
|