psvr/jobs-psrv.xqm

307 lines
13 KiB
Plaintext

(:~
: Job Library.
:
: @author Rave Technologies, https://www.rave-tech.com/, 2017
:)
module namespace jpsrv = 'http://www.rave-tech.com/bloomsbury/jobs-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';
import module namespace apsrv = 'http://www.rave-tech.com/bloomsbury/audit-psrv' at 'audit-psrv.xqm';
import module namespace rpsrv = 'http://www.rave-tech.com/bloomsbury/report-psrv' at 'report-psrv.xqm';
declare namespace product = 'http://cms.bloomsbury.com/product-manifest';
declare namespace pipeline = 'http://cms.bloomsbury.com/pipeline';
(:~
: Run Jobs
: @param $body <job><ingest><area>Professional</area><ctype>looseleaf</ctype><ignore-validation>true</ignore-validation><mail>true</mail><path>D:\Backup\Projects\bloomsbury\SharedData\Steve\word2xml\small66.zip</path></ingest></job>
: @return empty sequence
:)
declare %updating function jpsrv:run-job(
$body as document-node()
)
{
admin:write-log('=============process starts================')
,
if($body/job/job-info/id)
then
let $jobLocation := fn:concat($cpsrv:TempPipelineDir,'/',$body/job/job-info/id)
let $jobType := $body/job/job-info/job-type/text()
return
if(($jobType='Ingest') or ($jobType='Checkin') or ($jobType='Publish'))
then
if((file:exists($jobLocation)) and ($jobType='Ingest'))
then ()
else jpsrv:jobs($body)
else ()
else admin:write-log('=============Invalid Job ID================')
};
(:~
: To ingest a content
: @param $body Job Chunk
: @header $authorization Authorization key
: @return element(result)
:)
declare %updating function jpsrv:jobs(
$body as document-node()
)
{
try
{ (:let $DataServer := 'http://10.102.200.111:8984/rest/':)
let $DataServer := 'http://172.20.2.16:8984/rest/'
let $area := $body/job/job-info/area/text()
let $ctype := $body/job/job-info/content-type/text()
let $ignoreValidation := $body/job/job-info/ignore-validation/text()
let $sendMail := $body/job/job-info/mail-required/text()
let $contentPath := fn:replace($body/job/job-info/uploaded-file/text(),'\\','/')
let $sessionValue := fn:concat($body/job/job-info/requester-id/text(),'$$$$',$body/job/job-info/requester-email/text())
let $UploadFileName := fn:tokenize($contentPath,'\\')[fn:last()]
let $jobID := $body/job/job-info/id/text()
let $cid := $body/job/job-info/cid/text()
let $createJobLocation := file:create-dir(fn:concat($cpsrv:TempPipelineDir,$jobID,$cpsrv:ReadyToMove))
let $cformat := $body/job/job-info/content-format/text()
let $pid := $body/job/job-info/pipeline-id/text()
let $jobType := $body/job/job-info/job-type/text()
let $lockInfo := if($body/job/job-info/lock)
then $body/job/job-info/lock
else <lock/>
return
if(($jobType='Ingest') or ($jobType='Checkin'))
then
if($cid)
then
(
let $reportPath := fn:concat($cpsrv:TempPipelineDir,$jobID,$cpsrv:ReadyToMove,$cpsrv:JobDir)
let $createDir := file:create-dir($reportPath)
return file:write(fn:concat($reportPath,$jobID,'.xml'),$body)
,
if($cformat='zip')
then copsrv:upload-zip($cid,$pid,$contentPath,$jobID,$ctype,$ignoreValidation,$area,$sessionValue,'zip',$jobType,$lockInfo)
else
if($cformat='xml')
then copsrv:upload-xml($cid,$pid,$contentPath,$jobID,$ctype,$ignoreValidation,$area,$sessionValue,'xml',$jobType)
else
if($cformat='docx')
then copsrv:upload-docx($cid,$pid,$contentPath,$jobID,$ctype,$ignoreValidation,$area,$sessionValue,'docx',$jobType)
else ()
)
else admin:write-log('************************ System error ************************')
else
if($jobType='Publish')
then
(
let $reportPath := fn:concat($cpsrv:TempPipelineDir,$jobID,$cpsrv:ReadyToMove,$cpsrv:JobDir)
let $createDir := file:create-dir($reportPath)
return file:write(fn:concat($reportPath,$jobID,'.xml'),$body)
,
let $jobTempDir := fn:concat($cpsrv:TempPipelineDir,$jobID)
let $productID := $body/job/job-info/product-id/text()
let $httpTarget := fn:concat($cpsrv:DataServer,$cpsrv:Database,$cpsrv:ProductDir,$productID,$cpsrv:LatestDir,$productID,'.xml')
let $appliedProduct := (http:send-request(
<http:request method='get' username='{$cpsrv:User}' password='{$cpsrv:Password}' send-authorization='true'
href='{$httpTarget}' auth-method='Basic'>
</http:request>
))[2]
let $pipelineID := $body/job/job-info/pipeline-id/text()
let $RootpipelineID := $appliedProduct/product:product/@pipelineRef/string()
let $ProductName := $appliedProduct/product:product/@name/string()
let $PipelinehttpTarget := fn:concat($DataServer,'bloomsbury','/pipelines/',$pipelineID,'.xml')
let $PipelineXml := (http:send-request(
<http:request method='get' username='admin@bloomsbury.com' password='admin' send-authorization='true'
href='{$PipelinehttpTarget}' auth-method='Basic'>
</http:request>
))[2]
let $steps := for $eachStep in $PipelineXml/*:pipeline/child::*/local-name()
return $eachStep
let $normalizedsteps := string-join($steps,'|')
return
if(fn:contains($normalizedsteps,'xquery'))
then
let $PublishedContent := for $XqueryStep at $pos in $PipelineXml/*:pipeline/*:xquery
let $ProductContent := $appliedProduct/*:product/*:contents/*:content
(: let $BaseUriToken := fn:concat(fn:string-join(fn:tokenize($XqueryStep,'\\')[position()=1 to fn:last()-1],'\'),'\'):)
let $BaseUriToken := $XqueryStep/@xml:base/string()
let $XQueryUri := ($BaseUriToken||$XqueryStep/@href/string())
let $XqueryInputLocation := if($pos = 1) then ($jobID||'/step-'||$pos) else ($jobID||'/step-'||$pos)
return
(
xquery:eval(
'declare base-uri "' || $BaseUriToken || '";'||
file:read-text($XQueryUri),
map
{
'{http://cms.bloomsbury.com/export}JOB_ID':$XqueryInputLocation,
'{http://cms.bloomsbury.com/export}product':$ProductName,
'{http://cms.bloomsbury.com/export}PUBLISH_JOB_BASE':$cpsrv:TempPipelineDir
}
)
,
let $tempFilePath := if($pos = 1) then fn:concat($cpsrv:TempPipelineDir,$jobID,'\step-',$pos,'\output\')
else fn:concat($cpsrv:TempPipelineDir,$jobID,'\step-',$pos,'\input\')
let $productName := $body/job/job-info/product-name/text()
let $ProductUri := file:children($cpsrv:TempPipelineDir||$jobID||'\step-1'||'\manifest\')
let $product := fn:doc($ProductUri)
let $ctype := $product/*:content/@sourceContentType/string()
let $httpTarget := fn:concat($cpsrv:DataServer,$cpsrv:Database,$cpsrv:PipelineDir,$pipelineID,'.xml')
let $appliedPipeline := (http:send-request(
<http:request method='get' username='{$cpsrv:User}' password='{$cpsrv:Password}' send-authorization='true'
href='{$httpTarget}' auth-method='Basic'>
</http:request>
))[2]
for $PublishedContentUri in file:list($cpsrv:TempPipelineDir||$jobID||'\step-'||$pos||'\output\',true())[fn:contains(.,'.xml')]
let $PublishedContentId := substring-before(fn:tokenize($PublishedContentUri,'\\')[fn:last()],'_')
return
(
copsrv:run-pipeline($jobID,$PublishedContentId,$tempFilePath,$ignoreValidation,$sessionValue,$pipelineID,$ctype,$productID,(),$tempFilePath,$appliedPipeline/pipeline:pipeline,$jobType)
,
(
if(
file:is-dir($cpsrv:TempPipelineDir||$jobID||'/step-'||$pos+1||'/input')
)
then jpsrv:CopyFilesFromInputDirToInputDir($cpsrv:TempPipelineDir,$jobID,$pos)
else()
)
)
)
return
(
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<content-type>{$ctype}</content-type>
<pipelineid>{$pipelineID}</pipelineid>
<cid></cid>
<temp-location>{fn:concat($cpsrv:TempPipelineDir,$jobID)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$body/job/job-info/product-name/text()}</input-type>
<job-type>{$jobType}</job-type>
</job>
)
)
else
let $PublishedContent := for $item in $appliedProduct/product:product/product:contents/product:content[@pipelineRef/string()=$pipelineID]/product:item
let $ctype := $item/parent::product:content/@sourceContentType/string()
let $httpTarget := fn:concat($cpsrv:DataServer,$cpsrv:Database,$cpsrv:PipelineDir,$pipelineID,'.xml')
let $appliedPipeline := (http:send-request(
<http:request method='get' username='{$cpsrv:User}' password='{$cpsrv:Password}' send-authorization='true'
href='{$httpTarget}' auth-method='Basic'>
</http:request>
))[2]
let $cid := $item/@id/string()
let $tempFilePath := fn:concat($cpsrv:TempPipelineDir,$jobID,'/','content/',$ctype,'/',$cid)
return
copsrv:run-pipeline($jobID,$cid,$tempFilePath,$ignoreValidation,$sessionValue,$pipelineID,$ctype,$productID,(),$tempFilePath,$appliedPipeline/pipeline:pipeline,$jobType)
return
(
$PublishedContent,
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<content-type>{$ctype}</content-type>
<pipelineid>{$pipelineID}</pipelineid>
<cid></cid>
<temp-location>{fn:concat($cpsrv:TempPipelineDir,$jobID)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$body/job/job-info/product-name/text()}</input-type>
<job-type>{$jobType}</job-type>
</job>
)
)
)
else ()
}
catch *
{
let $stepChunk := <step>
<type>Job Process Error</type>
<error>
<code>{$err:code}</code>
<description>{$err:description}</description>
<additional>{$err:additional}</additional>
</error>
</step>
return
(
rpsrv:update($body/job/job-info/id/text(),$stepChunk),
copsrv:move-to-db(
<job>
<job-id>{$body/job/job-info/id/text()}</job-id>
<content-type>{$body/job/job-info/content-type/text()}</content-type>
<temp-location>{fn:concat($cpsrv:TempPipelineDir,$body/job/job-info/id/text())}</temp-location>
<ignore-validation>{$body/job/job-info/ignore-validation/text()}</ignore-validation>
<session-value>{ fn:concat($body/job/job-info/requester-id/text(),'$$$$',$body/job/job-info/requester-email/text())}</session-value>
<input-type>{$body/job/job-info/product-name/text()}</input-type>
<job-type>{$body/job/job-info/job-type/text()}</job-type>
<failure>yes</failure>
</job>
),
admin:write-log("[Process Job][Error: " || $err:description || "]"),
admin:write-log("[Process Job][Error: " || $err:additional || "]"),
fn:error(xs:QName('jpsrv:error'), $err:additional)
)
}
};
declare %updating function jpsrv:CopyFilesFromInputDirToInputDir(
$PublishBaseDir as xs:string,
$JobId as xs:string,
$pos as xs:integer
)
{
let $Outputdir := ($PublishBaseDir||$JobId||'/step-'||$pos||'\output\')
for $files in file:list($Outputdir,true())[fn:ends-with(.,'xml')]
let $Filename := fn:tokenize($files,'\\')[fn:last()]
let $DocUri := ($PublishBaseDir||$JobId||'\step-'||$pos||'\output\'||$files)
let $TargetUri := ($PublishBaseDir||$JobId||'\step-'||$pos+1||'\input\'||$Filename)
return
file:write($TargetUri,fn:doc($DocUri))
};
declare %updating function jpsrv:Delete-Files(
$PublishBaseDir as xs:string,
$JobId as xs:string
)
{
let $ContentDir := ($PublishBaseDir||$JobId||'\output\')
for $Files in file:list($ContentDir,fn:true())[fn:ends-with(.,'xml')]
let $DocUri := ($PublishBaseDir||$JobId||'\output\'||$Files)
return
file:delete($DocUri)
};
(: Job Scheduler :)
declare
%rest:path("/jpsrv/scheduler")
%rest:GET
function jpsrv:create-scheduler(
)
{
let $job := jobs:invoke('C:\Program Files (x86)\BaseX\webapp\eBloomsbury\psrv\sch-psrv.xqm',(),map {'interval':'PT30S'})
return admin:write-log('=========== scheduler registered ================')
};
(:~ Return error message to the UI if authentication fails :)
declare
%rest:error("jpsrv:error")
%rest:error-param("description", "{$message}")
function jpsrv:error($message)
{
admin:write-log($message)
};