Name

sn_agent.PolicyStateUtil

Description

No description available

Script

var PolicyStateUtil = Class.create();
PolicyStateUtil.prototype = {
  initialize: function() {
      // Maps a policy's sys_id to a GlideRecord pointing to the policy's corresponding policy state record.
      //
      // If a policy state record is updated using one of the functions in this Script Include, then
      // the policy state record cache entry does not need to be invalidated, since the functions in
      // this Script Include get the GlideRecord from the cache.
      //
      // If a policy state record is modified without getting the policy state record from the cache,
      // the modifier will need to also call the invalidateCacheEntry function for the policy.
      this.policyStateRecordCache = {};
      this.domainInfo = new DomainInfo();
  },

  /**
   * Gets the policy state record for the passed the policy record and
   * returns the last_calc_time of the policy state record.
   * 
   * Parameters:
   *     policyGr - a GlideRecord pointing to an sn_agent_policy record
   * 
   * Return:
   *     the last_calc_time of the policy's corresponding policy state record
   */
  getPolicyStateLastCalcTime: function(policyGr) {
      var policyStateGr = this.getPolicyStateRecord(policyGr);
      return policyStateGr.getValue('last_calc_time');
  },

  /**
   * Gets the policy state record for the passed the policy record and
   * returns the last_full_calc_time of the policy state record.
   * 
   * Parameters:
   *     policyGr - a GlideRecord pointing to an sn_agent_policy record
   * 
   * Return:
   *     the last_full_calc_time of the policy's corresponding policy state record
   */
  getPolicyStateLastFullCalcTime: function(policyGr) {
      var policyStateGr = this.getPolicyStateRecord(policyGr);
      return policyStateGr.getValue('last_full_calc_time');
  },

  /**
   * Gets the policy state record for the passed the policy record and
   * returns the publish_status of the policy state record.
   * 
   * Parameters:
   *     policyGr - a GlideRecord pointing to an sn_agent_policy record
   * 
   * Return:
   *     the publish_status of the policy's corresponding policy state record
   */
  getPolicyStatePublishStatus: function(policyGr) {
      var policyStateGr = this.getPolicyStateRecord(policyGr);
      return policyStateGr.getValue('publish_status');
  },

  /**
   * Gets the policy state record for the passed policy record and sets the publish_status
   * on the policy state record.
   * 
   * Parameters:
   *     policyGr - a GlideRecord pointing to a record in sn_agent_policy
   *     newStatus - the new publish status
   */
  updatePolicyStatePublishStatus: function(policyGr, newStatus) {
      var policyStateGr = this.getPolicyStateRecord(policyGr);
      policyStateGr.setValue('publish_status', newStatus);
      policyStateGr.update();
  },

  /**
   * Synchronizes the publish_status between the policy record and its policy state record.
   * Called from policy activation and publish code.
   * If domain separation is active, policy activation and publish can happen on a non-leaf
   * domain, so this function needs update all policy state records for this policy across
   * the leaf domains.
   * 
   * Since this function performs a batch update, the cache is invalidated.
   * 
   * Parameters:
   *     policyGr - a GlideRecord pointing to a record in sn_agent_policy
   */
  synchronizePolicyStatePublishStatus: function(policyGr) {
      var policySysId = policyGr.getUniqueValue();
      var publishStatus = policyGr.getValue('publish_status');
      var policyStateGr = new GlideRecord('sn_agent_policy_state');
      policyStateGr.addQuery('policy', policySysId);
      policyStateGr.setValue('publish_status', publishStatus);
      policyStateGr.updateMultiple();
      this.invalidateCache();
  },

  /**
   * Gets the corresponding policy state record for the policy record.
   * If one does not exist, create it by copying the values from the policy record into the new policy state record.
   * This function should be updated if the sn_agent_policy_state schema changes.
   * 
   * Parameters:
   *     policyGr - a GlideRecord pointing to a record in sn_agent_policy
   * 
   * Return:
   *     a GlideRecord pointing to the sn_agent_policy_state record
   */
  getPolicyStateRecord: function(policyGr) {
      // return the policy state record for this policy if it already exists
      var policyStateGr = this.getExistingPolicyStateRecord(policyGr);
      if (policyStateGr)
          return policyStateGr;

      // the policy state record for this policy does not exist, so create the policy state record by
      // copying the values from the policy record
      var policySysId = policyGr.getUniqueValue();
      var publishStatus = policyGr.getValue('publish_status');
      var publishError = policyGr.getValue('publish_error');
      policyStateGr = this.createPolicyStateRecord(policySysId, publishStatus, publishError);
      if (policyStateGr)
          return policyStateGr;

      gs.warn('PolicyStateUtil getPolicyStateRecord: could not create policy state record for policy ' + policySysId);
  },

  /**
   * Gets the corresponding policy state record for the policy record if it exists.
   * 
   * Parameters:
   *     policyGr - a GlideRecord pointing to a record in sn_agent_policy
   * 
   * Return:
   *     a GlideRecord pointing to the policy's corresponding policy state record (sn_agent_policy_state) if it exists.
   *     undefined otherwise.
   */
  getExistingPolicyStateRecord: function(policyGr) {
      var policySysId = policyGr.getUniqueValue();

      // check cache
      var policyStateGr = this.getPolicyStateRecordFromCache(policySysId);
      if (policyStateGr)
          return policyStateGr;

      // cache miss, so check the table
      policyStateGr = new GlideRecord('sn_agent_policy_state');
      if (policyStateGr.get('policy', policySysId)) {
          // this policy state record was not in the cache, so we should add it now
          this.putCacheEntry(policySysId, policyStateGr);
          return policyStateGr;
      }

      // return undefined if we could not find the policy state record for this policy
      return;
  },

  /**
   * Gets the specified policy's policy state record from the cache.
   * 
   * Parameters:
   *     policySysId - the sys_id of the policy
   * 
   * Return:
   *     the policy state record if it was found in the cache. undefined otherwise.
   */
  getPolicyStateRecordFromCache: function(policySysId) {
      var currentDomainId = this.domainInfo.getCurrentDomain();
      if (!this.policyStateRecordCache[currentDomainId])
          return;

      return this.policyStateRecordCache[currentDomainId][policySysId];
  },

  /**
   * Creates a policy state record and adds it to the policy state record cache.
   * 
   * Parameters:
   *     policySysId - the sys_id of the policy record that this policy state record corresponds to
   *     publishStatus (optional) - the publish status that the new record should have
   *     publishError (optional) - the publish error that the new record should have
   * 
   * Return:
   *     a GlideRecord pointing to the policy state record if it was created. undefined otherwise.
   */
  createPolicyStateRecord: function(policySysId, publishStatus, publishError) {
      if (!policySysId) {
          gs.warn('Cannot create policy state record without policy sys_id');
          return;
      }

      var policyStateGr = new GlideRecord('sn_agent_policy_state');
      policyStateGr.setValue('policy', policySysId);
      policyStateGr.setValue('publish_status', publishStatus);
      if (publishError)
          policyStateGr.setValue('publish_error', publishError);
      policyStateGr.insert();
      // add newly created policy state record to cache
      this.putCacheEntry(policySysId, policyStateGr);
      return policyStateGr;
  },

  /**
   * Sets the publish status on the policy state records for the passed policies.
   * Does not use the policy state record cache since this performs a batch update.
   * Invalidates the cache entries for the passed policies.
   * 
   * Parameters:
   *     policySysIds - a policy sys_id as a string, or an array of policy sys_ids
   *     newStatus - the publish status that the policy state records should be set to
   * 
   * Return:
   *     none
   */
  setPublishStatusForPolicies: function(policySysIds, newStatus) {
      if (!policySysIds || policySysIds.length < 1)
          return;

      // invalidate the cache entries for the policies since
      // the policy states are being updated without using the cache
      for (var i = 0; i < policySysIds.length; i++) {
          var policySysId = policySysIds[i];
          this.invalidateCacheEntry(policySysId);
      }

      // batch update the policy state records
      var policyStateGr = new GlideRecord('sn_agent_policy_state');
      policyStateGr.addQuery('policy', policySysIds);
      policyStateGr.setValue('publish_status', newStatus);
      policyStateGr.updateMultiple();
  },

  /**
   * Adds an entry to the cache, mapping a policy's sys_id to the policy's corresponding
   * policy state record.
   * 
   * Parameters:
   *     policySysId - the sys_id of the policy record
   *     policyStateGr - a GlideRecord pointing to the policy's corresponding policy state record
   */
  putCacheEntry: function(policySysId, policyStateGr) {
      var currentDomainId = this.domainInfo.getCurrentDomain();
      this.policyStateRecordCache[currentDomainId] = this.policyStateRecordCache[currentDomainId] ? this.policyStateRecordCache[currentDomainId] : {};
      this.policyStateRecordCache[currentDomainId][policySysId] = policyStateGr;
  },

  /**
   * Removes the specified policy from the policy state record cache.
   * The next time the policy's policy state record is needed, it will be queried from
   * the policy state table.
   * 
   * Parameters:
   *     policySysId - the sys_id of the policy that should be removed from the cache
   * 
   * Return:
   *     none
   */
  invalidateCacheEntry: function(policySysId) {
      var currentDomainId = this.domainInfo.getCurrentDomain();
      if (!this.policyStateRecordCache[currentDomainId])
          // did not find entries for this domain in the cache, nothing to remove
          return;

      if (!this.policyStateRecordCache[currentDomainId][policySysId])
          // did not an entry for the policy sys id in the cache, nothing to remove
          return;

      delete this.policyStateRecordCache[currentDomainId][policySysId];
  },

  /**
   * Invalidates the entire policy state record cache.
   * 
   * Return:
   *     none
   */
  invalidateCache: function() {
      this.policyStateRecordCache = {};
  },

  /**
   * Deletes the specified policy's corresponding policy state record if it exists.
   * If the policy state record is in the cache, the cache entry for this
   * policy is invalidated.
   * 
   * Parameters:
   *     policySysId - the sys_id of the policy that will have its policy state record deleted
   * 
   * Return:
   *     none
   */
  deletePolicyStateRecord: function(policySysId) {
      var policyStateGr = this.getPolicyStateRecordFromCache(policySysId);
      if (policyStateGr) {
          // policy state record was found in cache
          // invalidate the cache entry and delete the record
          this.invalidateCacheEntry(policySysId);
          policyStateGr.deleteRecord();
          return;
      }

      // look for a policy state record that has the specified policy
      policyStateGr = new GlideRecord('sn_agent_policy_state');
      policyStateGr.addQuery('policy', policySysId);
      policyStateGr.query();
      if (!policyStateGr.hasNext())
          return;

      // if this is called from a leaf domain, we only expect to find one record
      // that points to the specified policy, but we will use deleteMultiple to be safe
      policyStateGr.deleteMultiple();
  },

  type: 'PolicyStateUtil'
};

Sys ID

a61a06d477b521107c09ee1b8d5a993d

Offical Documentation

Official Docs: