Imports System.ComponentModel
Imports System.Text.RegularExpressions
Imports DevExpress.XtraGrid.Views.Base

Public Class frmAdministration
    Public Shared _Namenkonvention As String
    Public Shared _aktDokart_Id As Integer

    Private SourceAttributes As List(Of String)
    Private SourceObjectTypes As List(Of String)

    Private Current_ObjectType As String = ""

    Private GroupToDelete As Integer = Nothing
    Private IsInsert As Boolean = False
    Dim frmloaded As Boolean = False
    Private Sub TBDD_DOKUMENTARTBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
        Save_Dokumentart()
    End Sub

    Private Sub SetStatus(Text As String)
        txtStatus.Caption = Text & " - " & Now.ToShortTimeString
    End Sub

    Private Sub ClearStatus()
        txtStatus.Caption = String.Empty
    End Sub

    Private Sub frmAdministration_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Me.TBDD_DOKUMENTARTTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBDD_EINGANGSARTENTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBDD_MODULESTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBDD_DOKART_MODULETableAdapter.Connection.ConnectionString = MyConnectionString
            TBDD_INDEX_MANTableAdapter.Connection.ConnectionString = MyConnectionString
            TBDD_INDEX_AUTOMTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBDD_USERTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBGI_CONFIGURATIONTableAdapter.Connection.ConnectionString = MyConnectionString
            'Me.TBGI_OBJECTTYPE_EMAIL_INDEXTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBDD_INDEX_MAN_POSTPROCESSINGTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBWHDD_INDEX_MANTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.VWGI_USER_GROUPS_RELATIONTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBGI_REGEX_DOCTYPETableAdapter.Connection.ConnectionString = MyConnectionString
        Catch ex As Exception
            MsgBox("Error in load Connection-Strings - Check Database Connection - Form will be closed: " & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
            Me.Close()
        End Try

        Dim oDragDropManager As New ClassDragDrop()
        oDragDropManager.AddGridView(viewAssignedGroups)
        oDragDropManager.AddGridView(viewAvailableGroups)
        oDragDropManager.AddGridView(viewAssignedUsers)
        oDragDropManager.AddGridView(viewAvailableUsers)

        Dim oDatatable As New DataTable()
        oDatatable.Columns.Add("VALUE_TEXT")
        oDatatable.Columns.Add("DISPLAY_TEXT")

        oDatatable.Rows.Add("Default", "Datei Überschreiben")
        oDatatable.Rows.Add("New version", "Neue Version erstellen")
        oDatatable.Rows.Add("Question", "Nachfragen")

        ComboBox3.DataSource = oDatatable

        Try
            'Me.TBDD_DOKUMENTARTTableAdapter.Fill(Me.MyDataset.TBDD_DOKUMENTART)
            Load_Dokart()
            LOGGER.Debug("...Done Load_Dokart")
            Me.TBDD_EINGANGSARTENTableAdapter.Fill(Me.MyDataset.TBDD_EINGANGSARTEN)
            LOGGER.Debug("...Done TBDD_EINGANGSARTEN")
            Me.TBDD_MODULESTableAdapter.Fill(Me.MyDataset.TBDD_MODULES)
            LOGGER.Debug("...Done TBDD_MODULES")
        Catch ex As Exception
            LOGGER.Error(ex)
            MsgBox("Error in frmAdministration_Load: " & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
        End Try
        If IDB_ACTIVE = False Then
            WINDREAM_DIRECTCheckBox.Visible = True
        Else
            WINDREAM_DIRECTCheckBox.Visible = False
        End If
        'If GI_withWindream = True Then
        '    If ClassWindream.Init() = True Then
        '    End If
        'End If
    End Sub
    Private Sub ObjektTypenEintragen()
        Try
            ' Combobox leeren
            Me.OBJEKTTYPComboBox.Items.Clear()
            ' alle Objekttypen durchlaufen
            If SourceObjectTypes Is Nothing Then
                SourceObjectTypes = WINDREAM.ObjectTypes
            End If

            For Each oObjecttype As String In SourceObjectTypes
                ' und in die Combobox eintragen
                Me.OBJEKTTYPComboBox.Items.Add(oObjecttype)
            Next
        Catch ex As Exception
            MsgBox("Fehlernachricht:" & vbNewLine & ex.Message, MsgBoxStyle.Critical, "Unexpected error inm Eintragen der Objekttypen")
        End Try
    End Sub
    Sub Load_Dokart()
        Try
            Me.TBDD_DOKUMENTARTTableAdapter.FillByModuleGI(Me.MyDataset.TBDD_DOKUMENTART)

            If MyDataset.TBDD_DOKUMENTART.Rows.Count > 0 Then
                GridViewProfile.SelectRow(1)
                EnableControls(XtraTabPageProfiles)
            End If
            If Me.DOKART_GUIDTextBox.Text <> "" Then
                Load_TabData()
            End If

        Catch ex As Exception
            MsgBox("Error in Load_Dokart: " & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub
    Sub Load_TabData()
        If frmloaded = False Then
            Exit Sub
        End If

        RibbonPageGroupProfileRegex.Enabled = False
        RibbonPageGroupManualIndex.Enabled = False
        RibbonPageGroupManualIndexFunctions.Enabled = False
        RibbonPageGroupAutoIndex.Enabled = False

        Select Case XtraTabControl2.SelectedTabPage.Name
            Case XtraTabPageManualIndex.Name
                RibbonPageGroupManualIndex.Enabled = True

                If DOKART_GUIDTextBox.Text <> "" Then
                    Load_INDEXMAN(Me.DOKART_GUIDTextBox.Text)
                End If
            Case XtraTabPageManualIndexFunctions.Name
                RibbonPageGroupManualIndexFunctions.Enabled = True

                Load_PostProcessing(Me.DOKART_GUIDTextBox.Text)
            Case XtraTabPageAutoIndex.Name
                Load_INDEXE_AUTO(Me.DOKART_GUIDTextBox.Text)

                RibbonPageGroupAutoIndex.Enabled = True
            Case XtraTabPageFolderPath.Name
                Try
                    Dim Dt As DataTable = DATABASE_ECM.GetDatatable("select NAME from VWDDINDICES_MAN_AUTO_ACTIVE where DOCTYPE_ID = " & DOKART_GUIDTextBox.Text & " order by NAME")
                    cmbCrFolderIndex.DataSource = Dt
                    cmbCrFolderIndex.DisplayMember = Dt.Columns(0).ColumnName
                Catch ex As Exception
                    MsgBox("Error in Load Data TabData 4: " & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
                End Try
            Case XtraTabPageProfileRegex.Name
                TBGI_REGEX_DOCTYPETableAdapter.Fill(MyDataset.TBGI_REGEX_DOCTYPE, DOKART_GUIDTextBox.Text)
                RibbonPageGroupProfileRegex.Enabled = True
        End Select
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'First create a FolderBrowserDialog object
        Dim FolderBrowserDialog1 As New FolderBrowserDialog
        ' Then use the following code to create the Dialog window
        ' Change the .SelectedPath property to the default location
        With FolderBrowserDialog1
            If ZIEL_PFADTextBox.Text <> "" Then
                .SelectedPath = ZIEL_PFADTextBox.Text
            End If
            ' Desktop is the root folder in the dialog.
            '.RootFolder = Environment.SpecialFolder.Desktop
            ' Prompt the user with a custom message.
            If USER_LANGUAGE = "de-DE" Then
                .Description = "Zielordner für Dokumentart wählen:"
            Else
                .Description = "Choose destination folder for Documenttype:"
            End If

            If .ShowDialog = DialogResult.OK Then
                Dim Path As String = .SelectedPath
                ZIEL_PFADTextBox.Text = .SelectedPath
            End If
        End With
    End Sub
    Sub Save_Dokumentart()
        Try
            Try
                Me.TBDD_DOKUMENTARTBindingSource.EndEdit()
            Catch ex As Exception
                If ex.Message.StartsWith("Der interne Datatable") Then
                    Save_Dokumentart()
                Else
                    MsgBox("Error in EndEdit 1 documenttype: " & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
                End If
            End Try

            If MyDataset.TBDD_DOKUMENTART.GetChanges Is Nothing = False Then
                If Not IsInsert Then
                    Me.GEANDERTWERTextBox.Text = Environment.UserName
                End If

                Try
                    Me.TBDD_DOKUMENTARTBindingSource.EndEdit()
                Catch ex As Exception
                    If ex.Message.StartsWith("Der interne Datatable") Then
                        Save_Dokumentart()
                    Else
                        MsgBox("Error in EndEdit 2 documenttype: " & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
                    End If
                End Try

                TBDD_DOKUMENTARTTableAdapter.Update(MyDataset.TBDD_DOKUMENTART)

                If IsInsert = True Then
                    Dim oNewID As Integer
                    oNewID = DATABASE_ECM.GetScalarValue("SELECT MAX(GUID) FROM TBDD_DOKUMENTART")
                    If IsNumeric(oNewID) Then
                        Dim osql = $"EXEC PRDD_CHECK_REL_DOCTYPE_MODULE 'fileFLOW', {oNewID}"
                        If DATABASE_ECM.ExecuteNonQuery(osql) = False Then
                            MsgBox("Fehler beim zuweisen des neuen Profils zum Modul fileFLOW/GI!", MsgBoxStyle.Critical, Text)
                        End If
                    End If
                End If

                SetStatus("Profil gespeichert!")
            Else
                ClearStatus()
            End If
            IsInsert = False
        Catch ex As Exception
            MsgBox("Error in Save_Dokumentart: " & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub
    Private Sub btnNamenkonvention_Click(sender As Object, e As EventArgs) Handles btnNamenkonvention.Click
        Save_Dokumentart()
        Dim _temp As String
        _temp = Me.NAMENKONVENTIONTextBox.Text
        _Namenkonvention = Me.NAMENKONVENTIONTextBox.Text
        _aktDokart_Id = DOKART_GUIDTextBox.Text
        frmNamenkonvention.ShowDialog()
        Me.NAMENKONVENTIONTextBox.Text = _Namenkonvention
        If _temp <> NAMENKONVENTIONTextBox.Text Then
            Save_Dokumentart()
        End If
    End Sub

    Private Sub TBDD_DOKUMENTARTBindingSource_CurrentChanged(sender As Object, e As EventArgs) Handles TBDD_DOKUMENTARTBindingSource.CurrentChanged
        If DOKART_GUIDTextBox.Text <> String.Empty Then
            CURRENT_DOKART_ID = DOKART_GUIDTextBox.Text
            Load_TabData()


            gridAssignedGroups.DataSource = ClassDatatables.GetAssignedGroups(CURRENT_DOKART_ID)
            gridAvailableGroups.DataSource = ClassDatatables.GetAvailableGroups(CURRENT_DOKART_ID)
            gridAssignedUsers.DataSource = ClassDatatables.GetAssignedUsers(CURRENT_DOKART_ID)
            gridAvailableUsers.DataSource = ClassDatatables.GetAvailableUsers(CURRENT_DOKART_ID)

            If WINDREAM_DIRECTCheckBox.Checked = True Then
                load_WDIndices()
            End If

            If TextBox3.Text = String.Empty Then
                TextBox5.Enabled = False
                TextBox1.Enabled = False
            Else
                TextBox5.Enabled = True
                TextBox1.Enabled = True
            End If
        End If
    End Sub

    Private Sub TBDD_DOKUMENTARTBindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TBDD_DOKUMENTARTBindingSource.AddingNew
        Save_Dokumentart()
        Try
            Dim sql = "SELECT COUNT(T.GUID) FROM TBDD_DOKUMENTART T, TBDD_DOKUMENTART_MODULE T1 WHERE T.AKTIV = 1 AND T.GUID = T1.DOKART_ID AND T1.MODULE_ID = 1"
            Dim DoctypeCount = DATABASE_ECM.GetScalarValue(sql)
            If DoctypeCount = LICENSE_DOCTYPE_COUNT Then
                If USER_LANGUAGE = "de-DE" Then
                    MsgBox("Ihre Anzahl von konfigurierbaren Dokuemntarten ist limitiert auf: '" & LICENSE_DOCTYPE_COUNT.ToString & "'. Sie können nun keine Dokumentart hinzufügen!" & vbNewLine & "Bitte kontaktieren Sie Digital Data für weitere Details.", MsgBoxStyle.Information)
                Else
                    MsgBox("Your amount of configurable documenttypes is limited to " & LICENSE_DOCTYPE_COUNT.ToString & " doctypes. You can not add more doctypes!" & vbNewLine & "Please contact Digital Data for further licensedetails", MsgBoxStyle.Information)
                End If
                Exit Sub
            End If
        Catch ex As Exception
            MsgBox("Unexpected error in Add Doctype: " & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
        End Try

        MyDataset.TBDD_DOKUMENTART.ERSTELLTWERColumn.DefaultValue = Environment.UserName
        MyDataset.TBDD_DOKUMENTART.OBJEKTTYPColumn.DefaultValue = ""
        MyDataset.TBDD_DOKUMENTART.NAMENKONVENTIONColumn.DefaultValue = "[%vOFilename]-[%vYY_MM_DD]"
        MyDataset.TBDD_DOKUMENTART.DUPLICATE_HANDLINGColumn.DefaultValue = "New version"
        IsInsert = True
    End Sub

    Private Sub Load_ZuordnungDokart_Module(Dokart As Integer)
        Try
            Me.TBDD_DOKART_MODULETableAdapter.Fill(Me.MyDataset.TBDD_DOKART_MODULE, Dokart)
        Catch ex As System.Exception
            MsgBox("Error in Load Zuordnung_Dokart: " & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
        End Try

    End Sub
    Private Sub btnadd_Click(sender As Object, e As EventArgs)
        Dim osql = $"EXEC PRDD_CHECK_REL_DOCTYPE_MODULE 'fileFLOW',{DOKART_GUIDTextBox.Text}"
        DATABASE_ECM.ExecuteNonQuery(osql)
        LoadfileFLOW_Doctypes()
    End Sub

    Private Sub OBJEKTTYPComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles OBJEKTTYPComboBox.SelectedIndexChanged
        If WINDREAM_DIRECTCheckBox.Checked = True Then
            load_WDIndices()

            Current_ObjectType = OBJEKTTYPComboBox.Text
        End If
    End Sub
    Sub load_WDIndices()
        Try
            If OBJEKTTYPComboBox.Text <> "" Then
                Me.WD_INDEXComboBox.Items.Clear()
                Me.WD_INDEXComboBox.Items.Add("")
                Me.INDEXNAME_AutoIndexCMB.Items.Clear()

                If SourceAttributes Is Nothing Or Current_ObjectType <> OBJEKTTYPComboBox.Text Then
                    SourceAttributes = WINDREAM.GetIndiciesByObjecttype(OBJEKTTYPComboBox.Text)
                End If

                If SourceAttributes IsNot Nothing Then
                    For Each oIndex As String In SourceAttributes
                        Me.WD_INDEXComboBox.Items.Add(oIndex)
                        INDEXNAME_AutoIndexCMB.Items.Add(oIndex)
                    Next
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error in load_WDIndices: ")
        End Try
    End Sub


    Private Sub frmAdministration_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        If DOKART_GUIDTextBox.Text <> "" Then
            Load_Dokart()
            'Load_INDEXMAN(Me.DOKART_GUIDTextBox.Text)
            Load_ZuordnungDokart_Module(DOKART_GUIDTextBox.Text)
            If SUGGESTIONCheckBox.CheckState = CheckState.Checked Then
                btnSQLView.Enabled = True
            Else
                btnSQLView.Enabled = False
            End If
            Load_INDEXMAN(Me.DOKART_GUIDTextBox.Text)
        End If
        frmloaded = True
        WM_CHECKED_CHANGE()
    End Sub

    Private Sub Load_INDEXMAN(ProfileId As Integer)
        Try
            Me.TBDD_INDEX_MANTableAdapter.Fill(Me.MyDataset.TBDD_INDEX_MAN, ProfileId)
            If MyDataset.TBDD_INDEX_MAN.Rows.Count > 0 Then
                EnableControls(XtraTabPageManualIndex)
            End If
            ClearStatus()
        Catch ex As System.Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error in Load IndexeManuell: ")
        End Try
    End Sub
    Private Sub Load_INDEXE_AUTO(ProfileId As Integer)
        Try
            Me.TBDD_INDEX_AUTOMTableAdapter.Fill(Me.MyDataset.TBDD_INDEX_AUTOM, ProfileId)
            If MyDataset.TBDD_INDEX_AUTOM.Rows.Count > 0 Then
                EnableControls(XtraTabPageAutoIndex)
            End If
            CheckIndexAutoWD()
            SetAutomIndexVectorCheckboxEnabled()
        Catch ex As System.Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error in Load Indexe Auto: ")
        End Try
    End Sub
    Private Sub Load_PostProcessing(ProfileId As Integer)
        Try
            Me.TBWHDD_INDEX_MANTableAdapter.Fill(Me.MyDataset.TBWHDD_INDEX_MAN, ProfileId)
            Me.TBDD_INDEX_MAN_POSTPROCESSINGTableAdapter.Fill(Me.MyDataset.TBDD_INDEX_MAN_POSTPROCESSING, ProfileId)
            If MyDataset.TBDD_INDEX_MAN_POSTPROCESSING.Rows.Count > 0 Then
                EnableControls(XtraTabPageManualIndexFunctions)
            End If
            ClearStatus()
        Catch ex As System.Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error in Load Postprocessing-Values: ")
        End Try

    End Sub


    Sub CheckIndexAutoWD()
        If WINDREAM_DIRECTCheckBox.Checked = True Then
            INDEXNAME_AutoIndexTXT.Visible = False
            INDEXNAME_AutoIndexCMB.Visible = True
        Else
            INDEXNAME_AutoIndexTXT.Visible = True
            INDEXNAME_AutoIndexCMB.Visible = False
        End If
    End Sub
    Private Sub TBDD_INDEX_MANBindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TBDD_INDEX_MANBindingSource.AddingNew
        Try
            Save_Dokumentart()
            IsInsert = True
            MyDataset.TBDD_INDEX_MAN.ADDED_WHOColumn.DefaultValue = Environment.UserName
            MyDataset.TBDD_INDEX_MAN.DOK_IDColumn.DefaultValue = Me.DOKART_GUIDTextBox.Text
            MyDataset.TBDD_INDEX_MAN.NAMEColumn.DefaultValue = "Title of Indexstep"
            NAMETextBox.Focus()
            EnableControls(XtraTabPageManualIndex)
            If GI_withWindream = True Then
                Load_INDEXMAN(Me.DOKART_GUIDTextBox.Text)
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error in Add IndexMan: ")
        End Try

    End Sub

    Private Sub ToolStripButton7_Click(sender As Object, e As EventArgs)
        Save_IndexMan()
    End Sub
    Sub Save_IndexMan()
        Try
            If Not IsInsert Then
                CHANGED_WHOTextBox.Text = Environment.UserName
            End If

            Me.TBDD_INDEX_MANBindingSource.EndEdit()
            If MyDataset.TBDD_INDEX_MAN.GetChanges Is Nothing = False Then
                Me.TBDD_INDEX_MANBindingSource.EndEdit()
                TBDD_INDEX_MANTableAdapter.Update(MyDataset.TBDD_INDEX_MAN)
                SetStatus("Manueller Index erfolgreich gespeichert!")
            Else
                ClearStatus()
            End If
            IsInsert = False
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error in Save Man Index: ")
        End Try
    End Sub

    Sub Save_Regex()
        If Not IsInsert Then
            TextBox4.Text = Environment.UserName
        End If

        Try
            Me.TBGI_REGEX_DOCTYPEBindingSource.EndEdit()
        Catch ex As Exception
            MsgBox("Error in Save Regex1: " & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
            Exit Sub
        End Try

        If MyDataset.TBGI_REGEX_DOCTYPE.GetChanges Is Nothing = False Then
            Try
                Me.TBGI_REGEX_DOCTYPEBindingSource.EndEdit()
            Catch ex As Exception
                MsgBox("Error in Save Regex2: " & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
            End Try
            TBGI_REGEX_DOCTYPETableAdapter.Update(MyDataset.TBGI_REGEX_DOCTYPE)
            SetStatus("Profilzuordnung gespeichert!")
        Else
            ClearStatus()
        End If

        IsInsert = False
    End Sub

    Sub Save_IndexAuto()
        Try
            Me.TBDD_INDEX_AUTOMBindingSource.EndEdit()
            If MyDataset.TBDD_INDEX_AUTOM.GetChanges Is Nothing = False Then
                CHANGED_WHOTextBox3.Text = Environment.UserName
                Me.TBDD_INDEX_AUTOMBindingSource.EndEdit()
                TBDD_INDEX_AUTOMTableAdapter.Update(MyDataset.TBDD_INDEX_AUTOM)
                SetStatus("Automatischer Index erfolgreich gespeichert!")
            Else
                ClearStatus()
            End If
            IsInsert = False
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error in Save Auto Index: ")
        End Try
    End Sub

    Private Sub XtraTabControl1_SelectedPageChanged(sender As Object, e As DevExpress.XtraTab.TabPageChangedEventArgs) Handles XtraTabControl1.SelectedPageChanged
        Dim oSelectedPageName = XtraTabControl1.SelectedTabPage.Name

        If oSelectedPageName = XtraTabPageProfiles.Name Then
            RibbonControl1.SelectedPage = RibbonPageStart
        End If

        If oSelectedPageName = XtraTabPageUsersGroups.Name And DOKART_GUIDTextBox.Text <> "" Then
            gridAssignedGroups.DataSource = ClassDatatables.GetAssignedGroups(DOKART_GUIDTextBox.Text)
            gridAvailableGroups.DataSource = ClassDatatables.GetAvailableGroups(DOKART_GUIDTextBox.Text)
            gridAssignedUsers.DataSource = ClassDatatables.GetAssignedUsers(DOKART_GUIDTextBox.Text)
            gridAvailableUsers.DataSource = ClassDatatables.GetAvailableUsers(DOKART_GUIDTextBox.Text)
        End If

        Select Case XtraTabControl1.SelectedTabPageIndex
            Case 0
                If DOKART_GUIDTextBox.Text <> "" Then
                    Load_ZuordnungDokart_Module(DOKART_GUIDTextBox.Text)
                End If
        End Select
    End Sub



    Private Sub WINDREAM_DIRECTCheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles WINDREAM_DIRECTCheckBox.CheckedChanged
        WM_CHECKED_CHANGE()
    End Sub

    Sub WM_CHECKED_CHANGE()
        If WINDREAM_DIRECTCheckBox.Checked Then
            lblObjekttyp.Visible = True
            lblWDINDEX.Visible = True
            OBJEKTTYPComboBox.Visible = True
            WD_INDEXComboBox.Visible = True
            ObjektTypenEintragen()
        Else
            lblObjekttyp.Visible = False
            lblWDINDEX.Visible = False
            OBJEKTTYPComboBox.Visible = False
            WD_INDEXComboBox.Visible = False
        End If
    End Sub

    Private Function LoadIDXEmail(Otype As String) As Integer
        Try
            Return TBGI_OBJECTTYPE_EMAIL_INDEXTableAdapter.Fill(MyDataset.TBGI_OBJECTTYPE_EMAIL_INDEX, Otype)
        Catch ex As System.Exception
            MsgBox("Error in LoadIDXEmail:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return -1
        End Try
    End Function

    Private Sub btnidxemail_add_Click(sender As Object, e As EventArgs)
        Me.TBGI_OBJECTTYPE_EMAIL_INDEXBindingSource.AddNew()
    End Sub

    Private Sub XtraTabControl2_SelectedPageChanged(sender As Object, e As DevExpress.XtraTab.TabPageChangedEventArgs) Handles XtraTabControl2.SelectedPageChanged
        Load_TabData()
    End Sub
    Private Sub ToolStripButton22_Click(sender As Object, e As EventArgs)
        Save_IndexAuto()
    End Sub

    Private Sub TBDD_INDEX_AUTOMBindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TBDD_INDEX_AUTOMBindingSource.AddingNew
        Try
            Save_Dokumentart()
            MyDataset.TBDD_INDEX_AUTOM.ADDED_WHOColumn.DefaultValue = Environment.UserName
            MyDataset.TBDD_INDEX_AUTOM.DOCTYPE_IDColumn.DefaultValue = Me.DOKART_GUIDTextBox.Text
            EnableControls(XtraTabPageAutoIndex)
            IsInsert = True
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error in Add IndexeAuto: ")
        End Try

    End Sub

    Private Sub TBDD_INDEX_AUTOMBindingSource_CurrentChanged(sender As Object, e As EventArgs) Handles TBDD_INDEX_AUTOMBindingSource.CurrentChanged
        CheckIndexAutoWD()
        ClearStatus()
    End Sub

    Private Sub SQL_ACTIVECheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles SQL_ACTIVECheckBox.CheckedChanged
        If frmloaded = True Then
            If SQL_ACTIVECheckBox.CheckState = CheckState.Checked Then
                btnSQLViewAuto.Visible = True
                lblValue.Visible = False
                VALUETextBox.Visible = False
                VALUETextBox.Text = ""
                lblWindowParameter.Visible = False
                lbFileparameter.Visible = False
                btnAddWindowsVariable.Visible = False
            Else
                lblValue.Visible = True
                VALUETextBox.Visible = True
                VALUETextBox.Text = ""
                btnSQLViewAuto.Visible = False
                lblWindowParameter.Visible = True
                lbFileparameter.Visible = True
                btnAddWindowsVariable.Visible = True
            End If
            'If IsInsert = False Then
            '    Save_IndexAuto()
            'End If
        End If
    End Sub

    Private Sub btnSQLViewAuto_Click(sender As Object, e As EventArgs) Handles btnSQLViewAuto.Click
        Dim oIndexGuid As Integer = GUIDAUTO_INDEXTextbox.Text
        Dim oDocTypeGuid As Integer = DOKART_GUIDTextBox.Text

        Try
            Dim oConnection As Integer = 1
            Dim oSQLCommand = ""

            If txtAutomIndexConnectionId.Text <> String.Empty Then
                oConnection = txtAutomIndexConnectionId.Text
            End If

            If txtAutomIndexSQLResult.Text <> String.Empty Then
                oSQLCommand = txtAutomIndexSQLResult.Text
            End If

            'Dim oForm As New frmSQL_DESIGNER(oIndexGuid, oDocTypeGuid) With {
            '    .SQLCommand = oSQLCommand,
            '    .ConnectionID = oConnection
            '}

            'If oForm.ShowDialog() = DialogResult.OK Then
            '    txtAutomIndexSQLResult.Text = oForm.SQLCommand
            '    txtAutomIndexConnectionId.Text = oForm.SQLConnectionId
            'End If

            Dim oDatatable = DATABASE_ECM.GetDatatable($"SELECT * FROM TBDD_INDEX_MAN WHERE DOK_ID = {oDocTypeGuid} AND ACTIVE = 1")
            Dim oPlaceholders = oDatatable.AsEnumerable().
                ToDictionary(Of String, String)(Function(row) row.Item("NAME"), Function(row) row.Item("NAME"))

            Dim oForm2 As New DigitalData.GUIs.Common.frmSQLEditor(LOGCONFIG, DATABASE_ECM) With {
                .SQLCommand = oSQLCommand,
                .SQLConnection = oConnection,
                .PlaceholdersManual = oPlaceholders,
                .PlaceholdersManualTitle = "Manueller Index",
                .PlaceholdersManualPrefix = "CTRL"
            }

            Dim oResult = oForm2.ShowDialog()
            If oResult = DialogResult.OK Then
                txtAutomIndexSQLResult.Text = oForm2.SQLCommand
                txtAutomIndexConnectionId.Text = oForm2.SQLConnection
            End If
        Catch ex As Exception
            LOGGER.Error(ex)
        End Try
    End Sub
    Private Sub btnSQLView_Click(sender As Object, e As EventArgs) Handles btnSQLView.Click
        Dim oIndexGuid As Integer = GUIDTextBox1.Text
        Dim oDocTypeGuid As Integer = DOKART_GUIDTextBox.Text

        Try
            Dim oConnection As Integer = 1
            Dim oSQLCommand = ""

            If txtManIndexConnectionId.Text <> String.Empty Then
                oConnection = txtManIndexConnectionId.Text
            End If

            If txtManIndexSQL.Text <> String.Empty Then
                oSQLCommand = txtManIndexSQL.Text
            End If

            Dim oDatatable = DATABASE_ECM.GetDatatable($"SELECT * FROM TBDD_INDEX_MAN WHERE DOK_ID = {oDocTypeGuid} AND ACTIVE = 1")
            Dim oPlaceholders = oDatatable.AsEnumerable().
                ToDictionary(Of String, String)(Function(row) row.Item("NAME"), Function(row) row.Item("NAME"))

            Dim oForm2 As New DigitalData.GUIs.Common.frmSQLEditor(LOGCONFIG, DATABASE_ECM) With {
                .SQLCommand = oSQLCommand,
                .SQLConnection = oConnection,
                .PlaceholdersManual = oPlaceholders,
                .PlaceholdersManualPrefix = "CTRL"
            }

            Dim oResult = oForm2.ShowDialog()
            If oResult = DialogResult.OK Then
                txtManIndexSQL.Text = oForm2.SQLCommand
                txtManIndexConnectionId.Text = oForm2.SQLConnection
            End If
        Catch ex As Exception
            LOGGER.Error(ex)
        End Try
    End Sub

    Private Sub TYPEComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TYPEComboBox.SelectedIndexChanged
        If TYPEComboBox.SelectedIndex <> -1 Then
            FUNCTION1TextBox.Visible = False
            FUNCTION2TextBox.Visible = False
            Function1Label.Visible = False
            Function2Label.Visible = False

            Select Case TYPEComboBox.Text
                Case "VBREPLACE"
                    If USER_LANGUAGE = "de-DE" Then
                        Text1Label.Text = "Alter Wert:"
                        Text2Label.Text = "Neuer Wert:"
                    Else
                        Text1Label.Text = "Old Value:"
                        Text2Label.Text = "New Value:"
                    End If

                    Text1Label.Visible = True
                    Text1TextBox.Visible = True
                    Text1TextBox.Width = 150

                    Text2Label.Visible = True
                    Text2TextBox.Visible = True
                    Text2TextBox.Width = 150

                    Text3Label.Visible = False
                    Text3TextBox.Visible = False

                Case "VBSPLIT"
                    If USER_LANGUAGE = "de-DE" Then
                        Text1Label.Text = "Split-Zeichen:"
                        Text2Label.Text = "Wähle Vorkommen: (Nullbasierend)"
                    Else
                        Text1Label.Text = "Split-Character:"
                        Text2Label.Text = "Choose occurrence: (Zero-Based)"
                    End If

                    Text1Label.Visible = True
                    Text1TextBox.Visible = True
                    Text1TextBox.Width = 150

                    Text2Label.Visible = True
                    Text2TextBox.Visible = True
                    Text2TextBox.Width = 150

                    Text3Label.Visible = False
                    Text3TextBox.Visible = False

                Case "REG. EXPRESSION"
                    If USER_LANGUAGE = "de-DE" Then
                        Text1Label.Text = "Regulärer Ausdruck:"
                    Else
                        Text1Label.Text = "Regular Expression:"
                    End If

                    Text1TextBox.Visible = True
                    Text1TextBox.Width = 300

                    Text2Label.Visible = False
                    Text2TextBox.Visible = False

                    Text3Label.Visible = False
                    Text3TextBox.Visible = False
            End Select
        End If

    End Sub
    Private Sub ToolStripButton30_Click(sender As Object, e As EventArgs)
        Save_PostProcessing()
    End Sub
    Sub Save_PostProcessing()
        Try
            If Not IsInsert Then
                CHANGED_WHOTextBox4.Text = Environment.UserName
            End If

            TBDD_INDEX_MAN_POSTPROCESSINGBindingSource.EndEdit()
            If MyDataset.TBDD_INDEX_MAN_POSTPROCESSING.GetChanges Is Nothing = False Then

                TBDD_INDEX_MAN_POSTPROCESSINGBindingSource.EndEdit()
                Me.TBDD_INDEX_MAN_POSTPROCESSINGTableAdapter.Update(MyDataset.TBDD_INDEX_MAN_POSTPROCESSING)
                SetStatus("Nachbearbeitungsfunktion erfolgreich gespeichert!")
            Else
                ClearStatus()
            End If

            IsInsert = False
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error in Save Post Processing: ")
        End Try
    End Sub

    Private Sub TBDD_INDEX_MAN_POSTPROCESSINGBindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TBDD_INDEX_MAN_POSTPROCESSINGBindingSource.AddingNew
        Save_Dokumentart()
        EnableControls(XtraTabPageManualIndexFunctions)
        MyDataset.TBDD_INDEX_MAN_POSTPROCESSING.ADDED_WHENColumn.DefaultValue = DateTime.Now
        MyDataset.TBDD_INDEX_MAN_POSTPROCESSING.ADDED_WHOColumn.DefaultValue = Environment.UserName
        MyDataset.TBDD_INDEX_MAN_POSTPROCESSING.SEQUENCEColumn.DefaultValue = 1
        MyDataset.TBDD_INDEX_MAN_POSTPROCESSING.COMMENTColumn.DefaultValue = "Funktions Name"
        IsInsert = True
    End Sub
    Sub EnableControls(Control As Control, Optional Value As Boolean = True)
        For Each oSubControl As Control In Control.Controls
            If oSubControl.Enabled <> Value Then
                oSubControl.Enabled = Value
            End If
        Next
    End Sub
    Private Sub ToolStripButton31_Click(sender As Object, e As EventArgs)
        Load_PostProcessing(Me.DOKART_GUIDTextBox.Text)
    End Sub

    Sub LoadfileFLOW_Doctypes()
        Dim oFocusedRow = GridViewProfile.FocusedRowHandle
        Load_Dokart()
        GridViewProfile.FocusedRowHandle = oFocusedRow
    End Sub

    Private Sub btncrFolder_delete_Click(sender As Object, e As EventArgs) Handles btncrFolder_delete.Click
        Me.FOLDER_FOR_INDEXTextBox.Text = ""
        Save_Dokumentart()
    End Sub

    Private Sub btnaddcrFolderParameter_Click(sender As Object, e As EventArgs) Handles btnaddcrFolderParameter.Click
        Dim str As String
        If (FOLDER_FOR_INDEXTextBox.Text = "") = False Then
            str = FOLDER_FOR_INDEXTextBox.Text & "\"
        End If
        If cmbCrFolderIndex.SelectedIndex <> -1 Then
            Me.FOLDER_FOR_INDEXTextBox.Text = str & "[%" & cmbCrFolderIndex.Text & "]"
            cmbCrFolderIndex.SelectedIndex = -1
        ElseIf cmbCrFolderDate.SelectedIndex <> -1 Then
            Me.FOLDER_FOR_INDEXTextBox.Text = str & "[%V_" & cmbCrFolderDate.Text & "]"
            cmbCrFolderDate.SelectedIndex = -1
        ElseIf txtcrFoldermanuell.Text <> "" Then
            Me.FOLDER_FOR_INDEXTextBox.Text = str & txtcrFoldermanuell.Text
            txtcrFoldermanuell.Text = ""
        End If
        Me.TBDD_DOKUMENTARTTableAdapter.cmdUpdateFolderIndex(Environment.UserName, FOLDER_FOR_INDEXTextBox.Text, DOKART_GUIDTextBox.Text)

        SetStatus("Profil gespeichert")
    End Sub
    Private Sub cmbCrFolderIndex_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbCrFolderIndex.SelectedIndexChanged
        cmbCrFolderDate.SelectedIndex = -1
        txtcrFoldermanuell.Text = ""
    End Sub

    Private Sub cmbCrFolderDate_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbCrFolderDate.SelectedIndexChanged
        cmbCrFolderIndex.SelectedIndex = -1
        txtcrFoldermanuell.Text = ""
    End Sub

    Private Sub txtcrFoldermanuell_TextChanged(sender As Object, e As EventArgs) Handles txtcrFoldermanuell.TextChanged
        cmbCrFolderDate.SelectedIndex = -1
        cmbCrFolderIndex.SelectedIndex = -1
    End Sub

    Private Sub XtraTabControl2_SelectedPageChanging(sender As Object, e As DevExpress.XtraTab.TabPageChangingEventArgs) Handles XtraTabControl2.SelectedPageChanging
        Save_Dokumentart()
    End Sub

    Private Sub TBDD_USERBindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TBDD_USERBindingSource.AddingNew
        MyDataset.TBDD_USER.ADDED_WHOColumn.DefaultValue = Environment.UserName
        EnableControls(XtraTabPageUsersGroups)
    End Sub

    Private Sub btnAddWindowsVariable_Click(sender As Object, e As EventArgs) Handles btnAddWindowsVariable.Click
        If Not lbFileparameter.SelectedItem Is Nothing Then
            If GUIDAUTO_INDEXTextbox.Text <> "" Then
                Dim value As String

                Select Case lbFileparameter.SelectedIndex
                    Case 0 : value = "$filename_ext"
                    Case 1 : value = "$filename"
                    Case 2 : value = "$extension"
                    Case 3 : value = "$FileCreateDate"
                    Case 4 : value = "$FileCreatedWho"
                    Case 5 : value = "$DateDDMMYYY"
                    Case 6 : value = "$Username"
                    Case 7 : value = "$Usercode"
                End Select

                If value <> "" Then
                    VALUETextBox.Text = value
                    Save_IndexAuto()
                End If
            Else
                If USER_LANGUAGE = "de-DE" Then
                    MsgBox("Bitte fügen Sie zuerst einen neuen Index hinzu!", MsgBoxStyle.Information)
                Else
                    MsgBox("Please add a new index first!", MsgBoxStyle.Information)
                End If
            End If
        End If
    End Sub

    Private Sub ToolStripButton41_Click(sender As Object, e As EventArgs)
        Load_INDEXMAN(Me.DOKART_GUIDTextBox.Text)
    End Sub

    Private Sub ToolStripButton2_Click(sender As Object, e As EventArgs)
        If GUIDTextBox1.Text <> "" Then
            Try
                Dim del = "DELETE FROM TBDD_INDEX_MAN_POSTPROCESSING WHERE IDXMAN_ID = " & GUIDTextBox1.Text
                If DATABASE_ECM.ExecuteNonQuery(del) = True Then
                    del = "DELETE FROM TBDD_INDEX_MAN WHERE GUID = " & GUIDTextBox1.Text
                    If DATABASE_ECM.ExecuteNonQuery(del) = True Then
                        Load_INDEXMAN(Me.DOKART_GUIDTextBox.Text)
                    End If
                End If
            Catch ex As Exception
                MsgBox("Unexpected Error in deleting manual index: " & ex.Message, MsgBoxStyle.Critical)
            End Try
        End If
    End Sub

    Private Sub tsbtnRefreshAutomatic_Click(sender As Object, e As EventArgs)
        Load_INDEXE_AUTO(Me.DOKART_GUIDTextBox.Text)
    End Sub

    Private _indexIsVectorField As Boolean = False

    Private Sub WD_INDEXComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles WD_INDEXComboBox.SelectedIndexChanged
        _indexIsVectorField = indexIsVectorField(WD_INDEXComboBox.Text)
        MULTISELECTCheckBox.Enabled = _indexIsVectorField


        If Not _indexIsVectorField Then
            MULTISELECTCheckBox.Checked = False
            'VKT_PREVENT_MULTIPLE_VALUESCheckbox.Checked = False
        End If

        If MULTISELECTCheckBox.Checked Then
            VKT_PREVENT_MULTIPLE_VALUESCheckbox.Enabled = True
            VKT_ADD_ITEMCheckbox.Enabled = True
        Else
            VKT_PREVENT_MULTIPLE_VALUESCheckbox.Enabled = False
            VKT_ADD_ITEMCheckbox.Enabled = False
        End If
    End Sub

    Private Function indexIsVectorField(pIndexName As String) As Boolean
        Try
            Dim selectedIndexName As String = pIndexName

            If selectedIndexName = String.Empty Then
                Return False
            End If

            ' Vektorindexe fangen bei 4000 an
            Return WINDREAM.GetIndexType(selectedIndexName) > 4000
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub TBGI_REGEX_DOCTYPEBindingSource_AddingNew(sender As Object, e As AddingNewEventArgs) Handles TBGI_REGEX_DOCTYPEBindingSource.AddingNew
        MyDataset.TBGI_REGEX_DOCTYPE.ADDED_WHOColumn.DefaultValue = Environment.UserName
        MyDataset.TBGI_REGEX_DOCTYPE.ADDED_WHENColumn.DefaultValue = Date.Now()
        MyDataset.TBGI_REGEX_DOCTYPE.DOCTYPE_IDColumn.DefaultValue = DOKART_GUIDTextBox.Text

        TextBox5.Enabled = True
        TextBox1.Enabled = True

        IsInsert = True
    End Sub

    Private Sub BarButtonItem1_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
        frmSQL_Admin.ShowDialog()
    End Sub

    Private Sub BarButtonItem2_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick
        frmLicense.ShowDialog()
    End Sub

    Private Sub gridAssignedGroups_DragDrop(sender As Object, e As DragEventArgs) Handles gridAssignedGroups.DragDrop
        Try
            Dim data As String = e.Data.GetData(DataFormats.Text)
            Dim groupId As Integer = data.Split("|")(0)
            Dim profileId As Integer = DOKART_GUIDTextBox.Text

            ClassDatatables.AddGroupToProfile(groupId, profileId)
            gridAssignedGroups.DataSource = ClassDatatables.GetAssignedGroups(profileId)
            gridAvailableGroups.DataSource = ClassDatatables.GetAvailableGroups(profileId)
        Catch ex As Exception
            LOGGER.Error(ex)
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler beim Hinzufügen einer Gruppe:")
        End Try
    End Sub

    Private Sub gridAvailableGroups_DragDrop(sender As Object, e As DragEventArgs) Handles gridAvailableGroups.DragDrop
        Try
            Dim data As String = e.Data.GetData(DataFormats.Text)
            Dim groupId As Integer = data.Split("|")(0)
            Dim profileId As Integer = DOKART_GUIDTextBox.Text

            ClassDatatables.RemoveGroupFromProfile(groupId, profileId)
            gridAssignedGroups.DataSource = ClassDatatables.GetAssignedGroups(profileId)
            gridAvailableGroups.DataSource = ClassDatatables.GetAvailableGroups(profileId)
        Catch ex As Exception
            LOGGER.Error(ex)
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler beim Hinzufügen einer Gruppe:")
        End Try
    End Sub

    Private Sub BarButtonItem4_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem4.ItemClick
        Dim result As MsgBoxResult

        If USER_LANGUAGE = "de-DE" Then
            result = MessageBox.Show("Sind Sie sicher dass Sie dieses Profil löschen wollen?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        Else
            result = MessageBox.Show("Are you sure you want to delete this profile?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        End If

        If result = MsgBoxResult.Yes Then
            Try
                Dim del As String = " EXEC PRDD_GLOBIX_DELETE_DOCTYPE " & DOKART_GUIDTextBox.Text
                If DATABASE_ECM.ExecuteNonQuery(del) = True Then
                    Load_Dokart()
                    TBDD_DOKUMENTARTBindingSource.ResetBindings(True)

                    If USER_LANGUAGE = "de-DE" Then
                        MsgBox("Das Profil wurde erfolgreich gelöscht!", MsgBoxStyle.Information)
                    Else
                        MsgBox("Profile successfully deleted!", MsgBoxStyle.Information)
                    End If

                End If
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error in Delete DocType: ")
            End Try
        End If
    End Sub
    Private Sub BarButtonItem8_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem8.ItemClick
        Dim result As MsgBoxResult

        If USER_LANGUAGE = "de-DE" Then
            result = MessageBox.Show("Sind Sie sicher dass Sie dieses Profil kopieren wollen?", "Bestätigung erforderlich:", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        Else
            result = MessageBox.Show("Are you sure you want to copy this profile", "Confirmation needed:", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        End If

        If result = MsgBoxResult.Yes Then
            Try
                Dim oFF_ID = DATABASE_ECM.GetScalarValue("SELECT GUID FROM TBDD_MODULES WHERE SHORT_NAME = 'fileFLOW'")
                Dim copy = "exec PRDD_COPY_DOKPROFILE " & DOKART_GUIDTextBox.Text & ", " & oFF_ID
                If DATABASE_ECM.ExecuteNonQuery(copy) Then

                    If USER_LANGUAGE = "de-DE" Then
                        MsgBox("Das Profil wurde erfolgreich kopiert und auf INAKTIV gesetzt." & vbNewLine & "Bitte beachten Sie:" & vbNewLine & "- Das Profil wurde keiner Usergruppe zugeordnet.", MsgBoxStyle.Information)
                    Else
                        MsgBox("Profile was copied successfully and set to INACTIVE." & vbNewLine & "Please note:" & vbNewLine & "- Profile was not assigned to a usergroup", MsgBoxStyle.Information)
                    End If

                    Load_Dokart()
                End If
            Catch ex As Exception
                MsgBox("Error in Copy Doctype:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            End Try
        End If
    End Sub

    Private Sub BarButtonItem7_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem7.ItemClick
        LoadfileFLOW_Doctypes()
    End Sub

    Private Sub BarButtonItem5_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem5.ItemClick
        Save_Dokumentart()
    End Sub

    Private Sub BarButtonItem3_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem3.ItemClick
        TBDD_DOKUMENTARTBindingSource.AddNew()
    End Sub



    Private Sub BarButtonItem13_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs)
        TBGI_REGEX_DOCTYPEBindingSource.AddNew()
    End Sub

    Private Sub BarButtonItem15_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem15.ItemClick
        frmConnections.ShowDialog()
    End Sub

    Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
        Try
            If Regex.IsMatch(TextBox1.Text, TextBox5.Text, RegexOptions.IgnoreCase) Then
                MsgBox("The RegEx resulted in a proper match!", MsgBoxStyle.Information, "Perfect:")
            Else
                MsgBox("No Match- There might be an error in the RegEx!", MsgBoxStyle.Information, "Something wrong:")
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error in Testing Regex: ")
        End Try
    End Sub

    Private Sub ToolStripButton9_Click(sender As Object, e As EventArgs)
        Try
            Dim oSQL = $"DELETE FROM TBGI_REGEX_DOCTYPE WHERE GUID = {TextBox3.Text}"
            If DATABASE_ECM.ExecuteNonQuery(oSQL) = True Then
                Me.TBGI_REGEX_DOCTYPETableAdapter.Fill(Me.MyDataset.TBGI_REGEX_DOCTYPE, DOKART_GUIDTextBox.Text)
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error in Deleting Regex:")
        End Try
    End Sub

    Private Sub BarButtonItem12_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs)
        ObjektTypenEintragen()
    End Sub

    Private Sub BarButtonItem13_ItemClick_1(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem13.ItemClick
        Save_Regex()
    End Sub

    Private Sub BarButtonItem14_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem14.ItemClick
        Try
            Dim oSQL = $"DELETE FROM TBGI_REGEX_DOCTYPE WHERE DOCTYPE_ID = {DOKART_GUIDTextBox.Text}"
            DATABASE_ECM.ExecuteNonQuery(oSQL)
            TBGI_REGEX_DOCTYPETableAdapter.Fill(MyDataset.TBGI_REGEX_DOCTYPE, DOKART_GUIDTextBox.Text)
        Catch ex As Exception
            LOGGER.Error(ex)
            MsgBox("Fehler beim Löschen der Automatischen Profilauswahl", MsgBoxStyle.Critical, Text)
        End Try
    End Sub

    Private Sub BarButtonItem16_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem16.ItemClick
        Try
            TBDD_INDEX_MANBindingSource.AddNew()

            If WINDREAM_DIRECTCheckBox.Checked = True Then
                load_WDIndices()
            End If
        Catch ex As Exception
            LOGGER.Error(ex)
        End Try
    End Sub

    Private Sub BarButtonItem17_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem17.ItemClick
        Save_IndexMan()
    End Sub

    Private Sub BarButtonItem19_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem19.ItemClick
        Dim result As MsgBoxResult

        If USER_LANGUAGE = "de-DE" Then
            result = MessageBox.Show("Sind Sie sicher dass Sie diesen manuellen Index löschen wollen?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        Else
            result = MessageBox.Show("Are you sure you want to delete this manual index?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        End If

        If result = MsgBoxResult.Yes Then
            If GUIDTextBox1.Text <> "" Then
                Try
                    Dim del = "DELETE FROM TBDD_INDEX_MAN_POSTPROCESSING WHERE IDXMAN_ID = " & GUIDTextBox1.Text
                    If DATABASE_ECM.ExecuteNonQuery(del) = True Then
                        del = "DELETE FROM TBDD_INDEX_MAN WHERE GUID = " & GUIDTextBox1.Text
                        If DATABASE_ECM.ExecuteNonQuery(del) = True Then
                            Load_INDEXMAN(Me.DOKART_GUIDTextBox.Text)
                        End If
                    End If
                Catch ex As Exception
                    MsgBox("Unexpected Error in deleting manual index: " & ex.Message, MsgBoxStyle.Critical)
                End Try
            End If
        End If
    End Sub

    Private Sub BarButtonItem18_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem18.ItemClick
        If DOKART_GUIDTextBox.Text <> String.Empty Then
            Dim oSelectedIndex = ListBoxControl3.SelectedIndex
            Load_INDEXMAN(DOKART_GUIDTextBox.Text)
            ListBoxControl3.SelectedIndex = oSelectedIndex
        End If
    End Sub

    Private Sub BarButtonItem20_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem20.ItemClick
        Try
            TBDD_INDEX_MAN_POSTPROCESSINGBindingSource.AddNew()
        Catch ex As Exception
            LOGGER.Error(ex)
        End Try
    End Sub

    Private Sub BarButtonItem21_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem21.ItemClick
        Save_PostProcessing()
    End Sub

    Private Sub BarButtonItem23_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem23.ItemClick
        Dim result As MsgBoxResult

        If USER_LANGUAGE = "de-DE" Then
            result = MessageBox.Show("Sind Sie sicher dass Sie diese Nachbearbeitungs-Funktion löschen wollen?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        Else
            result = MessageBox.Show("Are you sure you want to delete this postprocessing function?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        End If

        If result = MsgBoxResult.Yes Then
            If GUIDTextBox1.Text <> "" Then
                Try
                    Dim del = "DELETE FROM TBDD_INDEX_MAN_POSTPROCESSING WHERE GUID = " & GUIDTextBox4.Text
                    If DATABASE_ECM.ExecuteNonQuery(del) = True Then
                        Load_PostProcessing(Me.DOKART_GUIDTextBox.Text)
                    End If
                Catch ex As Exception
                    MsgBox("Unexpected Error in deleting manual index: " & ex.Message, MsgBoxStyle.Critical)
                End Try
            End If
        End If
    End Sub

    Private Sub BarButtonItem22_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem22.ItemClick
        Dim oSelectedIndex = ListBox1.SelectedIndex
        Load_PostProcessing(Me.DOKART_GUIDTextBox.Text)
        ListBox1.SelectedIndex = oSelectedIndex
    End Sub

    Private Sub BarButtonItem24_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem24.ItemClick
        Try
            TBDD_INDEX_AUTOMBindingSource.AddNew()

            If WINDREAM_DIRECTCheckBox.Checked = True Then
                load_WDIndices()
            End If
        Catch ex As Exception
            LOGGER.Error(ex)
        End Try
    End Sub

    Private Sub ToolStripButton17_Click(sender As Object, e As EventArgs)
        MsgBox("TODO: Implement")
    End Sub

    Private Sub BarButtonItem27_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem27.ItemClick
        Dim result As MsgBoxResult

        If USER_LANGUAGE = "de-DE" Then
            result = MessageBox.Show("Sind Sie sicher dass Sie diesen automatischen Index löschen wollen?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        Else
            result = MessageBox.Show("Are you sure you want to delete this automatic index?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        End If

        If result = MsgBoxResult.Yes Then
            Try
                Dim oSQL = $"DELETE FROM TBDD_INDEX_AUTOM WHERE GUID = {GUIDAUTO_INDEXTextbox.Text}"
                DATABASE_ECM.ExecuteNonQuery(oSQL)
                Load_INDEXE_AUTO(Me.DOKART_GUIDTextBox.Text)
            Catch ex As Exception
                LOGGER.Error(ex)
                MsgBox("Fehler beim Löschen des Automatischen Indexes", MsgBoxStyle.Critical, Text)
            End Try
        End If
    End Sub

    Private Sub BarButtonItem26_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem26.ItemClick
        Dim oSelectedIndex = ListBoxControl4.SelectedIndex
        Load_INDEXE_AUTO(Me.DOKART_GUIDTextBox.Text)
        ListBoxControl4.SelectedIndex = oSelectedIndex
    End Sub

    Private Sub BarButtonItem25_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem25.ItemClick
        Save_IndexAuto()
    End Sub

    Private Sub gridAssignedUsers_DragDrop(sender As Object, e As DragEventArgs) Handles gridAssignedUsers.DragDrop
        Try
            Dim data As String = e.Data.GetData(DataFormats.Text)
            Dim userId As Integer = data.Split("|")(0)
            Dim profileId As Integer = DOKART_GUIDTextBox.Text

            ClassDatatables.AddUserToProfile(userId, profileId)
            gridAssignedUsers.DataSource = ClassDatatables.GetAssignedUsers(profileId)
            gridAvailableUsers.DataSource = ClassDatatables.GetAvailableUsers(profileId)
        Catch ex As Exception
            LOGGER.Error(ex)
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler beim Hinzufügen eines Users:")
        End Try
    End Sub

    Private Sub gridAvailableUsers_DragDrop(sender As Object, e As DragEventArgs) Handles gridAvailableUsers.DragDrop
        Try
            Dim data As String = e.Data.GetData(DataFormats.Text)
            Dim userId As Integer = data.Split("|")(0)
            Dim profileId As Integer = DOKART_GUIDTextBox.Text

            ClassDatatables.RemoveUserFromProfile(userId, profileId)
            gridAssignedUsers.DataSource = ClassDatatables.GetAssignedUsers(profileId)
            gridAvailableUsers.DataSource = ClassDatatables.GetAvailableUsers(profileId)
        Catch ex As Exception
            LOGGER.Error(ex)
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler beim Hinzufügen eines Users:")
        End Try
    End Sub

    Private Sub BarButtonItem6_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem6.ItemClick
        If DOKART_GUIDTextBox.Text <> String.Empty Then
            Dim profileId As Integer = DOKART_GUIDTextBox.Text

            gridAssignedUsers.DataSource = ClassDatatables.GetAssignedUsers(profileId)
            gridAvailableUsers.DataSource = ClassDatatables.GetAvailableUsers(profileId)
        End If
    End Sub

    Private Sub BarButtonItem28_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem28.ItemClick
        Try
            TBGI_REGEX_DOCTYPEBindingSource.AddNew()
            EnableControls(XtraTabPageProfileRegex)
        Catch ex As Exception
            LOGGER.Error(ex)
        End Try
    End Sub

    Private Sub GridView1_ValidateRow(sender As Object, e As ValidateRowEventArgs) Handles GridViewProfile.ValidateRow
        e.Valid = True
    End Sub

    Private Sub GridView1_InvalidRowException(sender As Object, e As InvalidRowExceptionEventArgs) Handles GridViewProfile.InvalidRowException
        e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction
    End Sub

    Private Sub MULTISELECTCheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles MULTISELECTCheckBox.CheckedChanged
        If MULTISELECTCheckBox.Checked Then
            VKT_PREVENT_MULTIPLE_VALUESCheckbox.Enabled = True
        Else
            VKT_PREVENT_MULTIPLE_VALUESCheckbox.Enabled = False
        End If
    End Sub

    Private Sub SUGGESTIONCheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles SUGGESTIONCheckBox.CheckedChanged
        If SUGGESTIONCheckBox.CheckState = CheckState.Checked Then
            btnSQLView.Enabled = True
        Else
            btnSQLView.Enabled = False
        End If
    End Sub

    Private Sub SUGGESTIONCheckBoxMULTISELECTCheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles SUGGESTIONCheckBox.CheckedChanged, MULTISELECTCheckBox.CheckedChanged
        If SUGGESTIONCheckBox.Checked Or MULTISELECTCheckBox.Checked Then
            VKT_ADD_ITEMCheckbox.Enabled = True
        Else
            VKT_ADD_ITEMCheckbox.Enabled = False
        End If
    End Sub

    Private Sub TBDD_INDEX_MANBindingSource_CurrentChanged(sender As Object, e As EventArgs) Handles TBDD_INDEX_MANBindingSource.CurrentChanged
        If ListBoxControl3.SelectedIndex = -1 Then
            EnableControls(Panel1, False)
        Else
            EnableControls(Panel1)
        End If

        If SUGGESTIONCheckBox.CheckState = CheckState.Checked Then
            btnSQLView.Enabled = True
        Else
            btnSQLView.Enabled = False
        End If

        If MULTISELECTCheckBox.Checked Then
            VKT_PREVENT_MULTIPLE_VALUESCheckbox.Enabled = True
        Else
            VKT_PREVENT_MULTIPLE_VALUESCheckbox.Enabled = False
        End If

        If MULTISELECTCheckBox.Checked Or SUGGESTIONCheckBox.Checked Then
            VKT_ADD_ITEMCheckbox.Enabled = True
        Else
            VKT_ADD_ITEMCheckbox.Enabled = False
        End If
    End Sub

    Private Sub BarButtonItem29_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem29.ItemClick
        frmEmailIndexing.ShowDialog()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If lbManIndexDefaultValueParameter.SelectedIndex >= 0 Then
            Dim oValue As String = String.Empty

            Select Case lbManIndexDefaultValueParameter.SelectedIndex
                Case 0 : oValue = "$filename_ext"
                Case 1 : oValue = "$filename"
                Case 2 : oValue = "$extension"
                Case 3 : oValue = "$FileCreateDate"
                Case 4 : oValue = "$FileCreatedWho"
                Case 5 : oValue = "$DateDDMMYYY"
                Case 6 : oValue = "$Username"
                Case 7 : oValue = "$Usercode"
                Case 8 : oValue = "$Null"
            End Select

            DEFAULT_VALUETextBox.Text = oValue
        End If
    End Sub

    Private Sub GridView1_BeforeLeaveRow(sender As Object, e As RowAllowEventArgs) Handles GridViewProfile.BeforeLeaveRow
        Dim oRowView As DataRowView = GridViewProfile.GetRow(e.RowHandle)
        Dim oProfileName As String = oRowView?.Row.Item("BEZEICHNUNG")

        If oRowView Is Nothing Then
            Exit Sub
        End If

        Check_HasChanges(oProfileName)
    End Sub

    Private Function Check_HasChanges(ProfileName As String) As Boolean
        TBDD_DOKUMENTARTBindingSource.EndEdit()
        If MyDataset.TBDD_DOKUMENTART.GetChanges IsNot Nothing Then
            If Ask_SaveChanges("Dokumentarten", ProfileName) Then
                Save_Dokumentart()
                Return True
            Else
                MyDataset.TBDD_DOKUMENTART.RejectChanges()
                Return False
            End If
        End If

        TBDD_INDEX_MANBindingSource.EndEdit()
        If MyDataset.TBDD_INDEX_MAN.GetChanges IsNot Nothing Then
            If Ask_SaveChanges("Manuelle Indexe", ProfileName) Then
                Save_IndexMan()
                Return True
            Else
                MyDataset.TBDD_INDEX_MAN.RejectChanges()
                Return False
            End If
        End If

        TBDD_INDEX_AUTOMBindingSource.EndEdit()
        If MyDataset.TBDD_INDEX_AUTOM.GetChanges() IsNot Nothing Then
            If Ask_SaveChanges("Automatische Indexe", ProfileName) Then
                Save_IndexAuto()
                Return True
            Else
                MyDataset.TBDD_INDEX_AUTOM.RejectChanges()
                Return False
            End If
        End If

        TBDD_INDEX_MAN_POSTPROCESSINGBindingSource.EndEdit()
        If MyDataset.TBDD_INDEX_MAN_POSTPROCESSING.GetChanges() IsNot Nothing Then
            If Ask_SaveChanges("Nachbearbeitung", ProfileName) Then
                Save_PostProcessing()
                Return True
            Else
                MyDataset.TBDD_INDEX_AUTOM.RejectChanges()
                Return False
            End If
        End If

        TBGI_REGEX_DOCTYPEBindingSource.EndEdit()
        If MyDataset.TBGI_REGEX_DOCTYPE.GetChanges() IsNot Nothing Then
            If Ask_SaveChanges("Automatische Profilauswahl", ProfileName) Then
                Save_Regex()
                Return True
            Else
                MyDataset.TBGI_REGEX_DOCTYPE.RejectChanges()
                Return False
            End If
        End If

        Return True
    End Function

    Private Function Ask_SaveChanges(Entity As String, ProfileName As String) As Boolean
        Dim oMessage = $"Sie haben im Profil '{ProfileName}' ungespeicherte Änderungen im Bereich '{Entity}'. Wollen Sie die Änderungen speichern?"
        Dim oResult = MsgBox(oMessage, MsgBoxStyle.Question Or MsgBoxStyle.YesNo, Text)

        If oResult = MsgBoxResult.Yes Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub frmAdministration_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        Check_HasChanges(BEZEICHNUNGTextBox.Text)
    End Sub

    Private Sub INDEXNAME_AutoIndexCMB_SelectedIndexChanged(sender As Object, e As EventArgs) Handles INDEXNAME_AutoIndexCMB.SelectedIndexChanged
        SetAutomIndexVectorCheckboxEnabled()
    End Sub

    Private Function SetAutomIndexVectorCheckboxEnabled()
        _indexIsVectorField = indexIsVectorField(INDEXNAME_AutoIndexCMB.Text)

        If _indexIsVectorField Then
            chkOverwrite.Enabled = True
            chkPreventMultipleValues.Enabled = True
        Else
            chkOverwrite.Enabled = False
            chkPreventMultipleValues.Enabled = False
        End If
    End Function


End Class
