moduleconfig.java

来自「电信的网厅的整站代码」· Java 代码 · 共 687 行 · 第 1/2 页

JAVA
687
字号
package com.doone.webtemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.doone.util.FileLogger;

public class ModuleConfig {

    private String name = null;

    private String code = null;

    private String title = null;

    private String desc = null;

    private String path = null;

    private String status = null;

    private List childModules = null; // ModuleConfig

    private ModuleConfig parent = null;

    private List pages = null; // PageConfig

    private String fullPath = "";

    ModuleConfig() {

    }

    static ModuleConfig getInstance() {
        ModuleConfig mc = new ModuleConfig();
        return mc;
    }

    static ModuleConfig getInstance(ModuleConfig parent, Element module) {
        String _name = module.attributeValue("name");
        String _code = module.attributeValue("code");
        String _title = module.attributeValue("title");
        String _desc = module.attributeValue("desc");
        String _path = module.attributeValue("path");
        String _status = module.attributeValue("status");

        // 必填项为空,模块无效。不能添加到集合中。
        if (_name == null || _code == null || _path == null)
            return null;
        if (_title == null)
            _title = _name;
        if (_status == null)
            _status = "N";

        ModuleConfig mc = new ModuleConfig();
        mc.setName(_name);
        mc.setCode(_code);
        mc.setTitle(_title);
        mc.setPath(_path);
        mc.setStatus(_status);
        mc.setDesc(_desc);

        mc.parent = parent;

        // 设置模块路径
        String sFullPath = "";
        if (parent != null) {
            sFullPath = parent.getFullPath() + mc.getPath() + "/";
        } else {
            sFullPath = TemplateFactory.getRootPath() + mc.getPath() + "/";
        }

        if (sFullPath.lastIndexOf("//") != -1)
            sFullPath = sFullPath.substring(0, sFullPath.length() - 1);

        mc.setFullPath(sFullPath);

        // 获取子模块。
        try {
            List me = module.elements("module");
            mc.childModules = new ArrayList();

            Map modules = new HashMap();
            for (int i = 0; i < me.size(); i++) {
                Element moduleElt = (Element) me.get(i);

                ModuleConfig smc = ModuleConfig.getInstance(mc, moduleElt);
                if (smc != null) {
                    if (TemplateFactory.getCodeFactory(smc.getCode()) != null) {
                        FileLogger.getLogger().warn(
                                "模块编号“" + mc.getCode() + "”重复,系统将不再加载新模块信息.");
                        continue;
                    }

                    if (modules.get(smc.getName()) != null) {
                        FileLogger.getLogger().warn(
                                "模块名称重名:" + smc.getName() + ",将不再加载新的模块。");
                        continue;
                    }

                    modules.put(smc.getName(), new Integer(1));

                    mc.childModules.add(smc);
                    TemplateFactory.putCodeFactory(smc);
                }
            }
        } catch (Exception ex) {
            String errMsg = "加载子模块配置出错,模块名称为:" + mc.getName() + "、模块编号为:"
                    + mc.getCode();
            FileLogger.getLogger().warn(errMsg, ex);
        }

        // 获取模块页面对象集合。
        try {
            List pcl = module.elements("page");
            mc.pages = new ArrayList();

            for (int i = 0; i < pcl.size(); i++) {
                Element pce = (Element) pcl.get(i);

                PageConfig pc = PageConfig.getInstance(mc, pce);
                if (pc != null) {
                    pc.setFullPath(mc.getFullPath());

                    mc.pages.add(pc);
                }
            }

        } catch (Exception ex) {
            String errMsg = "加载模块页面配置时出错,模块名称为:" + mc.getName() + "、模块编号为:"
                    + mc.getCode();
            FileLogger.getLogger().warn(errMsg, ex);
        }

        return mc;
    }

    Element getXmlElement() {
        if (name == null || code == null || path == null)
            return null;
        Element ret = DocumentHelper.createElement("module");
        ret.addAttribute("name", getName());
        ret.addAttribute("code", getCode());
        ret.addAttribute("path", getPath());
        if (title != null)
            ret.addAttribute("title", getTitle());
        if (desc != null)
            ret.addAttribute("desc", getDesc());
        ret.addAttribute("status", getStatus());

        // 获取页面的Element对象。
        try {
            if (pages == null)
                pages = new ArrayList();
            for (int i = 0; i < pages.size(); i++) {
                PageConfig pc = (PageConfig) pages.get(i);

                Element mce = pc.getXmlElement();
                if (mce != null)
                    ret.add(mce);
            }
        } catch (Exception ex) {
            String errMsg = "获取模块的页面Element对象时出错。模块名称为:" + getName()
                    + ",模块编号为:" + getCode();
            FileLogger.getLogger().warn(errMsg, ex);
        }

        // 获取子模块对象。
        try {
            if (childModules == null)
                childModules = new ArrayList();
            for (int i = 0; i < childModules.size(); i++) {
                ModuleConfig mc = (ModuleConfig) childModules.get(i);

                Element mce = mc.getXmlElement();
                if (mce != null)
                    ret.add(mce);
            }
        } catch (Exception ex) {
            String errMsg = "获取模块的子模块Element对象时出错。模块名称为:" + getName()
                    + ",模块编号为:" + getCode();
            FileLogger.getLogger().warn(errMsg, ex);
        }

        return ret;
    }

