-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataHelper.vb
119 lines (96 loc) · 4 KB
/
DataHelper.vb
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
Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Linq
Namespace TreeViewReport
Public Class DataHelper
Private levelField As Integer
Private nodesCount As Integer
Private nodesListField As List(Of ParentNode)
Private rows As List(Of DataRow)
Private dataTable As DataTable
Public ReadOnly Property ReportDataSource As DataTable
Get
Return dataTable
End Get
End Property
Public Property Level As Integer
Public ReadOnly Property NodesList As List(Of ParentNode)
Get
Return nodesListField
End Get
End Property
Public Sub New(ByVal level As Integer)
Me.Level = level
GenerateDataTable()
nodesListField = New List(Of ParentNode)()
rows = dataTable.AsEnumerable().ToList()
nodesCount = dataTable.Rows.Count - 1
ParseTree(Nothing)
End Sub
Private Sub GenerateDataTable()
dataTable = New DataTable()
dataTable.Columns.Add("Key", GetType(Object))
dataTable.Columns.Add("ParentKey", GetType(Object))
dataTable.Columns.Add("Name", GetType(String))
dataTable.Columns.Add("Description", GetType(String))
dataTable.Rows.Add(0, Nothing, "RootNode", "RootNode")
Dim parentKey As Integer = 0
Dim currentKey As Integer = 1
Dim rnd As Random = New Random()
For levelCount As Integer = 1 To Level
Dim childCount As Integer = rnd.Next(1, 4)
For j As Integer = 1 To childCount
Dim name As String = String.Format("Child Node {0}", currentKey)
Dim description As String = String.Format("Child Node {0} Parent {1}", currentKey, parentKey)
dataTable.Rows.Add(currentKey, parentKey, name, description)
currentKey += 1
Next
parentKey += 1
Next
End Sub
Private Function CreateNewNode(ByVal dataRow As DataRow) As ParentNode
Dim node As ParentNode = New ParentNode()
node.NodeKey = dataRow(0)
node.ParentNodeKey = dataRow(1)
node.NodeName = dataRow(2).ToString()
node.NodeDescription = dataRow(3).ToString()
node.Children = New List(Of ParentNode)()
Return node
End Function
Private Sub ParseTree(ByVal parent As ParentNode)
If parent IsNot Nothing Then 'create child nodes
Dim foundRows As List(Of DataRow) = rows.FindAll(Function(x) x.ItemArray(1).Equals(parent.NodeKey))
For Each row As DataRow In foundRows
If nodesCount >= 0 Then
Dim node As ParentNode = CreateNewNode(row)
parent.Children.Add(node)
node.Level = Threading.Interlocked.Increment(levelField)
ParseTree(node)
nodesCount -= 1
levelField -= 1
End If
Next ' create root ParentNode
Else
Dim foundRow As DataRow = rows.Find(Function(x) x.ItemArray(1).Equals(DBNull.Value))
Dim node As ParentNode = CreateNewNode(foundRow)
node.Level = levelField
nodesListField.Add(node)
rows.Remove(foundRow)
nodesCount -= 1
ParseTree(node)
End If
End Sub
End Class
Public Class SimpleNode
Public Property NodeKey As Object
Public Property ParentNodeKey As Object
Public Property NodeName As String
Public Property NodeDescription As String
End Class
Public Class ParentNode
Inherits SimpleNode
Public Property Children As List(Of ParentNode)
Public Property Level As Integer
End Class
End Namespace