package com.galaxy.pub.rm.namespace;
import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import com.galaxy.pub.database.*;
import com.galaxy.pub.util.XMLUtil;
import com.galaxy.pub.util.enums.AlphaCase;
import com.galaxy.pub.util.enums.DATA_TYPE;
import com.galaxy.pub.util.enums.MODE;
import com.galaxy.pub.util.enums.NodeOrAttribute;
import com.galaxy.pub.util.enums.QUOTEMARK;
import com.galaxy.pub.database.ConnInfo;
import com.galaxy.pub.database.SystemConfigFile;
/**
* 用来获取命名空间下的数据,对象 只要是树状结构,都可以通过这个方法来操作。
*
* @author xwm
*
*/
public class NameSpace implements Tree{
protected String _nodeName = "node";
protected String _rootname = "";
protected String _tablename = "kp_modelnamespace";
protected String _data_tablename = "kp_modellib";// 挂在该节点下的表名。
protected String _data_nsid = "nsid";// 挂在该节点下的表的nsid字段名称。
protected String _data_label = "label";// 挂在该节点下的表的数据的显示名称。
protected String _nsid = "nsid";
protected String _parentid = "parentid";
protected String _displayorder = "displayorder";
protected String _nsname = "nsname";
protected String _label = "label";
protected String _strxml = "";
protected String _filter = "";
protected String _nsid_Type = "number";//nsid的字段类型,有number类型和string类型
protected String _parentid_Type = "number";//parentid的字段类型,有number类型和string类型
protected QUOTEMARK _symbol = QUOTEMARK.SINGLE;
protected NodeOrAttribute _attOrNode = NodeOrAttribute.ATTRIBUTE;
protected AlphaCase _case = AlphaCase.NOCHANGE;
protected MODE _mode = MODE.PINGMIAN;// 0:平行结构 1:层次结构
protected int _rootid = -1;
protected static boolean isInit = false;
public NameSpace() throws Exception {
System.out.println("init---------namespace()");
tree2memory();
}
public NameSpace(Connection conn) throws Exception {
System.out.println("init---------namespace(conn)");
tree2memory(conn);
}
public synchronized void refrash() throws Exception{
isInit = false;
tree2memory();
}
public synchronized void refrash(Connection conn) throws Exception{
isInit = false;
tree2memory(conn);
}
/**
* 设置一些参数值。
*
* @param tablename
* 表名
* @param nsid
* 根节点名称,默认是node
* @param nsid
* 主键名称
* @param parentid
* 父键名称
* @param displayorder
* 显示顺序字段名称
* @param nsname
* 英文名称
* @param label
* 中文名称
*/
public void init(String tablename, String rootname, String nsid,
String parentid, String displayorder, String nsname, String label,
int model, String nodeName) {
_tablename = tablename;
_nsid = nsid;
_parentid = parentid;
_displayorder = displayorder;
_nsname = nsname;
_label = label;
_nodeName = nodeName;
_rootname = rootname;
}
public void tree2memory() throws Exception {
Connection conn = null;
try {
tree2memory(conn);
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
Query.close(conn);
}
}
public void tree2memory(Connection conn) throws Exception {
try {
if(!isInit){
String sql = "select * from " + _tablename;
MemoryDB.toAnotherConn(conn, sql, _tablename, MemoryDB
.getMemoConn());
isInit = true;
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
public NameSpace(String rootName, String nodeName,Connection conn) throws Exception {
this._rootname = rootName;
this._nodeName = nodeName;
tree2memory(conn);
}
public NameSpace(String rootName, String nodeName,String tablename,Connection conn) throws Exception {
this._rootname = rootName;
this._nodeName = nodeName;
this._tablename = tablename;
tree2memory(conn);
}
protected void addAttribute(MyHashMap map) throws SQLException {
String att = " ";
if("".equals(map.getString(_nsid))){
System.err.println("addAttribute(map) 的map.getString(_nsid)==空");
}
Connection conn = MemoryDB.getMemoConn();
try {
map.put("id", map.getString(_nsid));
String symbol = _nsid_Type.equals("string")?"'":"";
int childcount = new Query(conn, false)
.getFirstInt("select count(*) from " + _tablename
+ " where " + _parentid + "=" +symbol+ map.getString(_nsid)+symbol
+ "");
map.put("childcount", childcount);
String nsnameseq = getSeqence(conn, map.getString(_nsid), _nsname);
String labelseq = getSeqence(conn, map.getString(_nsid), _label);
String nsidseq = getSeqence(conn, map.getString(_nsid), _nsid);
int level = 0;
if (nsnameseq != null) {
level = nsnameseq.split("\\.").length - 1;
}
if (nsnameseq.startsWith(".")) {
nsnameseq = nsnameseq.substring(1);
labelseq = labelseq.substring(1);
level = level - 1;
}
map.put("nslevel", level);
map.put("nsid", map.getString(_nsid));
map.put("nsname", map.getString(_nsname));
map.put("label", map.getString(_label));
map.put("namesequence", nsnameseq);
map.put("labelsequence", labelseq);
map.put("nsidsequence", nsidseq);
map.put("idsequence", nsidseq);
map.put("nslevel", level);
if (childcount > 0) {
map.put("haschildren", "true");
map.put("isBranch", "true");
} else {
map.put("haschildren", "false");
map.put("isBranch", "false");
}
if(!map.getString("isdata").equals("true")){
map.put("isdata", "false");
}
} catch (SQLException e) {
throw e;
} finally {
// Query.close(conn);
}
}
/**
* 返回所有的孩子节点
*
* @param nsid
* @return
* @throws SQLException
*/
public String getAllChildNs(Connection conn, String nsid)
throws SQLException {
String str = "";
String symbol = _parentid_Type.equals("string")?"'":"";
List list = new Query(conn, false).get("select " + _nsid + " from "
+ _tablename + " where " + _parentid + "=" + symbol+ nsid+symbol
+ " order by " + _displayorder);
for (int i = 0; i < list.size(); i++) {
MyHashMap map = (MyHashMap) list.get(i);
System.out.println(map.getString(_nsid.toLowerCase()));
str += "," + map.getString(_nsid.toLowerCase())
+ getAllChildNs(conn, map.getString(_nsid.toLowerCase()));
}
return str;
}
/**
*
*/
public String getAllChildNs(String nsid) throws SQLException {
String str = "";
Connection conn = new ConnInfo().getConn();
try{
str = getAllChildNs(conn,nsid);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
Query.close(conn);
}
return str;
}
/**
* 取到所有的祖辈.
*
* @param conn
* @param nsid
* @return
* @throws SQLException
*/
public String getAllParentID(Connection conn, String nsid)
throws SQLException {
if (nsid == null || nsid.trim().equals(""))
return "";
if (conn == null) {
conn = new ConnInfo().getConn();
}
String symbol = _nsid_Type.equals("string")?"'":"";
String pname = new Query(conn, false).getFirstString("select "
+ _nsname + " from " + _tablename + " where " + _nsid
+ "=(select " + _parentid + " from " + _tablename + " where "
+ _nsid +"=" + symbol+ nsid +symbol+ ")");
String name = new Query(conn, false).getFirstString("select " + _nsname
+ " from " + _tablename + " where " + _nsid + "=" + symbol+nsid+symbol);
String pid = new Query(conn, false).getFirstStrin