LP1825851 Server managed/processed print templates
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / core / idl.service.ts
index 89f8411..21ec24a 100644 (file)
@@ -133,5 +133,68 @@ export class IdlService {
 
         return result;
     }
+
+    // Given a field on an IDL class, returns the name of the field
+    // on the linked class that acts as the selector for the linked class.
+    // Returns null if no selector is found or the field is not a link.
+    getLinkSelector(fmClass: string, field: string): string {
+        let fieldDef = this.classes[fmClass].field_map[field];
+
+        if (fieldDef.map) {
+            // For mapped fields, we want the selector field on the
+            // remotely linked object instead of the directly
+            // linked object.
+            const linkedClass = this.classes[fieldDef.class];
+            fieldDef = linkedClass.field_map[fieldDef.map];
+        }
+
+        if (fieldDef.class) {
+            const classDef = this.classes[fieldDef.class];
+            if (classDef.pkey) {
+                return classDef.field_map[classDef.pkey].selector || null;
+            }
+        }
+        return null;
+    }
+
+    toHash(obj: any, flatten?: boolean): any {
+
+        if (typeof obj !== 'object' || obj === null) {
+            return obj;
+        }
+
+        if (Array.isArray(obj)) {
+            return obj.map(item => this.toHash(item));
+        }
+
+        const fieldNames = obj._isfieldmapper ?
+            Object.keys(this.classes[obj.classname].field_map) :
+            Object.keys(obj);
+
+        const hash: any = {};
+        fieldNames.forEach(field => {
+
+            const val = this.toHash(
+                typeof obj[field] === 'function' ?  obj[field]() : obj[field],
+                flatten
+            );
+
+            if (val === undefined) { return; }
+
+            if (flatten && val !== null &&
+                typeof val === 'object' && !Array.isArray(val)) {
+
+                Object.keys(val).forEach(key => {
+                    const fname = field + '.' + key;
+                    hash[fname] = val[key];
+                });
+
+            } else {
+                hash[field] = val;
+            }
+        });
+
+        return hash;
+    }
 }