📄 traversesubcontentcachetransform.java
字号:
/*
* $Id: TraverseSubContentCacheTransform.java,v 1.5 2004/01/13 06:16:30 byersa Exp $
*
* Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* 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.
*
*/
package org.ofbiz.content.webapp.ftl;
import java.io.IOException;
import java.io.Writer;
import java.sql.Timestamp;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.io.StringWriter;
import javax.servlet.http.HttpServletRequest;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.GeneralException;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.content.content.ContentServicesComplex;
import org.ofbiz.content.content.ContentWorker;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import freemarker.template.Environment;
import freemarker.template.SimpleHash;
import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateTransformModel;
import freemarker.template.TransformControl;
import freemarker.template.TemplateModelException;
/**
* TraverseSubContentCacheTransform - Freemarker Transform for URLs (links)
*
* @author <a href="mailto:byersa@automationgroups.com">Al Byers</a>
* @version $Revision: 1.5 $
* @since 3.0
*/
public class TraverseSubContentCacheTransform implements TemplateTransformModel {
public static final String module = TraverseSubContentCacheTransform.class.getName();
public static final String [] saveKeyNames = {"contentId", "subContentId", "mimeTypeId", "subContentDataResourceView", "wrapTemplateId", "templateContentId", "pickWhen", "followWhen", "returnAfterPickWhen", "returnBeforePickWhen", "globalNodeTrail", "entityList", "viewSize", "viewIndex", "highIndex", "lowIndex", "listSize", "thisViewIndex", "thisViewSize", "nodeTrail", "passedGlobalNodeTrail"};
public static final String [] removeKeyNames = {"templateContentId", "subDataResourceTypeId", "mapKey", "wrappedFTL", "nodeTrail", "thisViewIndex", "thisViewSize"};
/**
* A wrapper for the FreeMarkerWorker version.
*/
public static Object getWrappedObject(String varName, Environment env) {
return FreeMarkerWorker.getWrappedObject(varName, env);
}
public static String getArg(Map args, String key, Environment env) {
return FreeMarkerWorker.getArg(args, key, env);
}
public static String getArg(Map args, String key, Map ctx) {
return FreeMarkerWorker.getArg(args, key, ctx);
}
public Writer getWriter(final Writer out, Map args) {
final StringBuffer buf = new StringBuffer();
final Environment env = Environment.getCurrentEnvironment();
final Map templateCtx = (Map) FreeMarkerWorker.getWrappedObject("context", env);
//FreeMarkerWorker.convertContext(templateCtx);
if (Debug.verboseOn()) Debug.logVerbose(FreeMarkerWorker.logMap("(T)before save", templateCtx, 0),module);
final Map savedValues = FreeMarkerWorker.saveValues(templateCtx, saveKeyNames);
if (Debug.verboseOn()) Debug.logVerbose(FreeMarkerWorker.logMap("(T)after save", templateCtx, 0),module);
FreeMarkerWorker.overrideWithArgs(templateCtx, args);
if (Debug.verboseOn()) Debug.logVerbose(FreeMarkerWorker.logMap("(T)after overrride", templateCtx, 0),module);
if (Debug.verboseOn()) Debug.logVerbose("args:" + args,module);
final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env);
final HttpServletRequest request = (HttpServletRequest) FreeMarkerWorker.getWrappedObject("request", env);
FreeMarkerWorker.getSiteParameters(request, templateCtx);
List trail = (List)templateCtx.get("globalNodeTrail");
List passedGlobalNodeTrail = null;
if (trail != null && trail.size() > 0)
passedGlobalNodeTrail = new ArrayList(trail);
else
passedGlobalNodeTrail = new ArrayList();
GenericValue view = null;
if (passedGlobalNodeTrail.size() > 0) {
view = (GenericValue)((Map)passedGlobalNodeTrail.get(passedGlobalNodeTrail.size() - 1)).get("value");
}
String contentId = (String)templateCtx.get("contentId");
String subContentId = (String)templateCtx.get("subContentId");
if (UtilValidate.isNotEmpty(subContentId) || UtilValidate.isNotEmpty(contentId) ) {
String thisContentId = subContentId;
if (UtilValidate.isEmpty(thisContentId))
thisContentId = contentId;
// Don't want to duplicate a node value
String passedContentId = null;
if (passedGlobalNodeTrail.size() > 0) {
Map nd = (Map)passedGlobalNodeTrail.get(passedGlobalNodeTrail.size() - 1);
passedContentId = (String)nd.get("contentId");
}
if (UtilValidate.isNotEmpty(thisContentId)
&& ( UtilValidate.isEmpty(passedContentId) || !thisContentId.equals(passedContentId))) {
try {
view = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", thisContentId));
} catch (GenericEntityException e) {
Debug.logError(e, "Error getting sub-content", module);
throw new RuntimeException(e.getMessage());
}
passedGlobalNodeTrail.add(FreeMarkerWorker.makeNode(view));
}
}
templateCtx.put("passedGlobalNodeTrail", passedGlobalNodeTrail);
templateCtx.put("contentId", null);
templateCtx.put("subContentId", null);
final Map traverseContext = new HashMap();
String vwIdx = (String)templateCtx.get("viewIndex");
if (UtilValidate.isEmpty(vwIdx))
vwIdx = "0";
final int viewIndex = Integer.parseInt(vwIdx);
templateCtx.put("viewIndex", new Integer(viewIndex).toString());
String vwSz = (String)templateCtx.get("viewSize");
if (UtilValidate.isEmpty(vwSz))
vwSz = "10";
final int viewSize = Integer.parseInt(vwSz);
templateCtx.put("viewSize", new Integer(viewSize).toString());
traverseContext.put("delegator", delegator);
Map whenMap = new HashMap();
whenMap.put("followWhen", (String)templateCtx.get( "followWhen"));
whenMap.put("pickWhen", (String)templateCtx.get( "pickWhen"));
whenMap.put("returnBeforePickWhen", (String)templateCtx.get( "returnBeforePickWhen"));
whenMap.put("returnAfterPickWhen", (String)templateCtx.get( "returnAfterPickWhen"));
traverseContext.put("whenMap", whenMap);
String fromDateStr = (String)templateCtx.get( "fromDateStr");
String thruDateStr = (String)templateCtx.get( "thruDateStr");
Timestamp fromDate = null;
if (fromDateStr != null && fromDateStr.length() > 0) {
fromDate = UtilDateTime.toTimestamp(fromDateStr);
}
traverseContext.put("fromDate", fromDate);
Timestamp thruDate = null;
if (thruDateStr != null && thruDateStr.length() > 0) {
thruDate = UtilDateTime.toTimestamp(thruDateStr);
}
traverseContext.put("thruDate", thruDate);
String startContentAssocTypeId = (String)templateCtx.get( "contentAssocTypeId");
if (startContentAssocTypeId != null)
startContentAssocTypeId = "SUB_CONTENT";
traverseContext.put("contentAssocTypeId", startContentAssocTypeId);
String direction = (String)templateCtx.get( "direction");
if (UtilValidate.isEmpty(direction))
direction = "From";
traverseContext.put("direction", direction);
return new LoopWriter(out) {
public void write(char cbuf[], int off, int len) {
//StringBuffer ctxBuf = (StringBuffer) templateContext.get("buf");
//ctxBuf.append(cbuf, off, len);
buf.append(cbuf, off, len);
}
public void flush() throws IOException {
out.flush();
}
public int onStart() throws TemplateModelException, IOException {
//templateContext.put("buf", new StringBuffer());
templateCtx.put("thisViewIndex", new Integer(0));
templateCtx.put("thisViewSize", new Integer(0));
templateCtx.put("lowIndex", "-1");
templateCtx.put("highIndex", "0");
List nodeTrail = null;
GenericValue subContentDataResourceView = null;
List passedGlobalNodeTrail = (List)templateCtx.get("passedGlobalNodeTrail");
if (passedGlobalNodeTrail.size() > 0) {
int sz = passedGlobalNodeTrail.size() ;
nodeTrail = new ArrayList();
//nodeTrail = passedGlobalNodeTrail.subList(sz - 1, sz);
subContentDataResourceView = (GenericValue)((Map)passedGlobalNodeTrail.get(sz - 1)).get("value");
} else {
// This really can't happen. Should probably throw exception
nodeTrail = new ArrayList();
}
traverseContext.put("nodeTrail", nodeTrail);
GenericValue content = null;
Map rootNode = FreeMarkerWorker.makeNode(subContentDataResourceView);
if (Debug.verboseOn()) Debug.logVerbose("in TraverseSubContentCache, onStart, rootNode:" + rootNode, module);
FreeMarkerWorker.traceNodeTrail("1",nodeTrail);
ContentWorker.selectKids(rootNode, traverseContext);
FreeMarkerWorker.traceNodeTrail("2",nodeTrail);
nodeTrail.add(rootNode);
boolean isPick = checkWhen(subContentDataResourceView, (String)traverseContext.get("contentAssocTypeId"));
rootNode.put("isPick", new Boolean(isPick));
if (Debug.verboseOn()) Debug.logVerbose("in TraverseSubContentCache, onStart, isPick(1):" + isPick, module);
if (!isPick) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -