COMPILE FUNCTION obj_Reactor(Method,Parms)

/*
	Methods for Reactor table
    
	10/30/2005 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    SetReadings(Reactor,ReadBy,ReadDTM,ReadHrs,ReadWfrs)		;* Set last readings values
    TubeChange(Reactor,Date,ReactLogID)							;* Creates new Line item for Tube Change
    CloseOpenModes(Reactor)										;* Closes extraneous open REACT_MODE records
	
	
*/



DECLARE SUBROUTINE  Set_Status, Msg, obj_Tables, Send_Dyn, ErrMsg, Mona_Services
DECLARE FUNCTION Get_Status, Msg, Utility, obj_Tables, Environment_Services, GetTickCount


$INSERT REACTOR_EQUATES
$INSERT REACT_MODE_EQUATES
$INSERT REACT_STATE_EQUATES
$INSERT REACT_ITEM_EQUATES
$INSERT MSG_EQUATES
$insert REACTOR_CHILD_KEY_IDS_EQUATES
$Insert LOGICAL

IsProd = Environment_Services('IsProd')
If IsProd EQ True$ then
	MonaResource = 'GRP_OPENINSIGHT_MES_OP_FE_OBJREACTOR'
end else
	MonaResource = 'GRP_OPENINSIGHT_MES_OP_FE_DEV_OBJREACTOR'
end

ErrTitle = 'Error in Stored Procedure "obj_Reactor"'
ErrorMsg = ''

IF NOT(ASSIGNED(Method))	THEN ErrorMsg = 'Unassigned parameter "Method" passed to subroutine'
IF NOT(ASSIGNED(Parms))		THEN Parms = ''

IF ErrorMsg NE '' THEN
	Set_Status(-1,ErrTitle:@SVM:ErrorMsg)
	RETURN ''
END

Result = ''

BEGIN CASE
	CASE Method = 'CurrItem'		; GOSUB CurrItem
	CASE Method = 'CurrGraphite'	; GOSUB CurrGraphite
	CASE Method = 'SetLastRead'		; GOSUB SetLastRead
	CASE Method = 'TubeChange'		; GOSUB TubeChange
	CASE Method = 'Unload'			; GOSUB Unload
	CASE Method = 'CloseOpenModes'	; GOSUB CloseOpenModes
	
	CASE 1
		ErrorMsg = 'Unknown Method ':Method:' passed to object routine.'

END CASE

IF ErrorMsg NE '' THEN
	Set_Status(-1,ErrTitle:@SVM:ErrorMsg)
END

RETURN Result


* * * * * * *
CurrItem:
* * * * * * * 

StartTick = GetTickCount()
MetricName = 'CurrItem'

ReactNo 	= Parms[1,@RM]
ItemType	= Parms[COL2()+1,@RM]
ReactRec	= Parms[COL2()+1,@RM]

IF ReactNo = '' THEN RETURN
IF ItemType = '' THEN RETURN

IF ReactRec = '' THEN
	ReactRec = XLATE('REACTOR',ReactNo,'','X')
	IF ReactRec = '' THEN
		RETURN
	END
END

CurrInstItems = ReactRec<REACTOR_CURR_INST_ITEMS$>

ciCnt	= COUNT(CurrInstItems,@VM) + (CurrInstItems NE '')
RIKeys	= ''

FOR I = 1 TO ciCnt
	RIKeys<1,I> = FIELD(CurrInstItems<1,I>,'*',2)
NEXT I


CurrInstTypes = XLATE('REACT_ITEM',RIKeys,REACT_ITEM_RI_TYPE$,'X')

LOCATE ItemType IN CurrInstTypes USING @VM SETTING Pos THEN
	Result = FIELD(CurrInstItems<1,Pos>,'*',2)
END

EndTick = GetTickCount()
Mona_Services('QueueLatencyAndCountMetrics', MonaResource, MetricName, StartTick, EndTick)

RETURN


* * * * * * *
CurrGraphite:
* * * * * * *

StartTick = GetTickCount()
MetricName = 'CurrGraphite'

ReactNo	= Parms[1,@RM]
ReactRec	= Parms[COL2()+1,@RM]

IF ReactNo = '' THEN RETURN

IF ReactRec = '' THEN
	ReactRec = XLATE('REACTOR',ReactNo,'','X')
	IF ReactRec = '' THEN
		RETURN
	END
END

CurrInstItems = ReactRec<REACTOR_CURR_INST_ITEMS$>							;* These are React_Item_Hist keys

ciCnt	= COUNT(CurrInstItems,@VM) + (CurrInstItems NE '')
RIKeys	= ''

FOR I = 1 TO ciCnt
	RIKeys<1,I> = FIELD(CurrInstItems<1,I>,'*',2)
NEXT I

CurrInstTypes = XLATE('REACT_ITEM',RIKeys,REACT_ITEM_RI_TYPE$,'X')

