From b366bf5021a0d9afe2adb3e19d902778b63d8249 Mon Sep 17 00:00:00 2001
From: Timm Fitschen <t.fitschen@indiscale.com>
Date: Wed, 20 Oct 2021 17:53:13 +0200
Subject: [PATCH] WIP: fix SELECT id ...

---
 src/core/xsl/query.xsl             | 31 ++++++++++++------
 test/core/js/modules/entity.xsl.js |  5 +--
 test/core/js/modules/query.xsl.js  | 51 +++++++++++++++++++++++++-----
 3 files changed, 68 insertions(+), 19 deletions(-)

diff --git a/src/core/xsl/query.xsl b/src/core/xsl/query.xsl
index 2b647c07..6a95e656 100644
--- a/src/core/xsl/query.xsl
+++ b/src/core/xsl/query.xsl
@@ -223,15 +223,28 @@
         <xsl:value-of select="$field-name"/>
       </xsl:attribute>
       <div class="caosdb-f-property-value caosdb-v-property-value">
-        <xsl:apply-templates select="/Response/*[@id=$entity-id and Version/@id=$version-id]" mode="walk-select-segments">
-        <!--<xsl:apply-templates select="/Response/*[@id=$entity-id]" mode="walk-select-segments">-->
-          <xsl:with-param name="first-segment">
-            <xsl:value-of select="substring-before(concat($field-name, '.'), '.')"/>
-          </xsl:with-param>
-          <xsl:with-param name="next-segments">
-            <xsl:value-of select="substring-after($field-name, '.')"/>
-          </xsl:with-param>
-        </xsl:apply-templates>
+        <xsl:choose>
+          <xsl:when test="$version-id!=''">
+            <xsl:apply-templates select="/Response/*[@id=$entity-id and Version/@id=$version-id]" mode="walk-select-segments">
+              <xsl:with-param name="first-segment">
+                <xsl:value-of select="substring-before(concat($field-name, '.'), '.')"/>
+              </xsl:with-param>
+              <xsl:with-param name="next-segments">
+                <xsl:value-of select="substring-after($field-name, '.')"/>
+              </xsl:with-param>
+            </xsl:apply-templates>
+          </xsl:when>
+          <xsl:otherwise>
+            <xsl:apply-templates select="/Response/*[@id=$entity-id]" mode="walk-select-segments">
+              <xsl:with-param name="first-segment">
+                <xsl:value-of select="substring-before(concat($field-name, '.'), '.')"/>
+              </xsl:with-param>
+              <xsl:with-param name="next-segments">
+                <xsl:value-of select="substring-after($field-name, '.')"/>
+              </xsl:with-param>
+            </xsl:apply-templates>
+          </xsl:otherwise>
+        </xsl:choose>
       </div>
     </td>
   </xsl:template>
diff --git a/test/core/js/modules/entity.xsl.js b/test/core/js/modules/entity.xsl.js
index bbc1cac0..e5ff1e87 100644
--- a/test/core/js/modules/entity.xsl.js
+++ b/test/core/js/modules/entity.xsl.js
@@ -317,7 +317,7 @@ function applyTemplates(xml, xsl, mode, select = "*") {
     return xslt(xml, modXsl);
 }
 
