Compile function REACTOR_MODES_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        :   Reactor_Modes_Services

    Description :   Handler program for all module related services.

    Notes       :   The generic parameters should contain all the necessary information to process the services. Often
                    this will be information like the data Record and Key ID.

    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)
        11/13/2024    djs     Added AddExistingServiceCategory

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

$Insert LOGICAL
$Insert SERVICE_SETUP
$Insert REACTOR_MODES_EQUATES
$Insert REACT_PROB_CAT_EQUATES
$Insert REACTOR_EQUATES
$Insert RLIST_EQUATES

Declare function   Database_Services, React_Prob_Cat_Services, Reactor_Modes_Services, Memberof, Error_Services
Declare function   Nica_Orders_Services, Reactor_Services
Declare subroutine Error_Services, Database_Services, delete, RList, Reactor_Services

GoToService

Return Response or ""

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

Service AddNotificationGroup(ReactorModesKey, Class)
	
    If ReactorModesKey NE '' AND Class NE '' then
        ReactorModesRec = Database_Services('ReadDataRow', 'REACTOR_MODES', ReactorModesKey)
        Locate Class in ReactorModesRec<REACTOR_MODES_NOTIFY_CLASS$> using @VM setting dummy then
            Error_Services('Add', 'Error in REACTOR_MODES_SERVICES ' : Service : ': Notifcation already exists')
        end else
            ReactorModesRec<REACTOR_MODES_NOTIFY_CLASS$, -1> = Class
            Database_Services('WriteDataRow','REACTOR_MODES',ReactorModesKey,ReactorModesRec,1,0,1)
            if Error_Services('HasError') then
                Error_Services('Add', 'Error in REACTOR_MODES_SERVICES ' : Service : ': ' : Error_Services('GetMessage'))
            end
        end
    end else
        Error_Services('Add', 'Error in REACTOR_MODES_SERVICES: Null parameter ReactorModesKey or Class ID.')
        Return
    end
    
end service


Service DeleteNotificationGroup(ReactorModesKey, Class)
	
    If ReactorModesKey NE '' AND Class NE '' then

        ReactorModesRec = Database_Services('ReadDataRow', 'REACTOR_MODES', ReactorModesKey,1,0,1)
        Locate Class in ReactorModesRec<REACTOR_MODES_NOTIFY_CLASS$> using @VM setting classPos then
            ReactorModesRec = Delete(ReactorModesRec,REACTOR_MODES_NOTIFY_CLASS$,classPos,0)
            Database_Services('WriteDataRow', 'REACTOR_MODES',ReactorModesKey,ReactorModesRec)
        end else
            Error_Services('Add', 'Error in REACTOR_MODES_SERVICES: Null parameter ReactorModesKey or Class ID.')
        end
    end
    
end service


Service AddEnterPermission(ReactorModesKey, Group)
	
    If ReactorModesKey NE '' AND Group NE '' then
        ReactorModesRec                                  = Database_Services('ReadDataRow', 'REACTOR_MODES', ReactorModesKey)
        Locate Group in ReactorModesRec<REACTOR_MODES_GROUPS_ENTER$> using @VM setting dummy then
            Error_Services('Add', 'Error in REACTOR_MODES_SERVICES ' : Service : ': Group permission already exists')
        end else
            ReactorModesRec<REACTOR_MODES_GROUPS_ENTER$, -1> = Group
            Database_Services('WriteDataRow','REACTOR_MODES',ReactorModesKey,ReactorModesRec,1,0,1)
            if Error_Services('HasError') then
                Error_Services('Add', 'Error in REACTOR_MODES_SERVICES ' : Service : ': ' : Error_Services('GetMessage'))
            end
        end
    end else
        Error_Services('Add', 'Error in REACTOR_MODES_SERVICES: Null parameter ReactorModesKey or Group ID.')
        Return
    end
    
end service