    ModuleConfig getParent() {
        return parent;
    }

    ModuleConfig getChildren(String sName) {
        ModuleConfig ret = null;
        if (sName != null)
            sName = sName.trim();
        for (int i = 0; i < childModules.size(); i++) {
            ModuleConfig mc = (ModuleConfig) childModules.get(i);
            if (mc.getName().equals(sName)) {
                ret = mc;
                break;
            }
        }

        return ret;
    }

    ModuleConfig getChildNode(String sPath) {
        ModuleConfig ret = null;
        String path = getPathName(sPath);

        for (int i = 0; i < childModules.size(); i++) {
            ModuleConfig mc = (ModuleConfig) childModules.get(i);
            if (mc.getPath().equals(path)) {
                ret = mc;
                break;
            }
        }

        return ret;
    }

    void appendChild(ModuleConfig mc) {
        // TODO 将子模块添加到该模块下。
    }

    /**
     * 返回第一个有效的页面。
     *
     * @param request
     *            当前请求的页面对象。
     * @return
     */
    public PageConfig getValidPage(HttpServletRequest request) {
        PageConfig ret = null;
        PageConfig defPage = null;
        for (int i = 0; i < this.pages.size(); i++) {
            PageConfig pc = (PageConfig) this.pages.get(i);
            if (pc.isValid(request)) {
                if (pc.getIsDefault()) {
                    defPage = pc;
                } else {
                    ret = pc;
                    break;
                }
            }
        }

        if (ret == null && defPage != null) {
            ret = defPage;
        }

        return ret;
    }

    /**
     * 返回有效页面列表
     *
     * @param request
     *            当前请求的页面对象。
     * @return
     */
    public PageConfig[] getValidPages(HttpServletRequest request) {
        PageConfig[] ret = null;
        ArrayList list = new ArrayList();
        for (int i = 0; i < this.pages.size(); i++) {
            PageConfig pc = (PageConfig) this.pages.get(i);
            if (pc.isValid(request)) {
                list.add(pc);
            }
        }

        FileLogger log = new FileLogger();

        ret = new PageConfig[list.size()];
        for (int i = 0; i < ret.length; i++) {
            PageConfig pc = (PageConfig) list.get(0);
            int pcIdx = 0;

            log.debug("list.Size()-->" + String.valueOf(list.size()));
            log.debug("Start pcIdx-->" + String.valueOf(pcIdx));
            for (int j = 1; j < list.size(); j++) {
                //log.debug("Curr Index-->" + String.valueOf(j));
                PageConfig curr = (PageConfig) list.get(j);

                if (!maxPageConfig(pc, curr)) {
                    pc = curr;
                    pcIdx = j;
                }
            }

            log.debug("End pcIdx-->" + String.valueOf(pcIdx));

            ret[i] = pc;
            list.remove(pcIdx);
        }

        return ret;
    }

    /**
     * 返回有效页面列表
     *
     * @param request
     *            当前请求的页面对象。
     * @return
     */
    public PageConfig[] getValidPages(Map oParam) {
        PageConfig[] ret = null;
        ArrayList list = new ArrayList();
        for (int i = 0; i < this.pages.size(); i++) {
            PageConfig pc = (PageConfig) this.pages.get(i);
            if (pc.isValid(oParam)) {
                list.add(pc);
            }
        }

        FileLogger log = new FileLogger();

        ret = new PageConfig[list.size()];
        for (int i = 0; i < ret.length; i++) {
            PageConfig pc = (PageConfig) list.get(0);
            int pcIdx = 0;

            log.debug("list.Size()-->" + String.valueOf(list.size()));
            log.debug("Start pcIdx-->" + String.valueOf(pcIdx));
            for (int j = 1; j < list.size(); j++) {
                //log.debug("Curr Index-->" + String.valueOf(j));
                PageConfig curr = (PageConfig) list.get(j);

                if (!maxPageConfig(pc, curr)) {
                    pc = curr;
                    pcIdx = j;
                }
            }

            log.debug("End pcIdx-->" + String.valueOf(pcIdx));

⌨️ 快捷键说明

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