/*------------------------------------------------------------------------
   File        : gemis_startImpl
   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 utilities.utilitiesHandler FROM PROPATH.
USING src.ch.adprime.gemis.WebHandlerUtilities FROM PROPATH.

CLASS gemis_startImpl INHERITS WebHandler:
    
    DEFINE VARIABLE outilitiesHandler AS utilitiesHandler.
    
    { ttInput.i }

    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 hqAufko       AS HANDLE NO-UNDO.
    DEFINE VARIABLE hbAufko       AS HANDLE NO-UNDO.
    
    /*------------------------------------------------------------------------------
     Purpose:
     Notes:
    ------------------------------------------------------------------------------*/

    METHOD PUBLIC VOID createAufkoResponse(  ):
        
        DEFINE VARIABLE lcString AS LONGCHAR  NO-UNDO.
        
        DEFINE VARIABLE cTitle   AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cHeader  AS CHARACTER NO-UNDO.
        DEFINE VARIABLE iAnzRec  AS INTEGER   NO-UNDO.
        
        FIND FIRST ttInput.
        iAnzRec = 0.
        
        hbttInput = TEMP-TABLE ttInput:DEFAULT-BUFFER-HANDLE.
        cTitle = outilitiesHandler:getTitle(
            INPUT hbttInput
            ).
        cHeader = outilitiesHandler:getHeader(
            INPUT hbttInput
            ).
            
        ASSIGN
            hbttInput::params = SUBSTITUTE('Title=&1;Header=&2':U, cTitle, cHeader).
        
        outilitiesHandler:startBrowserKopf (
            INPUT   hbttInput,
            OUTPUT  httBrowserKo
            ).

        outilitiesHandler:createQuery(
            INPUT   hbttInput,
            INPUT   httBrowserKo,
            OUTPUT  hqAufko,
            OUTPUT  hbAufko
            ).
            
        outilitiesHandler:createTemptableBrowser(
            INPUT   hbttInput,
            INPUT   httBrowserKo,
            OUTPUT  httBrowser
            ).
        
        iAnzRec = outilitiesHandler:fillBrowserTT (
            INPUT        httBrowserKo,
            INPUT        hqAufko,
            INPUT        hbttInput,
            INPUT-OUTPUT httBrowser
            ).
            
        httBrowserKo::actualpage = 1.
        outilitiesHandler:updateBrowserKo( INPUT httBrowserKo ).
        
        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 lRetVal      AS LOGICAL           NO-UNDO.
        DEFINE VARIABLE cAktion      AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE oInput       AS JsonObject        NO-UNDO.
        
        DEFINE VARIABLE hbttMandant  AS HANDLE            NO-UNDO.
        DEFINE VARIABLE lcHTML       AS LONGCHAR          NO-UNDO.
        DEFINE VARIABLE cDatei       AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE ii           AS INTEGER           NO-UNDO.
        DEFINE VARIABLE lAdmin       AS LOGICAL           NO-UNDO.
        DEFINE VARIABLE hFeld        AS HANDLE            NO-UNDO.
        DEFINE VARIABLE iAktStufe    AS INTEGER           NO-UNDO.
        DEFINE VARIABLE lFirst       AS LOGICAL           NO-UNDO.
        DEFINE VARIABLE cKomma       AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cQueryString AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cString      AS CHARACTER         NO-UNDO.
        
        DEFINE BUFFER bwebmenu FOR webmenu.

        lRetVal = WebHandlerUtilities:getJsonFromRequest(INPUT poRequest, OUTPUT oJsonData) NO-ERROR.
        MESSAGE 'getJsonFromRequest = ' lRetVal STRING(lcJsonString).
        
        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:
            IF cAktion = 'aufko' THEN 
            DO:
                createAufkoResponse ( ).
                httBrowser:WRITE-JSON('JsonObject', oJsonData).
                oJsonData:write(lcJsonString, FALSE).
                LEAVE. 
            END.
             
            REPEAT TRANSACTION ON ERROR UNDO, LEAVE:
                IF ttInput.benutzer = 'Admin' THEN LEAVE.
                FOR EACH webmenu NO-LOCK USE-INDEX webmenu-k1
                    WHERE webmenu.mandant  = ttInput.mandant
                    AND   webmenu.benutzer = 'Admin'
                    AND   webmenu.sprcd    = INTEGER(ttInput.sprcd):
                    FIND bwebmenu USE-INDEX webmenu-k1
                        WHERE bwebmenu.mandant  = webmenu.mandant
                        AND   bwebmenu.benutzer = ttInput.benutzer
                        AND   bwebmenu.sprcd    = webmenu.sprcd
                        AND   bwebmenu.stufe1   = webmenu.stufe1 NO-ERROR.
                    IF NOT AVAILABLE bwebmenu THEN CREATE bwebmenu.
                    BUFFER-COPY webmenu
                        EXCEPT  webmenu.benutzer
                        TO      bwebmenu
                        ASSIGN  
                        bwebmenu.benutzer = ttInput.benutzer.
                END.
                RELEASE webmenu.
                RELEASE bwebmenu.
                LEAVE.
            END.
 
            lcJsonString = CHR(123).
            lFirst       = TRUE.
            iAktStufe    = 0.
    
            IF ttInput.action = 'first' THEN 
            DO:
                FOR EACH webmenu NO-LOCK USE-INDEX webmenu-k1
                    WHERE webmenu.mandant  = ttInput.mandant
                    AND   webmenu.benutzer = ttInput.benutzer
                    AND   webmenu.sprcd    = INTEGER(ttInput.sprcd) :
                    
                    CASE webmenu.stufe2:
                        WHEN 0 THEN
                            DO:
                                IF iAktStufe = 1 THEN
                                DO:
                                    lcJsonString = lcJsonString + SUBSTITUTE('&1,', CHR(125)).
                                END.
            
                                IF iAktStufe = 2 THEN
                                DO:
                                    lcJsonString = lcJsonString + SUBSTITUTE('&1&1,', CHR(125)).
                                END.
                                IF NOT lFirst THEN 
                                DO:
                                    lcJsonString = lcJsonString + ','.
                                    lFirst = FALSE.
                                END. 
                                IF webmenu.menulink = '' THEN lcJsonString = lcJsonString + SUBSTITUTE('&1: &2', QUOTER(webmenu.Menutext), CHR(123)                ).
                                ELSE                          lcJsonString = lcJsonString + SUBSTITUTE('&1: &2', QUOTER(webmenu.menutext), QUOTER(webmenu.menulink)).
                                iAktStufe = webmenu.stufe2.
                            END.
        
                        WHEN 1 THEN
                            DO:
                                IF iAktStufe = 1 THEN lcJsonString = lcJsonString + ','. 
                                IF iAktStufe = 2 THEN
                                DO:
                                    lcJsonString = lcJsonString + SUBSTITUTE('&1,', CHR(125)).
                                END.
                                IF webmenu.menulink = '' THEN lcJsonString = lcJsonString + SUBSTITUTE('&1: &2', QUOTER(webmenu.menutext), CHR(123)).
                                ELSE                          lcJsonString = lcJsonString + SUBSTITUTE('&1: &2', QUOTER(webmenu.menutext), QUOTER(webmenu.menulink)).
                                iAktStufe = webmenu.stufe1.
                            END.
        
                        WHEN 2 THEN
                            DO:
                                IF iAktStufe = 2 THEN  lcJsonString = lcJsonString + ','.
                                IF webmenu.menulink = '' THEN lcJsonString = lcJsonString + SUBSTITUTE('&1: &2', QUOTER(webmenu.menutext), CHR(123)                ).
                                ELSE                          lcJsonString = lcJsonString + SUBSTITUTE('&1: &2', QUOTER(webmenu.menutext), QUOTER(webmenu.menulink)).
                                iAktStufe = webmenu.stufe1.
                            END.
        
                    END CASE.
    
                END.
                
                IF iAktStufe = 2 THEN lcJsonString = lcJsonString + SUBSTITUTE('&1&1', CHR(125)).
                IF iAktStufe = 1 THEN lcJsonString = lcJsonString + SUBSTITUTE('&1'  , CHR(125)).
                lcJsonString = lcJsonString + CHR(125).
                LEAVE.
            END.
            
            IF ttInput.action BEGINS 'stufe' THEN 
            DO:
                CASE ttInput.action:
                    WHEN 'stufe0'  THEN 
                        iAktStufe = 0.
                    WHEN 'stufe1' THEN 
                        iAktStufe = 1.
                    WHEN 'stufe2' THEN  
                        iAktStufe = 2.
                END CASE.
                
                cKomma = ''.
                
                IF ttInput.params <> '' THEN 
                DO:
                    FIND FIRST webmenu NO-LOCK USE-INDEX webmenu-k1
                        WHERE webmenu.mandant  = ttInput.mandant
                        AND   webmenu.benutzer = ttInput.benutzer
                        AND   webmenu.sprcd    = INTEGER(ttInput.sprcd)
                        /*                        AND   webmenu.stufe    = iAktStufe - 1*/
                        AND   webmenu.menutext = ttInput.params NO-ERROR.
                    ii = webmenu.stufe1.
                END.

                FOR EACH webmenu NO-LOCK USE-INDEX webmenu-k1
                    WHERE webmenu.mandant  = ttInput.mandant
                    AND   webmenu.benutzer = ttInput.benutzer
                    AND   webmenu.sprcd    = INTEGER(ttInput.sprcd)
                    AND   webmenu.stufe1   > ii:

                    IF webmenu.stufe2 < iAktStufe THEN LEAVE.
                    IF webmenu.stufe2 > iAktStufe THEN NEXT.

                    IF webmenu.menulink = '' THEN lcJsonString = lcJsonString + SUBSTITUTE('&3&1:&2', QUOTER(webmenu.menutext), QUOTER(STRING(iAktStufe,"9")), cKomma  ).
                    ELSE                          lcJsonString = lcJsonString + SUBSTITUTE('&3&1:&2', QUOTER(webmenu.menutext), QUOTER(webmenu.menulink)     , cKomma ).
                    cKomma = ', '.
                END.
                    
                lcJsonString = lcJsonString + CHR(125).
                LEAVE.
            END.

            FOR EACH webmenu NO-LOCK
                WHERE webmenu.mandant  = ttInput.mandant
                AND   webmenu.benutzer = ttInput.benutzer
                AND   webmenu.sprcd    = INTEGER(ttInput.sprcd)
                AND   INDEX(webmenu.wortindex, ttInput.suchfeld) > 0:
                        
                IF webmenu.menulink = '' THEN lcJsonString = lcJsonString + SUBSTITUTE('&3&1:&2', QUOTER(webmenu.menutext), QUOTER(STRING(iAktStufe,"9")), cKomma  ).
                ELSE                          lcJsonString = lcJsonString + SUBSTITUTE('&3&1:&2', QUOTER(webmenu.menutext), QUOTER(webmenu.menulink)     , cKomma ).
                cKomma = ', '.
            END.
            lcJsonString = lcJsonString + CHR(125).
            LEAVE.
        END.
        
        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).
        oWriter:Write(lcJsonString).
        oWriter:Close().

        MESSAGE "Exiting HandlePost".

        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.
