leaf.java

来自「cwbbs 云网论坛源码」· Java 代码 · 共 833 行 · 第 1/2 页

JAVA
833
字号
package cn.js.fan.module.cms;import java.io.*;import java.sql.*;import java.util.*;import javax.servlet.http.*;import cn.js.fan.base.*;import cn.js.fan.cache.jcs.*;import cn.js.fan.db.*;import cn.js.fan.module.cms.template.*;import cn.js.fan.security.*;import cn.js.fan.util.*;import cn.js.fan.web.*;import com.cloudwebsoft.framework.db.*;import org.apache.log4j.*;import cn.js.fan.module.cms.ext.UserGroupPrivDb;public class Leaf implements Serializable, ITagSupport {    transient RMCache rmCache = RMCache.getInstance();    String connname = "";    transient Logger logger = Logger.getLogger(Leaf.class.getName());    int docId;    public static final int TYPE_LIST = 2;    public static final int TYPE_DOCUMENT = 1;    public static final int TYPE_NONE = 0;    public static final int TYPE_COLUMN = 3;    public static final int TYPE_SUB_SITE = 4;    public static final String ROOTCODE = "root";    public static final String CODE_SITE = "site";     private String code = "", name = "", description = "", parent_code = "-1",            root_code = "", add_date = "";    private int orders = 1, layer = 1, child_count = 0, islocked = 0;    final String LOAD = "select code,name,description,parent_code,root_code,orders,layer,child_count,add_date,islocked,type,isHome,doc_id,template_id,pluginCode,price,template_doc_id,doc_count,template_catalog,logo,is_post from directory where code=?";    boolean isHome = false;    final String dirCache = "CMSDIR";    public String get(String field) {        if (field.equals("code"))            return getCode();        else if (field.equals("name"))            return getName();        else if (field.equals("desc"))            return getDescription();        else if (field.equals("parent_code"))            return getParentCode();        else if (field.equals("root_code"))            return getRootCode();        else if (field.equals("layer"))            return "" + getLayer();        else            return "";    }    public Leaf() {        connname = Global.defaultDB;        if (connname.equals(""))            logger.info("Directory:conname is empty");    }    public Leaf(String code) {        connname = Global.defaultDB;        if (connname.equals(""))            logger.info("Directory:conname is empty");        this.code = code;        loadFromDb();    }    public void renew() {        if (logger == null) {            logger = Logger.getLogger(Leaf.class.getName());        }        if (rmCache == null) {            rmCache = RMCache.getInstance();        }    }    public void loadFromDb() {        ResultSet rs = null;        Conn conn = new Conn(connname);        try {            PreparedStatement ps = conn.prepareStatement(LOAD);            ps.setString(1, code);            rs = conn.executePreQuery();            if (rs != null && rs.next()) {                this.code = rs.getString(1);                name = rs.getString(2);                description = rs.getString(3);                parent_code = rs.getString(4);                root_code = rs.getString(5);                orders = rs.getInt(6);                layer = rs.getInt(7);                child_count = rs.getInt(8);                java.util.Date d = DateUtil.parse(rs.getString(9));                add_date = DateUtil.format(d, "yyyy-MM-dd HH:mm:ss");                islocked = rs.getInt(10);                type = rs.getInt(11);                isHome = rs.getInt(12) > 0 ? true : false;                docId = rs.getInt(13);                templateId = rs.getInt(14);                pluginCode = rs.getString(15);                price = rs.getDouble(16);                templateDocId = rs.getInt(17);                docCount = rs.getInt(18);                templateCatalog = rs.getString(19);                logo = StrUtil.getNullStr(rs.getString(20));                post = rs.getInt(21)==1;                loaded = true;            }        } catch (Exception e) {            logger.error("loadFromDb: " + e.getMessage());        } finally {            if (conn != null) {                conn.close();                conn = null;            }        }    }    public static Leaf getSubsiteOfLeaf(String dirCode) {        Leaf plf = new Leaf();        plf = plf.getLeaf(dirCode);        if (plf.getType() == Leaf.TYPE_SUB_SITE) {            return plf;        }        String parentCode = plf.getParentCode();        while (!parentCode.equals(Leaf.ROOTCODE)) {            plf = plf.getLeaf(parentCode);            if (plf == null || !plf.isLoaded())                break;            if (plf.getType() == Leaf.TYPE_SUB_SITE) {                return plf;            }            parentCode = plf.getParentCode();        }        return null;    }    public static boolean isLeafOfSubsite(String dirCode) {        return getSubsiteOfLeaf(dirCode)==null?false:true;    }    public int getDocID() {        return docId;    }    public void setDocID(int d) {        this.docId = d;    }    public String getCode() {        return code;    }    public void setCode(String code) {        this.code = code;    }    public String getName() {        return name;    }    public void setRootCode(String c) {        this.root_code = c;    }    public void setType(int t) {        this.type = t;    }    public void setName(String n) {        this.name = n;    }    public void setDescription(String desc) {        this.description = desc;    }    public int getOrders() {        return orders;    }    public boolean getIsHome() {        return isHome;    }    public void setParentCode(String p) {        this.parent_code = p;    }    public String getParentCode() {        return this.parent_code;    }    public void setIsHome(boolean b) {        this.isHome = b;    }    public void setTemplateId(int templateId) {        this.templateId = templateId;    }    public String getRootCode() {        return root_code;    }    public int getLayer() {        return layer;    }    public void setLayer(int layer) {        this.layer = layer;    }    public String getDescription() {        return description;    }    public int getType() {        return type;    }    public int getTemplateId() {        return templateId;    }    public boolean isLoaded() {        return loaded;    }    public String getPluginCode() {        return pluginCode;    }    public double getPrice() {        return price;    }    public int getTemplateDocId() {        return templateDocId;    }    public int getDocCount() {        return docCount;    }    public String getTemplateCatalog() {        return templateCatalog;    }    public String getLogo() {        return logo;    }    public boolean isPost() {        return post;    }    public int getChildCount() {        return child_count;    }    public Vector getChildren() {        Vector v = new Vector();        String sql =                "select code from directory where parent_code=? order by orders asc";        Conn conn = new Conn(connname);        ResultSet rs = null;        try {            PreparedStatement pstmt = conn.prepareStatement(sql);            pstmt.setString(1, code);            rs = conn.executePreQuery();            if (rs != null) {                while (rs.next()) {                    String c = rs.getString(1);                                        v.addElement(getLeaf(c));                }            }        } catch (SQLException e) {            logger.error("getChildren: " + e.getMessage());        } finally {            if (conn != null) {                conn.close();                conn = null;            }        }        return v;    }        public Vector getAllChild(Vector vt, Leaf leaf) throws ErrMsgException {        Vector children = leaf.getChildren();        if (children.isEmpty())            return children;        vt.addAll(children);        Iterator ir = children.iterator();        while (ir.hasNext()) {            Leaf lf = (Leaf) ir.next();            getAllChild(vt, lf);        }                return vt;    }    public String toString() {        return "Leaf is " + name;    }    private int type;    public boolean save(JdbcTemplate jt) {        String sql = "update directory set doc_count=? where code=?";        boolean re = false;        try {            re = jt.executeUpdate(sql, new Object[] {new Integer(docCount),                                  code}) == 1;                    } catch (SQLException e) {            logger.error("save:" + e.getMessage());        }        if (re) {            removeFromCache(code);        }        return re;    }    public synchronized boolean update() {        String sql = "update directory set name=" + StrUtil.sqlstr(name) +                     ",description=" + StrUtil.sqlstr(description) +                     ",type=" + type + ",isHome=" + (isHome ? "1" : "0") +                     ",doc_id=" + docId + ",template_id=" + templateId +                     ",orders=" + orders + ",layer=" + layer + ",child_count=" +                     child_count + ",pluginCode=" + StrUtil.sqlstr(pluginCode) +                     ",price=" + price +                     ",template_doc_id=" + templateDocId + ",template_catalog=" +                     StrUtil.sqlstr(templateCatalog) + ",logo=" + StrUtil.sqlstr(logo) + ",is_post=" + (post?1:0) +                     " where code=" + StrUtil.sqlstr(code);                RMConn conn = new RMConn(connname);        int r = 0;        try {            r = conn.executeUpdate(sql);            try {                if (r == 1) {                    removeFromCache(code);                                        LeafChildrenCacheMgr.remove(parent_code);                }            } catch (Exception e) {                logger.error("update: " + e.getMessage());            }        } catch (SQLException e) {            logger.error("update: " + e.getMessage());        }        boolean re = r == 1 ? true : false;        if (re) {            removeFromCache(code);        }        return re;    }        public synchronized boolean update(String newParentCode) throws            ErrMsgException {        if (newParentCode.equals(parent_code))            return false;        if (newParentCode.equals(code))            throw new ErrMsgException("不能将本节点设为父节点!");                Leaf lfparent = getLeaf(newParentCode);        int oldorders = orders;        int neworders = lfparent.getChildCount() + 1;        int parentLayer = lfparent.getLayer();        String sql = "update directory set name=" + StrUtil.sqlstr(name) +                     ",description=" + StrUtil.sqlstr(description) +                     ",type=" + type + ",isHome=" + (isHome ? "1" : "0") +                     ",doc_id=" + docId + ",template_id=" + templateId +                     ",parent_code=" + StrUtil.sqlstr(newParentCode) +                     ",orders=" + neworders + ",price=" + price +                     ",layer=" + (parentLayer + 1) + ",template_doc_id=" +                     templateDocId + ",template_catalog=" + StrUtil.sqlstr(templateCatalog) + ",logo=" + StrUtil.sqlstr(logo) +                     ",is_post=" + (post?1:0) +                     " where code=" + StrUtil.sqlstr(code);        String oldParentCode = parent_code;        parent_code = newParentCode;        RMConn conn = new RMConn(connname);        int r = 0;        try {            r = conn.executeUpdate(sql);            try {                if (r == 1) {                    removeFromCache(code);                    removeFromCache(newParentCode);                    removeFromCache(oldParentCode);                                        LeafChildrenCacheMgr.remove(oldParentCode);                    LeafChildrenCacheMgr.remove(newParentCode);                                        sql = "select code from directory where parent_code=" +                          StrUtil.sqlstr(oldParentCode) +                          " and orders>" + oldorders;                    ResultIterator ri = conn.executeQuery(sql);                    while (ri.hasNext()) {                        ResultRecord rr = (ResultRecord) ri.next();                        Leaf clf = getLeaf(rr.getString(1));                        clf.setOrders(clf.getOrders() - 1);

⌨️ 快捷键说明

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