⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 freemarkerworker.java

📁 国外的一套开源CRM
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            //Debug.logVerbose("in checkForLoop, templatePath:" +path, "");
        if (templateList == null) {
            templateList = new ArrayList();
        } else {
            if (templateList.contains(path)) {
                Debug.logVerbose("in checkForLoop, " +path + " has been visited.", "");
                throw new IOException(path + " has already been visited.");
            }
        }
        templateList.add(path);
        ctx.put("templateList", templateList);
        return;
    }

    public static Map createEnvironmentMap(Environment env) {
        Map templateRoot = new HashMap();
        Set varNames = null;
        try {
            varNames = env.getKnownVariableNames();
        } catch (TemplateModelException e1) {
            Debug.logError(e1, "Error getting FreeMarker variable names, will not put pass current context on to sub-content", module);
        }
        if (varNames != null) {
            Iterator varNameIter = varNames.iterator();
            while (varNameIter.hasNext()) {
                String varName = (String) varNameIter.next();
                templateRoot.put(varName, FreeMarkerWorker.getWrappedObject(varName, env));
            }
        }
        return templateRoot;
    }
    
    public static void renderTemplate(String templateIdString, String template, Map context, Writer outWriter) throws TemplateException, IOException {
        Reader templateReader = new StringReader(template);
        renderTemplate(templateIdString, templateReader, context, outWriter);
    }
    
    public static void renderTemplate(String templateIdString, Reader templateReader, Map context, Writer outWriter) throws TemplateException, IOException {
        if (context == null) {
            context = new HashMap();
        }
        
        Configuration config = makeDefaultOfbizConfig();            
        Template template = new Template(templateIdString, templateReader, config);            
        
        // add the OFBiz transforms/methods
        addAllOfbizTransforms(context);
        
        // process the template with the given data and write
        // the email body to the String buffer
        template.process(context, outWriter);
    }

    public static void traceNodeTrail(String lbl, List nodeTrail) {

        if (!Debug.verboseOn()) {
            return;
        }
        if (nodeTrail == null) {
            Debug.logVerbose("[" + lbl + "] nodeTrail is null.", "");
            return;
        }
        String s = "";
        int sz = nodeTrail.size();
        s = "nTsz:" + sz;
        if (sz > 0) {
            Map cN = (Map)nodeTrail.get(sz - 1);
            if (cN != null) {
                String cid = (String)cN.get("contentId");
                s += " cN[" + cid + "]";
                List kids = (List)cN.get("kids");
                int kSz = (kids == null) ? 0 : kids.size();
                s += " kSz:" + kSz;
                Boolean isPick = (Boolean)cN.get("isPick");
                s += " isPick:" + isPick;
            }
        }
        Debug.logVerbose("[" + lbl + "] " + s, "");
        return;
    }

    public static String logMap(String lbl, Map map, int indent) {
   
        String sep = ":";
        String eol = "\n";
        String spc = "";
        for (int i=0; i<indent; i++) 
            spc += "  ";
        String s = (lbl != null) ? lbl : "";
        s += "=" + indent + "==>" + eol;
        Set keySet = map.keySet();
        Iterator it = keySet.iterator();
        while (it.hasNext()) {
            String key = (String)it.next();
            if ("request response session".indexOf(key) < 0) {
                Object obj = map.get(key);
                s += spc + key + sep;;
                if (obj instanceof GenericValue) {
                    GenericValue gv = (GenericValue)obj;
                    GenericPK pk = gv.getPrimaryKey();
                    s += logMap("GMAP[" + key + " name:" + pk.getEntityName()+ "]", pk, indent + 1);
                } else if (obj instanceof List) {
                    s += logList("LIST[" + ((List)obj).size() + "]", (List)obj, indent + 1);
                } else if (obj instanceof Map) {
                    s += logMap("MAP[" + key + "]", (Map)obj, indent + 1);
                } else if (obj != null) {
                    s += obj + sep + obj.getClass() + eol;
                } else {
                    s += eol;
                }
            }
        }
        return s + eol + eol;
    }

    public static String logList(String lbl, List lst, int indent) {
   
        String sep = ":";
        String eol = "\n";
        String spc = "";
        if (lst == null)
            return "";
        int sz = lst.size();
        for (int i=0; i<indent; i++) 
            spc += "  ";
        String s = (lbl != null) ? lbl : "";
        s += "=" + indent + "==> sz:" + sz + eol;
        Iterator it = lst.iterator();
        while (it.hasNext()) {
            Object obj = it.next();
                s += spc;
                if (obj instanceof GenericValue) {
                    GenericValue gv = (GenericValue)obj;
                    GenericPK pk = gv.getPrimaryKey();
                    s += logMap("MAP[name:" + pk.getEntityName() + "]", pk, indent + 1);
                } else if (obj instanceof List) {
                    s += logList("LIST[" + ((List)obj).size() + "]", (List)obj, indent + 1);
                } else if (obj instanceof Map) {
                    s += logMap("MAP[]", (Map)obj, indent + 1);
                } else if (obj != null) {
                    s += obj + sep + obj.getClass() + eol;
                } else {
                    s += eol;
                }
        }
        return s + eol + eol;
    }

    public static Map saveValues(Map context, String [] saveKeyNames) {
        Map saveMap = new HashMap();
        for (int i=0; i<saveKeyNames.length; i++) {
            String key = (String)saveKeyNames[i];
            Object o = context.get(key);
            if (o instanceof Map)
                o = new HashMap((Map)o);
            else if (o instanceof List)
                o = new ArrayList((List)o);
            saveMap.put(key, o);
if (key.equals("globalNodeTrail")) {
    Debug.logInfo("saveValues,key:" + key + " csv:" + nodeTrailToCsv((List)o), "");
}
if (key.equals("nodeTrail")) {
    Debug.logInfo("saveValues,key:" + key + " csv:" + nodeTrailToCsv((List)o), "");
}
        }
        return saveMap;
    }

    public static void reloadValues(Map context, Map saveValues ) {
        Set keySet = saveValues.keySet();
        Iterator it = keySet.iterator();
        while (it.hasNext()) {
            String key = (String)it.next();
            Object o = saveValues.get(key);
            context.put(key, saveValues.get(key));
if (key.equals("globalNodeTrail")) {
    Debug.logInfo("reloadValues,key:" + key + " csv:" + nodeTrailToCsv((List)o), "");
}
if (key.equals("nodeTrail")) {
    Debug.logInfo("reloadValues,key:" + key + " csv:" + nodeTrailToCsv((List)o), "");
}
        }
        return;
    }

    public static void removeValues(Map context, String [] removeKeyNames ) {
        for (int i=0; i<removeKeyNames.length; i++) {
            String key = (String)removeKeyNames[i];
            context.remove(key);
        }
        return;
    }

    public static void overrideWithArgs(Map ctx, Map args) {
        Set keySet = args.keySet();
        Iterator it = keySet.iterator();
        while (it.hasNext()) {
            String key = (String)it.next();
            Object obj = args.get(key);
            if (obj != null) {
                Object unwrappedObj = unwrap(obj);
                if (unwrappedObj == null)
                    unwrappedObj = obj;
       Debug.logVerbose("in overrideWithArgs,  key:" + key + " uObj:" + unwrappedObj,"");
                ctx.put(key, unwrappedObj.toString());
            } else {
                ctx.put(key, null);
            }
        }
        return;
    }

    public static void convertContext(Map ctx) {
        Set keySet = ctx.keySet();
        Iterator it = keySet.iterator();
        while (it.hasNext()) {
            String key = (String)it.next();
            Object obj = ctx.get(key);
            if (obj != null) {
                Object unwrappedObj = unwrap(obj);
                if (unwrappedObj != null) {
                    ctx.put(key, unwrappedObj);
                }
            }
        }
        return;
    }

    public static void getSiteParameters(HttpServletRequest request, Map ctx) {

        if (request == null) 
            return;
        ServletContext servletContext = request.getSession().getServletContext();
        String rootDir = (String)ctx.get("rootDir");
        String webSiteId = (String)ctx.get("webSiteId");
        String https = (String)ctx.get("https");
        if (UtilValidate.isEmpty(rootDir)) {
            rootDir = servletContext.getRealPath("/");
            ctx.put("webSiteId", webSiteId);
        }
        if (UtilValidate.isEmpty(webSiteId)) {
            webSiteId = (String) servletContext.getAttribute("webSiteId");
            ctx.put("https", https);
        }
        if (UtilValidate.isEmpty(https)) {
            https = (String) servletContext.getAttribute("https");
            ctx.put("rootDir", rootDir);
        }
        return;
    }

    public static Map makeNode(GenericValue thisContent) {
        Map thisNode = new HashMap();
        thisNode.put("value", thisContent);
        thisNode.put("contentId", thisContent.get("contentId"));
        thisNode.put("contentTypeId", thisContent.get("contentTypeId"));
        return thisNode;
    }

    public static String nodeTrailToCsv(List nodeTrail) {
        
        if (nodeTrail == null)
            return "";
        StringBuffer csv = new StringBuffer();
        Iterator it = nodeTrail.iterator();
        while (it.hasNext()) {
            if (csv.length() > 0)
                csv.append(",");
            Map node = (Map)it.next();
            String contentId = (String)node.get("contentId");
            csv.append(contentId);
        }
        return csv.toString();
    }

    public static List csvToList(String csv, GenericDelegator delegator) {
        
        ArrayList outList = new ArrayList();
        List contentIdList = StringUtil.split(csv, ",");
        GenericValue content = null;
        String contentId = null;
        String contentName = null;
        ArrayList values = null;
        Iterator it = contentIdList.iterator();
        while (it.hasNext()) {
            contentId = (String)it.next();
            try {
                content = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", contentId));
            } catch(GenericEntityException e) {
                Debug.logError(e.getMessage(), module);
                return new ArrayList();
            }
            contentName = (String)content.get("contentName");
            values = new ArrayList();
            values.add(contentId);
            values.add(contentName);
            outList.add(values);    
        }
        return outList;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -