 
/*------------------------------------------------------------------------
   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.* FROM PROPATH.
USING src.ch.adprime.gemis.WebHandlerUtilities FROM PROPATH.
USING utilities.utilitiesHandler FROM PROPATH.
USING utilities.selectboxHandler FROM PROPATH.
USING OpenEdge.Web.DataObject.Writer.JsonArrayValueWriter FROM PROPATH.

CLASS admin.formprogramImpl INHERITS WebHandler: 

    DEFINE VARIABLE ccompany          AS CHARACTER        NO-UNDO.
    DEFINE VARIABLE cuser_name        AS CHARACTER        NO-UNDO.
    DEFINE VARIABLE cAnzeigeName      AS CHARACTER        NO-UNDO.
    DEFINE VARIABLE ilanguage_id      AS INTEGER          NO-UNDO.
    DEFINE VARIABLE cSessionToken     AS CHARACTER        NO-UNDO.
    
    DEFINE VARIABLE outilitiesHandler AS utilitiesHandler NO-UNDO.
    DEFINE VARIABLE oSelectboxHandler AS selectboxHandler 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'
        FIELD lselected    AS LOGICAL   SERIALIZE-NAME 'selected'
        .
    DEFINE VARIABLE htcompanies AS HANDLE NO-UNDO.
        
    DEFINE TEMP-TABLE tusers
        FIELD user_name    AS CHARACTER 
        FIELD display_name AS CHARACTER
        .
    DEFINE VARIABLE htusers AS HANDLE NO-UNDO.
    
    DEFINE TEMP-TABLE tlanguages
        FIELD language_id   AS INTEGER 
        FIELD language_name AS CHAR
        .
    DEFINE VARIABLE htlanguages AS HANDLE NO-UNDO.
    
    
    DEFINE TEMP-TABLE tSellanguages
        FIELD language_id   AS INTEGER   SERIALIZE-NAME 'language_id'
        FIELD language_name AS CHARACTER 
        .
    /*    DEFINE DATASET DSselectOptions                            */
    /*        SERIALIZE-NAME 'selectOptyions'                       */
    /*        FOR tSelCompanies, tSelLanguages, tSelRole, tSelStore.*/
    /*    DEFINE VARIABLE hDSselectOptions AS HANDLE.               */
    
    /*------------------------------------------------------------------------------
     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 cDefautlCharSet     AS CHARACTER         NO-UNDO INIT 'UTF-8'.
        DEFINE VARIABLE oParser             AS ObjectModelParser NO-UNDO.           
        DEFINE VARIABLE cCorralationID      AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cContent-Type       AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE lcJsonInhalt        AS LONGCHAR          NO-UNDO.
        DEFINE VARIABLE ii                  AS INTEGER           NO-UNDO.
        DEFINE VARIABLE i1                  AS INTEGER           NO-UNDO.
        
        DEFINE VARIABLE oJsonResponse       AS JsonObject        NO-UNDO.
        DEFINE VARIABLE oJsonCompanies      AS JsonArray         NO-UNDO.
        DEFINE VARIABLE oJsonUsers          AS JsonArray         NO-UNDO.
        DEFINE VARIABLE oJsonLanguages      AS JsonArray         NO-UNDO.
        DEFINE VARIABLE oJsonBox            AS JsonArray         NO-UNDO.
        DEFINE VARIABLE oJsonSelectedFields AS JsonArray         NO-UNDO.
        DEFINE VARIABLE oJsonData           AS JsonObject        NO-UNDO.
        DEFINE VARIABLE oJsonIdent          AS JsonObject        NO-UNDO.
        DEFINE VARIABLE oJsonSelectBoxes    AS JsonObject        NO-UNDO.
        DEFINE VARIABLE oJsonRecords        AS JsonArray         NO-UNDO.
        
        DEFINE VARIABLE ccompany            AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cuser_name          AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE ilanguage_id        AS INTEGER           NO-UNDO.
        DEFINE VARIABLE cFunction           AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cForm_Id            AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE lRetVal             AS LOGICAL           NO-UNDO.
        DEFINE VARIABLE httBuffer           AS HANDLE            NO-UNDO.
        DEFINE VARIABLE cFiles              AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cFile               AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cFileName           AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cFieldName          AS CHARACTER         NO-UNDO.
        
        DEFINE VARIABLE cQueryString        AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cQueryPairs         AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cQueryKey           AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cQueryValue         AS CHARACTER         NO-UNDO.
        
        DEFINE BUFFER bwebmenu FOR webmenu.
        
        outilitiesHandler = NEW utilitiesHandler().
        oselectboxHandler = NEW selectboxHandler().
        
        cFunction = poRequest:URI:GetQueryValue("function") NO-ERROR.
        
        outilitiesHandler:getHeaderData(
            INPUT  poRequest    ,
            OUTPUT ccompany     ,
            OUTPUT cuser_name   ,
            OUTPUT cAnzeigeName ,
            OUTPUT ilanguage_id ,
            OUTPUT cSessionToken
            ).

        oJsonIdent = NEW JsonObject().
        oJsonIdent:ADD('company'     , ccompany     ).
        oJsonIdent:ADD('user_name'   , cuser_name   ).
        oJsonIdent:ADD('language_id' , iLanguage_id ).
        oJsonIdent:ADD('sessionToken', cSessionToken).
        oJsonIdent:ADD('function'    , cFunction    ).
        
        cQueryString = STRING(poRequest:GetContextValue("QUERY_STRING"))    NO-ERROR.
        DO ii = 1 TO NUM-ENTRIES(cQueryString, '&'):
            cQueryPairs = ENTRY(ii, cQueryString, '&').
            cQueryKey   = ENTRY(1 , cQueryPairs , '=').
            cQueryValue = ENTRY(2 , cQueryPairs , '=').
            IF oJsonIdent:Has(cQueryKey) THEN oJsonIdent:SET(cQueryKey, cQueryValue) NO-ERROR.
            ELSE                              oJsonIdent:ADD(cQueryKey, cQueryValue) NO-ERROR.
        END.
        cFunction = oJsonIdent:GetCharacter('function').
        
        // MESSAGE 'Inhalt von Header und Query ' STRING(oJsonIdent:GetJsonText()).
        
        lRetVal = FALSE.
        DO WHILE cFunction = 'init':
            EMPTY TEMP-TABLE tcompanies.
            FOR EACH companies NO-LOCK WHERE companies.active:
                CREATE  tcompanies.
                BUFFER-COPY companies TO tcompanies.
                ASSIGN 
                    tcompanies.lselected = (IF companies.company = ccompany THEN TRUE ELSE FALSE).
            END.
            
            EMPTY TEMP-TABLE tusers.
            FOR EACH users NO-LOCK WHERE users.active:
                CREATE  tusers.
                BUFFER-COPY users TO tusers.
            END.
            
            EMPTY TEMP-TABLE tlanguages.
            FOR EACH languages NO-LOCK:
                CREATE  tlanguages.
                BUFFER-COPY languages TO tlanguages.
            END.
            
            oJsonResponse   = NEW JsonObject().
            oJsonCompanies  = NEW JsonArray().
            oJsonUsers      = NEW JsonArray().
            oJsonLanguages  = NEW JsonArray().
            
            htcompanies = TEMP-TABLE tcompanies:DEFAULT-BUFFER-HANDLE.
            htusers     = TEMP-TABLE tusers    :DEFAULT-BUFFER-HANDLE.
            htlanguages = TEMP-TABLE tlanguages:DEFAULT-BUFFER-HANDLE.
            
            htcompanies:WRITE-JSON('JsonArray', oJsonCompanies ).
            htusers    :WRITE-JSON('JsonArray', oJsonUsers     ).
            htlanguages:WRITE-JSON('JsonArray', oJsonLanguages ).
            
            oJsonResponse:ADD('companies', oJsonCompanies).
            oJsonResponse:ADD('users'    , oJsonUsers    ).
            oJsonResponse:ADD('languages', oJsonLanguages).
            
            // oJsonResponse:WriteFile('C:\TEMP\init.json', TRUE).
            oJsonResponse:WRITE(lcJsonInhalt, FALSE).
            
            lRetVal = TRUE.
            LEAVE.  
        END.

        DO WHILE cFunction = 'loadconfig':
            FIND htmldokumente NO-LOCK 
                WHERE htmldokumente.company     =         oJsonIdent:GetCharacter('company')
                AND   htmldokumente.user_name   =         oJsonIdent:GetCharacter('user_name')
                AND   htmldokumente.form_id     =         oJsonIdent:GetCharacter('form_id')
                AND   htmldokumente.language_id = INTEGER(oJsonIdent:GetCharacter('language_id')) NO-ERROR.
            IF NOT AVAILABLE htmldokumente THEN LEAVE.
MESSAGE 'gefunden'.
            cFiles = htmldokumente.tables.

            oJsonSelectBoxes = NEW JsonObject().
            FOR EACH selectboxfields NO-LOCK
                WHERE selectboxfields.company = htmldokumente.company
                AND   selectboxfields.program = htmldokumente.form_id:
                MESSAGE selectboxfields.boxfields.
                DO ii = 1 TO NUM-ENTRIES(selectboxfields.boxfields, ';'):
                    cFieldName = ENTRY(ii, selectboxfields.boxfields, ';').
                    cFileName  = ENTRY(1, cFieldName, '_').

                    IF cFieldName MATCHES('*company*') THEN 
                    DO:
                        oJsonBox  = NEW JsonArray().
                        httBuffer = oSelectboxHandler:getSelboxCompany(cFileName).
                        httBuffer:WRITE-JSON ('JsonArray', oJsonBox).
                        oJsonSelectBoxes:ADD(cFieldName, oJsonBox).
                        NEXT.
                    END.
            
                    IF cFieldName MATCHES('*language_id*') THEN 
                    DO:
                        oJsonBox  = NEW JsonArray().
                        httBuffer = oSelectboxHandler:getSelboxLanguage(cFileName).
                        httBuffer:WRITE-JSON ('JsonArray', oJsonBox).
                        oJsonSelectBoxes:ADD(cFieldName, oJsonBox).
                        NEXT.
                    END.
                    
                /*                    httBuffer = oSelectboxHandler:getSelboxLaguages(). */
                /*                    oJsonCompanies = NEW JsonArray().                  */
                /*                    httBuffer:WRITE-JSON ('JsonArray', oJsonLanguages).*/
                END.
            END.
            
            DO ii = 1 TO NUM-ENTRIES(cFiles, ';'):
                httBuffer = outilitiesHandler:getData(
                    INPUT  oJsonIdent            ,
                    INPUT  ENTRY(ii, cFiles, ';') ,
                    INPUT  ''        
                    ).
                oJsonRecords = NEW JsonArray().
                httBuffer:WRITE-JSON('JsonArray', oJsonRecords).
            END.

            oJsonResponse = NEW JsonObject().
            oJsonResponse:ADD('formTitle', htmldokumente.formtitle).
            oJsonResponse:ADD('formId'   , htmldokumente.form_id  ).

            oParser   = NEW ObjectModelParser().
            oJsonData = CAST(oParser:Parse(htmldokumente.htmltext), JsonObject).
            oJsonSelectedFields = NEW JsonArray().
            oJsonSelectedFields = oJsonData:GetJsonArray('selectedFields').
            
            oJsonResponse:ADD('selectedFields'  , oJsonSelectedFields).
            oJsonResponse:ADD('selectOptionsFix', oJsonSelectBoxes).

            oJsonResponse:ADD('records', oJsonRecords).            
            
            // oJsonResponse:WriteFile('C:\TEMP\loadconfig.json', TRUE).
            oJsonResponse:WRITE(lcJsonInhalt, FALSE).
            
            lRetVal = TRUE.
            LEAVE.
        END.
        
        DO WHILE cFunction = "getformconfig":
            FIND htmldokumente NO-LOCK 
                WHERE htmldokumente.company     = oJsonIdent:GetCharacter('company')
                AND   htmldokumente.user_name   = oJsonIdent:GetCharacter('user')
                AND   htmldokumente.form_id     = oJsonIdent:GetCharacter('form_id')
                AND   htmldokumente.language_id = INTEGER(oJsonIdent:GetCharacter('language_id')) NO-ERROR.
            IF NOT AVAILABLE htmldokumente THEN LEAVE.
            
            oJsonResponse = NEW JsonObject().
            oJsonResponse:ADD('formTitle'       , htmldokumente.formtitle) NO-ERROR.
            oJsonResponse:ADD('formId'          , htmldokumente.form_id  ) NO-ERROR.
            oJsonResponse:ADD('targetCompany'   , oJsonIdent:GetCharacter('company')   ) NO-ERROR.
            oJsonResponse:ADD('targetUser'      , oJsonIdent:GetCharacter('user')       ) NO-ERROR.
            oJsonResponse:ADD('targetLanguageId', oJsonIdent:GetCharacter('language_id')) NO-ERROR.
             
            oParser   = NEW ObjectModelParser().
            oJsonData = CAST(oParser:Parse(htmldokumente.htmltext), JsonObject).
            oJsonSelectedFields = NEW JsonArray().
            oJsonSelectedFields = oJsonData:GetJsonArray('selectedFields').
            oJsonResponse:ADD('selectedFields'  , oJsonSelectedFields).

            // oJsonResponse:WriteFile('C:\TEMP\getformconfigresponse.json', TRUE).
            
            oJsonResponse:WRITE(lcJsonInhalt, FALSE ).

            lRetVal = TRUE.
            LEAVE.
        END.            
            

        IF NOT lRetVal THEN 
        DO:
            oJsonResponse = NEW JsonObject().
            oJsonResponse:ADD('success', FALSE).
            oJsonResponse:WRITE(lcJsonInhalt, FALSE).
        END.
                
        cContent-Type   = "application/json".
        cDefautlCharSet = 'utf-8'.

        oBody = NEW STRING(lcJsonInhalt).
        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 oJsonWebmenu  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 ccompany      AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cuser_name    AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE ilanguage_id  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).
            
        ccompany     = oJsonData:GetCharacter('company' ).
        cuser_name   = oJsonData:GetCharacter('user_name' ).
        ilanguage_id = INTEGER(oJsonData:GetCharacter('language_id')).

        // MESSAGE 'HANDLE POST ' ccompany '-' cuser_name '-' ilanguage_id.
        
    END METHOD.

    /*------------------------------------------------------------------------------
     Purpose:
     Notes:
    ------------------------------------------------------------------------------*/
    DESTRUCTOR PUBLIC formprogramImpl ( ):

    END DESTRUCTOR.

END CLASS.