-function callTemplate(xsl, template, params, wrap_call) {
+function callTemplate(xsl, template, params, wrap_call, root) {
     let entryRuleStart = '<xsl:call-template name="' + template + '">';
     let entryRuleEnd = '</xsl:call-template>';
     var entryRule = entryRuleStart;
@@ -331,5 +331,6 @@ function callTemplate(xsl, template, params, wrap_call) {
     entryRule = '<xsl:template xmlns="http://www.w3.org/1999/xhtml" priority="9" match="/">' +
         entryRule + '</xsl:template>';
     let modXsl = injectTemplate(xsl, entryRule);
-    return xslt(str2xml('<root/>'), modXsl);
+    root = root || '<root/>';
+    return xslt(str2xml(root), modXsl);
 }
diff --git a/test/core/js/modules/query.xsl.js b/test/core/js/modules/query.xsl.js
index 371b5159..ed75283c 100644
--- a/test/core/js/modules/query.xsl.js
+++ b/test/core/js/modules/query.xsl.js
@@ -28,19 +28,25 @@ QUnit.module("query.xsl", {
         // load query.xsl
         var done = assert.async();
         var qunit_obj = this;
-        $.ajax({
-            cache: true,
-            dataType: 'xml',
-            url: "xsl/query.xsl",
-        }).done(function(data, textStatus, jdXHR) {
-            insertParam(data, "entitypath", "/entitypath/");
-            qunit_obj.queryXSL = data;
-        }).always(function() {
+        _retrieveQueryXSL().then(function(xsl) {
+            qunit_obj.queryXSL = xsl;
             done();
         });
     }
 });
 
+async function _retrieveQueryXSL() {
+    var queryXsl = await transformation.retrieveXsltScript("query.xsl");
+    var entityXsl = await transformation.retrieveXsltScript("entity.xsl");
+    var commonXsl = await transformation.retrieveXsltScript("common.xsl");
+    var xsl = transformation.mergeXsltScripts(entityXsl, [commonXsl, queryXsl]);
+    insertParam(xsl, "entitypath", "/entitypath/");
+    insertParam(xsl, "filesystempath", "/filesystempath/");
+    insertParam(xsl, "lowercase", "abcdefghijklmnopqrstuvwxyz");
+    insertParam(xsl, "uppercase", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
+    return xsl;
+}
+
 /* TESTS */
 QUnit.test("availability", function(assert) {
     assert.ok(this.queryXSL);
@@ -183,6 +189,35 @@ QUnit.test("template select-table-row ", function(assert){
     assert.equal(next.tagName, "A", "tagName = A");
 });
 
+QUnit.test("template select-table-cell (with version) ", function(assert){
+  let cell = callTemplate(this.queryXSL, "select-table-cell", {"version-id": "vid-2345", "entity-id": "eid-1234", "field-name": "name"}, (x) => `<table><tbody><tr>${x}</tr></tbody></table>`,`<Response><Entity id="eid-1234" name="the-name"><Version id="vid-2345"/></Entity></Response>`);
+    var next = cell.firstElementChild;
+    assert.equal(next.tagName, "TABLE", "tagName = TABLE");
+    next = next.firstElementChild;
+    assert.equal(next.tagName, "TBODY", "tagName = TBODY");
+    next = next.firstElementChild;
+    assert.equal(next.tagName, "TR", "tagName = TR");
+    next = next.firstElementChild;
+    assert.equal(next.tagName, "TD", "tagName = TD");
+    next = next.textContent;
+    assert.equal(next, "eid-1234", "id = eid-1234");
+});
+
+
+QUnit.test("template select-table-cell (id only) ", function(assert){
+    let cell = callTemplate(this.queryXSL, "select-table-cell", {"entity-id": "eid-1234", "field-name": "id"}, (x) => `<table><tbody><tr>${x}</tr></tbody></table>`,`<Response><Entity id="eid-1234"/></Response>`);
+    var next = cell.firstElementChild;
+    assert.equal(next.tagName, "TABLE", "tagName = TABLE");
+    next = next.firstElementChild;
+    assert.equal(next.tagName, "TBODY", "tagName = TBODY");
+    next = next.firstElementChild;
+    assert.equal(next.tagName, "TR", "tagName = TR");
+    next = next.firstElementChild;
+    assert.equal(next.tagName, "TD", "tagName = TD");
+    next = next.textContent;
+    assert.equal(next, "eid-1234", "id = eid-1234");
+});
+
 /* MISC FUNCTIONS */
 function getQueryForm(queryXSL) {
     var html = callTemplate(queryXSL, "caosdb-query-panel", {});
-- 
GitLab