TangCheng
2025-02-28 d787e447e95c7b897c2cc9c0e832f8d2e5084934
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package com.smtaiserver.smtaiserver.graph;
 
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
 
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;
 
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<String, Set<String>>    _mapOTYPE2ChildId = null;
    private Map<String, Set<String>>    _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<String> setRECID = _mapOTYPE2ChildId.get(OTYPE);
                if(setRECID == null)
                {
                    setRECID = new HashSet<String>();
                    _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<String> setRECID = _mapOTYPE2ParentId.get(OTYPE);
                if(setRECID == null)
                {
                    setRECID = new HashSet<String>();
                    _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<String, SMTAIGraphNode> getChildren(Object OTYPE) throws Exception
    {
        Map<String, SMTAIGraphNode> mapResult = new HashMap<>();
        
        SMTAIGraphManager.DBCache dbCache = new SMTAIGraphManager.DBCache();
        try
        {
            Set<String> 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<String> 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<String, SMTAIGraphNode> getParents(Object OTYPE) throws Exception
    {
        Map<String, SMTAIGraphNode> mapResult = new HashMap<>();
        
        SMTAIGraphManager.DBCache dbCache = new SMTAIGraphManager.DBCache();
        try
        {
            Set<String> 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<String> 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();
        }
    }
}