Name

sn_mab_api.TreeConfigurationNode

Description

No description available

Script

var TreeConfigurationNode = Class.create();

TreeConfigurationNode.prototype = {
  /**
   * @param gr {GlideRecord}
   */
  initialize: function (gr) {
      this.relationships = [];
      this.references = [];
      this.configKey = '__EMPTY_CONFIGMETADATA';
      this.compatible = null;

      //Create empty Configuration Node
      if (!gr)
          return;

      this.configKey = gr.getValue('config_key');
      this.compatible = gr.getValue('compatible');
      if (!this.configKey)
          new ErrorHandler().throwBadConfigError('Invalid configKey retrieved for TreeConfigurationNode');

      if (gr.getValue('relationship_config'))
          this.relationships = this._createRelationships(this._toObject(gr.getValue('relationship_config')));

      if (gr.getValue('reference_config'))
          this.references = this._createReferences(this._toObject(gr.getValue('reference_config')));
  },

  /**
   * Returns the configKey
   * @return {String}
   */
  getConfigKey: function() {
      return this.configKey;
  },

  /**
   * Returns a map that meets json spec for tree configs
   * @return {*|{TreeConfigurationNode}}
   */
  getFormattedNode: function() {
      var node = {};

      node[this.configKey] = {
          relationships: this.relationships,
          references: this.references
      };

      return node;
  },

  _createRelationships: function(relationshipJSON) {
      var relationships = [];
      if (relationshipJSON.length > 0) {
          relationshipJSON.forEach(function(relationship) {
              relationships.push(new Relationship(relationship));
          });
      }
      return relationships;
  },

  _createReferences: function(referenceJSON) {
      var references = [];
      if (referenceJSON.length > 0) {
          referenceJSON.forEach(function(reference) {
              references.push(new Reference(reference));
          });
      }
      return references;
  },

  _toObject: function(json) {
      try {
          return JSON.parse(json);
      } catch (e) {
          gs.error(gs.getMessage('Unable to parse Json from TreeConfigurationNode for configKey: {0}', this.configKey));
          return null;

      }
  },

  type: 'TreeConfigurationNode',
};

function Relationship(relationshipJSON) {
  if (!relationshipJSON.remoteTableName)
      new ErrorHandler().throwBadConfigError('Relationship not defined correctly, no remoteTableName');

  if (!relationshipJSON.remoteRefFieldName)
      new ErrorHandler().throwBadConfigError('Relationship not defined correctly, no remoteRefFieldName');

  this.remoteTableName = relationshipJSON.remoteTableName;
  this.remoteRefFieldName = relationshipJSON.remoteRefFieldName;
  if (relationshipJSON.localRefFieldName)
      this.localRefFieldName = relationshipJSON.localRefFieldName;

  if (relationshipJSON.conditional)
      this.conditional = relationshipJSON.conditional;
}

function Reference(referenceJSON) {
  if (!referenceJSON.name)
      new ErrorHandler().throwBadConfigError('ReferenceField not defined correctly, no field name');

  this.name = referenceJSON.name;

  if (referenceJSON.conditional)
      this.conditional = referenceJSON.conditional;
}

Sys ID

2a7aeab4b7d220108223e126de11a9e1

Offical Documentation

Official Docs: