 
/*------------------------------------------------------------------------
   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 getFileDIctImpl INHERITS WebHandler: 

    DEFINE TEMP-TABLE tFileDIct
        SERIALIZE-NAME 'fields'
        FIELD cName   AS CHARACTER SERIALIZE-NAME 'name' 
        FIELD cType   AS CHARACTER SERIALIZE-NAME 'type'
        FIELD iLaenge AS INTEGER   SERIALIZE-NAME 'width'
        .
    /*------------------------------------------------------------------------------
     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 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 oJsonArray      AS JsonArray         NO-UNDO.
        DEFINE VARIABLE lcJsonInhalt    AS LONGCHAR          NO-UNDO.
        DEFINE VARIABLE ii              AS INTEGER           NO-UNDO.
        DEFINE VARIABLE cFileName       AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cFelder         AS CHARACTER         NO-UNDO.
        
        cFileName = poRequest:URI:GetQueryValue("File").
        MESSAGE 'File-Name = ' cFileName.
        
        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._Field._Field-Name
                tFileDIct.cType   = REPLACE(AnaDat._Field._Data-Type, '-', '_')
                tFileDIct.iLaenge = 10.
            CASE AnaDat._Field._Data-Type:
                WHEN 'logical' THEN 
                    tFileDIct.cType   = 'checkbox'.
                WHEN 'character' THEN 
                    tFileDIct.cType   = 'text'.
                WHEN 'integer' THEN 
                    tFileDIct.cType   = 'number'.
                WHEN 'logical' THEN 
                    tFileDIct.cType   = 'checkbox'.
            END CASE.
        END.
        htFileDIct = TEMP-TABLE tFileDIct:DEFAULT-BUFFER-HANDLE.
                 
        cContent-Type = "application/json".
           
        oError = NEW ErrorMessage().
        oError:lDebug = lDebug.
          
        oJsonResponse = NEW JsonObject().
        htFileDIct:WRITE-JSON('JsonObject', oJsonResponse).
        
        oJsonResponse:WRITE(lcJsonInhalt, FALSE).
        /*        ii = INDEX(lcJsonInhalt, '[').                 */
        /*        lcJsonInhalt = SUBSTRING(lcJsonInhalt, ii).    */
        /*                                                       */
        /*        ii = R-INDEX(lcJsonInhalt, ']').               */
        /*        lcJsonInhalt = SUBSTRING(lcJsonInhalt, 01, ii).*/

        MESSAGE "GetHandler".
          
//        oBody = NEW String(oJsonResponse:GetJsonText()).         
        oBody = NEW String(lcJsonInhalt).         
        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.