project BLF > class TSODImport > method ImportSODData

Description

ImportSODData: this main method of the class is the entry-point for this technical class and allows callers to upload SOD-data


Internal usage


unused


program code (program1/tsodimport.p)

/* ======================================================================================== */
/* Technical class that can be used to import SOD-data contained in comma-separated files   */
/* The content and structure of these 3 or 4 files is the same as what we have when using   */
/* the "SOD Import/Export" from the menu                                                    */
/* The reason for having this technical class is that it can be run on server-side so users */
/* do not run into problems about the memory that is not sufficient on their clients.       */
/* ======================================================================================== */
/* Running this program from a windows-shortcut can be done like this:                      */
/*    Target:  <progress-executable> -p <prog-name>  -pf <pf-file>  -param <file-names>     */
/*    StartIn: Directory where <program-name> is located and where the log-file of this     */
/*             program will be created.                                                     */
/* <prog-name>   This is a simple Progress .p-file that sets the propath equal to the one   */
/*               of the Fin-AppServer, followed by this "run program/tsodimport.p", and     */
/*               ends with a Quit-statement                                                 */
/* <pf-file>     File with extention 'pf' holding the Progress-start-up settings (eg -mmax) */
/* <file-names>  Comma-separated list of the files that will be used as input. The order of */
/*               these files must be like this: SOD-Category, SOD-Matrix, Resources, Roles  */
/*               Note that the one for the 'Roles' is optional.                             */
/*               If no file-names are specified using the -param option, this program will  */
/*               try to use default file-names to upload the SOD-data from the StartIn      */
/*               directory                                                                  */
/*               When specifying 'Help' as value or simply omitting the '-param'-option,    */
/*               the log-file will contain some additional information about the usage of   */
/*               the session-parameters for this program                                    */
/* ======================================================================================== */
/* Please note that all the information that is mentioned above to start the program can    */
/* also be coded in a batch-script (on Unix or on Windows) and that this can be run from    */
/* the command-prompt in stead of using the Windows-shortcut as mentioned above             */
/* When using a script you can set the propath in the script and so you can start the main  */
/* program "program/tsodimport.p" straight from the <progress-executable>                   */
/* ======================================================================================== */
/* Logging and optional errors are all stored in a physical-file in the temp-direcotry of   */
/* the Progress-session and is named "ImportSOD-YYYYMMDD-HHMMSS.log"                        */
/* ======================================================================================== */


/* ================================================================================================= */                                                                                                       
/* Manualy define oiReturnstatus as a normal variable as we need it here with exactly this name to   */
/* be able to exeute queries and we cannot define it with this name as a data-item and this method   */
/* should not have any parameters as it need to be possible to run it straight from a command-prompt */
/* ================================================================================================= */
define variable oiReturnStatus as integer no-undo.

