(:~
 : Report library.
 :
 : @author Rave Technologies, https://www.rave-tech.com/, 2017
 :)
module namespace 		rpsrv  	= 'http://www.rave-tech.com/bloomsbury/report-psrv';
import module namespace cpsrv  = 'http://www.rave-tech.com/bloomsbury/config-psrv'   at 'config-psrv.xqm';
import module namespace copsrv = 'http://www.rave-tech.com/bloomsbury/contents-psrv' at 'content-psrv.xqm';
declare namespace 		pipeline 	= 'http://cms.bloomsbury.com/pipeline';
(:~
 : Append error report to the specific job.
 : @param  $jobID ID of the job to update status
 : @param  $stepReport The step information to record
 : @return empty sequence
 :)
declare %updating function rpsrv:update(
	$jobID as xs:string,
	$stepReport as element(step)
)
{
	file:append(fn:concat($cpsrv:TempPipelineDir,$jobID,$cpsrv:ReadyToMove,$cpsrv:JobDir,$jobID,'.xml'),$stepReport)
};
(:~
 : Append final status of the report.
 : @param  $jobID ID of the job to update status
 : @param  $ignoreValidation Ignore validation if error happens
 : @param  $contentID ID of the content
 : @return empty sequence
 :)
declare %updating function rpsrv:update-final-status(
	$jobID as xs:string,
	$ignoreValidation as xs:string,
	$contentID as xs:string
)
{
	let $reportUri	:= fn:concat($cpsrv:TempPipelineDir,$jobID,$cpsrv:ReadyToMove,$cpsrv:JobDir,$jobID,'.xml')
	let $file 		:= file:read-text($reportUri)
	let $steps 		:= fn:substring-after($file,'')
	let $jobText	:= fn:parse-xml(fn:substring-before($file,$steps))
	let $jobXml		:=  copy $c :=  $jobText
						modify replace node $c/job/ingestion-report/steps with fn:parse-xml(fn:concat('',fn:normalize-space($steps),''))
						return $c
	return
		if($jobXml/job/ingestion-report/status[.='InProgress'] or $jobXml/job/ingestion-report/status[.=''])
		then
		(
			if($jobXml/job/ingestion-report/steps/step/error/*)
			then
				if($ignoreValidation='true')
				then
					let $jobXml	:=  copy $c :=  $jobXml
									modify
									(
										replace node $c/job/ingestion-report/status with SuccessWithWarnings,
										replace node $c/job/ingestion-report/finished-on with {fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}
									)
									return $c
					return file:write($reportUri,$jobXml)
				else
				(
					let $jobXml	:=  copy $c :=  $jobXml
									modify
									(
										replace node $c/job/ingestion-report/status with Failure,
										replace node $c/job/ingestion-report/finished-on with {fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))},
										 for $d in $c/job/job-info/lock/file
										 return
								         replace node $d/latest-version with {$d/previous-version/string()}
									)
									return $c
					return file:write($reportUri,$jobXml)
					,
					file:delete(fn:concat($cpsrv:TempPipelineDir,$jobID,$cpsrv:ReadyToMove,$cpsrv:ContentDir),fn:true())
				)
			else
					let $jobXml	:=  copy $c :=  $jobXml
									modify
									(
										replace node $c/job/ingestion-report/status with Success,
										replace node $c/job/ingestion-report/finished-on with {fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}
									)
									return $c
					return file:write($reportUri,$jobXml)
		)
		else ()
};