Compile function PRS_Stage_Services(@Service, @Params)
/***********************************************************************************************************************

    This program is proprietary and is not to be used by or disclosed to others, nor is it to be copied without written
    permission from SRP Computer Solutions, Inc.

    Name        :   PRS_Stage_Services

    Description :   Handler program for all module related services.

    Notes       :   Service module to support environmental state issues. Environmental refers to the state of the
                    operating system, which includes version, client vs. server, and path to critical systems.

    Parameters  :
        Service         [in] -- Name of the service being requested
        Param1-10   [in/out] -- Additional request parameter holders
        Response       [out] -- Response to be sent back to the Controller (MCP) or requesting procedure

    Metadata    :

    History     :   (Date, Initials, Notes)
        12/06/22    djs     Original programmer.

***********************************************************************************************************************/

#pragma precomp SRP_PreCompiler

$insert LOGICAL
$insert SERVICE_SETUP
$insert PROD_SPEC_EQUATES
$insert PRS_STAGE_EQUATES

Declare function    Database_Services, Error_Services, SRP_JSON
Declare subroutine  Database_Services, Error_Services, SRP_JSON

GoToService else
    Error_Services('Add', Service : ' is not a valid service request within the ' : ServiceModule : ' services module.')
end

Return Response or ""

//-----------------------------------------------------------------------------
// SERVICES
//-----------------------------------------------------------------------------