/* ================= */
/* Start the logging */
/* ================= */
assign vcLogFileName = session:temp-dir + 
                       (if substring(session:temp-dir,length(session:temp-dir,'character':U),-1,'character':U) = "/":U or 
                           substring(session:temp-dir,length(session:temp-dir,'character':U),-1,'character':U) = "~\":U 
                        then "":U 
                        else if opsys = "UNIX":U
                             then "/":U
                             else "~\":U) + 
                       "ImportSOD-":U + string(year(today),"9999":U) + string(month(today),"99":U) + string(day(today),"99":U) + "-" + replace(string(time,"HH:MM:SS":U),":":U,"":U) + ".log":U no-error.
<M-61 run WriteMessagesToLog
   (input  'SOD Import started':U (icMessage), 
    input  error-status:get-message(1) (icProgressErrorMessage), 
    input  ? (ilIncludetFcMessages), 
    output viFcReturnSuper (oiReturnStatus)) in TSODImport>

/* ================ */
/* Initial settings */
/* ================ */
assign vcDelimiter = ",":U.
empty temp-table tImportSODX1Category.
empty temp-table tImportSODX2Resource.
empty temp-table tImportSODX3Matrix.
empty temp-table tImportSODRole.
empty temp-table tImportSODRoleResource.
empty temp-table tImportSODX1TransString.
empty temp-table tImportSODX2CustomTable0.
empty temp-table tImportSODX3CustomTable1.
empty temp-table tImportSODX4CustomTable2.

/* ============================================================================ */
/* Technical trick to ensure that we can access the db before we are logged in  */
/* NOTE: code copied from generated instance programs (start)                   */
/* ============================================================================ */
<M-62 run WriteMessagesToLog
   (input  'Database-access enabling started':U (icMessage), 
    input  error-status:get-message(1) (icProgressErrorMessage), 
    input  ? (ilIncludetFcMessages), 
    output viFcReturnSuper (oiReturnStatus)) in TSODImport>  
publish "ComponentPoolIsRunning":U (output vlComponentPoolRunning).
if vlComponentPoolRunning = false
then do:
    <M-58 run Main  () in ComponentPool>
    session:add-super-procedure(vhFcComponent).
end. /* if vlComponentPoolRunning = false */
<M-60 run WriteMessagesToLog
   (input  'Database-access enabling ended':U (icMessage), 
    input  error-status:get-message(1) (icProgressErrorMessage), 
    input  true (ilIncludetFcMessages), 
    output viFcReturnSuper (oiReturnStatus)) in TSODImport>  
    
/* ============================================================================================================================= */
/* Start and Open Session so we can later on run Login on it - similar code can be found in TApplication:Synchronize()           */
/* Note we set the locally defined viSessionID equal to the ID of the new inst5ance of Session that is stored in viSessionTSODID */
/* ============================================================================================================================= */
<M-21 run WriteMessagesToLog
   (input  'Start-up of Session started':U (icMessage), 
    input  ? (icProgressErrorMessage), 
    input  ? (ilIncludetFcMessages), 
    output viFcReturnSuper (oiReturnStatus)) in TSODImport>
<I-27 {tFcStartAndOpenInstance
     &CLASS                = "Session"
     &SESSIONID            = "0"}>
assign viSessionID = viSessionTSODID.     
<M-39 run WriteMessagesToLog
   (input  'Start-up of Session ended. Session-ID: ':U + string(viSessionTSODID) (icMessage), 
    input  error-status:get-message(1) (icProgressErrorMessage), 
    input  true (ilIncludetFcMessages), 
    output viFcReturnSuper (oiReturnStatus)) in TSODImport>

/* ====================================================================================================== */
/* Now we have a Session that is open, we read out the required debug-level from config-file "SOD.config" */
/* and ensure the Session uses now the correct debugging via ct-log by calling SetDebugLevel()            */
/* ====================================================================================================== */
<M-77 run WriteMessagesToLog
   (input  'Setting CT-Logging started':U (icMessage), 
    input  ? (icProgressErrorMessage), 
    input  ? (ilIncludetFcMessages), 
    output viFcReturnSuper (oiReturnStatus)) in TSODImport>
assign vcSODConfigFile = search("SOD.config":U).
if vcSODConfigFile <> ?
then do:
    input from value(vcSODConfigFile ).
    repeat on error undo, throw:
        import unformatted vcSODConfigLine no-error.
        if vcSODConfigLine begins "BLDebugLevel=":U
        then viSODDebugLevel = integer(substring(vcSODConfigLine , length("BLDebugLevel=":U, "CHARACTER":U) + 1, -1, "CHARACTER":U)) no-error.
    end. /* repeat : */
    input close.
end. /* if vcSODConfigFile <> ? */
else do:
    /* Create the config file at the same location as the server.xml */
    assign vcSODConfigFile = search("server.xml":U).
    if vcSODConfigFile <> ?
    then do:
        assign vcSODConfigFile = replace(vcSODConfigFile , "server.xml":U, "SOD.config":U).
        output to value(vcSODConfigFile ).
        put unformatted "BLDebugLevel=0":U skip.
        output close.
    end. /* if vcSODConfigFile <> ? */
end. /* Not if vcSODConfigFile <> ? */
if viSODDebugLevel > 0
then do:
    <M-74 run SetDebugLevel  (input  viSODDebugLevel (iiDebugLevel)) in Session>
end. /* if viSODDebugLevel > 0 */
<M-16 run WriteMessagesToLog
   (input  'Setting CT-Logging started. Log-level: ':U + string(viSODDebugLevel) (icMessage), 
    input  error-status:get-message(1) (icProgressErrorMessage), 
    input  true (ilIncludetFcMessages), 
    output viFcReturnSuper (oiReturnStatus)) in TSODImport>

/* ==================================================== */
/* First ensure we are logged in to the Fin-application */
/* ==================================================== */
<M-20 run WriteMessagesToLog
   (input  'Retrieval of User and Password started.':U (icMessage), 
    input  ? (icProgressErrorMessage), 
    input  ? (ilIncludetFcMessages), 
    output viFcReturnSuper (oiReturnStatus)) in TSODImport>  
assign vcSODUser     = 'mfg':U
       vcSODPassword = '':U.
/* Get the system user data  - similar code can be found in TApplication:Synchronize() */
<Q-57 assign vlFcQueryRecordsAvailable = UserPrim (NoCache)
   (input 0, (UsrId)
    input 0, (UsrLogin)) in BUser>
if vlFcQueryRecordsAvailable <> false
then do :
    <Q-4 run SystBySystAdminUser (all) (Read) (NoCache)  (output dataset tqSystBySystAdminUser) in BSystem>
    find first tqSystBySystAdminUser no-error.    
    if available tqSystBySystAdminUser
    then do :
        assign vcSODUser = tqSystBySystAdminUser.tcUsrLogin.
        <M-44 run WriteMessagesToLog
           (input  'Password decryption started for user: '  + vcSODUser (icMessage), 
            input  ? (icProgressErrorMessage), 
            input  ? (ilIncludetFcMessages), 
            output viFcReturnSuper (oiReturnStatus)) in TSODImport> 
        /* Decrypt the password using class TCrypt */
        assign vhFcComponent = ?.
        <M-49 run MainBlock  () in TCrypt>
        assign vhHandleToTCrypt = vhFcComponent.        
        <M-23 run DecryptString
           (input  tqSystBySystAdminUser.tcSystAdminUserPwd (icInputString), 
            output vcSODPassword (ocOutputString), 
            output viFcReturnSuper (oiReturnStatus)) in TCrypt> 
        <M-24 run WriteMessagesToLog
           (input  'Password decryption ended for user: '  + vcSODUser + ' - Returnstatus: ' + string(viFcReturnSuper) (icMessage), 
            input  error-status:get-message(1) (icProgressErrorMessage), 
            input  true (ilIncludetFcMessages), 
            output viFcReturnSuper (oiReturnStatus)) in TSODImport>
        run gipr_DeleteProcedure in vhHandleToTCrypt.
        delete procedure vhHandleToTCrypt.
    end. /* if available tqSystBySystAdminUser */
end. /* if vlFcQueryRecordsAvailable <> false */
<M-85 run WriteMessagesToLog
   (input  'Retrieval of User and Password ended. User: ':U + string(vcSODUser) (icMessage), 
    input  ? (icProgressErrorMessage), 
    input  ? (ilIncludetFcMessages), 
    output viFcReturnSuper (oiReturnStatus)) in TSODImport>  

/* ============================================================================================================== */
/* First ensure we are logged in to the Fin-application - similar code can be found in TApplication:Synchronize() */
/* ============================================================================================================== */
<M-51 run WriteMessagesToLog
   (input  'Application log-in started':U (icMessage), 
    input  ? (icProgressErrorMessage), 
    input  ? (ilIncludetFcMessages), 
    output viFcReturnSuper (oiReturnStatus)) in TSODImport>  
<M-68 run Login
   (input  vcSODUser (icLogin), 
    input  vcSODPassword (icPassword), 
    input  '' (icExtra), 
    output vlValidLogin (olValid), 
    output viExternalReturnStatus (oiReturnStatus)) in Session>
<M-35 run WriteMessagesToLog
   (input  'Application log-in ended. ':U + 'Succes: ' + string(vlValidLogin) + ' - Returnstatus: ' + string(viExternalReturnStatus) (icMessage), 
    input  error-status:get-message(1) (icProgressErrorMessage), 
    input  true (ilIncludetFcMessages), 
    output viFcReturnSuper (oiReturnStatus)) in TSODImport>      
if not vlValidLogin or 
   viExternalReturnStatus < 0   
then do:
    <I-36 {tFcCloseAndStopInstance
         &CLASS           = "Session"}>
    return.
end. /* if not vlValidLogin or */

/* ========================================================================== */
/* About the files holding the data to load:                                  */
/* First get the 4 files-names out of the parameters for the progress-session */
/* Use default files names when they are not provided in the -param option    */
/* ========================================================================== */
assign vcSessionParameteres = session:parameter.
if vcSessionParameteres = ?
then assign vcSessionParameteres = "":U.
assign vcSessionParameteres = trim(vcSessionParameteres).
<M-76 run WriteMessagesToLog
   (input  'Session-parameters: ':U + vcSessionParameteres (icMessage), 
    input  ? (icProgressErrorMessage), 
    input  ? (ilIncludetFcMessages), 
    output viFcReturnSuper (oiReturnStatus)) in TSODImport> 
/* If the vcSessionParameteres has value 'help' then we will extend the log-file with some information on how to use the -param option */
if vcSessionParameteres = "help":U or 
   vcSessionParameteres = "":U
then do :
    assign vcDummy    = "                               ":U /* to outline the output properly */
           vcHelpText = "Additional information on the usage of the sesison-parameters (option '-param'):":U + chr(10) +  vcDummy + 
                        "The value of this session-parameter can hold a string with 4 comma-separated entries with this functionality: ":U + chr(10) + vcDummy + 
                        "1: Path and name of the comma-separated file holding the SOD-Categories that will be loaded.":U + chr(10) +  vcDummy + 
                        "   If this entry is left empty then the program will look for a file named 'SODCategories.csv' will be used.":U + chr(10) +  vcDummy + 
                        "   If the file cannot be found then the program will raise an error.":U + chr(10) +  vcDummy + 
                        "2: Path and name of the comma-separated file holding the SOD-Matrix that will be loaded.":U + chr(10) +  vcDummy + 
                        "   If this entry is left empty then the program will look for a file named 'SODMatrix.csv'.":U + chr(10) +  vcDummy + 
                        "   If the file cannot be found then the program will raise an error.":U + chr(10) + vcDummy + 
                        "3: Path and name of the comma-separated file holding the SOD-Resources that will be loaded.":U + chr(10) +  vcDummy + 
                        "   If this entry is left empty then the program will look for a file named 'SODResources.csv'.":U + chr(10) +  vcDummy + 
                        "   If the file cannot be found then the program will raise an error.":U + chr(10) + vcDummy + 
                        "4: Path and name of the comma-separated file holding the SOD-Roles that will be loaded.":U + chr(10) +  vcDummy + 
                        "   If this entry is left empty then the program will look for a file named 'SODRoles.csv'.":U + chr(10) +  vcDummy + 
                        "   If the file cannot be found then the only effect will be that no roles will be imported.":U.
    <M-97 run WriteMessagesToLog
       (input  vcHelpText (icMessage), 
        input  ? (icProgressErrorMessage), 
        input  ? (ilIncludetFcMessages), 
        output viFcReturnSuper (oiReturnStatus)) in TSODImport> 
end. /* if vcSessionParameteres = "help":U  */
<M-19 run WriteMessagesToLog
   (input  'Locating the input-files started':U (icMessage), 
    input  ? (icProgressErrorMessage), 
    input  ? (ilIncludetFcMessages), 
    output viFcReturnSuper (oiReturnStatus)) in TSODImport> 
/* Either use the files as specified in the session-parameters, either use default file-names */
assign vcFileNameSODCategories = if vcSessionParameteres <> "":U and 
                                    trim(entry(1,vcSessionParameteres,",":U)) <> "":U
                                 then entry(1,vcSessionParameteres,",":U)
                                 else "SODCategories.csv":U
       vcFileNameSODMatrix     = if vcSessionParameteres <> "":U and 
                                    num-entries(vcSessionParameteres,",":U) > 1 and 
                                    trim(entry(2,vcSessionParameteres,",":U)) <> "":U
                                 then entry(2,vcSessionParameteres,",":U)
                                 else "SODMatrix.csv":U
       vcFileNameSODResources  = if vcSessionParameteres <> "":U and 
                                    num-entries(vcSessionParameteres,",":U) > 2 and 
                                    trim(entry(3,vcSessionParameteres,",":U)) <> "":U
                                 then entry(3,vcSessionParameteres,",":U)
                                 else "SODResources.csv":U
       vcFileNameSODRoles      = if vcSessionParameteres <> "":U and 
                                    num-entries(vcSessionParameteres,",":U) > 3 and 
                                    trim(entry(4,vcSessionParameteres,",":U)) <> "":U
                                 then entry(4,vcSessionParameteres,",":U) 
                                 else "SODRoles.csv":U.
/* The first 3 files are mandatory, the 4th one with the Roles is optional */
/* SOD Categroy */
assign file-info:file-name = vcFileNameSODCategories.
if file-info:full-pathname = ?
then do :
    <M-9 run WriteMessagesToLog
       (input  'Unable to find the file holding the data to import for the SOD-Categories - file-name: ':U + vcFileNameSODCategories (icMessage), 
        input  ? (icProgressErrorMessage), 
        input  ? (ilIncludetFcMessages), 
        output viFcReturnSuper (oiReturnStatus)) in TSODImport>
    return.
end. /* if file-info:full-pathname = ? */
assign vcFileNameSODCategories = file-info:full-pathname.
/* SOD Matrix */
assign file-info:file-name = vcFileNameSODMatrix.
if file-info:full-pathname = ?
then do :
    <M-8 run WriteMessagesToLog
       (input  'Unable to find the file holding the data to import for the SOD-Matrix - file-name: ':U + vcFileNameSODMatrix (icMessage), 
        input  ? (icProgressErrorMessage), 
        input  ? (ilIncludetFcMessages), 
        output viFcReturnSuper (oiReturnStatus)) in TSODImport>
    return.
end. /* if search = ? */
assign vcFileNameSODMatrix = file-info:full-pathname. 
/* SOD Resources */
assign file-info:file-name = vcFileNameSODResources.
if file-info:full-pathname = ?
then do :       
    <M-17 run WriteMessagesToLog
       (input  'Unable to find the file holding the data to import for the SOD-Resources - file-name: ':U + vcFileNameSODResources (icMessage), 
        input  ? (icProgressErrorMessage), 
        input  ? (ilIncludetFcMessages), 
        output viFcReturnSuper (oiReturnStatus)) in TSODImport>
    return.
end. /* if search = ? */
assign vcFileNameSODResources = file-info:full-pathname.
/* SOD Roles */
assign file-info:file-name = vcFileNameSODRoles
       vcFileNameSODRoles  = file-info:full-pathname.
/* Logging */
assign vcFiles = vcFileNameSODCategories + ", ":U + vcFileNameSODMatrix + ", ":U + vcFileNameSODResources + ", ":U + (if vcFileNameSODRoles = ? then "?":U else vcFileNameSODRoles).
<M-88 run WriteMessagesToLog
   (input  'Locating the input-files ended. ':U + 'Files to use: '+ vcFiles (icMessage), 
    input  ? (icProgressErrorMessage), 
    input  ? (ilIncludetFcMessages), 
    output viFcReturnSuper (oiReturnStatus)) in TSODImport>
    
/* ==================================================================================== */
/* Call submethod that will:                                                            */
/*   - Read the Roles and RoleResources from file into the temp-tables                  */
/*   - Call method in BRole to save the Roles and RoleResources                         */
/* No need to check the returns-status of this call as that is stored in the log-file   */
/* ==================================================================================== */
<M-94 run ImportSODDataBRole
   (input  vcFileNameSODRoles (icFileNameSODRoles), 
    input  vcFileNameSODResources (icFileNameSODResources), 
    input  vcDelimiter (icDelimiter), 
    output vlImportError (olImportError), 
    output viFcReturnSuper (oiReturnStatus)) in TSODImport>
if vlImportError = true 
then Return.

/* ==================================================================================== */
/* Call submethod that will:                                                            */
/*   - Read the Categories, Resources and the SOD-Matrixfrom file into the temp-tables  */
/*   - Call method in BSystem to save this data                                         */
/* No need to check the returns-status of this call as that is stored in the log-file   */
/* ==================================================================================== */
<M-50 run ImportSODDataBSystem
   (input  vcFileNameSODCategories (icFileNameSODCategories), 
    input  vcFileNameSODResources (icFileNameSODResources), 
    input  vcFileNameSODMatrix (icFileNameSODMatrix), 
    input  vcDelimiter (icDelimiter), 
    output vlImportError (olImportError), 
    output viFcReturnSuper (oiReturnStatus)) in TSODImport>
if vlImportError = true 
then Return.

/* =================================================================================================== */
/* This catch-block ensures progress-errors are properly captured in the log-file                      */
/* =================================================================================================== */
Catch syster as progress.lang.error:
    assign oiReturnStatus = -98.
    <M-96 run WriteMessagesToLog
       (input  'Catched system-error':U (icMessage), 
        input  syster:GetMessage (1) + ' - ':U + syster:ToString() + ' - ':U + error-status:get-message(1) (icProgressErrorMessage), 
        input  true (ilIncludetFcMessages), 
        output viFcReturnSuper (oiReturnStatus)) in TSODImport>    
End catch.

/* =================================================================================================== */
/* Finally: Close Session and end the Logging                                                          */
/* The Finally-clock we will always write tFcMessages as we might have got them when something crashed */
/* =================================================================================================== */
Finally :
    /* ===== Close and Stop  Session */
    <M-15 run WriteMessagesToLog
       (input  'Stop Session started':U (icMessage), 
        input  error-status:get-message(1) (icProgressErrorMessage), 
        input  true (ilIncludetFcMessages), 
        output viFcReturnSuper (oiReturnStatus)) in TSODImport>
    if viSessionTSODID <> 0 and 
       viSessionTSODID <> ? and 
       valid-handle(vhSessionTSODInst)
    then do :
        <I-22 {tFcCloseAndStopInstance
             &CLASS           = "Session"}>
    end. /* viSessionTSODID <> 0 and */     
    <M-82 run WriteMessagesToLog
       (input  'Stop Session ended':U (icMessage), 
        input  error-status:get-message(1) (icProgressErrorMessage), 
        input  true (ilIncludetFcMessages), 
        output viFcReturnSuper (oiReturnStatus)) in TSODImport>
    /* ===== Close Stream */     
    input stream sImpStream close. /* no error is raised even is the stream in not open */
    <M-32 run WriteMessagesToLog
       (input  'SOD Import ended':U +  ' - Returnstatus: ' + string(oiReturnStatus) (icMessage), 
        input  error-status:get-message(1) (icProgressErrorMessage), 
        input  true (ilIncludetFcMessages), 
        output viFcReturnSuper (oiReturnStatus)) in TSODImport>    
End Finally.