Option Explicit

'*****************************************************
'UIS Sample script to create new JOA


'*****************************************************

Dim strError
Dim strToken
Dim strURL


strToken = "NONWORKINGTOKEN0000000000+111111111122222222223333333333444444444455555555556666666666ABCDE/ABC="
strURL = "https://uis.usajobs.gov/USAJOBSIntegrationService/IntegrationServices.svc"

If Not ProcessPositionOpening(strError, strToken) Then
	WScript.Echo strError
Else	
    WScript.Echo "Acknowlegment saved. Please review."
End If

Function ProcessPositionOpening(strError, strToken)

	'Declare variables
	Dim objXML
	Dim nodTemp
	Dim objHTTP

	'This can be cached or read from the file each time
	Set objXML = CreateObject("MSXML2.DOMDocument.4.0")
	If Not objXML.load("JOA_Sample.xml") Then
		strError = "Fatal Error Loading JOA_Sample.xml " & objXML.parseError.reason 
		ProcessPositionOpening = False
		Exit Function
	End If

    'Set namespaces for UIS and HR-XML
    dim namespaces 
    namespaces = "xmlns:s='http://schemas.xmlsoap.org/soap/envelope/' xmlns:t='http://uis.usajobs.gov' xmlns:h='http://uis.usajobs.gov' xmlns:oa='http://www.openapplications.org/oagis/9' xmlns:hrxml='http://www.hr-xml.org/3'" 
	objXML.setProperty "SelectionNamespaces", namespaces

    'Update JOA sample with current data to create a new JOA
	objXML.documentElement.selectSingleNode("s:Header/h:USAJOBSHeader/h:AuthenticationHeader/h:USAJAuthenticateToken").text = strToken
    objXML.documentElement.selectSingleNode("s:Body/t:ProcessPositionOpening/oa:ApplicationArea/oa:BODID").text = "123456789"
    objXML.documentElement.selectSingleNode("s:Body/t:ProcessPositionOpening/oa:ApplicationArea/oa:CreationDateTime").text = Now  
    objXML.documentElement.selectSingleNode("s:Body/t:ProcessPositionOpening/hrxml:DataArea/oa:Process/oa:ActionCriteria/oa:ActionExpression/@actionCode").text = "Add"
 
        
	'Webservice call to UIS to process JOA Sample
	Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP.4.0")
	objHTTP.Open "POST", "http://localhost:2373/IntegrationServices.svc"
	objHTTP.setRequestHeader "Content-Type", "text/xml"
	objHTTP.setRequestHeader "SOAPAction", " http://uis.usajobs.gov/IProcessPositionOpening/ProcessPositionOpening"	
	objHTTP.Send objXML


	'Get the response back
	If Not objXML.load(objHTTP.ResponseXML) Then
		ProcessPositionOpening = False
		strError = "Fatal Error loading XML response " & objXML.parseError.reason & " - " & objHTTP.ResponseText
		Exit Function
	End If
      
    objXML.save("Acknowledgement.xml")
	
    ProcessPositionOpening = True

	Set objXML = Nothing
	Set nodTemp = Nothing

End Function
