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

CLASS formdesigner.initialDataImpl INHERITS WebHandler: 

    DEFINE VARIABLE outilitiesHandler AS utilitiesHandler NO-UNDO.
    
    DEFINE TEMP-TABLE tcompanies
        SERIALIZE-NAME 'companies'
        FIELD company      AS CHARACTER SERIALIZE-NAME 'company' 
        FIELD company_name AS CHARACTER SERIALIZE-NAME 'company_name'
        .
    DEFINE VARIABLE htcompanies AS HANDLE NO-UNDO.
    
    DEFINE TEMP-TABLE tusers
        SERIALIZE-NAME 'users'
        FIELD user_name    AS CHARACTER SERIALIZE-NAME 'user-name' 
        FIELD display_name AS CHARACTER SERIALIZE-NAME 'display_name'
        .
    DEFINE VARIABLE htusers AS HANDLE NO-UNDO.
        
    DEFINE TEMP-TABLE tlanguages
        SERIALIZE-NAME 'languages'
        FIELD language_id   AS INTEGER   SERIALIZE-NAME 'id' 
        FIELD language_name AS CHARACTER SERIALIZE-NAME 'text'
        .
    DEFINE VARIABLE htlanguages 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 oJsonArray      AS JsonArray         NO-UNDO.
        DEFINE VARIABLE oJsonMandant    AS JsonObject        NO-UNDO.
        DEFINE VARIABLE oJsonBenutzer   AS JsonObject        NO-UNDO.
        DEFINE VARIABLE oJsonlanguages  AS JsonObject        NO-UNDO.
        DEFINE VARIABLE cDefautlCharSet AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cContentType    AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cCorralationId  AS CHARACTER         NO-UNDO.
           
        DEFINE VARIABLE lcJsonInhalt    AS LONGCHAR          NO-UNDO.
        DEFINE VARIABLE ccompany        AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE ii              AS INTEGER           NO-UNDO.
        
        MESSAGE 'start get'.
        
        EMPTY TEMP-TABLE tcompanies .
        EMPTY TEMP-TABLE tusers.
        EMPTY TEMP-TABLE tlanguages .
        
        ii = 0.
        FOR EACH companies NO-LOCK 
            WHERE companies.active = TRUE:
            ii = ii + 1.
            
            CREATE  tcompanies.
            BUFFER-COPY companies TO tcompanies.
            IF ii = 1 THEN ccompany = companies.company.
        END.
        
        CREATE  tusers.
        ASSIGN 
            tusers.user_name     = 'Admin'
            tusers.display_name  = 'alle Benutzer (Default/admin)'.
        FOR EACH users NO-LOCK
            WHERE users.company = ccompany:
                
            IF users.user_name = 'Admin' THEN NEXT.
                
            CREATE  tusers.
            BUFFER-COPY users TO tusers.
        END.
        
        FOR EACH languages NO-LOCK:
            CREATE  tlanguages.
            BUFFER-COPY LANGUAGES TO tlanguages.
        END.
        
        cContentType    = 'application/json'.
        cDefautlCharSet = 'utf-8'.

        oJsonMandant   = NEW JsonObject().
        oJsonBenutzer  = NEW JsonObject().
        oJsonlanguages = NEW JsonObject().
        
        htcompanies = TEMP-TABLE tcompanies:DEFAULT-BUFFER-HANDLE.
        htusers     = TEMP-TABLE tusers    :DEFAULT-BUFFER-HANDLE.
        htlanguages = TEMP-TABLE tlanguages:DEFAULT-BUFFER-HANDLE.

        outilitiesHandler = NEW utilitiesHandler ().
        htcompanies :WRITE-JSON("JsonObject", oJsonMandant ).
        htusers:WRITE-JSON("JsonObject", oJsonBenutzer).
        htlanguages :WRITE-JSON("JsonObject", oJsonlanguages ).

        lcJsonInhalt
        = CHR(123)  /* "{" */
            + outilitiesHandler:trimStructure(INPUT oJsonMandant)
            + ', '
            + outilitiesHandler:trimStructure(INPUT oJsonBenutzer)
            + ', '
            + outilitiesHandler:trimStructure(INPUT oJsonlanguages)
            + CHR(125).   /* "}" */

        oBody = NEW String(lcJsonInhalt).
        MESSAGE STRING(oBody).
        
        WebHandlerUtilities:createHttpResponse(INPUT INTEGER(StatusCodeEnum:OK), INPUT oBody, INPUT cContentType, INPUT cDefautlCharSet, INPUT cCorralationID, OUTPUT oResponse).
        
        oWriter = NEW WebResponseWriter(oResponse).
        oWriter:Open(). 
        oWriter:Close(). 
            
        RETURN 0.
        
        CATCH e AS Progress.Lang.Error:

        END CATCH.

        FINALLY:
            DELETE OBJECT oJsonMandant     .
            DELETE OBJECT oJsonBenutzer    .
            DELETE OBJECT oJsonlanguages     .
            DELETE OBJECT outilitiesHandler.
        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.

END CLASS.