eXist-db passing parameters to templates through controller.xql (URL mapping) - xquery

I am working with eXist-db 4.2.1 and Xquery 3.1 using the eXist's default installation of controller.xql and view.xq.
I have a document.html to which I pass any incoming url structured with /doc/some-requested-doc-id at the end to produce a dynamically-created page based on some-requested-doc-id.
So, the incoming url can be either http://localhost:8080/exist/apps/deheresi/doc/MS609-0001 or
http://localhost:8080/exist/apps/deheresi/doc/MS609-0001.xml
and they are treated the same...
In the file controller.xql I have a condition for matching this request, which identifies /doc/ and cleans up the expected some-requested-doc-id using a function which is passed to parameter name="currentdoc":
[...]
else if (starts-with($exist:path, "/doc/")) then
(: strip out any extensions and rename global variable as .xml:)
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/document.html">
<add-parameter name="currentdoc"
value="{concat(functx:substring-before-match($exist:resource,'[.]'),'.xml')}"/>
</forward>
<view>
<forward url="{$exist:controller}/modules/view.xql"/>
</view>
</dispatch>
[...]
The requested .html file is as follows, which itself calls other HTML templates and/or dynamically created content in XQuery:
<div data-template="templates:surround" data-template-with="templates/site_wrapper.html" data-template-at="content">
<div data-template="document:title-bar"/>
<div class="col-sm-12">
<div class="col-md-2 sidebar">
<div data-template="document:doc-sidebar-sub1"/>
<div data-template="document:doc-sidebar-sub2"/>
<div data-template="document:doc-sidebar-sub3"/>
<div data-template="document:doc-sidebar-sub4"/>
</div>
<div class="col-md-10 document-view">
<div data-template="document:doc-xsl-docview"/>
</div>
</div>
</div>
The 5 data-template="document:... calls depend on the same parameter provided by <add-parameter>, for example <div data-template="document:title-bar"/> calls:
declare function document:title-bar(
$node as node(),
$model as map(*),
$currentdoc as xs:string)
{
let $persid := person:person-name(data(doc(concat($globalvar:URIdata,$currentdoc))/tei:TEI/tei:text//tei:persName[#role="dep"]/#nymRef))
let $doctypeen := data(doc(concat($globalvar:URIdata,$currentdoc))/tei:TEI/tei:text//tei:div[#type="doc_type"]/#subtype)
let $x :=
<div class="col-md-12 document-title">
<h2><span class="en">{$doctypeen}: </span><span class="fr">{document:doc-type-french($doctypeen)} : </span>{$persid}</h2>
</div>
return $x
};
Even if I hard-code the parameter in the module controller.xql:
<add-parameter name="currentdoc" value="MS609-00001.xml"/>
I still get the same error, which doesn't happen if I hard code the parameter in the template call:
The actual cardinality for parameter 3 does not match the
cardinality declared in the function's signature:
document:title-bar($node as node(), $model as map,
$currentdoc as xs:string) item()*.
Expected cardinality: exactly one, got 0.
The 'expected cardinality' suggests that the parameter is not coming into the function?
EDIT:
If I change the order of parameters in the function above to
declare function document:title-bar(
$currentdoc as xs:string,
$node as node(),
$model as map(*))
I get a different error:
Supplied argument 2 of function:
document:title-bar($currentdoc as xs:string,
$node as node(), $model as map) item()* does not
match required type. Required type node(), got map. `
Many thanks in advance.

The <add-parameter> directive needs to be moved to the 2nd <forward> directive—so that modules/view.xql has access to the parameter. The corrected version of this fragment of your controller is:
else if (starts-with($exist:path, "/doc/")) then
(: strip out any extensions and rename global variable as .xml:)
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/document.html"/>
<view>
<forward url="{$exist:controller}/modules/view.xql">
<add-parameter name="currentdoc" value="{concat(functx:substring-before-match($exist:resource,'[.]'),'.xml')}"/>
</forward>
</view>
</dispatch>
The templating documentation also shows this - see the 2nd code sample under the "Set up" section here: https://exist-db.org/exist/apps/doc/templating#D3.35.
(There was a mistake in the answer you referenced - which I have now corrected. Apologies, and thanks for your thorough testing and well-articulated questions!)

Related

Thymleaf | parameterized segment in a th:each

I am trying to use a parameterized segment in a th:each, but I am running into this exception:
EL1007E: Property or field 'author' cannot be found on null
If I got it correct, it means that the object whose variables I'm trying to access is null, though through breakpoint and debug in my Spring MVC app I know for sure there are two elements in the list.
Here is the Controller:
#GetMapping("/")
public String getHomePage(Model model) {
log.info("Recupero la home-page");
model.addAttribute("reviews", mainService.getAllReviews());
return "home";
}
Here is the th:each:
<div
th:each="review: ${reviews}"
th:assert="({review} != null)"
th:replace="fragments/utilities :: test(author=${review.author},message=${review.review})"
></div>
Here is the fragment:
<div th:fragment="test(author, message)">
<p th:text="${message}" class="mt-2 text-dark"></p>
<h6 th:text="${author}"></h6>
</div>
Here is a screenshot of the Model at runtime right before returning the webpage to the client:
What's going wrong? Why does it say review object is null?
There is an issue with your main Thymeleaf template. You need to account on Thymeleaf Attribute Precedence which indicates that th:replacewill be running first, before th:each and replace the entire <div> element. The working code may look similar to ...
<th:block th:each="review: ${reviews}">
<div th:replace="fragments/utilities :: test(author=${review.author},message=${review.review})"></div>
</th:block>

Add a variable to all rendering

I'm operating on a symfony application and I would like to set a new variable from all controllers to all rendering.
The reason is that something in my footer has become dynamic and in compliance with the MVC pattern I'd like to put the processing of this new data in my controllers.
What is the good way to do this with symfony ?
EDIT
I am not using symfony as a REST API, the Symfony server is only serving twig rendered templates as HTML.
Details on my case :
the current twig template has hard-coded titles for a form :
<div>
<h2>Today</h2>
<!-- today's inputs .... -->
</div>
<div>
<h2>Tomorrow</h2>
<!-- tomorrow's inputs .... -->
</div>
I'd like to give to variables to my views : $today and $tomorrow.
This way I'd be able to render day names instead of today or tomorrow.
<div>
<h2>{{ today }}</h2>
<!-- today's inputs .... -->
</div>
<div>
<h2>{{ tomorrow }}</h2>
<!-- tomorrow's inputs .... -->
</div>
For example if today is Tuesday, variables has to be assigned this way :
$today = "Tuesday" and $tomorrow = "Wednesday".
What's more
This is not a question about this specific case. I'd like to now if there is a way to pass a variable to all views without editing all controllers. As I see it, I'd put a parent action to all controller to generate this variable. I just wanted to know if this is the usual way.
I don't want to use ajax calls, and I don't want to put complex twig code inside my template. I want to handle this via controllers.
Please read official documentation about using global variables.
Off the top of my head - you can inject...
...scalar values from the global twig config
...scalar values from the service container parameters
...services (read php objects)
Or you can write Twig extension, like:
class DateExtension extends \Twig_Extension
{
public function getFunctions()
{
return [
new \Twig_SimpleFunction('get_date', array($this, 'getDate'))
];
}
public function getDate($date)
{
// format it how you want
return (new \DateTime($date))->format('Y-m-d H:i:s');
}
}
And then use it in any template simply by:
<div>
<h2>{{ get_date('today') }}</h2>
<!-- today's inputs .... -->
</div>
<div>
<h2>{{ get_date('tomorrow') }}</h2>
<!-- tomorrow's inputs .... -->
</div>
About what kind of "dynamic" do you speak? If this dynamic goes from changing of some value stored in the data storage of your app, than, I belive, you could define controller action to retrive this data from, for example, your database and then call it via AJAX on every page load to obtain those value on client-side. Or maybe even use help of WebSocket's. But this is just assumption. If you really need help you should provide us more information about context of your task.

Trouble using xmldb:store with collection name with space

I cannot seem to get this right. I have a system where the web user is running a jQuery request to add a new document. The code works perfect if the collection they are storing to have no spaces in the name. This code is:
xquery version "3.0";
declare option exist:serialize "method=xhtml media-type=text/html indent=yes";
let $specialty := request:get-parameter("specialty", "")
let $collection := xmldb:encode-uri($specialty)
let $docnum := request:get-parameter("docnum", "")
let $title := request:get-parameter("title", "")
let $file := concat($docnum, '.xml')
let $path := concat($collection, '/', $file)
let $content := <article>
<div
class="content"
doc="{$docnum}"
title="{xmldb:decode($title)}">
<div
class="panel">
<h1>First Panel</h1>
<p>Content goes here</p>
</div>
</div>
</article>
return
let $new_article := xmldb:store($collection, $file, $content, 'application/xml')
return
let $changepermiss := sm:chmod($new_article, 'rw-rw----')
return
<li
class="file"
data-xpath="{$path}"
>
<a
data-xpath="{$path}"
onclick="doc_open('{$path}')"
href="#"><span
class="glyphicon glyphicon-tags"></span>   {$docnum, ': ', xmldb:decode($title)}</a>
</li>
Now, if the collection name that comes from the request contains a space ... like this (after encoding) ... it fails.
"/db/Test/data/Core/Test%20Collection"
The error is:
exerr:ERROR Could not locate collection: /db/Test/data/Core/Test%20Collection
This occurs at this line: xmldb:store($collection, $file, $content, 'application/xml') with $collection not be recognized because of the space.
Now, to test things out, I run this and it is fine.
xquery version "3.0";
declare option exist:serialize "method=xhtml media-type=text/html indent=yes";
let $test := "/db/EIDO/data/Core/Disease%20Prevention"
let $content := <article>
<div
class="content"
doc="D777"
title="Test Document">
<div
class="panel">
<h1>First Panel</h1>
<p>Content goes here</p>
</div>
</div>
</article>
return
let $newdoc := xmldb:store($test, "test.xml", $content, 'application/xml')
return
$newdoc
I cannot understand where I am going wrong in trying to reference the collection with a space in xmldb:store.
NOTE: As I said, the code works if there is no space in the name of the collection.
I have tried many combinations of encoding the URL and making a string from it but nothing seems to work.
If your collection or resource names are possibly going to contain characters that need to be encoded, you need to use xmldb:encode-uri() on the names. In your first sample, you encode the collection name ($collection) but not the resource name ($file). In your second sample, you pre-encode the collection name, but the resource name contains no characters that need to be encoded; so you've fully accounted for the encoding.
Take care to ensure that the collection name, as encoded, does exist. If not, you should pre-create the collection. This could actually be the cause of the error you cite.
When you create links to these resources in HTML output, take care not to double-encode them.
You might find it useful to reference an app that handles encoding well. See the source to eXide https://github.com/wolfgangmm/eXide.
The answer was permission issue (although I am a bit lost at that).
The collection had rw-rw-r-- and the user is in the group and is in fact the owner of the collection.
When I change to include execute (rwxrwxr--) it works as expected. Confusing, but apparently execute permission in required on the collection that is going to be stored into.

Meteor: single post view, via id and flow router is not parsing any data from the collection

Im trying to open a single record from my #each loop of items into its own view by clicking a link that says 'see more', which will take me to the single article. I set up my Flow router and its working but i cannot see the data that's supposed to come in.
the template helper for the single article looks like this
Template.collectionSingle.helpers({
articles: function () {
var id = FlowRouter.getParam('_id')
return theCollection.findOne({_id: id});
}
});
}
my route looks like this
FlowRouter.route('/collection/:_id', {
name: 'collection',
action() {
BlazeLayout.render("AppLayout", {main: "collectionSingle"});
}
});
and the template "collectionSingle" looks like this
<template name="collectionSingle">
<h1>{{title}}</h1>
<h1>This is a test</h1>
<img src="{{thumbnail}}" alt="" />
</template>
when i navigate to http://localhost:3000/collection/thePublication-0
all i see is the test message 'This is a test', but i don't see the {{title}} nor the thumbnail.
furthermore, when i change:
return theCollection.findOne({_id: id});
to another one of my collections :
return OtherCollection.findOne({_id: id});
http://localhost:3000/collection/thePublication-0
remains the same.
how can i successfully have a single articles page for each of my articles and have them linked properly with flow router?
You need to actually use your template helper that is returning the data context:
<template name="collectionSingle">
<h1>{{title}}</h1>
<h1>This is a test</h1>
{{#with articles}}
<img src="{{thumbnail}}" alt="" />
{{/with}}
</template>
Since your articles helper returns a single document you use {{#with articles}}. If it returned a cursor or array you would loop over that with {{#each articles}}. The normal convention is to use the singular form for the former, the plural for the latter.

XQuery Recursive Function Call for Inner Tag

I'm trying to prepare an XML file to parse it JSON and its context is such as:
<user_manual>
<embed-language_part id="SL14686180">
<language_part id="1" role="-" lang="de">
<embed-user_manual_part id="1">
<user_manual_part id="1" role="-" document-type="IU">
<embed-chapter id="1">
<?ecls-start-embedded-resource resource="ecls_bio_becls_a3_a30660983"?>
<chapter id="1" role="-" toctitle="yes" footrowtitle="no" type="security">
<embed-title_module id="1">
<title_module id="1" role="-">
<title id="1">Sicherheits- und Warnhinweise</title>
</title_module>
</embed-title_module>
<embed-section id="1">
<section id="1" footrowtitle="no" role="-" toctitle="yes">
<embed-section id="2">
<section id="2">
<embed-title_module id="2">
<title_module id="2" role="-">
<title id="2">Eisschale</title>
</title_module>
</embed-title_module>
</section>
</embed-section>
<embed-title_module id="3">
<title_module id="31" role="-">
<title id="3">Bevor Sie das Gerat in Betrieb nehmen</title>
</title_module>
</embed-title_module>
</section>
</embed-section>
</chapter>
</embed-chapter>
</user_manual_part>
</embed-user_manual_part>
</language_part>
</embed-language_part>
</user_manual>
I wrote an XQuery script regarding to my expectations first (assume that $doc is document, $matnr is 22333),
declare variable $doc external;
declare variable $matnr external;
<dmContainer>{
for $language in $doc/user_manual/embed-language_part/language_part
let $lang_code := data($language/#lang)
for $embed_chapter in $language/embed-user_manual_part/user_manual_part/embed-chapter
let $objectid := data($embed_chapter/processing-instruction('ecls-start-embedded-resource'))[1]
let $fileattr := string($objectid)
let $filename := translate(substring-after($objectid,'resource='),'"','')
let $postfix := substring(tokenize($filename,'_')[last()], 2)
let $name := concat($matnr, '_', $postfix)
return (element {$lang_code} {
attribute title {data($embed_chapter/chapter/embed-title_module/title_module/title)},
attribute language {$lang_code},
attribute name {$name},
for $section in $embed_chapter/chapter/embed-section/section
return <section title="{data($section/embed-title_module/title_module/title)}"></section>
})
}</dmContainer>
This returns:
<dmContainer>
<de title="Sicherheits- und Warnhinweise" language="de" name="223333_30660983">
<section title="Bevor Sie das Gerat in Betrieb nehmen" />
</de>
</dmContainer>
Return contains the chapter element and its first section's title for the JSON but I have to add this one to all sections (the sections included by sections too).
According to the input XML the sections can have another sections (one or more) recursively. You can look the example by searching it deeply. The question is that how i can add these sections to my output with a proper recursive way(i mean not just the child level one level two children are included too) , i searched for some examples recursive functions of XQuery but i couldn't get any one.
Expected output:
<dmContainer>
<de title="Sicherheits- und Warnhinweise" language="de" name="223333_30660983">
<section title="Bevor Sie das Gerat in Betrieb nehmen">
<section title="Eisschale"/>
</section>
</de>
</dmContainer>
How can I get all sections?
If you just want all sections in that chapter (in document order), go with the descendant-or-self-step, abbreviated //.
for $section in $embed_chapter/chapter//embed-section/section
return <section title="{data($section/embed-title_module/title_module/title)}"
If document order doesn't work out for you (eg., first the title of the current section, then all subsections on the current level, no matter if they actually precede the title), you will have to write your own function traversing the tree: a function that gets the current section, returns the title, and recursively calls itself for the direct subsections (if there are any).

Resources