Service AddReleasePermission(ReactorModesKey, Group)
	
    If ReactorModesKey NE '' AND Group NE '' then
        ReactorModesRec = Database_Services('ReadDataRow', 'REACTOR_MODES', ReactorModesKey)
        Locate Group in ReactorModesRec<REACTOR_MODES_GROUPS_RELEASE$> using @VM setting dummy then
            Error_Services('Add', 'Error in REACTOR_MODES_SERVICES ' : Service : ': Group permission already exists')
        end else
            ReactorModesRec<REACTOR_MODES_GROUPS_RELEASE$, -1> = Group
            Database_Services('WriteDataRow','REACTOR_MODES',ReactorModesKey,ReactorModesRec,1,0,1)
            if Error_Services('HasError') then
                Error_Services('Add', 'Error in REACTOR_MODES_SERVICES ' : Service : ': ' : Error_Services('GetMessage'))
            end
        end
    end else
        Error_Services('Add', 'Error in REACTOR_MODES_SERVICES: Null parameter ReactorModesKey or Group ID.')
        Return
    end
    
end service


Service AddServiceCategory(ReactorModesKey, NewServiceName, NewServiceCategory)
	
    If ReactorModesKey EQ '' then
        Error_Services('Add', 'Error in REACTOR_MODES_SERVICES: Null parameter ReactorModesKey.')
        Return
    end
    
    ReactorModesRec = Database_Services('ReadDataRow', 'REACTOR_MODES', ReactorModesKey)
    NewCatKey = React_Prob_Cat_Services('Create',NewServiceName,NewServiceCategory)
    ReactorModesRec<REACTOR_MODES_SERVICE_CATEGORIES$, -1> = NewServiceName
    ReactorModesRec<REACTOR_MODES_SERVICE_CATEGORY_ID$, -1> = NewCatKey
    
    Database_Services('WriteDataRow','REACTOR_MODES',ReactorModesKey,ReactorModesRec,1,0,1)
    
End Service


Service AddExistingServiceCategory(ReactorModesKey, ServCatId)
	
    If ReactorModesKey NE '' then
		ReactorModesRec = Database_Services('ReadDataRow', 'REACTOR_MODES', ReactorModesKey)
		If Error_Services('NoError') then
			If RowExists('REACT_PROB_CAT', ServCatId) then
				ServName = Xlate('REACT_PROB_CAT', ServCatId, REACT_PROB_CAT_REACTOR_PROBLEM_CATEGORY$, 'X')
				CurrCats = ReactorModesRec<REACTOR_MODES_SERVICE_CATEGORY_ID$>
				Locate ServCatId in CurrCats using @VM setting vPos else
					ReactorModesRec<REACTOR_MODES_SERVICE_CATEGORIES$, -1>  = ServName
					ReactorModesRec<REACTOR_MODES_SERVICE_CATEGORY_ID$, -1> = ServCatId
					Database_Services('WriteDataRow', 'REACTOR_MODES', ReactorModesKey, ReactorModesRec, True$, False$, True$)					
				end
			end else
				Error_Services('Add', 'Error in ':Service:' service. REACT_PROB_CAT ':ServCatId:' does not exist!')
			end
		end
    end else
    	Error_Services('Add', 'Error in REACTOR_MODES_SERVICES: Null parameter ReactorModesKey.')
    end
    
End Service


Service DeleteServiceCategory(ReactorModesKey, CatId)
	
    ReactorModesRec = Database_Services('ReadDataRow', 'REACTOR_MODES', ReactorModesKey,1,0,1)
    Locate CatId in ReactorModesRec<REACTOR_MODES_SERVICE_CATEGORY_ID$> using @VM setting catPos then
        ReactorModesRec = Delete(ReactorModesRec,REACTOR_MODES_SERVICE_CATEGORY_ID$,catPos,0)
        ReactorModesRec = Delete(ReactorModesRec,REACTOR_MODES_SERVICE_CATEGORIES$,catPos,0)
    end
    Database_Services('WriteDataRow', 'REACTOR_MODES',ReactorModesKey,ReactorModesRec)
    
end service


Service DeleteEnterPermission(ReactorModesKey, Group)
	
    ReactorModesRec = Database_Services('ReadDataRow', 'REACTOR_MODES', ReactorModesKey,1,0,1)
    Locate Group in ReactorModesRec<REACTOR_MODES_GROUPS_ENTER$> using @VM setting groupPos then
        ReactorModesRec = Delete(ReactorModesRec,REACTOR_MODES_GROUPS_ENTER$,groupPos,0)
    end
    Database_Services('WriteDataRow', 'REACTOR_MODES',ReactorModesKey,ReactorModesRec)
    
