/*------------------------------------------------------------------------
   File        : adressenImpl
   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 adressenImpl INHERITS WebHandler:
    
    DEFINE VARIABLE outilitiesHandler AS utilitiesHandler NO-UNDO.
    
    { ttInput.i    }
    
    DEFINE VARIABLE hFeld         AS HANDLE    NO-UNDO.
    DEFINE VARIABLE httBrowserKo  AS HANDLE    NO-UNDO.
    DEFINE VARIABLE httBrowserze  AS HANDLE    NO-UNDO.
    DEFINE VARIABLE httViewerKo   AS HANDLE    NO-UNDO.
    DEFINE VARIABLE httViewerZe   AS HANDLE    NO-UNDO.
    DEFINE VARIABLE httBrowser    AS HANDLE    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 i1            AS INTEGER   NO-UNDO.
    DEFINE VARIABLE i2            AS INTEGER   NO-UNDO.
    DEFINE VARIABLE cInhalt       AS CHARACTER NO-UNDO.
    DEFINE VARIABLE cFeld         AS CHARACTER NO-UNDO.
    DEFINE VARIABLE iAnzRec       AS INTEGER   NO-UNDO.
    DEFINE VARIABLE cAktion       AS CHARACTER NO-UNDO.
    DEFINE VARIABLE cUpdateAktion AS CHARACTER NO-UNDO
        INIT 'add,update,copy'.

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

    METHOD PUBLIC LONGCHAR addDatenFromJson(  ):
        
        DEFINE VARIABLE rRecid       AS RECID     NO-UNDO.
        DEFINE VARIABLE cResult      AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cFeld        AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cMessage     AS LONGCHAR  NO-UNDO.
        DEFINE VARIABLE iAnzahl      AS INTEGER   NO-UNDO.
        DEFINE VARIABLE cTabelle     AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cIndexFields AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cWhere       AS CHARACTER NO-UNDO.
        DEFINE VARIABLE ii           AS INTEGER   NO-UNDO.
        DEFINE VARIABLE cDataType    AS CHARACTER NO-UNDO.
        DEFINE VARIABLE hFeld        AS HANDLE    NO-UNDO.
        DEFINE VARIABLE cWert        AS CHARACTER NO-UNDO.
        DEFINE VARIABLE lRetVal      AS LOGICAL   NO-UNDO.
        
        iAnzRec     = 0.
        rRecid      = INT64(hbttInput::ident).
        cTabelle    = hbttInput::tabelle.
        
        CREATE BUFFER hbDBTabelle FOR TABLE cTabelle.
        hbttDBTabelle:FIND-FIRST ().
        
        cIndexFields = outilitiesHandler:getPrimaryIndexFields(cTabelle).
        
        cWhere = ''.
        DO ii = 1 TO NUM-ENTRIES(cIndexFields, ','):
            cFeld     = ENTRY(ii, cIndexFields, ',').
            hFeld     = hbDBTabelle:BUFFER-FIELD (cFeld).
            cWert     = hbttDBTabelle:BUFFER-FIELD(cFeld):BUFFER-VALUE(0).
            cDataType = hFeld:DATA-TYPE.
            CASE cDataType:
                WHEN 'character' THEN 
                    cWert = QUOTER(cWert).
            END CASE.
            cWhere = cWhere
                + (IF cWhere = '' THEN 'WHERE ' ELSE 'AND ')
                + SUBSTITUTE('&1.&3 = &2 ', cTabelle, cWert, cFeld ).
        END.
        lRetVal = hbDBTabelle:FIND-UNIQUE (cWhere, INTEGER(NO-LOCK) ) NO-ERROR.
        
        IF lRetVal THEN
        DO:
            outilitiesHandler:getErrorMessage(
                INPUT '9',
                INPUT INTEGER(hbttInput::sprcd),
                INPUT hbttDBTabelle::Geb_Cd,
                OUTPUT cMessage
                ).
            IF cMessage <> '' THEN
            DO:
                RETURN cMessage.
            END.
        END.

        REPEAT TRANSACTION ON ERROR UNDO, LEAVE:
            hbDBTabelle:BUFFER-CREATE().
            hbDBTabelle:BUFFER-COPY(hbttDBTabelle).
            hbttInput::ident = TRIM(STRING(hbDBTabelle:RECID,'>>>>>>>>9')).
            rRecid = hbDBTabelle:RECID.
            httBrowserKo::rRecid. 

            hbDBTabelle:BUFFER-RELEASE().
            LEAVE.
        END.

        iAnzahl = 0.
        CREATE QUERY hqDBTabelle.
        hqDBTabelle:SET-BUFFERS (hbDBTabelle).
        cWhere = SUBSTITUTE('FOR EACH &1 NO-LOCK WHERE &1.Firma = &2 ', cTabelle, QUOTER(hbttInput::mandant)).
        hqDBTabelle:QUERY-PREPARE (cWhere).
        hqDBTabelle:QUERY-OPEN().
        hqDBTabelle:GET-FIRST() NO-ERROR.

        DO WHILE NOT hqDBTabelle:QUERY-OFF-END:
            iAnzahl = iAnzahl + 1.
            IF hbDBTabelle:RECID = rRecid THEN LEAVE.
            hqDBTabelle:GET-NEXT () NO-ERROR.
        END.
        
        DELETE OBJECT hbDBTabelle NO-ERROR.
        DELETE OBJECT hqDBTabelle NO-ERROR.

        httBrowserKo::actualpage = (iAnzahl - (iAnzahl MOD httBrowserKo::pagesize)) / httBrowserKo::pagesize.
        IF httBrowserKo::actualpage < 1 THEN httBrowserKo::actualpage = 0.
        
        outilitiesHandler:updateBrowserKo(INPUT httBrowserKo).

        cAktion = 'next'.

        createNextResponse().

        cMessage = ''.
        RETURN cMessage.

    END METHOD.

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

    METHOD PUBLIC LONGCHAR copyDatenFromJson(  ):
        
        DEFINE VARIABLE rRecid   AS RECID     NO-UNDO.
        DEFINE VARIABLE cResult  AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cFeld    AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cMessage AS LONGCHAR  NO-UNDO.
        DEFINE VARIABLE iAnzahl  AS INTEGER   NO-UNDO.
        DEFINE VARIABLE cTabelle AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cWhere   AS CHARACTER NO-UNDO.

        cAktion = 'add'.
        
        cMessage = addDatenFromJson().        

        RETURN cMessage.

    END METHOD.

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

    METHOD PUBLIC VOID createLastResponse(  ):
        
        DEFINE VARIABLE lRetVal AS LOGICAL NO-UNDO.
        
        outilitiesHandler:createQuery(
            INPUT   hbttInput,
            INPUT   httBrowserKo,
            OUTPUT  hqDBTabelle,
            OUTPUT  hbDBTabelle
            ).
            
        outilitiesHandler:createTemptableBrowser(
            INPUT   hbttInput,
            INPUT   httBrowserKo,
            OUTPUT  httBrowser
            ).

        iAnzRec = outilitiesHandler:fillBrowserTT (
            INPUT        httBrowserKo,
            INPUT        hqDBTabelle,
            INPUT        hbttInput,
            INPUT-OUTPUT httBrowser
            ).
        
        httBrowserKo::actualpage = httBrowserKo::actualpage + 1.
        outilitiesHandler:updateBrowserKo( INPUT httBrowserKo ).
        
        RETURN.

    END METHOD.

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

    METHOD PUBLIC VOID createNextResponse(  ):
        
        DEFINE VARIABLE lRetVal AS LOGICAL NO-UNDO.
        
        outilitiesHandler:createQuery(
            INPUT   hbttInput,
            INPUT   httBrowserKo,
            OUTPUT  hqDBTabelle,
            OUTPUT  hbDBTabelle
            ).
            
        outilitiesHandler:createTemptableBrowser(
            INPUT   hbttInput,
            INPUT   httBrowserKo,
            OUTPUT  httBrowser
            ).

        DO WHILE TRUE:
            iAnzRec = outilitiesHandler:fillBrowserTT (
                INPUT        httBrowserKo,
                INPUT        hqDBTabelle,
                INPUT        hbttInput,
                INPUT-OUTPUT httBrowser
                ).
            IF iAnzRec = 0 AND httBrowserKo::actualpage > 0 THEN 
            DO:
                httBrowserKo::actualpage = httBrowserKo::actualpage - 1.
                NEXT.
            END.
            LEAVE.
        END.
        
        httBrowserKo::actualpage = httBrowserKo::actualpage + 1.
        outilitiesHandler:updateBrowserKo( INPUT httBrowserKo ).
        
        RETURN.

    END METHOD.

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

    METHOD PUBLIC VOID createPrevResponse(  ):
        
        DEFINE VARIABLE lRetVal AS LOGICAL NO-UNDO.
        
        httBrowserKo::actualpage = httBrowserKo::actualpage - 2.
        IF (httBrowserKo::actualpage < 0) THEN httBrowserKo::actualpage = 0.
        
        outilitiesHandler:createQuery(
            INPUT   hbttInput,
            INPUT   httBrowserKo,
            OUTPUT  hqDBTabelle,
            OUTPUT  hbDBTabelle
            ).
            
        outilitiesHandler:createTemptableBrowser(
            INPUT   hbttInput,
            INPUT   httBrowserKo,
            OUTPUT  httBrowser
            ).

        iAnzRec = outilitiesHandler:fillBrowserTT (
            INPUT        httBrowserKo,
            INPUT        hqDBTabelle,
            INPUT        hbttInput,
            INPUT-OUTPUT httBrowser
            ).
        
        httBrowserKo::actualpage = httBrowserKo::actualpage + 1.
        outilitiesHandler:updateBrowserKo( INPUT httBrowserKo ).
        
        RETURN.

    END METHOD.

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

    METHOD PUBLIC VOID createSameResponse(  ):
        
        DEFINE VARIABLE lRetVal AS LOGICAL   NO-UNDO.
        DEFINE VARIABLE cTitle  AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cHeader AS CHARACTER NO-UNDO.
        
        cTitle = outilitiesHandler:getTitle(
            INPUT hbttInput
            ).
        MESSAGE 'Titel = ' cTitle.
        cHeader = outilitiesHandler:getHeader(
            INPUT hbttInput
            ).
        MESSAGE 'Header = ' cHeader.
        ASSIGN
            hbttInput::params = SUBSTITUTE('Title=&1;Header=&2':U, cTitle, cHeader).
            
        outilitiesHandler:createQuery(
            INPUT   hbttInput,
            INPUT   httBrowserKo,
            OUTPUT  hqDBTabelle,
            OUTPUT  hbDBTabelle
            ).
        httBrowserKo::actualpage = httBrowserKo::actualpage - 1.
        outilitiesHandler:updateBrowserKo( INPUT httBrowserKo ).
            
        outilitiesHandler:createTemptableBrowser(
            INPUT   hbttInput,
            INPUT   httBrowserKo,
            OUTPUT  httBrowser
            ).

        DO WHILE TRUE:
            iAnzRec = outilitiesHandler:fillBrowserTT (
                INPUT        httBrowserKo,
                INPUT        hqDBTabelle,
                INPUT        hbttInput,
                INPUT-OUTPUT httBrowser
                ).
            IF iAnzRec = 0 AND httBrowserKo::actualpage > 0 THEN 
            DO:
                httBrowserKo::actualpage = httBrowserKo::actualpage - 1.
                NEXT.
            END.
            LEAVE.
        END.
        
        httBrowserKo::actualpage = httBrowserKo::actualpage + 1.
        outilitiesHandler:updateBrowserKo( INPUT httBrowserKo ).
        
        RETURN.

    END METHOD.

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

    METHOD PUBLIC VOID createSearchResponse(  ):
        
        DEFINE VARIABLE lRetVal AS LOGICAL NO-UNDO.
        
        // FIND FIRST ttInput.

        /*        lRetVal = outilitiesHandler:getBrowserKopf(*/
        /*            INPUT  hbttInput,                      */
        /*            OUTPUT httBrowserKo                    */
        /*            ).                                     */
        /*                                                   */
        httBrowserKo::actualpage = 0.
        
        outilitiesHandler:createSearchQuery(
            INPUT   hbttInput,
            INPUT   httBrowserKo,
            OUTPUT  hqDBTabelle,
            OUTPUT  hbDBtabelle
            ).
            
        outilitiesHandler:createTemptableBrowser(
            INPUT   hbttInput,
            INPUT   httBrowserKo,
            OUTPUT  httBrowser
            ).

        iAnzRec = outilitiesHandler:fillBrowserTT (
            INPUT        httBrowserKo,
            INPUT        hqDBtabelle,
            INPUT        hbttInput,
            INPUT-OUTPUT httBrowser
            ).
        
        httBrowserKo::actualpage = httBrowserKo::actualpage = 0.
        outilitiesHandler:updateBrowserKo( INPUT httBrowserKo ).
        
        RETURN.
        
    END METHOD.

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

    METHOD PUBLIC VOID createSelectResponse(  ):
        
        DEFINE VARIABLE ii         AS INTEGER   NO-UNDO.
        DEFINE VARIABLE hViewer_ze AS HANDLE    NO-UNDO.
        DEFINE VARIABLE lRetVal    AS LOGICAL   NO-UNDO.
        DEFINE VARIABLE cInhalt    AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cWhere     AS CHARACTER NO-UNDO.
        DEFINE VARIABLE rRecid     AS RECID     NO-UNDO.
        
        iAnzRec    = 0.
        hViewer_ze = BUFFER viewer_ze:HANDLE.
        rRecid     = INT64(hbttInput::ident). 
        
        outilitiesHandler:startViewerKopf (
            INPUT   hbttInput,
            OUTPUT  httViewerKo
            ).

        outilitiesHandler:createTemptableViewer(
            INPUT   hbttInput,
            INPUT   httViewerKo,
            OUTPUT  httDBTabelle
            ).
        
        cWhere = SUBSTITUTE('WHERE RECID(&1) = &2 ', hbttInput::tabelle, INT64(hbttInput::ident) ).
        hbDBTabelle:FIND-UNIQUE(cWhere, INTEGER(NO-LOCK)) NO-ERROR.    

        httDBTabelle:EMPTY-TEMP-TABLE().
        httDBTabelle:BUFFER-CREATE().
        httDBTabelle:BUFFER-COPY(hbDBtabelle).
        httDBTabelle::ident = TRIM(STRING(hbDBTabelle:RECID,'>>>>>>>>9')).         
            
        iAnzRec = iAnzRec + 1.
        
        MESSAGE 'Start getField' httDBTabelle:NUM-FIELDS.
        
        DO ii = 1 TO httDBTabelle:NUM-FIELDS:
            FIND FIRST viewer_ze NO-LOCK
                WHERE viewer_ze.mandant  = hbttInput::mandant
                AND   viewer_ze.benutzer = hbttInput::benutzer
                AND   viewer_ze.program  = hbttInput::programm
                AND   viewer_ze.tabelle  = hbttInput::tabelle
                AND   viewer_ze.feld     = httDBTabelle:BUFFER-FIELD(ii):NAME
                AND   viewer_ze.addFieldTabelle <> '' NO-ERROR.
            IF NOT AVAILABLE viewer_ze THEN NEXT.
            lRetVal = outilitiesHandler:getField(
                INPUT hbttInput,
                INPUT viewer_ze.addFieldTabelle,
                INPUT viewer_ze.addFieldField,
                INPUT viewer_ze.addFieldKeyFields,
                INPUT viewer_ze.addFieldKeyValues,
                INPUT httDBTabelle,
                OUTPUT cInhalt
                ).
            httDBTabelle:BUFFER-FIELD(ii):BUFFER-VALUE(0) = cInhalt.
            MESSAGE httDBTabelle:BUFFER-FIELD(ii):NAME httDBTabelle:BUFFER-FIELD(ii):BUFFER-VALUE(0).
        END.
        
        httBrowserKo::lastrecid = httDBTabelle::ident.
        outilitiesHandler:updateBrowserKo(INPUT httBrowserKo).
                
        RETURN.

    END METHOD.

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

    METHOD PUBLIC VOID createSortResponse(  ):
        
        DEFINE VARIABLE cSortfeld AS CHARACTER NO-UNDO INIT ''.
        DEFINE VARIABLE cRichtung AS CHARACTER NO-UNDO INIT ''.
        DEFINE VARIABLE cJsonFeld AS LONGCHAR  NO-UNDO INIT ''.
        
        DEFINE VARIABLE hQuery    AS HANDLE    NO-UNDO.
        DEFINE VARIABLE hBuffer   AS HANDLE    NO-UNDO.
        
        FIND FIRST ttInput.
        
        cJsonFeld = CHR(123) + SUBSTITUTE(' "&1" : "&1" ', hbttInput::SORT) + CHR(125).
        
        outilitiesHandler:createTemptableBrowser(
            INPUT  hbttInput     , 
            INPUT  httBrowserKo ,
            OUTPUT httBrowser
            ).
            
        MESSAGE 'cJsonFeld = ' STRING(cJsonFeld).

        httBrowser:READ-JSON('LONGCHAR', cJsonFeld, 'EMPTY').
        
        CREATE QUERY hQuery.
        hQuery:SET-BUFFERS(httBrowser).
        hQuery:QUERY-PREPARE ('FOR EACH ttBrowser NO-LOCK').
        hQuery:QUERY-OPEN ().
        hQuery:GET-FIRST ().
        DO i1 = 1 TO httBrowser:NUM-FIELDS:
            IF httBrowser:BUFFER-FIELD(i1):BUFFER-VALUE(0) = '' THEN NEXT.
            cSortfeld = httBrowser:BUFFER-FIELD(i1):NAME.
            LEAVE.
        END.
        hQuery:QUERY-CLOSE ().
        DELETE OBJECT hQuery.       
        
        MESSAGE 'Sortfeld = ' cSortfeld.
        
        outilitiesHandler:setSortField(
            INPUT cSortfeld             ,
            INPUT-OUTPUT httBrowserKo
            ).
        
        MESSAGE 'Sortierung neu' cSortfeld '->' httBrowserKo::sortfeld.
        
        cAktion = 'start'.
        createStartResponse().
        
        RETURN.

    END METHOD.

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

    METHOD PUBLIC VOID createStartResponse(  ):
        
        DEFINE VARIABLE lcString AS LONGCHAR  NO-UNDO.
        
        DEFINE VARIABLE cTitle   AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cHeader  AS CHARACTER NO-UNDO.

        iAnzRec = 0.
        
        cTitle = outilitiesHandler:getTitle(
            INPUT hbttInput
            ).
        MESSAGE 'Titel = ' cTitle.
        cHeader = outilitiesHandler:getHeader(
            INPUT hbttInput
            ).
        MESSAGE 'Header = ' cHeader.
        ASSIGN
            hbttInput::params = SUBSTITUTE('Title=&1;Header=&2':U, cTitle, cHeader).
        
        outilitiesHandler:startBrowserKopf (
            INPUT   hbttInput,
            OUTPUT  httBrowserKo
            ).

        outilitiesHandler:createQuery(
            INPUT   hbttInput,
            INPUT   httBrowserKo,
            OUTPUT  hqDBTabelle,
            OUTPUT  hbDBTabelle
            ).
            
        outilitiesHandler:createTemptableBrowser(
            INPUT   hbttInput,
            INPUT   httBrowserKo,
            OUTPUT  httBrowser
            ).
        
        iAnzRec = outilitiesHandler:fillBrowserTT (
            INPUT        httBrowserKo,
            INPUT        hqDBTabelle,
            INPUT        hbttInput,
            INPUT-OUTPUT httBrowser
            ).
            
        httBrowserKo::actualpage = 1.
        outilitiesHandler:updateBrowserKo( INPUT httBrowserKo ).
        
        RETURN.

    END METHOD.

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

    METHOD PUBLIC VOID deleteDatenFromJson(  ):

        DEFINE VARIABLE cTabelle AS CHARACTER NO-UNDO.        
        DEFINE VARIABLE rRecid   AS RECID     NO-UNDO.
        DEFINE VARIABLE cWhere   AS CHARACTER NO-UNDO.
        
        iAnzRec    = 0.
        rRecid     = INT64(hbttInput::ident).
        cTabelle   = hbttInput::tabelle.
        
        CREATE BUFFER hbDBTabelle FOR TABLE cTabelle.

        REPEAT TRANSACTION ON ERROR UNDO, LEAVE:
            cWhere = SUBSTITUTE('WHERE RECID(&1) = &2 ', cTabelle, rRecid).
            hbDBTabelle:FIND-UNIQUE (cWhere, INTEGER(EXCLUSIVE-LOCK) ) NO-ERROR.
            hbDBTabelle:BUFFER-DELETE  ().
            hbDBTabelle:BUFFER-RELEASE ().
            hbDBTabelle:GET-PREV ().
            rRecid = (IF hbDBTabelle:AVAILABLE THEN hbDBTabelle:RECID ELSE ? ).
            LEAVE.
        END.
        
        DELETE OBJECT hbDBTabelle NO-ERROR.

        httDBTabelle:EMPTY-TEMP-TABLE().
        IF rRecid = ? THEN
        DO:
            hbttInput::ident = ''.
            cAktion          = 'first'.
        END.
        ELSE
        DO:
            hbttInput::ident = TRIM(STRING(rRecid,'>>>>>>>>9')).
            cAktion          = 'next'.
        END.

        httBrowserKo::actualpage = httBrowserKo::actualpage - 1.
        IF httBrowserKo::actualpage < 0 THEN httBrowserKo::actualpage = 0.
        httBrowserKo::lastrecid  = rRecid.
        outilitiesHandler:updateBrowserKo(INPUT httBrowserKo).

        createNextResponse().

        RETURN.

    END METHOD.

    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 oJsonArray   AS JsonArray         NO-UNDO.
        DEFINE VARIABLE oJsonDataset AS JsonObject        NO-UNDO.
        DEFINE VARIABLE oInput       AS JsonObject        NO-UNDO.
        
        DEFINE VARIABLE lRetVal      AS LOGICAL           NO-UNDO.
        DEFINE VARIABLE iRetVal      AS INTEGER           NO-UNDO.
        DEFINE VARIABLE httBuffer    AS HANDLE            NO-UNDO.
        DEFINE VARIABLE cGetHtml     AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE lcHTML       AS LONGCHAR          NO-UNDO.
        DEFINE VARIABLE cString      AS CHARACTER         NO-UNDO.
        
        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.
        
        CREATE TEMP-TABLE httDBTabelle.
        CREATE BUFFER hbDBTabelle FOR TABLE ttInput.tabelle.
        httDBTabelle:CREATE-LIKE (hbDBTabelle).
        httDBTabelle:ADD-NEW-FIELD ('ident', 'character').
        httDBTabelle:TEMP-TABLE-PREPARE(ttInput.tabelle).
        hbttDBTabelle = httDBTabelle:DEFAULT-BUFFER-HANDLE.
        
        httDBTabelle:EMPTY-TEMP-TABLE ().
        
        outilitiesHandler = NEW utilitiesHandler ().

        DO WHILE lRetVal:
            MESSAGE 'Temptable-Name = ' httDBTabelle:NAME.

            IF LOOKUP(cAktion, cUpdateAktion, ',') > 0 THEN 
            DO:                  
                lRetVal = outilitiesHandler:readInputDatenFromJson(
                    INPUT oJsonData,
                    INPUT hbttInput,
                    INPUT-OUTPUT hbttDBTabelle
                    ).
                hbttDBTabelle:FIND-FIRST ().
                IF NOT lRetVal THEN LEAVE.
            END.
            
            lRetVal = outilitiesHandler:getBrowserKopf(
                INPUT  hbttInput,
                OUTPUT httBrowserKo
                ).
            IF cAktion = 'same' THEN ttInput.ident = TRIM(STRING(httBrowserKo::lastrecid,'>>>>>>>>9')).
            
            CASE cAktion:
                WHEN 'start'  THEN createStartResponse ( ).
                WHEN 'same'   THEN createSameResponse  ( ).
                WHEN 'first'  THEN createStartResponse ( ).
                WHEN 'last'   THEN createLastResponse  ( ).
                WHEN 'next'   THEN createNextResponse  ( ).
                WHEN 'prev'   THEN createPrevResponse  ( ).
                WHEN 'select' THEN createSelectResponse( ).
                WHEN 'search' THEN createSearchResponse( ).
                WHEN 'sort'   THEN createSortResponse  ( ).
                WHEN 'update' THEN updateDatenFromJson ( ).
                WHEN 'delete' THEN deleteDatenFromJson ( ).
                
                WHEN 'copy'   THEN 
                    DO:
                        lcJsonString = copyDatenFromJson   ( ).
                        IF lcJsonString <> '' THEN 
                        DO:
                            lRetVal = FALSE.
                            LEAVE.
                        END.
                    END.
                WHEN 'add'   THEN 
                    DO:
                        lcJsonString = addDatenFromJson   ( ).
                        IF lcJsonString <> '' THEN 
                        DO:
                            lRetVal = FALSE.
                            LEAVE.
                        END.
                    END.
                    
            END CASE.
            
            MESSAGE 'Anzahl gelesene Adress-Record = ' iAnzRec.
            
            hbttDBTabelle:FIND-FIRST () NO-ERROR.
            FIND FIRST ttInput.
            
            /*  IF  NOT AVAILABLE ttBrowser  AND         */
            /*      NOT AVAILABLE ttKGebinde THEN LEAVE. */

            IF iAnzRec = 0 THEN 
            DO:
                lRetVal = FALSE.
                LEAVE.
            END.
                
            httBuffer = (IF cAktion = 'select' THEN httDBTabelle ELSE httBrowser).
            
            oJsonArray = NEW JsonArray().
            
            oJsonData = NEW JsonObject().
            hbttInput:WRITE-JSON('JsonObject', oJsonData).
            oJsonArray:Add(oJsonData).
            
            oJsonData = NEW JsonObject().
            httBuffer:WRITE-JSON('JsonObject', oJsonData).
            oJsonArray:Add(oJsonData).
            
            oJsonArray:WRITE(lcJsonString, FALSE).
            oJsonData:WRITE(lcJsonString, FALSE).
            MESSAGE 'JsonArray = ' STRING(lcJsonString).

            MESSAGE 'lcString Rückgabe: ' + STRING(lcJsonString).

            oResponse             = NEW OpenEdge.Web.WebResponse().
            oResponse:StatusCode  = INTEGER(StatusCodeEnum:OK).
            oResponse:ContentType = "application/json;charset=utf-8".
            oWriter               = NEW WebResponseWriter(oResponse).
            
            oWriter:Open().
            oWriter:Write(lcJsonString).
            oWriter:Close().
            
            LEAVE.
        END.
        
        IF NOT lRetVal THEN 
        DO:
            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().
        END.

        DELETE OBJECT httDBTabelle NO-ERROR.
        DELETE OBJECT hbDBTabelle  NO-ERROR.
        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.

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

    METHOD PUBLIC LOGICAL updateDatenFromJson(  ):
        
        DEFINE VARIABLE lRetVal  AS LOGICAL   NO-UNDO.
        DEFINE VARIABLE rRecid   AS RECID     NO-UNDO.
        DEFINE VARIABLE cResult  AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cFeld    AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cWhere   AS CHARACTER NO-UNDO.
        DEFINE VARIABLE iAnzahl  AS INTEGER   NO-UNDO.
        DEFINE VARIABLE cTabelle AS CHARACTER NO-UNDO.
        
        iAnzRec    = 0.
        rRecid     = INT64(hbttInput::ident).
        cTabelle   = hbttInput::tabelle.
        
        CREATE BUFFER hbDBTabelle FOR TABLE cTabelle.

        cWhere = SUBSTITUTE('WHERE RECID(&1) = &2 ', hbttInput::tabelle, rRecid).
        hbDBTabelle:FIND-UNIQUE (cWhere, INTEGER(NO-LOCK) ) NO-ERROR.
        hbttDBTabelle:FIND-FIRST ().

        lRetVal = hbDBTabelle:BUFFER-COMPARE(hbttDBTabelle).

        REPEAT TRANSACTION WHILE NOT lRetVal ON ERROR UNDO, LEAVE:
            cWhere = SUBSTITUTE('WHERE RECID(&1) = &2 ', hbttInput::tabelle, rRecid).
            hbDBTabelle:FIND-UNIQUE (cWhere, INTEGER(EXCLUSIVE-LOCK) ) NO-ERROR.
            DO i1 = 1 TO hbDBTabelle:NUM-FIELDS:
                cFeld = hbDBTabelle:BUFFER-FIELD(i1):NAME.
                hFeld = hbttDBTabelle:BUFFER-FIELD(cFeld) NO-ERROR.
                IF hFeld:BUFFER-VALUE(0) = hbDBTabelle:BUFFER-FIELD(i1):BUFFER-VALUE(0) THEN NEXT.
                hbDBTabelle:BUFFER-FIELD(i1):BUFFER-VALUE(0) = hFeld:BUFFER-VALUE(0).
            END.
            hbDBTabelle:BUFFER-RELEASE ().
            LEAVE.
        END.

        DELETE OBJECT hqDBTabelle NO-ERROR.
        CREATE QUERY hqDBTabelle.
        cWhere = SUBSTITUTE('FOR EACH &1 WHERE &1.Firma = &2 NO-LOCK ', cTabelle, QUOTER(hbttInput::mandant)).
        hqDBTabelle:SET-BUFFERS(hbDBTabelle).
        hqDBTabelle:QUERY-PREPARE(cWhere).
        hqDBTabelle:QUERY-OPEN().
        hqDBTabelle:GET-FIRST() NO-ERROR.
        iAnzahl = 0.

        DO WHILE NOT hqDBTabelle:QUERY-OFF-END:
            iAnzahl = iAnzahl + 1.
            IF hbDBTabelle:RECID = rRecid THEN LEAVE.
            hqDBTabelle:GET-NEXT () NO-ERROR.
        END.
        
        DELETE OBJECT hbDBTabelle NO-ERROR.
        DELETE OBJECT hqDBTabelle NO-ERROR.

        httBrowserKo::actualpage = (iAnzahl - (iAnzahl MOD httBrowserKo::pagesize)) / httBrowserKo::pagesize.
        IF httBrowserKo::actualpage < 1 THEN httBrowserKo::actualpage = 0.

        httDBTabelle:EMPTY-TEMP-TABLE ().
        hbttInput::ident = TRIM(STRING(rRecid,'>>>>>>>>9')).
        cAktion          = 'next'.

        IF (iAnzahl MOD httBrowserKo::pagesize) = 0 THEN httBrowserKo::actualpage = httBrowserKo::actualpage - 1.
        httBrowserKo::lastrecid  = INT64(ttInput.ident).

        outilitiesHandler:updateBrowserKo(INPUT httBrowserKo).

        createNextResponse().

        lRetVal = TRUE.
        RETURN lRetVal.

    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.

    /*------------------------------------------------------------------------------
     Purpose:
     Notes:
    ------------------------------------------------------------------------------*/
    DESTRUCTOR PUBLIC adressenImpl ( ):
	    
        DELETE OBJECT outilitiesHandler NO-ERROR.
	    
        MESSAGE 'DESTRUCTOR adressenImpl'.

    END DESTRUCTOR.

END CLASS.
