|
| 1 | +var DataLookupUtils = Class.create(); |
| 2 | + |
| 3 | +DataLookupUtils.prototype = { |
| 4 | + initialize: function (tableName, sortByColumn) { |
| 5 | + this.tableName = tableName; |
| 6 | + this.columnNames = this._getColumnNames(tableName); |
| 7 | + this.queryColumns = this.columnNames; |
| 8 | + |
| 9 | + sortByColumn = sortByColumn || false; |
| 10 | + this.sortColumn = sortByColumn ? DataLookupUtils.SORT_BY_COLUMN : DataLookupUtils.SORT_BY_ORDER; // optional, use the field being retrieved or the built in order field |
| 11 | + }, |
| 12 | + |
| 13 | + /** |
| 14 | + * query lookup data |
| 15 | + * can accept either an array of keys, or multiple parameters |
| 16 | + * e.g |
| 17 | + * getLookupData('key1', 'key2', 'key3'); |
| 18 | + * getLookupData(['key1', 'key2', 'key3']); |
| 19 | + */ |
| 20 | + getLookupData: function (_keys) { |
| 21 | + var keys = []; |
| 22 | + if (_keys && typeof _keys != 'object') { |
| 23 | + for (var _arg in arguments) { |
| 24 | + keys.push(arguments[_arg]); |
| 25 | + } |
| 26 | + } else { |
| 27 | + keys = _keys; |
| 28 | + } |
| 29 | + |
| 30 | + if (keys.length >= this.queryColumns.length) { |
| 31 | + NiceError.raise(gs.getMessage("Too many keys ({0}) provided. Maximum is {1}", [keys.length.toString(), (this.queryColumns.length - 1).toString()])); |
| 32 | + } |
| 33 | + |
| 34 | + try { |
| 35 | + var fieldIdx = 0; |
| 36 | + var fieldName = this.queryColumns[fieldIdx]; |
| 37 | + var gq = new global.GlideQuery(this.tableName) |
| 38 | + .where('active', true); |
| 39 | + |
| 40 | + if (keys.length > 0) { |
| 41 | + // loop through the keys |
| 42 | + keys.forEach(function (key, idx, arr) { |
| 43 | + gq = gq.where(fieldName, key); |
| 44 | + fieldName = this.queryColumns[++fieldIdx]; |
| 45 | + }, this); |
| 46 | + } |
| 47 | + |
| 48 | + gq = gq.orderBy(this.sortColumn == DataLookupUtils.SORT_BY_COLUMN ? fieldName : 'order') |
| 49 | + .whereNotNull(fieldName) |
| 50 | + .select(fieldName) |
| 51 | + .map(function (_x) { |
| 52 | + // just need the data, not the other stuff |
| 53 | + return _x[fieldName]; |
| 54 | + }) |
| 55 | + .reduce(function (arr, e) { |
| 56 | + // remove duplicates |
| 57 | + if (arr.indexOf(e) == -1) arr.push(e); |
| 58 | + return arr; |
| 59 | + }, []); |
| 60 | + |
| 61 | + return gq; |
| 62 | + } catch (e) { |
| 63 | + NiceError.raise(e); |
| 64 | + } |
| 65 | + }, |
| 66 | + |
| 67 | + /** |
| 68 | + * override the columns to use for lookups |
| 69 | + * |
| 70 | + */ |
| 71 | + setColumns: function (columns) { |
| 72 | + if (columns.length == 0) return; |
| 73 | + |
| 74 | + this.queryColumns = []; |
| 75 | + columns.forEach(function (_col) { |
| 76 | + if (this.columnNames.indexOf(_col) > -1) { |
| 77 | + this.queryColumns.push(_col); |
| 78 | + } else { |
| 79 | + NiceError.raise(gs.getMessage('Cannot find column {0}. Valid columns are {1}', [_col, this.columnNames.join(', ')])); |
| 80 | + } |
| 81 | + }, this); |
| 82 | + |
| 83 | + }, |
| 84 | + |
| 85 | + /** |
| 86 | + * build the list of column names for the table |
| 87 | + * use the default system view for the table and exclude all system and inherited fields |
| 88 | + * column order determines key lookup |
| 89 | + */ |
| 90 | + _getColumnNames: function () { |
| 91 | + return new global.GlideQuery('sys_ui_list_element') |
| 92 | + .where('list_id.name', this.tableName) |
| 93 | + .where('list_id.view', 'Default view') |
| 94 | + // ignore any system or inherited fields from the table |
| 95 | + .where('element', 'NOT IN', ['sys_class_name', 'sys_created_by', 'sys_created_on', 'sys_id', 'sys_mod_count', 'sys_name', 'sys_package', 'sys_policy', 'sys_scope', 'sys_updated_by', 'sys_updated_on', 'sys_update_name', 'active', 'order']) |
| 96 | + .whereNull('list_id.sys_user') // make sure we get the system view |
| 97 | + .orderBy('position') |
| 98 | + .select('element') |
| 99 | + .reduce(function (arr, e) { |
| 100 | + arr.push(e.element); |
| 101 | + return arr; |
| 102 | + }, []); |
| 103 | + }, |
| 104 | + |
| 105 | + type: 'DataLookupUtils' |
| 106 | +}; |
| 107 | + |
| 108 | +DataLookupUtils.SORT_BY_ORDER = 0; |
| 109 | +DataLookupUtils.SORT_BY_COLUMN = 1; |
0 commit comments