Public Class frmDokumentart_Konfig
    Private Shared _Instance As frmDokumentart_Konfig = Nothing
    Private insert As Boolean = False
    Public Shared akt_DokartID As Integer
    Private IsInsert As Boolean = False
    Public Shared Function Instance() As frmDokumentart_Konfig
        If _Instance Is Nothing OrElse _Instance.IsDisposed = True Then
            _Instance = New frmDokumentart_Konfig
        End If
        _Instance.BringToFront()
        Return _Instance
    End Function
    Private Sub TBDD_EINGANGSARTENBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles TBDD_EINGANGSARTENBindingNavigatorSaveItem.Click
        Save_Dokart()
    End Sub

    Private Sub frmDokumentart_Konfig_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        ClassWindowLocation.SaveFormLocationSize(Me, 99, 99, "frmDokumentartKonfig")
    End Sub

    Private Sub frmDokumentart_Konfig_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            ClassWindowLocation.LoadFormLocationSize(Me, 99, 99, "frmDokumentartKonfig")
            Me.TBDD_INDEX_AUTOMTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBDD_DOKUMENTARTTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBDD_EINGANGSARTENTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBPMO_WD_FORMVIEW_DOKTYPESTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBPMO_FORM_VIEWTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBWH_DOKART_MODULETableAdapter.Connection.ConnectionString = MyConnectionString

            Refresh_Dokart(False)
            ClassWindream.Init()
            ObjektTypenEintragen()
            If GUIDTextBox.Text <> "" Then
                Refresh_DokID_FORM_VIEW()
                Refresh_DokartModule(GUIDTextBox.Text)
                Refresh_Indexe_Auto(GUIDTextBox.Text)
                akt_DokartID = GUIDTextBox.Text
                Refresh_DokartModule(GUIDTextBox.Text)
            End If

            Me.TBPMO_FORM_VIEWTableAdapter.Fill(DD_DMSDataSet.TBPMO_FORM_VIEW)
        Catch ex As Exception
            MsgBox("Error in Load:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Sub Refresh_DokID_FORM_VIEW()
        Try
            Me.TBPMO_WD_FORMVIEW_DOKTYPESTableAdapter.Fill(DD_DMSDataSet.TBPMO_WD_FORMVIEW_DOKTYPES, GUIDTextBox.Text)
        Catch ex As Exception
            MsgBox("Error in Refresh_DokID_FORM_VIEW:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Sub Save_Dokart()
        Try
            Me.TBDD_DOKUMENTARTBindingSource.EndEdit()
            If DD_DMSDataSet.TBDD_DOKUMENTART.GetChanges Is Nothing = False Then
                Me.GEANDERTWERTextBox.Text = Environment.UserName
                Me.TBDD_DOKUMENTARTBindingSource.EndEdit()
                Me.TBDD_DOKUMENTARTTableAdapter.Update(Me.DD_DMSDataSet.TBDD_DOKUMENTART)
                If IsInsert = True Then
                    Dim DOCID = ClassDatabase.Execute_Scalar("SELECT MAX(GUID) FROM TBDD_DOKUMENTART WHERE BEZEICHNUNG = '" & BEZEICHNUNGTextBox.Text & "'", True)
                    If DOCID > 0 Then
                        Dim insert = "INSERT INTO TBDD_DOKUMENTART_MODULE (DOKART_ID, MODULE_ID) VALUES (" & DOCID & ", 0)"
                        If ClassDatabase.Execute_non_Query(insert, True) = True Then
                            Refresh_DokartModule(DOCID)
                            IsInsert = False
                        End If
                    End If
                End If
                Me.lblSaveDokart.Visible = True
            Else
                Me.lblSaveDokart.Visible = False
            End If
        Catch ex As Exception
            MsgBox("Error in Save_Dokart:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Sub Refresh_Dokart(All As Boolean)
        Try
            If All = False Then
                If LogErrorsOnly = False Then ClassLogger.Add(">> Dokumentarten für RecordOrganizer werden geladen", False)
                Me.TBDD_DOKUMENTARTTableAdapter.FillByPMO(Me.DD_DMSDataSet.TBDD_DOKUMENTART, USER_LANGUAGE)
                If LogErrorsOnly = False Then ClassLogger.Add(">> Anzahl Dokumentarten: " & DD_DMSDataSet.TBDD_DOKUMENTART.Rows.Count, False)
            Else
                If LogErrorsOnly = False Then ClassLogger.Add(">> Alle Dokumentarten werden geladen", False)
                Me.TBDD_DOKUMENTARTTableAdapter.Fill(Me.DD_DMSDataSet.TBDD_DOKUMENTART, USER_LANGUAGE)
            End If

        Catch ex As Exception
            MsgBox("Error in Refresh_Dokart:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Sub Refresh_Indexe_Auto(DokTypeid As Integer)
        Try
            Me.TBDD_INDEX_AUTOMTableAdapter.Fill(Me.DD_DMSDataSet.TBDD_INDEX_AUTOM, DokTypeid)
        Catch ex As Exception
            MsgBox("Error in Refresh_Indexe_Auto:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Sub Save_Indexe_Auto()
        Try
            Me.TBDD_INDEX_AUTOMBindingSource.EndEdit()
            If DD_DMSDataSet.TBDD_INDEX_AUTOM.GetChanges Is Nothing = False Then
                Me.CHANGED_WHOTextBox.Text = Environment.UserName
                Me.TBDD_INDEX_AUTOMBindingSource.EndEdit()
                Me.TBDD_INDEX_AUTOMTableAdapter.Update(Me.DD_DMSDataSet.TBDD_INDEX_AUTOM)
                Me.lblSaveIndex.Visible = True

            Else
                Me.lblSaveIndex.Visible = False
            End If
        Catch ex As Exception
            MsgBox("Error in Save_Indexe_Auto:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Sub Refresh_Eingangsarten()
        Try
            Me.TBDD_EINGANGSARTENTableAdapter.Fill(Me.DD_DMSDataSet.TBDD_EINGANGSARTEN)
        Catch ex As Exception
            MsgBox("Error in Refresh_Eingangsarten:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Private Sub TBDD_DOKUMENTARTBindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TBDD_DOKUMENTARTBindingSource.AddingNew
        Me.DD_DMSDataSet.TBDD_DOKUMENTART.ERSTELLTWERColumn.DefaultValue = Environment.UserName
        IsInsert = True
    End Sub

    Private Sub TBDD_EINGANGSARTENBindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TBDD_EINGANGSARTENBindingSource.AddingNew
        Me.DD_DMSDataSet.TBDD_EINGANGSARTEN.ERSTELLTWERColumn.DefaultValue = Environment.UserName
    End Sub

    Private Sub TBDD_INDEX_AUTOMBindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TBDD_INDEX_AUTOMBindingSource.AddingNew
        Me.DD_DMSDataSet.TBDD_INDEX_AUTOM.ADDED_WHOColumn.DefaultValue = Environment.UserName
        Me.DD_DMSDataSet.TBDD_INDEX_AUTOM.DOCTYPE_IDColumn.DefaultValue = GUIDTextBox.Text
        load_Indices()
    End Sub

    Private Sub btnZielpfad_Click(sender As Object, e As EventArgs) Handles btnZielpfad.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
            ' Desktop is the root folder in the dialog.
            '.RootFolder = Environment.SpecialFolder.Desktop
            ' Prompt the user with a custom message.
            .Description = "Zielordner für Dokumentart wählen:"
            If .ShowDialog = DialogResult.OK Then
                Dim Path As String = .SelectedPath
                ZIEL_PFADTextBox.Text = .SelectedPath
            End If
        End With
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        If ListBox1.SelectedIndex <> -1 Then
            Refresh_Indexe_Auto(GUIDTextBox.Text)
            akt_DokartID = GUIDTextBox.Text
            Refresh_DokID_FORM_VIEW()
            Refresh_DokartModule(GUIDTextBox.Text)
        End If
    End Sub
    Private Sub ObjektTypenEintragen()
        Try
            Dim oDokumentTyp As WINDREAMLib.WMObject
            ' Combobox leeren
            Me.OBJEKTTYPComboBox.Items.Clear()
            ' alle Objekttypen durchlaufen
            For Each oDokumentTyp In ClassWindream.GetObjecttypesAsObjects
                ' und in die Combobox eintragen
                Me.OBJEKTTYPComboBox.Items.Add(oDokumentTyp.aName)
            Next
        Catch ex As Exception
            MsgBox("Fehlernachricht:" & vbNewLine & ex.Message, MsgBoxStyle.Critical, "Fehler beim Eintragen der Objekttypen")
        End Try

    End Sub

    Private Sub OBJEKTTYPComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles OBJEKTTYPComboBox.SelectedIndexChanged
        load_Indices()
    End Sub
    Sub load_Indices()
        Try
            If Me.OBJEKTTYPComboBox.SelectedIndex <> -1 Then
                Me.INDEXNAMEComboBox.Items.Clear()
                Dim indexe = ClassWindream.GetIndicesByObjecttype(OBJEKTTYPComboBox.Text)
                If indexe IsNot Nothing Then
                    Me.INDEXNAMEComboBox.Items.Add("(ONLY for Nameconvention)")
                    For Each index As String In indexe
                        Me.INDEXNAMEComboBox.Items.Add(index)
                    Next
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Fehler bei cmbDokumentTypen.SelectedIndexChanged: ")
        End Try
    End Sub
    Private Sub ToolStripButton7_Click(sender As Object, e As EventArgs) Handles ToolStripButton7.Click
        Save_Indexe_Auto()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnAddDokart_Entity.Click
        Me.TBPMO_WD_FORMVIEW_DOKTYPESTableAdapter.cmdInsert(Me.ComboBox1.SelectedValue, Me.GUIDTextBox.Text, Me.OBJEKTTYPComboBox.Text, Environment.UserName)
        Refresh_DokID_FORM_VIEW()
    End Sub

    Private Sub XtraTabControl1_SelectedPageChanged(sender As Object, e As DevExpress.XtraTab.TabPageChangedEventArgs) Handles XtraTabControl1.SelectedPageChanged
        Select Case XtraTabControl1.SelectedTabPageIndex
            Case 1
                Refresh_DokID_FORM_VIEW()
                Me.btnAddDokart_Entity.Text = "Entität zu Dokumentart ' " & Me.KURZNAMETextBox.Text & "' hinzufügen"
        End Select
    End Sub

    Private Sub btnNamenkonvention_Click(sender As Object, e As EventArgs) Handles btnNamenkonvention.Click
        Save_Dokart()
        Dim _temp As String
        '_temp = Me.NAMENKONVENTIONTextBox.Text
        'akt_Namenkonvention = _temp
        akt_DokartID = GUIDTextBox.Text
        frmDokumentart_NameConvention.ShowDialog()
        'Me.NAMENKONVENTIONTextBox.Text = akt_Namenkonvention
        'If _temp <> NAMENKONVENTIONTextBox.Text Then
        '    Save_Dokart()
        'End If
    End Sub

    Private Sub ToolStripButton14_Click(sender As Object, e As EventArgs) Handles ToolStripButton14.Click

    End Sub

    Private Sub ToolStripButton9_Click(sender As Object, e As EventArgs) Handles ToolStripButton9.Click
        Dim i As Integer
        i = TBPMO_WD_FORMVIEW_DOKTYPESDataGridView.CurrentRow.Index
        If TBPMO_WD_FORMVIEW_DOKTYPESDataGridView.Item(0, i).Value Is Nothing = False Then
            TBPMO_WD_FORMVIEW_DOKTYPESTableAdapter.DeleteQuery(TBPMO_WD_FORMVIEW_DOKTYPESDataGridView.Item(0, i).Value)
            Refresh_DokID_FORM_VIEW()
        End If
    End Sub

    Private Sub ToolStripButton15_Click(sender As Object, e As EventArgs) Handles ToolStripButton15.Click
        Refresh_DokID_FORM_VIEW()
    End Sub

    Private Sub TBDD_INDEX_AUTOMBindingSource_CurrentChanged(sender As Object, e As EventArgs) Handles TBDD_INDEX_AUTOMBindingSource.CurrentChanged
        Me.lblSaveIndex.Visible = False
    End Sub

    Private Sub TBDD_DOKUMENTARTBindingSource_CurrentChanged(sender As Object, e As EventArgs) Handles TBDD_DOKUMENTARTBindingSource.CurrentChanged
        Me.lblSaveDokart.Visible = False
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorDeleteItem.Click
        Dim result As MsgBoxResult = MsgBox("Wollen Sie diese Dokumentart wirklich löschen?", MsgBoxStyle.YesNo, "Frage:")
        If result = MsgBoxResult.Yes Then
            Dim del As String = "DELETE FROM TBDD_INDEX_MAN WHERE DOK_ID = " & akt_DokartID
            If ClassDatabase.Execute_non_Query(del, True) = True Then
                del = "DELETE FROM TBDD_INDEX_AUTOM WHERE DOCTYPE_ID = " & akt_DokartID
                If ClassDatabase.Execute_non_Query(del, True) = True Then
                    del = "DELETE FROM TBDD_DOKUMENTART_MODULE WHERE DOKART_ID = " & akt_DokartID
                    If ClassDatabase.Execute_non_Query(del, True) = True Then
                        del = "DELETE FROM TBPMO_WD_FORMVIEW_DOKTYPES WHERE DOCTYPE_ID = " & akt_DokartID
                        If ClassDatabase.Execute_non_Query(del, True) = True Then
                            del = "DELETE FROM TBDD_DOKUMENTART WHERE GUID = " & akt_DokartID
                            If ClassDatabase.Execute_non_Query(del, True) = True Then
                                Dim pmoobj As String = "DOCTYPE_TITLE" & akt_DokartID
                                del = "DELETE FROM TBPMO_LANGUAGE_OBJECT WHERE PMO_OBJECT_NAME = '" & pmoobj & "'"
                                If ClassDatabase.Execute_non_Query(del, True) = True Then
                                    MsgBox("Die Dokumentart wurde erfolgreich gelöscht!", MsgBoxStyle.Information)
                                    Refresh_Dokart(False)
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub ToolStripButton16_Click(sender As Object, e As EventArgs) Handles ToolStripButton16.Click
        Refresh_Dokart(False)
    End Sub

    Private Sub ToolStripButton17_Click(sender As Object, e As EventArgs) Handles ToolStripButton17.Click
        Refresh_Dokart(True)
    End Sub

    Private Sub Refresh_DokartModule(dokart_id As Integer)
        Try
            Me.TBWH_DOKART_MODULETableAdapter.Fill(Me.DD_DMSDataSet.TBWH_DOKART_MODULE, dokart_id)
        Catch ex As System.Exception
            MsgBox("Error in Refresh_DokartModule:" & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
        End Try

    End Sub

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            TBWH_DOKART_MODULETableAdapter.InsertDokartModule(GUIDTextBox.Text)
            Refresh_DokartModule(GUIDTextBox.Text)
        Catch ex As Exception
            MsgBox("Error in Add Dokart/Module:" & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub

    Private Sub INDEXNAMEComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles INDEXNAMEComboBox.SelectedIndexChanged
        If INDEXNAMEComboBox.SelectedIndex <> -1 Then
            If COMMENTTextBox.Text = String.Empty Then
                If INDEXNAMEComboBox.Text = "(ONLY for Nameconvention)" Then
                    COMMENTTextBox.Text = "Please insert a description for this placeholder"
                End If

            End If
        End If
    End Sub
End Class