1. 程式人生 > 實用技巧 >QAD Cimload 自定義通用類

QAD Cimload 自定義通用類

1.類

 
 /*------------------------------------------------------------------------
    File        : cimload
    Purpose     : A COMMON CLASS FOR CIMLOAD 
    Syntax      : 
    Description : Compatibility with PROGRESS OPENEDGE 10.1A and above
    Author(s)   : TERRENCE ZHANG
    Created     : Mon Mar 21 10:03:55 CST 2016
    Notes       : {mfdeclre.i} must exists before this class was instanced
                  Both Windows GUI/CHUI and Unix Chui can use
  ----------------------------------------------------------------------*/
  
&SCOPED-DEFINE SK + CHR(10) +
/*換行符*/
&SCOPED-DEFINE TEMPPATH "d:\temp\"
/*指定臨時檔案路徑*/

CLASS cimload: 
    /*------------------------------------------------------------------------------
     Purpose:
     Notes: 相容10.1A 沒有用到後續OOP新增的 名稱空間 屬性 過載 等新功能
    ------------------------------------------------------------------------------*/
    DEFINE PUBLIC VARIABLE temppath AS CHARACTER INIT {&TEMPPATH} NO-UNDO.  /*臨時檔案路徑*/
    DEFINE PUBLIC VARIABLE batchmode AS LOGICAL NO-UNDO.  /*預設為GUI模式匯入*/
    DEFINE PUBLIC VARIABLE testmode AS LOGICAL NO-UNDO.  /*是否為測試模式*/
    DEFINE PROTECTED VARIABLE datalog AS CHARACTER NO-UNDO.
    DEFINE PROTECTED VARIABLE errlog AS CHARACTER NO-UNDO.
    DEFINE PROTECTED VARIABLE user_lang_dir AS CHARACTER NO-UNDO.  /*對應語言程式目錄*/
    
    DEFINE PROTECTED VARIABLE SPACES AS CHARACTER NO-UNDO.  /*空值*/
    DEFINE PUBLIC VARIABLE hverr AS LOGICAL NO-UNDO.  /*是否有錯誤*/
    DEFINE PUBLIC VARIABLE errmsg AS CHARACTER NO-UNDO.  /*錯誤資訊*/

    /*擷取匯入日誌裡的值 一般為流水自動產生的ID 最多支援9個*/
    DEFINE PROTECTED VARIABLE lblname AS CHARACTER EXTENT 9 NO-UNDO.  /*要讀取的欄位名稱*/
    DEFINE PROTECTED VARIABLE strval AS CHARACTER EXTENT 9 NO-UNDO.  /*讀取到的值*/
    
    METHOD PUBLIC VOID setlblname(INPUT n AS INTEGER,INPUT elbl AS CHARACTER):
        ASSIGN lblname[n] = elbl.
    END METHOD.

    METHOD PUBLIC CHARACTER getstrval(INPUT n AS INTEGER):
        RETURN strval[n].
    END METHOD.
    /*10.1A版本尚不支援PUBLIC 變數是陣列  所以用set/get  新版本直接屬性陣列代替*/
    
    DEFINE PUBLIC VARIABLE strexnum AS INTEGER INIT 1 NO-UNDO.  /*偏移讀取*/
    DEFINE PUBLIC VARIABLE invchar AS CHARACTER NO-UNDO. /*無效的符號  讀取時消除*/

    CONSTRUCTOR PUBLIC cimload(INPUT langdir AS CHARACTER):    /*輸入 對應語言程式目錄 global_user_lang_dir*/
        ASSIGN user_lang_dir = langdir.    
    END CONSTRUCTOR.

    METHOD PUBLIC VOID cgdomain(INPUT domain AS CHARACTER):    /*內建切換域的方法*/
        DEFINE VARIABLE domstr AS CHARACTER.
        ASSIGN domstr = QUOTER(domain) {&SK} ".".
        IF NOT batchmode THEN uiload(domstr,"mgdomchg.p").
        ELSE batchload(domstr,"mgdomchg.p").
    END METHOD.
    
    /*batchrun = YES*/
    METHOD PUBLIC VOID batchload(INPUT dstring AS CHARACTER,INPUT execname AS CHARACTER):    
        ASSIGN datalog = temppath + GUID + "cim".
        ASSIGN errlog = datalog + ".err".
        OUTPUT TO VALUE(datalog).
        PUT UNFORMATTED dstring.
        OUTPUT CLOSE.
        INPUT FROM VALUE(datalog).
        OUTPUT TO VALUE(errlog) APPEND.    /*匯入產生的LOG*/
        PAUSE 0 BEFORE-HIDE.
        RUN VALUE(user_lang_dir + SUBSTRING(execname,1,2) + '/' + execname).
        INPUT CLOSE.
        OUTPUT CLOSE.
        IF SEARCH(errlog) NE ? THEN geterror(INPUT errlog).
        IF NOT testmode THEN DO:
            OS-DELETE VALUE(errlog).
            OS-DELETE VALUE(datalog).  /*刪除臨時檔案*/
        END.
    END METHOD.  /*BATCH MODE*/
    /*batchrun = NO*/
    
    METHOD PUBLIC VOID uiload(INPUT dstring AS CHARACTER,INPUT execname AS CHARACTER):    /*dstring是character 有32k大小限制  longchar則沒有*/
        ASSIGN datalog = temppath + GUID + "cim".
        ASSIGN errlog = datalog + ".err".
        OUTPUT TO VALUE(datalog).
        PUT UNFORMATTED dstring.
        OUTPUT CLOSE.
        INPUT FROM VALUE(datalog).
        OUTPUT TO VALUE(errlog) APPEND.    /*匯入產生的LOG*/
        PAUSE 0 BEFORE-HIDE.
        RUN VALUE(user_lang_dir + SUBSTRING("gpwinrun.p",1,2) + '/' + "gpwinrun.p") (execname, 'CIMLOAD-PROCESS PLEASE WAIT...').
        INPUT CLOSE.
        OUTPUT CLOSE.
        IF SEARCH(errlog) NE ? THEN geterror(INPUT errlog).
        IF NOT testmode THEN DO:
            OS-DELETE VALUE(errlog).
            OS-DELETE VALUE(datalog).  /*刪除臨時檔案*/
        END.
    END METHOD.  /*UI MODE*/

    METHOD PROTECTED FINAL CHARACTER getTermLabel(INPUT eterm AS CHARACTER):
        FIND FIRST lbl_mstr WHERE lbl_lang = user_lang_dir AND lbl_term = eterm NO-LOCK NO-ERROR.
        IF AVAILABLE lbl_mstr THEN 
        RETURN TRIM(lbl_long).
        ELSE 
        RETURN eterm.
    END METHOD.  /*根據語言取錯誤資訊的LABEL*/
    
    METHOD PROTECTED VOID geterror(INPUT errfile AS CHARACTER):   /*獲取匯入時的錯誤資訊*/
    /*匯出錯誤相關定義*/
        DEFINE VARIABLE linefield AS CHARACTER EXTENT 40 FORMAT "x(80)" NO-UNDO.
        DEFINE VARIABLE progress_errors AS INTEGER NO-UNDO.
        DEFINE VARIABLE function_warnings AS INTEGER NO-UNDO.
        DEFINE VARIABLE function_errors AS INTEGER NO-UNDO.
        DEFINE VARIABLE e AS INTEGER INIT 0 NO-UNDO.
        
        DEFINE VARIABLE lineflist AS CHARACTER NO-UNDO.
        DEFINE VARIABLE ex AS INTEGER NO-UNDO.
        
        ASSIGN progress_errors = 0
        function_warnings = 0
        function_errors = 0.
        ASSIGN hverr = NO errmsg = STRING(TODAY) + " " + STRING(TIME,"HH:MM:SS") + " " + CHR(10).
        DO e = 1 TO 9:
            strval[e] = SPACES.
        END.
        INPUT FROM VALUE(errfile) NO-ECHO.
        REPEAT:
            linefield = SPACES.
            SET linefield WITH WIDTH 255.
            /*START 擷取指定的關鍵詞資訊*/
            DO e = 1 TO 9:
                IF lblname[e] EQ SPACES OR strval[e] NE SPACES THEN NEXT.
                
                IF strexnum < 1 THEN strexnum = 1.
                lineflist = ''.
                DO ex = 1 TO strexnum:
                    lineflist = lineflist + linefield[ex].
                END.
                
                IF lineflist BEGINS lblname[e] THEN 
                DO:
                    ASSIGN strval[e] = REPLACE(lineflist,lblname[e],SPACES).   
                    IF invchar NE SPACES THEN strval[e] = REPLACE(strval[e],invchar,SPACES).
                END.
            END.
            
            /*END 取值 多語言可用*/
            IF linefield[1] = "**" OR
            linefield[1] = "ORACLE" OR
            linefield[1] = (getTermLabel("WARNING") + ":") OR
            linefield[1] = (getTermLabel("ERROR") + ":")
            THEN DO:
                DO e = 1 TO 40:
                    IF linefield[e] = SPACES THEN LEAVE.
                    ASSIGN errmsg = errmsg + " " + linefield[e].
                END.
                e = e - 1.
                ASSIGN errmsg = errmsg + CHR(10).
                
                IF linefield[1] = "**" OR linefield[1] = "ORACLE" THEN DO:
                    IF (linefield[2] = "tr_hist" AND linefield[e] = "(132)") OR
                    (linefield[2] = "op_hist" AND linefield[e] = "(132)") THEN
                    function_warnings = function_warnings + 1.
                    ELSE 
                    ASSIGN
                    progress_errors = progress_errors + 1.
                END.
                ELSE
                IF linefield[1] = (getTermLabel("WARNING") + ":")
                THEN
                function_warnings = function_warnings + 1.
                ELSE 
                IF linefield[1] = (getTermLabel("ERROR") + ":")
                THEN 
                function_errors = function_errors + 1.
            END.
            
        END.
        INPUT CLOSE.
        IF progress_errors NE 0 OR function_errors NE 0 THEN ASSIGN hverr = YES.
        ASSIGN errmsg = errmsg + getTermLabel("DATABASE_ERRORS") + ' ' + STRING(progress_errors) + ' ' 
        + getTermLabel("PROGRAM_WARNINGS") + ' ' + STRING(function_warnings) + ' ' 
        + getTermLabel("PROGRAM_ERRORS") + ' ' + STRING(function_errors).
    END METHOD.  /*讀取日誌裡的錯誤*/

END CLASS.

2.例子

 {mfdeclre.i}    /*必須*/ 
 define variable c as cimload no-undo.
 define variable s as character no-undo.
 assign s = "us100" + chr(10) + ".".    /*字串流*/
 c = new cimload(global_user_lang_dir).
 batchrun = YES.    /*必須*/ 
 c:batchload(s,"mgdomchg.p").    /*input 字串流  input 呼叫程式*/
 batchrun = NO.
 message c:errmsg view-as alert-box.    /*顯示匯入後提示資訊*/
 delete object c.
 c = ?.