/*------------------------------------------------------------------------
   File        : getPrimaryIndexFieldsImpl
   Purpose     : 
   Syntax      : 
   Description : 
   Author(s)   : walter.riechsteiner
   Created     : Wed May 29 21:02:53 CEST 2024
   Notes       : 
 ----------------------------------------------------------------------*/

BLOCK-LEVEL ON ERROR UNDO, THROW.

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 utilities.utilitiesHandler FROM PROPATH.

CLASS getPrimaryIndexFieldsImpl INHERITS WebHandler:

    DEFINE VARIABLE outilitiesHandler AS utilitiesHandler NO-UNDO.

    { ttInput.i    }

    METHOD OVERRIDE PROTECTED INTEGER HandlePost(INPUT poRequest AS IWebRequest):
        DEFINE VARIABLE oResponse     AS IHttpResponse     NO-UNDO.
        DEFINE VARIABLE oWriter       AS WebResponseWriter NO-UNDO.
        DEFINE VARIABLE oJsonData     AS JsonObject        NO-UNDO.
        DEFINE VARIABLE lcJsonString  AS LONGCHAR          NO-UNDO.
        DEFINE VARIABLE oInput        AS JsonObject        NO-UNDO.
        
        DEFINE VARIABLE hbttDBTabelle AS HANDLE            NO-UNDO.
        DEFINE VARIABLE httDBTabelle  AS HANDLE            NO-UNDO.
        DEFINE VARIABLE hqDBTabelle   AS HANDLE            NO-UNDO.
        DEFINE VARIABLE hbDBTabelle   AS HANDLE            NO-UNDO.
    
        DEFINE VARIABLE lRetVal       AS LOGICAL           NO-UNDO.
        DEFINE VARIABLE iSprcd        AS INTEGER           NO-UNDO.
        DEFINE VARIABLE cAktion       AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cIndexFields  AS CHARACTER         NO-UNDO.
        
        EMPTY TEMP-TABLE ttInput    .

        lRetVal = WebHandlerUtilities:getJsonFromRequest(INPUT poRequest, OUTPUT oJsonData) NO-ERROR.
        MESSAGE 'getJsonFromRequest    = ' lRetVal.
        
        oJsonData:WRITE(lcJsonString, FALSE).
        MESSAGE 'ganzer eingangsstring = ' STRING(lcJsonString).
        
        //
        //  Input-Daten (Steuersatz) aus JsonObject lesen und in ttInput füllen
        //
        EMPTY TEMP-TABLE ttInput    .
        hbttInput = TEMP-TABLE ttInput:DEFAULT-BUFFER-HANDLE.
        oInput    = NEW JsonObject().
        oInput    = oJsonData:GetJsonObject('ttInput').
        lRetVal   = hbttInput:READ-JSON ('JsonObject', oInput).
        
        FIND FIRST ttInput.
        cAktion = ttInput.action.
        
        outilitiesHandler = NEW utilitiesHandler ().
        
        DO WHILE lRetVal:
            
            FIND FIRST ttInput.
            cIndexFields = outilitiesHandler:getPrimaryIndexFields(
                INPUT ttInput.tabelle
                ).
            
            MESSAGE 'Rückgabe: ' + cIndexFields.

            oResponse             = NEW OpenEdge.Web.WebResponse().
            oResponse:StatusCode  = INTEGER(StatusCodeEnum:OK).
            oResponse:ContentType = "text/plain".
            oWriter               = NEW WebResponseWriter(oResponse).
            
            oWriter:Open().
            oWriter:Write(cIndexFields).
            oWriter:Close().
            
            LEAVE.
        END.
        
        DO WHILE NOT lRetVal: 
            oResponse = NEW OpenEdge.Web.WebResponse().
            oResponse:StatusCode = INTEGER(StatusCodeEnum:OK).
            oResponse:ContentType = "application/json;charset=utf-8".
            oWriter = NEW WebResponseWriter(oResponse).
            oWriter:Open().
            MESSAGE 'lcString Rückgabe: ' + STRING(lcJsonString) VIEW-AS ALERT-BOX.
            oWriter:Write(lcJsonString).
            oWriter:Close().
            LEAVE.
        END.

        MESSAGE "Exiting HandlePost" VIEW-AS ALERT-BOX.

        CATCH e AS Progress.Lang.Error:
            MESSAGE "Exception occurred: " + e:GetMessage(1) VIEW-AS ALERT-BOX ERROR BUTTONS OK.
            RETURN -1.
        END CATCH.
        
    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.
