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

    Name        :   Rds_Test_Services

    Description :   Handler program for all RDS_TEST services.

    Notes       :   Application errors should be logged using the Error Services module. There are a few methodological
                    assumptions built into way errors are managed which are important to understand in order to properly
                    work with Error Services:

                    - The term 'top' refers to the originating procedure of a call stack and the term 'bottom' refers to
                      the last routine (or the current routine) within a call stack. Within the OpenInsight Debugger
                      this will appear backwards since the originating procedure always appears at the bottom of the
                      list and the current routine appears at the top of the list. We are using this orientation because
                      it is common to refer to the process of calling other procedures as 'drilling down'.

                    - The reason for defining the orientation of the call stack is because Error_Services allows for
                      multiple error conditions to be appended to an original error. In most cases this will happen when
                      a procedure at the bottom of the stack generates an error condition and then returns to its
                      calling procedure. This higher level procedure can optionally add more information relevant to
                      itself. This continues as the call stack 'bubbles' its way back to the top to where the
                      originating procedure is waiting.

                    - Native OpenInsight commands that handle errors (e.g., Set_Status, Set_FSError, Set_EventStatus)
                      preserve their error state until explicitly cleared. This can hinder the normal execution of code
                      since subsequent procedures (usually SSPs) will fail if a pre-existing error condition exists.
                      Our philosophy is that error conditions should automatically be cleared before a new procedure
                      is executed to avoid this problem. However, the nature of Basic+ does not make this easy to
                      automate for any given stored procedure. Therefore, if a stored procedure wants to conform to our
                      philosophy then it should include a call into the 'Clear' service request at the top of the
                      program. Alternatively this can be done through a common insert (see SERVICE_SETUP for example.)

                    - Service modules will use the SERVICE_SETUP insert and therefore automatically clear out any
                      error conditions that were set before.

    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)
        09/26/2025    djs     Original programmer.

***********************************************************************************************************************/
#pragma precomp SRP_PreCompiler

$Insert SERVICE_SETUP
$insert APP_INSERTS
$Insert RDS_EQUATES
$Insert RDS_TEST_EQUATES
$Insert PROD_SPEC_EQUATES
$Insert RDS_LAYER_EQUATES
$INSERT PRS_LAYER_EQU

Declare function Database_Services, Error_Services, obj_Prod_Spec
Declare subroutine Database_Services, Error_Services

GoToService

Return Response or ""

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