ciCnt = COUNT(RIKeys,@VM) + (RIKeys NE '')

FOR I = 1 TO ciCnt
	CurrInstType = CurrInstTypes<1,I>
	IF CurrInstType = 'S' OR CurrInstType[1,1] = 'R' THEN
		Result<1,-1> = RIKeys<1,I>
	END
NEXT I

EndTick = GetTickCount()
Mona_Services('QueueLatencyAndCountMetrics', MonaResource, MetricName, StartTick, EndTick)

RETURN


* * * * * * *
SetLastRead:
* * * * * * *

* This is probably not used anymore JCH 12/11/2013  needs to be checked then chucked

ReactorNo	= Parms[1,@RM]
ReadTypes	= Parms[COL2()+1,@RM]
ReadDTMs	= Parms[COL2()+1,@RM]

IF ReactorNo = ''	THEN ErrorMsg = 'Null parameter "ReactorNo" passed to routine. (':Method:')'
IF ReadTypes = ''	THEN ErrorMsg = 'Null parameter "ReadType" passed to routine. (':Method:')'
IF ReadDTMs = ''	THEN ErrorMsg = 'Null parameter "ReadDTM" passed to routine. (':Method:')'

IF ErrorMsg NE '' THEN RETURN

ValidReadTypes = 'HRS':@VM:'WFRS'

ReadCnt = COUNT(ReadTypes,@VM) + (ReadTypes NE '')

thisReadDTMs = ''

FOR I = 1 TO ReadCnt
	ReadDTM = ReadDTMs<1,I>
	ReadType = ReadTypes<1,I>
	
	thisReadDTM = ICONV(ReadDTM,'DT')
	IF thisReadDTM = '' THEN
		ErrorMsg = 'Invalid ReadDTM ':QUOTE(ReadDTM):' passed to routine. (':Method:')'
		RETURN
	END ELSE
		thisReadDTMs<1,I> = thisReadDTM
	END
	
	LOCATE ReadType IN ValidReadTypes USING @VM SETTING Pos ELSE
		ErrorMsg = 'Invalid parameter "ReadType - "':QUOTE(ReadType):' passed to routine. (':Method:')'
		RETURN
	END
NEXT I

otParms = 'REACTOR':@RM:ReactorNo
ReactorRec = obj_Tables('ReadRec',otParms)

IF Get_Status(errCode) THEN
	RETURN
END

FOR I = 1 TO ReadCnt

	ReadType = ReadTypes<1,I>
	
	IF ReadType = 'HRS'		THEN ReactorRec<REACTOR_LAST_READ_HRS_DTM$> = thisReadDTMs<1,I>
	IF ReadType = 'WFRS'	THEN ReactorRec<REACTOR_LAST_READ_WFRS_DTM$> = thisReadDTMs<1,I>

	
NEXT I
	
otParms = FieldStore(OtParms,@RM,4,0,ReactorRec)	;* Put record in 4th field of OtParms
obj_Tables('WriteRec',otParms)

IF Get_Status(errCode) THEN
	ErrMsg(errCode)
END

RETURN

* * * * * *
CloseOpenModes:
* * * * * *

ReactorNo	= Parms[1,@RM]

IF ReactorNo = '' THEN RETURN

* OpenModes = XLATE('REACTOR',ReactorNo,REACTOR_CURR_MODE_KEY$,'X')
OpenModes   = Xlate('REACTOR_CHILD_KEY_IDS', ReactorNo, REACTOR_CHILD_KEY_IDS_REACT_MODE_KEY_IDS$, 'X')

IF NOT(INDEX(OpenModes,@VM,1)) THEN RETURN

DEBUG

FixCnt = COUNT(OpenModes,@VM) 	;* This is 1 less than the actual count!!!

OPEN 'REACT_MODE' TO ReactModeTable THEN

	FOR I = 1 TO FixCnt
		READ ClosingReactModeRec FROM ReactModeTable,OpenModes<1,I> THEN
			READ CloseByReactModeRec FROM ReactModeTable,OpenModes<1,I+1> THEN
				StopDTM		= FIELD(OpenModes<1,I+1>,'*',2)
				StopUser	= CloseByReactModeRec<REACT_MODE_START_USER$>
				ClosingReactModeRec<REACT_MODE_STOP_DTM$>	= StopDTM
				ClosingReactModeRec<REACT_MODE_STOP_USER$>	= StopUser
				
				WRITE ClosingReactModeRec ON ReactModeTable,OpenModes<1,I> THEN
					Send_Dyn('Closing ':OpenModes<1,I>:' with ':OpenModes<1,I+1>)
				END
					
			END	;* End of CloseBy Record Record
		END	;* End of Closing Record Read


	NEXT I
END






RETURN

* * * * * * *
TubeChange:
* * * * * * * 


RETURN



* * * * * * *
Unload:
* * * * * * * 



RETURN


