📄 leaf.java
字号:
if (newParentCode.equals(parent_code))
return false;
if (newParentCode.equals(code))
throw new ErrMsgException("不能将本节点设为父节点!");
// 把该结点加至新父结点,作为其最后一个孩子,同设其layer为父结点的layer + 1
Leaf lfparent = getLeaf(newParentCode);
int oldorders = orders;
int neworders = lfparent.getChildCount() + 1;
int parentLayer = lfparent.getLayer();
String sql = "update blog_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 +
",layer=" + (parentLayer+1) +
" 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);
//DirListCacheMgr更新
LeafChildrenCacheMgr.remove(oldParentCode);
LeafChildrenCacheMgr.remove(newParentCode);
// 更新原来父结点中,位于本leaf之后的orders
sql = "select code from blog_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);
clf.update();
}
// 更新其所有子结点的layer
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();
}
// 将其原来的父结点的孩子数-1
Leaf oldParentLeaf = getLeaf(oldParentCode);
oldParentLeaf.setChildCount(oldParentLeaf.getChildCount() - 1);
oldParentLeaf.update();
// 将其新父结点的孩子数 + 1
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 {
// 计算得出插入结点的orders
int childorders = child_count + 1;
String updatesql = "";
String insertsql = "insert into blog_directory (code,name,parent_code,description,orders,root_code,child_count,layer,type,add_date) values (";
insertsql += StrUtil.sqlstr(childleaf.getCode()) + "," +
StrUtil.sqlstr(childleaf.getName()) +
"," + StrUtil.sqlstr(code) +
"," + StrUtil.sqlstr(childleaf.getDescription()) + "," +
childorders + "," + StrUtil.sqlstr(root_code) +
",0," + (layer+1) + "," + childleaf.getType() + "," + StrUtil.sqlstr("" + System.currentTimeMillis()) +
")";
if (!SecurityUtil.isValidSql(insertsql))
throw new ErrMsgException("请勿输入非法字符如;号等!");
Conn conn = new Conn(connname);
try {
//更改根结点的信息
updatesql = "Update blog_directory 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;
}
/**
* 每个节点有两个Cache,一是本身,另一个是用于存储其孩子结点的cache
* @param code String
*/
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 blog_directory where code=" + StrUtil.sqlstr(leaf.getCode());
boolean r = rmconn.executeUpdate(sql) == 1 ? true : false;
sql = "update blog_directory set orders=orders-1 where parent_code=" + StrUtil.sqlstr(leaf.getParentCode()) + " and orders>" + leaf.getOrders();
rmconn.executeUpdate(sql);
sql = "update blog_directory set child_count=child_count-1 where code=" + StrUtil.sqlstr(leaf.getParentCode());
rmconn.executeUpdate(sql);
// removeFromCache(leaf.getCode());
// removeFromCache(leaf.getParentCode());
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 blog_directory where parent_code=" +
StrUtil.sqlstr(parent_code) +
" and orders=" + (orders + 1);
} else {
sql = "select code from blog_directory 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 = "";
// 取出该结点的移动方向上的下一个兄弟结点的orders
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 blog_directory set orders=orders+1" +
" where code=" + StrUtil.sqlstr(code);
conn.executeUpdate(sql);
sql = "update blog_directory set orders=orders-1" +
" where code=" + StrUtil.sqlstr(bleaf.getCode());
conn.executeUpdate(sql);
}
if (direction.equals("up")) {
sql = "update blog_directory set orders=orders-1" +
" where code=" + StrUtil.sqlstr(code);
conn.executeUpdate(sql);
sql = "update blog_directory set orders=orders+1" +
" where code=" + StrUtil.sqlstr(bleaf.getCode());
conn.executeUpdate(sql);
}
conn.commit();
removeFromCache(code);
removeFromCache(bleaf.getCode());
} 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 setPluginCode(String pluginCode) {
this.pluginCode = pluginCode;
}
public void setChildCount(int childCount) {
this.child_count = childCount;
}
private int templateId = -1;
private boolean loaded = false;
private String pluginCode;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -