package com.smtaiserver.smtaiserver.graph; import com.smtaiserver.smtaiserver.database.SMTDatabase; import com.smtaiserver.smtaiserver.database.SMTDatabase.DBRecord; import com.smtaiserver.smtaiserver.database.SMTDatabase.DBRecords; import com.smtservlet.util.Json; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; public class SMTAIGraphNode { private String _recId; private String _otype; private String _oname; private String _title; @SuppressWarnings("unused") private Json _jsonProp; private SMTAIGraphManager _manager; private Map> _mapOTYPE2ChildId = null; private Map> _mapOTYPE2ParentId = null; public SMTAIGraphNode(SMTAIGraphManager manager, SMTDatabase db, String recId, String tableNode, String tableLink) throws Exception { _manager = manager; // 查询节点信息 DBRecords recsNode = db.querySQL("SELECT otype, oname, title, prop_json FROM " + tableNode + " T WHERE recid=?", new Object[] {recId}); if(recsNode.getRowCount() == 0) throw new Exception("can't find graph node : " + recId); DBRecord recNode = recsNode.getRecord(0); _recId = recId; _otype = recNode.getString("OTYPE"); _oname = recNode.getString("ONAME"); _title = recNode.getString("TITLE"); // 查询子节点信息 DBRecords recsChild = db.querySQL("SELECT otype, c2 FROM " + tableLink + " T WHERE c1=?", new Object[] {recId}); if(recsChild.getRowCount() > 0) { _mapOTYPE2ChildId = new HashMap<>(); for(DBRecord recChild : recsChild.getRecords()) { String OTYPE = recChild.getString("OTYPE"); Set setRECID = _mapOTYPE2ChildId.get(OTYPE); if(setRECID == null) { setRECID = new HashSet(); _mapOTYPE2ChildId.put(OTYPE, setRECID); } setRECID.add(recChild.getString("C2")); } } // 查询父节点信息 DBRecords recsParent = db.querySQL("SELECT otype, c1 FROM " + tableLink + " T WHERE c2=?", new Object[] {recId}); if(recsParent.getRowCount() > 0) { _mapOTYPE2ParentId = new HashMap<>(); for(DBRecord recParent : recsParent.getRecords()) { String OTYPE = recParent.getString("OTYPE"); Set setRECID = _mapOTYPE2ParentId.get(OTYPE); if(setRECID == null) { setRECID = new HashSet(); _mapOTYPE2ParentId.put(OTYPE, setRECID); } setRECID.add(recParent.getString("C1")); } } } public String getRecId() { return _recId; } public String getOTYPE() { return _otype; } public String getONAME() { return _oname; } public String getTitle() { return _title; } public Map getChildren(Object OTYPE) throws Exception { Map mapResult = new HashMap<>(); SMTAIGraphManager.DBCache dbCache = new SMTAIGraphManager.DBCache(); try { Set list; if(OTYPE != null) { if(OTYPE instanceof String) { list = new HashSet<>(); list.add((String)OTYPE); } else if(OTYPE instanceof String[]) { list = new HashSet<>(); for(String curOTYPE : (String[])OTYPE) { list.add(curOTYPE); } } else { throw new Exception("unknw OTYPE : " + OTYPE.getClass().toString()); } } else { list = _mapOTYPE2ChildId.keySet(); } for(String curOTYPE : list) { Set setRecId = _mapOTYPE2ChildId.get(curOTYPE); if(setRecId != null) { for(String recId : setRecId) { SMTAIGraphNode graphNode = _manager.getGraphNode(recId, dbCache); mapResult.put(graphNode.getRecId(), graphNode); } } } return mapResult; } finally { dbCache.close(); } } public Map getParents(Object OTYPE) throws Exception { Map mapResult = new HashMap<>(); SMTAIGraphManager.DBCache dbCache = new SMTAIGraphManager.DBCache(); try { Set list; if(OTYPE != null) { if(OTYPE instanceof String) { list = new HashSet<>(); list.add((String)OTYPE); } else if(OTYPE instanceof String[]) { list = new HashSet<>(); for(String curOTYPE : (String[])OTYPE) { list.add(curOTYPE); } } else { throw new Exception("unknw OTYPE : " + OTYPE.getClass().toString()); } } else { list = _mapOTYPE2ParentId.keySet(); } for(String curOTYPE : list) { Set setRecId = _mapOTYPE2ParentId.get(curOTYPE); if(setRecId != null) { for(String recId : setRecId) { SMTAIGraphNode graphNode = _manager.getGraphNode(recId, dbCache); mapResult.put(graphNode.getRecId(), graphNode); } } } return mapResult; } finally { dbCache.close(); } } }