Name

global.NotArchivedTableList

Description

Return a list of tables that have not been archived

Script


  gs.include("PrototypeServer");
  var NotArchivedTableList = Class.create();

  NotArchivedTableList.prototype = Object.extendsObject(AbstractAjaxProcessor, {

      process: function() {
          // This shouldn't be necessary but can't get the right function call when a "process" function exists in a class.
          // Return two lists of tables  one that is valid for API but not regular rules, and another that is valid for
          // regular rules but not API rules
          if (this.getName() == 'getExclusionLists') {
              this.currentTable = this.getParameter('sysparm_table');
              this.currentApiOnly = this.getParameter('sysparm_api_only') == 'true';

              var exclusionObject = {
                  api_only: this._getTableList('api_only_only'),
                  regular_only: this._getTableList('regular_only'),
              };

              return JSON.stringify(exclusionObject);
          }

          this.curentTable = current ? current.table : null;
          this.currentApiOnly = current ? current.api_only : null;
          this.tableList = this._getTableList('init');

          return this.tableList;
      },

      _intersect: function(a, b) {
          var t;
          if (b.length > a.length) t = b, b = a, a = t; // indexOf to loop over shorter
          return a.filter(function (e) {
              return b.indexOf(e) > -1;
          });
      },

      // returns array of table names that are valid for for new sys_archive rules, or for 'api_only_only'/'regular_only',
      // the delta between the two groups, (i.e. `api_only_only = api_only - regular``)
      _getTableList: function(mode) {
          var currentAppScope = gs.getCurrentApplicationId();

          var dd = new GlideRecordSecure('sys_dictionary');
          dd.addNullQuery('element');
          dd.addQuery('name', 'NOT MATCHES', 'ar\\_%');
          dd.addQuery('name', 'NOT MATCHES', 'ts\\_%');

          switch (mode) {
              case 'init':
                  var regList = this._getRegularExclusions()
                  var apiList = this._getApiExclusions()
                  dd.addQuery('name', 'NOT IN', this._intersect(regList, apiList));
                  break;
              case 'api_only_only':
                  dd.addQuery('name', 'IN', this._getRegularExclusions());
                  dd.addQuery('name', 'NOT IN', this._getApiExclusions());
                  break;
              case 'regular_only':
                  dd.addQuery('name', 'NOT IN', this._getRegularExclusions());
                  dd.addQuery('name', 'IN', this._getApiExclusions());
                  break;
          }

          var qc = dd.addQuery('attributes', 'DOES NOT CONTAIN', 'update_synch=true');
          qc.addOrCondition('attributes', '=', '');
          dd.orderBy('name');
          dd.query();

          var tableList = new Array();
          while (dd.next()) {
              var scope = dd.getValue('sys_scope');
              //only add tables belongs to the given application scope to the table list
              if (scope == currentAppScope) {
                  var tableName = dd.name + '';
                  if (SncTableRotationExtensions.isRotationExtension(tableName) || SncTableRotationExtensions.isRotated(tableName))
                      continue;

                  tableList.push(tableName);
              }
          }

          return tableList;
      },

      _getApiExclusions: function() {
          return this._getExclusionList(true);
      },

      _getRegularExclusions: function() {
          return this._getExclusionList(false);
      },

      // returns array of table names that should not be allowed for new sys_archive rule creation
      _getExclusionList: function(isApi) {
          var list = new Array();
          var ar = new GlideRecordSecure('sys_archive');
          var qc = ar.addQuery('api_only', isApi);
          if (!isApi) {
            qc.addOrCondition('api_only', null)
          }

          // if we are editing a pre-existing rule, don't restrict the user's choice based on their current table selected
          if (this.currentTable && this.currentApiOnly == isApi) {
              ar.addQuery('table', '!=', this.currentTable);
          }

          ar.query();

          while (ar.next())
              list.push(ar.table + '');

          // two regular rules are not allowed to be created in the same heirarchy
          if (!isApi) {
              list = this._deduplicatedHierarchy(list);
          }

          return list;
      },

      _deduplicatedHierarchy: function(archivedTables) {
          var answer = [];
          var collectionOfHierarchies = {};

          for(var idx in archivedTables) {
              var tableHierarchy = GlideDBObjectManager.get().getHierarchy(archivedTables[idx]);
              for (var x = 0; x < tableHierarchy.size(); ++x) {
                  collectionOfHierarchies[tableHierarchy.get(x)] = tableHierarchy.get(x);
              }
          }

          for (var y in collectionOfHierarchies)
              answer.push(y);

          return answer;
      },

      type: "NotArchiveTableList"
  });

Sys ID

ae5711109f631000dada207c7f4bcc53

Offical Documentation

Official Docs: