//AB/ Project Copy from file
//AB/ Object oIDE_Project is a Dialog_Project
    //AB/ Set ProjectName to "Copy from file"
    //AB/ Set ProjectFileName to "Copy_from_file.dg"
    //AB/ Set GenerateFileName to "NONAME10"
    //AB/ Set Size to 384 381

// Project Object Structure
//   Copy_from_file is a dbModalPanel
//     filTil is a Form
//     filFra is a Form
//     oBox is a Container3d
//       oFlettede is a Grid
//       oTextBox1 is a Textbox
//       oFelterTil is a List
//       oFlet is a Button
//       oFletAlle is a Button
//       oFelterFra is a List
//       oJoinMissing is a Button
//     cmbIndex is a ComboForm
//     oOK_bn is a Button
//     oAnnuller_bn is a Button

// Register all objects
Register_Object Copy_from_file
Register_Object cmbIndex
Register_Object filFra
Register_Object filTil
Register_Object oAnnuller_bn
Register_Object oBox
Register_Object oFelterFra
Register_Object oFelterTil
Register_Object oFlet
Register_Object oFletAlle
Register_Object oFlettede
Register_Object oJoinMissing
Register_Object oOK_bn
Register_Object oTextBox1


//AB-StoreTopStart
Use Import_textfile.dg

//AB-StoreTopEnd

//AB-IgnoreStart
Use dfdbmdpn.pkg
Use Windows.pkg
Use dfCmbFrm.pkg
//AB-IgnoreEnd



