'Const dbProvider = "Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\Program Files\Switch Center\SwitchCenterDB.mdb;"
'Const dbProvider = "{SQL Server};SERVER=MySQLServer;DATABASE=MySQLDatabase;"

'Add new visio page 
Set visioApplication = WScript.CreateObject("Visio.Application", "vsoEvent_")
Set visioPeriphDoc = visioApplication.Documents.Open("PERIPH_M.VSS")
Set visioDocument = visioApplication.Documents.Add("")
Set visioPage = visioDocument.Pages(1)

'Load database to visio
Set rootData = visioDocument.DataRecordsets.Add(dbProvider, "Select * From Switches where UplinkInterface=''", 0, "Root")
Set switchesData = visioDocument.DataRecordsets.Add(dbProvider, "select Switches.Mac, Switches.Ip, Switches.Name, Switches.SysName, Switches.DownlinkMac, (select Ports.Description From Ports where Ports.Switch = Switches.Mac and Ports.Interface = Switches.UplinkInterface) + ' (' + Switches.UplinkInterface + ')', (select Ports.Description From Ports where Ports.Switch = Switches.DownlinkMac and Ports.Interface = Switches.DownlinkInterface) + ' (' + Switches.DownlinkInterface + ')', (select Ports.Speed From Ports where Ports.Switch = Switches.DownlinkMac and Ports.Interface = Switches.DownlinkInterface) From Switches", 0, "Switches")

'Set colors
tengigaColor = "RGB(0,102,0)"
gigaColor = "RGB(153,51,204)"
fastColor = "RGB(255,153,0)"
ethernetColor = "RGB(51,204,255)"
unknownColor = "RGB(192,192,192)"

'Set legend
Set visioSelection = visioApplication.ActiveWindow.Selection
Set titleRect = visioPage.DrawRectangle(0, 11, 3, 10.4)
titleRect.CellsSRC(1, 3, 0).FormulaU = "RGB(204,213,176)"
titleRect.Text = "Switch Center - Network Topology"
titleRect.CellsSRC(1, 11, 4) = 0
titleRect.CellsSRC(1, 2, 3) = 5
Set subtitleRect = visioPage.DrawRectangle(0, 10.7, 3, 10.4)
subtitleRect.CellsSRC(1, 3, 0).FormulaU = "RGB(224,230,206)"
subtitleRect.Text = "Link Speed Legend"
Set legendRect = visioPage.DrawRectangle(0, 10.4, 3, 9.2)
Set tengigaLink = visioPage.DrawLine(0.2, 10.2, 2.8, 10.2)
tengigaLink.CellsSRC(1, 2, 1).FormulaU = tengigaColor
tengigaLink.Text = "Ten Giga Ethernet"
Set gigaLink = visioPage.DrawLine(0.2, 10, 2.8, 10)
gigaLink.CellsSRC(1, 2, 1).FormulaU = gigaColor
gigaLink.Text = "Giga Ethernet"
Set fastLink = visioPage.DrawLine(0.2, 9.8, 2.8, 9.8)
fastLink.CellsSRC(1, 2, 1).FormulaU = fastColor
fastLink.Text = "Fast Ethernet"
Set ethernetLink = visioPage.DrawLine(0.2, 9.6, 2.8, 9.6)
ethernetLink.CellsSRC(1, 2, 1).FormulaU = ethernetColor
ethernetLink.Text = "Ethernet"
Set unknownLink = visioPage.DrawLine(0.2, 9.4, 2.8, 9.4)
unknownLink.CellsSRC(1, 2, 1).FormulaU = unknownColor
unknownLink.Text = "Unknown/Other"
Set commentRect = visioPage.DrawRectangle(0, 9.2, 3, 8.9)
commentRect.Text = "Double-click SWITCH for more details" 
commentRect.CellsSRC(3, 0, 1).FormulaU = "RGB(0,66,26)"
visioSelection.Select titleRect, 2
visioSelection.Select subtitleRect, 2
visioSelection.Select legendRect, 2
visioSelection.Select tengigaLink, 2
visioSelection.Select gigaLink, 2
visioSelection.Select fastLink, 2
visioSelection.Select ethernetLink, 2
visioSelection.Select unknownLink, 2
visioSelection.Select commentRect, 2
Set legend = visioSelection.Group
legendRect.SendToBack

'Set logo
Set logo = visioPage.DrawRectangle(1.3, 0, 5.3, 0)
logo.Text = "Lan-Secure Switch center (www.lan-secure.com)"
logo.CellsSRC(3, 0, 1).FormulaU = "RGB(0,66,26)" 
logo.CellsSRC(1, 2, 2) = 0

