Name
sn_service_builder.ASBUtils
Description
No description available
Script
var ASBUtils = Class.create();
ASBUtils.prototype = {
initialize: function() {
this.CONSTANTS = new sn_service_builder.ASBConstants();
},
/**
* copyFields
* copy the fields between gliderecords
*
* @param {GlideRecord} sourceGr - the GlideRecord to copy the fields from
* @param {GlideRecord} destinationGr - the GlideRecord to copy the fields into
* @param {array} excludedFields - an array of fields not to be copied
*/
copyFields: function(sourceGr, destinationGr, excludedFields) {
for (var field in sourceGr) {
if (!excludedFields || excludedFields.indexOf(field) === -1)
destinationGr.setValue(field, sourceGr.getValue(field));
}
},
/**
* deleteReferences
* deletes records in a mtom table if sysId is in referenceField
*
* @param {string} tableName - the GlideRecord to copy the fields from
* @param {string} refrenceField - the field that contains the reference to the item
* @param {sysId} sysId - the id of the record
* @param {string} encodedQuery - adds encoded query to reduce result for certain tables
*/
deleteReferences: function(tableName, referenceField, sysId, encodedQuery) {
//delete all of the records with a reference to the sysId
var gr = new GlideRecord(tableName);
gr.addQuery(referenceField, sysId);
if (encodedQuery)
gr.addEncodedQuery(encodedQuery);
gr.query();
while (gr.next()) {
if (!gr.deleteRecord())
throw 'Could not update ' + tableName + ' reference for ' + sysId;
}
},
/**
* setCheckoutState
* updates the state and checkout fields
*
* @param {GlideRecord} gr - the GlideRecord to update
* @param {string} checkout - the value of the checkout field (optional)
* @param {string} state - the value of the state field (optional)
* @return {boolean} true if values updated successfully
*/
setCheckoutState: function(gr, checkout, state) {
if (!gr || !gr.isValidRecord())
return false;
if (checkout)
gr.setValue(this.CONSTANTS.FIELD.CHECKOUT, checkout);
if (state)
gr.setValue(this.CONSTANTS.FIELD.STATE, state);
if (gr.update() == null) {
gs.error('[ASB] Error while updating checkout or state: ' + gr.name);
return false;
}
return true;
},
/**
* urlToBuilder
* get URl for service builder wizard, either to create a brand new service or edit an
* existing service if a sysId is used
* @param {string} table - the table to open in the wizard
* @param {string} sysId - not required, if added will open that item in the wizard,
* @return {string} - url to wizard
*/
urlToBuilder: function(table, sysId) {
var url = this.CONSTANTS.WIZARD.PREFIX;
var checkoutServiceUtil = new sn_service_builder.ASBCheckoutService();
if (!sysId || !typeof sysId === 'string' || !table || !typeof table === 'string')
gs.error('[ASB] Cannot find item in Service Builder');
switch (table) {
case this.CONSTANTS.TABLE.CMDB_CI_SERVICE_BUSINESS:
url += this.CONSTANTS.WIZARD.CMDB_CI_SERVICE_BUSINESS;
break;
case this.CONSTANTS.TABLE.CMDB_CI_SERVICE_TECHNICAL:
url += this.CONSTANTS.WIZARD.CMDB_CI_SERVICE_TECHNICAL;
break;
case this.CONSTANTS.TABLE.SERVICE_OFFERING:
url += this.CONSTANTS.WIZARD.SERVICE_OFFERING;
url += "/params/record-id/";
var draftOfferingSysId = null;
var offering = new GlideRecord(this.CONSTANTS.TABLE.SERVICE_OFFERING);
if (offering.get(sysId)) {
// if there is an existing draft offering, return the sys_id
var existingDraftOffering = this.getDraftRecord(offering);
if (existingDraftOffering && existingDraftOffering.isValidRecord())
return url += existingDraftOffering.getUniqueValue();
// check out the parent service if it has not been checked out yet
if (offering.getValue('parent')) {
var draftService = checkoutServiceUtil.checkoutService(offering.parent.sys_class_name, offering.getValue('parent'));
if (draftService && draftService.sysId)
// only check out the offering that triggered the checkout
draftOfferingSysId = checkoutServiceUtil.checkoutOffering(offering, draftService.sysId);
}
}
if (draftOfferingSysId)
return url += draftOfferingSysId;
return null;
default:
gs.error('[ASB] Item is not supported in ASB');
return null;
}
if (sysId) {
var checkoutService = checkoutServiceUtil.checkoutService(table, sysId);
if (checkoutService.sysId)
url += "/params/record-id/" + checkoutService.sysId;
else
return null;
}
return url;
},
/**
* draftRecordExists
* Checks whether the draft record for a particular offering exists
* @param {string} current - reference for current record
* @return {boolean}
*/
draftRecordExists: function(current) {
var gr = new GlideRecord(this.CONSTANTS.TABLE.OFFERING);
gr.addQuery(this.CONSTANTS.FIELD.PUBLISHED_REF, current.sys_id);
gr.query();
return gr.hasNext();
},
/**
* getDraftRecord
* retrieve draft version of the passed in record, if it exists
* @param {string} gr
* @returns {GlideRecord | null}
*/
getDraftRecord: function(gr) {
if (!gr || !gr.isValidRecord())
return null;
var supportedTables = [
this.CONSTANTS.TABLE.OFFERING,
this.CONSTANTS.TABLE.CMDB_CI_SERVICE_BUSINESS,
this.CONSTANTS.TABLE.CMDB_CI_SERVICE_TECHNICAL
];
var grTableName = gr.getValue(this.CONSTANTS.FIELD.SYS_CLASS_NAME);
if (supportedTables.indexOf(grTableName) < 0)
return null;
if (gr.getValue(this.CONSTANTS.FIELD.STATE) === this.CONSTANTS.FIELD_VALUE.DRAFT)
return gr;
var draftRecord = new GlideRecord(grTableName);
if (draftRecord.get(this.CONSTANTS.FIELD.PUBLISHED_REF, gr.getUniqueValue()))
return draftRecord;
return null;
},
/**
* Returns the GlideRecord object of a Service Portfolio node without a published item, signifying it is the published version
* @param {string} nodeId - Sys id of a SP Node
* @returns {GlideRecord} - GlideRecord of the published SP Node
*/
getPublishedNode: function(nodeId) {
var nodeGr = this.getNodeOfAnyType(nodeId);
if (!nodeGr.isValid()) {
var errorMessage = gs.getMessage('The following node is invalid: {0}', [nodeId]);
return gs.error(errorMessage);
}
var table = nodeGr.getTableName();
var publishedGr = new GlideRecord(table);
if (this.isServiceTable(table) || table === this.CONSTANTS.TABLE.OFFERING) {
if (!nodeGr.getValue(this.CONSTANTS.FIELD.PUBLISHED_REF)) {
publishedGr = nodeGr;
} else {
publishedGr.get(nodeGr.getValue(this.CONSTANTS.FIELD.PUBLISHED_REF));
}
return publishedGr;
} else {
return nodeGr;
}
},
/**
* Given an SPM object (offering/service/taxonomy_node/portfolio) id, returns the GR
* @param {string} nodeId SP Node sys_id
* @return {object} Gliderecord
*/
getNodeOfAnyType: function(nodeId) {
var nodeGr;
nodeGr = new GlideRecord(this.CONSTANTS.TABLE.SERVICE_OFFERING);
nodeGr.query(this.CONSTANTS.FIELD.SYS_ID, nodeId);
if (nodeGr.next())
return nodeGr;
nodeGr = new GlideRecord(this.CONSTANTS.TABLE.CMDB_CI_SERVICE);
nodeGr.query(this.CONSTANTS.FIELD.SYS_ID, nodeId);
if (nodeGr.next())
return nodeGr;
nodeGr = new GlideRecord(this.CONSTANTS.TABLE.SPM_TAXONOMY_NODE);
nodeGr.query(this.CONSTANTS.FIELD.SYS_ID, nodeId);
if (nodeGr.next())
return nodeGr;
nodeGr = new GlideRecord(this.CONSTANTS.TABLE.SPM_SERVICE_PORTFOLIO);
nodeGr.query(this.CONSTANTS.FIELD.SYS_ID, nodeId);
if (nodeGr.next())
return nodeGr;
},
/**
* Checks a table name against the list of tables for a service
* @param {string} table table name
* @return {Boolean} true or false if its a match
*/
isServiceTable: function(table) {
return (table == this.CONSTANTS.TABLE.CMDB_CI_SERVICE || table == this.CONSTANTS.TABLE.CMDB_CI_SERVICE_BUSINESS || table == this.CONSTANTS.TABLE.CMDB_CI_SERVICE_TECHNICAL);
},
/**
* editOfferingCondition
* Returns condition that displays edit offering button if:
* 1. User has authorization
* 2. Record (parent) is a technical or business service.
* 3. Hide when draft record does not exist and checkout is true.
* @param {string} current - reference for current record
* @return {boolean}
*/
editOfferingCondition: function(current) {
var draftRecordExists = this.draftRecordExists(current);
var utils = new global.SPMUtilsFactory().getUtils();
var technicalService = this.CONSTANTS.TABLE.CMDB_CI_SERVICE_TECHNICAL;
var businessService = this.CONSTANTS.TABLE.CMDB_CI_SERVICE_BUSINESS;
return utils.checkOfferingAuthorization(current) && (current.parent.sys_class_name == technicalService || current.parent.sys_class_name == businessService) && !(!draftRecordExists && current.checkout == 'true');
},
/**
* Returns an object for ServiceBuilderUtilAjax to populate relationships in Service Offering CRP
* @param {GlideRecord} gr GlideRecord of cmdb_rel_ci
* @param {string} nodeId sys_id of parent record
* @param {string} relationshipId sys_id of cmdb_rel_type
* @returns {object} Object containing information needed for variable in CRP
*/
buildRelationshipResponseObject: function(gr, nodeId, relationshipId) {
return {
sys_id: gr.getValue(this.CONSTANTS.FIELD.SYS_ID),
parent_id: nodeId,
child_id: gr.getValue(this.CONSTANTS.FIELD.CHILD),
type_id: relationshipId
};
},
/**
* Returns a list of offering sys_id's for which the input node has a parent-child relationship
* @param {string} nodeId SP Node sys_id
* @return {array} List of offering sys_ids which the input node has a parent-child relationship with
*/
getOfferingsNodeDependsOn: function(nodeId) {
var offeringList = [];
var relationshipGr = new GlideRecord(this.CONSTANTS.TABLE.CMDB_REL_CI);
relationshipGr.addQuery(this.CONSTANTS.FIELD.PARENT + '.' + this.CONSTANTS.FIELD.SYS_ID, nodeId);
relationshipGr.addQuery(this.CONSTANTS.FIELD.CHILD + '.' + this.CONSTANTS.FIELD.CLASS, this.CONSTANTS.TABLE.SERVICE_OFFERING);
relationshipGr.addQuery(this.CONSTANTS.FIELD.TYPE, this.CONSTANTS.FIELD_VALUE.DEPENDS_ON_USED_BY);
relationshipGr.query();
while (relationshipGr.next()) {
offeringList.push(this.buildRelationshipResponseObject(relationshipGr, nodeId, this.CONSTANTS.FIELD_VALUE.DEPENDS_ON_USED_BY));
}
return offeringList;
},
/**
* Returns a list of application service sys_id's for which the input node has a parent-child relationship
* @param {string} nodeId SP Node sys_id
* @return {array} List of application service sys_ids which the input node has a parent-child relationship with
*/
getServicesNodeDependsOn: function(nodeId) {
var servicesList = [];
var relationshipGr = new GlideRecord(this.CONSTANTS.TABLE.CMDB_REL_CI);
relationshipGr.addQuery(this.CONSTANTS.FIELD.PARENT + '.' + this.CONSTANTS.FIELD.SYS_ID, nodeId);
relationshipGr.addQuery(this.CONSTANTS.FIELD.CHILD + '.' + this.CONSTANTS.FIELD.CLASS, '!=', this.CONSTANTS.TABLE.SERVICE_OFFERING);
relationshipGr.addQuery(this.CONSTANTS.FIELD.TYPE, this.CONSTANTS.FIELD_VALUE.DEPENDS_ON_USED_BY);
relationshipGr.query();
while (relationshipGr.next()) {
var childId = relationshipGr.getValue(this.CONSTANTS.FIELD.CHILD);
var appServiceGr = new GlideRecord(this.CONSTANTS.TABLE.CMDB_CI_SERVICE_AUTO); // Need to query cmdb_ci_service_auto table specifically since not all application services have a sys_class_name of cmdb_ci_service_auto
if (appServiceGr.get(childId)) {
servicesList.push(this.buildRelationshipResponseObject(relationshipGr, nodeId, this.CONSTANTS.FIELD_VALUE.DEPENDS_ON_USED_BY));
}
}
return servicesList;
},
/**
* Returns a list of application service sys_id's for which the input node has a parent-child relationship
* @param {string} nodeId SP Node sys_id
* @return {array} List of application service sys_ids which the input node has a parent-child relationship with
*/
getServicesNodeContains: function(nodeId) {
var servicesList = [];
var relationshipGr = new GlideRecord(this.CONSTANTS.TABLE.CMDB_REL_CI);
relationshipGr.addQuery(this.CONSTANTS.FIELD.PARENT + '.' + this.CONSTANTS.FIELD.SYS_ID, nodeId);
relationshipGr.addQuery(this.CONSTANTS.FIELD.CHILD + '.' + this.CONSTANTS.FIELD.CLASS, '!=', this.CONSTANTS.TABLE.SERVICE_OFFERING);
relationshipGr.addQuery(this.CONSTANTS.FIELD.TYPE, this.CONSTANTS.FIELD_VALUE.CONTAINS_CONTAINED_BY);
relationshipGr.query();
while (relationshipGr.next()) {
var childId = relationshipGr.getValue(this.CONSTANTS.FIELD.CHILD);
var appServiceGr = new GlideRecord(this.CONSTANTS.TABLE.CMDB_CI_SERVICE_AUTO); // Need to query cmdb_ci_service_auto table specifically since not all application services have a sys_class_name of cmdb_ci_service_auto
if (appServiceGr.get(childId)) {
servicesList.push(this.buildRelationshipResponseObject(relationshipGr, nodeId, this.CONSTANTS.FIELD_VALUE.CONTAINS_CONTAINED_BY));
}
}
return servicesList;
},
/**
* Returns class name of the service
* @param {string} sys_id of the service
* @return {string} class as a string
*/
getServiceClass: function(serviceId) {
var serviceGr = new GlideRecord(this.CONSTANTS.TABLE.CMDB_CI_SERVICE);
if (!serviceGr.get(serviceId))
return;
var serviceClass = serviceGr.getValue('sys_class_name');
return serviceClass;
},
type: 'ASBUtils'
};
Sys ID
52bb91750703201070e493d0fad30091