Public Class frmMass_Change

    Private Sub frmMass_Change_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            SUCCESSFULL = False
            Dim msg = "Anzahl zu ändernde Datensätze: "
            If USER_LANGUAGE <> "de-DE" Then
                msg = "Count of records to be changed:"
            End If
            lblCount.Text = msg & MASS_RECORD_IDs2CHANGE.Count
            cmbColumn.Items.Clear()
            For Each Str As String In MASS_COLUMN_LIST
                If Str.ToLower = "addedwho" Or Str.ToLower = "changedwho" Or Str.ToLower = "addedwhen" Or Str.ToLower = "changedwhen" Then
                    Continue For
                Else
                    cmbColumn.Items.Add(Str)
                End If
            Next
        Catch ex As Exception
            MsgBox("Unexpected Error in load form:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
       
    End Sub
    Private Sub btncancel_Click(sender As Object, e As EventArgs) Handles btncancel.Click
        Me.Close()
    End Sub

    Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        Try
            If txtNewValue.Text <> "" And Me.cmbColumn.SelectedIndex <> -1 And cmbColumn.Text <> "" Then
                Dim msg = "Sind Sie sicher, dass Sie diese Massenänderung durchführen wollen?" & vbNewLine & MASS_RECORD_IDs2CHANGE.Count.ToString & " Datensätze werden geändert!"
                If USER_LANGUAGE <> "de-DE" Then
                    msg = "Are You sure You want to execute this mass change?" & vbNewLine & MASS_RECORD_IDs2CHANGE.Count.ToString & " records will be changed!"
                End If
                Cursor = Cursors.WaitCursor
                Dim result As MsgBoxResult
                result = MessageBox.Show(msg, "Confirmation:", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                If result = MsgBoxResult.Yes Then
                    Dim val = txtNewValue.Text
                    Dim SQL = String.Format("SELECT GUID FROM TBPMO_CONTROL WHERE COL_NAME = '{0}' AND FORM_ID = {1}", cmbColumn.Text, CURRENT_ENTITY_ID)
                    Dim control_Id = ClassDatabase.Execute_Scalar(SQL, True)
                    If Not IsNothing(control_Id) Then
                        If control_Id > 0 Then
                            SQL = String.Format("SELECT CONTROL_TYPE_ID FROM TBPMO_CONTROL WHERE GUID = {0}", control_Id)
                            Dim control_type_ID = ClassDatabase.Execute_Scalar(SQL, True)
                            If control_type_ID = 4 Then

                                Try
                                    val = CDate(val)
                                Catch ex As Exception
                                    MsgBox("Error in Converting value '" & val & "' to date", MsgBoxStyle.Critical)
                                    Cursor = Cursors.Default
                                    Exit Sub
                                End Try
                            End If
                            If control_type_ID = 7 Or control_type_ID = 12 Or control_type_ID = 14 Then
                                msg = "Achtung: für diese Spalte existieren mehrere Werte. Diese Werte werden gelöscht." & vbNewLine & "Wollen Sie dennoch fortfahren?"
                                If USER_LANGUAGE <> "de-DE" Then
                                    msg = "Attention: there are multiple values for this column. These values will be deleted." & vbNewLine & "Would You like to continue?"
                                End If
                                Dim result1 As MsgBoxResult
                                result1 = MessageBox.Show(msg, "Confirmation:", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                                If result1 = MsgBoxResult.No Then
                                    Exit Sub
                                Else
                                    For Each Record As String In MASS_RECORD_IDs2CHANGE
                                        Dim del = String.Format("DELETE FROM TBPMO_CONTROL_VALUE WHERE CONTROL_ID = {0} AND RECORD_ID = {1}", control_Id, CInt(Record))
                                        ClassDatabase.Execute_non_Query(del, False)
                                        ClassProxy.PRPROXY_CONTROL_DEL(CInt(Record), CURRENT_ENTITY_ID, control_Id)
                                    Next

                                End If
                            End If
                            Try
                                Dim err As Boolean = False
                                Dim RIGHT_ONLY_READ_RECORD_FILE = False
                                For Each Record As String In MASS_RECORD_IDs2CHANGE
                                    If SQL_RECORD_AND_FILE_READ_ONLY <> "" Then
                                        RIGHT_ONLY_READ_RECORD_FILE = ClassHelper.Check_Record_Read_only(CInt(Record), CURRENT_ENTITY_ID)
                                    End If
                                    If RIGHT_ONLY_READ_RECORD_FILE = False Then
                                        SQL = String.Format("SELECT GUID FROM TBPMO_CONTROL_VALUE WHERE CONTROL_ID = {0} AND RECORD_ID = {1}", control_Id, CInt(Record))
                                        Dim ex_GUID = ClassDatabase.Execute_Scalar(SQL, True)
                                        If Not IsNothing(ex_GUID) Then
                                            SQL = String.Format("UPDATE TBPMO_CONTROL_VALUE SET VALUE = '{0}',CHANGED_WHO = '{1}' WHERE GUID = {2}", val, USER_USERNAME, ex_GUID)
                                            If ClassDatabase.Execute_non_Query(SQL, False) = False Then
                                                MsgBox("Error in updating the value for Record: " & Record & vbNewLine & "Please check the logfile.", MsgBoxStyle.Exclamation)
                                            Else
                                                ClassProxy.PRPROXY_CONTROL_VALUE_UPD_INS(CURRENT_ENTITY_ID, control_Id, CInt(Record), val)
                                            End If
                                        Else
                                            SQL = String.Format("INSERT INTO TBPMO_CONTROL_VALUE (CONTROL_ID, RECORD_ID, VALUE, ADDED_WHO) VALUES ({0},{1},'{2}','{3}')", control_Id, CInt(Record), val, USER_USERNAME)
                                            If ClassDatabase.Execute_non_Query(SQL, False) = False Then
                                                MsgBox("Error in inserting the value for Record: " & Record & vbNewLine & "Please check the logfile.", MsgBoxStyle.Exclamation)
                                            Else
                                                ClassProxy.PRPROXY_CONTROL_VALUE_UPD_INS(CURRENT_ENTITY_ID, control_Id, CInt(Record), val)
                                            End If
                                        End If
                                    End If

                                    
                                Next
                                SUCCESSFULL = True
                                msg = "Alle Änderungen wurden ausgeführt!"
                                If USER_LANGUAGE <> "de-DE" Then
                                    msg = "All changes were executed!"
                                End If
                                MsgBox(msg, MsgBoxStyle.Information)
                            Catch ex As Exception
                                MsgBox("Unexpected Error in Getting record to be changed:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
                            End Try
                        Else
                            MsgBox("No defined column found! (1)", MsgBoxStyle.Information)
                        End If
                    Else
                        MsgBox("No defined column found! (2)", MsgBoxStyle.Information)
                    End If
                End If
            End If

        Catch ex As Exception
            MsgBox("Unexpected Error in Execute mass change:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
        Cursor = Cursors.Default
    End Sub
End Class