 
/*------------------------------------------------------------------------
   File        : queryHandler
   Purpose     : 
   Syntax      : 
   Description : 
   Author(s)   : walter.riechsteiner
   Created     : Wed Jul 10 18:09:37 CEST 2024
   Notes       : 
 ----------------------------------------------------------------------*/

USING Progress.Json.ObjectModel.JsonObject FROM PROPATH.
USING OpenEdge.Net.HTTP.IHttpRequest FROM PROPATH.
USING System.Drawing.CharacterRange FROM ASSEMBLY.

CLASS utilities.selectboxHandler: 

    DEFINE VARIABLE i1 AS INTEGER NO-UNDO.
    DEFINE VARIABLE i2 AS INTEGER NO-UNDO.

        
    /*------------------------------------------------------------------------------
     Purpose:
     Notes:
    ------------------------------------------------------------------------------*/

    METHOD PUBLIC HANDLE getSelboxCompany( INPUT cFileName AS CHARACTER ):
        
        DEFINE VARIABLE cttBufferName AS CHARACTER NO-UNDO.
        
        DEFINE VARIABLE httSelect     AS HANDLE    NO-UNDO.
        DEFINE VARIABLE httBuffer     AS HANDLE    NO-UNDO.
        
        cttBufferName = SUBSTITUTE('&1_company', cFileName).
        
        CREATE TEMP-TABLE httSelect.
        httSelect:ADD-NEW-FIELD('value', 'character').
        httSelect:ADD-NEW-FIELD('label', 'character').
        httSelect:TEMP-TABLE-PREPARE (cttBufferName).
        httBuffer = httSelect:DEFAULT-BUFFER-HANDLE.

        FOR EACH companies NO-LOCK 
            WHERE companies.active :
            httBuffer:BUFFER-CREATE ().
            ASSIGN 
                httBuffer:BUFFER-FIELD('value'):BUFFER-VALUE = companies.company
                httBuffer:BUFFER-FIELD('label'):BUFFER-VALUE = companies.company_name.
        END.

        RETURN httBuffer.

    END METHOD.

    /*------------------------------------------------------------------------------
     Purpose:
     Notes:
    ------------------------------------------------------------------------------*/

    METHOD PUBLIC HANDLE getSelboxLanguage( INPUT cFileName AS CHARACTER ):
        
        DEFINE VARIABLE cttBufferName AS CHARACTER NO-UNDO.
        
        DEFINE VARIABLE httSelect     AS HANDLE    NO-UNDO.
        DEFINE VARIABLE httBuffer     AS HANDLE    NO-UNDO.
        
        cttBufferName = SUBSTITUTE('&1_company', cFileName).
        
        CREATE TEMP-TABLE httSelect.
        httSelect:ADD-NEW-FIELD('value', 'integer'   ).
        httSelect:ADD-NEW-FIELD('label', 'character' ).
        httSelect:TEMP-TABLE-PREPARE ('cttBufferName').
        httBuffer = httSelect:DEFAULT-BUFFER-HANDLE.

        FOR EACH languages NO-LOCK: 
            httBuffer:BUFFER-CREATE ().
            ASSIGN 
                httBuffer:BUFFER-FIELD('value'):BUFFER-VALUE = languages.language_id
                httBuffer:BUFFER-FIELD('label'):BUFFER-VALUE = languages.language_name.
        END.

        RETURN httBuffer.

    END METHOD.

    /*------------------------------------------------------------------------------
     Purpose:
     Notes:
    ------------------------------------------------------------------------------*/

    METHOD PUBLIC HANDLE getSelboxTables( INPUT cFileName AS CHARACTER ):
        
        DEFINE VARIABLE cttBufferName AS CHARACTER NO-UNDO.
        
        DEFINE VARIABLE httSelect     AS HANDLE    NO-UNDO.
        DEFINE VARIABLE httBuffer     AS HANDLE    NO-UNDO.
        
        cttBufferName = SUBSTITUTE('&1_table_name', cFileName).

        CREATE TEMP-TABLE httSelect.
        httSelect:ADD-NEW-FIELD('value', 'character'   ).
        httSelect:ADD-NEW-FIELD('label', 'character' ).
        httSelect:TEMP-TABLE-PREPARE (cttBufferName).
        httBuffer = httSelect:DEFAULT-BUFFER-HANDLE.

        httBuffer:BUFFER-CREATE ().
        ASSIGN 
            httBuffer:BUFFER-FIELD('value'):BUFFER-VALUE = 'table'
            httBuffer:BUFFER-FIELD('label'):BUFFER-VALUE = 'Default-Value (table)'.
        FOR EACH Ge_MIS._File NO-LOCK
            WHERE NOT Ge_MIS._File._File-Name BEGINS '_'
            AND   NOT Ge_MIS._File._File-Name BEGINS 'SYS': 
            httBuffer:BUFFER-CREATE ().
            ASSIGN 
                httBuffer:BUFFER-FIELD('value'):BUFFER-VALUE = Ge_MIS._File._File-Name
                httBuffer:BUFFER-FIELD('label'):BUFFER-VALUE = Ge_MIS._File._File-Name.
        END.
        FOR EACH FRAMEWORK._File NO-LOCK
            WHERE NOT FRAMEWORK._File._File-Name BEGINS '_'
            AND   NOT FRAMEWORK._File._File-Name BEGINS 'SYS': 
            httBuffer:BUFFER-CREATE ().
            ASSIGN 
                httBuffer:BUFFER-FIELD('value'):BUFFER-VALUE = FRAMEWORK._File._File-Name
                httBuffer:BUFFER-FIELD('label'):BUFFER-VALUE = FRAMEWORK._File._File-Name.
        END.

        RETURN httBuffer.

    END METHOD.

    /*------------------------------------------------------------------------------
     Purpose:
     Notes:
    ------------------------------------------------------------------------------*/

    METHOD PUBLIC HANDLE getSelboxUsers( INPUT cFileName AS CHARACTER  ):
        
        DEFINE VARIABLE cttBufferName AS CHARACTER NO-UNDO.
        
        DEFINE VARIABLE httSelect     AS HANDLE    NO-UNDO.
        DEFINE VARIABLE httBuffer     AS HANDLE    NO-UNDO.
        
        cttBufferName = SUBSTITUTE('&1_user_name', cFileName).

        CREATE TEMP-TABLE httSelect.
        httSelect:ADD-NEW-FIELD('value', 'character'   ).
        httSelect:ADD-NEW-FIELD('label', 'character' ).
        httSelect:TEMP-TABLE-PREPARE (cttBufferName).
        httBuffer = httSelect:DEFAULT-BUFFER-HANDLE.
        
        FOR EACH users NO-LOCK: 
            httBuffer:BUFFER-CREATE ().
            ASSIGN 
                httBuffer:BUFFER-FIELD('value'):BUFFER-VALUE = users.user_name
                httBuffer:BUFFER-FIELD('label'):BUFFER-VALUE = users.display_name.
        END.

        RETURN httBuffer.

    END METHOD.

    /*------------------------------------------------------------------------------
     Purpose:
     Notes:
    ------------------------------------------------------------------------------*/

    METHOD PUBLIC HANDLE getSelectBoxData(
        INPUT ipoIdent      AS JsonObject,
        INPUT ipcFileName   AS CHARACTER,
        INPUT ipcValueField AS CHARACTER,
        INPUT ipcLabelField AS CHARACTER 
        ):
            
        DEFINE VARIABLE cttBufferName AS CHARACTER NO-UNDO.
        
        DEFINE VARIABLE httSelect     AS HANDLE    NO-UNDO.
        DEFINE VARIABLE httBuffer     AS HANDLE    NO-UNDO.
        DEFINE VARIABLE hBuffer       AS HANDLE    NO-UNDO.
        DEFINE VARIABLE hQuery        AS HANDLE    NO-UNDO.
        DEFINE VARIABLE hFeld         AS HANDLE    NO-UNDO.
        DEFINE VARIABLE lcompany      AS LOGICAL   NO-UNDO.
        DEFINE VARIABLE lactive       AS LOGICAL   NO-UNDO.
        DEFINE VARIABLE lwhere        AS LOGICAL   NO-UNDO.
        DEFINE VARIABLE cWhere        AS CHARACTER NO-UNDO.
        
        cttBufferName = SUBSTITUTE('&1_&2', ipcFileName, ipcValueField).
        CREATE BUFFER hBuffer FOR TABLE ipcFileName.
        MESSAGE cttBufferName.        
        IF ipcFileName <> 'companies' THEN 
        DO:
            hFeld = hBuffer:BUFFER-FIELD('company'):HANDLE NO-ERROR.
            lcompany = (IF VALID-HANDLE(hFeld) THEN TRUE ELSE FALSE).
        END.
        
        hFeld = hBuffer:BUFFER-FIELD('active'):HANDLE NO-ERROR.
        lactive  = (IF VALID-HANDLE(hFeld) THEN TRUE ELSE FALSE).
        
        CREATE TEMP-TABLE httSelect.
        httSelect:ADD-NEW-FIELD('value', 'character').
        httSelect:ADD-NEW-FIELD('label', 'character').
        httSelect:TEMP-TABLE-PREPARE (cttBufferName).
        httBuffer = httSelect:DEFAULT-BUFFER-HANDLE.
        MESSAGE 'lcompany = ' lcompany  ' lactive = ' lactive.        
        lWhere = (IF lcompany OR lactive THEN TRUE ELSE FALSE).
        cWhere = SUBSTITUTE('FOR EACH &1 NO-LOCK ', ipcFileName).
        IF lcompany THEN 
        DO:
            cWhere = cWhere + SUBSTITUTE('&1 &2.company = &3 ', (IF lWhere THEN 'WHERE' ELSE 'AND'), ipcFileName, QUOTER(ipoIdent:getCharacter('company'))).
            lwhere = FALSE.
        END.
        IF lactive  THEN 
        DO:
            cWhere = cWhere + SUBSTITUTE('&1 &2.active  = &3 ', (IF lWhere THEN 'WHERE' ELSE 'AND'), ipcFileName, TRUE                            ).
            lwhere = FALSE.
        END.
        MESSAGE cWhere.
        
        CREATE QUERY hQuery.
        hQuery:SET-BUFFERS(hBuffer).
        hQuery:QUERY-PREPARE (cWhere).
        hQuery:QUERY-OPEN().
        hQuery:GET-FIRST ().
        DO WHILE NOT hQuery:QUERY-OFF-END:
            httBuffer:BUFFER-CREATE ().
            ASSIGN 
                httBuffer:BUFFER-FIELD('value'):BUFFER-VALUE = hBuffer:BUFFER-FIELD(ipcValueField):BUFFER-VALUE
                httBuffer:BUFFER-FIELD('label'):BUFFER-VALUE = hBuffer:BUFFER-FIELD(ipcLabelField):BUFFER-VALUE.
            hQuery:GET-NEXT ().
        END.

        // httBuffer:WRITE-JSON('FILE', 'C:\TEMP\httBuffer.json', TRUE).

        RETURN httBuffer.

    END METHOD.

    /*------------------------------------------------------------------------------
     Purpose:
     Notes:
    ------------------------------------------------------------------------------*/

    METHOD PUBLIC HANDLE getUserRoleSelect(
        INPUT  ipccompany AS CHARACTER
        ):
        
        DEFINE VARIABLE httBuffer AS HANDLE NO-UNDO.

        CREATE BUFFER httBuffer.
        httBuffer:ADD-NEW-FIELD ('value', 'CHARACTER').
        httBuffer:ADD-NEW-FIELD ('label', 'CHARACTER').
        httBuffer:TEMP-TABLE-PREPARE ('users_role').
        
        httBuffer:BUFFER-CREATE().
        ASSIGN 
            httBuffer:BUFFER-FIELD('value'):buffer-value = 'admin'
            httBuffer:BUFFER-FIELD('label'):buffer-value = 'admin'.        
        httBuffer:BUFFER-CREATE().
        ASSIGN 
            httBuffer:BUFFER-FIELD('value'):buffer-value = 'user'
            httBuffer:BUFFER-FIELD('label'):buffer-value = 'Benutzer'.        
        
        RETURN httBuffer.

    END METHOD.

    /*------------------------------------------------------------------------------
     Purpose:
     Notes:
    ------------------------------------------------------------------------------*/

    DESTRUCTOR PUBLIC selectboxHandler ( ):
        MESSAGE 'Destructor selectboxHandler '.
    /*        DELETE OBJECT hQuery NO-ERROR.    */
    /*        DELETE OBJECT hBuffer NO-ERROR.   */
    END DESTRUCTOR.

END CLASS.