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

    Name        :   Config_Services

    Description :   Handler program for all Config 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)
        01/05/20    djs     Original programmer.
														

***********************************************************************************************************************/
#pragma precomp SRP_PreCompiler
$insert LOGICAL

Declare function   Database_Services, SRP_JSON, Error_Services
Declare subroutine SRP_JSON, Error_Services, Database_Services

GoToService

Return Response or ""

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

Service GetOEE(itemURL)
    
    If SRP_JSON(objJSON, 'New', 'Object') then
        If SRP_JSON(objOEE, 'New', 'Object') then
            ReactTypes     = 'ASM':@VM:'ASM+':@VM:'HTR':@VM:'EPP'
            ReactTypeNames = 'ASMOEE':@VM:'ASMPlusOEE':@VM:'HTROEE':@VM:'EpiProOEE'
            For each ReactType in ReactTypes using @VM setting vPos
                OEE = Database_Services('ReadDataRow', 'APP_INFO', 'OEE*':ReactType)
                If Error_Services('NoError') then
                    SRP_JSON(objOEE, 'SetValue', ReactTypeNames<0, vPos>, OEE)
                end
            Next ReactType
            SRP_JSON(objJSON,  'Set', 'OEE', objOEE)
            SRP_JSON(objOEE, 'Release')
        end else
            Error_Services('Add', 'Unable to create JSON representation in the ' : Service : ' service.')
        end

        If itemURL NE '' then
            // The itemURL was passed in so add HAL+JSON properties.
            // Create the _links property and then all link objects needed for this resource.
            If SRP_JSON(objLinks, 'New', 'Object') then
                // Create a self link.
                If SRP_JSON(objLink, 'New', 'Object') then
                    SRP_JSON(objLink, 'SetValue', 'href', ItemURL, 'String')
                    SRP_JSON(objLink, 'SetValue', 'title', 'Self', 'String')
                    SRP_JSON(objLinks, 'Set', 'self', objLink)
                    SRP_JSON(objLink, 'Release')
                end
                SRP_JSON(objJSON, 'Set', '_links', objLinks)
                SRP_JSON(objLinks, 'Release')
            end
            // Create the _class property for this resource.
            SRP_JSON(objJSON, 'SetValue', '_class', 'resource')
        end 
        jsonRecord = SRP_JSON(objJSON, 'Stringify', 'Styled')
        SRP_JSON(objJSON, 'Release')
        Response   = jsonRecord
    end else
        Error_Services('Add', 'Unable to create JSON representation in the ' : Service : ' service.')
    end
    
End Service


Service UpdateOEE(OEEjson)
    
    If OEEjson NE '' then
        If SRP_JSON(objJSON, 'Parse', OEEjson) EQ '' then
            objOEE         = SRP_JSON(objJSON, 'Get', 'OEE')
            ReactTypes     = 'ASM':@VM:'ASM+':@VM:'HTR':@VM:'EPP'
            ReactTypeNames = 'ASMOEE':@VM:'ASMPlusOEE':@VM:'HTROEE':@VM:'EpiProOEE'
            For each ReactType in ReactTypes using @VM setting vPos
                OEEKey = 'OEE*':ReactType
                OEEVal = SRP_JSON(objOEE, 'GetValue', ReactTypeNames<0, vPos>)
                Database_Services('WriteDataRow', 'APP_INFO', OEEKey, OEEVal)
            While Error_Services('NoError')
            Next ReactType
            SRP_JSON(objOEE, 'Release')
            SRP_JSON(objJSON, 'Release')
        end else
            Error_Services('Add', 'Error parsing OEEjson in ':Service:' service.')
        end
    end else
        Error_Services('Add', 'Error in ':Service:' service. Null OEEjson passed in.')
    end
    
end service


Service GetMaterialTrackSettings(UserId)
	
	Response = ''
	If (UserId NE '') then
		RepUserId = UserId
		Convert @Lower.Case to @Upper.Case in RepUserId
		If RowExists('REPORT_CONFIG', 'MATERIAL_TRACK_WEB*':RepUserId) then
			Response  = Database_Services('ReadDataRow', 'REPORT_CONFIG', 'MATERIAL_TRACK_WEB*':RepUserId)
		end else
			Response  = Database_Services('ReadDataRow', 'REPORT_CONFIG', 'MATERIAL_TRACK_WEB*DEFAULT')
		end
	end else
		Response  = Database_Services('ReadDataRow', 'REPORT_CONFIG', 'MATERIAL_TRACK_WEB*DEFAULT')
	end
	
end service


Service UpdateMaterialTrackSettings(MtSettingsJson, UserId)
	
	If ( (MtSettingsJson NE '') and (UserId NE '') ) then
		RepUserId = UserId
		Convert @Lower.Case to @Upper.Case in RepUserId
		Database_Services('WriteDataRow', 'REPORT_CONFIG', 'MATERIAL_TRACK_WEB*':RepUserId, MtSettingsJson)	
	end else
		Error_Services('Add', 'Error in ':Service:' service. Null MtSettingsJson or UserId passed into service.')
	end
	
end service


