Imports DD_Rights
Imports System.ComponentModel

Public Class frmCheckRightsEntity
    Private DT_FILES As DataTable
    Private _error As Boolean = False
    Private countfiles As Integer = 0
    Private WorkingFiles As Integer = 0
    Private CURR_DOCID As Integer
    Private CURR_DOCPATH As String
    Private ENTITY_ID As Integer
    Dim righterrors As Boolean = False
    Private Sub frmCheckRightsEntity_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            Dim sql = String.Format("SELECT T.FORM_ID, [dbo].[FNPMO_GETOBJECTCAPTION]('{0}','FORMVIEW_TITLE' + CONVERT(VARCHAR(5), T.[FORM_VIEW_ID]), 1) AS FORM_TITLE FROM VWPMO_CONSTRUCTOR_FORMS T WHERE DOCUMENT_VIEW = 1", USER_LANGUAGE)
            Dim DT As DataTable = clsDatabase.Return_Datatable(sql)
            cmbentity.DataSource = DT
            cmbentity.ValueMember = DT.Columns(0).ColumnName
            cmbentity.DisplayMember = DT.Columns(1).ColumnName
            lblstate.Visible = False
        Catch ex As Exception
            MsgBox("Unexpected error in load form: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub btnCheckRights_Click(sender As Object, e As EventArgs) Handles btnCheckRights.Click
        If cmbentity.SelectedIndex <> -1 Then
            Try
                righterrors = False
                CURR_DOCID = 0
                ENTITY_ID = cmbentity.SelectedValue
                Me.ProgressBar1.Visible = True
                'BackgroundWorker erstellen ...
                BW_RightsEntity = New BackgroundWorker
                BW_RightsEntity.WorkerReportsProgress = True
                BW_RightsEntity.WorkerSupportsCancellation = True
                Dim sqlfiles = String.Format("SELECT T.DocID,T.FULL_FILENAME,TRL.RECORD_ID,TR.FORM_ID AS [ENTITY_ID] FROM VWPMO_DOC_SEARCH T, TBPMO_DOC_RECORD_LINK TRL, TBPMO_RECORD TR where T.DocID = TRL.DOC_ID AND TRL.RECORD_ID = TR.GUID AND TR.FORM_ID = {0} ORDER BY DocID", ENTITY_ID)
                DT_FILES = clsDatabase.Return_Datatable(sqlfiles)
                If DD_Rights.ClassWDRights.Init(chklogging.Checked, ClassDatabase.ConnectionStringRM) = False Then
                    MsgBox("Could not init rights management. " & vbNewLine & "Check logfile", MsgBoxStyle.Critical)
                    Exit Sub
                End If
                If DT_FILES.Rows.Count = 0 Then
                    MsgBox("No files were found! Please check docsearch!", MsgBoxStyle.Critical)
                    Exit Sub
                Else
                    clsLogger.Add(String.Format(">> {0} files must be Checked!", DT_FILES.Rows.Count), False)
                End If
                btncancel.Visible = True
                lblstate.Visible = True
                lblstate.Text = "Starting Background Worker...."
                countfiles = DT_FILES.Rows.Count - 1
                WorkingFiles = 0
                ProgressBar1.Maximum = DT_FILES.Rows.Count
                AddHandler BW_RightsEntity.DoWork, AddressOf bw_DoWork
                '.. und starten
                BW_RightsEntity.RunWorkerAsync()
            Catch ex As Exception
                MsgBox("Unexpected error in starting backgroundworker: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
                Me.ProgressBar1.Visible = False
            End Try
        End If
    End Sub

    Private Sub BW_RightsEntity_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BW_RightsEntity.ProgressChanged
        Dim state = "Working on file '" & WorkingFiles.ToString & "' of '" & countfiles.ToString & "'"
        lblstate.Text = state
        Me.ProgressBar1.Value = e.ProgressPercentage
    End Sub
    Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        _error = False
        Dim i As Integer = 1
        'Jede Datei einzeln verarbeiten
        For Each row As DataRow In DT_FILES.Rows
            If BW_RightsEntity.CancellationPending = True Then
                MsgBox("Backgroundworker aborting - check log!", MsgBoxStyle.Critical)
                e.Cancel = True
                Exit For
            Else
                '###
                WorkingFiles += 1
                CURR_DOCID = row.Item("DocID")
                CURR_DOCPATH = row.Item("FULL_FILENAME")
                If DD_Rights.ClassWDRights.Doc_Renew_Rights(CURR_DOCID, CURR_DOCPATH, True) Then
                    If DD_Rights.ClassWDRights.MSG_RESULT <> "" Then
                        righterrors = True
                    End If
                Else
                    MsgBox("Unexpected Error in DD_Rights.ClassWDRights.Doc_Renew_Rights - Check the log!", MsgBoxStyle.Exclamation)
                    If DD_Rights.ClassWDRights.MSG_RESULT <> "" Then
                        MsgBox(DD_Rights.ClassWDRights.MSG_RESULT, MsgBoxStyle.Information)
                    End If
                    _error = True
                End If
                '###
                'If DD_Rights.ClassRights.Collect_Users(row.Item("RECORD_ID"), row.Item("ENTITY_ID")) = True Then
                '    WorkingFiles += 1
                '    CURR_DOCID = row.Item("DocID")
                '    If DD_Rights.ClassRights.TransferRights2Service(row.Item("DocID"), row.Item("FULL_FILENAME"), 1) Then
                '        If DD_Rights.ClassRights.MSG_RESULT <> "" Then
                '            righterrors = True
                '        End If
                '    Else
                '        MsgBox("Unexpected Error in DD_Rights.ClassRights.TransferRights2Service - Check the log!", MsgBoxStyle.Exclamation)
                '        If DD_Rights.ClassRights.MSG_RESULT <> "" Then
                '            MsgBox(DD_Rights.ClassRights.MSG_RESULT, MsgBoxStyle.Information)
                '        End If
                '        _error = True
                '    End If
                'Else
                '    MsgBox("Unexpected Error in DD_Rights.ClassRights.Collect_Users - Check the log!", MsgBoxStyle.Exclamation)
                '    If DD_Rights.ClassRights.MSG_RESULT <> "" Then
                '        MsgBox(DD_Rights.ClassRights.MSG_RESULT, MsgBoxStyle.Information)
                '    End If
                '    _error = True
                'End If
                BW_RightsEntity.ReportProgress(i)
                i += 1
            End If
        Next
    End Sub

    Private Sub btncancel_Click(sender As Object, e As EventArgs) Handles btncancel.Click
        ' Cancel the asynchronous operation.
        Me.BW_RightsEntity.CancelAsync()
    End Sub
    Private Sub BW_RightsEntity_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BW_RightsEntity.RunWorkerCompleted
        Try
            btncancel.Visible = False
            lblstate.Visible = False
            ProgressBar1.Visible = False
            If _error = False Then
                'clsDatabase.Execute_non_Query("UPDATE TBPMO_SERVICE_RIGHT_CONFIG SET RUN_SERVICE = 1 WHERE GUID = 1")
                If righterrors = True Then
                    MsgBox("All rights of files belonging to entity were checked and refreshed, " & vbNewLine & "BUT some Userrights could not be set. Check the logfile!", MsgBoxStyle.Exclamation, "Attention")
                Else
                    MsgBox("All rights of files belonging to entity were checked and refreshed!", MsgBoxStyle.Information)
                End If
            Else
                MsgBox("Some errors occured while preparing and queuing rights...please check the log!" , MsgBoxStyle.Exclamation)
            End If
        Catch ex As Exception

        End Try
    End Sub
End Class