psvr/content-psrv.xqm

2261 lines
94 KiB
Plaintext

(:~
: Add content into the system
:
: @author Rave Technologies, https://www.rave-tech.com/, 2017
:)
module namespace copsrv = 'http://www.rave-tech.com/bloomsbury/contents-psrv';
import module namespace cpsrv = 'http://www.rave-tech.com/bloomsbury/config-psrv' at 'config-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';
import module namespace schematron = 'http://github.com/Schematron/schematron-basex';
declare namespace pipeline = 'http://cms.bloomsbury.com/pipeline';
declare namespace product = 'http://cms.bloomsbury.com/product-manifest';
declare namespace rest = 'http://basex.org/rest';
declare namespace xsl = 'http://www.w3.org/1999/XSL/Transform';
(:~
: Run pipeline for ZIP format
: @param $cid Content ID
: @param $pid Pipeline ID
: @param $contentPath Content path with file name to upload into the system
: @param $jobID ID of the Job to upload content
: @param $ctype Content type of the ingest documents
: @param $ignoreValidation Ignore validation if error happens
: @param $area Related area of the documents "Academic/Professional"
: @param $sessionValue SessionValue
: @param $inputType Input type (xml,word,zip)
: @param $jobType Input type (Ingest,Checkin,Publish)
: @param $whatToCheckin An xml chunk to checkin already checked-out files only
: @return empty sequence
:)
declare %updating function copsrv:upload-zip(
$cid as xs:string,
$pid as xs:string,
$contentPath as xs:string,
$jobID as xs:string,
$ctype as xs:string,
$ignoreValidation as xs:string,
$area as xs:string,
$sessionValue as xs:string,
$inputType as xs:string,
$jobType as xs:string,
$whatToCheckin as element(lock)?
)
{
let $_Log := admin:write-log(fn:concat('[',$jobID,'][Ignore Validation : ',$ignoreValidation,']'))
let $_Log := admin:write-log(fn:concat('[',$jobID,'][Content ID : ',$cid,']'))
let $_Log := admin:write-log(fn:concat('[',$jobID,'][Pipeline ID : ',$pid,']'))
let $_Log := admin:write-log(fn:concat('[',$cid,'][contentID : ',$cid,']'))
let $tempFilePath := fn:concat($cpsrv:TempPipelineDir,$jobID)
let $jobXml := fn:concat($cpsrv:TempPipelineDir,$jobID,$cpsrv:ReadyToMove,$cpsrv:JobDir,$jobID,'.xml')
return
(
if($pid)
then
(
(: To unzip ZIP file into temp folder :)
let $startedOn := fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))
return
try
{
let $httpTarget := fn:concat($cpsrv:DataServer,$cpsrv:Database,$cpsrv:PipelineDir,$pid,'.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]
return
if($appliedPipeline/pipeline:pipeline[@id=$pid])
then
(
admin:write-log("["|| $jobID || "][Pipeline is available: " || $pid || "]")
,
(: to unzip only checked-out files:)
if($jobType='Checkin')
then
let $createDir := file:create-dir(fn:concat($tempFilePath,'/zip-contents/'))
let $extractTo := fn:concat($tempFilePath,'/zip-contents/')
let $archive := file:read-binary($contentPath)
let $extractZip := let $filterCheckinRecords := let $array := fn:string-join(
for $uri in $whatToCheckin/file[exist='yes'][locked-by]/uri/text()
return fn:tokenize($uri,'/')[fn:last()]
, '|')
return if(fn:contains($array,'|')) then fn:concat('(',$array,')') else $array
for $entries in archive:entries($archive)
let $_log := admin:write-log('')
return
if(fn:matches(fn:replace($entries,'.docx','.xml'),fn:replace($filterCheckinRecords,'.docx','.xml')))
then archive:extract-to($extractTo, $archive, $entries)
else ()
let $stepChunk := <step>
<type>{'ZipToUnzip' || ' : ' || $contentPath}</type>
<location>{$extractTo}</location>
<started-on>{$startedOn}</started-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error></error>
</step>
return
(
rpsrv:update($jobID,$stepChunk),
admin:write-log(fn:concat('[',$jobID,'][UnZip Main Zip File]'))
)
else
let $extractTo := fn:concat($tempFilePath,'/zip-contents/')
let $createDir := file:create-dir(fn:concat($tempFilePath,'/zip-contents/'))
let $archive := file:read-binary($contentPath)
return
(
if($ctype='looseleaf')
then
if(archive:entries($archive)[fn:contains(.,$cpsrv:HeaderXml)])
then
archive:extract-to($extractTo, $archive, archive:entries($archive)[fn:contains(.,'header.xml')])
else ()
else()
,
if($ctype='looseleaf')
then
for $x in fetch:xml(fn:concat($tempFilePath,'\zip-contents\',$cpsrv:HeaderXml),map{'xinclude': false() })//*:include/@href/string()
return archive:extract-to($extractTo, $archive, archive:entries($archive)[fn:contains(.,fn:replace($x,'.xml','.docx'))])
else
(
archive:extract-to($extractTo, $archive, archive:entries($archive)[fn:contains(.,fn:replace($cid, '[^0-9]', ''))])
)
,
let $stepChunk := <step>
<type>{'ZipToUnzip' || ' : ' || $contentPath}</type>
<location>{$extractTo}</location>
<started-on>{$startedOn}</started-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error></error>
</step>
return
(
rpsrv:update($jobID,$stepChunk),
admin:write-log(fn:concat('[',$jobID,'][UnZip Main Zip File]'))
)
)
,
if($ctype='looseleaf')
then
copsrv:copy-header($jobID,$cid,fn:concat($tempFilePath,'\zip-contents\'),$area,$ctype,$sessionValue,$ignoreValidation,$jobType,$appliedPipeline/pipeline:pipeline)
else()
,
copsrv:run-pipeline($jobID,$cid,$tempFilePath,$ignoreValidation,$sessionValue,$pid,$ctype,$area,$inputType,$contentPath,$appliedPipeline/pipeline:pipeline,$jobType)
)
else
(
admin:write-log("["|| $jobID || "][Pipeline is not available: " || $pid || "]")
,
let $stepChunk := <step>
<type>Pipeline Check</type>
<started-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</started-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error>Pipeline is not available</error>
</step>
return rpsrv:update($jobID,$stepChunk)
,
file:append($jobXml,<status>Failure</status>)
,
file:append($jobXml,<finished-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</finished-on>)
,
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<content-type>{$ctype}</content-type>
<cid>{$cid}</cid>
<temp-location>{fn:concat($tempFilePath,'/',$cpsrv:ReadyToMove)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$inputType}</input-type>
<job-type>{$jobType}</job-type>
</job>
)
,
fn:error(xs:QName('copsrv:pipeline-error'),'Pipeline run failure.')
)
}
catch *
{ (: Error generated at the time of unzip zip file :)
let $stepChunk := <step>
<type>Pipeline Run Failure</type>
<started-on>{$startedOn}</started-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error>
<code>{$err:code}</code>
<description>{$err:description}</description>
<additional>{$err:additional}</additional>
</error>
</step>
return
(
rpsrv:update($jobID,$stepChunk)
,
admin:write-log(fn:concat('[',$jobID,'][Error: ',$err:additional,']'))
,
file:append($jobXml,<status>Failure</status>)
,
file:append($jobXml,<finished-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</finished-on>)
,
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<content-type>{$ctype}</content-type>
<cid>{$cid}</cid>
<temp-location>{fn:concat($tempFilePath,'/',$cpsrv:ReadyToMove)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$inputType}</input-type>
<job-type>{$jobType}</job-type>
</job>
)
)
}
)
else
(
admin:write-log(fn:concat('[',$jobID,'][Ingest pipeline is not available]'))
,
let $jobXml := fn:doc(fn:concat($cpsrv:TempPipelineDir,$jobID,$cpsrv:ReadyToMove,$cpsrv:JobDir,$jobID,'.xml'))
let $stepChunk := <step>
<type>Ingest Pipeline</type>
<started-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</started-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error>Ingest pipeline is not available</error>
</step>
return
(
rpsrv:update($jobID,$stepChunk)
,
file:append($jobXml,<status>Failure</status>)
,
file:append($jobXml,<finished-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</finished-on>)
,
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<content-type>{$ctype}</content-type>
<cid>{$cid}</cid>
<temp-location>{fn:concat($tempFilePath,'/',$cpsrv:ReadyToMove)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$inputType}</input-type>
<job-type>{$jobType}</job-type>
</job>
)
)
)
)
};
(:~
: Run pipeline for XML format
: @param $cid Content ID
: @param $pid Pipeline ID
: @param $contentPath Content path with file name to upload into the system
: @param $jobID ID of the Job to upload content
: @param $ctype Content type of the ingest documents
: @param $ignoreValidation Ignore validation if error happens
: @param $area Related area of the documents "Academic/Professional"
: @param $sessionValue SessionValue
: @param $inputType Input type (xml,word,zip)
: @param $jobType Input type (Ingest,Checkin,Publish)
: @return empty sequence
:)
declare %updating function copsrv:upload-xml(
$cid as xs:string,
$pid as xs:string,
$contentPath as xs:string,
$jobID as xs:string,
$ctype as xs:string,
$ignoreValidation as xs:string,
$area as xs:string,
$sessionValue as xs:string,
$inputType as xs:string,
$jobType as xs:string
)
{
(:let $_Log := admin:write-log(fn:concat('[',$jobID,'][***************************Job Started***************************]')):)
let $_Log := admin:write-log(fn:concat('[',$jobID,'][Ignore Validation : ',$ignoreValidation,']'))
let $_Log := admin:write-log(fn:concat('[',$jobID,'][Content ID : ',$cid,']'))
let $_Log := admin:write-log(fn:concat('[',$jobID,'][Pipeline ID : ',$pid,']'))
let $tempFilePath := fn:concat($cpsrv:TempPipelineDir,$jobID)
let $jobXml := fn:concat($cpsrv:TempPipelineDir,$jobID,$cpsrv:ReadyToMove,$cpsrv:JobDir,$jobID,'.xml')
let $startedOn := fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))
return
(
if($pid)
then
try
{
let $httpTarget := fn:concat($cpsrv:DataServer,$cpsrv:Database,$cpsrv:PipelineDir,$pid,'.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]
return
if($appliedPipeline/pipeline:pipeline[@id=$pid])
then
(
admin:write-log("["|| $jobID || "][Pipeline is available: " || $pid || "]")
,
copsrv:run-pipeline($jobID,$cid,$tempFilePath,$ignoreValidation,$sessionValue,$pid,$ctype,$area,$inputType,$contentPath,$appliedPipeline/pipeline:pipeline,$jobType)
)
else
(
admin:write-log("["|| $jobID || "][Pipeline is not available: " || $pid || "]")
,
let $stepChunk := <step>
<type>Pipeline Check</type>
<started-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</started-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error>Pipeline is not available</error>
</step>
return rpsrv:update($jobID,$stepChunk)
,
file:append($jobXml,<status>Failure</status>)
,
file:append($jobXml,<finished-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</finished-on>)
,
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<content-type>{$ctype}</content-type>
<cid>{$cid}</cid>
<temp-location>{fn:concat($tempFilePath,'/',$cpsrv:ReadyToMove)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$inputType}</input-type>
<job-type>{$jobType}</job-type>
</job>
)
,
fn:error(xs:QName('copsrv:pipeline-error'),'Pipeline run failure.')
)
}
catch *
{ (: Error generated at the time of unzip zip file :)
let $stepChunk := <step>
<type>Pipeline Run Failure</type>
<started-on>{$startedOn}</started-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error>
<code>{$err:code}</code>
<description>{$err:description}</description>
<additional>{$err:additional}</additional>
</error>
</step>
return
(
rpsrv:update($jobID,$stepChunk)
,
admin:write-log(fn:concat('[',$jobID,'][Error: ',$err:description,']'))
,
file:append($jobXml,<status>Failure</status>)
,
file:append($jobXml,<finished-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</finished-on>)
,
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<content-type>{$ctype}</content-type>
<cid>{$cid}</cid>
<temp-location>{fn:concat($tempFilePath,'/',$cpsrv:ReadyToMove)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$inputType}</input-type>
<job-type>{$jobType}</job-type>
</job>
)
)
}
else
(
admin:write-log(fn:concat('[',$jobID,'][Ingest pipeline is not available]'))
,
let $jobXml := fn:concat($cpsrv:TempPipelineDir,$jobID,$cpsrv:ReadyToMove,$cpsrv:JobDir,$jobID,'.xml')
let $stepChunk := <step>
<type>Ingest Pipeline</type>
<started-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</started-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error>Ingest pipeline is not available</error>
</step>
return
(
rpsrv:update($jobID,$stepChunk)
,
file:append($jobXml,<status>Failure</status>)
,
file:append($jobXml,<finished-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</finished-on>)
,
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<content-type>{$ctype}</content-type>
<cid>{$cid}</cid>
<temp-location>{fn:concat($tempFilePath,'/',$cpsrv:ReadyToMove)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$inputType}</input-type>
<job-type>{$jobType}</job-type>
</job>)
)
)
)
};
(:~
: Run pipeline for Docx format
: @param $cid Content ID
: @param $pid Pipeline ID
: @param $contentPath Content path with file name to upload into the system
: @param $jobID ID of the Job to upload content
: @param $ctype Content type of the ingest documents
: @param $ignoreValidation Ignore validation if error happens
: @param $area Related area of the documents "Academic/Professional"
: @param $sessionValue SessionValue
: @param $inputType Input type (xml,word,zip)
: @param $jobType Input type (Ingest,Checkin,Publish)
: @return empty sequence
:)
declare %updating function copsrv:upload-docx(
$cid as xs:string,
$pid as xs:string,
$contentPath as xs:string,
$jobID as xs:string,
$ctype as xs:string,
$ignoreValidation as xs:string,
$area as xs:string,
$sessionValue as xs:string,
$inputType as xs:string,
$jobType as xs:string
)
{
let $_Log := admin:write-log(fn:concat('[',$jobID,'][Ignore Validation : ',$ignoreValidation,']'))
let $_Log := admin:write-log(fn:concat('[',$jobID,'][Content ID : ',$cid,']'))
let $_Log := admin:write-log(fn:concat('[',$jobID,'][Pipeline ID : ',$pid,']'))
let $tempFilePath := fn:concat($cpsrv:TempPipelineDir,$jobID)
let $jobXml := fn:concat($cpsrv:TempPipelineDir,$jobID,$cpsrv:ReadyToMove,$cpsrv:JobDir,$jobID,'.xml')
return
(
if($pid)
then
(
(: To unzip ZIP file into temp folder :)
let $startedOn := fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))
return
try
{
let $httpTarget := fn:concat($cpsrv:DataServer,$cpsrv:Database,$cpsrv:PipelineDir,$pid,'.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]
return
if($appliedPipeline/pipeline:pipeline[@id=$pid])
then
(
admin:write-log("["|| $jobID || "][Pipeline is available: " || $pid || "]")
,
copsrv:run-pipeline($jobID,$cid,$tempFilePath,$ignoreValidation,$sessionValue,$pid,$ctype,$area,$inputType,$contentPath,$appliedPipeline/pipeline:pipeline,$jobType)
)
else
(
admin:write-log("["|| $jobID || "][Pipeline is not available: " || $pid || "]")
,
let $stepChunk := <step>
<type>Pipeline Check</type>
<started-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</started-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error>Pipeline is not available</error>
</step>
return rpsrv:update($jobID,$stepChunk)
,
file:append($jobXml,<status>Failure</status>)
,
file:append($jobXml,<finished-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</finished-on>)
,
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<content-type>{$ctype}</content-type>
<cid>{$cid}</cid>
<temp-location>{fn:concat($tempFilePath,'/',$cpsrv:ReadyToMove)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$inputType}</input-type>
<job-type>{$jobType}</job-type>
</job>
)
,
fn:error(xs:QName('copsrv:pipeline-error'),'Pipeline run failure.')
)
}
catch *
{ (: Error generated at the time of unzip zip file :)
let $stepChunk := <step>
<type>Pipeline Run Failure</type>
<started-on>{$startedOn}</started-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error>
<code>{$err:code}</code>
<description>{$err:description}</description>
<additional>{$err:additional}</additional>
</error>
</step>
return
(
rpsrv:update($jobID,$stepChunk)
,
admin:write-log(fn:concat('[',$jobID,'][Error: ',$err:description,']'))
,
file:append($jobXml,<status>Failure</status>)
,
file:append($jobXml,<finished-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</finished-on>)
,
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<content-type>{$ctype}</content-type>
<cid>{$cid}</cid>
<temp-location>{fn:concat($tempFilePath,'/',$cpsrv:ReadyToMove)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$inputType}</input-type>
<job-type>{$jobType}</job-type>
</job>
)
)
}
)
else
(
admin:write-log(fn:concat('[',$jobID,'][Ingest pipeline is not available]'))
,
let $jobXml := fn:concat($cpsrv:TempPipelineDir,$jobID,$cpsrv:ReadyToMove,$cpsrv:JobDir,$jobID,'.xml')
let $stepChunk := <step>
<type>Ingest Pipeline</type>
<started-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</started-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error>Ingest pipeline is not available</error>
</step>
return
(
rpsrv:update($jobID,$stepChunk)
,
file:append($jobXml,<status>Failure</status>)
,
file:append($jobXml,<finished-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</finished-on>)
,
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<content-type>{$ctype}</content-type>
<cid>{$cid}</cid>
<temp-location>{fn:concat($tempFilePath,'/',$cpsrv:ReadyToMove)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$inputType}</input-type>
<job-type>{$jobType}</job-type>
</job>
)
)
)
)
};
(:~
: Run Content type specific pipeline
: @param $jobID ID of the Job to upload content
: @param $contentID ID of the content
: @param $tempFilePath Temporary location of the process
: @param $ignoreValidation Ignore validation if error happens
: @param $sessionValue Session Value
: @param $pipelineID Ingest Pipeline ID
: @param $inputType Input type (xml,word,zip)
: @param $contentPath Content path with file name to upload into the system
: @param $appliedPipeline Content type specific pipeline
: @param $jobType Input type (Ingest,Checkin,Publish)
: @return empty sequence
:)
declare %updating function copsrv:run-pipeline(
$jobID as xs:string,
$contentID as xs:string,
$tempFilePath as xs:string,
$ignoreValidation as xs:string,
$sessionValue as xs:string,
$pipelineID as xs:string,
$ctype as xs:string,
$area as xs:string,
$inputType as xs:string?,
$contentPath as xs:string,
$appliedPipeline as item(),
$jobType as xs:string
)
{
try
{
copsrv:copy-relaxng()
,
let $totalStep := fn:count($appliedPipeline/child::*)
for $eachStep at $step in $appliedPipeline/child::*
return
if($eachStep[fn:local-name()='transform'])
then
if(fn:ends-with(fn:lower-case($eachStep/@href),'.xpl'))
then copsrv:run-transpact($eachStep,$step,$jobID,$appliedPipeline,$totalStep,$contentID,$ctype,$area,$ignoreValidation,$inputType,$contentPath,$jobType,$sessionValue)
else copsrv:run-transformation($eachStep,$step,$jobID,$appliedPipeline,$totalStep,$contentID,$ctype,$area,$ignoreValidation,$inputType,$contentPath,$jobType,$sessionValue)
else
if($eachStep[fn:local-name()='validate'])
then copsrv:run-validation($eachStep,$step,$jobID,$appliedPipeline,$totalStep,$contentID,$ctype,$area,$ignoreValidation,$inputType,$contentPath,$jobType,$sessionValue)
else ()
,
copsrv:update-final-status($jobID,$ignoreValidation,$contentID)
(:if($jobType='Publish')
then
(
let $productID := fn:substring-after($area,'$$$$')
let $lastItemID := fn:doc(fn:concat($config:Database,$config:ProductDir,$productID,$config:LatestDir,$productID,'.xml'))/product:product/product:contents/product:content/product:item[fn:last()]/@id/string()
return
if($lastItemID=$contentID)
then
let $archive := archive:create-from(fn:concat(fn:substring-before($contentPath,$ctype),'/Publish/'))
let $zipName := fn:concat($productID,'_',fn:format-dateTime(fn:current-dateTime(), "[Y1,4][M01][D01][H01][m01][s01][f01]"))
return
(
file:write-binary(fn:concat($config:PublishDir,$zipName,'.zip'),$archive)
,
insert node <location>{fn:concat($config:PublishDir,$zipName,'.zip')}</location> as last into db:open($config:Database,fn:concat($config:JobDir,$jobID,'.xml'))/job/job-info
,
config:update-message("[Content Publish][Content published location : " || fn:concat($config:PublishDir,$zipName,'.zip') || "]")
)
else ()
)
else ():)
,
if($jobType='Publish')
then
(:let $productID := $area
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 $productName := $appliedProduct/product:product/@name/string()
let $lastItemID := $appliedProduct/product:product/product:contents/product:content[fn:last()]/@pipelineRef/string()
return
if($lastItemID=$appliedPipeline/@id/string())
then
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<content-type>{$ctype}</content-type>
<pipelineid>{$appliedPipeline/@id/string()}</pipelineid>
<cid>{$contentID}</cid>
<temp-location>{fn:concat($tempFilePath,'/',$cpsrv:ReadyToMove)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$productName}</input-type>
<job-type>{$jobType}</job-type>
</job>
)
else ():)()
else
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<content-type>{$ctype}</content-type>
<cid>{$contentID}</cid>
<temp-location>{fn:concat($tempFilePath,'/',$cpsrv:ReadyToMove)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$inputType}</input-type>
<job-type>{$jobType}</job-type>
</job>
)
}
catch *
{
admin:write-log("[Run Pipeline][Error: " || $err:description || "]")
,
admin:write-log("[Run Pipeline][Error: " || $err:additional || "]")
,
let $stepChunk := <step>
<type>Pipeline run failure</type>
<started-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</started-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error>{$err:additional}</error>
</step>
return
(
rpsrv:update($jobID,$stepChunk)
,
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<pipelineid>{$appliedPipeline/@id/string()}</pipelineid>
<cid></cid>
<temp-location>{fn:concat($tempFilePath,'/',$cpsrv:ReadyToMove)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$inputType}</input-type>
<job-type>{$jobType}</job-type>
<failure>yes</failure>
</job>
)
)
}
};
(:~
: Run Transpact part of the pipeline
: @param $stepChunk Step element of pipeline
: @param $stepNumber Step Number of the pipeline
: @param $jobID ID of the Job
: @param $pipelineXml Pipeline XML
: @param $totalStep Total available steps in the pipeline
: @param $contentID Content ID
: @param $area Area of the job
: @param $ctype Content type of the job
: @param $ignoreValidation Ignore validation flag
: @param $inputType Input type (xml,word,zip)
: @param $contentPath Content path with file name to upload into the system
: @param $jobType Input type (Ingest,Checkin,Publish)
: @param $sessionValue Session Value
: @return empty sequence
:)
declare %updating function copsrv:run-transpact(
$stepChunk as node(),
$stepNumber as xs:integer,
$jobID as xs:string,
$pipelineXml as element(pipeline:pipeline),
$totalStep as xs:integer,
$contentID as xs:string,
$ctype as xs:string,
$area as xs:string,
$ignoreValidation as xs:string,
$inputType as xs:string?,
$contentPath as xs:string,
$jobType as xs:string,
$sessionValue as xs:string
)
{
let $startedOn := fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))
let $tempFilePath := fn:concat($cpsrv:TempPipelineDir,$jobID)
let $outDir := if($stepChunk/@label/string()) then $stepChunk/@label/string() else fn:concat('step',$stepNumber)
let $inDir := if(($stepNumber eq 1) and ($inputType='zip'))
then 'zip-contents'
else
if(($stepNumber eq 1) and ($inputType!='zip'))
then $contentPath
else
if($pipelineXml//child::*[fn:position()=fn:sum($stepNumber - 1)]/@label/string())
then $pipelineXml//child::*[fn:position()=fn:sum($stepNumber - 1)]/@label/string()
else fn:concat('step',fn:sum($stepNumber - 1))
let $xplLocation := $stepChunk/@href/string()
let $failOnError := if($stepChunk/@failOnError/string()) then $stepChunk/@failOnError/string() else 'false'
let $printFiles := if($inputType='zip')
then
let $array := if(file:exists(fn:concat($tempFilePath,'\zip-contents\',$cpsrv:HeaderXml)))
then
fn:string-join(
for $printFile in fetch:xml(fn:concat($tempFilePath,'\zip-contents\',$cpsrv:HeaderXml),map { 'xinclude': false() })//*:include[@outputformat='print']/@href/string()
return $printFile,'|')
else 'NONE'
return
if(fn:contains($array,'|'))
then fn:concat('(',$array,')')
else if($array) then $array else 'NONE'
else 'NONE'
return
try
{
(: Step 2: transpact conversion :)
let $createTempDir := file:create-dir(fn:concat($tempFilePath,'/',$outDir,'/'))
let $startedOn := fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))
for $file in if($inputType='zip')
then file:list(fn:concat($tempFilePath,'/',$inDir,'/'))[if($stepNumber eq 1) then ends-with(.,'.docx') else ends-with(.,'.xml')]
else
if($stepNumber eq 1)
then fn:tokenize($contentPath,'/')[fn:last()]
else fn:replace(fn:tokenize($contentPath,'/')[fn:last()],'.docx','.xml')
let $file := if(fn:ends-with($file,'.xml') or fn:ends-with($file,'.docx')) then $file else fn:concat($file,'.xml')
let $docUri := if(($stepNumber eq 1) and ($inputType!='zip'))
then $contentPath
else
if(($stepNumber eq 1) and ($jobType='Publish'))
then
if($ctype='looseleaf')
then fn:concat($contentPath,'/',$cpsrv:HeaderXml)
else file:children($contentPath)
else fn:concat($tempFilePath,'/',$inDir,'/',$file)
let $outputUri := fn:concat($tempFilePath,'/',$outDir,'/',fn:replace($file,'.docx','.xml'))
let $printFiles := if($stepNumber eq 1)
then $printFiles
else fn:replace($printFiles,'\.docx','.xml')
let $conversion := if(fn:matches(fn:tokenize($docUri,'/')[fn:last()],$printFiles))
then 'PRINT-READY'
else
if(fn:ends-with($xplLocation,'docx2hub.xpl'))
then proc:execute($cpsrv:CalabashBatch, ('-o', fn:concat('result=file:///',fn:encode-for-uri($outputUri)), fn:concat('file:///',$xplLocation), fn:concat('docx=file:///',fn:encode-for-uri($docUri))))
else proc:execute($cpsrv:CalabashBatch, ('-o', fn:concat('result=file:///',fn:encode-for-uri($outputUri)), '-i', fn:concat('source=file:///',fn:encode-for-uri($docUri)), fn:concat('file:///',$xplLocation),'top-level-element-name=article'))
return
if($conversion='PRINT-READY')
then
let $stepChunk := <step><type>{'PRINT-READY: ' || $outDir || ' : ' || $file}</type></step>
let $wordML := if($stepNumber eq 1)
then
let $archive := file:read-binary($docUri)
return fn:parse-xml(archive:extract-text($archive,$cpsrv:DocXml))
else fn:doc($docUri)
return
(
rpsrv:update($jobID,$stepChunk)
,
file:write($outputUri, $wordML)
,
if($totalStep eq $stepNumber)
then copsrv:save-to-db($jobID,$wordML,$contentID,$area,$ctype,fn:replace($file,'\.docx','.xml'),$sessionValue,$jobType,$contentPath,"PRINT-READY")
else ()
,
admin:write-log(fn:concat('[',$jobID,'][Step ',$stepNumber,'][',$outDir,' : ',$file,']'))
)
else
if(fn:number($conversion/code/text()) eq 0)
then
( (: transpact conversion passed :)
let $stepChunk := <step>
<type>{$outDir || ' : ' || $file}</type>
<started-on>{$startedOn}</started-on>
<location>{$outputUri}</location>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error></error>
</step>
return
(
rpsrv:update($jobID,$stepChunk)
,
if($totalStep eq $stepNumber)
then copsrv:save-to-db($jobID,fn:doc($outputUri),$contentID,$area,$ctype,fn:replace($file,'\.docx','.xml'),$sessionValue,$jobType,$contentPath,"NORMAL")
else ()
,
admin:write-log(fn:concat('[',$jobID,'][Step ',$stepNumber,'][',$outDir,' : ',$file,']'))
)
)
else (: transpact conversion fail :)
let $stepChunk := <step>
<type>{$outDir || ' : ' || $file}</type>
<started-on>{$startedOn}</started-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error>{$conversion}</error>
</step>
return
(
rpsrv:update($jobID,$stepChunk)
,
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<content-type>{$ctype}</content-type>
<pipelineid>{$pipelineXml/@id/string()}</pipelineid>
<cid></cid>
<temp-location>{fn:concat($tempFilePath,'/',$cpsrv:ReadyToMove)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$inputType}</input-type>
<job-type>{$jobType}</job-type>
<failure>yes</failure>
</job>
)
,
admin:write-log(fn:concat('[',$jobID,'][Step ',$stepNumber,'][Error][',$outDir,' : ',$file,']'))
,
fn:error(xs:QName('copsrv:pipeline-error'),'Transpact error')
)
}
catch * {(: transpact conversion error :)
let $stepChunk := <step>
<type>Transpact Error</type>
<started-on>{$startedOn}</started-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error>
<code>{$err:code}</code>
<description>{$err:description}</description>
<additional>{$err:additional}</additional>
</error>
</step>
return
(
(:copsrv:job-update-report($jobID,$ignoreValidation,$contentID,$stepChunk,"SystemError",$jobType)
,:)
admin:write-log(fn:concat('[',$jobID,'][Error: ',$err:description,']'))
,
rpsrv:update($jobID,$stepChunk)
,
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<content-type>{$ctype}</content-type>
<pipelineid>{$pipelineXml/@id/string()}</pipelineid>
<cid></cid>
<temp-location>{fn:concat($tempFilePath,'/',$cpsrv:ReadyToMove)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$inputType}</input-type>
<job-type>{$jobType}</job-type>
<failure>yes</failure>
</job>
)
,
fn:error(xs:QName('copsrv:pipeline-error'),'Transpact error')
)
}
};
(:~
: Run Transformation part of the pipeline
: @param $stepChunk Step element of pipeline
: @param $stepNumber Step Number of the pipeline
: @param $jobID ID of the Job
: @param $pipelineXml Pipeline XML
: @param $totalStep Total available steps in the pipeline
: @param $contentID Content ID
: @param $area Area of the job
: @param $ctype Content type of the job
: @param $ignoreValidation Ignore validation flag
: @param $inputType Input type (xml,word,zip)
: @param $contentPath Content path with file name to upload into the system
: @param $jobType Input type (Ingest,Checkin,Publish)
: @param $sessionValue Session Value
: @return empty sequence
:)
declare %updating function copsrv:run-transformation(
$stepChunk as node(),
$stepNumber as xs:integer,
$jobID as xs:string,
$pipelineXml as element(pipeline:pipeline),
$totalStep as xs:integer,
$contentID as xs:string,
$ctype as xs:string,
$area as xs:string,
$ignoreValidation as xs:string,
$inputType as xs:string?,
$contentPath as xs:string,
$jobType as xs:string,
$sessionValue as xs:string
)
{
try
{
let $startedOn := fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))
let $tempFilePath := fn:concat($cpsrv:TempPipelineDir,$jobID)
let $outDir := if($stepChunk/@label/string()) then $stepChunk/@label/string() else fn:concat('step',$stepNumber)
let $inDir := if(($stepNumber eq 1) and ($inputType='zip'))
then 'zip-contents'
else
if((($stepNumber eq 1) and ($inputType!='zip')) or ($stepNumber eq 1) and ($jobType='Publish'))
then $contentPath
else
if($pipelineXml//child::*[fn:position()=fn:sum($stepNumber - 1)]/@label/string())
then $pipelineXml//child::*[fn:position()=fn:sum($stepNumber - 1)]/@label/string()
else fn:concat('step',fn:sum($stepNumber - 1))
let $failOnError := if($stepChunk/@failOnError/string()) then $stepChunk/@failOnError/string() else 'false'
let $xslLocation := $stepChunk/@href/string()
let $httpTarget := fn:concat($cpsrv:DataServer,$cpsrv:Database,$xslLocation)
let $appliedXslt := http:send-request(
<http:request method='get' username='{$cpsrv:User}' password='{$cpsrv:Password}' send-authorization='true'
href='{$httpTarget}'>
</http:request>
)
let $xsltDoc := ($appliedXslt)[2]/xsl:stylesheet
let $printFiles := if($inputType='zip')
then
let $array := if(file:exists(fn:concat($tempFilePath,'\zip-contents\',$cpsrv:HeaderXml)))
then
fn:string-join(
for $printFile in fetch:xml(fn:concat($tempFilePath,'\zip-contents\',$cpsrv:HeaderXml),map { 'xinclude': false() })//*:include[@outputformat='print']/@href/string()
return $printFile,'|')
else 'NONE'
return
if(fn:contains($array,'|'))
then fn:concat('(',$array,')')
else if($array) then $array else 'NONE'
else 'NONE'
return
(: transformation pass :)
let $createTempDir := file:create-dir(fn:concat($tempFilePath,'/',$outDir,'/'))
let $startedOn := fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))
for $file in if($inputType='zip')
then file:list(fn:concat($tempFilePath,'/',$inDir,'/'))[if($stepNumber eq 1) then ends-with(.,'.docx') else ends-with(.,'.xml')]
else
if($stepNumber eq 1)
then fn:tokenize($contentPath,'/')[fn:last()]
else fn:replace(fn:tokenize($contentPath,'/')[fn:last()],'.docx','.xml')
let $file := if(fn:ends-with($file,'.xml') or fn:ends-with($file,'.docx')) then $file else fn:concat($file,'.xml')
let $docUri := if(($stepNumber eq 1) and ($inputType!='zip'))
then $contentPath
else
if(($stepNumber eq 1) and ($jobType='Publish'))
then
if($ctype='looseleaf')
then fn:concat($contentPath,'/',$cpsrv:HeaderXml)
else file:children($contentPath)
else fn:concat($tempFilePath,'/',$inDir,'/',$file)
let $outputUri := fn:concat($tempFilePath,'/',$outDir,'/',fn:replace($file,'.docx','.xml'))
let $headerid := if ($jobType='Publish') then ()
else if ($jobType='Checkin')
then
if(file:exists(fn:concat($tempFilePath,'\zip-contents\',$cpsrv:HeaderXml)))
then fetch:xml(concat($tempFilePath,'\zip-contents\','header.xml'),map { 'xinclude': false() })/*:book/@xml:id/string()
else()
else if ($jobType='Ingest' and $ctype='looseleaf') then fetch:xml(concat($tempFilePath,'\zip-contents\','header.xml'),map { 'xinclude': false() })/*:book/@xml:id/string()
else()
let $conversion := if(fn:matches(fn:tokenize($docUri,'/')[fn:last()],fn:replace($printFiles,'.docx','.xml'))) then 'PRINT-READY'
else if ($jobType='Ingest' and $ctype='looseleaf') then xslt:transform(fn:doc($docUri), $xsltDoc, map { 'BookISBN': $headerid})
else if ($jobType='Publish') then xslt:transform(fn:doc($docUri), $xsltDoc)
else if($jobType='Checkin' and file:exists(fn:concat($tempFilePath,'\zip-contents\',$cpsrv:HeaderXml))) then xslt:transform(fn:doc($docUri), $xsltDoc, map { 'BookISBN': $headerid})
else xslt:transform(fn:doc($docUri), $xsltDoc)
return
if($conversion='PRINT-READY')
then
(
let $stepChunk := <step><type>{'PRINT-READY: ' || $outDir || ' : ' || $file}</type></step>
return rpsrv:update($jobID,$stepChunk)
,
(:file:write($outputUri, fn:doc($docUri)):)
file:write($outputUri, fn:doc($docUri), map { 'method': 'xml', 'indent': 'no' })
,
if($totalStep eq $stepNumber)
then copsrv:save-to-db($jobID,$conversion,$contentID,$area,$ctype,fn:replace($file,'\.docx','.xml'),$sessionValue,$jobType,$contentPath,"PRINT-READY")
else ()
,
admin:write-log(fn:concat('[',$jobID,'][Step ',$stepNumber,'][',$outDir,' : ',$file,']'))
)
else
(
let $stepChunk := <step>
<type>{$outDir || ' : ' || $file}</type>
<started-on>{$startedOn}</started-on>
<location>{$outputUri}</location>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error></error>
</step>
return
rpsrv:update($jobID,$stepChunk)
,
(:file:write($outputUri, $conversion):)
file:write($outputUri, $conversion, map { 'method': 'xml', 'indent': 'no' })
,
if($totalStep eq $stepNumber)
then copsrv:save-to-db($jobID,$conversion,$contentID,$area,$ctype,fn:replace($file,'\.docx','.xml'),$sessionValue,$jobType,$contentPath,"NORMAL")
else ()
,
admin:write-log(fn:concat('[',$jobID,'][Step ',$stepNumber,'][',$outDir,' : ',$file,']'))
)
}
catch * {(: transformation error :)
let $stepChunk := <step>
<type>Transformation Error</type>
<started-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</started-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error>
<code>{$err:code}</code>
<description>{$err:description}</description>
<additional>{$err:additional}</additional>
</error>
</step>
return
(
(:copsrv:job-update-report($jobID,$ignoreValidation,$contentID,$stepChunk,"Failure",$jobType)
,:)
admin:write-log(fn:concat('[',$jobID,'][Error: ',$err:description,']'))
,
rpsrv:update($jobID,$stepChunk)
,
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<content-type>{$ctype}</content-type>
<pipelineid>{$pipelineXml/@id/string()}</pipelineid>
<cid></cid>
<temp-location>{fn:concat($cpsrv:TempPipelineDir,$jobID,$cpsrv:ReadyToMove)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$inputType}</input-type>
<job-type>{$jobType}</job-type>
<failure>yes</failure>
</job>
)
,
fn:error(xs:QName('copsrv:pipeline-error'),'Transformation error')
)
}
};
(:~
: Run Validation part of the pipeline
: @param $stepChunk Step element of pipeline
: @param $stepNumber Step Number of the pipeline
: @param $jobID ID of the Job
: @param $pipelineXml Pipeline XML
: @param $totalStep Total available steps in the pipeline
: @param $contentID Content ID
: @param $area Area of the job
: @param $ctype Content type of the job
: @param $ignoreValidation Ignore validation flag
: @param $inputType Input type (xml,word,zip)
: @param $contentPath Content path with file name to upload into the system
: @param $jobType Input type (Ingest,Checkin,Publish)
: @param $sessionValue Session Value
: @return empty sequence
:)
declare %updating function copsrv:run-validation(
$stepChunk as node(),
$stepNumber as xs:integer,
$jobID as xs:string,
$pipelineXml as element(pipeline:pipeline),
$totalStep as xs:integer,
$contentID as xs:string,
$ctype as xs:string,
$area as xs:string,
$ignoreValidation as xs:string,
$inputType as xs:string?,
$contentPath as xs:string,
$jobType as xs:string,
$sessionValue as xs:string
)
{
let $startedOn := fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))
let $tempFilePath := fn:concat($cpsrv:TempPipelineDir,$jobID)
let $outDir := if($stepChunk/@label/string()) then $stepChunk/@label/string() else fn:concat('step',$stepNumber)
let $inDir := if(($stepNumber eq 1) and ($inputType='zip'))
then 'zip-contents'
else
if(($stepNumber eq 1) and ($inputType!='zip'))
then $contentPath
else
if($pipelineXml//child::*[fn:position()=1]/local-name(.)='xquery')
then $contentPath
else
if($pipelineXml//child::*[fn:position()=fn:sum($stepNumber - 1)]/@label/string())
then $pipelineXml//child::*[fn:position()=fn:sum($stepNumber - 1)]/@label/string()
else fn:concat('step',fn:sum($stepNumber - 1))
let $failOnError := if($stepChunk/@failOnError/string()) then $stepChunk/@failOnError/string() else 'false'
let $printFiles := if($inputType='zip')
then
let $array := if(file:exists(fn:concat($tempFilePath,'\zip-contents\',$cpsrv:HeaderXml)))
then
fn:string-join(
for $printFile in fetch:xml(fn:concat($tempFilePath,'\zip-contents\',$cpsrv:HeaderXml),map { 'xinclude': false() })//*:include[@outputformat='print']/@href/string()
return $printFile,'|')
else 'NONE'
return
if(fn:contains($array,'|'))
then fn:concat('(',$array,')')
else if($array) then $array else 'NONE'
else 'NONE'
let $schemaname := $pipelineXml//*:validate[@label="schema-validation"]/@href/string()
let $SchematronName := $pipelineXml//*:validate[@label="schematron-validation"]/@href/string()
let $SchematronBaseDir := (base-uri($pipelineXml//*:validate[@label="schematron-validation"]))
let $SchematronUri := resolve-uri($SchematronName,$SchematronBaseDir)
return
try
{
let $createTempDir := file:create-dir(fn:concat($tempFilePath,'/',$outDir,'/'))
let $startedOn := fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))
for $file in if($inputType='zip')
then if($ctype='looseleaf') then file:list(fn:concat($tempFilePath,'/',$inDir,'/'))[if($stepNumber eq 1) then ends-with(.,'.docx') else ends-with(.,'.xml')]
else file:list(fn:concat($tempFilePath,'/',$inDir,'/'))
else if($pipelineXml//child::*[fn:position()=1]/local-name(.)='xquery')
(:then file:list($inDir,true())[fn:contains(.,'xml') and fn:matches(.,replace($contentID, '[^0-9]', ''))]:)
then file:list($inDir,true())[fn:contains(.,'xml')]
else if($stepNumber eq 1 and not($pipelineXml//child::*[fn:position()=1]/local-name(.)='xquery'))
then fn:tokenize($contentPath,'/')[fn:last()]
else fn:replace(fn:tokenize($contentPath,'/')[fn:last()],'.docx','.xml')
let $file := if(fn:ends-with($file,'.xml') or fn:ends-with($file,'.docx')) then $file else fn:concat($file,'.xml')
let $docUri := if(($stepNumber eq 1) and ($inputType!='zip'))
then $contentPath
else
if(($stepNumber eq 1) and ($jobType='Publish'))
then
if($ctype='looseleaf')
then fn:concat($contentPath,'/',$cpsrv:HeaderXml)
else file:children($contentPath)
(:else fn:concat($tempFilePath,'/',$inDir,'/',$file):)
else if($jobType='Publish' and $pipelineXml//child::*[fn:position()=1]/local-name(.)='xquery')
then fn:concat($inDir,'/',$file)
else fn:concat($tempFilePath,'/',$inDir,'/',$file)
let $outputUri := if($pipelineXml//child::*[fn:position()=1]/local-name(.)='xquery')
then fn:concat($tempFilePath,'/',$outDir,'/',fn:tokenize($docUri,'\\')[fn:last()])
else
fn:concat($tempFilePath,'/',$outDir,'/',fn:replace($file,'.docx','.xml'))
let $inputXml := if(fn:ends-with($docUri,$cpsrv:HeaderXml))
then fetch:xml($docUri,map{'xinclude':false()})
else fn:doc($docUri)
let $conversion := if(fn:matches(fn:tokenize($docUri,'/')[fn:last()],fn:replace($printFiles,'.docx','.xml')))
then "PRINT-READY"
else
if(fn:ends-with(fn:lower-case($stepChunk/@href/string()),'.rng'))
then (:validate:rng-report($inputXml, $validator, fn:true()):)
(:let $xplLocation := "file:///C:/xproc/calabash/extensions/transpect/rng-extension/xpl/rng-validate.xpl":)
(:let $resultfilenameuri := fn:tokenize($docUri,'/')[fn:last()]:)
let $resultfilenameuri := fn:tokenize(fn:replace($docUri,'\\','/'),'/')[fn:last()]
let $resultfilename := substring-before($resultfilenameuri,'.xml')
let $SchemaBaseDir := fn:base-uri($pipelineXml//*:validate[@label="schema-validation"])
let $proc := proc:execute(
$cpsrv:CalabashBatch,
(
'-o', 'result=' ||$cpsrv:XprocTempResultDir||$resultfilename,
'-i', 'source='||'file:///'||fn:replace($docUri,'\\','/'),
'-i', 'schema='||$SchemaBaseDir||$schemaname,
$cpsrv:SchemaxplLocation
)
)
return
if(fn:number($proc/code/text()) eq 0)
then
(
let $SchemaValidationTempDoc := fn:doc(fn:concat($cpsrv:XprocTempResultDir,$resultfilename))
return
(
if($SchemaValidationTempDoc/*:errors='') then 'valid' else $SchemaValidationTempDoc )
)
else $proc
else if(fn:ends-with(fn:lower-case($stepChunk/@href/string()),'.xsd'))
then
(
(:validate:xsd-report(fn:doc($docUri),fn:concat($cpsrv:XsdDir,$schemaname)):)
let $SchemaBaseDir := fn:base-uri($pipelineXml//*:validate[@label="schema-validation"])
return
validate:xsd-report(fn:doc($docUri),fn:resolve-uri($schemaname,$SchemaBaseDir))
)
else ()
return
(
if($conversion='PRINT-READY')
then
(
let $stepChunk := <step><type>{'PRINT-READY: '|| $outDir || ' : ' || $file}</type></step>
return rpsrv:update($jobID,$stepChunk)
,
file:write($outputUri, $inputXml)
,
if($totalStep eq $stepNumber)
then copsrv:save-to-db($jobID,$inputXml,$contentID,$area,$ctype,fn:replace($file,'\.docx','.xml'),$sessionValue,$jobType,$contentPath,"PRINT-READY")
else ()
,
admin:write-log(fn:concat('[',$jobID,'][Step ',$stepNumber,'][',$outDir,' : ',$file,']'))
)
else if(fn:ends-with(fn:lower-case($stepChunk/@href/string()),'.rng'))
then
if($conversion='valid')
then
let $stepChunk := <step>
<type>{$outDir || ' : ' || $file}</type>
<started-on>{$startedOn}</started-on>
<location>{$outputUri}</location>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error></error>
</step>
return
(
rpsrv:update($jobID,$stepChunk)
,
file:write($outputUri, $inputXml, map { 'method': 'xml', 'indent': 'no' }),
if($totalStep eq $stepNumber)
then copsrv:save-to-db($jobID,$inputXml,$contentID,$area,$ctype,if($pipelineXml//child::*[fn:position()=1]/local-name(.)='xquery') then fn:tokenize($docUri,'\\')[fn:last()] else fn:replace($file,'\.docx','.xml'),$sessionValue,$jobType,$contentPath,"NORMAL")
else ()
,
admin:write-log(fn:concat('[',$jobID,'][Step ',$stepNumber,'][',$outDir,' : ',$file,']'))
)
else
let $stepChunk := <step>
<type>{$outDir || ' : ' || $file}</type>
<started-on>{$startedOn}</started-on>
<location>{$outputUri}</location>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
{$conversion}
</step>
return
(
admin:write-log(fn:concat('[',$jobID,'][Step ',$stepNumber,'][error][',$outDir,' : ',$file,']'))
,
if($ignoreValidation='true' and $failOnError='false')
then file:write($outputUri, $inputXml)
else ()
,
if($totalStep eq $stepNumber)
then
(
copsrv:save-to-db($jobID,$inputXml,$contentID,$area,$ctype,fn:replace($file,'\.docx','.xml'),$sessionValue,$jobType,$contentPath,"NORMAL")
,
copsrv:job-update-report($jobID,$ignoreValidation,$contentID,$stepChunk, if($ignoreValidation='true' or $failOnError='true') then "SuccessWithWarnings" else "SystemError",$jobType)
)
else ()
,
rpsrv:update($jobID,$stepChunk)
,
if($failOnError='true')
then
(
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<content-type>{$ctype}</content-type>
<pipelineid>{$pipelineXml/@id/string()}</pipelineid>
<cid></cid>
<temp-location>{fn:concat($tempFilePath,'/',$cpsrv:ReadyToMove)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$inputType}</input-type>
<job-type>{$jobType}</job-type>
<failure>yes</failure>
</job>
)
,
fn:error(xs:QName('copsrv:pipeline-error'),'RNC validation error')
)
else ()
)
else if(fn:ends-with(fn:lower-case($stepChunk/@href/string()),'.xsd'))
then
if($conversion='valid')
then
let $stepChunk := <step>
<type>{$outDir || ' : ' || $file}</type>
<started-on>{$startedOn}</started-on>
<location>{$outputUri}</location>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error></error>
</step>
return
(
rpsrv:update($jobID,$stepChunk)
,
file:write($outputUri, $inputXml, map { 'method': 'xml', 'indent': 'no' })
,
if($totalStep eq $stepNumber)
then copsrv:save-to-db($jobID,$inputXml,$contentID,$area,$ctype,if($pipelineXml//child::*[fn:position()=1]/local-name(.)='xquery') then fn:tokenize($docUri,'\\')[fn:last()] else fn:replace($file,'\.docx','.xml'),$sessionValue,$jobType,$contentPath,"NORMAL")
else ()
,
admin:write-log(fn:concat('[',$jobID,'][Step ',$stepNumber,'][',$outDir,' : ',$file,']'))
)
else
let $stepChunk := <step>
<type>{$outDir || ' : ' || $file}</type>
<started-on>{$startedOn}</started-on>
<location>{$outputUri}</location>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error>{$conversion/message}</error>
</step>
return
(
admin:write-log(fn:concat('[',$jobID,'][Step ',$stepNumber,'][error][',$outDir,' : ',$file,']'))
,
if($ignoreValidation='true' and $failOnError='false')
then file:write($outputUri, $inputXml)
else ()
,
(
rpsrv:update($jobID,$stepChunk)
)
,
if($totalStep eq $stepNumber)
then
(
copsrv:save-to-db($jobID,$inputXml,$contentID,$area,$ctype,fn:replace($file,'\.docx','.xml'),$sessionValue,$jobType,$contentPath,"NORMAL")
,
copsrv:job-update-report($jobID,$ignoreValidation,$contentID,$stepChunk, if($ignoreValidation='true' or $failOnError='true') then "SuccessWithWarnings" else "SystemError",$jobType)
)
else ()
,
if($failOnError='true')
then
(
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<content-type>{$ctype}</content-type>
<pipelineid>{$pipelineXml/@id/string()}</pipelineid>
<cid></cid>
<temp-location>{fn:concat($tempFilePath,'/',$cpsrv:ReadyToMove)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$inputType}</input-type>
<job-type>{$jobType}</job-type>
<failure>yes</failure>
</job>
)
)
else ()
)
else ()
),
if($jobType='Ingest' and $ctype='looseleaf')
then
if(fetch:xml(concat($tempFilePath,$cpsrv:bloomsburydbk,$cpsrv:HeaderXml),map{'xinclude': false() })//*:include[@outputformat='print'])
then
let $headeruri := concat($tempFilePath,$cpsrv:bloomsburydbk,$cpsrv:HeaderXml)
let $headerXML := fetch:xml($headeruri,map{'xinclude': false() })
return
if($headerXML/*:book/*:include[@outputformat='print'])
then
let $out :=$headerXML transform with {delete node //*:include[@outputformat='print']}
return
file:write(concat($tempFilePath,$cpsrv:bloomsburydbk,$cpsrv:HeaderXml),$out)
else()
else()
else()
,
if(fn:ends-with(fn:lower-case($stepChunk/@href/string()),'.sch') and $jobType ='Ingest')
then
let $headeruri := if($ctype='looseleaf') then concat($tempFilePath,$cpsrv:bloomsburydbk,$cpsrv:HeaderXml) else for $uri in file:children(concat($tempFilePath,'\schema-validation\')) return $uri
for $inputXML in fn:doc($headeruri)
let $conversion := schematron:validate($inputXML, schematron:compile($SchematronUri))
return
if(schematron:is-valid($conversion))
then
for $docuri in if($ctype='looseleaf') then file:children(concat($tempFilePath,'/schema-validation/')) else file:children(concat($tempFilePath,'/schema-validation/'))
let $filename := fn:tokenize($docuri, '\\')[fn:last()]
let $inputXml := if(fn:ends-with($docuri,$cpsrv:HeaderXml))
then fetch:xml($docuri,map{'xinclude':false()})
else fn:doc($docuri)
let $outputUri := fn:concat($tempFilePath,'/',$outDir,'/',fn:replace($filename,'.docx','.xml'))
let $stepChunk := <step>
<type>{$outDir || ' : ' || $headeruri}</type>
<started-on>{$startedOn}</started-on>
<location>{$headeruri}</location>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error></error>
</step>
return
(
rpsrv:update($jobID,$stepChunk)
,
file:write($outputUri, $inputXml, map { 'method': 'xml', 'indent': 'no' })
,
if($totalStep eq $stepNumber)
then
if(fn:matches(fn:tokenize($docuri,'/')[fn:last()],fn:replace($printFiles,'.docx','.xml')))
then copsrv:save-to-db($jobID,$inputXml,$contentID,$area,$ctype,fn:replace($filename,'\.docx','.xml'),$sessionValue,$jobType,$contentPath,"PRINT-READY")
else
copsrv:save-to-db($jobID,$inputXml,$contentID,$area,$ctype,fn:replace($filename,'\.docx','.xml'),$sessionValue,$jobType,$contentPath,"NORMAL")
else ()
,
admin:write-log(fn:concat('[',$jobID,'][Step ',$stepNumber,'][',$outDir,' : ',$filename,']'))
)
else
let $stepChunk := <step>
<type>{$outDir || ' : ' || $headeruri}</type>
<started-on>{$startedOn}</started-on>
<location>{$headeruri}</location>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<errors>
{
for $message in schematron:messages($conversion)
return
<message xpath="{schematron:message-location($message)}">{fn:concat(schematron:message-level($message), ': ', schematron:message-description($message))}</message>
}
</errors>
</step>
return
(
if($ignoreValidation='true' or $failOnError='false')
then
for $docuri in file:children(concat($tempFilePath,'/schema-validation/'))
let $filename := fn:tokenize($docuri, '\\')[fn:last()]
let $inputXml := if(fn:ends-with($docuri,$cpsrv:HeaderXml))
then fetch:xml($docuri,map{'xinclude':false()})
else fn:doc($docuri)
let $outputUri := fn:concat($tempFilePath,'/',$outDir,'/',fn:replace($filename,'.docx','.xml'))
return
(
file:write($outputUri, $inputXml, map { 'method': 'xml', 'indent': 'no' })
)
else(),
if($totalStep eq $stepNumber)
then
for $docuri in file:children(concat($tempFilePath,'/schema-validation/'))
let $filename := fn:tokenize($docuri, '\\')[fn:last()]
let $inputXml := if(fn:ends-with($docuri,$cpsrv:HeaderXml))
then fetch:xml($docuri,map{'xinclude':false()})
else fn:doc($docuri)
let $outputUri := fn:concat($tempFilePath,'/',$outDir,'/',fn:replace($filename,'.docx','.xml'))
return
(
copsrv:job-update-report($jobID,$ignoreValidation,$contentID,$stepChunk, if($ignoreValidation='true' or $failOnError='true') then "SuccessWithWarnings" else "SystemError",$jobType)
,
if(fn:matches(fn:tokenize($docuri,'/')[fn:last()],fn:replace($printFiles,'.docx','.xml')))
then copsrv:save-to-db($jobID,$inputXml,$contentID,$area,$ctype,fn:replace($filename,'\.docx','.xml'),$sessionValue,$jobType,$contentPath,"PRINT-READY")
else
copsrv:save-to-db($jobID,$inputXml,$contentID,$area,$ctype,fn:replace($filename,'\.docx','.xml'),$sessionValue,$jobType,$contentPath,"NORMAL")
)
else (),
rpsrv:update($jobID,$stepChunk)
,
if($failOnError='true')
then
(
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<content-type>{$ctype}</content-type>
<pipelineid>{$pipelineXml/@id/string()}</pipelineid>
<cid></cid>
<temp-location>{fn:concat($tempFilePath,'/',$cpsrv:ReadyToMove)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$inputType}</input-type>
<job-type>{$jobType}</job-type>
<failure>yes</failure>
</job>
)
,
fn:error(xs:QName('copsrv:pipeline-error'),'Schematron validation error')
)
else()
)
else if(fn:ends-with(fn:lower-case($stepChunk/@href/string()),'.sch') and $jobType ='Publish')
then
for $docUri in file:children(concat($tempFilePath,'/schema-validation/'))
let $file := fn:tokenize($docUri, '\\')[fn:last()]
let $inputXml := fn:doc($docUri)
let $outputUri := fn:concat($tempFilePath,'/',$outDir,'/',fn:replace($file,'.docx','.xml'))
let $validation := schematron:validate($inputXml, schematron:compile($SchematronUri))
return
if(schematron:is-valid($validation))
then
let $stepChunk := <step>
<type>{$outDir || ' : ' || $file}</type>
<started-on>{$startedOn}</started-on>
<location>{$outputUri}</location>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error></error>
</step>
return
(
rpsrv:update($jobID,$stepChunk)
,
file:write($outputUri, $inputXml),
file:delete($docUri),
if($totalStep eq $stepNumber)
then copsrv:save-to-db($jobID,$inputXml,$contentID,$area,$ctype,fn:replace($file,'\.docx','.xml'),$sessionValue,$jobType,$contentPath,"NORMAL")
else ()
,
admin:write-log(fn:concat('[',$jobID,'][Step ',$stepNumber,'][',$outDir,' : ',$file,']'))
)
else
let $stepChunk := <step>
<type>{$outDir || ' : ' || $file}</type>
<started-on>{$startedOn}</started-on>
<location>{$outputUri}</location>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error>
{
for $message in schematron:messages($validation)
return
<p>{fn:concat(schematron:message-level($message), ': ', schematron:message-description($message))}</p>
}
</error>
</step>
return
(
admin:write-log(fn:concat('[',$jobID,'][Step ',$stepNumber,'][error][',$outDir,' : ',$file,']'))
,
if($ignoreValidation='true' or $failOnError='false')
then (file:write($outputUri, $inputXml),
file:delete($docUri) )
else ()
,
if($totalStep eq $stepNumber)
then
(
copsrv:job-update-report($jobID,$ignoreValidation,$contentID,$stepChunk, if($ignoreValidation='true' or $failOnError='true') then "SuccessWithWarnings" else "SystemError",$jobType)
,
copsrv:save-to-db($jobID,$inputXml,$contentID,$area,$ctype,fn:replace($file,'\.docx','.xml'),$sessionValue,$jobType,$contentPath,"NORMAL")
)
else ()
,
rpsrv:update($jobID,$stepChunk)
,
if($failOnError='true')
then
(
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<content-type>{$ctype}</content-type>
<pipelineid>{$pipelineXml/@id/string()}</pipelineid>
<cid></cid>
<temp-location>{fn:concat($tempFilePath,'/',$cpsrv:ReadyToMove)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$inputType}</input-type>
<job-type>{$jobType}</job-type>
<failure>yes</failure>
</job>
)
,
fn:error(xs:QName('copsrv:pipeline-error'),'Schematron validation error')
)
else ()
)
else if(fn:ends-with(fn:lower-case($stepChunk/@href/string()),'.sch') and $jobType ='Checkin')
then
let $headeruri := concat($tempFilePath,$cpsrv:bloomsburydbk,$cpsrv:HeaderXml)
let $inputXML := fn:doc($headeruri)
for $docuri in file:children(concat($tempFilePath,'/schema-validation/'))
let $filename := fn:tokenize($docuri, '\\')[fn:last()]
(:let $inputXml := fn:doc($docuri):)
let $inputXml := if(fn:ends-with($docuri,$cpsrv:HeaderXml))
then fetch:xml($docuri,map{'xinclude':false()})
else fn:doc($docuri)
let $outputUri := fn:concat($tempFilePath,'/',$outDir,'/',fn:replace($filename,'.docx','.xml'))
let $stepChunk := <step>
<type>{$outDir || ' : ' || (if($ctype='looseleaf') then $headeruri else $docuri)}</type>
<started-on>{$startedOn}</started-on>
<location>{if($ctype='looseleaf') then $headeruri else $docuri}</location>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error></error>
</step>
return
(
rpsrv:update($jobID,$stepChunk)
,
file:write($outputUri, $inputXml)
,
if($totalStep eq $stepNumber)
then
(:copsrv:save-to-db($jobID,$inputXml,$contentID,$area,$ctype,fn:replace($filename,'\.docx','.xml'),$sessionValue,$jobType,$contentPath,"NORMAL"):)
if(fn:matches(fn:tokenize($docuri,'/')[fn:last()],fn:replace($printFiles,'.docx','.xml')))
then copsrv:save-to-db($jobID,$inputXml,$contentID,$area,$ctype,fn:replace($filename,'\.docx','.xml'),$sessionValue,$jobType,$contentPath,"PRINT-READY")
else
copsrv:save-to-db($jobID,$inputXml,$contentID,$area,$ctype,fn:replace($filename,'\.docx','.xml'),$sessionValue,$jobType,$contentPath,"NORMAL")
else ()
,
admin:write-log(fn:concat('[',$jobID,'][Step ',$stepNumber,'][',$outDir,' : ',$filename,']'))
)
else()
}
catch * {(: validation error :)
let $stepChunk := <step>
<type>Validation Error</type>
<started-on>{$startedOn}</started-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<error>
<code>{$err:code}</code>
<description>{$err:description}</description>
<additional>{$err:additional}</additional>
</error>
</step>
return
(
(:copsrv:job-update-report($jobID,$ignoreValidation,$contentID,$stepChunk,"SystemError",$jobType)
,:)
admin:write-log(fn:concat('[',$jobID,'][Error: ',$err:description,']'))
,
rpsrv:update($jobID,$stepChunk)
,
copsrv:move-to-db(
<job>
<job-id>{$jobID}</job-id>
<content-type>{$ctype}</content-type>
<pipelineid>{$pipelineXml/@id/string()}</pipelineid>
<cid></cid>
<temp-location>{fn:concat($tempFilePath,'/',$cpsrv:ReadyToMove)}</temp-location>
<ignore-validation>{$ignoreValidation}</ignore-validation>
<session-value>{$sessionValue}</session-value>
<input-type>{$inputType}</input-type>
<job-type>{$jobType}</job-type>
<failure>yes</failure>
</job>
)
,
fn:error(xs:QName('copsrv:pipeline-error'),'Validation Error')
)
}
};
(:~
: Save content to DB.
: @param $jobID ID of the Job
: @param $inputXml XML content to save
: @param $cid Content ID
: @param $area Area of the job
: @param $ctype Content type of the job
: @param $outputFileName File name to save
: @param $sessionValue Session Value
: @param $jobType Input type (Ingest,Checkin,Publish)
: @param $contentPath Content path with file name to upload into the system
: @param $cformat Content format (PRINT-READY,NORMAL)
: @return empty sequence
:)
declare %updating function copsrv:save-to-db(
$jobID as xs:string,
$inputXml as item(),
$cid as xs:string,
$area as xs:string,
$ctype as xs:string,
$outputFileName as xs:string,
$sessionValue as xs:string,
$jobType as xs:string,
$contentPath as xs:string,
$cformat as xs:string
)
{
if($jobType='Ingest' or $jobType='Checkin')
then
let $tempFilePath := fn:concat($cpsrv:TempPipelineDir,$jobID,$cpsrv:ReadyToMove)
let $createDir := file:create-dir($tempFilePath)
let $replacedfilename := fn:replace($outputFileName,'\.xml','')
(:let $coid := (:fn:concat($cid,'_',fn:replace($outputFileName,'\.xml','')):)
if($outputFileName eq $cpsrv:HeaderXml)
then fn:concat($cid,'_',$cid)
else fn:concat($cid,'_', if(fn:contains($replacedfilename , '_txt_xml')) then fn:replace($replacedfilename,'_txt_xml','') else $replacedfilename ):)
let $coid := if($ctype='looseleaf')
then
if($outputFileName eq $cpsrv:HeaderXml)
then fn:concat($cid,'_',$cid)
else fn:concat($cid,'_',fn:replace($outputFileName,'\.xml',''))
else fn:concat($cid,'_',$cid)
let $savePath := fn:replace(fn:concat($cpsrv:ContentDir,$cid,'/',$coid,$cpsrv:ContentDir,$cpsrv:LatestDir,$outputFileName),'//','/')
let $docxPath := fn:replace(fn:concat($cpsrv:ContentDir,$cid,'/',$coid,$cpsrv:ContentDir,$cpsrv:LatestDir,$cpsrv:WordDir,fn:replace($outputFileName,'.xml','.docx')),'//','/')
let $auditUri := fn:concat($cpsrv:ContentDir,$cid,'/',$coid,'/',$cpsrv:AuditFileName)
let $InfoUri := fn:concat($cpsrv:ContentDir,$cid,'/',$coid,'/',$cpsrv:InfoXml)
return
(
file:create-dir(fn:concat($tempFilePath,fn:substring-before($savePath,fn:tokenize($savePath,'/')[fn:last()])))
,
(:file:write(fn:concat($tempFilePath,$savePath),$inputXml):)
file:write(fn:concat($tempFilePath,$savePath),$inputXml, map { 'method': 'xml', 'indent': 'no' })
,(: copy original docx into db :)
let $unzipPath := fn:concat($cpsrv:TempPipelineDir,$jobID,'/zip-contents/')
return
if(file:exists(fn:concat($unzipPath,fn:replace($outputFileName,'.xml','.docx'))))
then
(
file:create-dir(fn:concat($tempFilePath,fn:substring-before($docxPath,fn:tokenize($docxPath,'/')[fn:last()])))
,
file:write-binary(fn:concat($tempFilePath,$docxPath),file:read-binary(fn:concat($unzipPath,fn:replace($outputFileName,'.xml','.docx'))))
)
else
if(fn:ends-with($contentPath,'.docx'))
then
(
file:create-dir(fn:concat($tempFilePath,fn:substring-before($docxPath,fn:tokenize($docxPath,'/')[fn:last()])))
,
file:write-binary(fn:concat($tempFilePath,$docxPath),file:read-binary($contentPath))
)
else ()
,
apsrv:content-ingest($auditUri,$sessionValue,$jobID)
,
if($jobType='Ingest' or $jobType='Checkin')
then copsrv:create-other-meta($InfoUri,$ctype,$area,$jobType,$jobID,$cid,$cformat)
else ()
,
admin:write-log(fn:concat('[',$jobID,'][Content added into the system : ',$outputFileName,']'))
)
else
if($jobType='Publish')
then
let $JobPath := fn:concat($cpsrv:TempPipelineDir,$jobID,$cpsrv:ReadyToMove,$cpsrv:JobDir,$jobID,'.xml')
let $file := file:read-text($JobPath)
let $steps := fn:substring-after($file,'</job>')
let $jobXml := fn:parse-xml(fn:substring-before($file,$steps))
let $productID := $jobXml/job/job-info/product-id/text()
let $httpTarget := fn:concat($cpsrv:DataServer,$cpsrv:Database,$cpsrv:ProductDir,$productID,$cpsrv:LatestDir,$productID,'.xml')
let $productXml := (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 $productContent in $productXml/*:product/*:contents/*:content
let $PipelineId := $productContent/@pipelineRef/string()
let $PipelinehttpTarget := fn:concat($cpsrv:DataServer,$cpsrv:Database,'/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,'|')
let $lastItemID := $productXml/product:product/product:contents/product:content/product:item[fn:last()]/@id/string()
let $productTitle := $jobXml/job/job-info/product-name/text()
let $productTaxonomyID := $productXml/product:product/@taxonomyRef/string()
let $jobDirectory := fn:substring-before($contentPath,$ctype)
let $currentDate := fn:format-date(fn:current-date(), "[Y1,4][M01][D01]")
let $contentDir := fn:concat($cpsrv:TempPipelineDir,$jobID,$cpsrv:ReadyToMove,'/Publish/',$productTitle,'/content/xml/',$ctype,'/',$currentDate)
let $metaDir := fn:concat($cpsrv:TempPipelineDir,$jobID,$cpsrv:ReadyToMove,'/Publish/',$productTitle,'/metadata/taxonomy/',$currentDate)
let $contentmetaDir := fn:concat($cpsrv:TempPipelineDir,$jobID,$cpsrv:ReadyToMove,'/Publish/',$productTitle,'/metadata/content-metadata/',$currentDate)
let $createTempDir := file:create-dir($contentDir)
let $createTempDir := if(fn:contains($normalizedsteps,'xquery')) then () else file:create-dir($metaDir)
let $createTempDir := if(fn:contains($normalizedsteps,'xquery')) then () else file:create-dir($contentmetaDir)
let $PublishedOutputFilename := fn:tokenize($outputFileName,'\\')[fn:last()]
return
(
(: Copy content to file-system :)
file:write(fn:concat($contentDir,'/',$PublishedOutputFilename),$inputXml)
,
(: Copy taxonomy to file-system :)
if(fn:contains($normalizedsteps,'xquery'))
then ()
else
let $httpTarget := fn:concat($cpsrv:DataServer,$cpsrv:Database,$cpsrv:TaxonomyDir,$productTaxonomyID,'.xml')
let $appliedTaxonomy := (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 $taxonomy := copy $c := $appliedTaxonomy/*
modify delete node $c/taxonomy/@id
return $c
return file:write(fn:concat($metaDir,'/',$productTitle,'-',$productTaxonomyID,'.xml'),$taxonomy)
,
(: Copy metadata to file-system :)
(:let $httpTarget := fn:concat($cpsrv:DataServer,'?run=eBloomsbury/cpsr-meta.xq&amp;cid=',$cid):)
if(fn:contains($normalizedsteps,'xquery'))
then ()
else
let $httpTarget := fn:concat($cpsrv:DataServer,'?run=eBloomsbury/cpsr-meta.xq&amp;cid=',$cid,'&amp;ctype=',$ctype)
(:let $httpTarget := fn:concat($cpsrv:DataServer,'?run=eBloomsbury/.xq&amp;cid=',$cid):)
let $metaUri := (http:send-request(
<http:request method='get' username='{$cpsrv:User}' password='{$cpsrv:Password}' send-authorization='true'
href='{$httpTarget}' auth-method='Basic'>
</http:request>
))
for $metadatauri in ($metaUri)[2]/uri/p/text()
let $metafilename := fn:tokenize($metadatauri,'/')[fn:last()]
let $httpTarget := fn:concat($cpsrv:DataServer,fn:replace($metadatauri,'\s','%20'))
let $metaXml := (http:send-request(
<http:request method='get' username='{$cpsrv:User}' password='{$cpsrv:Password}' send-authorization='true'
href='{$httpTarget}' auth-method='Basic'>
</http:request>
))[2]
return
(
file:write(fn:concat($contentmetaDir,'/',$metafilename),$metaXml)
)
)
else ()
};
(:~ Return error message to the UI if authentication fails :)
declare
%rest:error("copsrv:pipeline-error")
%rest:error-param("description", "{$message}")
function copsrv:pipeline-error($message)
{
fn:error()
};
(:~
: Copy header.xml file from file-system to database.
: @param $jobID ID of the Job to upload content
: @param $contentID ID of the content
: @param $headerLocation Header file location
: @param $area Related area of the documents "Academic/Professional"
: @param $ctype Content type of the ingest documents
: @param $sessionValue Session Value
: @param $ignoreValidation Ignore validation if error happens
: @param $jobType Input type (Ingest,Checkin,Publish)
: @param $appliedPipeline Content type specific pipeline
: @return empty sequence
:)
declare %private %updating function copsrv:copy-header(
$jobID as xs:string,
$contentID as xs:string,
$headerLocation as xs:string,
$area as xs:string,
$ctype as xs:string,
$sessionValue as xs:string,
$ignoreValidation as xs:string,
$jobType as xs:string,
$appliedPipeline as item()
)
{
if($jobType='Checkin')
then
(
let $headerPath := fn:concat($headerLocation,$cpsrv:HeaderXml)
for $file in file:children($headerLocation)[fn:ends-with(.,'.xml')]
let $filename := fn:tokenize($file,'\\')[fn:last()]
let $path := fn:concat($cpsrv:TempPipelineDir,$jobID)
let $validateDir:= (
for $child at $pos in $appliedPipeline/child::*
return
if($child[local-name()='validate'])
then
if($child/preceding-sibling::*[@label][1])
then $child//preceding-sibling::*[1]/@label/string()
else
if($pos eq 1)
then fn:concat('step',fn:string($pos))
else fn:concat('step',fn:string(fn:sum($pos - 1)))
else ()
)[1]
let $savePath := fn:replace(fn:concat($cpsrv:ContentDir,$contentID,'/',$contentID,$cpsrv:ContentDir,$cpsrv:LatestDir,$cpsrv:HeaderXml),'//','/')
let $headerXML := if(file:exists($headerPath))
then
copy $c := fetch:xml($headerPath,map{'xinclude':false()})
modify (
for $include in $c//*:include/@href
return
replace value of node $include with fn:replace($include,'.docx','.xml')
)
return $c
else ()
for $files in fetch:xml($file,map{'xinclude':false()})
return
(
file:create-dir(fn:concat($cpsrv:TempPipelineDir,$jobID,'/',$validateDir))
,
if(file:exists($headerPath))
then file:write(fn:concat($cpsrv:TempPipelineDir,$jobID,'/',$validateDir,'/',$cpsrv:HeaderXml),$headerXML)
else ()
,
file:write(fn:concat($cpsrv:TempPipelineDir,$jobID,'/',$validateDir,'/',$filename),$files )
)
)
else
let $headerPath := fn:concat($headerLocation,$cpsrv:HeaderXml)
let $path := fn:concat($cpsrv:TempPipelineDir,$jobID)
let $validateDir:= (
for $child at $pos in $appliedPipeline/child::*
return
if($child[local-name()='validate'])
then
if($child/preceding-sibling::*[@label][1])
then $child//preceding-sibling::*[1]/@label/string()
else
if($pos eq 1)
then fn:concat('step',fn:string($pos))
else fn:concat('step',fn:string(fn:sum($pos - 1)))
else ()
)[1]
let $savePath := fn:replace(fn:concat($cpsrv:ContentDir,$contentID,'/',$contentID,$cpsrv:ContentDir,$cpsrv:LatestDir,$cpsrv:HeaderXml),'//','/')
let $headerXML := copy $c := fetch:xml($headerPath,map{'xinclude':false()})
modify (
for $include in $c//*:include/@href
return
replace value of node $include with fn:replace($include,'.docx','.xml')
)
return $c
return
(
file:create-dir(fn:concat($cpsrv:TempPipelineDir,$jobID,'/',$validateDir))
,
file:write(fn:concat($cpsrv:TempPipelineDir,$jobID,'/',$validateDir,'/',$cpsrv:HeaderXml),$headerXML)
)
};
(:~
: Update final and time of the report
: @param $jobID ID of the Job to upload content
: @param $ignoreValidation Ignore validation if error happens
: @param $contentID ID of the content
: @return empty sequence
:)
declare %private %updating function copsrv:update-final-status(
$jobID as xs:string,
$ignoreValidation as xs:string,
$contentID as xs:string
)
{
let $updateStatus := jobs:eval(
"
import module namespace rpsrv = 'http://www.rave-tech.com/bloomsbury/report-psrv' at 'report-psrv.xqm';
declare variable $jobID external;
declare variable $ignoreValidation external;
declare variable $contentID external;
rpsrv:update-final-status($jobID,$ignoreValidation,$contentID)
",
map
{
xs:QName('jobID'):$jobID,
xs:QName('ignoreValidation'):$ignoreValidation,
xs:QName('contentID'):$contentID
},
map{'start':'PT0.4S'}
)
return ()
};
(:~
: Update final and time of the report
: @param $jobID ID of the Job to upload content
: @param $ignoreValidation Ignore validation if error happens
: @param $contentID ID of the content
: @param $stepChunk Step chunk
: @param $status (failure|SystemError)
: @param $jobType Input type (Ingest,Checkin,Publish)
: @return empty sequence
:)
declare %private %updating function copsrv:job-update-report(
$jobID as xs:string,
$ignoreValidation as xs:string,
$contentID as xs:string,
$stepChunk as element(step),
$status as xs:string,
$jobType as xs:string
)
{
let $updateStatus := jobs:eval(
"
import module namespace copsrv = 'http://www.rave-tech.com/bloomsbury/contents-psrv' at 'content-psrv.xqm';
declare variable $jobID external;
declare variable $ignoreValidation external;
declare variable $contentID external;
declare variable $stepChunk external;
declare variable $status external;
delcare variable $jobType external;
copsrv:update-report($jobID,$ignoreValidation,$contentID,$stepChunk,$status,$jobType)
",
map
{
xs:QName('jobID'):$jobID,
xs:QName('ignoreValidation'):$ignoreValidation,
xs:QName('contentID'):$contentID,
xs:QName('stepChunk'):$stepChunk,
xs:QName('status'):$status,
xs:QName('jobType'):$jobType
},
map{'start':'PT0.2S'}
)
return ()
};
(:~
: 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
: @param $stepChunk Step chunk
: @param $status (failure)
: @param $jobType Input type (Ingest,Checkin,Publish)
: @return empty sequence
:)
declare %updating function copsrv:update-report(
$jobID as xs:string,
$ignoreValidation as xs:string,
$contentID as xs:string,
$stepChunk as element(step),
$status as xs:string,
$jobType as xs:string
)
{
file:append(fn:concat($cpsrv:TempPipelineDir,$jobID,$cpsrv:ReadyToMove,$cpsrv:JobDir,$jobID,'.xml'),$stepChunk)
,
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,'</job>')
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('<steps>',fn:normalize-space($steps),'</steps>'))
return $c
return
let $jobXml := copy $c := $jobXml
modify
(
replace node $c/job/ingestion-report/status with <status>{if($status='SystemError') then 'Failure' else $status}</status>,
replace node $c/job/ingestion-report/finished-on with <finished-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</finished-on>
)
return $c
return file:write($reportUri,$jobXml)
,
if(($status='Failure' or $status='SystemError') and $jobType!='Publish')
then file:delete(fn:concat($cpsrv:TempPipelineDir,$jobID,$cpsrv:ReadyToMove,$cpsrv:ContentDir),fn:true())
else ()
};
(:~
: Add/append other meta information of the document.
: @param $infoUri Location of the info file to update
: @param $ctype Content Type of the document
: @param $area Related area of the document
: @param $status Status of the document
: @param $jobID ID of the Job
: @param $cid as xs:string,
: @param $cformat Content format (PRINT-READY,NORMAL)
: @return empty sequence
:)
declare %updating function copsrv:create-other-meta(
$infoUri as xs:string,
$ctype as xs:string,
$area as xs:string?,
$status as xs:string,
$jobID as xs:string,
$cid as xs:string,
$cformat as xs:string
)
{
let $tempFilePath := fn:concat($cpsrv:TempPipelineDir,$jobID,$cpsrv:ReadyToMove)
return
(
file:create-dir(fn:concat($tempFilePath,fn:substring-before($infoUri,fn:tokenize($infoUri,'/')[fn:last()])))
,
file:write(
fn:concat($tempFilePath,$infoUri),
<info>
<ctype>{$ctype}</ctype>
<area>{$area}</area>
<cid>{$cid}</cid>
<status>{$status}</status>
<ingest-job>{$jobID}</ingest-job>
{if($cformat='PRINT-READY') then <content-format>PRINT-READY</content-format> else ()}
</info>
)
,
cpsrv:update-message("[Content Info][Other information has been added " || $infoUri || "]")
)
};
(:~
: To get checkout information
: @param $uri The uri of the files which has been selected to checkin (db uri)
: @param $userID Current User ID who wants to checkin
: @return element (locks)
:)
declare function copsrv:lock-information(
$uri as xs:string,
$userID as xs:string
) as element(locks)
{
<locks>
{
let $httpTarget := fn:concat($cpsrv:DataServer,$cpsrv:Database,$cpsrv:LockDir,$userID,'.xml')
let $appliedPipeline := (http:send-request(
<http:request method='get' username='{$cpsrv:User}' password='{$cpsrv:Password}' send-authorization='true'
href='{$httpTarget}'>
</http:request>
))[2]
let $lockDoc := $appliedPipeline/locks (: navin to check:)
for $eachUri in fn:tokenize($uri,'\|')
return
<lock>
<uri>{$eachUri}</uri>
{
$lockDoc/locks/lock/files[file/uri=$eachUri]/version,
$lockDoc/locks/lock/files[file/uri=$eachUri]/parent::lock/requester-id,
$lockDoc/locks/lock/files[file/uri=$eachUri]/parent::lock/requester-email
}
</lock>
}
</locks>
};
(:~
: Copy RelaxNG file into temporary directory.
: @return empty sequence
:)
declare %private %updating function copsrv:copy-relaxng()
{
let $httpTarget := fn:concat($cpsrv:DataServer,$cpsrv:Database,$cpsrv:ValidationDir)
let $allSchema := http:send-request(
<http:request method='get' username='{$cpsrv:User}' password='{$cpsrv:Password}' send-authorization='true'
href='{$httpTarget}'>
</http:request>
)
for $rnc in ($allSchema)[2]/rest:database/rest:resource[@type='raw']/text()
let $fileName := fn:tokenize($rnc,'/')[fn:last()]
let $httpTarget := fn:concat($cpsrv:DataServer,$cpsrv:Database,'/',$rnc)
let $schema := (http:send-request(
<http:request method='get' username='{$cpsrv:User}' password='{$cpsrv:Password}' send-authorization='true'
href='{$httpTarget}'>
</http:request>
))[2]
return
try
{
file:write-text(fn:concat(file:temp-dir(),$fileName), bin:decode-string($schema,'UTF-8'))
}
catch *
{
admin:write-log("[Copy RelaxNG][Error: " || $err:description || "]"),
admin:write-log("[Copy RelaxNG][Error: " || $err:additional || "]"),
update:output('')
}
};
declare %updating function copsrv:move-to-db(
$jonInfo as element(job)
)
{
let $httpTarget := fn:concat(fn:substring-before($cpsrv:DataServer,'/rest/'),'/data')
let $sendRequest := http:send-request(
<http:request
method='POST'
username='{$cpsrv:User}'
password='{$cpsrv:Password}'
send-authorization='true'
auth-method='Basic'
href='{$httpTarget}'>
<http:header name="Content-Type" value="application/xml"/>
<http:body media-type='application/xml'>
<job>{$jonInfo/*}</job>
</http:body>
</http:request>
)
return admin:write-log('==========request-sent===============')
};