/*------------------------------------------------------------------------
   File        : anmeldungImpl
   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 anmeldungImpl INHERITS WebHandler:
    
    DEFINE VARIABLE cAktion           AS CHARACTER        NO-UNDO.
    
    DEFINE VARIABLE outilitiesHandler AS utilitiesHandler.
    
    DEFINE TEMP-TABLE ttMandant SERIALIZE-NAME 'mandanten'
        FIELD mandant     AS CHARACTER 
        FIELD bezeichnung AS CHARACTER
        FIELD choose      AS CHARACTER 
        .
    { 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 lRetVal      AS LOGICAL           NO-UNDO.
        
        DEFINE VARIABLE hbttMandant  AS HANDLE            NO-UNDO.
        DEFINE VARIABLE cGetHtml     AS CHARACTER         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 oInput       AS JsonObject        NO-UNDO.

        MESSAGE "Entering HandlePost" VIEW-AS ALERT-BOX.
        
        lRetVal = WebHandlerUtilities:getJsonFromRequest(INPUT poRequest, OUTPUT oJsonData) NO-ERROR.
        oJsonData:WRITE(lcJsonString, FALSE).
        //
        //  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:
            hbttInput    = TEMP-TABLE ttInput   :DEFAULT-BUFFER-HANDLE.
            hbttMandant  = TEMP-TABLE ttMandant :DEFAULT-BUFFER-HANDLE.
            
            MESSAGE 'Inputdaten gelesen mit returncode ' lRetVal.
            IF NOT lRetVal THEN LEAVE.
            
            FIND FIRST ttInput.
             
            IF ttInput.action = 'first' THEN 
            DO:
                EMPTY TEMP-TABLE ttMandant.
                FOR EACH Mandant NO-LOCK
                    WHERE Mandant.Zustand = TRUE:
                    CREATE  ttMandant.
                    ASSIGN 
                        ttMandant.Mandant     = Mandant.Firma
                        ttMandant.Bezeichnung = Mandant.Name.
                /*                    ttMandant.choose      = (IF Mandant = '2000' THEN '1' ELSE '0').*/
                END.
                
                oJsonData = NEW JsonObject().
                hbttMandant:WRITE-JSON('JsonObject', oJsonData).
                oJsonData:WRITE(lcJsonString, TRUE).
            END.
            
            IF ttInput.action = 'anmelden' THEN 
            DO:
                oJsonData:WRITE(lcJsonString, FALSE).
                
                FIND FIRST ttInput.
                
                IF INDEX(ttInput.benutzer, '_Admin') > 0 THEN 
                DO:
                    ttInput.benutzer = REPLACE(ttInput.benutzer, '_Admin', '').
                    lAdmin = TRUE.
                END.

                IF NOT lAdmin THEN 
                DO:
                    FIND Benutzer NO-LOCK 
                        WHERE Benutzer.Firma    = ttInput.mandant
                        AND   Benutzer.Benutzer = ttInput.benutzer
                        AND   Benutzer.Kennwort = ttInput.password NO-ERROR.
                END.
                ELSE 
                DO:
                    FIND FIRST Benutzer NO-LOCK 
                        WHERE Benutzer.Firma    = ttInput.mandant
                        AND   Benutzer.Benutzer = ttInput.benutzer NO-ERROR.
                END.
                IF NOT AVAILABLE (Benutzer) THEN 
                DO:
                    outilitiesHandler:getErrorMessage(INPUT '1990', INPUT 1, INPUT '', OUTPUT lcJsonString).
                    
                    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().
                    RETURN -1.
                END.
                
                ttInput.action = 'return'.
                ttInput.link   = 'gemis_start.html'.
                ttInput.sprcd  = STRING(Benutzer.Sprcd).
                    
                lcJsonString = ''.
                DO ii = 1 TO hbttInput:NUM-FIELDS:
                    lcJsonString = lcJsonString
                        + (IF lcJsonString = '' THEN '' ELSE ', ')
                        + QUOTER(hbttInput:BUFFER-FIELD(ii):NAME) 
                        + ': '
                        + QUOTER(hbttInput:BUFFER-FIELD(ii):BUFFER-VALUE).
                END.
                lcJsonString = CHR(123) + lcJsonString + chr(125).
                MESSAGE 'Return-Daten nach füllen aus ttInput ' STRING(lcJsonString).
            END.
            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) VIEW-AS ALERT-BOX.
        oWriter:Write(lcJsonString).
        oWriter:Close().

        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.
