 
/*------------------------------------------------------------------------
   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.
USING Progress.Json.ObjectModel.JsonDataType FROM PROPATH.

CLASS admin.formdesignerImpl INHERITS WebHandler: 

    DEFINE VARIABLE oResponse         AS IHttpResponse     NO-UNDO.         
    DEFINE VARIABLE oWriter           AS WebResponseWriter NO-UNDO.
    DEFINE VARIABLE oBody             AS String            NO-UNDO.
    
    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 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 tFileDict
        SERIALIZE-NAME 'fields'
        FIELD cId          AS CHARACTER SERIALIZE-NAME 'id'
        FIELD cName        AS CHARACTER SERIALIZE-NAME 'fieldName'
        FIELD cLabel       AS CHARACTER SERIALIZE-NAME 'label' 
        FIELD cType        AS CHARACTER SERIALIZE-NAME 'type'
        FIELD cDisplayType AS CHARACTER SERIALIZE-NAME 'displayType'
        FIELD cInputMode   AS CHARACTER SERIALIZE-NAME 'inputMode'
        FIELD lshowSpinner AS LOGICAL   SERIALIZE-NAME 'showSpinner'
        FIELD cFormat      AS CHARACTER SERIALIZE-NAME 'format'
        FIELD iMaxLength   AS INTEGER   SERIALIZE-NAME 'maxLength'
        FIELD iDecimals    AS INTEGER   SERIALIZE-NAME 'decimalPlaces'
        FIELD leditable    AS LOGICAL   SERIALIZE-NAME 'editable'
        FIELD lhidden      AS LOGICAL   SERIALIZE-NAME 'hidden'
        FIELD cAusrichtung AS CHARACTER SERIALIZE-NAME 'align'
        .
        
    DEFINE TEMP-TABLE tConfig
        SERIALIZE-NAME 'existingConfigs'
        FIELD cId        AS CHARACTER SERIALIZE-NAME 'id'
        FIELD cFormTitle AS CHARACTER SERIALIZE-NAME 'formTitle'
        FIELD cFormId    AS CHARACTER SERIALIZE-NAME 'formId' 
        FIELD cCreated   AS CHARACTER SERIALIZE-NAME 'created'
        . 

    DEFINE DATASET dsResponse
        SERIALIZE-NAME 'dsResponse'
        FOR tFileDict, tConfig.
    DEFINE VARIABLE hdsResponse AS HANDLE NO-UNDO.
        
    /*------------------------------------------------------------------------------
     Purpose:
     Notes:
    ------------------------------------------------------------------------------*/

    METHOD PUBLIC LOGICAL createTemptable(
        INPUT ipoJsonFields AS JsonArray,
        OUTPUT ophtTable    AS HANDLE   ,
        OUTPUT ophBuffer    AS HANDLE 
        ):
        
        DEFINE VARIABLE lRetVal     AS LOGICAL    NO-UNDO.
        DEFINE VARIABLE iType       AS INTEGER    NO-UNDO.
        DEFINE VARIABLE cType       AS CHARACTER  NO-UNDO.
        DEFINE VARIABLE cField      AS CHARACTER  NO-UNDO.
        DEFINE VARIABLE cFunction   AS CHARACTER  NO-UNDO.
        DEFINE VARIABLE ii          AS INTEGER    NO-UNDO.
        DEFINE VARIABLE i1          AS INTEGER    NO-UNDO.
        DEFINE VARIABLE cFieldNames AS CHARACTER  NO-UNDO EXTENT.
        DEFINE VARIABLE oJsonField  AS JsonObject NO-UNDO.

        CREATE TEMP-TABLE ophtTable.
        DO i1 = 1 TO ipoJsonFields:LENGTH:
            oJsonField  = NEW JsonObject().
            oJsonField  = ipoJsonFields:GetJsonObject(i1).
            IF i1 = 1 THEN 
            DO:
                cFieldNames = oJsonField:getNames() NO-ERROR.
                DO ii = 1 TO EXTENT(cFieldNames):
                    cField = cFieldNames[ii].
                    iType  = oJsonField:GetType(cField).
                    cType  = ''.
                    CASE iType:
                        WHEN JsonDataType:STRING THEN 
                            cType = 'character'.
                        WHEN JsonDataType:NUMBER THEN 
                            cType = 'integer'.
                        WHEN JsonDataType:BOOLEAN THEN 
                            cType = 'logical'.
                        WHEN JsonDataType:OBJECT THEN 
                            MESSAGE 'Json-Feldtype  OBJECT' cField.
                        WHEN JsonDataType:ARRAY THEN 
                            MESSAGE 'Json-Feldtype  ARRAY' cField.
                        WHEN JsonDataType:NULL THEN 
                            cType = 'character'.
                    END CASE.
                    IF cType = '' THEN cType = 'character'.
                    ophtTable:ADD-NEW-FIELD(cField, cType) NO-ERROR.
                END.
                ophtTable:TEMP-TABLE-PREPARE('ttTemp').
                ophBuffer = ophtTable:DEFAULT-BUFFER-HANDLE.
            END.
            ophBuffer:BUFFER-CREATE().
            
            DO ii = 1 TO EXTENT(cFieldNames):
                cField = cFieldNames[ii].
                CASE ophBuffer:BUFFER-FIELD(cField):DATA-TYPE:
                    WHEN 'character' THEN 
                        ophBuffer:BUFFER-FIELD(cField):BUFFER-VALUE = oJsonField:GetCharacter(cField) NO-ERROR. 
                    WHEN 'integer' THEN 
                        ophBuffer:BUFFER-FIELD(cField):BUFFER-VALUE = oJsonField:GetInteger(cField) NO-ERROR. 
                    WHEN 'logical' THEN 
                        ophBuffer:BUFFER-FIELD(cField):BUFFER-VALUE = STRING(oJsonField:GetLogical(cField)) NO-ERROR.
                END CASE.
                IF ophBuffer:BUFFER-FIELD(cField):BUFFER-VALUE = ? THEN ophBuffer:BUFFER-FIELD(cField):BUFFER-VALUE = ''.
            END.
            DELETE OBJECT oJsonField NO-ERROR.
        END.

        lRetVal = TRUE.
        RETURN lRetVal.

    END METHOD.

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

    METHOD PUBLIC LOGICAL fillFileDict(
        INPUT oJsonIdent AS JsonObject
        ):
        
        DEFINE VARIABLE lresult     AS LOGICAL   NO-UNDO INIT FALSE.
        DEFINE VARIABLE ii          AS INTEGER   NO-UNDO.
        DEFINE VARIABLE i1          AS INTEGER   NO-UNDO.
        DEFINE VARIABLE i2          AS INTEGER   NO-UNDO.
        DEFINE VARIABLE lRetVal     AS LOG       NO-UNDO INIT FALSE.
        DEFINE VARIABLE hBuffer     AS HANDLE    NO-UNDO.
        DEFINE VARIABLE hQuery      AS HANDLE    NO-UNDO.
        DEFINE VARIABLE rFile       AS RECID     NO-UNDO.
        DEFINE VARIABLE cViewas     AS CHARACTER NO-UNDO.
        
        DEFINE VARIABLE cFileNames  AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cFileName   AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cLDBName    AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cDBName     AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cTestName   AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cDBFileName AS CHARACTER NO-UNDO.
        EMPTY TEMP-TABLE tFileDict.

        lRetVal = FALSE.
        cFileNames = oJsonIdent:GetCharacter('tables').
        DO ii = 1 TO NUM-ENTRIES(cFileNames, ';'):
            cFileName = ENTRY(ii,cFileNames, ';').
            
            DO i1 = 1 TO NUM-DBS:
                cDBName   = LDBNAME(i1).
                cFileName = SUBSTITUTE('&1.&2', cDBName, cFileName) NO-ERROR.        
                CREATE BUFFER hBuffer FOR TABLE cFileName NO-ERROR.
                IF  ERROR-STATUS:ERROR        OR 
                    NOT VALID-HANDLE(hBuffer) THEN NEXT.
                /*                    lRetVal = hBuffer:FIND-FIRST('WHERE _File-Name = ' + QUOTER(cFileName), NO-LOCK) NO-ERROR.*/
                /*                    IF lRetVal THEN                                                                           */
                /*                    DO:                                                                                       */
                /*                        rFile = hBuffer:RECID.                                                                */
                /*                        DELETE OBJECT hBuffer NO-ERROR.                                                       */
                /*                        LEAVE.                                                                                */
                /*                    END.                                                                                      */
                /*                    DELETE OBJECT hBuffer.                                                                    */
                lRetVal = TRUE.
            END.
            IF NOT lRetVal THEN NEXT.
            
