-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbio.xqm
More file actions
707 lines (667 loc) · 24.6 KB
/
bio.xqm
File metadata and controls
707 lines (667 loc) · 24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
xquery version "3.0";
module namespace bio.bio = "bio.bio";
(:~
: This xquery module is derived from the xpr application
:
: @author emchateau & sardinecan (ANR Experts)
: @since 2019-01
: @licence GNU http://www.gnu.org/licenses
: @version 0.2
:
: biographiX is free software: you can redistribute it and/or modify
: it under the terms of the GNU General Public License as published by
: the Free Software Foundation, either version 3 of the License, or
: (at your option) any later version.
:
:)
import module namespace G = 'bio.globals' at './globals.xqm' ;
import module namespace bio.mappings.html = 'bio.mappings.html' at './mappings.html.xqm' ;
import module namespace bio.models.bio = 'bio.models.bio' at './models.bio.xqm' ;
import module namespace bio.models.statistics = 'bio.models.statistics' at './models.statistics.xqm' ;
import module namespace Session = 'http://basex.org/modules/session';
import module namespace functx = "http://www.functx.com";
declare namespace rest = "http://exquery.org/ns/restxq" ;
declare namespace file = "http://expath.org/ns/file" ;
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization" ;
declare namespace db = "http://basex.org/modules/db" ;
declare namespace web = "http://basex.org/modules/web" ;
declare namespace update = "http://basex.org/modules/update" ;
declare namespace perm = "http://basex.org/modules/perm" ;
declare namespace user = "http://basex.org/modules/user" ;
declare namespace session = 'http://basex.org/modules/session' ;
declare namespace http = "http://expath.org/ns/http-client" ;
declare namespace json = "http://basex.org/modules/json" ;
declare namespace ev = "http://www.w3.org/2001/xml-events" ;
declare namespace eac = "https://archivists.org/ns/eac/v2" ;
declare namespace rico = "rico" ;
declare namespace map = "http://www.w3.org/2005/xpath-functions/map" ;
declare namespace xf = "http://www.w3.org/2002/xforms" ;
declare namespace xlink = "http://www.w3.org/1999/xlink" ;
declare namespace bio = "bio" ;
declare default element namespace "bio" ;
declare default function namespace "bio.bio" ;
declare default collation "http://basex.org/collation?lang=fr" ;
(:~
: This resource function defines the application root
: @return redirect to the home page or to the install
:)
declare
%rest:path("/bio")
%output:method("xml")
function index() {
if ( db:exists("bio") )
then web:redirect("/bio/home")
else web:redirect("/bio/install")
};
(:~
: This resource function install
: @return create the db
: @todo create the prosopo db
:)
declare
%rest:path("/bio/install")
%output:method("xml")
%updating
function install() {
if (db:exists("bio"))
then (
update:output("La base bio existe déjà !")
)
else (
update:output("La base bio a été créée"),
db:create( "bio")
)
};
(:~
: This resource function defines the application home
: @return redirect to the expertises list
:)
declare
%rest:path("/bio/home")
%output:method("xml")
function home() {
web:redirect("/bio/biographies/view")
};
(:~
: This resource function creates the about page
: @return an about page
:)
declare
%rest:path("/bio/about")
%output:method("html")
function about() {
let $content := map {
'title' : 'À propos',
'data' :
<div>Application Bio</div>
}
let $outputParam := map {
'layout' : "template.xml"
}
return bio.models.bio:wrapper($content, $outputParam)
};
(:~
: This resource shows app weather
: @todo template
:)
declare
%rest:path("bio/meteo")
%output:method("html")
function meteo() {
let $biographies := getBiographies()
return
<html>
<head>
<title>!bio¡</title>
<meta charset="UTF-8"/>
</head>
<body>
<div>
<h1>Météo des experts</h1>
<div class="prosopographie">
<h2>Prosopographie</h2>
<ul>
<li>{fn:count($biographies/eac:eac-cpf) || ' fiches prosopographiques enregistrées dans la base de données'}
<ul>{
for $entityType in fn:distinct-values($biographies/descendant::eac:identity/@localType)
return <li>{ fn:count($biographies/eac:eac[descendant::eac:identity[@localType = $entityType]]) || ' entités ayant pour qualité "' ||$entityType || '"' }</li>
}</ul>
</li>
<li>{ fn:count($biographies/eac:eac[descendant::eac:localControl[@localType='detailLevel']/eac:term[fn:normalize-space(.) = 'completed']]) || ' fiches complètes' }</li>
<li>{ fn:count($biographies/eac:eac[descendant::eac:localControl[@localType='detailLevel']/eac:term[fn:normalize-space(.) = 'in progress']]) || ' fiches en cours de dépouillement' }</li>
<li>{ fn:count($biographies/eac:eac[descendant::eac:localControl[@localType='detailLevel']/eac:term[fn:normalize-space(.) = 'to revise']]) || ' fiches à revoir' }</li>
</ul>
</div>
</div>
</body>
</html>
};
(:~
: this function defines a static files directory for the app
: @param $file file or unknown path
: @return binary file
:)
declare
%rest:path('bio/files/{$file=.+}')
function bio.bio:file($file as xs:string) as item()+ {
let $path := file:base-dir() || 'files/' || $file
return
(
web:response-header( map {'media-type' : web:content-type($path)}),
file:read-binary($path)
)
};
(:~
: This resource function lists the entities
: @return an xml list of persons/corporate bodies
:)
declare
%rest:path("/bio/biographies")
%rest:produces('application/xml')
%output:method("xml")
function getBiographies() {
<bio>{ db:get('bio', 'biographies') }</bio>
};
(:~
: This resource function creates an new entity
: @return an xforms for the entity
:)
declare
%rest:path("bio/biographies/new")
%output:method("xml")
%perm:allow("prosopography")
function newBiography() {
let $content := map {
'instance' : '',
'model' : ('eacModel.xml', 'eacNoValidationModel.xml'),
'trigger' : 'eacTrigger.xml',
'form' : 'eacForm.xml'
}
let $outputParam := map {
'layout' : "template.xml"
}
return(
processing-instruction xml-stylesheet { fn:concat("href='", $G:xsltFormsPath, "'"), "type='text/xsl'"},
<?css-conversion no?>,
bio.models.bio:wrapper($content, $outputParam)
)
};
(:~
: This function consumes new entity
: @param $param content
:)
declare
%rest:path("bio/biographies/put")
%output:method("xml")
%rest:header-param("Referer", "{$referer}", "none")
%rest:PUT("{$param}")
%perm:allow("prosopography")
%updating
function putBiography($param, $referer) {
let $db := db:get("bio")
return
if ($param/*/@xml:id) then
let $location := fn:analyze-string($referer, 'bio/biographies/(.+?)/modify')//fn:group[@nr='1']
return db:put('bio', $param, 'biographies/'|| $location ||'.xml')
else
let $type := switch ($param//eac:identity/eac:entityType/@value)
case 'person' return 'person'
case 'org' return 'org'
case 'family' return 'family'
default return 'other'
let $id := $type || fn:generate-id($param)
let $param :=
copy $d := $param
modify(
insert node attribute xml:id {$id} into $d/*,
replace value of node $d//eac:recordId with $id
)
return $d
return(
db:add('bio', $param, 'biographies/'|| $id ||'.xml'),
update:output((
<rest:response>
<http:response status="200" message="">
<http:header name="Content-Language" value="fr"/>
<http:header name="Content-Type" value="text/plain; charset=utf-8"/>
</http:response>
</rest:response>,
<result>
<id>{$id}</id>
<message>Une nouvelle entité a été ajoutée : {$param//eac:nameEntry[@preferred='true']/eac:part}.</message>
</result>
))
)
};
(:~
: This resource function lists all the entities
: @return an ordered list of expertises in html
:)
declare
%rest:path("bio/biographies/view")
%rest:produces('application/html')
%output:method("html")
%output:html-version('5.0')
function getBiographiesHtml() {
let $content := map {
'title' : 'Liste des entités',
'data' : getBiographies()
}
let $outputParam := map {
'layout' : "listeBiographies.xml",
'mapping' : bio.mappings.html:listEac2html(map:get($content, 'data'), map{})
}
return bio.models.bio:wrapper($content, $outputParam)
};
(:~
: This resource function lists all the entities
: @return an ordered list of entities in json
:)
declare
%rest:path("/bio/biographies/json")
%rest:POST("{$body}")
%rest:produces('application/json')
%output:media-type('application/json')
%output:method('json')
function getBiographiesJson($body) {
let $body := json:parse( $body, map{"format" : "xquery"})
let $biographies := getBiographies()
let $meta := map {
'start' : $body?start,
'count' : $body?count,
'totalBiographies' : fn:count($biographies/eac:eac)
}
let $content := array{
for $biography in fn:subsequence($biographies/eac:eac, $body?start, $body?count)
return map{
'id' : fn:normalize-space($biography/@xml:id),
'name' : bio.mappings.html:getEntityName($biography/@xml:id)
}
}
return map{
"meta": $meta,
"content": $content
}
};
(:~
: This resource function get an entity
: @return an xml representation of an entity
:)
declare
%rest:path("bio/biographies/{$id}")
%output:method("xml")
function getBiography($id) {
db:get('bio', 'biographies/'||$id||'.xml')
};
(:~
: This resource function modify an entity
: @return an xforms to modify an entity
:)
declare
%rest:path("bio/biographies/{$id}/modify")
%output:method("xml")
%perm:allow("prosopography")
function modifyBiography($id) {
let $content := map {
'instance' : $id,
'path' : 'biographies',
'model' : ('eacModel.xml', 'eacNoValidationModel.xml'),
'trigger' : 'eacTrigger.xml',
'form' : 'eacForm.xml'
}
let $outputParam := map {
'layout' : "template.xml"
}
return
(processing-instruction xml-stylesheet { fn:concat("href='", $G:xsltFormsPath, "'"), "type='text/xsl'"},
<?css-conversion no?>,
bio.models.bio:wrapper($content, $outputParam)
)
};
(:~
: This resource function show an entity
: @return an html view of an entity with xquery templating
:)
declare
%rest:path("/bio/biographies/{$id}/view")
%rest:produces('application/html')
%output:method("html")
%output:html-version('5.0')
function getBiographyHtml($id) {
let $content := map {
'title' : 'Fiche de ' || $id,
'id' : $id,
'data' : getBiography($id)/eac:eac,
'trigger' : '',
'form' : ''
}
let $outputParam := map {
'layout' : "ficheEntite.xml",
'mapping' : bio.mappings.html:eac2html(map:get($content, 'data'), map{})
}
return bio.models.bio:wrapper($content, $outputParam)
};
(:~
: This resource function lists an entities
: @return an ordered list of entities in json
:)
declare
%rest:path("/bio/biographies/{$id}/json")
%rest:produces('application/json')
%output:media-type('application/json')
%output:method('json')
function getBiographyJson($id) {
let $biography := bio.bio:getBiography($id)
let $meta := map {}
let $content := map{
'id' : fn:normalize-space($biography/eac:eac/eac:control/eac:recordId),
'authorizedForm' : bio.mappings.html:getEntityName(fn:normalize-space($biography/eac:eac/@xml:id)),
'alternativeForms' : if($biography/eac:eac/eac:cpfDescription/eac:identity/eac:nameEntry[@preferredForm != 'true'][fn:normalize-space(.)!='']) then array{
for $alternativeForm in $biography/eac:eac/eac:cpfDescription/eac:identity/eac:nameEntry[@preferredForm != 'true'][fn:normalize-space(.)!='']
return
map:merge((
map{'sources' : array{bio.mappings.html:getSources($alternativeForm/@sourceReference, $biography/eac:eac/eac:control/eac:sources, map{})}},
map{'parts' : array{
for $part in $alternativeForm/eac:part
let $d := bio.mappings.html:getPart($part, map{})
return
map{'key' : $d[1], 'part': $d[2]}
}}
))
},
'existDates' : bio.mappings.html:getEacDates($biography/eac:eac/eac:cpfDescription/eac:description/eac:existDates/eac:dateRange, $biography/eac:eac/eac:control/eac:sources, map{}),
'sex' : $biography/eac:eac/eac:cpfDescription/eac:description/eac:localDescriptions/eac:localDescription[@localType='sex']/eac:term => fn:normalize-space(),
(:'places' : if($biography/eac:cpfDescription/eac:description/eac:places/eac:place[fn:normalize-space(.)!='']) then array{
for $place in $biography/eac:cpfDescription/eac:description/eac:places/eac:place[fn:normalize-space(.)!='']
return map{
'placeRole' : $place/eac:placeRole => fn:normalize-space(),
'placeEntry' : $place/eac:placeEntry => fn:normalize-space(),
'dates' : if($place/eac:dateSet/*[descendant-or-self::*/@standardDate != '' or descendant-or-self::*/@notAfter != '' or descendant-or-self::*/@notBefore != '']) then array{
for $date in $place/eac:dateSet/*[descendant-or-self::*/@standardDate != '' or descendant-or-self::*/@notAfter != '' or descendant-or-self::*/@notBefore != '']
return if($date/self::eac:dateRange) then map{
'from' : map{
'precision' : $date/eac:fromDate/@*[fn:local-name = ('standardDate', 'notBefore', 'notAfter')]/fn:local-name(),
'date' : $date/eac:fromDate/@*[fn:local-name = ('standardDate', 'notBefore', 'notAfter')] => fn:normalize-space()
},
'to' : map{
'precision' : $date/eac:toDate/@*[fn:local-name = ('standardDate', 'notBefore', 'notAfter')]/fn:local-name(),
'date' : $date/eac:toDate/@*[fn:local-name = ('standardDate', 'notBefore', 'notAfter')] => fn:normalize-space()
}
} else map{
'precision' : $date/@*[fn:local-name = ('standardDate', 'notBefore', 'notAfter')]/fn:local-name(),
'date' : $date/@*[fn:normalize-space(.)!=''] => fn:normalize-space(),
'sources' : if($date/bio:source[@xlink:href!='']) then array{
for $source in $date/bio:source[@xlink:href!='']
return map{
'id' : $source/@xlink:href => fn:substring-after('#'),
'source' : bio.mappings.html:getSource($source, map{}),
'note' : $source => fn:normalize-space()
}
}
}
},
'note' : $place/eac:descriptiveNote/eac:p => fn:normalize-space()
}
},:)
'occupations' : if($biography/eac:eac/eac:cpfDescription/eac:description/eac:occupations/eac:occupation[fn:normalize-space(.)!='']) then array{
for $occupation in $biography/eac:eac/eac:cpfDescription/eac:description/eac:occupations/eac:occupation[fn:normalize-space(.)!='']
return map{
'occupation' : $occupation/eac:term => fn:normalize-space(),
'dates' : if($occupation/*[self::eac:date or self::eac:dateRange or self::eac:dateSet][.//@*[fn:normalize-space(.) castable as xs:date or xs:gYearMonth or xs:gYear]]) then array { bio.mappings.html:getEacDates($occupation/*[self::eac:date or self::eac:dateRange], $biography/eac:eac/eac:control/eac:sources, map{})},
'sources' : if(fn:normalize-space($occupation/@sourceReference) != '') then bio.mappings.html:getEacSourceReference($occupation/@sourceReference, $biography/eac:eac/eac:control/eac:sources)
}
},
'functions' : if($biography/eac:eac/eac:cpfDescription/eac:description/eac:functions/eac:function[fn:normalize-space(.)!='']) then array{
for $function in $biography/eac:eac/eac:cpfDescription/eac:description/eac:functions/eac:function[fn:normalize-space(.)!='']
return map{
'function' : $function/eac:term => fn:normalize-space(),
'dates' : if($function/*[self::eac:date or self::eac:dateRange or self::eac:dateSet][.//@*[fn:normalize-space(.) castable as xs:date or xs:gYearMonth or xs:gYear]]) then bio.mappings.html:getEacDates($function/*[self::eac:date or self::eac:dateRange], $biography/eac:eac/eac:control/eac:sources, map{}),
'sources' : if(fn:normalize-space($function/@sourceReference) != '') then bio.mappings.html:getEacSourceReference($function/@sourceReference, $biography/eac:eac/eac:control/eac:sources)
}
},
'events' : if($biography/eac:eac/eac:cpfDescription/eac:description/eac:biogHist/eac:chronList/eac:chronItem[fn:normalize-space(.)!='']) then array{
for $event in $biography/eac:eac/eac:cpfDescription/eac:description/eac:biogHist/eac:chronList/eac:chronItem[fn:normalize-space(.)!='']
return map{
'event' : $event/eac:event => fn:normalize-space(),
'place' : if($event/eac:place[fn:normalize-space(.)!='']) then $event/eac:place => fn:normalize-space(),
(:@todo participants ?:)
'sources' : if(fn:normalize-space($event/@sourceReference) != '') then bio.mappings.html:getEacSourceReference($event/@sourceReference, $biography/eac:eac/eac:control/eac:sources),
'dates' : if($event/*[self::eac:date or self::eac:dateRange or self::eac:dateSet][.//@*[fn:normalize-space(.) castable as xs:date or xs:gYearMonth or xs:gYear]]) then bio.mappings.html:getEacDates($event/*[self::eac:date or self::eac:dateRange or self::eac:dateSet], $biography/eac:eac/eac:control/eac:sources, map{})
}
},
'relations' : if(fn:count($biography/eac:eac/eac:cpfDescription/eac:relations/eac:relation[fn:normalize-space(.)!='']) > 0) then array{
for $relation in $biography/eac:eac/eac:cpfDescription/eac:relations/eac:relation[fn:normalize-space(.)!=''] return map{
'relation' : $relation/eac:targetEntity/eac:part[@localType='full'] => fn:normalize-space(),
'roles' : if($relation/eac:targetRole[fn:normalize-space(.)!='']) then array{
for $role in $relation/eac:targetRole
let $sources := $relation/eac:relationType[@id = fn:substring-after($role/@target, '#')]/@sourceReference
return map{
'role' : $role => fn:normalize-space(),
'sources' : if(fn:normalize-space($sources) != '') then bio.mappings.html:getEacSourceReference($sources, $biography/eac:eac/eac:control/eac:sources)
}
},
'events' : if($relation/@target[fn:normalize-space(.)!='']) then array{
for $event in fn:tokenize($relation/@target, ' ')
let $eventId := fn:substring-after($event, '#')
return map{
'event' : $biography//eac:chronItem[@id = $eventId]/eac:event => fn:normalize-space()
}
}
}
}
}
return map {
'meta' : $meta,
'content' : $content
}
};
(:~
: This resource function lists the persons or corporate bodies
: @return an xml list of persons/corporate bodies
:)
declare
%rest:path("/bio/search/{$person}")
%rest:produces('application/xml')
%output:method("xml")
function getPerson($person) {
let $prosopo := getBiographies()/eac:eac
return (
<results xmlns="">{
for $person in $prosopo[fn:normalize-space(eac:cpfDescription/eac:identity) contains text { $person } all words using fuzzy]
return <result xml:id="{ $person/@xml:id }">{$person/descendant::eac:nameEntry[@preferredForm='true'][@status='authorized'][1]/eac:part[@localType='full'] => fn:normalize-space()}</result>
}</results>
)
};
(:~
: This function consumes
:
:)
declare
%rest:path("bio/networks")
%output:method("json")
%rest:produces("application/json")
function getNetworks() {
let $nodes :=
for $entity in db:get('bio', 'biographies')
let $id := $entity/eac:eac/@xml:id => fn:normalize-space()
return map {
"id" : $id,
"name" : bio.mappings.html:getEntityName($id)
}
let $links :=
for $relation in db:get('bio', 'biographies')/descendant::eac:relation[descendant::eac:part[@localType="databaseRef"][fn:normalize-space(.)!='']]
let $sourceId := $relation/ancestor::eac:eac/@xml:id => fn:normalize-space()
let $targetId := $relation/descendant::eac:part[@localType="databaseRef"]/fn:substring-after(., '#') => fn:normalize-space()
return map {
'source' : $sourceId,
'target' : $targetId
}
return map {
'nodes' : array{$nodes},
'links' : array{$links}
}
};
(:~
: This function consumes
:
:)
declare
%rest:path("bio/networks/{$id}")
%output:method("json")
%rest:produces("application/json")
function getEntityNetworks($id) {
let $entity := db:get('bio', 'biographies')/eac:eac[@xml:id=$id]
let $relations := $entity/descendant::eac:relation[descendant::eac:part[@localType="databaseRef"][fn:normalize-space(.)!='']]
let $nodes := (
map {
"id" : $id,
"name": bio.mappings.html:getEntityName($id)
},
for $relation in $relations
let $entityId := $relation/descendant::eac:part[@localType="databaseRef"]/fn:substring-after(., '#') => fn:normalize-space()
return map {
"id" : $entityId,
"name" : bio.mappings.html:getEntityName($entityId)
}
)
let $links :=
for $relation in $relations
let $sourceId := $id
let $targetId := $relation/descendant::eac:part[@localType="databaseRef"]/fn:substring-after(., '#') => fn:normalize-space()
return map {
'source' : $sourceId,
'target' : $targetId
}
return map {
'nodes' : array{$nodes},
'links' : array{$links}
}
};
(:~
: This resource function displays network
: @return
:)
declare
%rest:path("/bio/networks/view")
%rest:produces('application/xml')
%output:method("html")
function showNetwork() {
let $content := map {
'title' : 'Networks',
'data' : ''
}
let $outputParam := map {
'layout' : "network.xml"
}
return bio.models.bio:wrapper($content, $outputParam)
};
(:~
: Permissions: biographies
: Checks if the current user is granted; if not, redirects to the login page.
: @param $perm map with permission data
:)
(: declare
%perm:check('bio/biographies', '{$perm}')
function permBiographies($perm) {
let $user := Session:get('id')
return
if((fn:empty($user) or fn:not(user:list-details($user)/*:info/*:grant/@type = $perm?allow)) and fn:ends-with($perm?path, 'new'))
then web:redirect('/bio/login')
else if((fn:empty($user) or fn:not(user:list-details($user)/*:info/*:grant/@type = $perm?allow)) and fn:ends-with($perm?path, 'modify'))
then web:redirect('/bio/login')
else if((fn:empty($user) or fn:not(user:list-details($user)/*:info/*:grant/@type = $perm?allow)) and fn:ends-with($perm?path, 'put'))
then web:redirect('/bio/login')
}; :)
(:~ Login page (visible to everyone). :)
declare
%rest:path("bio/login")
%output:method("html")
function login() {
<html>
Please log in:
<form action="/bio/login/check" method="post">
<input name="name"/>
<input type="password" name="pass"/>
<input type="submit"/>
</form>
</html>
};
(:
: this function checks if the user is registered
:)
declare
%rest:path("bio/login/check")
%rest:query-param("name", "{$name}")
%rest:query-param("pass", "{$pass}")
function login($name, $pass) {
try {
user:check($name, $pass),
Session:set('id', $name),
web:redirect("/")
} catch user:* {
web:redirect("/")
}
};
(:
: this function logs out current user
:)
declare
%rest:path("bio/logout")
function logout() {
Session:delete('id'),
web:redirect("/")
};
(:~
: This resource function edits a new user
: @return an xforms to edit a new user
:)
declare
%rest:path("bio/users/new")
%output:method("xml")
%perm:allow("admin", "write")
function newUser() {
let $content := map {
'instance' : '',
'model' : 'bioUserModel.xml',
'trigger' : '',
'form' : 'bioUserForm.xml'
}
let $outputParam := map {
'layout' : "template.xml"
}
return
(processing-instruction xml-stylesheet { fn:concat("href='", $G:xsltFormsPath, "'"), "type='text/xsl'"},
<?css-conversion no?>,
bio.models.bio:wrapper($content, $outputParam)
)
};
(:~
: This function creates new user in dba.
: @todo return creation message
: @todo control for duplicate user.
:)
declare
%rest:path("bio/users/put")
%output:method("xml")
%rest:header-param("Referer", "{$referer}", "none")
%rest:PUT("{$param}")
%perm:allow("admin", "write")
%updating
function putUser($param, $referer) {
let $user := $param
let $userName := fn:normalize-space($user/*:user/*:name)
let $userPwd := fn:normalize-space($user/*:user/*:password)
let $userPermission := fn:normalize-space($user/*:user/*:permission)
let $userInfo :=
<info xmlns="">{
for $right in $user/*:user/*:info/*:grant
return <grant type="{$right/@type}">{fn:normalize-space($right)}</grant>
}</info>
return
user:create(
$userName,
$userPwd,
$userPermission,
'bio',
$userInfo)
};