 
/*------------------------------------------------------------------------
   File        : lookupHandler
   Purpose     : 
   Syntax      : 
   Description : 
   Author(s)   : walter.riechsteiner
   Created     : Mon Dec 29 20:24:32 CET 2025
   Notes       : 
 ----------------------------------------------------------------------*/


BLOCK-LEVEL ON ERROR UNDO, THROW.

USING Progress.Json.ObjectModel.JsonObject FROM PROPATH.

CLASS utilities.lookupHandler: 
    
    DEFINE VARIABLE httLookup  AS HANDLE NO-UNDO.   
    DEFINE VARIABLE hbttLookup AS HANDLE NO-UNDO. 

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

    METHOD PUBLIC LOGICAL city(
        INPUT  oJsonIdent  AS JsonObject,
        OUTPUT oJsonResult AS JsonObject
        ):
          
        DEFINE VARIABLE lRetVal            AS LOGICAL    NO-UNDO INIT FALSE.  
        DEFINE VARIABLE cSearchField       AS CHARACTER  NO-UNDO.
        DEFINE VARIABLE ccountry_code      AS CHARACTER  NO-UNDO.
        DEFINE VARIABLE cField             AS CHARACTER  NO-UNDO.
        DEFINE VARIABLE hcities            AS HANDLE     NO-UNDO.
        DEFINE VARIABLE iLanguage_id       AS INTEGER    NO-UNDO.
        DEFINE VARIABLE cDisplayFields     AS CHARACTER  NO-UNDO.
        DEFINE VARIABLE ii                 AS INTEGER    NO-UNDO.
        
        DEFINE VARIABLE oJsonDisplayLabels AS JsonObject.

        cSearchField   = oJsonIdent:GetCharacter('search').
        cDisplayFields = oJsonIdent:GetCharacter('displayFields').
        cDisplayFields = REPLACE(cDisplayFields, ' ', '').

        CREATE TEMP-TABLE httLookup.
        DO ii = 1 TO NUM-ENTRIES(cDisplayFields, ','):
            cField = ENTRY(ii, cDisplayFields, ',').
            httLookup:ADD-NEW-FIELD (cField, 'character').
        END.
        httLookup:TEMP-TABLE-PREPARE ('results').
        hbttLookup = httLookup:DEFAULT-BUFFER-HANDLE.
        
        cField        = oJsonIdent:GetCharacter('file_name') + '_country_code'.
        cCountry_code = oJsonIdent:GetCharacter(cField).
        cField        = oJsonIdent:GetCharacter('file_name') + '_language_id'.
        iLanguage_Id  = INTEGER(oJsonIdent:GetCharacter(cField)).
        oJsonIdent:SET('language_id', iLanguage_id).

        hcities = BUFFER cities:HANDLE.
        FOR EACH cities NO-LOCK
            WHERE INDEX(cities.wordindex, cSearchField) > 0
            AND   cities.country_code = cCountry_Code:
                
