Imports Microsoft.MetadirectoryServices
Imports System.IO
Imports System.Data.SqlClient
Imports OCG.Utility.Xml
Imports zbitz
Public Class MACallExport_PersonalWebsites
Implements IMAExtensibleFileImport
Implements IMAExtensibleCallExport
Const DB_TABLE_NAME As String = “PersonalWebsites”
Const DB_CONNECTION_STRING As String = “Database=MIISData;Data Source=localhost;Integrated Security=Yes;”
Dim sqlExportConnection As New SqlConnection(DB_CONNECTION_STRING)
Dim MAcol As New MAConfiguration(Utils.ExtensionsDirectory & “\MA_Params.xml”)
Dim Params As MAParams = MAcol.Parameters(“PersonalWebsites-MA”)
Dim MAName As String = Params(“ma-name”)
Public Sub GenerateImportFile(ByVal filename As String, ByVal connectTo As String, ByVal user As String, ByVal password As String, ByVal configParameters As ConfigParameterCollection, ByVal fullImport As Boolean, ByVal types As TypeDescriptionCollection, ByRef customData As String) Implements IMAExtensibleFileImport.GenerateImportFile
Dim WebRoot As String = Params(“personal-website-root”)
Dim fw As StreamWriter
Try
‘Open the output file specified in the run profile
fw = New StreamWriter(filename, False)
Catch ex As Exception
Throw New UnauthorizedAccessException(“Unable to open file: ” & filename)
End Try
fw.WriteLine(“WebPath,staffid,uid,Type,Status”)
ListFolders(WebPath, fw)
fw.Close()
End Sub
Private Sub ListFolders(ByVal path As String, ByRef fw As StreamWriter)
Dim s() As String
Dim folders As System.Collections.IEnumerator
Dim sqlQueryConnection As New SqlConnection(DB_CONNECTION_STRING)
sqlQueryConnection.Open()
s = System.IO.Directory.GetDirectories(path)
folders = s.GetEnumerator
While folders.MoveNext
Dim sqlQueryCommand As New SqlCommand(“SELECT * FROM ” & DB_TABLE_NAME & ” where WebPath=’” & folders.Current & “‘”, sqlQueryConnection)
Dim cnReader As SqlDataReader = sqlQueryCommand.ExecuteReader
Dim importLine As String
If cnReader.Read Then
‘DB columns: WebPath, staffid, uid, Status
importLine = cnReader.Item(1) & “,” & cnReader.Item(2) & “,” & cnReader.Item(3)
Else
importLine = “,,,”
End If
importLine = folders.Current & “,” & importLine
fw.WriteLine(importLine)
cnReader.Close()
End While
sqlQueryConnection.Close()
End Sub
Public Sub BeginExport(ByVal connectTo As String, ByVal user As String, ByVal password As String, ByVal configParameters As ConfigParameterCollection, ByVal types As TypeDescriptionCollection) Implements IMAExtensibleCallExport.BeginExport
sqlExportConnection.Open()
End Sub
Public Sub ExportEntry(ByVal modificationType As ModificationType, ByVal changedAttributes As String(), ByVal csentry As CSEntry) Implements IMAExtensibleCallExport.ExportEntry
Dim WebPath As String = csentry(“WebPath”).Value
If csentry(“Status”).IsPresent AndAlso csentry(“Status”).Value = “archive pending” Then
If ArchiveWebFolder(WebPath, csentry(“uid”).Value) = 0 Then
updateRow(WebPath, “archived”)
End If
ElseIf modificationType = modificationType.Delete AndAlso csentry(“Status”).Value = “archived” Then
updateRow(WebPath, “deleted”)
DeleteWebFolder(csentry)
ElseIf modificationType = modificationType.Add Then
If Not rowExists(WebPath) AndAlso csentry(“uid”).IsPresent Then
If CreateWebFolder(csentry) = 0 Then
addRow(WebPath, csentry(“staffid”).Value, csentry(“uid”).Value, “adding”)
End If
ElseIf rowExists(WebPath) And csentry(“Status”).Value = “adding” Then
If CreateWebFolder(csentry) = 0 Then
updateRow(WebPath, “active”)
End If
End If
Else
If Not rowExists(WebPath) Then
addRow(WebPath, csentry(“LBSNo”).Value, csentry(“uid”).Value, “active”)
End If
End If
End Sub
Private Function CreateWebFolder(ByVal csentry As CSEntry) As Integer
Dim script As String = Params(“script-createsite”)
RunProcess(script, csentry(“uid”).Value)
End Function
Private Function DeleteWebFolder(ByVal csentry As CSEntry) As Integer
Dim script As String = Params(“script-deletesite”)
RunProcess(script, csentry(“WebPath”).Value.Split(“\”)(4))
End Function
Private Function ArchiveWebFolder(ByVal path As String, ByVal uid As String) As Int16
Dim status As Int16
Dim archive As New Czzip
Dim archivePath As String = Params(“archive-path”) & uid & “.zip”
If System.IO.File.Exists(archivePath) Then
Throw New UnexpectedDataException(“Archive file ” & archivePath & ” already exists”)
End If
Dim logtxt = Date.Now & ” Zipping ” & path & “: “
status = archive.ZipFile(path, archivePath)
If status = 0 AndAlso Not System.IO.File.Exists(archivePath) Then
status = -1
End If
Return status
End Function
Public Sub EndExport() Implements IMAExtensibleCallExport.EndExport
End Sub
Private Function RunProcess(ByVal cmd As String, ByVal params As String) As Integer
Dim exitCode As Integer = -1
Dim proc As New System.Diagnostics.Process
Dim waitTime As Integer = 0
Const WAIT_TIME_IN_SECONDS = 5
Const ONE_SECOND = 1000
Const SLEEP_TIME_IN_SECONDS = 1
proc.StartInfo.FileName = cmd
proc.StartInfo.Arguments = params
proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal
proc.Start()
Try
If Not proc.HasExited Then
While True
System.Threading.Thread.Sleep(SLEEP_TIME_IN_SECONDS * ONE_SECOND)
waitTime += SLEEP_TIME_IN_SECONDS
If proc.HasExited Or waitTime >= WAIT_TIME_IN_SECONDS Then
Exit While
End If
End While
End If
If proc.HasExited Then
exitCode = proc.ExitCode
Else
Throw New UnexpectedDataException(“Timeout running process ” & cmd & ” ” & params)
End If
Catch ex As Exception
Throw New UnexpectedDataException(“Error: ” & ex.Message)
End Try
Return exitCode
proc.Kill()
End Function
Private Function deleteRows(ByVal WebPath As String) As Integer
Dim delSQL As String = “DELETE FROM ” & DB_TABLE_NAME & ” WHERE WebPath = ‘” & WebPath & “‘”
Dim sqlDelCommand As SqlCommand = New SqlCommand(delSQL, sqlExportConnection)
Return sqlDelCommand.ExecuteNonQuery()
End Function
Private Function addRow(ByVal WebPath As String, ByVal LBSNo As String, ByVal uid As String, ByVal Type As String, ByVal Status As String) As Integer
Dim addSQL As String = “INSERT INTO ” & DB_TABLE_NAME & ” (WebPath, staffid, uid, Status) VALUES” & “ (‘” & WebPath & “‘, ‘” & staffid & “‘, ‘” & uid & “‘, ‘” & Status & “‘)”
Dim sqlAddCommand As SqlCommand = New SqlCommand(addSQL, sqlExportConnection)
Return sqlAddCommand.ExecuteNonQuery()
End Function
Private Function updateRow(ByVal WebPath As String, ByVal Status As String) As Integer
Dim modSQL As String
Dim sqlModCommand As SqlCommand
modSQL = “UPDATE ” & DB_TABLE_NAME & ” SET Status =’” & Status & “‘ where WebPath=’” & WebPath & “‘”
sqlModCommand = New SqlCommand(modSQL, sqlExportConnection)
Return sqlModCommand.ExecuteNonQuery()
End Function
Private Function rowExists(ByVal WebPath As String) As Boolean
‘Returns TRUE if the row already exists in the table
‘Pass % for wildcard
Dim mySelectQuery As String = “SELECT * FROM ” & DB_TABLE_NAME & ” where WebPath = ‘” & WebPath & “‘”
Dim myConnection As New SqlConnection(“Database=MIISSync;Data Source=localhost;Integrated Security=Yes;”)
Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
myConnection.Open()
Dim myReader As SqlDataReader = myCommand.ExecuteReader()
rowExists = myReader.HasRows()
myReader.Close()
myConnection.Close()
End Function
Private Function escapedString(ByVal originalString As String, ByVal commandType As String) As String
Select Case commandType
Case “cmd”
escapedString = originalString.Replace(“,”, “\,”)
escapedString = escapedString.Replace(“+”, “\+”)
escapedString = escapedString.Replace(“>”, “\>”)
Return escapedString
Case “sql”
escapedString = originalString.Replace(“‘”, “””)
Return escapedString
Case Else
Throw New ArgumentException(“Valid arguments: cmd, sql”)
End Select
End Function
End Class