Name

sn_cmdb_int_util.DataDictionaryIRESync

Description

Keeps the data dictionary and the IRE rules in sync

Script

var DataDictionaryIRESync = Class.create();
DataDictionaryIRESync.prototype = {
  initialize: function() {},

  syncAll: function() {
      this.syncAllFields();
      this.syncAllLookups();
      this.syncAllRelatedEntries();
      this.syncAllRelationships();
  },

  syncAllFields: function() {
      var dataDictionaryFieldsGr = new GlideRecord("sn_cmdb_int_util_cmdb_integration_field");
      dataDictionaryFieldsGr.query();
      while (dataDictionaryFieldsGr.next()) {
          this.syncField(dataDictionaryFieldsGr);
          dataDictionaryFieldsGr.update();
      }
  },

  syncAllLookups: function() {
      var dataDictionaryLookupsGr = new GlideRecord("sn_cmdb_int_util_cmdb_integration_lookup");
      dataDictionaryLookupsGr.query();
      while (dataDictionaryLookupsGr.next()) {
          this.syncLookup(dataDictionaryLookupsGr);
          dataDictionaryLookupsGr.update();
      }
  },

  syncAllRelationships: function() {
      var dataDictionaryRelationshipsGr = new GlideRecord("sn_cmdb_int_util_cmdb_integration_relationship");
      dataDictionaryRelationshipsGr.query();
      while (dataDictionaryRelationshipsGr.next()) {
          this.syncRelationship(dataDictionaryRelationshipsGr);
          dataDictionaryRelationshipsGr.update();
      }
  },

  syncAllRelatedEntries: function() {
      var dataDictionaryRelatedEntriesGr = new GlideRecord("sn_cmdb_int_util_cmdb_integration_related_entry");
      dataDictionaryRelatedEntriesGr.query();
      while (dataDictionaryRelatedEntriesGr.next()) {
          this.syncRelatedEntry(dataDictionaryRelatedEntriesGr);
          dataDictionaryRelatedEntriesGr.update();
      }
  },

  syncField: function(dataDictionaryFieldsGr) {
      var className = dataDictionaryFieldsGr.cmdb_class.cmdb_class;
      var gr = new GlideRecord("cmdb_identifier_entry");
      gr.addQuery("active", true);
      gr.addQuery("identifier.applies_to", className);
      gr.addQuery("table", className);
      gr.query();
      while (!gr.hasNext()) {
          className = this._getParentClass(className);
          if (!className)
              break;

          gr = new GlideRecord("cmdb_identifier_entry");
          gr.addQuery("active", true);
          gr.addQuery("identifier.applies_to", className);
          gr.addQuery("table", className);
          gr.query();
      }

      var ireReferences = [];
      while (gr.next()) {
          var foundMatch = gr.attributes.split(",").indexOf(dataDictionaryFieldsGr.field.element + "") >= 0;
          if (foundMatch)
              ireReferences.push(gr.getUniqueValue());
      }
      ireReferences = ireReferences.join(",");
      dataDictionaryFieldsGr.cmdb_identifier_entrys = ireReferences;
  },

  syncLookup: function(dataDictionaryLookupsGr) {
      var className = dataDictionaryLookupsGr.cmdb_class.cmdb_class;
      var lookupClassName = dataDictionaryLookupsGr.lookup_for_class.cmdb_class;
      if (className == lookupClassName)
          return;
      var gr = new GlideRecord("cmdb_identifier_entry");
      gr.addQuery("active", true);
      gr.addQuery("table", className);
      gr.addQuery("identifier.applies_to", lookupClassName);
      gr.query();
      while (!gr.hasNext()) {
          lookupClassName = this._getParentClass(lookupClassName);
          if (!lookupClassName)
              break;
          gr = new GlideRecord("cmdb_identifier_entry");
          gr.addQuery("active", true);
          gr.addQuery("table", className);
          gr.addQuery("identifier.applies_to", lookupClassName);
          gr.query();
      }

      var ireReferences = [];
      while (gr.next())
          ireReferences.push(gr.getUniqueValue());

      dataDictionaryLookupsGr.cmdb_identifier_entrys = ireReferences.join(",");
  },

  syncRelatedEntry: function(dataDictionaryRelatedEntryGr) {
      var className = dataDictionaryRelatedEntryGr.cmdb_class.cmdb_class;
      var relatedClassName = dataDictionaryRelatedEntryGr.related_class.cmdb_class;
      if (className == relatedClassName)
          return;
      var gr = new GlideRecord("cmdb_related_entry");
      gr.addQuery("active", true);
      gr.addQuery("identifier.applies_to", className);
      gr.addQuery("table", dataDictionaryRelatedEntryGr);
      gr.query();
      while (!gr.hasNext()) {
          className = this._getParentClass(className);
          if (!className)
              break;
          gr = new GlideRecord("cmdb_related_entry");
          gr.addQuery("active", true);
          gr.addQuery("identifier.applies_to", className);
          gr.addQuery("table", relatedClassName);
          gr.query();
      }

      var relatedEntries = [];
      while (gr.next())
          relatedEntries.push(gr.getUniqueValue());

      dataDictionaryRelatedEntryGr.cmdb_related_entrys = relatedEntries.join(",");
  },

  syncRelationship: function(dataDictionaryRelationshipsGr) {
      var parentClass = dataDictionaryRelationshipsGr.parent_class.cmdb_class;
      var childClass = dataDictionaryRelationshipsGr.child_class.cmdb_class;
      var relationshipType = dataDictionaryRelationshipsGr.relationship_type;
      var ireReferences = [];
      var tempParentClass = parentClass;
      while (ireReferences.length == 0 && tempParentClass) {
          this._getDependentRelationships(dataDictionaryRelationshipsGr, tempParentClass, childClass, relationshipType, false, ireReferences);
          this._getDependentRelationships(dataDictionaryRelationshipsGr, tempParentClass, childClass, relationshipType, true, ireReferences);
          tempParentClass = this._getParentClass(tempParentClass);
      }
      var inverseIreReferencesLength = ireReferences.length;
      var tempChildClass = childClass;

      // in the IRE dependent relationships are only inherited if there is no dependent relationship on the child class itself
      // the ire can only use one dependent relationship type at a time
      while (ireReferences.length == 0 && tempChildClass) {
          this._getDependentRelationships(dataDictionaryRelationshipsGr, parentClass, tempChildClass, relationshipType, false, ireReferences);
          this._getDependentRelationships(dataDictionaryRelationshipsGr, parentClass, tempChildClass, relationshipType, true, ireReferences);
          tempChildClass = this._getParentClass(tempChildClass);
      }

      dataDictionaryRelationshipsGr.cmdb_metadata_hosting = ireReferences.join(",");
  },

  _getParentClass: function(className) {
      var tableGr = new GlideRecord("sys_db_object");
      tableGr.addQuery("name", className);
      tableGr.query();
      if (!tableGr.hasNext())
          return "";
      tableGr.next();
      return tableGr.super_class.name;
  },

  _getDependentRelationships: function(dataDictionaryRelationshipsGr, parentClass, childClass, relationshipType, isReverse, ireReferences) {
      var hostingGr = new GlideRecord("cmdb_metadata_hosting");
      hostingGr.addQuery("parent_type", isReverse ? childClass : parentClass);
      hostingGr.addQuery("child_type", isReverse ? parentClass : childClass);
      hostingGr.addQuery("is_reverse", isReverse);
      hostingGr.addQuery("rel_type", relationshipType);
      hostingGr.query();
      while (hostingGr.next())
          ireReferences.push(hostingGr.getUniqueValue());
  },

  type: 'DataDictionaryIRESync'
};

Sys ID

10c93d457360101032f927b751ab9ef7

Offical Documentation

Official Docs: