From 9f35b090ef074e404955b1d0b167c31f2d3d2793 Mon Sep 17 00:00:00 2001
From: Florian Spreckelsen <f.spreckelsen@indiscale.com>
Date: Tue, 7 Jan 2025 15:31:46 +0100
Subject: [PATCH] ENH: Add custom reference resolver

---
 .../ext/js/bis_custom_reference_resolver.js   | 107 ++++++++++++++++++
 1 file changed, 107 insertions(+)
 create mode 100644 sample-management-custom/caosdb-server/caosdb-webui/src/ext/js/bis_custom_reference_resolver.js

diff --git a/sample-management-custom/caosdb-server/caosdb-webui/src/ext/js/bis_custom_reference_resolver.js b/sample-management-custom/caosdb-server/caosdb-webui/src/ext/js/bis_custom_reference_resolver.js
new file mode 100644
index 0000000..780a0c2
--- /dev/null
+++ b/sample-management-custom/caosdb-server/caosdb-webui/src/ext/js/bis_custom_reference_resolver.js
@@ -0,0 +1,107 @@
+var bis_custom_reference_resolver = new function (getEntityID, getEntityName, getParents, getProperty, query) {
+
+    const lastname_prop_name = "Last name";
+    const firstname_prop_name = "First name";
+    const person_rt_name = "Person";
+    const bis_label_prop_name = "BIS label";
+    const custom_label_prop_name = "Custom label"
+    const container_rt_name = "Container";
+
+    const value_p_name = 'enumValue'
+    const fixation_rt_name = "Fixation"
+    const temp_storage_rt_name = "StorageTemperature"
+
+    /*
+     * Collect the names of gear types dynamically
+     */
+    this._getGearTypeNames = async function () {
+        const gearTypeNames = await query('SELECT name FROM RECORDTYPE Gear');
+        return gearTypeNames.map(e => getEntityName(e));
+    };
+
+    /**
+     * Return the name of the record type and value
+     */
+    this.rt_with_val = function (el) {
+        return this.rt_with_prop_value(el, value_p_name);
+    };
+
+    this.rt_with_prop_value = function (el, propName) {
+        var text = getParents(el)[0].name;
+        var valpr = getProperty(el, propName, false);
+        if (valpr == undefined) {
+            return text;
+        }
+        return text + ": " + valpr;
+    };
+
+    this.resolve_person_reference = function (entity) {
+        var properties = getProperties(entity);
+        if (properties == undefined) {
+            return;
+        }
+        const firstName = getProperty(entity, firstname_prop_name, false);
+        const lastName = getProperty(entity, lastname_prop_name, false);
+        if (firstName == undefined) {
+            if (lastName == undefined) {
+                return;
+            }
+            return lastName;
+        }
+        if (lastName == undefined) {
+            return lastName;
+        }
+        return firstName + " " + lastName;
+    };
+
+    this.resolve_default = function (entity) {
+        // This is the BIS default: If an entity has a valid BIS label, the
+        // preview should be "id, BIS label". If there is no BIS label but the
+        // entity has a name, the preview is "id, name".  Fallback is "id".
+        const bisLabel = getProperty(entity, bis_label_prop_name, false);
+        const customLabel = getProperty(entity, custom_label_prop_name, false);
+        const name = getEntityName(entity);
+        const id = getEntityID(entity);
+        var label = customLabel;
+        if (label == undefined) {
+            label = bisLabel;
+        }
+        if (label == undefined) {
+            if (name == undefined || name === "") {
+                return id;
+            }
+            return `${id}, ${name}`;
+        }
+        if (`${id}` === `${label}`) {
+            // prevent special case of BisLabel = BisID resulting in a preview that looks like "id, id".
+            return id;
+        }
+        return `${id}, ${label}`;
+    };
+
+    this.resolve = async function (id) {
+        const entity = (await resolve_references.retrieve(id))[0];
+        const gearTypeNames = await this._getGearTypeNames();
+        if (gearTypeNames.some(name => resolve_references.is_child(entity, name))) {
+            return {
+                "text": this.rt_with_prop_value(entity, "Configuration")
+            };
+        } else if (resolve_references.is_child(entity, person_rt_name)) {
+            return {
+                "text": this.resolve_person_reference(entity)
+            };
+        } else if (resolve_references.is_child(entity, temp_storage_rt_name)) {
+            return {
+                "text": this.rt_with_val(entity)
+            };
+        } else if (resolve_references.is_child(entity, fixation_rt_name)) {
+            return {
+                "text": this.rt_with_val(entity)
+            };
+        } else {
+            return {
+                "text": this.resolve_default(entity)
+            }
+        }
+    };
+}(getEntityID, getEntityName, getParents, getProperty, query);
-- 
GitLab