Function MCP(ServiceHandler, Service, Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, Param9, Param10)

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

    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        :   MCP (Master Controller Program)

    Description :   Controller program for the application.

    Notes       :   In an MVC framework this is the 'Controller' routine that accepts requests from Views and routes
                    them to the appropriate 'Models' as needed.

    Parameters  :
        ServiceHandler      [in] -- Name of the service handler
        Service             [in] -- Name of the service being requested
        Error              [out] -- Any errors that were created
        Param1-10       [in/out] -- Additional request parameter holders
        Response           [out] -- Response to be sent back to the requesting View

    History     :   (Date, Initials, Notes)
        04/26/10    dmb     Original programmer.
        01/25/13    dmb     Update calling signature by removing the Error argument. Implement Error_Services.
                            - [SRPFW-6]

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

Common /MCP/ ServiceHandlerList@

$insert LOGICAL
$insert MCP_SETUP

Begin Case
    Case Len(ServiceHandler)            ;   GoSub Call_Service_Handler
    Case Otherwise$                     ;   Error_Services('Set', 'No service handler was specified')
End Case

If Assigned(Response) else Response = ''

Return Response


Call_Service_Handler:

    // This is a call to a Linear Hash model. If another model is being used (e.g. U2 database server) then another
    // means, or API, of calling the business logic within that domain will need to be used.
    Convert @Lower_Case to @Upper_Case in ServiceHandler

    // Pass the service request to the service handler if it exists.
    // Note: It is critical that handler routine be named in this format: ServiceHandler_SERVICES
    IsServiceModule = True$ ; // Assume this is a service module if found.
    ObjExists       = No$   ; // Assume the object code for the service handler does not exist for now.
    NumApps         = Count(@APPID, @FM) + (@APPID NE '')
    For AppCnt  = 1 to NumApps
        AppID = @APPID<AppCnt>
        If AppID _EQC 'SYSPROG' then
            SysObjKey = '$' : ServiceHandler : '_SERVICES'
        end else
            SysObjKey = '$' : ServiceHandler : '_SERVICES' : '*' : @APPID<AppCnt>
        end
        ObjExists = (Index(ServiceHandlerList@, SysObjKey, 1) GT 0)
    Until ObjExists
    Next AppCnt

    If Not(ObjExists) then
        For AppCnt = 1 to NumApps
            AppID = @APPID<AppCnt>
            If AppID _EQC 'SYSPROG' then
                SysObjKey = '$' : ServiceHandler : '_SERVICES'
            end else
                SysObjKey = '$' : ServiceHandler : '_SERVICES' : '*' : @APPID<AppCnt>
            end
            ObjExists = (Xlate("SYSOBJ", SysObjKey, 0, "X") GT "")
        Until ObjExists
        Next AppCnt
        If (ObjExists) then ServiceHandlerList@ := SysObjKey : @FM
    end

    If Not(ObjExists) then
        // The procedure might not be written as a service module. Check for this condition.
        IsServiceModule = False$    ; // Assume this is not a service module if found.
        NumApps         = Count(@APPID, @FM) + (@APPID NE '')
        For AppCnt  = 1 to NumApps
            AppID = @APPID<AppCnt>
            If AppID _EQC 'SYSPROG' then
                SysObjKey = '$' : ServiceHandler
            end else
                SysObjKey = '$' : ServiceHandler : '*' : @APPID<AppCnt>
            end
            ObjExists = (Index(ServiceHandlerList@, SysObjKey, 1) GT 0)
        Until ObjExists
        Next AppCnt
    end

    If Not(ObjExists) then
        For AppCnt = 1 to NumApps
            AppID = @APPID<AppCnt>
            If AppID _EQC 'SYSPROG' then
                SysObjKey = '$' : ServiceHandler
            end else
                SysObjKey = '$' : ServiceHandler : '*' : @APPID<AppCnt>
            end
            ObjExists = (Xlate("SYSOBJ", SysObjKey, 0, "X") GT "")
        Until ObjExists
        Next AppCnt
        If (ObjExists) then ServiceHandlerList@ := SysObjKey : @FM
    end

    If (ObjExists) then
        If IsServiceModule then
            Handler = ServiceHandler : '_SERVICES'
        end else
            Handler = ServiceHandler
        end
        // Reload the procedure to get the most current version running.
        RTP27(Handler)
        // Call the procedure using the appropriate number of arguments.
        Begin Case
            Case Param10 NE ''  ; NumParams = 10
            Case Param9 NE ''   ; NumParams = 9
            Case Param8 NE ''   ; NumParams = 8
            Case Param7 NE ''   ; NumParams = 7
            Case Param6 NE ''   ; NumParams = 6
            Case Param5 NE ''   ; NumParams = 5
            Case Param4 NE ''   ; NumParams = 4
            Case Param3 NE ''   ; NumParams = 3
            Case Param2 NE ''   ; NumParams = 2
            Case Param1 NE ''   ; NumParams = 1
            Case Otherwise$     ; NumParams = 0
        End Case

        Begin Case
            Case NumParams EQ 0
                Response    = Function(@Handler(Service))
            Case NumParams EQ 1
                Response    = Function(@Handler(Service, Param1))
            Case NumParams EQ 2
                Response    = Function(@Handler(Service, Param1, Param2))
            Case NumParams EQ 3
                Response    = Function(@Handler(Service, Param1, Param2, Param3))
            Case NumParams EQ 4
                Response    = Function(@Handler(Service, Param1, Param2, Param3, Param4))
            Case NumParams EQ 5
                Response    = Function(@Handler(Service, Param1, Param2, Param3, Param4, Param5))
            Case NumParams EQ 6
                Response    = Function(@Handler(Service, Param1, Param2, Param3, Param4, Param5, Param6))
            Case NumParams EQ 7
                Response    = Function(@Handler(Service, Param1, Param2, Param3, Param4, Param5, Param6, Param7))
            Case NumParams EQ 8
                Response    = Function(@Handler(Service, Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8))
            Case NumParams EQ 9
                Response    = Function(@Handler(Service, Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, Param9))
            Case Otherwise$
                Response    = Function(@Handler(Service, Param1, Param2, Param3, Param4, Param5, Param6, Param7, Param8, Param9, Param10))
        End Case
    end else
        Error_Services('Set', ServiceHandler : '_SERVICES does not exist')
    end

return
