 
/*------------------------------------------------------------------------
   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 Progress.Json.ObjectModel.ObjectModelParser FROM PROPATH.
USING src.ch.adprime.gemis.WebHandlerUtilities FROM PROPATH.
USING utilities.utilitiesHandler FROM PROPATH.

CLASS admin.usersImpl INHERITS WebHandler: 

    DEFINE VARIABLE outilitiesHandler AS utilitiesHandler NO-UNDO.
    
    DEFINE TEMP-TABLE tusers
        SERIALIZE-NAME 'users'
        FIELD id          AS INT64     SERIALIZE-NAME 'id'
        FIELD username    AS CHARACTER SERIALIZE-NAME 'username'
        FIELD password    AS CHARACTER SERIALIZE-NAME 'password'
        FIELD firstname   AS CHARACTER SERIALIZE-NAME 'firstname'
        FIELD lastname    AS CHARACTER SERIALIZE-NAME 'lastname'
        FIELD email       AS CHARACTER SERIALIZE-NAME 'email'
        FIELD displayname AS CHARACTER SERIALIZE-NAME 'displayname'
        FIELD role        AS CHARACTER SERIALIZE-NAME 'role'
        FIELD language_id AS INTEGER   SERIALIZE-NAME 'language_id'
        FIELD active      AS LOGICAL   SERIALIZE-NAME 'active'
        FIELD created_at  AS CHARACTER SERIALIZE-NAME 'created_at'
        FIELD updated_at  AS CHARACTER SERIALIZE-NAME 'updated_at'
        FIELD iStatus     AS INTEGER   SERIALIZE-HIDDEN
        .
    DEFINE VARIABLE htusers AS HANDLE NO-UNDO.

    DEFINE TEMP-TABLE tusersUpdate
        SERIALIZE-NAME 'users'
        FIELD id          AS INT64     SERIALIZE-NAME 'id'
        FIELD username    AS CHARACTER SERIALIZE-NAME 'username'
        FIELD password    AS CHARACTER SERIALIZE-NAME 'password'
        FIELD firstname   AS CHARACTER SERIALIZE-NAME 'firstname'
        FIELD lastname    AS CHARACTER SERIALIZE-NAME 'lastname'
        FIELD email       AS CHARACTER SERIALIZE-NAME 'email'
        FIELD displayname AS CHARACTER SERIALIZE-NAME 'displayname'
        FIELD role        AS CHARACTER SERIALIZE-NAME 'role'
        FIELD language_id AS INTEGER   SERIALIZE-NAME 'language_id'
        FIELD active      AS LOGICAL   SERIALIZE-NAME 'active'
        FIELD created_at  AS CHARACTER SERIALIZE-NAME 'created_at'
        FIELD updated_at  AS CHARACTER SERIALIZE-NAME 'updated_at'
        .
    DEFINE VARIABLE htusersUpdate AS HANDLE NO-UNDO.

    /*------------------------------------------------------------------------------
     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 oBody           AS String                       NO-UNDO.
        DEFINE VARIABLE oJsonResponse   AS JsonObject                   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 oParser         AS ObjectModelParser            NO-UNDO.           
        DEFINE VARIABLE oHeader         AS OpenEdge.Net.HTTP.HttpHeader NO-UNDO.
        DEFINE VARIABLE lcJsonString    AS LONGCHAR                     NO-UNDO.

        DEFINE VARIABLE cMandant        AS CHARACTER                    NO-UNDO.
        DEFINE VARIABLE cBenutzer       AS CHARACTER                    NO-UNDO.
        DEFINE VARIABLE cAnzeigeName    AS CHARACTER                    NO-UNDO.
        DEFINE VARIABLE iSprcd          AS INTEGER                      NO-UNDO.
        DEFINE VARIABLE cSessionToken   AS CHARACTER                    NO-UNDO.
        DEFINE VARIABLE lRetVal         AS LOGICAL                      NO-UNDO INIT FALSE.        

        DEFINE VARIABLE cFunction       AS CHARACTER                    NO-UNDO.        
        DEFINE VARIABLE cusername       AS CHARACTER                    NO-UNDO.
        DEFINE VARIABLE language_id     AS INTEGER                      NO-UNDO.
        DEFINE VARIABLE oJsonUsers      AS JsonObject                   NO-UNDO.
        DEFINE VARIABLE lOk             AS LOGICAL                      NO-UNDO.
        DEFINE VARIABLE ii              AS INTEGER                      NO-UNDO.
        DEFINE VARIABLE dDateTime       AS DATETIME                     NO-UNDO.
        DEFINE VARIABLE cDateTime       AS CHARACTER                    NO-UNDO.
        
        oHeader      = poRequest:GetHeader('X-MANDANT-ID').
        cMandant     = oHeader:VALUE.
        
        oHeader      = poRequest:GetHeader('X-USERNAME').
        cBenutzer    = oHeader:VALUE.
        
        oHeader      = poRequest:GetHeader('X-USERDISPLAY').
        cAnzeigeName = oHeader:VALUE.
        
        oHeader = poRequest:GetHeader('X-LANGUAGE-ID').
        iSprcd  = INTEGER(oHeader:VALUE).
        
        oHeader = poRequest:GetHeader('X-SESSION-TOKEN').
        cSessionToken = oHeader:VALUE.
        
        outilitiesHandler = NEW utilitiesHandler().
        
        DEFINE BUFFER busers FOR users.

        cFunction = poRequest:URI:GetQueryValue("function") NO-ERROR.         
        cusername = poRequest:URI:GetQueryValue("username") NO-ERROR.         
        
        MESSAGE 'Function = ' cFunction 'username = ' cusername.

        EMPTY TEMP-TABLE tusers.
        EMPTY TEMP-TABLE tusersupdate.
         
        DO WHILE cFunction = 'init':
            FOR EACH users NO-LOCK:
                CREATE  tusers.
                BUFFER-COPY users
                    EXCEPT  users.created_at users.updated_at
                    TO      tusers
                    ASSIGN
                    tusers.created_at = outilitiesHandler:getCharDateTime( users.created_at )
                    tusers.updated_at = outilitiesHandler:getCharDateTime( users.updated_at ).
            END.
            lRetVal = TRUE.
            LEAVE.
        END.
                
/*        EMPTY TEMP-TABLE tusers.                                                                                                          */
/*        FIND FIRST users NO-LOCK                                                                                                          */
/*            WHERE users.username   = cusername                                                                                            */
/*            AND   users.language_id    = iSprcd NO-ERROR.                                                                                 */
/*        IF NOT AVAILABLE users THEN                                                                                                       */
/*        DO:                                                                                                                               */
/*            FIND FIRST users NO-LOCK                                                                                                      */
/*                WHERE users.mandant  = cMandant                                                                                           */
/*                AND   users.username = 'admin'                                                                                            */
/*                AND   users.language_id    = iSprcd NO-ERROR.                                                                             */
/*            IF AVAILABLE users THEN                                                                                                       */
/*            DO:                                                                                                                           */
/*                FOR EACH  users NO-LOCK                                                                                                   */
/*                    WHERE users.mandant  = cMandant                                                                                       */
/*                    AND   users.username = 'admin'                                                                                        */
/*                    AND   users.language_id    = iSprcd:                                                                                  */
/*                    CREATE busers.                                                                                                        */
/*                    BUFFER-COPY users                                                                                                     */
/*                        EXCEPT      users                                                                                                 */
/*                        TO          busers                                                                                                */
/*                        ASSIGN                                                                                                            */
/*                        busers.username = cusername                                                                                       */
/*                        busers.erstellt_am = NOW                                                                                          */
/*                        .                                                                                                                 */
/*                END.                                                                                                                      */
/*            END.                                                                                                                          */
/*        END.                                                                                                                              */
/*        FOR EACH users NO-LOCK                                                                                                            */
/*            WHERE users.mandant  = cMandant                                                                                               */
/*            AND   users.username = cusername                                                                                              */
/*            AND   users.language_id    = iSprcd:                                                                                          */
/*                                                                                                                                          */
/*            CREATE  tusers.                                                                                                               */
/*            BUFFER-COPY users                                                                                                             */
/*                EXCEPT erstellt_am mutiert_am                                                                                             */
/*                TO     tusers.                                                                                                            */
/*            ASSIGN                                                                                                                        */
/*                tusers.cId = SUBSTITUTE('&1&2&3', STRING(users.stufe1,'9999'), STRING(users.stufe2,'9999'), STRING(users.stufe3,'9999') ).*/
/*                                                                                                                                          */
/*            dDateTime = (IF users.erstellt_am <> ? THEN users.erstellt_am ELSE NOW).                                                      */
/*            cDateTime = STRING(dDateTime,'99-99-9999THH:MM:SS').                                                                          */
/*            tusers.erstellt_am = STRING(YEAR(dDateTime),'9999')                                                                           */
/*                + '-'                                                                                                                     */
/*                + STRING(MONTH(dDateTime),'99')                                                                                           */
/*                + '-'                                                                                                                     */
/*                + STRING(DAY(dDateTime),'99')                                                                                             */
/*                + SUBSTRING(cDateTime,11).                                                                                                */
/*            dDateTime = (IF users.mutiert_am <> ? THEN users.mutiert_am ELSE NOW).                                                        */
/*            cDateTime = STRING(dDateTime,'99-99-9999THH:MM:SS').                                                                          */
/*            tusers.mutiert_am = STRING(YEAR(dDateTime),'9999')                                                                            */
/*                + '-'                                                                                                                     */
/*                + STRING(MONTH(dDateTime),'99')                                                                                           */
/*                + '-'                                                                                                                     */
/*                + STRING(DAY(dDateTime),'99')                                                                                             */
/*                + SUBSTRING(cDateTime,11).                                                                                                */
/*        END.                                                                                                                              */
        
        htusers = TEMP-TABLE tusers:DEFAULT-BUFFER-HANDLE.
                 
        cContent-Type   = "application/json".
        cDefautlCharSet = 'utf-8'. 
        oJsonResponse   = NEW JsonObject().
        oParser         = NEW ObjectModelParser().
        
        oJsonUsers = NEW JsonObject().
        htusers:WRITE-JSON('JsonObject', oJsonUsers).
        
        oBody = NEW STRING(oJsonUsers:GetJsonText()).
        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.

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

    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 oJsonUsers    AS JsonArray         NO-UNDO.
        DEFINE VARIABLE oJsonMessage  AS JsonObject        NO-UNDO.
        DEFINE VARIABLE oJsonResponse AS JsonObject        NO-UNDO.
        DEFINE VARIABLE oFelder       AS JsonArray         NO-UNDO.
        DEFINE VARIABLE oFeld         AS JsonObject        NO-UNDO.
        DEFINE VARIABLE oParser       AS ObjectModelParser NO-UNDO.           
        
        DEFINE VARIABLE lRetVal       AS LOGICAL           NO-UNDO.
        DEFINE VARIABLE lcJsonString  AS LONGCHAR          NO-UNDO.
        DEFINE VARIABLE cJSON         AS LONGCHAR          NO-UNDO.
        DEFINE VARIABLE i1            AS INTEGER           NO-UNDO.
        
        DEFINE VARIABLE cTabellen     AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cMandant      AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cusername     AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE iSprcd        AS INTEGER           NO-UNDO.
        DEFINE VARIABLE cMaskenId     AS CHARACTER         NO-UNDO.
        
        lRetVal = WebHandlerUtilities:getJsonFromRequest(INPUT poRequest, OUTPUT oJsonData) NO-ERROR.

        oJsonData:WRITE(lcJsonString, FALSE).
        MESSAGE 'ganzer Eingangsstring = ' STRING(lcJsonString).
            
        cMandant  = oJsonData:GetCharacter('customer_id' ).
        cusername = oJsonData:GetCharacter('user_id' ).
        iSprcd    = INTEGER(oJsonData:GetCharacter('language_id')).

        MESSAGE 'HANDLE POST ' cMandant '-' cusername '-' iSprcd.