'Set root
Set rootRecordset = visioDocument.DataRecordsets(1)
rootIDs = rootRecordset.GetDataRowIDs("")
rootCount = UBound(rootIDs) + 1
if rootCount > 1 Then
Set ethernetShape = visioPeriphDoc.Masters("Ethernet")
Set root = visioPage.Drop(ethernetShape, 4, 10)
Set rootConnector = root.Cells("Connections.X1")
End If

'Set switches
switchIndex = 0
ReDim SwitchesArray(switchIndex)
Set switchesShape = visioPeriphDoc.Masters("Switch")
Set switchesRecordset = visioDocument.DataRecordsets(2)
switchesIDs = switchesRecordset.GetDataRowIDs("")
For switchesRow = LBound(switchesIDs) + 1 To UBound(switchesIDs) + 1
switchesData = switchesRecordset.GetRowData(switchesRow)
Set switchesArray(switchIndex) = visioPage.DropLinked(switchesShape, 3.5, 6, switchesRecordset.ID, switchesRow, False)
switchesArray(switchIndex).Data1 = switchesData(0)
If Len(switchesData(2)) > 0 Then
switchesArray(switchIndex).Text = switchesData(2) & Chr(10) & switchesData(1)
ElseIf Len(switchesData(3)) > 0 Then
switchesArray(switchIndex).Text = switchesData(3) & Chr(10) & switchesData(1)
ElseIf Len(switchesData(1)) > 0 Then
switchesArray(switchIndex).Text = switchesData(1)
Else switchesArray(switchIndex).Text = switchesData(0)
End If
switchIndex = switchIndex + 1
ReDim Preserve switchesArray(switchIndex)
Next

'Connect switches
For switchesIndex = LBound(switchesArray) To UBound(switchesArray) - 1
switchRow = switchesArray(switchesIndex).GetLinkedDataRow(switchesRecordset.ID)
switchData = switchesRecordset.GetRowData(switchRow)
If Len(switchData(4)) > 0 Then
	For parentIndex = LBound(switchesArray) To UBound(switchesArray) - 1
	parentRow = switchesArray(parentIndex).GetLinkedDataRow(switchesRecordset.ID)
	parentData = switchesRecordset.GetRowData(parentRow)
	If switchData(4) = parentData(0) Then
	Set connector = visioPage.Drop(visioApplication.ConnectorToolDataObject, 1, 1)
	If switchData(7) >= 10000000000 Then
	connector.CellsSRC(1, 2, 1).FormulaU = tengigaColor
	ElseIf switchData(7) >= 1000000000 Then
	connector.CellsSRC(1, 2, 1).FormulaU = gigaColor
	ElseIf switchData(7) >= 100000000 Then
	connector.CellsSRC(1, 2, 1).FormulaU = fastColor
	ElseIf switchData(7) >= 10000000 Then
	connector.CellsSRC(1, 2, 1).FormulaU = ethernetColor
	Else connector.CellsSRC(1, 2, 1).FormulaU = unknownColor
	End If
	switchText = switchesArray(switchesIndex).Text
	parentText = switchesArray(parentIndex).Text
	If InStr(switchText, Chr(10)) > 0 Then switchText = Left(switchText, InStr(switchText, Chr(10)) - 1)
	If InStr(parentText, Chr(10)) > 0 Then parentText = Left(parentText, InStr(parentText, Chr(10)) - 1)
	connector.Text = switchText & " :" & switchData(5) & Chr(10) & parentText & " :" & switchData(6)
	Set parentConnector = switchesArray(parentIndex).Cells("Connections.X1")
	Set switchConnector = switchesArray(switchesIndex).Cells("Connections.X1")
	connector.Cells("BeginX").GlueTo parentConnector
	connector.Cells("EndX").GlueTo switchConnector
	switchesArray(switchesIndex).BringToFront
	switchesArray(parentIndex).BringToFront
	Exit For
	End If
	Next
Else if rootCount > 1 Then
	Set connector = visioPage.DrawLine(1, 1, 5, 5)
	Set switchConnector = switchesArray(switchesIndex).Cells("Connections.X1")
	connector.Cells("BeginX").GlueTo rootConnector
	connector.Cells("EndX").GlueTo switchConnector
	switchesArray(switchesIndex).BringToFront
	root.BringToFront
	End If
End If
Next

'Set layout
visioPeriphDoc.Close
legend.SetCenter 0, 11
visioApplication.ActiveWindow.DeselectAll
visioPage.PageSheet.CellsSRC(1, 24, 0).FormulaU = "TRUE"
visioPage.PageSheet.CellsSRC(1, 24, 8).ResultIU = 3
visioPage.PageSheet.CellsSRC(1, 24, 9).ResultIU = 16
visioPage.Layout

'Wait for events
while true
WScript.Sleep(1000)
WEnd

Sub vsoEvent_BeforeQuit(app)
WScript.Quit
End Sub

Sub vsoEvent_BeforeShapeTextEdit(shape)
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "VisioSwitchTopology.vbs " & shape.Data1, 1, true
End Sub
