Skip to content

Commit 57ed149

Browse files
committed
fix a bug for updating a document
1 parent f1063cf commit 57ed149

File tree

7 files changed

+82
-44
lines changed

7 files changed

+82
-44
lines changed

repo/backend/update/delete_document.xqm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
xquery version "3.1";
1+
xquery version "4.0";
22

33
(:~
44
: This module allows deleting a document to an existing collection.

repo/error/dots_error.xqm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ declare function dots_error:metadata_mapping($dbName, $projectDirPath ) {
4848
)
4949
};
5050

51+
declare function dots_error:fileProblem() {
52+
error(xs:QName('fileProblem'), "* ❌ Error : file does not exist.")
53+
};
5154

55+
declare function dots_error:noDocument($resourceId) {
56+
error(xs:QName('noDocument'), concat("* ❌ Error : the document '", $resourceId, "' does not exist."))
57+
};
5258

5359

5460

repo/utils_dots.xqm

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
xquery version "4.0";
22

3+
4+
(:~
5+
: Module providing utility functions for DoTS.
6+
: These functions facilitate the retrieval of TEI documents,
7+
: metadata, paths, and identifiers from a BaseX database or import folder.
8+
: @version 1
9+
: @date 2025-06-30
10+
: @author École nationale des chartes - Philippe Pons
11+
:)
12+
313
module namespace utils_dots = "utils_dots";
414

515
import module namespace G = "globals";
616

717
declare namespace dots = "https://github.com/chartes/dots/";
818
declare namespace tei = "http://www.tei-c.org/ns/1.0";
919