Service UpdateRDSTestSpecs(RdsNo)
    
    ErrorMsg = ''
    If (RDSNo NE '') then
    	If RowExists('RDS', RDSNo) then
    		RDSRec    = Database_Services('ReadDataRow', 'RDS', RDSNo)
			If Error_Services('NoError') then
				LayerKeys = RDSRec<RDS_RDS_LAYER_KEYS$>
				PSNId     = RDSRec<RDS_PROD_SPEC_ID$>    			
    			For each LayerKey in LayerKeys using @VM setting vPos
    				LayerSet   = Field(LayerKey, '*', 2, 1)
					LayerSpecs = obj_Prod_Spec('GetLayerProp',PSNId:@RM:LayerSet:@RM:1)		;* Last parameter specifies no output conversion on return data
					LayerSpecs = Field(LayerSpecs,@FM,2,99)		;* Returns with the layer set ID in the first field of each line
					ErrCode    = ''
					If Not(Get_Status(ErrCode)) then 
						WaferSize		= Xlate('PROD_SPEC', PSNId, 'SUB_WAFER_SIZE', 'X')
						SubOrientation	= Xlate('PROD_SPEC', PSNId, 'SUB_ORIENTATION', 'X')
						ResUnits		= LayerSpecs<PRS_LAYER_RES_UNITS$>
						ReactorType		= Xlate('PROD_SPEC', PSNId, PROD_SPEC_REACTOR_TYPE$, 'X')
						ThickFilmMet	= Xlate('PROD_SPEC', PSNId, PROD_SPEC_THICKFILM_MET$, 'X')			;* Added 1/16/2009 JCH					
						
						RdsTestKeys = Database_Services('ReadDataColumn', 'RDS_LAYER', LayerKey, RDS_LAYER_RDS_TEST_KEYS$)
						If Error_Services('NoError') then
							For each RdsTestKey in RdsTestKeys using @VM setting RdsTestPos
								RdsTestRec = Database_Services('ReadDataRow', 'RDS_TEST', RdsTestKey)
								If Error_Services('NoError') then
									RDSTestRec<RDS_TEST_WAFER_SIZE$>        = WaferSize
									RDSTestRec<RDS_TEST_SUB_ORIENTATION$>   = SubOrientation
									RDSTestRec<RDS_TEST_SPEC_DOPANT$>       = LayerSpecs<PRS_LAYER_DOPANT$>
									RDSTestRec<RDS_TEST_SPEC_RECIPE$>       = LayerSpecs<PRS_LAYER_RECIPE$>
									RDSTestRec<RDS_TEST_SPEC_RECIPE_NAME$>  = LayerSpecs<PRS_LAYER_RECIPE_NAME$>
									RDSTestRec<RDS_TEST_SPEC_THICK_MIN$>    = LayerSpecs<PRS_LAYER_THICK_MIN$>
									RDSTestRec<RDS_TEST_SPEC_THICK_TARGET$> = LayerSpecs<PRS_LAYER_THICK_TARGET$>
									RDSTestRec<RDS_TEST_SPEC_THICK_MAX$>    = LayerSpecs<PRS_LAYER_THICK_MAX$>
									RDSTestRec<RDS_TEST_SPEC_THICK_UNITS$>  = LayerSpecs<PRS_LAYER_THICK_UNITS$>
									RDSTestRec<RDS_TEST_SPEC_RES_MIN$>      = LayerSpecs<PRS_LAYER_RES_MIN$>
									RDSTestRec<RDS_TEST_SPEC_RES_TARGET$>   = LayerSpecs<PRS_LAYER_RES_TARGET$>
									RDSTestRec<RDS_TEST_SPEC_RES_MAX$>      = LayerSpecs<PRS_LAYER_RES_MAX$>
									RDSTestRec<RDS_TEST_SPEC_RES_UNITS$>    = LayerSpecs<PRS_LAYER_RES_UNITS$>
									RDSTestRec<RDS_TEST_SPEC_CON_MIN$>      = LayerSpecs<PRS_LAYER_CONC_MIN$>
									RDSTestRec<RDS_TEST_SPEC_CON_TARGET$>   = LayerSpecs<PRS_LAYER_CONC_TARGET$>
									RDSTestRec<RDS_TEST_SPEC_CON_MAX$>      = LayerSpecs<PRS_LAYER_CONC_MAX$>
									RDSTestRec<RDS_TEST_SPEC_CON_UNITS$>    = LayerSpecs<PRS_LAYER_CONC_UNITS$>
									RDSTestRec<RDS_TEST_SPEC_STRESS_MIN$>   = LayerSpecs<PRS_LAYER_STRESS_MIN$>
									RDSTestRec<RDS_TEST_SPEC_STRESS_MAX$>   = LayerSpecs<PRS_LAYER_STRESS_MAX$>
									RDSTestRec<RDS_TEST_SPEC_TRANS$>        = LayerSpecs<PRS_LAYER_TRANS_SPEC$>
									RDSTestRec<RDS_TEST_SPEC_CRES_MIN$>     = LayerSpecs<PRS_LAYER_CRES_MIN$>
									RDSTestRec<RDS_TEST_SPEC_CRES_TARGET$>  = LayerSpecs<PRS_LAYER_CRES_TARGET$>
									RDSTestRec<RDS_TEST_SPEC_CRES_MAX$>     = LayerSpecs<PRS_LAYER_CRES_MAX$>
									RDSTestRec<RDS_TEST_SPEC_CRES_UNITS$>   = LayerSpecs<PRS_LAYER_CRES_UNITS$>

									For A = 11 to 19
										RDSTestRec<A> = LayerSpecs<PRS_LAYER_THICK_MEASUREMENT$,A-10>
									Next A
										
									For A = 24 to 32
										RDSTestRec<A> = LayerSpecs<PRS_LAYER_RES_MEASUREMENT$,A-23>
									Next A

									For A = 37 to 45
										RDSTestRec<A> = LayerSpecs<PRS_LAYER_CONC_MEASUREMENT$,A-36>
									Next A

									For A = 50 to 58
										RDSTestRec<A> = LayerSpecs<PRS_LAYER_STRESS_MEASUREMENT$,A-49>
									Next A

									For A = 121 to 129
										RDSTestRec<A> = LayerSpecs<PRS_LAYER_CRES_MEASUREMENT$,A-120>
									Next A

									RDSTestRec<RDS_TEST_SPEC_THICK_MPATTERN$>	= LayerSpecs<PRS_LAYER_THICK_MEASUREMENT$,PRS_MPATTERN$>
									RDSTestRec<RDS_TEST_SPEC_RES_MPATTERN$>		= LayerSpecs<PRS_LAYER_RES_MEASUREMENT$,PRS_MPATTERN$>
									RDSTestRec<RDS_TEST_SPEC_CON_MPATTERN$>		= LayerSpecs<PRS_LAYER_CONC_MEASUREMENT$,PRS_MPATTERN$>
									RDSTestRec<RDS_TEST_SPEC_STRESS_MPATTERN$>	= LayerSpecs<PRS_LAYER_STRESS_MEASUREMENT$,PRS_MPATTERN$>
									RDSTestRec<RDS_TEST_SPEC_CRES_MPATTERN$>	= LayerSpecs<PRS_LAYER_CRES_MEASUREMENT$,PRS_MPATTERN$>

									RDSTestRec<RDS_TEST_REACTOR_TYPE$> = ReactorType

									If ( (ReactorType EQ 'P') or (ReactorType EQ 'EPP') or (ThickFilmMet EQ True$) ) then
										SpecMap = XLATE('PROD_SPEC', PSNId, PROD_SPEC_TEST_POINT_MAP$, 'X')		;* Added 4/8/2009 JCH new field in PROD_SPEC
										If SpecMap NE '' then
											RDSTestRec<RDS_TEST_TEST_POINT_MAP$> = SpecMap
										end else
											;* Added ThickFilmMet check - 1/16/2009 JCH
											AllTargetThicks = XLATE('PROD_SPEC', PSNId, 'THICK_TARGET_ALL', 'X')
											TargetCnt = COUNT(AllTargetThicks,@VM) + (AllTargetThicks NE '')

											Begin Case
												Case TargetCnt = 2
													CombinedThick = SUM(AllTargetThicks)
													
												Case TargetCnt = 1 OR TargetCnt = 3
													CombinedThick = AllTargetThicks[-1,'B':@VM]
													
											End Case
											
											If OCONV(CombinedThick,'MD2') > '65.0' then
												RDSTestRec<RDS_TEST_TEST_POINT_MAP$> = 'FTIR_T'
											end else
												RDSTestRec<RDS_TEST_TEST_POINT_MAP$> = 'FTIR'
											end
										end
											
									end else
										RDSTestRec<RDS_TEST_TEST_POINT_MAP$> = 'ASM17'	;* 17 Point linear test pattern until PROD_SPEC is updated support other types
									end
									Database_Services('WriteDataRow', 'RDS_TEST', RdsTestKey, RdsTestRec)
									If Error_Services('HasError') then
										ErrorMsg = 'Error in ':Service:' service. Error message: ':Error_Services('GetMessage')
									end
								end else
									ErrorMsg = 'Error in ':Service:' service. Error message: ':Error_Services('GetMessage')
								end
							Next RdsTestKey
						end else
							ErrorMsg = 'Error in ':Service:' service. Error message: ':Error_Services('GetMessage')
						end
					end else
						ErrorMsg = 'Error in ':Service:' service. Error calling obj_Prod_Spec("GetLayerProp"). Error code: ':ErrCode
					end
				Next LayerKey					
			end else
				ErrorMsg = 'Error in ':Service:' service. Error message: ':Error_Services('GetMessage')
			end
    	end else
    		ErrorMsg = 'Error in ':Service:' service. RDS ':RdsNo:' does not exist'
    	end
    end else
    	ErrorMsg = 'Error in ':Service:' service. Null RDSNo passed into service'
    end
    
    If (ErrorMsg NE '') then Error_Services('Add', ErrorMsg)
		
End Service