end service


Service DeleteReleasePermission(ReactorModesKey, Group)
	
    ReactorModesRec = Database_Services('ReadDataRow', 'REACTOR_MODES', ReactorModesKey,1,0,1)
    Locate Group in ReactorModesRec<REACTOR_MODES_GROUPS_RELEASE$> using @VM setting groupPos then
        ReactorModesRec = Delete(ReactorModesRec,REACTOR_MODES_GROUPS_RELEASE$,groupPos,0)
    end
    Database_Services('WriteDataRow', 'REACTOR_MODES',ReactorModesKey,ReactorModesRec)
    
end service


Service GetAllModes()
	
    Statement = 'SELECT REACTOR_MODES BY DESCRIPTION'
    rlist( Statement, target_activelist$, '', '', '' )
    ModeList = ''
    IF @RecCount then
        EoF = 0
        NumKeys = @RecCount
        Cnt = 0
        Loop
            ReadNext ReactorModeID Else EoF = 1
        until EoF
            ModeList<-1> = ReactorModeID
        Repeat
    end
    Response = ModeList
    
end service


Service AvailableModes(ReactorModesKey, CurrUser, ReactNo)
	
    CurrMode            = ReactorModesKey
    AllowedModes        = ''
    CurModeReleaseGroups = XLATE('REACTOR_MODES', ReactorModesKey, REACTOR_MODES_GROUPS_RELEASE$, 'X')
    CanRelease          = 0
    
    IdleStartupRequired = Reactor_Services('GetIdleStartupRequired', ReactNo)
    
    // Check can release from current mode
    for each ReleaseGroup in CurModeReleaseGroups using @VM
        if Memberof(CurrUser, ReleaseGroup) then
            CanRelease = 1
        end
    Next ReleaseGroup
    if CanRelease then
    	PossibleModes = ''
    	CurModeReleaseModes = XLATE('REACTOR_MODES', ReactorModesKey, REACTOR_MODES_MODES_RELEASE$, 'X')
    	If CurModeReleaseModes NE '' then
    		for each ReleaseMode in CurModeReleaseModes using @VM
    			Locate ReleaseMode in PossibleModes using @FM setting DUMMY else
    				 PossibleModes<-1> = ReleaseMode
    			end
    		Next ReleaseMode
    	end else
    		AllModes = Reactor_Modes_Services('GetAllModes')
    		for each mode in AllModes using @FM
				modeEnterPerms = XLATE('REACTOR_MODES', mode, REACTOR_MODES_GROUPS_ENTER$, 'X')
				x = 1
				loop 
				while x LE DCount(modeEnterPerms, @VM)
					If Memberof(currUser, modeEnterPerms<1,x>) then
						Locate mode in AllowedModes using @FM setting DUMMY else
							PossibleModes<-1> = mode
						end
					end
					
					x += 1
					 
				repeat
				
			Next mode
    	end

    	For each PossibleMode in PossibleModes using @FM
    		AllowMode = True$
    		
    		If PossibleMode EQ 'IDLE_STARTUP' and IdleStartupRequired EQ False$ then
    			AllowMode = False$
    		end
    		If PossibleMode EQ CurrMode then
    			AllowMode = False$
    		end
    		If AllowMode EQ True$ then
    			Locate PossibleMode in AllowedModes using @FM setting DUMMY else
					PossibleModeEnterModes = XLATE('REACTOR_MODES', PossibleMode, REACTOR_MODES_MODES_ENTER$, 'X')
					if PossibleModeEnterModes NE '' then
						for each EnterMode in PossibleModeEnterModes using @VM
							If CurrMode _EQC EnterMode then
								AllowedModes<-1> = PossibleMode
							end else
								If IdleStartupRequired EQ True$ and PossibleMode EQ 'IDLE' then
									AllowedModes<-1> = PossibleMode
								end
							end
						Next EnterMode
					end else
						AllowedModes<-1> = PossibleMode
					end
				end
    		end	
    	Next PossibleMode
    end
    Response = AllowedModes
    
end service

