Skip to content
Snippets Groups Projects
Commit 9f35b090 authored by Florian Spreckelsen's avatar Florian Spreckelsen
Browse files

ENH: Add custom reference resolver

parent fede2cbc
No related branches found
No related tags found
No related merge requests found
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);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment