Name

sn_mab_api.ConfigKeyStackCreator

Description

No description available

Script

var ConfigKeyStackCreator = Class.create();
ConfigKeyStackCreator.prototype = {
  initialize: function () {
      this.configKeyFactory = new sn_mab_api.ConfigKeyFactory();
      this.validationHandler = new sn_mab_api.ValidationHandler();
      this.errorHandler = new sn_mab_api.ErrorHandler();
  },

  createStackFromNodes: function (branchNodeArray) {
      if (!branchNodeArray || !branchNodeArray.length) {
          new ErrorHandler().throwInternalError('Branch nodes must be provided');
      }

      var configKeyStack = [];
      branchNodeArray.forEach(function (node) {
          var stackEntry = this.createConfigKeyStackEntry(node);
          configKeyStack.push(stackEntry);
      }, this);

      return configKeyStack;
  },

  createConfigKeyStackEntry: function (node) {
      var currTableName = node.sys_class_name;
      this.validateExternalNodeTable(currTableName, 'External node with missing or non-existing table name: ' + currTableName);
      var currSysId = node.sys_id;
      this.validateExternalSysId(currSysId, 'External node with missing or non-existing SysId: ' + currSysId);
      var gr = this.createCurrentRecordGR(currTableName, currSysId);
      var generator = this.configKeyFactory.getGenerator(currTableName);
      var configMetadata = generator.getTreeConfiguration(gr, node);
      return new ConfigKeyStackEntry(configMetadata.getConfigKey(), currTableName, currSysId);
  },

  validateExternalNodeTable: function (tableName, err) {
      if (!tableName)
          this.errorHandler.throwBadRequestError(err);

      if (!this.validationHandler.isValidTable(tableName))
          this.errorHandler.throwNotFoundError(err);
  },

  validateExternalSysId: function (sysId, err) {
      if (!this.validationHandler.isGeneratedOrNormalSysId(sysId))
          this.errorHandler.throwBadRequestError(err);
  },

  createCurrentRecordGR: function (tableName, sysId) {
      var gr = new GlideRecord(tableName);
      if (this.isFakeId(sysId))
          gr.initialize();
      else if (!gr.get(sysId))
          new ErrorHandler().throwBadRequestError('No record found for table: ' + tableName + ' sysId: ' + sysId);

      return gr;
  },

  isFakeId: function (sysId) {
      return this.validationHandler.isGeneratedId(sysId);
  },

  type: 'ConfigKeyStackCreator'
};

Sys ID

058ed3e69c9b11eba8b30242ac130003

Offical Documentation

Official Docs: