Name
global.AgentGlobalUtils
Description
Functions to help facilitate cross scope transactions
Script
var AgentGlobalUtils = Class.create();
AgentGlobalUtils.prototype = {
initialize: function() {
},
// viewObj for each of the funtions will be in the following form:
//
// {
// "name": <name>
// "description": <description>
// "label": <label>
// "view_tables": [
// {
// "table": <table_name>,
// "order": 100,
// "var_prefix": <table_1_prefix>
// "where_clause": <table_1_clause>
// },
// {
// "table": <table_name>,
// "order": 100,
// "var_prefix", <table_2_prefix>,
// "where_clause", <table_2_clause>
// },
// ...
// ]
// }
//
// Called by publish/republish UI actions. Checks if DB view and DB view tables exists and has correct fields,
// and if not, deletes any current view and view tables and creates a new view and view tabes with the proper fields
ensureDBView: function(viewObj) {
if (this.validateDBView(viewObj))
return true;
this.deleteDBView(viewObj);
return this.createDBView(viewObj);
},
// Returns DB view sys id on success, or undefined if an error occurred
createDBView: function(viewObj) {
if (!viewObj) {
gs.warn("AgentGlobalUtils.generateDBView(): input null, cannot create DB View");
return;
}
var viewGR = new GlideRecord('sys_db_view');
viewGR.setValue('name', viewObj.name);
viewGR.setValue('description', viewObj.description);
viewGR.setValue('label', viewObj.label);
var viewID = viewGR.insert();
if (!viewID) {
gs.warn("AgentGlobalUtils.generateDBView(): failed to create DB view, please check input data");
return;
}
for (var i = 0; i < viewObj.view_tables.length; i++) {
if (!this._createDBViewTable(viewObj.view_tables[i], viewID)) {
gs.warn("AgentGlobalUtils.generateDBView(): Failed to create DB View Table, cannot continue.");
return;
}
}
return viewID;
},
// DB View is validated by checking that DB View Tables exist with the correct fields
validateDBView: function(viewObj) {
var viewGR = new GlideRecord('sys_db_view');
viewGR.addQuery('name', viewObj.name);
viewGR.query();
if (!viewGR.next()) {
gs.debug("AgentGlobalUtils.validateDBView(): Cannot find DB View with name " + viewObj.name);
return false;
}
// validation fails if any of the view tables fail validation
for (var i = 0; i < viewObj.view_tables.length; i++) {
if (!this._validateDBViewTable(viewObj.view_tables[i], viewGR.getUniqueValue()))
return false;
}
return true;
},
deleteDBView: function(viewObj) {
var viewGR = new GlideRecord('sys_db_view');
viewGR.addQuery('name', viewObj.name);
viewGR.query();
return viewGR.deleteRecord(); // DB view tables are cascade deleted from this
},
_createDBViewTable: function(viewTableObj, viewID) {
if (!viewTableObj) {
gs.warn("AgentGlobalUtils.generateDBViewTable(): input null, cannot create DB View Table");
return;
}
var viewTableGR = new GlideRecord('sys_db_view_table');
viewTableGR.setValue('table', viewTableObj.table);
viewTableGR.setValue('order', viewTableObj.order);
viewTableGR.setValue('variable_prefix', viewTableObj.var_prefix);
viewTableGR.setValue('where_clause', viewTableObj.where_clause);
viewTableGR.setValue('view', viewID);
return viewTableGR.insert();
},
_validateDBViewTable: function(viewTableObj, viewID) {
var viewTableGR = new GlideRecord('sys_db_view_table');
viewTableGR.addQuery('view', viewID);
viewTableGR.addQuery('table', viewTableObj.table);
viewTableGR.addQuery('order', viewTableObj.order);
viewTableGR.addQuery('variable_prefix', viewTableObj.var_prefix);
viewTableGR.addQuery('where_clause', viewTableObj.where_clause);
viewTableGR.query();
return viewTableGR.hasNext();
},
// This function compares the tableName and its parents against first the supportedCISubTypes and then supportedCIBaseTypes
// arrays until it finds a match, or until the cmdb_ci base type is found, in which case there is no match as that is the
// parent of the supported base types
getBaseTable: function(tableName, supportedCIBaseTypes) {
var dbUtil = GlideDBObjectManager.get();
while (tableName.startsWith('cmdb_ci') && tableName != 'cmdb_ci' && gs.tableExists(tableName)) {
if (supportedCIBaseTypes.indexOf("" + tableName) != -1)
return tableName;
tableName = dbUtil.getBase(tableName);
}
return;
},
type: 'AgentGlobalUtils'
};
Sys ID
b5d1c947ffba11102abec787d53bf18f