leaf.java

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

JAVA
634
字号
            r = conn.executeUpdate(sql);            try {                if (r == 1) {                    removeFromCache(code);                    removeFromCache(newParentCode);                    removeFromCache(oldParentCode);                                        LeafChildrenCacheMgr.remove(oldParentCode);                    LeafChildrenCacheMgr.remove(newParentCode);                                        sql = "select code from " + tableName + " 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);                        clf.update();                    }                                        Vector vt = new Vector();                    getAllChild(vt, this);                    int childcount = vt.size();                    Iterator ir = vt.iterator();                    while (ir.hasNext()) {                        Leaf childlf = (Leaf)ir.next();                        int layer = parentLayer + 1 + 1;                        String pcode = childlf.getParentCode();                        while (!pcode.equals(code)) {                            layer ++;                            Leaf lfp = getLeaf(pcode);                            pcode = lfp.getParentCode();                        }                        childlf.setLayer(layer);                        childlf.update();                    }                                        Leaf oldParentLeaf = getLeaf(oldParentCode);                    oldParentLeaf.setChildCount(oldParentLeaf.getChildCount() - 1);                    oldParentLeaf.update();                                        Leaf newParentLeaf = getLeaf(newParentCode);                    newParentLeaf.setChildCount(newParentLeaf.getChildCount() + 1);                    newParentLeaf.update();                }            } 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 boolean AddChild(Leaf childleaf) throws            ErrMsgException {                int childorders = child_count + 1;        String updatesql = "";        String insertsql = "insert into " + tableName + " (code,name,parent_code,link,orders,root_code,child_count,layer,type,add_date,pre_code,width,is_has_path,is_resource,target) values (";        insertsql += StrUtil.sqlstr(childleaf.getCode()) + "," +                StrUtil.sqlstr(childleaf.getName()) +                "," + StrUtil.sqlstr(code) +                "," + StrUtil.sqlstr(childleaf.getLink()) + "," +                childorders + "," + StrUtil.sqlstr(root_code) +                ",0," + (layer+1) + "," + childleaf.getType() +                "," + StrUtil.sqlstr("" + System.currentTimeMillis()) + "," + StrUtil.sqlstr(childleaf.getPreCode()) + "," + childleaf.getWidth() + "," + (childleaf.isHasPath()?1:0) + "," + (childleaf.isResource()?1:0) + "," + StrUtil.sqlstr(childleaf.getTarget()) + ")";        Conn conn = new Conn(connname);        try {                        updatesql = "Update " + tableName + " set child_count=child_count+1" +                        " where code=" + StrUtil.sqlstr(code);            conn.beginTrans();            conn.executeUpdate(insertsql);            conn.executeUpdate(updatesql);            removeFromCache(code);            conn.commit();                                            } catch (SQLException e) {            conn.rollback();            logger.error("AddChild: " + e.getMessage());            return false;        } finally {            if (conn != null) {                conn.close();                conn = null;            }        }        return true;    }        public void removeFromCache(String code) {        try {            rmCache.remove(code, dirCache);            LeafChildrenCacheMgr.remove(code);        } catch (Exception e) {            logger.error("removeFromCache: " + e.getMessage());        }    }    public void removeAllFromCache() {        try {            rmCache.invalidateGroup(dirCache);            LeafChildrenCacheMgr.removeAll();        } catch (Exception e) {            logger.error("removeAllFromCache: " + e.getMessage());        }    }    public Leaf getLeaf(String code) {        Leaf leaf = null;        try {            leaf = (Leaf) rmCache.getFromGroup(code, dirCache);        } catch (Exception e) {            logger.error("getLeaf1: " + e.getMessage());        }        if (leaf == null) {            leaf = new Leaf(code);            if (leaf != null) {                if (!leaf.isLoaded())                    leaf = null;                else {                    try {                        rmCache.putInGroup(code, dirCache, leaf);                    } catch (Exception e) {                        logger.error("getLeaf2: " + e.getMessage());                    }                }            }        } else            leaf.renew();        return leaf;    }    public boolean delsingle(Leaf leaf) {        RMConn rmconn = new RMConn(connname);        try {            String sql = "delete from " + tableName + " where code=" + StrUtil.sqlstr(leaf.getCode());            boolean r = rmconn.executeUpdate(sql) == 1 ? true : false;            sql = "update " + tableName + " set orders=orders-1 where parent_code=" + StrUtil.sqlstr(leaf.getParentCode()) + " and orders>" + leaf.getOrders();            rmconn.executeUpdate(sql);            sql = "update " + tableName + " set child_count=child_count-1 where code=" + StrUtil.sqlstr(leaf.getParentCode());            rmconn.executeUpdate(sql);                                    removeAllFromCache();                                            } catch (SQLException e) {            logger.error("delsingle: " + e.getMessage());            return false;        }        return true;    }    public void del(Leaf leaf) {        delsingle(leaf);        Iterator children = getChildren().iterator();        while (children.hasNext()) {            Leaf lf = (Leaf) children.next();            del(lf);        }    }    public Leaf getBrother(String direction) {        String sql;        RMConn rmconn = new RMConn(connname);        Leaf bleaf = null;        try {            if (direction.equals("down")) {                sql = "select code from " + tableName + " where parent_code=" +                      StrUtil.sqlstr(parent_code) +                      " and orders=" + (orders + 1);            } else {                sql = "select code from " + tableName + " where parent_code=" +                      StrUtil.sqlstr(parent_code) +                      " and orders=" + (orders - 1);            }            ResultIterator ri = rmconn.executeQuery(sql);            if (ri != null && ri.hasNext()) {                ResultRecord rr = (ResultRecord) ri.next();                bleaf = getLeaf(rr.getString(1));            }        } catch (SQLException e) {            logger.error("getBrother: " + e.getMessage());        }        return bleaf;    }    public boolean move(String direction) {        String sql = "";                boolean isexist = false;        Leaf bleaf = getBrother(direction);        if (bleaf != null) {            isexist = true;        }                if (isexist) {            Conn conn = new Conn(connname);            try {                conn.beginTrans();                if (direction.equals("down")) {                    sql = "update " + tableName + " set orders=orders+1" +                          " where code=" + StrUtil.sqlstr(code);                    conn.executeUpdate(sql);                    sql = "update " + tableName + " set orders=orders-1" +                          " where code=" + StrUtil.sqlstr(bleaf.getCode());                    conn.executeUpdate(sql);                }                if (direction.equals("up")) {                    sql = "update " + tableName + " set orders=orders-1" +                          " where code=" + StrUtil.sqlstr(code);                    conn.executeUpdate(sql);                    sql = "update " + tableName + " set orders=orders+1" +                          " where code=" + StrUtil.sqlstr(bleaf.getCode());                    conn.executeUpdate(sql);                }                conn.commit();                removeFromCache(code);                removeFromCache(bleaf.getCode());                LeafChildrenCacheMgr.removeAll();            } catch (Exception e) {                conn.rollback();                logger.error("move: " + e.getMessage());                return false;            } finally {                if (conn != null) {                    conn.close();                    conn = null;                }            }        }        return true;    }    public void setOrders(int orders) {        this.orders = orders;    }    public void setPreCode(String preCode) {        this.preCode = preCode;    }    public void setWidth(int width) {        this.width = width;    }    public void setHasPath(boolean hasPath) {        this.hasPath = hasPath;    }    public void setResource(boolean resource) {        this.resource = resource;    }    public void setTarget(String target) {        this.target = target;    }    public void setChildCount(int childCount) {        this.child_count = childCount;    }    public String getName(HttpServletRequest request) {        if (resource) {            return SkinUtil.LoadString(request, "res.label.forum.menu", name);        }        else            return name;    }    public String getLink(HttpServletRequest request) {        if (hasPath) {            return link.replaceFirst("\\$u", request.getContextPath());        } else            return link;    }    private boolean loaded = false;    private String preCode;    private int width = 60;    private boolean hasPath = false;    private boolean resource = true;}

⌨️ 快捷键说明

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