 
/*------------------------------------------------------------------------
   File        : adresse_dict
   Purpose     : 
   Syntax      : 
   Description : 
   Author(s)   : walter.riechsteiner
   Created     : Thu Jun 26 13:56:20 CEST 2025
   Notes       : 
 ----------------------------------------------------------------------*/
 
BLOCK-LEVEL ON ERROR UNDO, THROW.

USING OpenEdge.Core.String FROM PROPATH.
USING OpenEdge.Net.HTTP.IHttpResponse FROM PROPATH.
USING OpenEdge.Net.HTTP.StatusCodeEnum FROM PROPATH.
USING OpenEdge.Web.IWebRequest FROM PROPATH.
USING OpenEdge.Web.WebHandler FROM PROPATH.
USING OpenEdge.Web.WebResponseWriter FROM PROPATH.
USING Progress.Json.ObjectModel.JsonObject FROM PROPATH.
USING Progress.Json.ObjectModel.JsonArray FROM PROPATH.
USING src.ch.adprime.gemis.WebHandlerUtilities FROM PROPATH.
USING src.ch.adprime.gemis.ErrorMessage FROM PROPATH.

CLASS getTableFieldsImpl INHERITS WebHandler: 

    DEFINE TEMP-TABLE tFileDict
        SERIALIZE-NAME 'fields'
        FIELD cName      AS CHARACTER SERIALIZE-NAME 'fieldName'
        FIELD cLabel     AS CHARACTER SERIALIZE-NAME 'label' 
        FIELD cType      AS CHARACTER SERIALIZE-NAME 'type'
        FIELD iMaxLength AS INTEGER   SERIALIZE-NAME 'maxLength'
        FIELD cFormat    AS CHARACTER SERIALIZE-NAME 'format'
        FIELD lReadOnly  AS LOGICAL   SERIALIZE-NAME 'editable'
        FIELD lhidden    AS LOGICAL   SERIALIZE-NAME 'hidden'
        .
    /*------------------------------------------------------------------------------
     Purpose:
     Notes:
    ------------------------------------------------------------------------------*/

    METHOD OVERRIDE PROTECTED INTEGER HandleGet (INPUT poRequest AS IWebRequest ):
        DEFINE VARIABLE oResponse       AS IHttpResponse     NO-UNDO.         
        DEFINE VARIABLE oWriter         AS WebResponseWriter NO-UNDO.
        DEFINE VARIABLE oError          AS ErrorMessage      NO-UNDO.
        DEFINE VARIABLE oBody           AS String            NO-UNDO.
        DEFINE VARIABLE oJsonResponse   AS JsonObject        NO-UNDO.
        DEFINE VARIABLE oJsonArray      AS JsonArray         NO-UNDO.
        DEFINE VARIABLE lDebug          AS LOGICAL           NO-UNDO.
        DEFINE VARIABLE cDefautlCharSet AS CHARACTER         NO-UNDO INIT 'UTF-8'.
           
        DEFINE VARIABLE cCorralationID  AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cContent-Type   AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE htFileDict      AS HANDLE            NO-UNDO.
        DEFINE VARIABLE lcJsonInhalt    AS LONGCHAR          NO-UNDO.
        DEFINE VARIABLE ii              AS INTEGER           NO-UNDO.
        DEFINE VARIABLE i1              AS INTEGER           NO-UNDO.
        DEFINE VARIABLE i2              AS INTEGER           NO-UNDO.
        DEFINE VARIABLE cFileName       AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cFileNames      AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cFelder         AS CHARACTER         NO-UNDO.
        
        cFileNames = poRequest:URI:GetQueryValue("tables").
        MESSAGE 'File-Names = ' cFileNames.
        
        cFileNames = REPLACE(cFileNames, ',', ';').
        DO ii = 1 TO NUM-ENTRIES(cFileNames, ';'):
            cFileName = ENTRY(ii,cFileNames, ';').
            FIND FIRST AnaDat._File NO-LOCK 
                WHERE AnaDat._File._File-Name = cFileName NO-ERROR.
                
            FOR EACH AnaDat._Field OF AnaDat._File:
                CREATE tFileDict.
                ASSIGN
                    tFileDict.cName      = AnaDat._File._File-Name + '.' + AnaDat._Field._Field-Name
                    tFileDict.cType      = AnaDat._Field._Data-Type
                    tFileDict.cFormat    = AnaDat._Field._Format
                    tFileDict.lhidden    = FALSE
                    tFileDict.lReadOnly  = FALSE
                    tFileDict.iMaxLength = 10.

                FIND FIRST labeltexte NO-LOCK 
                    WHERE labeltexte.mandant  = '1000'
                    AND   labeltexte.benutzer = ''
                    AND   labeltexte.program  = ''
                    AND   labeltexte.feld     = Anadat._Field._Field-Name
                    AND   labeltexte.sprcd    = 1 NO-ERROR.

                IF AVAILABLE labeltexte THEN 
                DO:
                    tFileDict.cLabel = labeltexte.sidelabel.
                END.
                ELSE 
                DO:
                    tFileDict.cLabel = (IF AnaDat._Field._Label <> ? THEN AnaDat._Field._Label ELSE AnaDat._Field._Field-Name). 
                END.

                IF INDEX(AnaDat._Field._Format, '(') = 0 THEN tFileDict.iMaxLength = LENGTH(AnaDat._Field._Format).
                ELSE 
                DO:
                    i1 = INDEX(AnaDat._Field._Format, '(') + 1.
                    i2 = INDEX(AnaDat._Field._Format, ')').
                    tFileDict.iMaxLength = INTEGER(SUBSTRING(_Field._Format, i1, i2 - i1)).
                END.

            END.
        END.
        
        htFileDict = TEMP-TABLE tFileDict:DEFAULT-BUFFER-HANDLE.
                 
        cContent-Type   = "application/json".
        cDefautlCharSet = 'utf-8'. 
        oJsonArray      = NEW JsonArray().
        oJsonResponse   = NEW JsonObject().
        MESSAGE '1'.
        htFileDict:WRITE-JSON("JsonArray", oJsonArray).
        htFileDict:WRITE-JSON("JsonObject", oJsonResponse).

        MESSAGE "GetHandler" STRING(lcJsonInhalt).
          
