Skip to content

Commit 2e3a033

Browse files
committed
fix a bug when adding a document
1 parent 8d29106 commit 2e3a033

6 files changed

Lines changed: 69 additions & 33 deletions

File tree

repo/backend/TEI_add_id.xqm

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ xquery version "3.1";
55
: @author École nationale des chartes - Philippe Pons
66
: @since 2024-10-29
77
: @version 1.0
8-
: @todo revoir cette fonction si la valeur des attributs @ref est calculée (en s'apppuyant probablement sur la position du noeud dans le document)
98
:)
109

1110
module namespace dots.update = "backend/TEI_add_id";
@@ -16,15 +15,15 @@ declare default element namespace "https://github.com/chartes/dots/";
1615

1716
declare updating function dots.update:addXmlIdToFragment($dbName as xs:string) {
1817
for $fragments in db:get($dbName, $G:fragmentsRegister)//fragment
19-
let $ref := $fragments/@ref
2018
let $node-id := $fragments/@node-id
2119
let $tei := db:get-id($dbName, $node-id)
2220
where not($tei/@xml:id)
21+
let $ref := $fragments/@ref
2322
let $refValue := concat("r", $node-id)
2423
return
2524
(
26-
replace value of node $ref with $refValue,
27-
insert node attribute {"xml:id"} { $refValue } into $tei
25+
insert node attribute {"xml:id"} { $refValue } into $tei,
26+
replace value of node $ref with $refValue
2827
)
2928
};
3029