20+
(:~
21+
: This functions returns the DTS project identifier (dtsResourceId) for the root collection in the resources register of the given database.
22+
: @param $dbName the name of the BaseX database
23+
: @return the project identifier (dtsResourceId)
24+
:)
1025
declare function utils_dots:getIdProject($dbName as xs:string) {
1126
normalize-space(db:get($dbName, $G:resourcesRegister)//dots:collection[not(@parentIds)]/@dtsResourceId)
1227
};
@@ -19,13 +34,24 @@ declare function utils_dots:getDbName($resourceId as xs:string) {
1934
normalize-space(db:get($G:dots)//dots:member/node()[@dtsResourceId = $resourceId]/@dbName)
2035
};
2136

37+
(:~
38+
: This function retrieves a TEI document from the database using its xml:id.
39+
: @param $dbName the name of the database
40+
: @param $resourceId the document identifier
41+
: @return the TEI element
42+
:)
2243
declare function utils_dots:getDocument($dbName as xs:string, $resourceId as xs:string) {
2344
if (db:get($dbName)/tei:TEI[@xml:id = $resourceId])
2445
then db:get($dbName)/tei:TEI[@xml:id = $resourceId]
2546
else
2647
db:get($dbName)/node() ! db:path(.)[ends-with(., $resourceId)]
2748
};
2849

50+
(:~
51+
: This function retrieves the document identifier (xml:id) from a file path.
52+
: @param $docPath the path to the document file
53+
: @return the resource ID (xml:id or filename)
54+
:)
2955
declare function utils_dots:findDocId($docPath) {
3056
let $document := utils_dots:findDocInFolder($docPath)
3157
let $resourceId := $document/tei:TEI/@xml:id
@@ -44,6 +70,12 @@ declare function utils_dots:findDocInFolder($docPath) {
4470
doc($docPath)
4571
};
4672

73+
(:~
74+
: This function finds the path of a document in the database based on its identifier.
75+
: @param $dbName the name of the database
76+
: @param $resourceId the document identifier
77+
: @return the database path to the document
78+
:)
4779
declare function utils_dots:findPath($dbName as xs:string, $resourceId as xs:string) {
4880
head((
4981
db:get($dbName)/*:TEI[@xml:id = $resourceId] ! db:path(.)
@@ -53,7 +85,7 @@ declare function utils_dots:findPath($dbName as xs:string, $resourceId as xs:str
5385
};
5486

5587
(:~
56-
: Retrieves the document with the specified id.
88+
: This function retrieves the document with the specified id.
5789
: @param $dbName name of database
5890
: @param $resourceId resource ID
5991
: @param $strip strip processing instructions (by default true)
@@ -101,20 +133,38 @@ declare function utils_dots:getDocInRegister($dbName as xs:string, $resourceId a
101133
db:get($dbName, $G:resourcesRegister)//dots:member/node()[@dtsResourceId = $resourceId]
102134
};
103135

136+
(:~
137+
: This function extracts all parent identifiers from the @parentIds attribute of a <document> element.
138+
: @param $dbName the name of the database
139+
: @param $docInRegister the <dots:document> element
140+
: @return a sequence of parent identifiers
141+
:)
104142
declare function utils_dots:getParentIds($dbName as xs:string, $docInRegister as element(dots:document)) {
105143
for $parentId in tokenize($docInRegister/@parentIds, " ")
106144
return
107145
$parentId
108146
};
109147

148+
(:~
149+
: This function retrieves the root identifier of the DTS Collection endpoint.
150+
: @return the root identifier as a string
151+
:)
110152
declare function utils_dots:getRootId() {
111153
normalize-space(db:get($G:dots)/dots:metadataMap/dots:root/dots:id)
112154
};
113155

156+
(:~
157+
: This function retrieves the root title of the DTS Collection endpoint.
158+
: @return the root title as a string
159+
:)
114160
declare function utils_dots:getRootTitle() {
115161
normalize-space(db:get($G:dots)/dots:metadataMap/dots:root/dots:title)
116162
};
117163

164+
(:~
165+
: Retrieves the root description of the DTS Collection endpoint.
166+
: @return the root description as a string
167+
:)
118168
declare function utils_dots:getRootDescription() {
119169
let $desc := db:get($G:dots)/dots:metadataMap/dots:root/dots:description
120170
return

scripts/add_document.xq

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

33
import module namespace script = "script";
4-
import module namespace functx = 'http://www.functx.com';
5-
import module namespace G = "globals";
6-
import module namespace add_doc = "backend/update/add_document";
7-
8-
declare namespace dots = "https://github.com/chartes/dots/";
94

105
declare variable $dbName external := ();
116
declare variable $docPath external := ();

scripts/delete_document.xq

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
xquery version '4.0' ;
12

23
import module namespace script = "script";
34
import module namespace del_doc = "backend/update/delete_document";
@@ -6,14 +7,13 @@ import module namespace utils_dots = "utils_dots";
67
declare variable $dbName external := ();
78
declare variable $docId external := ();
89

9-
1010
let $doc := utils_dots:findPath($dbName, $docId)
1111
return
1212
if ($doc)
1313
then
1414
(
1515
del_doc:handleDelete($dbName, $docId),
16-
script:success(("Le document", $docId, " a bien été supprimé de la base ", $dbName, "."))
16+
script:success(("Le document ", $docId, " a bien été supprimé de la base ", $dbName, "."))
1717
)
1818
else
19-
script:error(concat("Le document", $docId, " n'existe pas."))
19+
script:error(concat("Le document ", $docId, " n'existe pas."))

scripts/project_create.xq

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
xquery version '4.0' ;
2+
13
import module namespace script = "script";
24

35
declare variable $projectDirPath external := ();

scripts/update_document.xq

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,31 @@
1-
xquery version "4.0";
1+
xquery version '4.0';
22

33
import module namespace G = "globals";
44
import module namespace utils_dots = "utils_dots";
55
import module namespace script = "script";
6-
import module namespace add_doc = "backend/update/add_document";
7-
import module namespace del_doc = "backend/update/delete_document";
8-
import module namespace functx = 'http://www.functx.com';
6+
import module namespace dots_error = "error/dots_error";
97

108
declare namespace dots = "https://github.com/chartes/dots/";
119

1210
declare variable $dbName external := ();
1311
declare variable $docPath external := ();
12+
declare variable $docId external := ();
1413

1514

16-
if (file:exists($docPath))
15+
if (db:exists($dbName))
1716
then
18-
(
19-
let $docId := utils_dots:findDocId($docPath)
20-
let $doc := utils_dots:findPath($dbName, $docId)
21-
return
22-
if ($doc)
23-
then
24-
(
25-
del_doc:handleDelete($dbName, $docId),
26-
let $parentIds :=
27-
let $pathInData := substring-after($docPath, "data/")
28-
let $pathCollection := functx:substring-before-last($pathInData, "/")
29-
let $collectionId := if (contains($pathCollection, "/")) then functx:substring-after-last($pathCollection, "/") else $pathCollection
30-
return
31-
$collectionId
32-
return
33-
let $coll := if ($parentIds = "") then "project" else db:get($dbName, $G:resourcesRegister)//dots:collection[@dtsResourceId = $parentIds]
34-
return
35-
if ($parentIds = "project" or $coll)
36-
then
37-
(
38-
add_doc:handleAddition($dbName, $docPath),
39-
script:success(("Le document '", $docId, "' a bien été mis à jour dans la base '", $dbName, "'."))
40-
)
41-
else script:error(concat("La collection '", $coll, "' n'existe pas."))
42-
)
43-
else
44-
script:error(concat("Le document '", $docId, "' n'existe pas.")))
45-
else
46-
script:error("Le fichier n'existe pas.")
17+
if (file:exists($docPath))
18+
then
19+
if (db:get($dbName, $G:resourcesRegister)//dots:document[@dtsResourceId = $docId])
20+
then
21+
for $script in ('../scripts/delete_document.xq', '../scripts/add_document_database.xq', '../scripts/add_document_fragments.xq', '../scripts/TEI_add_id.xq')
22+
return script:execute(xs:anyURI($script), map {
23+
'dbName': $dbName,
24+
'docPath': $docPath,
25+
'docId': $docId
26+
})
27+
else dots_error:noDocument($docId)
28+
else dots_error:fileProblem()
29+
else dots_error:dbError($dbName)
30+
31+

0 commit comments

Comments
 (0)