Service ConvertRecordToJSON(KeyID, Record, ItemURL)
    
    jsonRecord    = ''

    If KeyID NE '' then

        If Record EQ '' then Record = Database_Services('ReadDataRow', 'PRS_STAGE', KeyID)
        If Error_Services('NoError') then
            @DICT    = Database_Services('GetTableHandle', 'DICT.PRS_STAGE')
            @ID      = KeyID
            @RECORD  = Record
            If SRP_JSON(objJSON, 'New', 'Object') then
                
                If SRP_JSON(objPRSStage, 'New', 'Object') then 
                    SRP_JSON(objPRSStage, 'SetValue', 'keyId', @ID)
                    SRP_JSON(objPRSStage, 'SetValue', 'psn', Field(@ID, '*', 1))
                    SRP_JSON(objPRSStage, 'SetValue', 'stage', Field(@ID, '*', 2))
                    // Cleans
                    CleanTools     = {CLEAN_TOOL}
                    CleanToolDescs = {TOOL_DESC}
                    CleanRecipe    = {CLEAN_RECIPE}
                    CleanSigReq    = {CLEAN_SIG_REQ}
                    If SRP_JSON(objCleans, 'New', 'Object') then
                        SRP_JSON(objCleans, 'SetValue', 'cleanRecipe', CleanRecipe)
                        SRP_JSON(objCleans, 'SetValue', 'cleanSigReq', CleanSigReq, 'Boolean')
                        If SRP_JSON(objCleanToolsArray, 'New', 'Array') then
                            If CleanTools NE '' then
                                For each CleanTool in CleanTools using @VM setting vPos
                                    If SRP_JSON(objCleanTool, 'New', 'Object') then
                                        SRP_JSON(objCleanTool, 'SetValue', 'tool', CleanTool)
                                        SRP_JSON(objCleanTool, 'SetValue', 'description', CleanToolDescs<0, vPos>)
                                        SRP_JSON(objCleanToolsArray, 'Add', objCleanTool)
                                        SRP_JSON(objCleanTool, 'Release')
                                    end
                                Next CleanTool
                            end
                            SRP_JSON(objCleans, 'Set', 'cleanTools', objCleanToolsArray)
                            SRP_JSON(objCleanToolsArray, 'Release')
                        end
                        SRP_JSON(objPRSStage, 'Set', 'cleans', objCleans)
                        SRP_JSON(objCleans, 'Release')
                    end
                    
                    // Inspections
                    If SRP_JSON(objInsp, 'New', 'Object') then
                        SRP_JSON(objInsp, 'SetValue', 'microscope', {MICROSCOPE}, 'Boolean')
                        SRP_JSON(objInsp, 'SetValue', 'brightlight', {BRIGHTLIGHT}, 'Boolean')
                        SRP_JSON(objInsp, 'SetValue', 'inspSigReq', {INSP_SIG_REQ}, 'Boolean')
                        SRP_JSON(objInsp, 'SetValue', 'inspInterval', {INSP_INTERVAL})
                        If SRP_JSON(objFrontSide, 'New', 'Object') then
                            SRP_JSON(objFrontSide, 'SetValue', 'lpd', {LPD})
                            SRP_JSON(objFrontSide, 'SetValue', 'scratches', {SCRATCHES})
                            SRP_JSON(objFrontSide, 'SetValue', 'scratchLen', OConv({SCRATCH_LEN}, 'MD2'))
                            SRP_JSON(objFrontSide, 'SetValue', 'pits', {PITS})
                            SRP_JSON(objFrontSide, 'SetValue', 'mounds', {MOUNDS})
                            SRP_JSON(objFrontSide, 'SetValue', 'stackFaults', {STACK_FAULTS})
                            SRP_JSON(objFrontSide, 'SetValue', 'spikes', {SPIKES})
                            SRP_JSON(objFrontSide, 'SetValue', 'spots', {SPOTS})
                            SRP_JSON(objFrontSide, 'SetValue', 'blDefects', {BL_DEFECTS})
                            SRP_JSON(objInsp, 'Set', 'frontSide', objFrontSide)
                            SRP_JSON(objFrontSide, 'Release')
                        end
                        
                        If SRP_JSON(objBackSide, 'New', 'Object') then
                            SRP_JSON(objBackSide, 'SetValue', 'scratches', {BSIDE_SCRATCHES})
                            SRP_JSON(objBackSide, 'SetValue', 'scratchLen', OConv({BSIDE_SCRATCH_LEN}, 'MD2'))
                            SRP_JSON(objBackSide, 'SetValue', 'nodules', {BSIDE_NODULES})
                            SRP_JSON(objBackSide, 'SetValue', 'spikes', {BSIDE_SPIKES})
                            SRP_JSON(objInsp, 'Set', 'backSide', objBackSide)
                            SRP_JSON(objBackSide, 'Release')
                        end
                        
                        SRP_JSON(objPRSStage, 'Set', 'inspection', objInsp)
                        SRP_JSON(objInsp, 'Release')
                    end
                    
                    // Surfscan Tests
                    If SRP_JSON(objSurfscan, 'New', 'Object') then
                        SRP_JSON(objSurfscan, 'SetValue', 'surfscanSigReq', {SURFSCAN_SIG_REQ}, 'Boolean')
                        If SRP_JSON(objSurfscanRecipeArray, 'New', 'Array') then
                            SurfscanRecipes = {SURFSCAN_RECIPE}
                            If SurfscanRecipes NE '' then
                                SurfscanDefects     = {SURF_DEFECTS}
                                SurfscanHazes       = OConv({SURF_HAZE}, 'MD2')
                                SurfscanSampleSizes = {SS_SAMP_QTY}
                                For each SurfscanRecipe in SurfscanRecipes using @VM setting vPos
                                    If SRP_JSON(objSurfscanRecipe, 'New', 'Object') then
                                        SRP_JSON(objSurfscanRecipe, 'SetValue', 'recipe', SurfscanRecipe)
                                        SRP_JSON(objSurfscanRecipe, 'SetValue', 'defects', SurfscanDefects<0, vPos>)
                                        SRP_JSON(objSurfscanRecipe, 'SetValue', 'haze', SurfscanHazes<0, vPos>)
                                        SRP_JSON(objSurfscanRecipe, 'SetValue', 'sampleSize', SurfscanSampleSizes<0, vPos>)
                                        SRP_JSON(objSurfscanRecipeArray, 'Add', objSurfscanRecipe)
                                        SRP_JSON(objSurfscanRecipe, 'Release')
                                    end
                                Next SurfscanRecipe
                            end
                            
                            SRP_JSON(objSurfscan, 'Set', 'surfscanRecipes', objSurfscanRecipeArray)
                            SRP_JSON(objSurfscanRecipeArray, 'Release')
                        end
                        
                        SRP_JSON(objPRSStage, 'Set', 'surfscan', objSurfscan)
                        SRP_JSON(objSurfscan, 'Release')
                    end

                    // QA Metrology Tests
                    QAMetTests          = {MET_TEST}
                    QAMetProps          = {MET_PROP}
                    QAMetPropDescs      = {MET_PROP_DESC}
                    QAMetToolClasses    = {MET_TOOL_CLASS}
                    QAMetRecipes        = {MET_RECIPE}
                    QAMetRecipePatterns = {MET_RECIPE_PATTERN}
                    QAMetMins           = {MET_MIN}
                    QAMetMaxes          = {MET_MAX}
                    QAPhaseMins         = {MET_PHASE_MIN}
                    QAMetSlots          = {MET_SLOT}
                    QAMetWfrQtys        = {MET_WFR_QTY}
                    QAMetReactScheds    = {MET_REACT_SCHED}
                    QAMetIntervals      = {MET_INTERVAL}
                    QAMetStarts         = {MET_START}
                    QAMetSeqs           = {MET_SEQUENCE}
                    
                    If QAMetTests NE '' then
                        If SRP_JSON(objQAMetTestArray, 'New', 'Array') then
                            For each QAMetTest in QAMetTests using @VM setting vPos
                                If SRP_JSON(objQAMetTest, 'New', 'Object') then
                                    SRP_JSON(objQAMetTest, 'SetValue', 'test', QAMetTest)
                                    SRP_JSON(objQAMetTest, 'SetValue', 'property', QAMetProps<0, vPos>)
                                    SRP_JSON(objQAMetTest, 'SetValue', 'propertyDesc', QAMetPropDescs<0, vPos>)
                                    SRP_JSON(objQAMetTest, 'SetValue', 'toolClass', QAMetToolClasses<0, vPos>)
                                    SRP_JSON(objQAMetTest, 'SetValue', 'recipe', QAMetRecipes<0, vPos>)
                                    SRP_JSON(objQAMetTest, 'SetValue', 'recipePattern', QAMetRecipePatterns<0, vPos>)
                                    SRP_JSON(objQAMetTest, 'SetValue', 'min', QAMetMins<0, vPos>)
                                    SRP_JSON(objQAMetTest, 'SetValue', 'max', QAMetMaxes<0, vPos>)
                                    SRP_JSON(objQAMetTest, 'SetValue', 'phaseMin', QAPhaseMins<0, vPos>)
                                    SRP_JSON(objQAMetTest, 'SetValue', 'slots', QAMetSlots<0, vPos>)
                                    SRP_JSON(objQAMetTest, 'SetValue', 'wfrQty', QAMetWfrQtys<0, vPos>)
                                    SRP_JSON(objQAMetTest, 'SetValue', 'reactSched', QAMetReactScheds<0, vPos>, 'Boolean')
                                    SRP_JSON(objQAMetTest, 'SetValue', 'interval', QAMetIntervals<0, vPos>)
                                    SRP_JSON(objQAMetTest, 'SetValue', 'start', QAMetStarts<0, vPos>)
                                    SRP_JSON(objQAMetTest, 'SetValue', 'sequence', QAMetSeqs<0, vPos>)
                                    SRP_JSON(objQAMetTestArray, 'Add', objQAMetTest)
                                    SRP_JSON(objQAMetTest, 'Release')
                                end
                            Next QAMetTest
                            SRP_JSON(objPRSStage, 'Set', 'qaMetTests', objQAMetTestArray)
                            SRP_JSON(objQAMetTestArray, 'Release')
                        end
                    end
                    
                    SRP_JSON(objJSON, 'Set', 'prsStage', objPRSStage)
                    SRP_JSON(objPRSStage, 'Release')
                end
                
                If itemURL NE '' then
                    // The itemURL was passed in so add HAL+JSON properties.

                    // Create the _links property and then all link objects needed for this resource.
                    If SRP_JSON(objLinks, 'New', 'Object') then
                        // Create a self link.
                        If SRP_JSON(objLink, 'New', 'Object') then
                            SRP_JSON(objLink, 'SetValue', 'href', ItemURL, 'String')
                            SRP_JSON(objLink, 'SetValue', 'title', 'Self', 'String')
                            SRP_JSON(objLinks, 'Set', 'self', objLink)
                            SRP_JSON(objLink, 'Release')
                        end
                        SRP_JSON(objJSON, 'Set', '_links', objLinks)
                        SRP_JSON(objLinks, 'Release')
                    end

                    // Create the _class property for this resource.
                    SRP_JSON(objJSON, 'SetValue', '_class', 'resource')
                end
                jsonRecord = SRP_JSON(objJSON, 'Stringify', 'Styled')
                SRP_JSON(objJSON, 'Release')
            end else
                Error_Services('Add', 'Unable to create JSON representation in the ' : Service : ' service.')
            end
        end
    end else
        Error_Services('Add', 'KeyID argument was missing in the ' : Service : ' service.')
    end

    Response    = jsonRecord
    
end service


