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

'Get arguments
if WScript.Arguments.Count = 0 Then
WScript.Echo("Missing Argument: Switch Mac Address")
WScript.Quit
End If

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

'Load database to visio
On Error Resume Next
Set switchesData = visioDocument.DataRecordsets.Add(dbProvider, "Select * From Switches where Mac='" & WScript.Arguments(0) & "'", 0, "Switches")
Set nodesData = visioDocument.DataRecordsets.Add(dbProvider, "Select Nodes.Mac, Nodes.Ip, Nodes.Name, Ports.Description + ' (' + Ports.Interface + ')', Ports.Speed, Nodes.Type From Nodes left join Ports on Ports.switch = Nodes.switch and Ports.Interface = Nodes.Interface where Nodes.Switch='" & WScript.Arguments(0) & "'", 0, "Nodes")
If Err Then Set nodesData = visioDocument.DataRecordsets.Add(dbProvider, "Select Nodes.Mac, Nodes.Ip, Nodes.Name, Ports.Description + ' (' + Ports.Interface + ')', Ports.Speed From Nodes left join Ports on Ports.switch = Nodes.switch and Ports.Interface = Nodes.Interface where Nodes.Switch='" & WScript.Arguments(0) & "'", 0, "Nodes")

'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 - Switch 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"
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
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 switches
Set switchesShape = visioPeriphDoc.Masters("Switch")
Set switchesRecordset = visioDocument.DataRecordsets(1)
switceshIDs = switchesRecordset.GetDataRowIDs("")
For switchesRow = LBound(switceshIDs) + 1 To UBound(switceshIDs) + 1
Set switch = visioPage.DropLinked(switchesShape, 3.5, 6, switchesRecordset.ID, switchesRow, False)
Set switchConnector = switch.Cells("Connections.X1")
switchData = switchesRecordset.GetRowData(switchesRow)
If Len(switchData(4)) > 0 Then
switch.Text = switchData(4) & Chr(10) & switchData(2)
ElseIf Len(switchData(5)) > 0 Then
switch.Text = switchData(5) & Chr(10) & switchData(2)
ElseIf Len(switchData(2)) > 0 Then
switch.Text = switchData(2)
Else switch.Text = switchData(1)
End If
Next

'Set nodes
Set nodeShape = visioPeriphDoc.Masters("Server")
Set hostShape = visioPeriphDoc.Masters("Server")
Set printerShape = visioPeriphDoc.Masters("Printer")
Set routerShape = visioPeriphDoc.Masters("Router")
Set serverShape = visioPeriphDoc.Masters("Mainframe")
Set nodesRecordset = visioDocument.DataRecordsets(2)
nodesIDs = nodesRecordset.GetDataRowIDs("")
For nodesRow = LBound(nodesIDs) + 1 To UBound(nodesIDs) + 1
nodeData = nodesRecordset.GetRowData(nodesRow)
Set connector = visioPage.Drop(visioApplication.ConnectorToolDataObject, 1, 1)
If nodeData(4) >= 10000000000 Then
connector.CellsSRC(1, 2, 1).FormulaU = tengigaColor
ElseIf nodeData(4) >= 1000000000 Then
connector.CellsSRC(1, 2, 1).FormulaU = gigaColor
ElseIf nodeData(4) >= 100000000 Then
connector.CellsSRC(1, 2, 1).FormulaU = fastColor
ElseIf nodeData(4) >= 10000000 Then
connector.CellsSRC(1, 2, 1).FormulaU = ethernetColor
Else connector.CellsSRC(1, 2, 1).FormulaU = unknownColor
End If
If nodeData(5) = "Host" Then
Set node = visioPage.DropLinked(hostShape, 0, 0, nodesRecordset.ID, nodesRow, False)
ElseIf nodeData(5) = "Printer" Then
Set node = visioPage.DropLinked(printerShape, 0, 0, nodesRecordset.ID, nodesRow, False)
ElseIf nodeData(5) = "Router" Then
Set node = visioPage.DropLinked(routerShape, 0, 0, nodesRecordset.ID, nodesRow, False)
ElseIf nodeData(5) = "Server" Then
Set node = visioPage.DropLinked(serverShape, 0, 0, nodesRecordset.ID, nodesRow, False)
Else Set node = visioPage.DropLinked(nodeShape, 0, 0, nodesRecordset.ID, nodesRow, False)
End If
Set nodeConnector = node.Cells("Connections.X1")
connector.Cells("BeginX").GlueTo nodeConnector
connector.Cells("EndX").GlueTo switchConnector
If Len(nodeData(3)) > 0 Then connector.Text = nodeData(3)
If Len(nodeData(2)) > 0 Then
node.Text = nodeData(2) & Chr(10) & nodeData(1)
ElseIf Len(nodeData(1)) > 0 Then
node.Text = nodeData(1)
Else node.Text = nodeData(0)
End If
Next

'Set layout
switch.BringToFront
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