//        oBody = NEW String(oJsonResponse:GetJsonText()).         
        oBody = NEW String(oJsonResponse:GetJsonText()).         
        WebHandlerUtilities:createHttpResponse(INPUT INTEGER(StatusCodeEnum:OK), INPUT oBody, INPUT cContent-Type, INPUT cDefautlCharSet, INPUT cCorralationID, OUTPUT oResponse).
//        oResponse:SetHeader(HttpHeaderBuilder:Build(cHeaderCorralationID):Value(cCorralationID):Header).
           
        oWriter = NEW WebResponseWriter(oResponse).
        oWriter:Open(). 
        oWriter:Close(). 
            
        RETURN 0.
        
        CATCH e AS Progress.Lang.Error:

        END CATCH.

        FINALLY:

        END FINALLY.

    END METHOD.

    METHOD OVERRIDE PROTECTED INTEGER HandleNotAllowedMethod(INPUT poRequest AS IWebRequest):
        DEFINE VARIABLE result AS INTEGER NO-UNDO.
        MESSAGE "HandleNotAllowedMethod called" VIEW-AS ALERT-BOX.
        RETURN result.
    END METHOD.

    METHOD OVERRIDE PROTECTED INTEGER HandleNotImplemented(INPUT poRequest AS IWebRequest):
        DEFINE VARIABLE result AS INTEGER NO-UNDO.
        MESSAGE "HandleNotImplemented called" VIEW-AS ALERT-BOX.
        RETURN result.
    END METHOD.

END CLASS.