/*            IF NOT cities.postal_code BEGINS cSearchField THEN NEXT.*/
                            
            hbttLookup:BUFFER-CREATE ().
            hbttLookup:BUFFER-COPY(hcities).
        END.        
            
        oJsonResult = NEW JsonObject().
        httLookup:WRITE-JSON('JsonObject', oJsonResult) NO-ERROR.
        
        getDisplayLabels(
            INPUT  oJsonIdent        ,
            INPUT  cDisplayFields    ,
            OUTPUT oJsonDisplayLabels
            ).
            
        oJsonResult:ADD('displayLabels', oJsonDisplayLabels).
        
        lRetVal = TRUE.
        RETURN lRetVal.

    END METHOD.

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

    METHOD PUBLIC VOID getDisplayLabels(
        INPUT  oJsonIdent         AS JsonObject,
        INPUT  cFieldNames        AS CHARACTER ,
        OUTPUT oJsonDisplayLabels AS JsonObject
        ):
            
        DEFINE VARIABLE cUsers       AS CHARACTER NO-UNDO INIT 'admin'.
        DEFINE VARIABLE cFileNames   AS CHARACTER NO-UNDO INIT 'table'.
        DEFINE VARIABLE cProgram     AS CHARACTER NO-UNDO INIT 'program'.
        DEFINE VARIABLE cFileName    AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cUserName    AS CHARACTER NO-UNDO.
        DEFINE VARIABLE cCompany     AS CHARACTER NO-UNDO.
        DEFINE VARIABLE iLanguage_id AS INTEGER   NO-UNDO.
        DEFINE VARIABLE cField       AS CHARACTER NO-UNDO.
        DEFINE VARIABLE i1           AS INTEGER   NO-UNDO.
        DEFINE VARIABLE i2           AS INTEGER   NO-UNDO.
        DEFINE VARIABLE i3           AS INTEGER   NO-UNDO.
        
        cFileName    = oJsonIdent:GetCharacter('file_name').
        iLanguage_id = oJsonIdent:GetInteger  ('language_id').
        cUserName    = oJsonIdent:GetCharacter('user_name').
        cCompany     = oJsonIdent:GetCharacter('company').

        cFileNames = cFileNames + ',' + cFileName.                    
        IF cUserName <> cUsers THEN cUsers = cUsers + ',' + cUserName.
        
        oJsonDisplayLabels = NEW JsonObject().
        DO i1 = 1 TO NUM-ENTRIES(cFieldNames, ','):
            oJsonDisplayLabels:ADD(ENTRY(i1, cFieldNames, ','), ENTRY(i1, cFieldNames, ',')).
        END.
            
        DO i1 = 1 TO NUM-ENTRIES(cFileNames):
            cFileName = ENTRY(i1, cFileNames, ',').
            DO i2 = 1 TO NUM-ENTRIES(cUsers):
                cUserName = ENTRY(i2, cUsers, ',').
                DO i3 = 1 TO NUM-ENTRIES(cFieldNames, ','):
                    cField = ENTRY(i3, cFieldNames, ',').
                    FIND FIRST labeltexte
                        WHERE labeltexte.company     = ccompany
                        AND   labeltexte.user_name   = cUserName
                        AND   labeltexte.program     = cProgram
                        AND   labeltexte.table_name  = cFileName
                        AND   labeltexte.field_name  = cField
                        AND   labeltexte.language_id = ilanguage_id NO-ERROR.
                    IF AVAILABLE labeltexte THEN oJsonDisplayLabels:SET(cField, labeltexte.headlabel).
                END.
            END.
        END.                     
            
        RETURN.

    END METHOD.

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

    METHOD PUBLIC LOGICAL postal_code( 
        INPUT  oJsonIdent  AS JsonObject,
        OUTPUT oJsonResult AS JsonObject
        ):
          
        DEFINE VARIABLE lRetVal            AS LOGICAL    NO-UNDO INIT FALSE.  
        DEFINE VARIABLE cSearchField       AS CHARACTER  NO-UNDO.
        DEFINE VARIABLE ccountry_code      AS CHARACTER  NO-UNDO.
        DEFINE VARIABLE cField             AS CHARACTER  NO-UNDO.
        DEFINE VARIABLE hcities            AS HANDLE     NO-UNDO.
        DEFINE VARIABLE iLanguage_id       AS INTEGER    NO-UNDO.
        DEFINE VARIABLE cDisplayFields     AS CHARACTER  NO-UNDO.
        DEFINE VARIABLE ii                 AS INTEGER    NO-UNDO.
        
        DEFINE VARIABLE oJsonDisplayLabels AS JsonObject.

        cSearchField   = oJsonIdent:GetCharacter('search').
        cDisplayFields = oJsonIdent:GetCharacter('displayFields').
        cDisplayFields = REPLACE(cDisplayFields, ' ', '').

        CREATE TEMP-TABLE httLookup.
        DO ii = 1 TO NUM-ENTRIES(cDisplayFields, ','):
            cField = ENTRY(ii, cDisplayFields, ',').
            httLookup:ADD-NEW-FIELD (cField, 'character').
        END.
        httLookup:TEMP-TABLE-PREPARE ('results').
        hbttLookup = httLookup:DEFAULT-BUFFER-HANDLE.
        
        cField        = oJsonIdent:GetCharacter('file_name') + '_country_code'.
        cCountry_code = oJsonIdent:GetCharacter(cField).
        cField        = oJsonIdent:GetCharacter('file_name') + '_language_id'.
        iLanguage_Id  = INTEGER(oJsonIdent:GetCharacter(cField)).
        oJsonIdent:SET('language_id', iLanguage_id).

        hcities = BUFFER cities:HANDLE.
        FOR EACH cities NO-LOCK
            WHERE INDEX(cities.wordindex, cSearchField) > 0
            AND   cities.country_code = cCountry_Code:
                
            IF NOT cities.postal_code BEGINS cSearchField THEN NEXT.
                            
            hbttLookup:BUFFER-CREATE ().
            hbttLookup:BUFFER-COPY(hcities).
        END.        
            
        oJsonResult = NEW JsonObject().
        httLookup:WRITE-JSON('JsonObject', oJsonResult) NO-ERROR.
        
        getDisplayLabels(
            INPUT  oJsonIdent        ,
            INPUT  cDisplayFields    ,
            OUTPUT oJsonDisplayLabels
            ).
            
        oJsonResult:ADD('displayLabels', oJsonDisplayLabels).
        
        lRetVal = TRUE.
        RETURN lRetVal.

    END METHOD.

END CLASS.