@@ -5,7 +5,6 @@ xquery version "3.1";
55: @author École nationale des chartes - Philippe Pons
66: @since 2025-01-10
77: @version 1.0
8- : @todo L'ajout d'un document dans une collection qui n'existe pas est possible. Comment remédier à cela ?
98:)
109
1110module namespace add_doc = "backend/update/add_document" ;
@@ -17,45 +16,59 @@ import module namespace resources = "backend/resources_register_builder";
1716import module namespace fragments = "backend/fragments_register_builder" ;
1817import module namespace dots.update = "backend/TEI_add_id" ;
1918import module namespace script = "script" ;
19+ import module namespace utils_dots = "utils_dots" ;
2020
2121declare namespace dots = "https://github.com/chartes/dots/" ;
2222declare namespace dc = "http://purl.org/dc/elements/1.1/" ;
2323declare namespace tei = "http://www.tei-c.org/ns/1.0" ;
2424
25- declare updating function add_doc:handleAddition ($dbName, $docPath) {
26- let $csv := resources:getCSV-map ($dbName, "document" )
27- let $csv-frag := resources:getCSV-map ($dbName, "fragment" )
25+ (:~ Update function to handle the addition of a new document and its fragments to the database and registers.
26+ : @param $dbName db name
27+ : @param $docPath absolute path to the document to add
28+ : @return updates the database and registers with the new document and its fragments
29+ :)
30+ declare updating function add_doc:handleAddition ($dbName as xs:string, $docPath as xs:string, $parentId as xs:string) {
31+ let $resourceId := utils_dots:findDocId ($docPath)
32+ let $docInRegister := utils_dots:getDocInRegister ($dbName, $resourceId)
2833 return
29- (
30- add_doc:addDocToDB ($dbName, $docPath),
31- add_doc:addDocToResourcesReg ($dbName, $docPath, $csv)
34+ if ($docInRegister)
35+ then script:error (dots_error:documentExists ())
36+ else
37+ let $csv := resources:getCSV-map ($dbName, "document" )
38+ return
39+ (
40+ add_doc:addDocToDB ($dbName, $docPath, $parentId),
41+ add_doc:addDocToResourcesReg ($dbName, $docPath, $csv)
3242 )
3343};
3444
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- };
45+ (:~~~~~~~~~~~~~~~
46+ Update database
47+ ~~~~~~~~~~~~~~~~~:)
4048
4149(:~ Update function to add a new document with a specific path
4250: @param $dbName db name
4351: @param $docPath absolute path to the document to add
52+ : @param $parentId identifier of the parent collection. By default, the project identifier.
4453: @return add the document at the path $docPath to the db $dbName
45- : @todo the script using this function MUST check if contains($docPath, "data/").
4654:)
47- declare %private updating function add_doc:addDocToDB ($dbName as xs:string, $docPath) {
48- let $path := substring-after ($docPath, "data /" )
55+ declare %private updating function add_doc:addDocToDB ($dbName as xs:string, $docPath as xs:string, $path as xs:string ) {
56+ let $docName := functx: substring-after-last ($docPath, "/" )
4957 return
50- db:put ($dbName, $docPath, $path)
58+ db:put ($dbName, $docPath, concat ( $path, "/" , $docName) )
5159};
5260
61+ (:~~~~~~~~~~~~~~~~~~~~~~~~~~~
62+ Update resources_register.xml
63+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~:)
64+
5365(:~ Update function to add a `<document/>` element to dots resources register (`dots/resources_register.xml`)
5466: @param $dbName db name
5567: @param $docPath absolute path to the document to add
68+ : @param $csv map of the csv metadata
5669: @return a complete <document/> node
5770:)
58- declare updating %private function add_doc:addDocToResourcesReg ($dbName as xs:string, $docPath, $csv) {
71+ declare updating %private function add_doc:addDocToResourcesReg ($dbName as xs:string, $docPath as xs:string , $csv) {
5972 let $document := doc ($docPath)/tei:TEI
6073 let $projectName := G:getTopCollectionId ($dbName)
6174 let $dtsResourceId :=
@@ -75,44 +88,59 @@ declare updating %private function add_doc:addDocToResourcesReg($dbName as xs:st
7588 return $collId
7689 else $projectName
7790 return
78- if ($document)
79- then
80- let $resources_register := db:get ($dbName, $G:resourcesRegister)//dots:member
81- let $doc_in_register := $resources_register/dots:document [@dtsResourceId = $dtsResourceId]
82- return
83- if ($doc_in_register)
84- then
85- (
86- delete nodes $doc_in_register,
87- insert node <document xmlns = "https://github.com/chartes/dots/" dtsResourceId = "{$dtsResourceId} " maxCiteDepth = "{$maxCiteDepth} " parentIds = "{$parentIds} " >{
88- resources:getDocumentMetadata ($dbName, $document, $dtsResourceId, $csv),
89- resources:getDotsProjectName ($projectName)
90- }</document> after $doc_in_register
91- )
92- else
93- (
94- insert node <document xmlns = "https://github.com/chartes/dots/" dtsResourceId = "{$dtsResourceId} " maxCiteDepth = "{$maxCiteDepth} " parentIds = "{$parentIds} " >{
95- resources:getDocumentMetadata ($dbName, $document, $dtsResourceId, $csv),
96- resources:getDotsProjectName ($projectName)
91+ let $resources_register := db:get ($dbName, $G:resourcesRegister)//dots:member
92+ return
93+ (
94+ insert node <document xmlns = "https://github.com/chartes/dots/" dtsResourceId = "{$dtsResourceId} " maxCiteDepth = "{$maxCiteDepth} " parentIds = "{$parentIds} " >{
95+ resources:getDocumentMetadata ($dbName, $document, $dtsResourceId, $csv),
96+ resources:getDotsProjectName ($projectName)
9797 }</document> as last into $resources_register,
98- add_doc:updateTotalChildrenCollection ($dbName, $parentIds),
99- add_doc:addDocToSwitcherDots ($dbName, $dtsResourceId)
100- )
101- else ()
98+ add_doc:updateTotalChildrenCollection ($dbName, $parentIds),
99+ add_doc:addDocToSwitcherDots ($dbName, $dtsResourceId)
100+ )
101+ };
102+
103+ (:~ Update function to increment the number of documents in the parent collection
104+ : @param $dbName db name
105+ : @param $parentIds identifier of the collection to update
106+ : @return updating the value of the @totalChildren attribute in a <collection/> node.
107+ :)
108+ declare updating %private function add_doc:updateTotalChildrenCollection ($dbName as xs:string, $parentIds as xs:string) {
109+ let $parent := db:get ($dbName, $G:resourcesRegister)//dots:collection[@dtsResourceId = $parentIds]
110+ let $totalChildren := $parent/@totalChildren
111+ return
112+ if ($parent != "" )
113+ then replace value of node $totalChildren with xs:integer ($parent/@totalChildren) + 1
114+ else
115+ update:output ("La collection parente n'existe pas." )
116+ };
117+
118+ (:~~~~~~~~~~~~~~~~~~~~~~~~~~~
119+ Update fragments_register.xml
120+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~:)
121+
122+ (:~ Update function to handle the addition of fragments from a new document to the fragments register.
123+ : @param $dbName db name
124+ : @param $docPath absolute path to the document containing the fragments to add
125+ : @return updates the fragments register with the new fragments
126+ :)
127+ declare updating function add_doc:handleFragmentsAddition ($dbName as xs:string, $docPath) {
128+ let $csv-frag := resources:getCSV-map ($dbName, "fragment" )
129+ return
130+ add_doc:addFragInReg ($dbName, $docPath, $csv-frag)
102131};
103132
104133(:~ Update function to add `<fragment/>` nodes to the fragments register (`dots/fragments_register`).
105134: @param $dbName db name
106135: @param $docPath absolute path to the document to add
107136: @return sequence of <fragment/> nodes
108137:)
109- declare updating %private function add_doc:addFragInReg ($dbName as xs:string, $docPath, $csv) {
110- let $pathToDoc := functx:substring-after-last ($docPath, "data/" )
111- let $document := db:get ($dbName, $pathToDoc)/tei:TEI
138+ declare updating %private function add_doc:addFragInReg ($dbName as xs:string, $docPath, $csv) {
112139 let $resourceId :=
113- if ($document/@xml:id)
114- then normalize-space ($document/@xml:id)
115- else functx:substring-after-last ($pathToDoc, "/" )
140+ if (doc ($docPath)/*:TEI/@xml:id)
141+ then normalize-space (doc ($docPath)/*:TEI//@xml:id)
142+ else functx:substring-after-last ($docPath, "/" )
143+ let $document := utils_dots:getDocument ($dbName, $resourceId)
116144 let $maxCiteDepth := fragments:getMaxCiteDepth ($document//tei:refsDecl, 0 )
117145 for $citeStructurePosition in $document//tei:refsDecl/tei:citeStructure
118146 return
@@ -129,21 +157,9 @@ declare updating %private function add_doc:addFragInReg($dbName as xs:string, $d
129157 else insert node $fragment as last into $fragments_register
130158};
131159
132-
133- (:~ Update function to increment the number of documents in a collection
134- : @param $dbName db name
135- : @param $parentIds identifier of the collection to update
136- : @return updating the value of the @totalChildren attribute in a <collection/> node.
137- :)
138- declare updating %private function add_doc:updateTotalChildrenCollection ($dbName as xs:string, $parentIds as xs:string) {
139- let $parent := db:get ($dbName, $G:resourcesRegister)//dots:collection[@dtsResourceId = $parentIds]
140- let $totalChildren := $parent/@totalChildren
141- return
142- if ($parent != "" )
143- then replace value of node $totalChildren with xs:integer ($parent/@totalChildren) + 1
144- else
145- update:output ("La collection parente n'existe pas." )
146- };
160+ (:~~~~~~~~~~~~~~~~~~~~~~~~~~~
161+ Update switcher DoTS
162+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~:)
147163
148164(:~ Updat function to add the documet to the switcher dots
149165: @param $dbName db name
0 commit comments