From 2fe42126c7a7a2f0f30b70a6eef1ee4f562feb2d Mon Sep 17 00:00:00 2001
From: Bill Erickson <berickxx@gmail.com>
Date: Wed, 6 Mar 2019 17:41:25 -0500
Subject: [PATCH] LP#1819179: Angular value formatter gets link smarts

Teach the FormatService to display selector values for linked objects
when the requested field is a link field and it contains an object value
instead of just a id/key value.

Improve IDL data passing from the grid to the IDL service so it can
better determine which fields are avialble for link selector display.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Signed-off-by: Dan Wells <dbw2@calvin.edu>
---
 Open-ILS/src/eg2/src/app/core/format.service.ts | 38 +++++++++++++++++++++++++
 Open-ILS/src/eg2/src/app/share/grid/grid.ts     | 12 ++++++++
 2 files changed, 50 insertions(+)

diff --git a/Open-ILS/src/eg2/src/app/core/format.service.ts b/Open-ILS/src/eg2/src/app/core/format.service.ts
index 2c7e3885d9..ad7e9ce180 100644
--- a/Open-ILS/src/eg2/src/app/core/format.service.ts
+++ b/Open-ILS/src/eg2/src/app/core/format.service.ts
@@ -71,6 +71,44 @@ export class FormatService {
 
         switch (datatype) {
 
+            case 'link':
+                if (typeof value !== 'object') {
+                    return value + ''; // no fleshed value here
+                }
+
+                if (!params.idlClass || !params.idlField) {
+                    // Without a full accounting of the field data,
+                    // we can't determine the display value.
+                    return value + '';
+                }
+
+                const localClass = this.idl.classes[params.idlClass];
+
+                if (!localClass) {
+                    console.warn(`No such IDL class ${params.idlClass}`);
+                    return value + '';
+                }
+
+                if (!localClass.field_map[params.idlField]) {
+                    console.warn(`IDL class ${params.idlClass} ` +
+                        `has no field named "${params.idlField}"`);
+                    return value + '';
+                }
+
+                const linkType = localClass.field_map[params.idlField]['reltype'];
+                if (linkType !== 'has_a') {
+                    return value + ''; // eh?
+                }
+
+                const localField = localClass.field_map[params.idlField];
+                const remoteKey = localField['key'];
+
+                const remoteClass = this.idl.classes[localField['class']];
+                const remoteField = remoteClass.field_map[remoteKey];
+                const remoteSelector = remoteField.selector || remoteField.name;
+
+                return value[remoteSelector]() + '';
+
             case 'org_unit':
                 const orgField = params.orgField || 'shortname';
                 const org = this.org.get(value);
diff --git a/Open-ILS/src/eg2/src/app/share/grid/grid.ts b/Open-ILS/src/eg2/src/app/share/grid/grid.ts
index 16c5ea1279..3743488c39 100644
--- a/Open-ILS/src/eg2/src/app/share/grid/grid.ts
+++ b/Open-ILS/src/eg2/src/app/share/grid/grid.ts
@@ -20,6 +20,8 @@ export class GridColumn {
     hidden: boolean;
     visible: boolean;
     sort: number;
+    // IDL class of the object which contains this field.
+    // Not to be confused with the class of a linked object.
     idlClass: string;
     idlFieldDef: any;
     datatype: string;
@@ -189,6 +191,7 @@ export class GridColumnSet {
             const idlInfo = this.idlInfoFromDotpath(col.path);
             if (idlInfo) {
                 col.idlFieldDef = idlInfo.idlField;
+                col.idlClass = idlInfo.idlClass.name;
                 if (!col.label) {
                     col.label = col.idlFieldDef.label || col.idlFieldDef.name;
                     col.datatype = col.idlFieldDef.datatype;
@@ -634,6 +637,8 @@ export class GridContext {
 
         return this.format.transform({
             value: val,
+            idlClass: col.idlClass,
+            idlField: col.idlFieldDef ? col.idlFieldDef.name : col.name,
             datatype: col.datatype,
             datePlusTime: Boolean(col.datePlusTime)
         });
@@ -682,6 +687,12 @@ export class GridContext {
             if (!col.datatype) {
                 col.datatype = idlField.datatype;
             }
+            if (!col.idlFieldDef) {
+                idlField = col.idlFieldDef;
+            }
+            if (!col.idlClass) {
+                col.idlClass = idlClassDef.name;
+            }
             if (!col.label) {
                 col.label = idlField.label || idlField.name;
             }
@@ -858,6 +869,7 @@ export class GridContext {
             col.name = field.name;
             col.label = field.label || field.name;
             col.idlFieldDef = field;
+            col.idlClass = this.columnSet.idlClass;
             col.datatype = field.datatype;
             col.isIndex = (field.name === pkeyField);
             col.isAuto = true;
-- 
2.11.0