001package gudusoft.gsqlparser.dlineage.graph;
002
003import gudusoft.gsqlparser.EDbVendor;
004import gudusoft.gsqlparser.dlineage.DataFlowAnalyzer;
005import gudusoft.gsqlparser.dlineage.dataflow.model.RelationshipType;
006import gudusoft.gsqlparser.dlineage.dataflow.model.json.Dataflow;
007import gudusoft.gsqlparser.dlineage.dataflow.model.xml.*;
008import gudusoft.gsqlparser.dlineage.graph.model.Data;
009import gudusoft.gsqlparser.dlineage.graph.model.Result;
010import gudusoft.gsqlparser.dlineage.graph.ui.EdgeRelation;
011import gudusoft.gsqlparser.dlineage.graph.ui.Item;
012import gudusoft.gsqlparser.dlineage.graph.ui.Label;
013import gudusoft.gsqlparser.dlineage.graph.ui.Table;
014import gudusoft.gsqlparser.dlineage.graph.utils.XMLUtil;
015import gudusoft.gsqlparser.dlineage.metadata.*;
016import gudusoft.gsqlparser.dlineage.util.Pair;
017import gudusoft.gsqlparser.dlineage.util.Pair3;
018import gudusoft.gsqlparser.dlineage.util.RemoveDataflowFunction;
019import gudusoft.gsqlparser.sqlenv.TSQLEnv;
020import gudusoft.gsqlparser.util.json.JSON;
021import org.w3c.dom.Document;
022import java.util.*;
023import java.util.regex.Matcher;
024import java.util.regex.Pattern;
025import java.util.stream.Collectors;
026
027public class DataFlowGraphGenerator {
028
029    public String genDlineageGraph(EDbVendor vendor, boolean isIndirect, dataflow dataflow) throws Exception {
030        Result result = new Result();
031        Data data = new Data();
032        data.setMode("global");
033        List<RelationshipType> relationshipTypes =new ArrayList<>();
034        if(isIndirect){
035            relationshipTypes.add(RelationshipType.fdd);
036            relationshipTypes.add(RelationshipType.fdr);
037            relationshipTypes.add(RelationshipType.fddi);
038            relationshipTypes.add(RelationshipType.frd);
039        }
040        else{
041            relationshipTypes.add(RelationshipType.fdd);
042        }
043        Map graph = getSqlflowGraph(vendor, relationshipTypes, false, false, false, dataflow, false);
044        data.setGraph(graph);
045        result.setData(data);
046        Dataflow model = DataFlowAnalyzer.getSqlflowJSONModel(dataflow, vendor);
047        data.setSqlflow(gudusoft.gsqlparser.util.json.JSON.parseObject(gudusoft.gsqlparser.util.json.JSON.toJSONString(model)));
048        result.setCode(200);
049        result.setData(data);
050        result.setMsg(dataflow.getErrors());
051        return JSON.toJSONString(result);
052
053    }
054
055    public String genERGraph(EDbVendor vendor, dataflow dataflow) throws Exception {
056        Result result = new Result();
057        Data data = new Data();
058        data.setMode("global");
059        dataflow.setRelationships(dataflow.getRelationships().stream().filter(t ->  t.getType().equals("er")).collect(Collectors.toList()));
060        List<RelationshipType> relationshipTypes = new ArrayList<>();
061        relationshipTypes.add(RelationshipType.er);
062        Map graph = getSqlflowGraph(vendor, relationshipTypes, false, false, false, dataflow, false);
063        data.setGraph(graph);
064        result.setData(data);
065        Dataflow model = DataFlowAnalyzer.getSqlflowJSONModel(dataflow, vendor);
066        data.setSqlflow(gudusoft.gsqlparser.util.json.JSON.parseObject(gudusoft.gsqlparser.util.json.JSON.toJSONString(model)));
067        result.setCode(200);
068        result.setData(data);
069        result.setMsg(dataflow.getErrors());
070        return JSON.toJSONString(result);
071
072    }
073
074    public String genDataflowGraph(Dataflow model) throws Exception {
075        Result result = new Result();
076        Data data = new Data();
077        data.setMode("global");
078        List<RelationshipType> relationshipTypes =new ArrayList<>();
079        relationshipTypes.add(RelationshipType.fdd);
080        relationshipTypes.add(RelationshipType.fdr);
081        relationshipTypes.add(RelationshipType.fddi);
082        relationshipTypes.add(RelationshipType.frd);
083        dataflow dataflow = generateGraphFromDataflow(model);
084        Map graph = getSqlflowGraph(null, relationshipTypes, false, false, false, dataflow, false);
085        data.setGraph(graph);
086        result.setData(data);
087        data.setSqlflow(gudusoft.gsqlparser.util.json.JSON.parseObject(gudusoft.gsqlparser.util.json.JSON.toJSONString(model)));
088        result.setCode(200);
089        result.setData(data);
090        result.setMsg(dataflow.getErrors());
091        return JSON.toJSONString(result);
092
093    }
094
095    private Map getSqlflowGraph(EDbVendor vendor, List<RelationshipType> relationshipTypes,
096                                boolean showLinkOnly, boolean hideColumn, boolean ignoreFunction, dataflow dataflow,
097                                boolean containGraphML) throws Exception {
098        Map graph = new LinkedHashMap();
099        DataFlowGraph dataFlowGraph = new DataFlowGraph();
100        Map<String, String> tooltipMap = new HashMap<String, String>();
101        String orientation = dataflow.getOrientation();
102
103        if (ignoreFunction && (!relationshipTypes.contains(RelationshipType.er)) && (vendor!=null)) {
104            dataflow = new RemoveDataflowFunction().removeFunction(dataflow, vendor);
105        }
106        final Pair3<String, Boolean, Set<String>> graphContent = dataFlowGraph
107                .generateDataFlowGraph(dataflow,
108                        showLinkOnly, hideColumn,
109                        relationshipTypes,
110                        tooltipMap);
111        List<Object> graphNodes = dataFlowGraph.getNodes();
112        List<relationship> relationships = dataFlowGraph.getRelationships();
113        String graphml;
114        if (graphContent != null && graphContent.first != null) {
115            graphml = graphContent.first
116                    .replace("\r\n", "\n").replace("\n", " ");
117        } else {
118            graphml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\" xmlns:java=\"http://www.yworks.com/xml/yfiles-model/1.0/java\" xmlns:sys=\"http://www.yworks.com/xml/yfiles-model/markup/primitives/2.0\" xmlns:x=\"http://www.yworks.com/xml/yfiles-model/markup/2.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:y=\"http://www.yworks.com/xml/graphml\" xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd\"></graphml>";
119        }
120
121        String graphML = graphml;
122        String xml = graphML.replace("y:", "");
123
124        int groupNum = -1;
125        int columnNum = -1;
126        int index = 0;
127        String id;
128
129        Map<String, String[]> listIdMap = new LinkedHashMap<String, String[]>();
130        Map<String, String> modelColumnIdMap = new LinkedHashMap<String, String>();
131        Map<String, String> modelTableIdMap = new LinkedHashMap<String, String>();
132        Map<String, String[]> relationshipIdMap = new LinkedHashMap<String, String[]>();
133
134        LinkedList<Table> tables = new LinkedList<Table>();
135        List<Item> items = new ArrayList<Item>();
136
137        Pattern nodePattern = Pattern.compile("<node\\s+id=\"(.+?)\".+?(<data.+?</data>)", Pattern.DOTALL);
138        Matcher nodeMatcher = nodePattern.matcher(xml);
139        Map<String, Document> nodeMap = new LinkedHashMap<>();
140        while (nodeMatcher.find()) {
141            Document document = XMLUtil.parseXmlString(nodeMatcher.group(2));
142            if (document != null) {
143                nodeMap.put(nodeMatcher.group(1), document);
144            }
145        }
146
147        boolean showCallRelation = relationshipTypes.contains(RelationshipType.call);
148
149        if (!nodeMap.isEmpty()) {
150            for (String nodeId : nodeMap.keySet()) {
151                Document node = nodeMap.get(nodeId);
152                if (XMLUtil.containsNode(node, "ProxyAutoBoundsNode")) {
153                    if (!tables.isEmpty()) {
154                        tables.getLast().setColumns(items.toArray(new Item[0]));
155                    }
156                    items.clear();
157                    groupNum++;
158                    id = "n" + groupNum;
159                    columnNum = -1;
160                    appendTableGraph(tables, nodeMap.get(nodeId), id);
161                } else {
162                    if (XMLUtil.containsText(node, "hide-table-column")) {
163                        continue;
164                    }
165                    columnNum++;
166                    id = "n" + groupNum + "::n" + columnNum;
167                    appendColumnGraph(items, nodeMap.get(nodeId), id);
168                }
169                int nodeIndex = index;
170                if(showCallRelation){
171                    nodeIndex = nodeIndex/2;
172                }
173                Object selection = graphNodes.get(nodeIndex);
174                if (selection instanceof List) {
175                    List<column> columns = (List<column>) selection;
176                    List<String> ids = new ArrayList<String>();
177                    if (columns != null) {
178                        for (column column : columns) {
179                            ids.add(column.getId());
180                            modelColumnIdMap.put(column.getId(), id);
181                        }
182                    }
183                    listIdMap.put(id, ids.toArray(new String[0]));
184                } else if (selection instanceof column) {
185                    column column = (column) selection;
186                    modelColumnIdMap.put(column.getId(), id);
187                    listIdMap.put(id, new String[]{column.getId()});
188                } else if (selection instanceof table) {
189                    table table = (table) selection;
190                    modelTableIdMap.put(table.getId(), id);
191                    listIdMap.put(id, new String[]{table.getId()});
192                } else if (selection instanceof process) {
193                    process process = (process) selection;
194                    modelTableIdMap.put(process.getId(), id);
195                    listIdMap.put(id, new String[]{process.getId()});
196                } else if (selection instanceof procedure) {
197                    procedure procedure = (procedure) selection;
198                    modelTableIdMap.put(procedure.getId(), id);
199                    listIdMap.put(id, new String[]{procedure.getId()});
200                }
201                index++;
202            }
203        }
204
205        if (!tables.isEmpty()) {
206            tables.getLast().setColumns(items.toArray(new Item[0]));
207        }
208
209        Pattern relationshipPattern = Pattern.compile("<edge\\s+id=\"(.+?)\".+?source=\"(.+?)\".+?target=\"(.+?)\">", Pattern.DOTALL);
210        Matcher relationshipMatcher = relationshipPattern.matcher(xml);
211        Map<String, String[]> relationshipMap = new HashMap<String, String[]>();
212        while (relationshipMatcher.find()) {
213            relationshipMap.put(relationshipMatcher.group(1), new String[]{relationshipMatcher.group(2), relationshipMatcher.group(3)});
214        }
215        LinkedList<EdgeRelation> edges = new LinkedList<EdgeRelation>();
216
217        int edgeNum = 0;
218        if (relationships != null) {
219            Collections.reverse(relationships);
220            Set<Pair<String, String>> fddRelations = new HashSet<Pair<String, String>>();
221            Map<Pair<String, String>, String> relationshipEdgeMap = new HashMap<Pair<String, String>, String>();
222            for (relationship relationship : relationships) {
223                targetColumn target = relationship.getTarget();
224                List<sourceColumn> sourceColumn = relationship.getSources();
225                if(RelationshipType.call.name().equals(relationship.getType())){
226                    target = relationship.getCaller();
227                    sourceColumn = relationship.getCallees();
228                    if (target != null && sourceColumn != null) {
229                        for (sourceColumn source : sourceColumn) {
230                            String sourceId = modelTableIdMap.get(source.getId());
231                            String targetId = modelTableIdMap.get(target.getId());
232                            if (sourceId != null && targetId != null) {
233                                Pair<String, String> relationshipPair = new Pair<String, String>(sourceId, targetId);
234                                if (!fddRelations.contains(relationshipPair)) {
235                                    if (RelationshipType.fdd.name().equals(relationship.getType())) {
236                                        fddRelations.add(relationshipPair);
237                                    }
238                                    if (relationshipEdgeMap.containsKey(relationshipPair)) {
239                                        if (RelationshipType.fdd.name().equals(relationship.getType())) {
240                                            relationshipIdMap.put(relationshipEdgeMap.get(relationshipPair), new String[]{relationship.getId(), relationship.getType()});
241                                        }
242                                    } else {
243                                        EdgeRelation edge = new EdgeRelation();
244                                        edge.setId("e" + edgeNum);
245                                        //call relation的箭头方向和fdd是相反的
246                                        edge.setSourceId(targetId);
247                                        edge.setTargetId(sourceId);
248                                        edges.add(edge);
249                                        relationshipIdMap.put(edge.getId(), new String[]{relationship.getId(), relationship.getType()});
250                                        relationshipEdgeMap.put(relationshipPair, edge.getId());
251                                        edgeNum++;
252                                    }
253                                }
254                            }
255                        }
256                    }
257                }
258                else {
259                    if (target != null && sourceColumn != null) {
260                        for (sourceColumn source : sourceColumn) {
261                            String sourceId = getSourceId(source, modelColumnIdMap, modelTableIdMap);
262                            String targetId = getTargetId(target, modelColumnIdMap, modelTableIdMap);
263                            if (sourceId != null && targetId != null) {
264                                Pair<String, String> relationshipPair = new Pair<String, String>(sourceId, targetId);
265                                if (!fddRelations.contains(relationshipPair)) {
266                                    if (RelationshipType.fdd.name().equals(relationship.getType())) {
267                                        fddRelations.add(relationshipPair);
268                                    }
269                                    if (relationshipEdgeMap.containsKey(relationshipPair)) {
270                                        if (RelationshipType.fdd.name().equals(relationship.getType())) {
271                                            relationshipIdMap.put(relationshipEdgeMap.get(relationshipPair), new String[]{relationship.getId(), relationship.getType()});
272                                        }
273                                    } else {
274                                        EdgeRelation edge = new EdgeRelation();
275                                        edge.setId("e" + edgeNum);
276                                        edge.setSourceId(sourceId);
277                                        edge.setTargetId(targetId);
278                                        edges.add(edge);
279                                        relationshipIdMap.put(edge.getId(), new String[]{relationship.getId(), relationship.getType()});
280                                        relationshipEdgeMap.put(relationshipPair, edge.getId());
281                                        edgeNum++;
282                                    }
283                                }
284                            }
285                        }
286                    }
287                }
288            }
289        }
290
291        Map graphObject = new LinkedHashMap();
292        graphObject.put("tables", tables.toArray(new Table[0]));
293        graphObject.put("edges", edges.toArray(new EdgeRelation[0]));
294
295        graph.put("elements", graphObject);
296        graph.put("tooltip", tooltipMap);
297        graph.put("listIdMap", listIdMap);
298        graph.put("relationshipIdMap", relationshipIdMap);
299
300        if (containGraphML) {
301            graph.put("graphML", graphML);
302        }
303        Pair3<Map, dataflow, Set<String>> pair =  new Pair3<>(graph, dataflow, graphContent.third);
304        return pair.first;
305    }
306
307    private void appendColumnGraph(List<Item> items, Document document, String id) {
308        Item item = new Item();
309        item.setId(id);
310        String content = XMLUtil.getFirstNodeLabelTextRecursive(document.getDocumentElement());
311        Label label = new Label();
312        label.setContent(content);
313        item.setLabel(label);
314        items.add(item);
315    }
316
317    private void appendTableGraph(List<Table> tables, Document document, String id) {
318        Table table = new Table();
319        table.setId(id);
320        String content = XMLUtil.getFirstNodeLabelTextRecursive(document.getDocumentElement());
321        Label label = new Label();
322        label.setContent(content);
323        table.setLabel(label);
324        tables.add(table);
325    }
326
327    private String getTargetId(targetColumn target, Map<String, String> modelColumnIdMap, Map<String, String> modelTableIdMap) {
328        if (target.getId() != null) {
329            String targetId = modelColumnIdMap.get(target.getId());
330            if (targetId != null) {
331                return targetId;
332            } else if (target.getTarget_id() != null) {
333                return modelTableIdMap.get(target.getTarget_id());
334            }
335        }
336        return null;
337    }
338
339
340    private String getSourceId(sourceColumn source, Map<String, String> modelColumnIdMap, Map<String, String> modelTableIdMap) {
341        if (source.getId() != null) {
342            String sourceId = modelColumnIdMap.get(source.getId());
343            if (sourceId != null) {
344                return sourceId;
345            } else if (source.getSource_id() != null) {
346                return modelTableIdMap.get(source.getSource_id());
347            }
348        } else if (source.getSource_id() != null) {
349            return modelTableIdMap.get(source.getSource_id());
350        }
351        return null;
352    }
353
354    private gudusoft.gsqlparser.dlineage.dataflow.model.xml.table totable(gudusoft.gsqlparser.dlineage.metadata.Table table){
355        gudusoft.gsqlparser.dlineage.dataflow.model.xml.table aTable = new table();
356        aTable.setId(table.getId());
357        aTable.setName(table.getName());
358        aTable.setType(table.getType());
359        aTable.setDisplayName(table.getDisplayName());
360        aTable.setSubType(table.getSubType());
361        aTable.setCoordinate(table.getCoordinates().toString());
362        List<gudusoft.gsqlparser.dlineage.dataflow.model.xml.column> columns = new ArrayList<>();
363        aTable.setColumns(columns);
364        for(gudusoft.gsqlparser.dlineage.metadata.Column col: table.getColumns()){
365            gudusoft.gsqlparser.dlineage.dataflow.model.xml.column column = new column();
366            columns.add(column);
367            column.setId(col.getId());
368            column.setName(col.getName());
369            column.setSource(col.getSource());
370            column.setDataType(col.getDataType());
371            column.setCoordinate(col.getCoordinates().toString());
372            column.setPrimaryKey(col.isPrimaryKey());
373            column.setUnqiueKey(col.isUnqiueKey());
374            column.setIndexKey(col.isIndexKey());
375            column.setForeignKey(col.isForeignKey());
376        }
377        return aTable;
378    }
379    private dataflow generateGraphFromDataflow(Dataflow dataFlow) {
380        dataflow dataflow = new dataflow();
381        List<gudusoft.gsqlparser.dlineage.dataflow.model.xml.table> tables = new ArrayList<>();
382
383        if (dataFlow.getDbobjs() != null) {
384            Sqlflow sqlflow = dataFlow.getDbobjs();
385            if (sqlflow.getServers() != null) {
386                for (Server server : sqlflow.getServers()) {
387                    if (server.isSupportsCatalogs() && server.isSupportsSchemas()) {
388                        if (server.getDatabases() == null)
389                            continue;
390                        for (Database database : server.getDatabases()) {
391                            if (database.getSchemas() == null)
392                                continue;
393                            for (Schema schema : database.getSchemas()) {
394                                if (schema.getTables() != null) {
395                                    for (gudusoft.gsqlparser.dlineage.metadata.Table table : schema.getTables()) {
396                                        gudusoft.gsqlparser.dlineage.dataflow.model.xml.table aTable = totable(table);
397                                        if (server != null && !TSQLEnv.DEFAULT_SERVER_NAME.equalsIgnoreCase(server.getName())) {
398                                            aTable.setServer(server.getName());
399                                        }
400                                        if (database.getName() != null && !TSQLEnv.DEFAULT_DB_NAME.equalsIgnoreCase(database.getName())) {
401                                            aTable.setDatabase(database.getName());
402                                        }
403                                        if (schema.getName() != null && !TSQLEnv.DEFAULT_SCHEMA_NAME.equalsIgnoreCase(schema.getName())) {
404                                            aTable.setSchema(schema.getName());
405                                        }
406                                        tables.add(aTable);
407                                    }
408                                }
409                                if (schema.getViews() != null) {
410                                    for (gudusoft.gsqlparser.dlineage.metadata.Table table : schema.getViews()) {
411                                        gudusoft.gsqlparser.dlineage.dataflow.model.xml.table aTable = totable(table);
412                                        if (server != null && !TSQLEnv.DEFAULT_SERVER_NAME.equalsIgnoreCase(server.getName())) {
413                                            aTable.setServer(server.getName());
414                                        }
415                                        if (database.getName() != null && !TSQLEnv.DEFAULT_DB_NAME.equalsIgnoreCase(database.getName())) {
416                                            aTable.setDatabase(database.getName());
417                                        }
418                                        if (schema.getName() != null && !TSQLEnv.DEFAULT_SCHEMA_NAME.equalsIgnoreCase(schema.getName())) {
419                                            aTable.setSchema(schema.getName());
420                                        }
421                                        tables.add(aTable);
422                                    }
423                                }
424                                if (schema.getOthers() != null) {
425                                    for (gudusoft.gsqlparser.dlineage.metadata.Table table : schema.getOthers()) {
426                                        gudusoft.gsqlparser.dlineage.dataflow.model.xml.table aTable = totable(table);
427                                        if (server != null && !TSQLEnv.DEFAULT_SERVER_NAME.equalsIgnoreCase(server.getName())) {
428                                            aTable.setServer(server.getName());
429                                        }
430                                        if (database.getName() != null && !TSQLEnv.DEFAULT_DB_NAME.equalsIgnoreCase(database.getName())) {
431                                            aTable.setDatabase(database.getName());
432                                        }
433                                        if (schema.getName() != null && !TSQLEnv.DEFAULT_SCHEMA_NAME.equalsIgnoreCase(schema.getName())) {
434                                            aTable.setSchema(schema.getName());
435                                        }
436                                        tables.add(aTable);
437                                    }
438                                }
439                            }
440                        }
441                    } else if (server.isSupportsCatalogs() && server.getDatabases() != null) {
442                        for (Database database : server.getDatabases()) {
443                            if (database.getTables() != null) {
444                                for (gudusoft.gsqlparser.dlineage.metadata.Table table : database.getTables()) {
445                                    gudusoft.gsqlparser.dlineage.dataflow.model.xml.table aTable = totable(table);
446                                    if (server != null && !TSQLEnv.DEFAULT_SERVER_NAME.equalsIgnoreCase(server.getName())) {
447                                        aTable.setServer(server.getName());
448                                    }
449                                    if (database.getName() != null && !TSQLEnv.DEFAULT_DB_NAME.equalsIgnoreCase(database.getName())) {
450                                        aTable.setDatabase(database.getName());
451                                    }
452                                    tables.add(aTable);
453                                }
454                            }
455                            if (database.getViews() != null) {
456                                for (gudusoft.gsqlparser.dlineage.metadata.Table table : database.getViews()) {
457                                    gudusoft.gsqlparser.dlineage.dataflow.model.xml.table aTable = totable(table);
458                                    if (server != null && !TSQLEnv.DEFAULT_SERVER_NAME.equalsIgnoreCase(server.getName())) {
459                                        aTable.setServer(server.getName());
460                                    }
461                                    if (database.getName() != null && !TSQLEnv.DEFAULT_DB_NAME.equalsIgnoreCase(database.getName())) {
462                                        aTable.setDatabase(database.getName());
463                                    }
464                                    tables.add(aTable);
465                                }
466                            }
467                            if (database.getOthers() != null) {
468                                for (gudusoft.gsqlparser.dlineage.metadata.Table table : database.getOthers()) {
469                                    gudusoft.gsqlparser.dlineage.dataflow.model.xml.table aTable = totable(table);
470                                    if (server != null && !TSQLEnv.DEFAULT_SERVER_NAME.equalsIgnoreCase(server.getName())) {
471                                        aTable.setServer(server.getName());
472                                    }
473                                    if (database.getName() != null && !TSQLEnv.DEFAULT_DB_NAME.equalsIgnoreCase(database.getName())) {
474                                        aTable.setDatabase(database.getName());
475                                    }
476                                    tables.add(aTable);
477                                }
478                            }
479                        }
480                    } else if (server.isSupportsSchemas() && server.getSchemas() != null) {
481                        for (Schema schema : server.getSchemas()) {
482                            if (schema.getTables() != null) {
483                                for (gudusoft.gsqlparser.dlineage.metadata.Table table : schema.getTables()) {
484                                    gudusoft.gsqlparser.dlineage.dataflow.model.xml.table aTable = totable(table);
485                                    if (server != null && !TSQLEnv.DEFAULT_SERVER_NAME.equalsIgnoreCase(server.getName())) {
486                                        aTable.setServer(server.getName());
487                                    }
488                                    if (schema.getName() != null && !TSQLEnv.DEFAULT_SCHEMA_NAME.equalsIgnoreCase(schema.getName())) {
489                                        aTable.setSchema(schema.getName());
490                                    }
491                                    tables.add(aTable);
492                                }
493                            }
494                            if (schema.getViews() != null) {
495                                for (gudusoft.gsqlparser.dlineage.metadata.Table table : schema.getViews()) {
496                                    gudusoft.gsqlparser.dlineage.dataflow.model.xml.table aTable = totable(table);
497                                    if (server != null && !TSQLEnv.DEFAULT_SERVER_NAME.equalsIgnoreCase(server.getName())) {
498                                        aTable.setServer(server.getName());
499                                    }
500                                    if (schema.getName() != null && !TSQLEnv.DEFAULT_SCHEMA_NAME.equalsIgnoreCase(schema.getName())) {
501                                        aTable.setSchema(schema.getName());
502                                    }
503                                    tables.add(aTable);
504                                }
505                            }
506                            if (schema.getOthers() != null) {
507                                for (gudusoft.gsqlparser.dlineage.metadata.Table table : schema.getOthers()) {
508                                    gudusoft.gsqlparser.dlineage.dataflow.model.xml.table aTable = totable(table);
509                                    if (server != null && !TSQLEnv.DEFAULT_SERVER_NAME.equalsIgnoreCase(server.getName())) {
510                                        aTable.setServer(server.getName());
511                                    }
512                                    if (schema.getName() != null && !TSQLEnv.DEFAULT_SCHEMA_NAME.equalsIgnoreCase(schema.getName())) {
513                                        aTable.setSchema(schema.getName());
514                                    }
515                                    tables.add(aTable);
516                                }
517                            }
518                        }
519                    }
520                }
521            }
522        }
523
524        for (gudusoft.gsqlparser.dlineage.dataflow.model.xml.table dbObject : tables) {
525            if (dbObject.getColumns() == null)
526                continue;
527            table table = new table();
528            table.setId(dbObject.getId());
529            table.setCoordinate("[{\"x\":-1,\"y\":-1,\"hashCode\":\"0\"},{\"x\":-1,\"y\":-1,\"hashCode\":\"0\"}]");
530            table.setName(dbObject.getFullName());
531            table.setDisplayName(dbObject.getDisplayName());
532            table.setDatabase(dbObject.getDatabase());
533            table.setSchema(dbObject.getSchema());
534            table.setType(dbObject.getType());
535            table.setMore(dbObject.getMore());
536            switch (dbObject.getType()) {
537                case "table":
538                    dataflow.getTables().add(table);
539                    break;
540                case "view":
541                case "materialized view":
542                    dataflow.getViews().add(table);
543                    break;
544                case "resultset":
545                    if (table.getSubType() != null) {
546                        table.setType(table.getSubType());
547                        table.setSubType(null);
548                    }
549                    dataflow.getResultsets().add(table);
550                    break;
551                case "variable":
552                    dataflow.getVariables().add(table);
553                    break;
554                case "path":
555                    dataflow.getPaths().add(table);
556                    break;
557                case "stream":
558                    dataflow.getStreams().add(table);
559                    break;
560                case "datasource":
561                    dataflow.getDatasources().add(table);
562                    break;
563                case "stage":
564                    dataflow.getStages().add(table);
565                    break;
566                case "sequence":
567                    dataflow.getSequences().add(table);
568                    break;
569                case "schema":
570                    dataflow.getSchemas().add(table);
571                    break;
572                case "database":
573                    dataflow.getDatabases().add(table);
574                    break;
575                default:
576                    dataflow.getResultsets().add(table);
577            }
578
579            table.setColumns(new ArrayList<>());
580            for (gudusoft.gsqlparser.dlineage.dataflow.model.xml.column columnObject : dbObject.getColumns()) {
581                if (columnObject == null) {
582                    continue;
583                }
584                column column = new column();
585                column.setId(columnObject.getId());
586                column.setCoordinate("[{\"x\":-1,\"y\":-1,\"hashCode\":\"0\"},{\"x\":-1,\"y\":-1,\"hashCode\":\"0\"}]");
587                column.setName(columnObject.getName());
588                column.setDataType(columnObject.getDataType());
589                column.setPrimaryKey(columnObject.isPrimaryKey());
590                column.setIndexKey(columnObject.isIndexKey());
591                column.setUnqiueKey(columnObject.isUnqiueKey());
592                column.setForeignKey(columnObject.isForeignKey());
593                table.getColumns().add(column);
594            }
595        }
596
597        dataflow.setRelationships(new ArrayList<>());
598        for (gudusoft.gsqlparser.dlineage.dataflow.model.json.Relationship relationshipObject : dataFlow.getRelationships()) {
599            if (relationshipObject == null ||
600                    relationshipObject.getTarget() == null ||
601                    relationshipObject.getSources() == null ||
602                    relationshipObject.getSources().length == 0) {
603                continue;
604            }
605            relationship relationship = new relationship();
606            relationship.setId(relationshipObject.getId());
607            relationship.setType(relationshipObject.getType());
608            relationship.setEffectType(relationshipObject.getEffectType());
609            relationship.setTimestampMin(relationshipObject.getTimestampMin());
610            relationship.setTimestampMax(relationshipObject.getTimestampMax());
611
612            targetColumn targetColumn = new targetColumn();
613            targetColumn.setId(relationshipObject.getTarget().getId());
614            targetColumn.setParent_name(relationshipObject.getTarget().getParentName());
615            targetColumn.setParent_id(relationshipObject.getTarget().getParentId());
616            targetColumn.setColumn(relationshipObject.getTarget().getColumn());
617            relationship.setTarget(targetColumn);
618
619            relationship.setSources(new ArrayList<>());
620            if (relationshipObject.getSources() != null) {
621                for (gudusoft.gsqlparser.dlineage.dataflow.model.json.RelationshipElement sourceObject : relationshipObject.getSources()) {
622                    sourceColumn sourceColumn = new sourceColumn();
623                    sourceColumn.setParent_name(sourceObject.getParentName());
624                    sourceColumn.setParent_id(sourceObject.getParentId());
625                    sourceColumn.setColumn(sourceObject.getColumn());
626                    sourceColumn.setId(sourceObject.getId());
627                    relationship.getSources().add(sourceColumn);
628                }
629            }
630            dataflow.getRelationships().add(relationship);
631        }
632        dataflow.setOrientation(dataFlow.getOrientation());
633        return dataflow;
634    }
635}