teacup/data.xqm

954 lines
46 KiB
Plaintext

(:~
: Miscellaneous RESTXQ functions. :
:
: @author Rave Technologies, https://www.rave-tech.com/, 2017
:)
module namespace bdata = 'http://www.rave-tech.com/bloomsbury/data';
import module namespace config = 'http://www.rave-tech.com/bloomsbury/config' at './module/config.xqm';
import module namespace contents = 'http://www.rave-tech.com/bloomsbury/contents' at 'content.xqm';
import module namespace blcommon = 'http://www.rave-tech.com/bloomsbury/common' at 'common.xqm';
(:~
: To get processed job information from processing server and ingest into the Database
: @param <job><job-id></job-id><cid></cid><temp-location></temp-location><ignore-validation></ignore-validation><session-value></session-value><input-type></input-type><job-type></job-type></job>
: <job-id> Job ID
: <cid> Content ID
: <temp-location> Job Location
: <ignore-validation> Ignore validation if error happens
: <session-value> Session Value
: <input-type> Input type (xml,word,zip)
: <job-type> Input type (Ingest,Checkin,Publish)
: @return element(result)
:)
declare
%updating
%rest:path("/data")
%rest:POST("{$body}")
%rest:consumes("application/xml", "text/xml")
function bdata:create(
$body as document-node()
)
{
try
{
let $copyStart := fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))
let $jobID := $body/job/job-id/text()
let $_log := admin:write-log(fn:concat('*******job move to db: ', $jobID, ' *********'))
return
if($body/job/failure='yes')
then
let $path := fn:concat(fn:substring-before($body/job/temp-location/text(),$jobID),$jobID,'/ReadyToMove/')
for $files in file:list($path,fn:true())[fn:contains(.,'jobs\')][fn:ends-with(.,'.xml')]
let $filePath := fn:replace(fn:concat($path,$files),'/','\\')
let $file := file:read-text($filePath)
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>Failure</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
let $jobType := $jobXml/job/job-info/job-type/text()
let $userID := $jobXml/job/job-info/requester-id/text()
let $userEMail := $jobXml/job/job-info/requester-email/text()
let $submittedOn := $jobXml/job/job-info/submitted-on/text()
let $mailSent := $jobXml/job/job-info/mail-required/text()
let $productid := $jobXml/job/job-info/product-id/text()
let $productXML := fn:doc(fn:concat($config:CoreDatabase,$config:ProductDir,$productid,$config:LatestDir,$productid,'.xml'))
let $lastPipelineID := $productXML/*:product/*:contents/*:content[fn:last()]/@pipelineRef/string()
let $appliedPipeline := data($body/job/pipelineid)
return
if($jobType='Publish')
then
(
db:replace($config:CoreDatabase,$files,$jobXml)
,
if($mailSent='true' and ($lastPipelineID=$appliedPipeline))
then
bdata:send-mail(
<email-job-details>
<jobid>{$jobID}</jobid>
{
if($jobType='Publish')
then <product-name>{$jobXml/job/job-info/product-name/text()}</product-name>
else ()
}
<email>{$userEMail}</email>
<job-type>{$jobType}</job-type>
<status>Failure</status>
<username>{db:open($config:CoreDatabase,fn:concat($config:UserDir,$userID,'.xml'))/user/name/text()}</username>
<submitted-on>{$submittedOn}</submitted-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<authorize>ud6hoko1Bg132WAy6jVPHujyfRHj9MaNrPmz6ObSQ7RSwbENp3ranKWKWRxBBW8pHUHMh8KpVfS7t9At7Z3DLqstunsmNbjCPxg9lSmG5v0NGZETVX2xy1vqPB0PzJ5L</authorize>
</email-job-details>
)
else ()
)
else
(
db:replace($config:CoreDatabase,$files,$jobXml)
,
if($mailSent='true')
then
bdata:send-mail(
<email-job-details>
<jobid>{$jobID}</jobid>
{
if($jobType='Publish')
then <product-name>{$jobXml/job/job-info/product-name/text()}</product-name>
else ()
}
<email>{$userEMail}</email>
<job-type>{$jobType}</job-type>
<status>Failure</status>
<username>{db:open($config:CoreDatabase,fn:concat($config:UserDir,$userID,'.xml'))/user/name/text()}</username>
<submitted-on>{$submittedOn}</submitted-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<authorize>ud6hoko1Bg132WAy6jVPHujyfRHj9MaNrPmz6ObSQ7RSwbENp3ranKWKWRxBBW8pHUHMh8KpVfS7t9At7Z3DLqstunsmNbjCPxg9lSmG5v0NGZETVX2xy1vqPB0PzJ5L</authorize>
</email-job-details>
)
else ()
)
else
(
if($body/job/job-type='Ingest')
then
let $cid := $body/job/cid/text()
let $path := $body/job/temp-location/text()
let $ignoreValidation := $body/job/ignore-validation/text()
let $sessionValue := $body/job/session-value/text()
let $inputType := $body/job/input-type/text()
let $jobType := $body/job/job-type/text()
let $ctype := $body/job/content-type/text()
return
(: verify if job has failure status skip db ingestion :)
let $jobStatus := for $files in file:list($path,fn:true())[fn:contains(.,'jobs\')][fn:ends-with(.,'.xml')]
let $filePath := fn:replace(fn:concat($path,$files),'/','\\')
let $file := file:read-text($filePath)
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
if($jobXml/job/ingestion-report/status[.='InQueue'] or $jobXml/job/ingestion-report/status[.=''])
then
(
if($jobXml/job/ingestion-report/steps/step/error/* or $jobXml/job/ingestion-report/steps/step/*:errors/*)
then
if($jobXml//status[.='Failure'])
then "Failure"
else
if($ignoreValidation='true')
then "SuccessWithWarnings"
else "Failure"
else "Success"
)
else ()
return
(
(: ingest content into the system if job status is not fail :)
if($jobStatus="Failure")
then ()
else
for $files in file:list($path,fn:true())[fn:ends-with(.,'.xml') or fn:ends-with(.,'.docx')]
let $filePath := fn:concat($path,'\',$files)
return
if($files[fn:ends-with(.,'docx')])
then db:store(blcommon:get-db-name($ctype), $files, file:read-binary($filePath))
else
if($files[fn:ends-with(.,$config:AuditFileName)])
then
if(db:exists($config:AuditDatabase,fn:concat($ctype,$files)))
then
let $existingAudit := db:open($config:AuditDatabase,fn:concat($ctype,'/',$files))
return insert node fn:doc($filePath)/audits/audit as last into $existingAudit/audits
else db:replace($config:AuditDatabase,fn:concat($ctype,'/',$files),fn:doc($filePath))
else
if(fn:contains($files,'jobs\'))
then ()
else
(
if(fn:ends-with($files,$config:HeaderXml))
then db:replace(blcommon:get-db-name($ctype),$files,fetch:xml($filePath,map{'xinclude':false()}))
else db:replace(blcommon:get-db-name($ctype),$files,fn:doc($filePath))
)
,
for $files in file:list($path,fn:true())[fn:contains(.,'jobs\')][fn:ends-with(.,'.xml')]
let $filePath := fn:replace(fn:concat($path,$files),'/','\\')
let $file := file:read-text($filePath)
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
let $jobType := $jobXml/job/job-info/job-type/text()
let $userID := $jobXml/job/job-info/requester-id/text()
let $userEMail := $jobXml/job/job-info/requester-email/text()
let $submittedOn := $jobXml/job/job-info/submitted-on/text()
let $mailSent := $jobXml/job/job-info/mail-required/text()
return
if($jobXml/job/ingestion-report/status[.='InQueue'] or $jobXml/job/ingestion-report/status[.=''])
then
(
if($jobXml/job/ingestion-report/steps/step/error/* or $jobXml/job/ingestion-report/steps/step/*:errors/*)
then
if($jobXml//status[.='Failure'])
then
let $jobXml := copy $c := $jobXml
modify
(
replace node $c/job/ingestion-report/status with <status>Failure</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
(
db:replace(blcommon:get-db-name($ctype),$files,$jobXml)
,
if($mailSent='true')
then
bdata:send-mail(
<email-job-details>
<jobid>{$jobID}</jobid>
<email>{$userEMail}</email>
<job-type>{$jobType}</job-type>
<filename>{fn:tokenize($jobXml/job/job-info/uploaded-file,'/')[fn:last()]}</filename>
<status>Failure</status>
<username>{db:open($config:CoreDatabase,fn:concat($config:UserDir,$userID,'.xml'))/user/name/text()}</username>
<submitted-on>{$submittedOn}</submitted-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<authorize>ud6hoko1Bg132WAy6jVPHujyfRHj9MaNrPmz6ObSQ7RSwbENp3ranKWKWRxBBW8pHUHMh8KpVfS7t9At7Z3DLqstunsmNbjCPxg9lSmG5v0NGZETVX2xy1vqPB0PzJ5L</authorize>
</email-job-details>
)
else ()
)
else
if($ignoreValidation='true')
then
let $jobXml := copy $c := $jobXml
modify
(
replace node $c/job/ingestion-report/status with <status>SuccessWithWarnings</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
(
db:replace($config:CoreDatabase,$files,$jobXml)
,
if($mailSent='true')
then
bdata:send-mail(
<email-job-details>
<jobid>{$jobID}</jobid>
<email>{$userEMail}</email>
<job-type>{$jobType}</job-type>
<filename>{fn:tokenize($jobXml/job/job-info/uploaded-file,'/')[fn:last()]}</filename>
<status>SuccessWithWarnings</status>
<username>{db:open($config:CoreDatabase,fn:concat($config:UserDir,$userID,'.xml'))/user/name/text()}</username>
<submitted-on>{$submittedOn}</submitted-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<authorize>ud6hoko1Bg132WAy6jVPHujyfRHj9MaNrPmz6ObSQ7RSwbENp3ranKWKWRxBBW8pHUHMh8KpVfS7t9At7Z3DLqstunsmNbjCPxg9lSmG5v0NGZETVX2xy1vqPB0PzJ5L</authorize>
</email-job-details>
)
else ()
)
else
(
let $jobXml := copy $c := $jobXml
modify
(
replace node $c/job/ingestion-report/status with <status>Failure</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
(
db:replace($config:CoreDatabase,$files,$jobXml)
,
if($mailSent='true')
then
bdata:send-mail(
<email-job-details>
<jobid>{$jobID}</jobid>
<email>{$userEMail}</email>
<job-type>{$jobType}</job-type>
<filename>{fn:tokenize($jobXml/job/job-info/uploaded-file,'/')[fn:last()]}</filename>
<status>Failure</status>
<username>{db:open($config:CoreDatabase,fn:concat($config:UserDir,$userID,'.xml'))/user/name/text()}</username>
<submitted-on>{$submittedOn}</submitted-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<authorize>ud6hoko1Bg132WAy6jVPHujyfRHj9MaNrPmz6ObSQ7RSwbENp3ranKWKWRxBBW8pHUHMh8KpVfS7t9At7Z3DLqstunsmNbjCPxg9lSmG5v0NGZETVX2xy1vqPB0PzJ5L</authorize>
</email-job-details>
)
else ()
)
)
else
let $jobXml := copy $c := $jobXml
modify
(
replace node $c/job/ingestion-report/status with <status>Success</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
(
db:replace($config:CoreDatabase,$files,$jobXml)
,
if($mailSent='true')
then
bdata:send-mail(
<email-job-details>
<jobid>{$jobID}</jobid>
<email>{$userEMail}</email>
<job-type>{$jobType}</job-type>
<filename>{fn:tokenize($jobXml/job/job-info/uploaded-file,'/')[fn:last()]}</filename>
<status>Success</status>
<username>{db:open($config:CoreDatabase,fn:concat($config:UserDir,$userID,'.xml'))/user/name/text()}</username>
<submitted-on>{$submittedOn}</submitted-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<authorize>ud6hoko1Bg132WAy6jVPHujyfRHj9MaNrPmz6ObSQ7RSwbENp3ranKWKWRxBBW8pHUHMh8KpVfS7t9At7Z3DLqstunsmNbjCPxg9lSmG5v0NGZETVX2xy1vqPB0PzJ5L</authorize>
</email-job-details>
)
else ()
)
)
else ()
)
else
if($body/job/job-type='Checkin')
then
let $cid := $body/job/cid/text()
let $path := $body/job/temp-location/text()
let $ignoreValidation := $body/job/ignore-validation/text()
let $sessionValue := $body/job/session-value/text()
let $inputType := $body/job/input-type/text()
let $jobType := $body/job/job-type/text()
let $ctype := $body/job/content-type/text()
return
((: verify if job has failure status skip db ingestion :)
let $jobStatus := for $files in file:list($path,fn:true())[fn:contains(.,'jobs\')][fn:ends-with(.,'.xml')]
let $filePath := fn:replace(fn:concat($path,$files),'/','\\')
let $file := file:read-text($filePath)
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
if($jobXml/job/ingestion-report/status[.='InQueue'] or $jobXml/job/ingestion-report/status[.=''])
then
(
if($jobXml/job/ingestion-report/steps/step/error/* or $jobXml/job/ingestion-report/steps/step/*:errors/*)
then
if($jobXml//status[.='Failure'])
then "Failure"
else
if($ignoreValidation='true')
then "SuccessWithWarnings"
else "Failure"
else "Success"
)
else ()
return
if($jobStatus='Failure')
then ()
else
for $files in file:list($path,fn:true())[fn:ends-with(.,'.xml') or fn:ends-with(.,'.docx')]
let $filePath := fn:replace(fn:concat($path,$files),'/','\\')
let $outputFileName := fn:tokenize($files,'\\')[fn:last()]
return
(
if(fn:contains($filePath,'\content\latest\') and fn:ends-with($filePath,'.xml'))
then
let $lockedUri := fn:substring-after(db:open($config:CoreDatabase)/locks/lock/files/file[fn:tokenize(uri,'/')[fn:last()]=$outputFileName][fn:tokenize(uri,'/')[4]=$cid]/uri/text(),fn:concat('/', blcommon:get-db-name($ctype),'/'))
let $cid := if(fn:tokenize($lockedUri,'/')[2]) then fn:tokenize($lockedUri,'/')[2] else $cid
let $coid := if(fn:tokenize($lockedUri,'/')[3]) then fn:tokenize($lockedUri,'/')[3] else fn:concat($cid,'_',fn:substring-before($outputFileName,'.xml'))
return
(
bdata:create-version($cid,$coid,$outputFileName,$ctype,'Content')
)
else ()
,
if($files[fn:ends-with(.,'docx')])
then db:store(blcommon:get-db-name($ctype), $files, file:read-binary($filePath))
else
if($files[fn:ends-with(.,'audit.xml')])
then
let $comment := db:open($config:CoreDatabase,fn:concat($config:JobDir,$jobID,'.xml'))/job/job-info/comment/text()
return bdata:update($filePath,$sessionValue,'Checkin',$ctype,$comment)
else
if(fn:contains($files,'jobs\'))
then ()
else
(
if(fn:ends-with($files,$config:HeaderXml))
then db:replace(blcommon:get-db-name($ctype),$files,fetch:xml($filePath,map{'xinclude':false()}))
else db:replace(blcommon:get-db-name($ctype),$files,fn:doc($filePath))
,
let $lockRecord := db:open($config:CoreDatabase,'/locks/')/locks/lock[some $uri in files/file/uri satisfies fn:contains(., fn:replace(fn:substring-after($filePath,'ReadyToMove'),'\\','/'))]
return
if(fn:count($lockRecord/files/file) eq 1)
then delete node $lockRecord
else delete node $lockRecord/files/file[fn:contains(uri, fn:replace(fn:substring-after($filePath,'ReadyToMove'),'\\','/'))]
)
)
, (: update job :)
for $files in file:list($path,fn:true())[fn:contains(.,'jobs\')][fn:ends-with(.,'.xml')]
let $filePath := fn:replace(fn:concat($path,$files),'/','\\')
let $outputFileName := fn:tokenize($files,'\\')[fn:last()]
let $file := file:read-text($filePath)
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
let $jobXml := copy $c := $jobXml
modify
(
for $files in file:list($path,fn:true())[fn:ends-with(.,'.xml')][fn:contains(.,'\content\latest\')]
let $outputFileName := fn:tokenize($files,'\\')[fn:last()]
return
for $file in $c/job/job-info/lock/file[fn:contains(fn:tokenize(uri,'/')[fn:last()]
,fn:replace($outputFileName,'.docx','.xml'))
]
[fn:tokenize(uri,'/')[4]=$cid]
return insert node <check-in>Yes</check-in> as last into $file
)
return $c
let $jobType := $jobXml/job/job-info/job-type/text()
let $userID := $jobXml/job/job-info/requester-id/text()
let $userEMail := $jobXml/job/job-info/requester-email/text()
let $submittedOn := $jobXml/job/job-info/submitted-on/text()
let $mailSent := $jobXml/job/job-info/mail-required/text()
return
if($jobXml/job/ingestion-report/status[.='InQueue'] or $jobXml/job/ingestion-report/status[.=''])
then
(
if($jobXml/job/ingestion-report/steps/step/error/* or $jobXml/job/ingestion-report/steps/step/*:errors/*)
then
if($jobXml//status[.='Failure'])
then
let $jobXml := copy $c := $jobXml
modify
(
replace node $c/job/ingestion-report/status with <status>Failure</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
(
db:replace($config:CoreDatabase,$files,$jobXml)
,
if($mailSent='true')
then
bdata:send-mail(
<email-job-details>
<jobid>{$jobID}</jobid>
<email>{$userEMail}</email>
<job-type>{$jobType}</job-type>
<filename>{fn:tokenize($jobXml/job/job-info/uploaded-file,'/')[fn:last()]}</filename>
<status>Failure</status>
<username>{db:open($config:CoreDatabase,fn:concat($config:UserDir,$userID,'.xml'))/user/name/text()}</username>
<submitted-on>{$submittedOn}</submitted-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<authorize>ud6hoko1Bg132WAy6jVPHujyfRHj9MaNrPmz6ObSQ7RSwbENp3ranKWKWRxBBW8pHUHMh8KpVfS7t9At7Z3DLqstunsmNbjCPxg9lSmG5v0NGZETVX2xy1vqPB0PzJ5L</authorize>
</email-job-details>
)
else ()
)
else
if($ignoreValidation='true')
then
let $jobXml := copy $c := $jobXml
modify
(
replace node $c/job/ingestion-report/status with <status>SuccessWithWarnings</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
(
db:replace($config:CoreDatabase,$files,$jobXml)
,
if($mailSent='true')
then
bdata:send-mail(
<email-job-details>
<jobid>{$jobID}</jobid>
<email>{$userEMail}</email>
<job-type>{$jobType}</job-type>
<filename>{fn:tokenize($jobXml/job/job-info/uploaded-file,'/')[fn:last()]}</filename>
<status>SuccessWithWarnings</status>
<username>{db:open($config:CoreDatabase,fn:concat($config:UserDir,$userID,'.xml'))/user/name/text()}</username>
<submitted-on>{$submittedOn}</submitted-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<authorize>ud6hoko1Bg132WAy6jVPHujyfRHj9MaNrPmz6ObSQ7RSwbENp3ranKWKWRxBBW8pHUHMh8KpVfS7t9At7Z3DLqstunsmNbjCPxg9lSmG5v0NGZETVX2xy1vqPB0PzJ5L</authorize>
</email-job-details>
)
else ()
)
else
(
let $jobXml := copy $c := $jobXml
modify
(
for $d in $c/job/job-info/lock/file
return
(insert node <check-in>No</check-in> as last into $d,
replace node $d/latest-version with <latest-version>{$d/previous-version/string()}</latest-version>,
replace node $c/job/ingestion-report/status with <status>Failure</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
(
db:replace($config:CoreDatabase,$files,$jobXml)
,
if($mailSent='true')
then
bdata:send-mail(
<email-job-details>
<jobid>{$jobID}</jobid>
<email>{$userEMail}</email>
<job-type>{$jobType}</job-type>
<filename>{fn:tokenize($jobXml/job/job-info/uploaded-file,'/')[fn:last()]}</filename>
<status>Failure</status>
<username>{db:open($config:CoreDatabase,fn:concat($config:UserDir,$userID,'.xml'))/user/name/text()}</username>
<submitted-on>{$submittedOn}</submitted-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<authorize>ud6hoko1Bg132WAy6jVPHujyfRHj9MaNrPmz6ObSQ7RSwbENp3ranKWKWRxBBW8pHUHMh8KpVfS7t9At7Z3DLqstunsmNbjCPxg9lSmG5v0NGZETVX2xy1vqPB0PzJ5L</authorize>
</email-job-details>
)
else ()
)
)
else
let $jobXml := copy $c := $jobXml
modify
(
replace node $c/job/ingestion-report/status with <status>Success</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
(
db:replace($config:CoreDatabase,$files,$jobXml)
,
if($mailSent='true')
then
bdata:send-mail(
<email-job-details>
<jobid>{$jobID}</jobid>
<email>{$userEMail}</email>
<job-type>{$jobType}</job-type>
<filename>{fn:tokenize($jobXml/job/job-info/uploaded-file,'/')[fn:last()]}</filename>
<status>Success</status>
<username>{db:open($config:CoreDatabase,fn:concat($config:UserDir,$userID,'.xml'))/user/name/text()}</username>
<submitted-on>{$submittedOn}</submitted-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<authorize>ud6hoko1Bg132WAy6jVPHujyfRHj9MaNrPmz6ObSQ7RSwbENp3ranKWKWRxBBW8pHUHMh8KpVfS7t9At7Z3DLqstunsmNbjCPxg9lSmG5v0NGZETVX2xy1vqPB0PzJ5L</authorize>
</email-job-details>
)
else ()
)
)
else ()
, (: update property for delete/print-ready content:)
contents:delete-content($ctype,$jobID,$cid,$path,$sessionValue)
)
else
if($body/job/job-type='Publish')
then
let $_log := admin:write-log(fn:concat('*******Publish-job: ', $jobID, ' *********'))
let $cid := $body/job/cid/text()
let $path := $body/job/temp-location/text()
let $_log := admin:write-log(fn:concat('*******TempPath: ', $path, ' *********'))
let $ignoreValidation := $body/job/ignore-validation/text()
let $sessionValue := $body/job/session-value/text()
let $ProductName := $body/job/input-type/text()
let $jobType := $body/job/job-type/text()
let $productid := db:open($config:CoreDatabase,$config:ProductDir)/*:product[@name=$ProductName][fn:not(@delete='Yes')][fn:not(fn:matches(fn:base-uri(.),$config:VersionDir))]/@id/string()
let $productXML := fn:doc(fn:concat($config:CoreDatabase,$config:ProductDir,$productid,$config:LatestDir,$productid,'.xml'))
let $lastPipelineID := $productXML/*:product/*:contents/*:content[fn:last()]/@pipelineRef/string()
let $appliedPipeline := fn:data($body/job/pipelineid)
let $path := fn:concat(fn:substring-before($path,$jobID),$jobID,'/ReadyToMove/')
let $zipName := fn:concat($ProductName,'-',fn:string(fn:format-dateTime(fn:current-dateTime(), '[Y0001][M01][D01][H01][m01][s01][f01]')),'.zip')
(:let $createDir := file:create-dir(fn:concat($path,'Publish/',$ProductName,'/report/')):)
return
(
let $_log := admin:write-log(fn:concat('*******Path: ', $path, ' *********'))
for $files in file:list($path,fn:true())[fn:contains(.,'jobs\')][fn:ends-with(.,'.xml')]
let $filePath := fn:replace(fn:concat($path,$files),'/','\\')
let $outputFileName := fn:tokenize($files,'\\')[fn:last()]
let $file := file:read-text($filePath)
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
let $jobType := $jobXml/job/job-info/job-type/text()
let $userID := $jobXml/job/job-info/requester-id/text()
let $userEMail := $jobXml/job/job-info/requester-email/text()
let $submittedOn := $jobXml/job/job-info/submitted-on/text()
let $mailSent := $jobXml/job/job-info/mail-required/text()
return
if($jobXml/job/ingestion-report/status[.='InQueue'] or $jobXml/job/ingestion-report/status[.=''])
then
(
if($jobXml/job/ingestion-report/steps/step/error/*)
then
if($jobXml//status[.='Failure'])
then
let $jobXml := copy $c := $jobXml
modify
(
replace node $c/job/ingestion-report/status with <status>Failure</status>,
if($ignoreValidation='true')
then
( replace node $c/job/job-info/location with <location>{fn:concat(fn:substring-before($path,$jobID),$jobID,'/Publish/',$zipName)}</location>)
else(),
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
(
db:replace($config:CoreDatabase,$files,$jobXml),
file:write($filePath,$jobXml),
(:file:write(fn:concat($path,'Publish/',$ProductName,'/report/',$jobID,'.xml'),$jobXml),:)
if($mailSent='true' and ($lastPipelineID=$appliedPipeline))
then
bdata:send-mail(
<email-job-details>
<jobid>{$jobID}</jobid>
<product-name>{$ProductName}</product-name>
<email>{$userEMail}</email>
<job-type>{$jobType}</job-type>
<status>Failure</status>
<username>{db:open($config:CoreDatabase,fn:concat($config:UserDir,$userID,'.xml'))/user/name/text()}</username>
<submitted-on>{$submittedOn}</submitted-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<authorize>ud6hoko1Bg132WAy6jVPHujyfRHj9MaNrPmz6ObSQ7RSwbENp3ranKWKWRxBBW8pHUHMh8KpVfS7t9At7Z3DLqstunsmNbjCPxg9lSmG5v0NGZETVX2xy1vqPB0PzJ5L</authorize>
</email-job-details>
)
else ()
)
else
if($ignoreValidation='true')
then
let $jobXml := if(($jobXml/job/ingestion-report/steps/step[type='Pipeline run failure'])[1])
then
copy $c := $jobXml
modify
(
replace node $c/job/ingestion-report/status with <status>Failure</status>,
replace node $c/job/job-info/location with <location>{fn:concat(fn:substring-before($path,$jobID),$jobID,'/Publish/',$zipName)}</location>,
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
else
copy $c := $jobXml
modify
(
replace node $c/job/ingestion-report/status with <status>SuccessWithWarnings</status>,
replace node $c/job/job-info/location with <location>{fn:concat(fn:substring-before($path,$jobID),$jobID,'/Publish/',$zipName)}</location>,
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
(
db:replace($config:CoreDatabase,$files,$jobXml),
file:write($filePath,$jobXml),
(:file:write(fn:concat($path,'Publish/',$ProductName,'/report/',$jobID,'.xml'),$jobXml),:)
if($mailSent='true' and ($lastPipelineID=$appliedPipeline))
then
bdata:send-mail(
<email-job-details>
<jobid>{$jobID}</jobid>
<product-name>{$ProductName}</product-name>
<email>{$userEMail}</email>
<job-type>{$jobType}</job-type>
<status>SuccessWithWarnings</status>
<username>{db:open($config:CoreDatabase,fn:concat($config:UserDir,$userID,'.xml'))/user/name/text()}</username>
<submitted-on>{$submittedOn}</submitted-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<authorize>ud6hoko1Bg132WAy6jVPHujyfRHj9MaNrPmz6ObSQ7RSwbENp3ranKWKWRxBBW8pHUHMh8KpVfS7t9At7Z3DLqstunsmNbjCPxg9lSmG5v0NGZETVX2xy1vqPB0PzJ5L</authorize>
</email-job-details>
)
else ()
)
else
(
let $jobXml := copy $c := $jobXml
modify
(
replace node $c/job/ingestion-report/status with <status>Failure</status>,
if($ignoreValidation='true')
then
( replace node $c/job/job-info/location with <location>{fn:concat(fn:substring-before($path,$jobID),$jobID,'/Publish/',$zipName)}</location>)
else(),
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
(
db:replace($config:CoreDatabase,$files,$jobXml),
file:write($filePath,$jobXml),
(:file:write(fn:concat($path,'Publish/',$ProductName,'/report/',$jobID,'.xml'),$jobXml),:)
if($mailSent='true' and ($lastPipelineID=$appliedPipeline))
then
bdata:send-mail(
<email-job-details>
<jobid>{$jobID}</jobid>
<product-name>{$ProductName}</product-name>
<email>{$userEMail}</email>
<job-type>{$jobType}</job-type>
<status>Failure</status>
<username>{db:open($config:CoreDatabase,fn:concat($config:UserDir,$userID,'.xml'))/user/name/text()}</username>
<submitted-on>{$submittedOn}</submitted-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<authorize>ud6hoko1Bg132WAy6jVPHujyfRHj9MaNrPmz6ObSQ7RSwbENp3ranKWKWRxBBW8pHUHMh8KpVfS7t9At7Z3DLqstunsmNbjCPxg9lSmG5v0NGZETVX2xy1vqPB0PzJ5L</authorize>
</email-job-details>
)
else ()
)
)
else
let $jobXml := copy $c := $jobXml
modify
(
replace node $c/job/ingestion-report/status with <status>Success</status>,
replace node $c/job/job-info/location with <location>{fn:concat(fn:substring-before($path,$jobID),$jobID,'/Publish/',$zipName)}</location>,
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
(
db:replace($config:CoreDatabase,$files,$jobXml),
file:write($filePath,$jobXml),
(:file:write(fn:concat($path,'Publish/',$ProductName,'/report/',$jobID,'.xml'),$jobXml),:)
if($mailSent='true' and ($lastPipelineID=$appliedPipeline))
then
bdata:send-mail(
<email-job-details>
<jobid>{$jobID}</jobid>
<product-name>{$ProductName}</product-name>
<email>{$userEMail}</email>
<job-type>{$jobType}</job-type>
<status>Success</status>
<username>{db:open($config:CoreDatabase,fn:concat($config:UserDir,$userID,'.xml'))/user/name/text()}</username>
<submitted-on>{$submittedOn}</submitted-on>
<completed-on>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</completed-on>
<authorize>ud6hoko1Bg132WAy6jVPHujyfRHj9MaNrPmz6ObSQ7RSwbENp3ranKWKWRxBBW8pHUHMh8KpVfS7t9At7Z3DLqstunsmNbjCPxg9lSmG5v0NGZETVX2xy1vqPB0PzJ5L</authorize>
</email-job-details>
)
else ()
)
)
else ()
,
let $publish := fn:concat($path,'Publish/')
let $zip := archive:create-from($publish)
return file:write-binary(fn:concat(fn:substring-before($path,$jobID),$jobID,'/Publish/',$zipName), $zip)
)
else ()
)
}
catch *
{
admin:write-log("[Copy2DB][Error: " || $err:description || "]"),
admin:write-log("[Copy2DB][Error: " || $err:additional || "]")
}
};
(:~
: To get job in queue
: @return element(job)
:)
declare
%rest:path("/data/jobs")
%rest:GET
function bdata:job()
{
let $job := (for $eachJob in db:open($config:CoreDatabase,$config:JobDir)/job[ingestion-report/status='InQueue']
order by xs:dateTime($eachJob/job-info/submitted-on)
return
if(db:open($config:CoreDatabase,$config:JobDir)/job[ingestion-report/status='InProgress'])
then admin:write-log('*******Job already in progress*******')
else <job>{$eachJob/job-info,$eachJob/ingestion-report}</job>
)[1]
return if($job) then $job else <job>NONE</job>
};
(:~
: To update job status
: @return empty sequence
:)
declare
%updating
%rest:path("/data/job/update")
%rest:POST("{$body}")
%rest:consumes("application/xml", "text/xml")
function bdata:job-status(
$body as document-node()
)
{
let $jobID := $body/job/text()
let $uri := fn:concat($config:CoreDatabase,$config:JobDir,$jobID,'.xml')
return replace node doc($uri)/job/ingestion-report/status with <status>InProgress</status>
};
declare %updating function bdata:update(
$contentUri as xs:string,
$sessionValue as xs:string,
$action as xs:string,
$ctype as xs:string,
$comment as xs:string?
)
{
let $contentUri := fn:replace($contentUri,'/','\\')
let $cid := fn:tokenize(fn:substring-after($contentUri,'\content\'),'\\')[1]
let $coid := fn:tokenize(fn:substring-after($contentUri,'\content\'),'\\')[2]
let $auditUri := fn:concat($config:AuditDatabase,'/' ,$ctype,'/',$config:ContentDir,$cid,'/',$coid,'/',$config:AuditFileName)
let $version := (:let $versionUri := fn:replace($auditUri,$config:AuditFileName,$config:VersionControlFileName):)
let $versionUri := fn:concat($ctype,$config:ContentDir,$cid,'/',$coid,'/',$config:VersionControlFileName)
return
if(fn:doc-available($versionUri))
then fn:sum(fn:max(fn:doc($versionUri)/versions/version/@number) + 1)
else 1
let $AuditChunk := <audit>
<id>{random:uuid()}</id>
<requester-id>{fn:substring-before($sessionValue,'$$$$')}</requester-id>
<requester-email>{fn:substring-after($sessionValue,'$$$$')}</requester-email>
<type>{$action}</type>
<version>{$version}</version>
<date-time>{fn:adjust-dateTime-to-timezone(convert:integer-to-dateTime(prof:current-ms()))}</date-time>
<comment>{$comment}</comment>
</audit>
let $IsAudit := if(fn:doc-available($auditUri)) then fn:true() else fn:false()
return
if($IsAudit)
then
(
insert node $AuditChunk into fn:doc($auditUri)/audits,
admin:write-log("[Audit Record][Audit record has been updated " || $auditUri || "]")
)
else
(
(:db:add($config:AuditDatabase,<audits>{$AuditChunk}</audits>,fn:substring-after($auditUri,fn:concat(blcommon:get-db-name($ctype),'/')))
,:)
db:add($config:AuditDatabase,<audits>{$AuditChunk}</audits>,fn:substring-after($auditUri,fn:concat($config:AuditDatabase,'/'))),
admin:write-log("[Audit Record][Audit record has been created " || $auditUri || "]")
)
};
declare %private %updating function bdata:create-version(
$cid as xs:string,
$coid as xs:string,
$outputFileName as xs:string,
$ctype as xs:string,
$inputContent as xs:string
)
{
let $inputContent := if(fn:replace($outputFileName,'.xml','')='header') then 'Header' else $inputContent
let $versionFileName := let $tstamp := fn:current-dateTime()
let $suffix := fn:format-dateTime($tstamp, "[Y1,4][M01][D01][H01][m01][s01][f01]")
return fn:concat(fn:replace($outputFileName,'.xml',''),'_version_',$suffix,'.xml')
let $latestFileName := if($inputContent='Header')
then 'header.xml'
else if($ctype='play' or $ctype='screenplay' or $ctype='monograph') then $outputFileName
else if(fn:contains($coid,'_'))
then fn:concat(fn:substring-after($coid,'_'),'.xml')
else fn:concat($coid,'.xml')
let $coid := if(fn:replace($outputFileName,'.xml','')='header') then fn:concat($cid,'_',$cid) else $coid
let $targetUri := fn:concat($config:ContentDir,$cid,'/',$coid,'/content/latest/',$latestFileName)
let $versionControlUri := fn:concat($config:ContentDir,$cid,'/',$coid,'/',$config:VersionControlFileName)
let $nextVersion := if(fn:doc-available(fn:concat(blcommon:get-db-name($ctype),$versionControlUri)))
then fn:sum(fn:max(db:open(blcommon:get-db-name($ctype),$versionControlUri)/versions/version/@number) + 1)
else 1
let $versionUri := fn:replace(fn:concat($config:ContentDir,$cid,'/',$coid,$config:ContentDir,$config:VersionDir,'v',$nextVersion,'/',$latestFileName),'//','/')
return
(
(:copy old content into version directory:)
if(db:exists(blcommon:get-db-name($ctype),$targetUri))
then
(
db:replace(blcommon:get-db-name($ctype), $versionUri, db:open(blcommon:get-db-name($ctype),$targetUri)),
admin:write-log(fn:concat('#####version-updated######',$versionUri))
)
else
(
admin:write-log(fn:concat('#####version-not-updated######',$targetUri,'###########latest-file-name#####',$latestFileName,'##############COID######', ' ',$coid, '###########outputfilename######', ' ', $outputFileName))
)
,
(:copy old docx into version directory:)
let $wordUri := fn:replace(fn:replace($targetUri,'/content/latest/','/content/latest/word/'),'.xml','.docx')
let $docxVersionUri := fn:replace(fn:concat($config:ContentDir,$cid,'/',$coid,$config:ContentDir,$config:VersionDir,'v',$nextVersion,$config:WordDir,fn:replace($outputFileName,'.xml','.docx')),'//','/')
return
if(db:exists(blcommon:get-db-name($ctype),$wordUri))
then db:store(blcommon:get-db-name($ctype), $docxVersionUri, db:retrieve(blcommon:get-db-name($ctype),$wordUri))
else ()
,
admin:write-log("[Content Checkin][Content version is created : " || $versionUri || "]")
,
if(db:exists(blcommon:get-db-name($ctype),$targetUri))
then
if(fn:doc-available(fn:concat(blcommon:get-db-name($ctype),$versionControlUri)))
then
(
insert node <version number="{$nextVersion}" uri="{$versionUri}" on="{fn:adjust-dateTime-to-timezone(fn:current-dateTime())}"/> into db:open(blcommon:get-db-name($ctype),$versionControlUri)/versions,
admin:write-log("[Content Checkin][Content version control updated : " || $versionUri || "]")
)
else
db:replace(blcommon:get-db-name($ctype),$versionControlUri,<versions><version number="1" uri="{$versionUri}" on="{fn:adjust-dateTime-to-timezone(fn:current-dateTime())}"/></versions>)
else ()
)
};
declare %updating function bdata:send-mail(
$mailChunk as element(email-job-details)
)
{
let $sendMail := http:send-request(
<http:request
method='POST'
auth-method='Basic'
href='{$config:MailServer}'>
<http:header name="Content-Type" value="application/xml"/>
<http:body media-type='application/xml'>
{$mailChunk}
</http:body>
</http:request>
)
let $_log := admin:write-log(fn:concat('******* mail sent: ', $mailChunk/jobid/text(), ' *********'))
return ()
};