/*            
        oParser       = NEW ObjectModelParser().
        oJsonUsers  = NEW JsonArray().
        oJsonResponse = NEW JsonObject().
        lcJsonString  = oJsonData:GetJsonText('menuItems').
        
        oJsonUsers = CAST(oParser:Parse(lcJsonString), JsonArray) NO-ERROR.
        oJsonResponse:ADD('menuItems', oJsonUsers).

        htusersUpdate = TEMP-TABLE tusersUpdate:DEFAULT-BUFFER-HANDLE.
        htusersUpdate:READ-JSON ('JsonObject', oJsonResponse, 'EMPTY').
        
        /*        oJsonResponse:WRITE(lcJsonString, FALSE).                   */
        /*        htusersUpdate:READ-JSON ('longchar', lcJsonString, 'EMPTY').*/
        
        EMPTY TEMP-TABLE tusers.
        FOR EACH users NO-LOCK
            WHERE users.mandant  = cMandant
            AND   users.username = cusername
            AND   users.language_id    = iSprcd:
                
            CREATE tusers.
            BUFFER-COPY users
                EXCEPT  erstellt_am mutiert_am
                TO tusers
                ASSIGN 
                tusers.istatus = 0.
        END.
        
        FOR EACH tusersUpdate:
            FIND tusers
                WHERE tusers.mandant  = cMandant
                AND   tusers.username = cusername
                AND   tusers.language_id    = isprcd
                AND   tusers.stufe1   = tusersUpdate.stufe1
                AND   tusers.stufe2   = tusersUpdate.stufe2
                AND   tusers.stufe3   = tusersUpdate.stufe3 NO-ERROR.
                
            IF NOT AVAILABLE tusers THEN 
            DO:
                CREATE tusers.
                BUFFER-COPY tusersUpdate
                    EXCEPT  Mandant users sprcd erstellt_am mutiert_am
                    TO      tusers
                    ASSIGN
                    tusers.mandant     = cMandant
                    tusers.username    = cusername
                    tusers.language_id       = iSprcd
                    tusers.iStatus     = 1.
                NEXT.
            END.
            BUFFER-COPY tusersUpdate
                EXCEPT  Mandant users sprcd erstellt_am mutiert_am
                TO      tusers
                ASSIGN 
                tusers.istatus = 2.
        END.
        
        REPEAT TRANSACTION ON ERROR UNDO, LEAVE:
            FOR EACH tusers:
                IF tusers.istatus = 1 THEN 
                DO:
                    CREATE  users.
                    BUFFER-COPY tusers
                        EXCEPT  erstellt_am mutiert_am istatus
                        TO      users
                        ASSIGN  
                        users.erstellt_am = NOW
                        users.mutiert_am  = ?.
                END.
                FIND users OF tusers.
                IF tusers.istatus = 0 THEN 
                DO:
                    DELETE users.
                    NEXT.
                END.
                ASSIGN 
                    users.menutext   = tusers.menutext
                    users.menulink   = tusers.menulink
                    users.mutiert_am = NOW. 
            END.
            RELEASE users.
            LEAVE.
        END.
        
        OUTPUT TO 'C:\TEMP\users.csv' NO-MAP NO-CONVERT.
        FOR EACH users NO-LOCK:
            EXPORT DELIMITER ';' users.
        END.
        OUTPUT CLOSE.
        
        oResponse             = NEW OpenEdge.Web.WebResponse().
        oResponse:StatusCode  = INTEGER(StatusCodeEnum:OK).
        oResponse:ContentType = "application/json;charset=utf-8".
        oWriter               = NEW WebResponseWriter(oResponse).
                    
        oJsonMessage = NEW JsonObject().
        oJsonMessage:ADD('success', TRUE).
        oJsonMessage:WRITE(lcJsonString, TRUE).
        MESSAGE 'Rückmeldung ' STRING(lcJsonString).
                        
        oWriter:Open().
        oWriter:Write(lcJsonString).
        oWriter:Close().
*/            
        CATCH e AS Progress.Lang.Error:
            
        END CATCH.
            
        FINALLY:
        /*            DELETE OBJECT oJsonConfig. */
        /*            DELETE OBJECT oJsonMessage.*/
        END FINALLY.

    END METHOD.

END CLASS.