/*            cDBFileName = cDBName + '.' + '_Field'.                                                                                    */
/*                                                                                                                                       */
/*            CREATE BUFFER hBuffer FOR TABLE cDBFileName.                                                                               */
/*            CREATE QUERY hQuery.                                                                                                       */
/*            hQuery:SET-BUFFERS(hBuffer).                                                                                               */
/*            hQuery:QUERY-PREPARE(SUBSTITUTE('FOR EACH &1 WHERE _Field._File-Recid = &2 NO-LOCK BY _Field._Order', cDBFileName, rFile)).*/
/*            hQuery:QUERY-OPEN().                                                                                                       */
 
            DO WHILE NOT hQuery:QUERY-OFF-END:
                hQuery:GET-NEXT(NO-LOCK) NO-ERROR.
                IF hQuery:QUERY-OFF-END THEN LEAVE.
            DO i1 = 1 TO hBuffer:NUM-FIELDS:
                IF hBuffer:BUFFER-FIELD(i1):NAME  = 'wordindex' THEN NEXT.

                CREATE  tFileDict.
                ASSIGN
                    tFileDict.cId          = cFilename + '_' + hBuffer::_Field-Name
                    tFileDict.cName        = cFilename + '_' + hBuffer::_Field-Name
                    tFileDict.cFormat      = hBuffer::_Format
                    tFileDict.cType        = hBuffer::_Data-Type
                    tFileDict.iDecimals    = 0
                    tFileDict.lhidden      = FALSE
                    tFileDict.leditable    = TRUE
                    tFileDict.iMaxLength   = 12
                    tFileDict.cAusrichtung = 'left'
                    tFileDict.cDisplayType = ?
                    tFileDict.lShowSpinner = ?
                    tFileDict.cInputMode   = ?
                    .

                FIND FIRST labeltexte NO-LOCK 
                    WHERE labeltexte.company     = oJsonIdent:GetCharacter('company')
                    AND   labeltexte.table_name  = cFileName
                    AND   labeltexte.field_name  = hBuffer::_Field-Name
                    AND   labeltexte.language_id = oJsonIdent:GetInteger('language_id') NO-ERROR.
                IF NOT AVAILABLE labeltexte THEN 
                DO:
                    FIND FIRST labeltexte NO-LOCK 
                        WHERE labeltexte.company     = oJsonIdent:GetCharacter('company')
                        AND   labeltexte.table_name  = 'table'
                        AND   labeltexte.field_name  = hBuffer::_Field-Name NO-ERROR.
                END.
                IF NOT AVAILABLE labeltexte THEN 
                DO:
                    FIND FIRST labeltexte NO-LOCK 
                        WHERE labeltexte.company     = oJsonIdent:GetCharacter('company')
                        AND   labeltexte.field_name  = hBuffer::_Field-Name
                        AND   labeltexte.language_id = oJsonIdent:GetInteger('language_id') NO-ERROR.
                END.

                IF AVAILABLE labeltexte THEN 
                DO:
                    tFileDict.cLabel = labeltexte.sidelabel.
                END.
                ELSE 
                DO:
                    tFileDict.cLabel = (IF hBuffer::_Label <> ? THEN hBuffer::_Label ELSE hBuffer::_Field-Name). 
                END.

                IF INDEX(hBuffer::_Format, '(') = 0 THEN tFileDict.iMaxLength = LENGTH(hBuffer::_Format).
                ELSE 
                DO:
                    i1 = INDEX(hBuffer::_Format, '(') + 1.
                    i2 = INDEX(hBuffer::_Format, ')').
                    tFileDict.iMaxLength = INTEGER(SUBSTRING(hBuffer::_Format, i1, i2 - i1)).
                END.
                
                CASE tFileDict.cType:
                    WHEN 'character' THEN 
                        DO: 
                            ASSIGN 
                                tFileDict.cType     = 'text'
                                tFileDict.iDecimals = 0.
                            IF hBuffer::_Extent > 0 THEN tFileDict.cType = 'textarea'. 
                        END.
                    WHEN 'Date' THEN 
                        DO: 
                            ASSIGN 
                                tFileDict.cType        = 'date'
                                tFileDict.cFormat      = '99.99.9999'
                                tFileDict.iDecimals    = 0
                                tFileDict.iMaxLength   = tFileDict.iMaxLength + 2
                                tFileDict.cAusrichtung = 'center'.
                        END.
                    WHEN 'Decimal' THEN 
                        DO: 
                            ASSIGN 
                                tFileDict.cType        = 'decimal'
                                tFileDict.iDecimals    = hBuffer::_Decimals
                                tFileDict.cDisplayType = 'text'
                                tFileDict.cInputMode   = 'decimal'
                                tFileDict.lShowSpinner = FALSE
                                tFileDict.iMaxLength   = tFileDict.iMaxLength
                                tFileDict.cAusrichtung = 'right'.
                        END.
                    WHEN 'Integer' OR
                    WHEN 'Int64'   THEN 
                        DO: 
                            ASSIGN 
                                tFileDict.cType        = 'integer'
                                tFileDict.iDecimals    = 0
                                tFileDict.cDisplayType = 'text'
                                tFileDict.cInputMode   = 'numeric'
                                tFileDict.lShowSpinner = FALSE
                                tFileDict.iMaxLength   = tFileDict.iMaxLength
                                tFileDict.cAusrichtung = 'right'.
                        END.
                    WHEN 'Recid' THEN 
                        DO: 
                            ASSIGN 
                                tFileDict.cType        = 'integer'
                                tFileDict.iDecimals    = 0
                                tFileDict.cDisplayType = 'text'
                                tFileDict.cInputMode   = 'text'
                                tFileDict.lShowSpinner = FALSE
                                tFileDict.lHidden      = TRUE
                                tFileDict.leditable    = FALSE.
                        END.
                    WHEN 'logical' THEN 
                        DO:
                            ASSIGN  
                                tFileDict.cType        = 'checkbox'
                                tFileDict.cAusrichtung = 'center'.
                        END.
                END CASE.
                
                cViewas = hBuffer::_View-as.
                CASE cViewas:
                    WHEN 'combo-box' THEN 
                        tFileDict.cType = 'select'.
                    WHEN 'editor' THEN
                        ASSIGN  
                            tFileDict.cType = 'textarea'.
                END CASE.
                 
            /*                tFileDict.cFormat = REPLACE(tFileDict.cFormat, '9', '0').*/
            /*                tFileDict.cFormat = REPLACE(tFileDict.cFormat, '>', ''). */
            /*                tFileDict.cFormat = REPLACE(tFileDict.cFormat, '<', ''). */
            /*                tFileDict.cAusrichtung = 'right'.                        */
            END.
            hQuery:QUERY-CLOSE().
            DELETE OBJECT hBuffer.
            DELETE OBJECT hQuery.
        END.
        
        lResult = TRUE.

        RETURN lresult.

    END METHOD.

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

    METHOD OVERRIDE PROTECTED INTEGER HandleGet (INPUT poRequest AS IWebRequest ):
        DEFINE VARIABLE oJsonResponse   AS JsonObject        NO-UNDO.
        DEFINE VARIABLE oJsonIdent      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 oBody           AS String            NO-UNDO.
        
        DEFINE VARIABLE oJsonFileDict   AS JsonObject        NO-UNDO.
        DEFINE VARIABLE oJsonConfig     AS JsonObject        NO-UNDO.
        DEFINE VARIABLE oJsonArray      AS JsonArray         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 htFileDict      AS HANDLE            NO-UNDO.
        DEFINE VARIABLE htConfig        AS HANDLE            NO-UNDO.
        DEFINE VARIABLE lcJsonInhalt    AS LONGCHAR          NO-UNDO.
        DEFINE VARIABLE cFileNames      AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cFileName       AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE ii              AS INTEGER           NO-UNDO.
        DEFINE VARIABLE dDateTime       AS DATETIME          NO-UNDO.
        DEFINE VARIABLE cDateTime       AS CHARACTER         NO-UNDO.
        
        DEFINE VARIABLE oJsonMenu       AS JsonArray         NO-UNDO.
        DEFINE VARIABLE oJsonData       AS JsonObject        NO-UNDO.
        DEFINE VARIABLE cConfig_id      AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE lRetVal         AS LOGICAL           NO-UNDO INIT FALSE.
        DEFINE VARIABLE cFunction       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.
        
        outilitiesHandler = NEW utilitiesHandler().

        outilitiesHandler:getHeaderData(
            INPUT  poRequest    ,
            OUTPUT ccompany     ,
            OUTPUT cuser_name   ,
            OUTPUT cAnzeigeName ,
            OUTPUT ilanguage_id ,
            OUTPUT cSessionToken
            ).
        
        oJsonIdent = NEW JsonObject().
        oJsonIdent:ADD('H_company'     , ccompany     )   NO-ERROR.
        oJsonIdent:ADD('H_user_name'   , cuser_name   )   NO-ERROR.
        oJsonIdent:ADD('H_display_name', cAnzeigeName )   NO-ERROR.
        oJsonIdent:ADD('H_language_id' , iLanguage_id )   NO-ERROR.
        oJsonIdent:ADD('H_sessionToken', cSessionToken)   NO-ERROR.
        //
        //  Alle Parameter aus dem Query-String lesen und im Object oJsonIdent ergänzenz
        //
        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.
        
        IF NOT oJsonIdent:HAS('company'    ) THEN oJsonIdent:ADD('company'    , oJsonIdent:GetCharacter('H_company'    )).
        IF NOT oJsonIdent:HAS('user_name'  ) THEN oJsonIdent:ADD('user_name'  , oJsonIdent:GetCharacter('H_user_name'  )).
        IF NOT oJsonIdent:HAS('language_id') THEN oJsonIdent:ADD('language_id', oJsonIdent:GetInteger  ('H_language_id')).
        
        oJsonIdent:WriteFile('C:\TEMP\formdesigner_GET.json', TRUE).
        
        cFileNames = REPLACE(oJsonIdent:GetCharacter("tables"), ',', ';')  NO-ERROR.
        cFunction  = oJsonIdent:GetCharacter('function').

        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:WRITE(lcJsonInhalt, FALSE).
            
            lRetVal = TRUE.
            LEAVE.
        END.
        
        DO WHILE cFunction = 'load': 
            fillFileDict(
                INPUT oJsonIdent
                ).

            EMPTY TEMP-TABLE tConfig.
            FOR EACH htmldokumente NO-LOCK 
                WHERE htmldokumente.company     = ccompany
                AND   htmldokumente.user_name   = cuser_name
                AND   htmldokumente.language_id = ilanguage_id:
                    
                DO ii = 1 TO NUM-ENTRIES(cFileNames, ';'):
                    cFileName = ENTRY(ii, cFileNames, ';').

                    IF INDEX(htmldokumente.Form_Id, cFileName) = 0 THEN NEXT.
                    
                    IF htmldokumente.updated_at = ? THEN 
                    DO:
                        dDateTime = htmldokumente.created_at. 
                        cDateTime = STRING(htmldokumente.created_at,'99-99-9999THH:MM:SS').
                    END.
                    ELSE 
                    DO:
                        dDateTime = htmldokumente.updated_at. 
                        cDateTime = STRING(htmldokumente.updated_at,'99-99-9999THH:MM:SS').
                    END.

                    CREATE  tConfig.
                    ASSIGN  
                        tConfig.cId        = htmldokumente.form_id
                        tConfig.cFormTitle = htmldokumente.formtitle
                        tConfig.cCreated   = STRING(YEAR(dDateTime),'9999')
                                       + '-'
                                       + STRING(MONTH(dDateTime),'99')
                                       + '-'
                                       + STRING(DAY(dDateTime),'99')
                                       + SUBSTRING(cDateTime,11).

                    LEAVE.
                END.
            END.
        
            htFileDict  = TEMP-TABLE tFileDict :DEFAULT-BUFFER-HANDLE.
            htConfig    = TEMP-TABLE tConfig   :DEFAULT-BUFFER-HANDLE.
            hdsResponse = DATASET    dsResponse:HANDLE.
                 
            oJsonResponse = NEW JsonObject().
            oJsonResponse:READ(hdsResponse).
            oJsonResponse:WRITE(lcJsonInhalt, FALSE).
        
            lRetVal = TRUE.
            LEAVE.
        END.
        
        DO WHILE cFunction = 'delete':
            lRetVal = FALSE.
            REPEAT TRANSACTION ON ERROR UNDO, LEAVE:
                FIND FIRST htmldokumente 
                    WHERE htmldokumente.company     = ccompany
                    AND   htmldokumente.user_name   = cuser_name
                    AND   htmldokumente.language_id = ilanguage_id
                    AND   htmldokumente.form_id     = cConfig_id NO-ERROR.
                IF NOT AVAILABLE htmldokumente THEN LEAVE.
                DELETE  htmldokumente.
                lRetVal = TRUE.
                LEAVE.
            END.
            
            oJsonResponse   = NEW JsonObject().
            oJsonResponse:ADD("success", lRetVal).
            IF lRetVal THEN oJsonResponse:ADD('message', 'Konfiguration erfolreich gel�scht').
            ELSE            oJsonResponse:ADD('error'  , 'Konfiguration nicht gefunden'     ).
            oJsonResponse:write(lcJsonInhalt, FALSE).
                            
            LEAVE.
        END.
        
        DO WHILE cFunction = 'loaddetail':
            FIND FIRST htmldokumente NO-LOCK 
                WHERE htmldokumente.company     = ccompany
                AND   htmldokumente.user_name   = cuser_name
                AND   htmldokumente.language_id = ilanguage_id
                AND   htmldokumente.form_id     = cConfig_id NO-ERROR.
                
            lcJsonInhalt    = htmldokumente.htmltext.
            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).
           
        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 oJsonMessage AS JsonObject        NO-UNDO.
        DEFINE VARIABLE oFelder      AS JsonArray         NO-UNDO.
        DEFINE VARIABLE oFeld        AS JsonObject        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 ii           AS INTEGER           NO-UNDO.
        DEFINE VARIABLE oJsonConfig  AS JsonObject        NO-UNDO.
        DEFINE VARIABLE oJsonFields  AS JsonArray         NO-UNDO.
        
        DEFINE VARIABLE ctables      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 cformtitle   AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cformid      AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE lcJsonConfig AS LONGCHAR          NO-UNDO.
        
        DEFINE VARIABLE htTable      AS HANDLE            NO-UNDO.
        DEFINE VARIABLE hBuffer      AS HANDLE            NO-UNDO.
        DEFINE VARIABLE hQuery       AS HANDLE            NO-UNDO.
        DEFINE VARIABLE cFunction    AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cFieldNames  AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cFieldName   AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cFileName    AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE cBoxfields   AS CHARACTER         NO-UNDO.
        DEFINE VARIABLE lBoxField    AS LOGICAL           NO-UNDO.
        
        lRetVal = WebHandlerUtilities:getJsonFromRequest(INPUT poRequest, OUTPUT oJsonData) NO-ERROR.
        // MESSAGE 'getJsonFromRequest    = ' lRetVal.
        
        cFunction = poRequest:URI:GetQueryValue("function").
        // MESSAGE 'Funktion = ' cFunction.
        
        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')).
        ctables      =         oJsonData:GetCharacter('tables').
        // MESSAGE ccompany '-' cuser_name '-' ilanguage_id '-' ctables.
        
        oJsonConfig = NEW JsonObject().
        oJsonConfig = oJsonData:GetJsonObject ('config').
        cformtitle  = oJsonConfig:GetCharacter('formTitle').
        cformid     = oJsonConfig:GetCharacter('formId').
        oJsonFields = NEW JsonArray().
        oJsonFields = oJsonConfig:GetJsonArray('selectedFields').
        oJsonConfig:WRITE(lcJsonConfig, FALSE).
        
        oJsonConfig:WriteFile('C:\TEMP\FormDesignerOutput.json', TRUE).
        
        createTemptable (
            INPUT  oJsonFields,
            OUTPUT htTable    ,
            OUTPUT hBuffer    ).
        
        hBuffer:WRITE-JSON ('file', 'C:\TEMP\FeldAttributeAusFormdesigner.csv', TRUE).

        REPEAT TRANSACTION ON ERROR UNDO, LEAVE:
            FIND htmldokumente
                WHERE htmldokumente.company     = ccompany
                AND   htmldokumente.user_name   = cuser_name
                AND   htmldokumente.form_id     = cformid
                AND   htmldokumente.language_id = ilanguage_id NO-ERROR.
            IF NOT AVAILABLE htmldokumente THEN 
            DO:
                CREATE  htmldokumente.
                ASSIGN 
                    htmldokumente.company     = ccompany
                    htmldokumente.user_name   = cuser_name
                    htmldokumente.form_id     = cformid
                    htmldokumente.language_id = ilanguage_id
                    htmldokumente.created_at  = NOW.
            END.
            ASSIGN 
                htmldokumente.tables    = ctables
                htmldokumente.formtitle = cformtitle.
            htmltext = STRING(lcJsonConfig).
            // MESSAGE 'htmldokument gschrieben mit config ' STRING(lcJsonConfig).
            RELEASE htmldokumente.
            LEAVE.
        END.
        
        cBoxfields = ''.
        CREATE QUERY hQuery.
        hQuery:SET-BUFFERS(hBuffer).
        hQuery:QUERY-PREPARE ('FOR EACH ttTemp NO-LOCK').
        hQuery:QUERY-OPEN ().
        hQUery:GET-FIRST ().
        DO WHILE NOT hQuery:QUERY-OFF-END TRANSACTION:
            cFieldName = hBuffer:BUFFER-FIELD('fieldName'):BUFFER-VALUE.
            cFileName  = ENTRY(1, cFieldName, '_').
            FIND FIRST labeltexte
                WHERE labeltexte.company     = ccompany
                AND   labeltexte.user_name   = cuser_name
                AND   labeltexte.program     = cformid
                AND   labeltexte.table_name  = cFileName
                AND   labeltexte.field_name  = cFieldName
                AND   labeltexte.language_id = ilanguage_id NO-ERROR.
            IF NOT AVAILABLE labeltexte THEN 
            DO:
                CREATE  labeltexte.
                ASSIGN
                    labeltexte.company     = ccompany
                    labeltexte.user_name   = cuser_name
                    labeltexte.program     = cformid
                    labeltexte.table_name  = cFileName
                    labeltexte.field_name  = cFieldName
                    labeltexte.language_id = ilanguage_id.
            END.
            ASSIGN 
                labeltexte.sidelabel = hBuffer:BUFFER-FIELD('label'):BUFFER-VALUE
                labeltexte.headlabel = hBuffer:BUFFER-FIELD('label'):BUFFER-VALUE
                labeltexte.alignment = hBuffer:BUFFER-FIELD('align'):BUFFER-VALUE.
                
            FIND FIRST labeltexte
                WHERE labeltexte.company     = ccompany
                AND   labeltexte.user_name   = ''
                AND   labeltexte.program     = ''
                AND   labeltexte.table_name  = cFileName
                AND   labeltexte.field_name  = cFieldName
                AND   labeltexte.language_id = ilanguage_id NO-ERROR.
            IF NOT AVAILABLE labeltexte THEN 
            DO:
                CREATE  labeltexte.
                ASSIGN
                    labeltexte.company     = ccompany
                    labeltexte.user_name   = ''
                    labeltexte.program     = ''
                    labeltexte.table_name  = cFileName
                    labeltexte.field_name  = cFieldName
                    labeltexte.language_id = ilanguage_id
                    labeltexte.sidelabel   = hBuffer:BUFFER-FIELD('label'):BUFFER-VALUE
                    labeltexte.headlabel   = hBuffer:BUFFER-FIELD('label'):BUFFER-VALUE
                    labeltexte.alignment   = hBuffer:BUFFER-FIELD('align'):BUFFER-VALUE.
            END.
            
            FIND FIRST labeltexte
                WHERE labeltexte.company     = ccompany
                AND   labeltexte.user_name   = ''
                AND   labeltexte.program     = ''
                AND   labeltexte.table_name  = ''
                AND   labeltexte.field_name  = cFieldName
                AND   labeltexte.language_id = ilanguage_id NO-ERROR.
            IF NOT AVAILABLE labeltexte THEN 
            DO:
                CREATE  labeltexte.
                ASSIGN
                    labeltexte.company     = ccompany
                    labeltexte.user_name   = ''
                    labeltexte.program     = ''
                    labeltexte.table_name  = ''
                    labeltexte.field_name  = cFieldName
                    labeltexte.language_id = ilanguage_id
                    labeltexte.sidelabel   = hBuffer:BUFFER-FIELD('label'):BUFFER-VALUE
                    labeltexte.headlabel   = hBuffer:BUFFER-FIELD('label'):BUFFER-VALUE
                    labeltexte.alignment   = hBuffer:BUFFER-FIELD('align'):BUFFER-VALUE.
            END.
            RELEASE labeltexte.
            
            lBoxField = (IF hBuffer:BUFFER-FIELD('type'):BUFFER-VALUE = 'select' THEN TRUE ELSE FALSE).
            IF lBoxField THEN 
            DO:
                cBoxFields = cBoxfields
                    + (IF cBoxfields = '' THEN '' ELSE ';')
                    + hBuffer:BUFFER-FIELD('fieldName'):BUFFER-VALUE.
            END.
            
            hQuery:GET-NEXT ().
        END. 
        
        IF cBoxfields <> '' THEN 
        DO:
            REPEAT TRANSACTION ON ERROR UNDO, LEAVE:
                FIND FIRST selectboxfields
                    WHERE selectboxfields.company   = ccompany
                    AND   selectboxfields.program   = cFormId
                    AND   selectboxfields.user_name = ''
                    AND   selectboxfield.table_Name = '' NO-ERROR.
                IF NOT AVAILABLE selectboxfields THEN 
                DO:
                    CREATE  selectboxfields.
                    ASSIGN
                        selectboxfields.company   = ccompany
                        selectboxfields.program   = cFormId
                        selectboxfields.user_name = ''
                        selectboxfield.table_Name = ''.
                END.
                ASSIGN 
                    selectboxfield.boxfields = cBoxFields.
                RELEASE selectboxfields.
                LEAVE.
            END.
        END. 
        
        // MESSAGE 'geschrieben ' ccompany '-' cuser_name '-' cformid '-' ilanguage_id.
            
        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('id', cformid).
        oJsonMessage:ADD('message', 'erfolgreich gespeichert').
        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  NO-ERROR.*/
        /*            DELETE OBJECT oJsonMessage NO-ERROR.*/
        /*            DELETE OBJECT oJsonFields  NO-ERROR.*/
        END FINALLY.

    END METHOD.

END CLASS.