Object Copy_from_file is a dbModalPanel
    Set Label to "Copy from file"
    Set Location to 6 17
    Set Size to 326 328

    //AB-DDOStart


    //AB-DDOEnd

    Object filTil is a Form
        Set Label to "Filename To:"
        Set Size to 13 252
        Set Location to 5 65
        Set peAnchors to anTopLeftRight

        //AB-StoreStart
                                                                                                                                                                                
        Set enabled_state to False
        
        
        //AB-StoreEnd

    End_Object    // filTil

    Object filFra is a Form
        Set Label to "Filename From:"
        Set Size to 13 252
        Set Location to 20 65
        Set peAnchors to anTopLeftRight
        Set Prompt_Button_Mode to pb_PromptOn

        //AB-StoreStart
        Procedure Prompt
          Local String sInitDir sValgtFil
          Move (vSelect_File("Textfiles (*.txt;*.csv;*.asc)|*.txt;*.csv;*.asc|All files (*.*)|*.*", "Choose file", "")) to sValgtFil
          If (Trim(sValgtFil) <> "") Begin
              Set value to sValgtFil
              Send DoFillList of oFelterTil
              Send ImportTextfile sValgtFil
              Send DoFillList of oFelterFra
          End
        End_Procedure
        //AB-StoreEnd

    End_Object    // filFra

    Object oBox is a Container3d
        Set Size to 234 319
        Set Location to 37 2
        Object oFlettede is a Grid
            Set Size to 111 247
            Set Location to 16 34
            Set Resize_Column_state to False
            Set Highlight_Row_state to True
            Set peDisabledTextColor to clWindowText
            Set pbUseDisabledColor to True

            Set Line_Width to 2 0

            Set Form_Width    Item 0 to 120
            Set Header_Label  Item 0 to "To"
            
            Set Form_Width    Item 1 to 120
            Set Header_Label  Item 1 to "From"
            

            //AB-StoreStart
            On_Key Key_Shift+Key_F2 Send SletRaekke
            
            //Set Read_only_state to true
            
            // example method of how to fill a grid
            //Procedure DoFillGrid
            //    Integer i iMaxRows
            //    move 20 to iMaxRows
            //    for i from 1 to iMaxRows
            //        send Add_Item msg_None ("Col 1, row "+(string(i)) )
            //        send Add_Item msg_None ("Col 2, row "+(string(i)) )
            //        send Add_Item msg_None ("Col 3, row "+(string(i)) )
            //    loop
            //End_Procedure  // DoFillGrid
            
            //Procedure OnChange integer iNewItem
            //    string sValue
            //    get Value iNewItem to sValue // value of changed cell
            //
            //End_Procedure  // OnChange
            
            Procedure Add_Item Integer iMsg String sTekst
                Integer iItem
                Get item_count to iItem
                Forward Send Add_item iMsg sTekst
                Set shadow_state Item iItem to True
            End_Procedure
            
            Procedure SletRaekke
                Integer iItem
                Integer iRow
                String sTil sFra
                Get item_count to iItem
                If (iItem = 0) Procedure_Return
                Get current_item to iItem
                Move (iItem/2) to iRow
                Move (iRow*2) to iItem
                Get value Item iItem to sTil
                Send Delete_Item iItem
                Get value Item iItem to sFra
                Send Delete_Item iItem
                Send Add_Item of oFelterTil msg_None sTil
                Send Add_Item of oFelterFra msg_None sFra
            End_Procedure
            
            //AB-StoreEnd

        End_Object    // oFlettede

        Object oTextBox1 is a Textbox
            Set Label to "Joined fields"
            Set Auto_Size_State to False
            Set Location to 4 34
            Set Size to 10 247
            Set Justification_Mode to jMode_Center
            Set TypeFace to "MS Sans Serif"
        End_Object    // oTextBox1

        Object oFelterTil is a List
            Set Label to "TO:"
            Set Size to 62 120
            Set Location to 143 6
            Set Label_Col_Offset to 0
            Set Label_Justification_Mode to jMode_Top

            //AB-StoreStart
            Procedure DoFillList
                String sFil
                // Fill the list by sending Add_item
                Get value of filTil to sFil
                Send Delete_Data
                Send HentFeltNavne sFil Self
            End_Procedure  // DoFillList
            
            //Send DoFillList // message must be sent to fill the list
            
            //Procedure OnChange
            //    Integer iCurrent
            //    Get Current_item to iCurrent // item number of current item
            //
            //End_Procedure  // OnChange
            
            //AB-StoreEnd

        End_Object    // oFelterTil

        Object oFlet is a Button
            Set Label to "Join selected fields"
            Set Size to 38 50
            Set Location to 143 133
            Set MultiLineState to True

            //AB-StoreStart
            Procedure OnClick
                Integer iItem iItem2
                Boolean bValgt
                String sFeltFra sFeltTil
                Get item_count of oFelterTil to iItem
                If (iItem = 0) Procedure_Return
                Get item_count of oFelterFra to iItem
                If (iItem = 0) Procedure_Return
                Get Current_item of oFelterTil to iItem
                Get Select_state of oFelterTil Item iItem to bValgt
                If (bValgt = False) Procedure_Return
                Get value of oFelterTil Item iItem to sFeltTil
                If (sFeltTil = "") Procedure_Return
                Get Current_item of oFelterFra to iItem2
                Get Select_state of oFelterFra Item iItem2 to bValgt
                If (bValgt = False) Procedure_Return
                Get value of oFelterFra Item iItem2 to sFeltFra
                If (sFeltFra = "") Procedure_Return
                Send Add_Item of oFlettede msg_None sFeltTil
                Send Add_Item of oFlettede msg_None sFeltFra
                Send Delete_Item of oFelterTil iItem
                Send Delete_Item of oFelterFra iItem2
            End_Procedure // OnClick
            //AB-StoreEnd

        End_Object    // oFlet

        Object oFletAlle is a Button
            Set Label to "Join all fields with the same name"
            Set Size to 38 50
            Set Location to 187 133
            Set MultiLineState to True

            //AB-StoreStart
            Procedure OnClick
                Integer iFra iTil iMaxFra iMaxTil
                String sTil sFra
            
                Get item_count of oFelterTil to iMaxTil
                If (iMaxTil = 0) Procedure_Return
                Get item_count of oFelterFra to iMaxFra
                If (iMaxFra = 0) Procedure_Return
                Move 0 to iTil
            
                While (iTil < iMaxTil)
                    Get value of oFelterTil Item iTil to sTil
                    Move 0 to iFra
                    While (iFra < iMaxFra)
                        Get value of oFelterFra Item iFra to sFra
                        Move (Uppercase(sFra)) to sFra
                        If (sTil = sFra) Begin
                            Set current_item of oFelterTil to iTil
                            Set current_item of oFelterFra to iFra
                            Set select_state of oFelterTil Item iTil to True
                            Set select_state of oFelterFra Item iFra to True
                            Send KeyAction of oFlet
                            Get item_count of oFelterTil to iMaxTil
                            If (iMaxTil = 0) Procedure_Return
                            Get item_count of oFelterFra to iMaxFra
                            If (iMaxFra = 0) Procedure_Return
                            Move -1 to iTil
                            Move 999 to iFra
                        End
                        Else Increment iFra
                    Loop
                    Increment iTil
                Loop
            
            
            
            End_Procedure // OnClick
            //AB-StoreEnd

        End_Object    // oFletAlle

        Object oFelterFra is a List
            Set Label to "FROM:"
            Set Size to 62 120
            Set Location to 143 188
            Set Label_Col_Offset to 0
            Set Label_Justification_Mode to jMode_Top

            //AB-StoreStart
            Procedure DoFillList
                String sFil
                Integer iNoFields iCnt iColumns iOldState
                Boolean bSkip
                String sField
                // Fill the list by sending Add_item
                Get value of filFra to sFil
                Get dynamic_update_state to iOldState
                Set dynamic_update_state to False
                Send Delete_Data of oFlettede
                Send Delete_Data
                Get NumberOfImportFields of aFields to iColumns
                Get NumberOfFields of aFields to iNoFields
                Move 0 to iColumns
                For iCnt From 1 to iNoFields
                    Get FieldSkip of aFields iCnt to bSkip
                    If (bSkip = False) Begin
                        Get FieldName of aFields iCnt to sField
                        Send Add_Item msg_none sField
                    End
                Loop
                Set dynamic_update_state to iOldState
            End_Procedure  // DoFillList
            
            
            //AB-StoreEnd

        End_Object    // oFelterFra

        Object oJoinMissing is a Button
            Set Label to "Make missing fields and join them"
            Set Size to 20 120
            Set Location to 208 6
            Set MultiLineState to True

            //AB-StoreStart
            Procedure OnClick
                Integer iFra iTil iMaxFra iMaxTil iAktField
                String sTil sFra
                String sValgtFil
                Boolean bFundet
                Integer iFile iFile2 hFile iNewField iType
                Number nLength nRight
            
                Send KeyAction of oFletAlle
            
                Get item_count of oFelterFra to iMaxFra
                If (iMaxFra = 0) Procedure_Return
            
                Get piFileNumber to iFile2
            
                Get NextFreeFilenumber iFile2 to iFile
                If (iFile = 0) Procedure_Return
            
                Send DoCloseAllFiles
                Get psFilename to sValgtFil
            
                Open sValgtFil as iFile Mode DF_EXCLUSIVE
                If (Found) Begin
                    Send activate to resinfo
                    Move iFile to hFile
                    Structure_Start hFile
                        While (iMaxFra > 0)
                            Get value of oFelterFra Item 0 to sFra
                            Move (Uppercase(sFra)) to sFra
                            Move (Left(sFra, 15)) to sFra
                            Move 0 to iNewField
                            Create_Field hFile At iNewField
                            Get FieldAtNumber of aFields sFra to iAktField
                            Get FieldType of aFields iAktField to iType
                            If (iType = C_TEXTFIELDTYPE_ASCII) Set_Attribute DF_FIELD_TYPE of hFile iNewField to DF_ASCII
                            If (iType = C_TEXTFIELDTYPE_NUMERIC) Set_Attribute DF_FIELD_TYPE of hFile iNewField to DF_BCD 
                            If (iType = C_TEXTFIELDTYPE_DATE) Set_Attribute DF_FIELD_TYPE of hFile iNewField to DF_DATE
                            Get FieldLength of aFields iAktField to nLength
                            Set_Attribute DF_FIELD_LENGTH of hFile iNewField to (Integer(nLength))
                            If (iType = C_TEXTFIELDTYPE_NUMERIC) Begin
                                Move ((nLength - Integer(nLength)) * 10.0) to nRight
                                Set_Attribute DF_FIELD_LENGTH of hFile iNewField to (Integer(nLength) + nRight)
                                Set_Attribute DF_FIELD_PRECISION of hFile iNewField to nRight
                            End
                            Set_Attribute DF_FIELD_NAME of hFile iNewField to sFra
                            Get_Attribute DF_FIELD_NAME of hFile iNewField to sTil  // has been changed to legal fieldname
                            Send Add_Item of oFelterTil msg_None sTil
                            Get item_count of oFelterTil to iTil
                            Decrement iTil
                            Set current_item of oFelterTil to iTil
                            Set current_item of oFelterFra to 0
                            Set select_state of oFelterTil Item iTil to True
                            Set select_state of oFelterFra Item 0 to True
                            Send KeyAction of oFlet
                            Get item_count of oFelterFra to iMaxFra
                        Loop
                    Structure_End hFile DF_STRUCTEND_OPT_NONE "" (Resinfo(Self))
                    Send Close_panel to resinfo
                End
                Close iFile
            
            End_Procedure // OnClick
            //AB-StoreEnd

        End_Object    // oJoinMissing


        //AB-StoreStart
                                                                                                                                                                
                                                                                                                                                                
        //AB-StoreEnd

    End_Object    // oBox

    Object cmbIndex is a ComboForm
        Set Label to "Update by Index:"
        Set Size to 14 259
        Set Location to 274 62
        Set peAnchors to anTopLeftRight
        Set Form_Border to 0
        Set Label_Col_Offset to 2
        Set Label_Justification_Mode to jMode_Right
        Set Entry_State Item 0 to False

        //AB-StoreStart
        Procedure Combo_Fill_List
            Local Integer AntalIndex aktIndex antalSegmenter aktSegment Feltnr iVal
            Local String sBesked strTemp
            Integer iFile
            Get piFileNumber to iFile
            Send Combo_delete_data
            Send Combo_Add_Item "00: (Add records only)" // ingen index - brug recnum
            Get_Attribute DF_FILE_LAST_INDEX_NUMBER of iFile to AntalIndex
            For aktIndex From 1 to antalIndex
                Get_Attribute DF_INDEX_NUMBER_SEGMENTS of iFile (String(aktIndex)) to antalSegmenter
                If antalSegmenter Begin
                    Move "" to sBesked
                    If (aktIndex < 10) Append sBesked "0"
                    Append sBesked aktIndex
                    Append sBesked ": "
                    For aktSegment From 1 to antalSegmenter
                        Get_Attribute DF_INDEX_SEGMENT_FIELD of iFile (String(aktIndex)) (String(aktSegment)) to feltnr
                        Get string_value of (aFildef(Self)) Item (feltnr * 4) to strTemp
                        Append sBesked strTemp
                        //Get_attribute DF_INDEX_SEGMENT_CASE of iFile (string(aktIndex)) (string(aktSegment)) to iVal
                        //If (iVal = DF_CASE_USED) append sBesked "(UPP)"
                        Get_Attribute DF_INDEX_SEGMENT_DIRECTION of iFile (String(aktIndex)) (String(aktSegment)) to iVal
                        If (iVal = DF_DESCENDING) Append sBesked "(DEC)"
                        Append sBesked ", "
                    Loop
                    Trim sBesked to sBesked
                    If (Right(sBesked, 1) = ",") Move (Left(sBesked, (Length(sBesked) - 1))) to sBesked
                    Send Combo_Add_Item sBesked
                End
            Loop
            Set value Item 0 to "00: (Add records only)"
        End_Procedure
        
        //Procedure OnChange
        //    Local String sVal
        //    Local Integer iIndex iOk
        //    Get pIndexChangeOk To iOk
        //    If (Not(iOk)) Procedure_Return
        //    Send activate Of oData
        //    Get value Item 0 To sVal
        //    Left sVal To iIndex 2
        //    Set ordering Of oData To iIndex
        //    Send DoPaintTheList
        //End_Procedure
        
        //AB-StoreEnd

    End_Object    // cmbIndex

    Object oOK_bn is a Button
        Set Label to "&Start"
        Set Location to 293 217

        //AB-StoreStart
                                
        Procedure OnClick
            Integer iAntalNyePoster iMax iNumFields iAktField iAkt
            Integer FilenumFra FilenumTil iRecordsBefore iRecordsAfter
            Integer iMaxFraRec iAktFraRec iOldPct iAktPct iIndex iUpdated
            Handle hoFeltFra hoFeltTil
            String sFilTil sFeltnavn sChkFelt sData sVal
            Boolean bFound bSave bOk bFirst
            Integer iFile
            
            Get value of cmbIndex Item 0 to sVal
            Left sVal to iIndex 2
        
            Get item_count of oFlettede to iMax
            If (iMax = 0) Procedure_Return
        
            Get NextFreeFilenumber to FilenumTil
            If (FilenumTil = 0) Procedure_Return
        
            Send DoShowWorking "Copying records."
        
            Get value of filTil to sFilTil
        
            Get create U_array to hoFeltFra
            If (hoFeltFra = 0) Procedure_Return
            Get Create U_Array to hoFeltTil
            If (hoFeltTil = 0) Procedure_Return
        
            Get OpenTextFile to bOk
            If (bOk = False) Procedure_Return
            Get piCurrentFilenumber to FilenumFra
        
            Get piFileNumber to iFile
            Close iFile // Prevent lock problems
        
            Open sFilTil as FilenumTil Mode DF_SHARE
        
            Get_Attribute DF_FILE_RECORDS_USED of FilenumTil to iRecordsBefore
            Move 0 to iAkt
            While (iAkt < iMax)
                Get value of oFlettede Item iAkt to sChkFelt
                Get_Attribute DF_FILE_NUMBER_FIELDS of FilenumTil to iNumFields
                For iAktField From 1 to iNumFields
                    Get_Attribute DF_FIELD_NAME of FilenumTil iAktField to sFeltnavn
                    If (sFeltNavn = sChkFelt) Begin
                        Set value of hoFeltTil Item (item_count(hoFeltTil)) to iAktField
                        Move iNumFields to iAktField
                    End
                Loop
                Move (iAkt + 2) to iAkt
            Loop
        
            Move 1 to iAkt
            While (iAkt < iMax)
                Get value of oFlettede Item iAkt to sChkFelt
                Get FieldAtNumber of aFields sChkFelt to iAktField
                If (iAktField > 0) Begin
                    Set value of hoFeltFra Item (item_count(hoFeltFra)) to iAktField
                End
                Move (iAkt + 2) to iAkt
            Loop
        
            Get item_count of hoFeltFra to iMax
            Decrement iMax
        
            Send ignore_all of error_info_object
        
            Move 0 to iAktFraRec
            Move 0 to iUpdated
            Move False to bFound
            Move -1 to iOldPct
        
            Get_channel_size FilenumFra to iMaxFraRec
        
            Get ReadTextFile to bOk
            Get pbFirstRowIsFieldNames to bFirst
            If (bFirst = True) Get ReadTextFile to bOk
        
            Increment iAktFraRec
            While (bOk = True)
                Get_Channel_Position FilenumFra to iAktFraRec
                Move ((iAktFraRec * 1000.0) / iMaxFraRec) to iAktPct
                If (iAktPct <> iOldPct) Begin
                    Move iAktPct to iOldPct
                    Set label of (tekst2(info_panel)) to (String(iAktPct / 10.0))
                End
                Lock
                    Clear FilenumTil
                    For iAkt From 0 to iMax
                        Get integer_value of hoFeltFra Item iAkt to iAktField
                        Get DataFromField iAktField to sData
                        Get integer_value of hoFeltTil Item iAkt to iAktField
                        Set_Field_Value FilenumTil iAktField to sData
                    Loop
                    If (iIndex > 0) Begin
                        // Try updating an existing record, else save new
                        Move False to bSave
                        Vfind FilenumTil iIndex eq
                        Move (Found) to bFound
                        If (not(bFound)) Clear FilenumTil
                        For iAkt From 0 to iMax
                            Get integer_value of hoFeltFra Item iAkt to iAktField
                            Get DataFromField iAktField to sData
                            Get integer_value of hoFeltTil Item iAkt to iAktField
                            Get_Field_Value FilenumTil iAktField  to sVal
                            If (sVal <> sData) Begin
                                Set_Field_Value FilenumTil iAktField to sData
                                Move True to bSave
                            End
                        Loop
                    End
                    Else Move True to bSave
                    If (bSave) Begin
                        Indicate Err False
                        Saverecord FilenumTil
                        If ((bFound) and (not(err))) Increment iUpdated
                    End
                Unlock
                Get ReadTextFile to bOk
            Loop
        
            Send trap_all of error_info_object
        
            Send Destroy of hoFeltFra
            Send Destroy of hoFeltTil
        
            Get_Attribute DF_FILE_RECORDS_USED of FilenumTil to iRecordsAfter
        
            Send CloseTextFile
            Close FilenumTil
        
            Send DoUndersoegFil of Tilpas_vw
        
            Send DoLukInfopanel
        
            Move ("Number of new records:"*String(iRecordsAfter-iRecordsBefore)) to sVal
            Append sVal "\nNumber of updated records: " (String(iUpdated))
        
            Send info_box sVal
        
        End_Procedure
        
        //AB-StoreEnd

    End_Object    // oOK_bn

    Object oAnnuller_bn is a Button
        Set Label to "&Cancel"
        Set Location to 293 271

        //AB-StoreStart
                                                                                                                                                                                                        
        Procedure OnClick
            Send Request_cancel
        End_Procedure
        //AB-StoreEnd

    End_Object    // oAnnuller_bn


    //AB-StoreStart
    On_Key Key_Alt+Key_S Send KeyAction to oOk_bn
    On_Key Key_Alt+Key_C Send KeyAction to oAnnuller_bn
    On_Key kCancel Send close_panel
    
    Procedure Popup
        String sValgtFil
        Get psFilename to sValgtFil
        If (sValgtFil = "") Procedure_Return
        Set value of filTil to sValgtFil
        Set value of filFra to ""
        Send delete_Data of oFlettede
        Send delete_Data of oFelterTil
        Send Delete_Data of oFelterFra
        Send DoFillList of oFelterTil
        Send Combo_Fill_List of cmbIndex
        Forward Send Popup
    End_Procedure
    
    Procedure HentFeltNavne String sFil Integer oID
        Integer iNumFields iAktField Filenum iFile
        String sFeltnavn
        If (oID = 0) Procedure_Return
    
        Get piFilenumber to iFile
    
        Get NextFreeFilenumber iFile to Filenum
        If (Filenum = 0) Procedure_Return
    
        Open sFil as Filenum Mode DF_SHARE
        If (Found) Begin
            Get_Attribute DF_FILE_NUMBER_FIELDS of Filenum to iNumFields
            For iAktField From 1 to iNumFields
                Get_Attribute DF_FIELD_NAME of Filenum iAktField to sFeltnavn
                Send Add_Item of oID msg_None sFeltnavn
            Loop
        End
        Close Filenum
    End_Procedure
    
    Procedure ImportTextfile String sFile
        Set psImportFile to sFile
        Send Popup of import_textfile
    End_Procedure
    
    
    
    
    
    
    
    
    
    
    
    //AB-StoreEnd

End_Object    // Copy_from_file


//AB-StoreStart

//AB-StoreEnd

//AB/ End_Object    // oIDE_Project