Skip to content

Commit 51ef36b

Browse files
committed
add and correct update scripts
1 parent 3d20ad3 commit 51ef36b

24 files changed

+325
-229
lines changed

repo/backend/update/add_collection.xqm

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import module namespace resources = "backend/resources_register_builder";
99

1010
declare namespace dots = "https://github.com/chartes/dots/";
1111

12+
declare updating function add_coll:handleAddition($dbName as xs:string, $resourceId as xs:string, $parentId as xs:string := "") {
13+
add_coll:addCollToResourcesReg($dbName, $resourceId, $parentId),
14+
add_coll:updateMaxCiteDepthCollection($dbName, $parentId),
15+
add_coll:addCollToSwitcherDots($dbName, $resourceId)
16+
};
17+
1218
declare updating function add_coll:addCollToResourcesReg($dbName as xs:string, $resourceId as xs:string, $parentId as xs:string := "") {
1319
let $csv := resources:getCSV-map($dbName, $resourceId)
1420
let $parent := if ($parentId) then $parentId else utils_dots:getIdProject($dbName)
@@ -18,4 +24,31 @@ declare updating function add_coll:addCollToResourcesReg($dbName as xs:string, $
1824
<collection xmlns="https://github.com/chartes/dots/" dtsResourceId="{$resourceId}" totalChildren="0" parentIds="{$parent}">{
1925
resources:getCollectionMetadata($dbName, $resourceId, $csv)
2026
}</collection> as last into $resources_register
27+
};
28+
29+
(:~ Update function to increment the number of documents in a collection
30+
: @param $dbName db name
31+
: @param $parentIds identifier of the collection to update
32+
: @return updating the value of the @totalChildren attribute in a <collection/> node.
33+
:)
34+
declare updating %private function add_coll:updateMaxCiteDepthCollection($dbName as xs:string, $parentIds as xs:string) {
35+
let $idParentColl := if ($parentIds = "") then utils_dots:getIdProject($dbName) else $parentIds
36+
let $parent := db:get($dbName, $G:resourcesRegister)//dots:collection[@dtsResourceId = $idParentColl]
37+
let $totalChildren := $parent/@totalChildren
38+
return
39+
if ($parent != "")
40+
then replace value of node $totalChildren with xs:integer($parent/@totalChildren) + 1
41+
else
42+
update:output("La collection parente n'existe pas.")
43+
};
44+
45+
(:~ Update function to add the collection to the switcher dots
46+
: @param $dbName db name
47+
: @param $dtsResourceId identifier of the collection
48+
: @return <document/> node
49+
:)
50+
declare updating function add_coll:addCollToSwitcherDots($dbName as xs:string, $dtsResourceId as xs:string) {
51+
let $switcher := db:get($G:dots, $G:dbSwitcher)//dots:member
52+
return
53+
insert node <collection dtsResourceId="{$dtsResourceId}" dbName="{$dbName}"/> as last into $switcher
2154
};

repo/backend/update/add_document.xqm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import module namespace G = "globals";
1515
import module namespace dots_error = "error/dots_error";
1616
import module namespace resources = "backend/resources_register_builder";
1717
import module namespace fragments = "backend/fragments_register_builder";
18+
import module namespace dots.update = "backend/TEI_add_id";
1819
import module namespace script = "script";
1920

2021
declare namespace dots = "https://github.com/chartes/dots/";
@@ -28,7 +29,8 @@ declare updating function add_doc:handleAddition($dbName, $docPath) {
2829
(
2930
add_doc:addDocToDB($dbName, $docPath),
3031
add_doc:addDocToResourcesReg($dbName, $docPath, $csv),
31-
add_doc:addFragInReg($dbName, $docPath, $csv-frag)
32+
add_doc:addFragInReg($dbName, $docPath, $csv-frag),
33+
dots.update:addXmlIdToFragment($dbName)
3234
)
3335
};
3436

repo/backend/update/delete_collection.xqm

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@ xquery version "4.0";
1111

1212
module namespace del_coll = "backend/update/delete_collection";
1313

14+
import module namespace functx = 'http://www.functx.com';
1415
import module namespace utils_dots = "utils_dots";
1516
import module namespace G = "globals";
1617
import module namespace del_doc = "backend/update/delete_document";
18+
import module namespace resources = "backend/resources_register_builder";
19+
import module namespace fragments = "backend/fragments_register_builder";
1720

1821
declare default element namespace "https://github.com/chartes/dots/";
1922

23+
declare namespace tei = "http://www.tei-c.org/ns/1.0";
24+
2025
(:~ This function allows deleting a collection from the DTS resource registry in the BaseX database.
2126
: It removes the collection node, updates the parent collection's child count,
2227
: handles child documents (deletion or reassignment), and updates the DoTS switcher.
@@ -26,14 +31,14 @@ declare default element namespace "https://github.com/chartes/dots/";
2631
: if false, reassign them to the parent collection
2732
: @return database updates (delete node, update attributes, trigger document deletion or reassignment)
2833
:)
29-
declare updating function del_coll:handleDeleteColl($dbName as xs:string, $collectionId as xs:string, $option as xs:boolean) {
34+
declare updating function del_coll:handleDeleteColl($dbName as xs:string, $collectionId as xs:string, $option) {
3035
let $collection := db:get($dbName, $G:resourcesRegister)//member/collection[@dtsResourceId = $collectionId]
3136
let $docInColl := db:get($dbName, $G:resourcesRegister)//member/document[tokenize(@parentIds) = $collectionId]
3237
let $parentId := $collection/@parentIds
3338
return
3439
(
3540
delete node $collection,
36-
del_coll:changeParentTotalChildren($dbName, $parentId, count($docInColl)),
41+
del_coll:changeParentTotalChildren($dbName, $parentId, if ($option ="true") then 0 else count($docInColl)),
3742
del_doc:updateSwitcherDots($dbName, $collectionId)
3843
)
3944
};
@@ -62,22 +67,52 @@ declare updating %private function del_coll:changeParentTotalChildren($dbName as
6267
: @param $option boolean: true = delete documents; false = reassign to parent
6368
: @return database updates (delete or reassign documents)
6469
:)
65-
declare updating function del_coll:handleDocInColl($dbName as xs:string, $collectionId as xs:string, $option as xs:boolean) {
70+
declare updating function del_coll:handleDocInColl($dbName as xs:string, $collectionId as xs:string, $option) {
71+
let $csv := resources:getCSV-map($dbName, "fragment")
6672
for $document in db:get($dbName, $G:resourcesRegister)//document[tokenize(@parentIds) = $collectionId]
6773
let $resourceId := $document/@dtsResourceId
6874
let $pathDoc := utils_dots:findPath($dbName, $resourceId)
6975
let $parentDoc := $document/@parentIds
7076
let $parentCollId := db:get($dbName, $G:resourcesRegister)//collection[@dtsResourceId = $collectionId]/@parentIds
7177
return
72-
(
73-
replace value of node $parentDoc with replace($parentDoc, $collectionId, $parentCollId),
74-
db:delete($dbName, $pathDoc),
75-
db:put($dbName, $document, replace($pathDoc, concat($collectionId, "/"), "/"))
76-
)
78+
if ($option = "true")
79+
then
80+
(
81+
delete node $document,
82+
delete nodes db:get($dbName, $G:fragmentsRegister)//fragment[@resourceId = $resourceId],
83+
db:delete($dbName, $pathDoc)
84+
)
85+
else
86+
(
87+
delete nodes db:get($dbName, $G:fragmentsRegister)//fragment[@resourceId = $resourceId],
88+
replace value of node $parentDoc with replace($parentDoc, $collectionId, $parentCollId),
89+
db:delete($dbName, $pathDoc),
90+
db:put($dbName, utils_dots:findPathDoc($dbName, $resourceId), replace($pathDoc, concat($collectionId, "/"), "/")),
91+
del_coll:addFragInReg($dbName, $resourceId, utils_dots:getDocument($dbName, $resourceId), $csv)
92+
)
7793
};
7894

79-
80-
95+
(:~ Update function to add `<fragment/>` nodes to the fragments register (`dots/fragments_register`).
96+
: @param $dbName db name
97+
: @param $docPath absolute path to the document to add
98+
: @return sequence of <fragment/> nodes
99+
:)
100+
declare updating %private function del_coll:addFragInReg($dbName as xs:string, $resourceId as xs:string, $document, $csv) {
101+
let $maxCiteDepth := fragments:getMaxCiteDepth($document//tei:refsDecl, 0)
102+
for $citeStructurePosition in $document//tei:refsDecl/tei:citeStructure
103+
return
104+
let $fragments_register := db:get($dbName, $G:fragmentsRegister)//member
105+
let $fragment := fragments:handleCiteStructure($dbName, $document, "", $citeStructurePosition, 1, $resourceId, "", "", $maxCiteDepth, $csv)
106+
let $oldFragments := $fragments_register/fragment[@resourceId = $resourceId]
107+
return
108+
if ($oldFragments)
109+
then
110+
(
111+
delete nodes $oldFragments,
112+
insert node $fragment as last into $fragments_register
113+
)
114+
else insert node $fragment as last into $fragments_register
115+
};
81116

82117

83118

repo/backend/update/update_document_content.xqm

Lines changed: 0 additions & 123 deletions
This file was deleted.

repo/utils_dots.xqm

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ declare function utils_dots:getDbName($resourceId as xs:string) {
1919
normalize-space(db:get($G:dots)//dots:member/node()[@dtsResourceId = $resourceId]/@dbName)
2020
};
2121

22+
declare function utils_dots:getDocument($dbName as xs:string, $resourceId as xs:string) {
23+
if (db:get($dbName)/tei:TEI[@xml:id = $resourceId])
24+
then db:get($dbName)/tei:TEI[@xml:id = $resourceId]
25+
else
26+
db:get($dbName)/node() ! db:path(.)[ends-with(., $resourceId)]
27+
};
28+
29+
declare function utils_dots:findDocId($docPath) {
30+
let $document := utils_dots:findDocInFolder($docPath)
31+
let $resourceId := $document/tei:TEI/@xml:id
32+
return
33+
if ($resourceId)
34+
then $resourceId
35+
else file:name($docPath)
36+
};
37+
2238
(:~ This function allows retrieving the document with the $resourceId identifier in the import folder $project_dir_path
2339
: @param $resourceId document identifier
2440
: @param $project_dir_path absolute path to the data import folder

scripts/add_collection.sh

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,50 @@ function usage {
1414
echo ""
1515
echo "Add a new collection to an existing DoTS project"
1616
echo ""
17-
echo "usage: $programname --db_name string --doc_path string "
17+
echo "usage: $programname --basex_path string --db_name string --collection_id string"
1818
echo ""
19+
echo " --basex_path string absolute path to the basex folder 'bin'"
20+
echo " (example: /absolute/path/to/basex/bin)"
1921
echo " --db_name string basex project db name"
2022
echo " (example: theater)"
21-
echo " --collection_id string collection identifier"
23+
echo " --collection_id string identifier of the collection"
2224
echo " "
23-
echo " --basex_path string absolute path to the basex folder 'bin'"
24-
echo " (example: /absolute/path/to/basex/bin)"
25-
echo ""
25+
echo " [--parent_id] string by default (top_collection_id) : identifier of the parent collection"
26+
echo " "
27+
echo " [--unit_test] boolean by default (false) true: launch unit tests"
28+
echo " (example: false)"echo ""
2629
}
2730

2831
function die {
2932
printf "Script failed: %s\n\n" "$1"
3033
exit 1
3134
}
3235

33-
if [[ -z $db_name ]]; then
36+
if [[ -z $basex_path ]]; then
3437
usage
35-
die "Missing parameter --db_name"
36-
elif [[ -z $collection_id string ]]; then
38+
die "Missing parameter --basex_path"
39+
elif [[ -z $db_name ]]; then
3740
usage
38-
die "Missing parameter --collection_id string"
39-
elif [[ -z $basex_path ]]; then
41+
die "Missing parameter --db_name"
42+
elif [[ -z $collection_id ]]; then
4043
usage
41-
die "Missing parameter --basex_path"
44+
die "Missing parameter --collection_id"
45+
fi
46+
47+
bash "$basex_path/basex" -b dbName=$db_name -b resourceId=$collection_id -b parentId=$parent_id scripts/add_collection.xq;
48+
49+
if [[ $unit_test == 'true' ]]; then
50+
bash "$basex_path/basex" -b dbName=$db_name -t tests/project_create/project_registers_create_test.xq;
51+
bash "$basex_path/basex" -b dbName=$db_name -t tests/project_create/TEI_add_id_test.xq;
52+
bash "$basex_path/basex" -b dbName=$db_name -t tests/project_create/dots_switcher_update_test.xq;
4253
fi
4354

44-
bash "$basex_path/basex" -b dbName=$db_name -b docPath=$doc_path scripts/add_collection.xq
55+
56+
57+
58+
59+
60+
61+
62+
63+

scripts/add_collection.xq

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ return
2222
if ($collParent)
2323
then
2424
(
25-
add_coll:addCollToResourcesReg($dbName, $resourceId, $parentId),
25+
add_coll:handleAddition($dbName, $resourceId),
2626
script:success(concat("La collection '", $resourceId, "', sous-collection de '", $parentId, "', a bien été ajouté à la db ", $dbName, "."))
2727
)
2828
else script:error(concat("La collection parente '", $parentId, "' n'existe pas."))
2929
else
3030
(
31-
add_coll:addCollToResourcesReg($dbName, $resourceId),
31+
add_coll:handleAddition($dbName, $resourceId),
3232
script:success(concat("La collection '", $resourceId, "' a bien été ajouté à la db ", $dbName, "."))
3333
)
3434

0 commit comments

Comments
 (0)