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

    Name        :   PM_Spec_Services

    Description :   Handler program for all PM_SPEC 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)
        03/29/24    djs     Original programmer.

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

$Insert APP_INSERTS
$Insert SERVICE_SETUP
$Insert PM_EQUATES
$Insert PM_SPEC_EQUATES
$Insert TOOL_EQUATES

// Reduce Modes
Equ NEW_EXIST$ To 0 
Equ NEXT_CUR$  To 1
Equ ADD_EXIST$ To 2

Declare subroutine Error_Services, Reduce, Push.Select, Pop.Select

GoToService

Return Response or ""

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Service Parameter Options
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Options BOOLEAN     = True$, False$

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

Service UpdatePMCompDtm(PMSpecID)
    
    F1       = ''
    F2       = ''
    F3       = ''
    F4       = ''
    Push.Select(F1, F2, F3, F4)
    ErrorMsg = ''
    If (PMSpecID NE '') then
        If RowExists('PM_SPEC', PMSpecID) then
            TableName    = "PM"
            Flag         = ""
            Done         = False$
            CursorVar    = ""
            GoSub ClearCursors
            SortList     = "#COMP_DTM"
            ReduceScript = "WITH {PMS_ID} EQ ":PMSpecID
            Mode         = NEXT_CUR$
            Reduce(ReduceScript, SortList, Mode, TableName, CursorVar, Flag)
            If Flag then
                Select TableName By SortList Using CursorVar then
                    Open TableName To FileVar then
                        Loop
                            ReadNext Key Using CursorVar By AT else Done = TRUE$
                        Until Done
                            Read Rec From FileVar, Key then
                                NewStopDtm = Rec<PM_COMP_DTM$>
                                Done       = True$
                                Open 'PM_SPEC' to hPMSpec then
                                    WriteV NewStopDtm on hPMSpec, PMSpecID, PM_SPEC_LAST_PM_COMP_DTM$ else
                                        ErrorMsg = 'Error in ':Service:' service. Error writing LAST_PM_COMP_DTM on PM_SPEC record ':PMSpecID:'.'
                                    end
                                end else
                                    ErrorMsg = 'Error in ':Service:' service. Error opening PM_SPEC table.'
                                end
                            end else
                                ErrorMsg = 'Error in ':Service:' service. Error reading PM record ':Key:'.'
                            end
                        Repeat
                    end else
                        ErrorMsg = 'Error in ':Service:' service. Error opening PM table.'
                    end
                end else
                    ErrorMsg = 'Error in ':Service:' service. Error calling Select on PM table.'
                end
            end else
                ErrorMsg = 'Error in ':Service:' service. Error calling Reduce on PM table.'
            end
        end else
            ErrorMsg = 'Error in ':Service:' service. PM_SPEC record ':PMSpecID:' does not exist!'
        end
    end else
        ErrorMsg = 'Error in ':Service:' service. Null PMSpecID passed into service!'
    end
    
    Pop.Select(F1, F2, F3, F4)
    If ErrorMsg NE '' then
        Error_Services('Add', ErrorMsg)
        Response = False$
    end else
        Repsponse = True$
    end
    
end service


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Internal GoSubs
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

ClearCursors:
    
    For counter = 0 To 8
        ClearSelect counter
    Next counter
    
return