repo/backend/fragments_register_builder.xqm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ return
177177
: @param $n (xs:integer) The current depth level, initially set to 0.
178178
: @return xs:integer: The maximum citation depth found in the document.
179179
:)
180-
declare function fragments:getMaxCiteDepth($node, $n as xs:integer) {
180+
declare function fragments:getMaxCiteDepth($nodes, $n as xs:integer) as xs:integer {
181181
let $levels :=
182-
for $level in $node
182+
for $node in $nodes
183183
return
184184
if ($node/tei:citeStructure)
185185
then

repo/backend/update/add_document.xqm

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@ declare updating function add_doc:handleAddition($dbName, $docPath) {
2828
return
2929
(
3030
add_doc:addDocToDB($dbName, $docPath),
31-
add_doc:addDocToResourcesReg($dbName, $docPath, $csv),
32-
add_doc:addFragInReg($dbName, $docPath, $csv-frag),
33-
dots.update:addXmlIdToFragment($dbName)
31+
add_doc:addDocToResourcesReg($dbName, $docPath, $csv)
3432
)
3533
};
3634

35+
declare updating function add_doc:handleFragmentsAddition($dbName as xs:string, $docPath) {
36+
let $csv-frag := resources:getCSV-map($dbName, "fragment")
37+
return
38+
add_doc:addFragInReg($dbName, $docPath, $csv-frag)
39+
};
40+
3741
(:~ Update function to add a new document with a specific path
3842
: @param $dbName db name
3943
: @param $docPath absolute path to the document to add
@@ -91,7 +95,7 @@ declare updating %private function add_doc:addDocToResourcesReg($dbName as xs:st
9195
resources:getDocumentMetadata($dbName, $document, $dtsResourceId, $csv),
9296
resources:getDotsProjectName($projectName)
9397
}</document> as last into $resources_register,
94-
add_doc:updateMaxCiteDepthCollection($dbName, $parentIds),
98+
add_doc:updateTotalChildrenCollection($dbName, $parentIds),
9599
add_doc:addDocToSwitcherDots($dbName, $dtsResourceId)
96100
)
97101
else ()
@@ -131,7 +135,7 @@ declare updating %private function add_doc:addFragInReg($dbName as xs:string, $d
131135
: @param $parentIds identifier of the collection to update
132136
: @return updating the value of the @totalChildren attribute in a <collection/> node.
133137
:)
134-
declare updating %private function add_doc:updateMaxCiteDepthCollection($dbName as xs:string, $parentIds as xs:string) {
138+
declare updating %private function add_doc:updateTotalChildrenCollection($dbName as xs:string, $parentIds as xs:string) {
135139
let $parent := db:get($dbName, $G:resourcesRegister)//dots:collection[@dtsResourceId = $parentIds]
136140
let $totalChildren := $parent/@totalChildren
137141
return

scripts/add_document.xq

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,17 @@
11
xquery version '4.0' ;
22

3+
import module namespace script = "script";
34
import module namespace functx = 'http://www.functx.com';
45
import module namespace G = "globals";
5-
import module namespace script = "script";
66
import module namespace add_doc = "backend/update/add_document";
77

88
declare namespace dots = "https://github.com/chartes/dots/";
99

1010
declare variable $dbName external := ();
1111
declare variable $docPath external := ();
1212

13-
14-
if (file:exists($docPath))
15-
then
16-
let $parentIds :=
17-
let $pathInData := substring-after($docPath, "data/")
18-
let $pathCollection := functx:substring-before-last($pathInData, "/")
19-
let $collectionId := if (contains($pathCollection, "/")) then functx:substring-after-last($pathCollection, "/") else $pathCollection
20-
return
21-
$collectionId
22-
return
23-
let $coll := if ($parentIds = "") then "project" else db:get($dbName, $G:resourcesRegister)//dots:collection[@dtsResourceId = $parentIds]
24-
return
25-
if ($parentIds = "project" or $coll)
26-
then
27-
(
28-
add_doc:handleAddition($dbName, $docPath),
29-
script:success(("Le document a bien été ajouté à la base ", $dbName, "."))
30-
)
31-
else script:error("La collection n'existe pas.")
32-
else
33-
script:error("Le fichier n'existe pas.")
13+
for $script in ('../scripts/add_document_database.xq', '../scripts/add_document_fragments.xq', '../scripts/TEI_add_id.xq')
14+
return script:execute(xs:anyURI($script), map {
15+
'dbName': $dbName,
16+
'docPath': $docPath
17+
})

scripts/add_document_database.xq

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
xquery version '4.0' ;
2+
3+
import module namespace functx = 'http://www.functx.com';
4+
import module namespace G = "globals";
5+
import module namespace script = "script";
6+
import module namespace add_doc = "backend/update/add_document";
7+
8+
declare namespace dots = "https://github.com/chartes/dots/";
9+
10+
declare variable $dbName external := ();
11+
declare variable $docPath external := ();
12+
13+
if (file:exists($docPath))
14+
then
15+
let $pathInData := substring-after($docPath, "data/")
16+
let $parentIds :=
17+
let $pathCollection := functx:substring-before-last($pathInData, "/")
18+
let $collectionId := if (contains($pathCollection, "/")) then functx:substring-after-last($pathCollection, "/") else $pathCollection
19+
return
20+
$collectionId
21+
return
22+
let $coll :=
23+
if ($parentIds = "")
24+
then "project"
25+
else db:get($dbName, $G:resourcesRegister)//dots:collection[@dtsResourceId = $parentIds]
26+
return
27+
if ($parentIds = "project" or $coll)
28+
then
29+
(
30+
add_doc:handleAddition($dbName, $docPath),
31+
if (db:get($dbName, $pathInData))
32+
then script:warning(concat("Le document existait déjà et a été mis à jour dans la base '", $dbName, "'."))
33+
else script:success(("Le document a bien été ajouté à la base '", $dbName, "'."))
34+
)
35+
else script:error("La collection n'existe pas.")
36+
else
37+
script:error(concat("Le fichier '", $docPath, "' n'existe pas."))
38+
39+
40+

scripts/add_document_fragments.xq

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
xquery version '4.0' ;
2+
3+
import module namespace add_doc = "backend/update/add_document";
4+
5+
declare variable $dbName external := ();
6+
declare variable $docPath external := ();
7+
8+
9+
add_doc:handleFragmentsAddition($dbName, $docPath)

0 commit comments

Comments
 (0)