Name

sn_agent.ParentPolicyUtils

Description

No description available

Script

var ParentPolicyUtils = Class.create();
ParentPolicyUtils.prototype = {
  initialize: function() {
      this.policyStateUtil = new PolicyStateUtil();
  },

  // FLAT is represented as NONE in the label exposed in the UI

  deleteAllPoliciesTree: function(sysIdList, actionFromList) {
      var parentsMap = {};
      var policyGr = new GlideRecord("sn_agent_policy");
      if (actionFromList) {
          policyGr.addQuery("sys_id", "IN", sysIdList);
      } else {
          var qc1 = policyGr.addQuery("published_parent", "IN", sysIdList);
          qc1.addOrCondition("sys_id", "IN", sysIdList);
      }
      var qc2 = policyGr.addQuery("is_draft", "false");
      qc2.addOrCondition("publish_status", "0");
      policyGr.query();
      while (policyGr.next()) {
          if (policyGr.getValue("relationship_state") == "2") {
              // policyGr is a child policy
              parentsMap[policyGr.getValue("published_parent")] = 1;
          } else if (policyGr.getValue("relationship_state") == "1") {
              // policyGr is a parent policy
              this.setChildrenToFlat(policyGr.getValue("sys_id"));
          }
          policyGr.deleteRecord();
      }
      this.updateParentsToFlatIfNeeded(parentsMap);
  },

  updateParentsToFlatIfNeeded: function(parentsMap) {
      var parentsWithChildrenList = {};
      var policyGr = new GlideRecord("sn_agent_policy");
      policyGr.addQuery("published_parent", "IN", Object.keys(parentsMap));
      policyGr.query();
      while (policyGr.next()) {
          parentsWithChildrenList[policyGr.getValue("published_parent")] = 1;
      }
      var parentsWithoutChildrenList = this.subtractionOfMapAndList(parentsMap, Object.keys(parentsWithChildrenList));
      if (parentsWithoutChildrenList.length > 0) {
          this.setParentsToFlat(parentsWithoutChildrenList);
      }
  },

  subtractionOfMapAndList: function(mapToSubtractFrom, listToOmit) {
      for (index in listToOmit) {
          delete mapToSubtractFrom[listToOmit[index]];
      }
      return Object.keys(mapToSubtractFrom);
  },

  deletePolicyRecord: function(policySysId) {
      var policyGr = new GlideRecord("sn_agent_policy");
      if (policyGr.get(policySysId)) {
          if (policyGr.getValue("relationship_state") == "1") {
              // policyGr is a parent policy
              this.setChildrenToFlat(policyGr.getValue("sys_id"));
          }
          policyGr.deleteRecord();
      }
  },

  setChildrenToFlat: function(parentSysId) {
      var childGr = new GlideRecord("sn_agent_policy");
      childGr.addQuery("published_parent", parentSysId);
      childGr.setValue("relationship_state", "0");
      childGr.updateMultiple();
  },

  setParentToFlatIfNeeded: function(parentSysId) {
      var childGr = new GlideRecord("sn_agent_policy");
      childGr.addQuery("published_parent", parentSysId);
      childGr.setLimit(1);
      childGr.query();
      if (childGr.getRowCount() == 0) {
          this.setParentsToFlat(parentSysId);
      }
  },

  setParentsToFlat: function(parentSysIds) {
      var policyGr = new GlideRecord("sn_agent_policy");
      var qc = policyGr.addQuery("sys_id", "IN", parentSysIds);
      qc.addOrCondition("related_policy", "IN", parentSysIds);
      policyGr.setValue("relationship_state", "0");
      policyGr.updateMultiple();
  },

  deactivateRecord: function(policySysId) {
      return this.activateOrDeactivateRecord(policySysId, false);

  },

  activateRecord: function(policySysId) {
      return this.activateOrDeactivateRecord(policySysId, true);
  },

  activateOrDeactivateRecord: function(policySysId, activate) {
      var policyGr = new GlideRecord("sn_agent_policy");
      if (policyGr.get(policySysId)) {
          var policyInCurrentDomain = this.getPolicyInCurrentDomain(policyGr);
          policyInCurrentDomain.setValue("active", activate + "");
          if (activate)
              policyInCurrentDomain.setValue("publish_status", "4");
          policyInCurrentDomain.update();
          return policyInCurrentDomain.getValue("sys_id");
      }
  },

  deactivateAllPoliciesTree: function(sysIdList, actionFromList) {
      return this.activateOrDeactivateAllPoliciesTree(sysIdList, false, actionFromList);
  },

  activateAllPoliciesTree: function(sysIdList, actionFromList) {
      return this.activateOrDeactivateAllPoliciesTree(sysIdList, true, actionFromList);
  },

  getAllPolicyTreeGr: function(sysIdList, actionFromList, sysIdListInCurrentDomain) {
      var policyGr = new GlideRecord("sn_agent_policy");
      if (actionFromList) {
          policyGr.addQuery("sys_id", "IN", sysIdListInCurrentDomain);
      } else {
          var qc = policyGr.addQuery("published_parent", "IN", sysIdListInCurrentDomain);
          qc.addOrCondition("sys_id", "IN", sysIdListInCurrentDomain);
      }
      policyGr.addQuery("is_draft", "false");

      return policyGr;
  },

  activateOrDeactivateAllPoliciesTree: function(sysIdList, activate, actionFromList) {
      var sysIdListInCurrentDomain = this.getPoliciesSysIdListInCurrentDomain(sysIdList);
      var policyGr = this.getAllPolicyTreeGr(sysIdList, actionFromList, sysIdListInCurrentDomain);
      policyGr.query();

      // policyFullCalcCycleTime is only set if we are activating the policies
      var policyFullCalcCycleTime;
      if (activate) {
          var p = new PolicyClientsGeneratorNG();
          policyFullCalcCycleTime = p.getPolicyFullCalcCycleAsGlideTime();
      }

      while (policyGr.next()) {
          var policySysId = policyGr.getUniqueValue();
          policyGr.setValue("active", activate);
          if (!activate) {
              policyGr.update();
              continue;
          }

          policyGr.setValue('publish_status', '4');
          policyGr.update();
          this.policyStateUtil.synchronizePolicyStatePublishStatus(policyGr);
          // since we are activating the policy, we will set the flag for performing cmdb diff calculation to true
          // for any domain where the last time that the policy was fully calculated is more recent than the full calc cycle.
          // for policies that have not had a full calculation recently, the calculation code will already cover this case.
          var policyStateGr = new GlideRecord('sn_agent_policy_state');
          policyStateGr.addQuery('policy', policySysId);
          policyStateGr.query();
          while (policyStateGr.next()) {
              var lastFullCalcTime = new GlideDateTime(policyStateGr.getValue('last_full_calc_time'));
              if (lastFullCalcTime.after(policyFullCalcCycleTime)) {
                  // it has not been long enough to do a full policy calc
                  // set the flag for performing cmdb diff to true for this policy in this domain
                  var domainId = policyStateGr.getValue('sys_domain');
                  p.getAndUpdateReCalcFilterFlag("true", domainId);
              }
          }
      }

      return sysIdListInCurrentDomain;
  },

  getPoliciesSysIdListInCurrentDomain: function(sysIdList) {
      // Get a sys_id list of selected policies in the current domain
      var sysIdListInCurrentDomain = [];
      var policyGr = new GlideRecord("sn_agent_policy");
      policyGr.addQuery("sys_id", "IN", sysIdList);
      policyGr.query();
      while (policyGr.next()) {
          var policyInCurrentDomain = this.getPolicyInCurrentDomain(policyGr);
          if (policyInCurrentDomain)
              sysIdListInCurrentDomain.push(policyInCurrentDomain.getValue("sys_id"));
      }
      return sysIdListInCurrentDomain.join(',');
  },

  getPolicyInCurrentDomain: function(policyGr) {
      var policyInCurrentDomain = policyGr;
      var currentDomain = new sn_agent.DomainInfo().getCurrentDomain();
      var publishedRecordDoamin = policyGr.sys_domain;
      if (currentDomain != publishedRecordDoamin) {
          // If one of the policies in the tree has been updated in the current domain, the entire tree has been copied to the current domain
          policyInCurrentDomain = this.getPolicyInCurrentDomainIfAlreadyExist(policyGr.getValue("sys_id"));
          if (!policyInCurrentDomain) {
              policyInCurrentDomain = new sn_agent.PolicyDraftUtils().copyPolicyTreeToCurrentDomain(policyGr);
          }
      }
      return policyInCurrentDomain;
  },

  getPolicyInCurrentDomainIfAlreadyExist: function(policySysId) {
      var policyGr = new GlideRecord("sn_agent_policy");
      if (policyGr.get("sys_overrides", policySysId)) {
          return policyGr;
      }
      return null;
  },

  doesListContainParentPolicy: function(sysIdList) {
      var policyGr = new GlideRecord("sn_agent_policy");
      policyGr.addQuery("sys_id", "IN", sysIdList);
      policyGr.addQuery("relationship_state", "1");
      var qc = policyGr.addQuery("is_draft", "false");
      qc.addOrCondition("publish_status", "0");
      policyGr.query();
      return policyGr.hasNext();
  },

  doesListContainParentPolicyAndDeleteRecordsIfNot: function(sysIdList) {
      var doesListContainParentPolicy = this.doesListContainParentPolicy(sysIdList);
      if (!doesListContainParentPolicy)
          this.deleteAllPoliciesTree(sysIdList, true);
      return doesListContainParentPolicy;
  },

  doesListContainParentPolicyAndDeactivateRecordsIfNot: function(sysIdList) {
      var doesListContainParentPolicy = this.doesListContainParentPolicy(sysIdList);
      if (!doesListContainParentPolicy)
          this.deactivateAllPoliciesTree(sysIdList, true);
      return doesListContainParentPolicy;
  },

  doesListContainParentPolicyAndActivateRecordsIfNot: function(sysIdList) {
      var doesListContainParentPolicy = this.doesListContainParentPolicy(sysIdList);
      if (!doesListContainParentPolicy)
          this.activateAllPoliciesTree(sysIdList, true);
      return doesListContainParentPolicy;
  },

  fixAllPreviousEmptyDataToFlat: function() {
      var policyGr = new GlideRecord("sn_agent_policy");
      policyGr.addQuery("relationship_state", "");
      policyGr.setValue("relationship_state", "0");
      policyGr.updateMultiple();
  },

  copyChangesToDraftChildPolicies: function(publishedParentPolicyGr, fieldsToCopy) {
      var draftChildPolicyGr = new GlideRecord("sn_agent_policy");
      draftChildPolicyGr.addQuery("published_parent", publishedParentPolicyGr.getUniqueValue());
      draftChildPolicyGr.addQuery("is_draft", "true");
      draftChildPolicyGr.addQuery("inherit_from_parent", "true");
      draftChildPolicyGr.query();
      while (draftChildPolicyGr.next()) {
          if (this.copyChangesToChildEntity(fieldsToCopy, draftChildPolicyGr, publishedParentPolicyGr)) {
              // If the child's publish_status field is 'Published', change it to 'Published*'
              if (draftChildPolicyGr.getValue("publish_status") == "1") {
                  this.updatePolicyPublishStatus(draftChildPolicyGr.getValue("related_policy"), "2");
                  draftChildPolicyGr.setValue("publish_status", "2");
              }
          }
          draftChildPolicyGr.update();
      }
  },

  copyChangesToDraftChildChecks: function(publishedParentCheckGr, fieldsToCopy) {
      var draftChildCheckGr = new GlideRecord("sn_agent_check");
      draftChildCheckGr.addQuery("published_parent", publishedParentCheckGr.getUniqueValue());
      draftChildCheckGr.addQuery("is_draft", "true");
      draftChildCheckGr.addQuery("monitoring_policy.inherit_from_parent", "true");
      draftChildCheckGr.query();
      while (draftChildCheckGr.next()) {
          this.copyChangesToChildEntity(fieldsToCopy, draftChildCheckGr, publishedParentCheckGr);
          draftChildCheckGr.update();
      }
  },

  copyChangesToDraftChildCheckParams: function(table, publishedParentCheckParamGr, fieldsToCopy) {
      var draftChildCheckParamGr = new GlideRecord(table);
      draftChildCheckParamGr.addQuery("published_parent", publishedParentCheckParamGr.getUniqueValue());
      draftChildCheckParamGr.addQuery("is_draft", "true");
      draftChildCheckParamGr.addQuery("check.monitoring_policy.inherit_from_parent", "true");
      draftChildCheckParamGr.query();
      while (draftChildCheckParamGr.next()) {
          this.copyChangesToChildEntity(fieldsToCopy, draftChildCheckParamGr, publishedParentCheckParamGr);
          draftChildCheckParamGr.update();
      }
  },

  copyChangesToChildEntity: function(fieldsToCopy, childEntityGr, parentEntityGr) {
      var shouldPublishedStatusBeChanged = false;
      for (field in fieldsToCopy) {
          var fieldValue = fieldsToCopy[field];
          if (parentEntityGr.getValue(fieldValue) != childEntityGr.getValue(fieldValue)) {
              shouldPublishedStatusBeChanged = true;
              var parentEntityFieldValue = parentEntityGr.getValue(fieldValue);
              if (gs.nil(parentEntityFieldValue))
                  parentEntityFieldValue = "";
              childEntityGr.setValue(fieldValue, parentEntityFieldValue);
          }
      }
      return shouldPublishedStatusBeChanged;
  },

  updatePolicyPublishStatus: function(policyId, statusValue) {
      var policyGr = new GlideRecord("sn_agent_policy");
      if (policyGr.get(policyId)) {
          policyGr.setValue("publish_status", statusValue);
          policyGr.update();
      }
  },

  changeInheritFromParenValueForChildPolicy: function(current, inheritanceValue) {
      var currentDomain = new DomainInfo().getCurrentDomain();
      var publishedRecordDoamin = current.sys_domain;
      if (currentDomain != publishedRecordDoamin) {
          current = (new PolicyDraftUtils()).copyPolicyTreeToCurrentDomain(current);
      }
      current.inherit_from_parent = inheritanceValue;
      current.update();
      if (current.published_status != '0')
          this.updateInheritFromParenValueForDraftRecord(current, inheritanceValue);
      return current;
  },

  updateInheritFromParenValueForDraftRecord: function(current, inheritanceValue) {
      var policyGr = new GlideRecord("sn_agent_policy");
      if (policyGr.get("related_policy", current.getUniqueValue())) {
          policyGr.inherit_from_parent = inheritanceValue;
          policyGr.update();
      }
  },

  addNewDraftChildCheckParam: function(publishedCheckParamGr) {
      var darftChildCheckGr = new GlideRecord("sn_agent_check");
      darftChildCheckGr.addQuery("published_parent", publishedCheckParamGr.check);
      darftChildCheckGr.addQuery("is_draft", "true");
      darftChildCheckGr.addQuery("monitoring_policy.inherit_from_parent", "true");
      darftChildCheckGr.query();
      while (darftChildCheckGr.next()) {
          var copiedCheckParam = (new PolicyDraftUtils).cloneRecord(publishedCheckParamGr);
          copiedCheckParam.setValue("published_parent", publishedCheckParamGr.getUniqueValue());
          copiedCheckParam.setValue("check", darftChildCheckGr.getUniqueValue());
          copiedCheckParam.update();
      }
  },

  deleteChildCheckParam: function(table, publishedCheckParamGr) {
      var childCheckParamGr = new GlideRecord(table);
      childCheckParamGr.addQuery("published_parent", publishedCheckParamGr.getUniqueValue());
      childCheckParamGr.addQuery("is_draft", "true");
      childCheckParamGr.addQuery("check.monitoring_policy.inherit_from_parent", "true");
      childCheckParamGr.deleteMultiple();
  },

  deleteDraftChildChecks: function(publishedParentCheckGr) {
      var draftChildCheckGr = new GlideRecord("sn_agent_check");
      draftChildCheckGr.addQuery("published_parent", publishedParentCheckGr.getUniqueValue());
      draftChildCheckGr.addQuery("is_draft", "true");
      draftChildCheckGr.addQuery("monitoring_policy.inherit_from_parent", "true");
      draftChildCheckGr.query();
      while (draftChildCheckGr.next()) {
          this.deleteCheckDefFromChecksStr(draftChildCheckGr.getValue("monitoring_policy"), draftChildCheckGr.getValue("check_def"));
          draftChildCheckGr.deleteRecord();
      }
  },

  deleteCheckDefFromChecksStr: function(draftPolicyId, checkDefToDelete) {
      var draftPolicyGr = this.getGr(draftPolicyId, "sn_agent_policy");
      var checksStr = draftPolicyGr.getValue("checks_str");
      var checksStrList = checksStr.split(",");
      var checksStrResult = [];
      var shouldPublishedStatusBeChanged = false;
      for (checkDef in checksStrList) {
          var checkDefValue = checksStrList[checkDef];
          if (checkDefValue != checkDefToDelete)
              checksStrResult.push(checkDefValue);
          else
              shouldPublishedStatusBeChanged = true;
      }

      checksStrResult = checksStrResult.join(",");

      if (shouldPublishedStatusBeChanged) {
          draftPolicyGr.setValue("checks_str", checksStrResult);

          // If the policy publish_status field is 'Published', change it to 'Published*'
          if (draftPolicyGr.getValue("publish_status") == "1") {
              this.updatePolicyPublishStatus(draftPolicyGr.getValue("related_policy"), "2");
              draftPolicyGr.setValue("publish_status", "2");
          }
      }
      draftPolicyGr.update();
  },

  getGr: function(recordSysId, table) {
      var gr = new GlideRecord(table);
      if (gr.get(recordSysId))
          return gr;
      return null;
  },

  type: 'ParentPolicyUtils'
};

Sys ID

c1096e00dbc924102e0fd03cd39619a3

Offical Documentation

Official Docs: