MiisApp.vb: Private Sub InsertDeltaObjects


Private Sub InsertDeltaObjects(ByVal objectType As String)
    Dim sqlQueryConnection As New SqlConnection(MIISSYNC_CONNECTION_STRING)
    Dim instructionText As String
    If objectType = “person” Then
        instructionText = “Notepad is about to open. Paste in your Staff IDs, one per row, then save and close the file.”
    ElseIf objectType = “group” Then
        instructionText = “Notepad is about to open. Paste in your Group Names, one per row, then save and close the file.”
    Else
        Exit Sub
    End If
    ‘Open Notepad so the object identifiers can be pasted in
    MsgBox(instructionText, MsgBoxStyle.ApplicationModal)
    Dim myProcess As Process = System.Diagnostics.Process.Start(“notepad.exe”, ID_FILE)
    While Not myProcess.HasExited
        System.Threading.Thread.Sleep(100)
    End While

    ‘Read the identifiers into an array
    Dim myReader As New StreamReader(ID_FILE)
    Dim ids As String()
    Dim strLine As String
    i = -1
    While myReader.Peek <> -1
        strLine = myReader.ReadLine
        If strLine.Length > 0 Then
            i = i + 1
            ReDim Preserve ids(i)
            ids.SetValue(strLine, i)
        End If
    End While
    myReader.Close()
    If ids Is Nothing Then
        Exit Sub
    End If

    ‘Check the last log line
    Dim logline As String = LastLogLine()
    If logline.IndexOf(“DTS”) > 0 Then
        MsgBox(“Waiting for DTS tasks to complete…”, MsgBoxStyle.Critical)
        While logline.IndexOf(“DTS”) > 0
            System.Threading.Thread.Sleep(10000)
            logline = LastLogLine()
        End While
    End If

    ‘Run SQL inserts
    Dim msgText As String = “”
    For i = 0 To UBound(ids)
        If ids(i) <> “” Then
            If objectType = “person” Then
                sqlQueryConnection.Open()
                Dim sqlPeopleInsert As New SqlCommand(“insert into people_delta select * , ‘Modify’ from people_snapshot where staffid='” & ids(i) & “‘”, sqlQueryConnection)
                sqlPeopleInsert.ExecuteNonQuery()
                sqlQueryConnection.Close()
                sqlQueryConnection.Open()
                Dim sqlADMultiValueInsert As New SqlCommand(“insert into admultivalueobjects_delta values (” & ids(i) & “,’USER’,NULL,NULL,’Modify’,NULL)”, sqlQueryConnection)
                sqlADMultiValueInsert.ExecuteNonQuery()
                sqlQueryConnection.Close()
                sqlQueryConnection.Open()
                Dim sqlLDAPMultiValueInsert As New SqlCommand(“insert into ldapmultivalueobjects_delta values (” & ids(i) & “,’USER’,’Modify’,NULL)”, sqlQueryConnection)
                sqlLDAPMultiValueInsert.ExecuteNonQuery()
                sqlQueryConnection.Close()
                msgText = msgText & ” ” & ids(i)
            ElseIf objectType = “group” Then
                <<similar to person section, with updates relevant to group objects>>
            End If
        End If
     Next
     If msgText <> “” Then
        MsgBox(“Delta tables updated with ” & msgText & vbCrLf & “They will be included in the next Delta Imports of SchoolDB.”,  MsgBoxStyle.Information)
    End If
End Sub

Private Function LastLogLine() As String
    Dim logline As String
    Dim rowReader As SqlDataReader
    Dim sqlQueryConnection As New SqlConnection(MIISSYNC_CONNECTION_STRING)
    sqlQueryConnection.Open()
    Dim sqlQuery As New SqlCommand(“select top 1 entry from ” & ACTIVITY_LOG & ” order by item desc”, sqlQueryConnection)
    rowReader = sqlQuery.ExecuteReader
    While rowReader.Read()
        logline = rowReader.GetString(0)
    End While
    rowReader.Close()
    sqlQueryConnection.Close()
    LastLogLine = logline
End Function