Name

global.RedundantRelationsChecker

Description

No description available

Script

var RedundantRelationsChecker = Class.create();
RedundantRelationsChecker.prototype = {
  CLASS_NAME: "RedundantRelationsChecker", 

  initialize: function() {
  },

  checkRelations: function (topTaskId) {
      PmDebugUtil.logMsg(this.CLASS_NAME, "Into checkRelations -> " + topTaskId);
      if(JSUtil.notNil(topTaskId)) {
          var gr = this.getRelations(topTaskId);
          if(JSUtil.notNil(gr)) {
              var relations = [];
              while(gr.next()) {
                  relations.push({   sysId: gr.getValue("sys_id"),
                              parent: gr.getValue("parent"),
                              parent_number: gr.parent.getRefRecord().getValue("number"),
                              child: gr.getValue("child"),
                              child_number: gr.child.getRefRecord().getValue("number")
                              });
              }
              return this.getDuplicateRelations(relations) || [];
          }
      }
      return [];
  },

  getRelations: function (topTaskId) {
      PmDebugUtil.logMsg(this.CLASS_NAME, "Into getRelations -> " + topTaskId);
      if(JSUtil.notNil(topTaskId)) {
          var gr = new GlideRecord(PmTableConstants.PLANNED_TASK_REL);
          gr.addQuery("parent_top_task", topTaskId);
          gr.addQuery("child_top_task", topTaskId);
          gr.query();
          PmDebugUtil.logMsg(this.CLASS_NAME, "Into getRelations -> " + gr.getRowCount() 
              + " | " + gr.getEncodedQuery());
          return gr;
      }
  },

  getDuplicateRelations: function (relations) {
      PmDebugUtil.logMsg(this.CLASS_NAME, "Into getDuplicateRelations -> " + JSON.stringify(relations));
      var duplicateRelations = [];
      var message = gs.getMessage('is duplicate of');
      if(JSUtil.notNil(relations)) {
          for (var i = 0; i < relations.length; i++) {
              var relation = relations[i];
              for (var j = 0; j < relations.length; j++) {
                  if(i != j) {
                      var otherRelation = relations[j];
                      if(this.isDuplicateOf(relation, otherRelation)) {
                          otherRelation['duplicate_sys_id'] = relation.sysId;
                          otherRelation['duplicate_parent_number'] = relation.parent_number;
                          otherRelation['duplicate_child_number'] = relation.child_number;
                          otherRelation['message'] = message;
                          duplicateRelations.push(otherRelation);
                      }
                  }
              }
          }
      }
      return duplicateRelations;
  },

  isDuplicateOf: function (relation, otherRelation) {
      PmDebugUtil.logMsg(this.CLASS_NAME, "Into isDuplicateOf -> " + JSON.stringify(relation) + " | " 
          + JSON.stringify(otherRelation));
      if(JSUtil.notNil(relation) && JSUtil.notNil(otherRelation)) {
          return (relation.parent == otherRelation.parent) && this.isChildOf(otherRelation.child, relation.child);
      }
      return false;
  },

  isChildOf: function (taskId, otherParentId) {
      PmDebugUtil.logMsg(this.CLASS_NAME, "Into isChildOf -> " + taskId + " | " + otherParentId);
      if(JSUtil.notNil(taskId) && JSUtil.notNil(otherParentId)) {
          var arrayUtil = new ArrayUtil();
          var parentStack = [taskId];
          var parentId = this.getParent(taskId);
          while(JSUtil.notNil(parentId) && !arrayUtil.contains(parentStack, parentId)) {
              if(parentId == otherParentId)
                  return true;
              parentStack.push(parentId);
              parentId = this.getParent(parentId);
          }
      }
      return false;
  },

  getParent: function (taskId) {
      PmDebugUtil.logMsg(this.CLASS_NAME, "Into getParent -> " + taskId);
      if(JSUtil.notNil(taskId) ) {
          var gr = new GlideRecord("planned_task");
          if(gr.get(taskId)) {
              PmDebugUtil.logMsg(this.CLASS_NAME, "Into getParent: Parent -> " + gr.getValue("parent"));
              return gr.getValue("parent");
          }
      }
  },
  type: 'RedundantRelationsChecker'
};

Sys ID

0f381e589f011300730e5bb0657fcf83

Offical Documentation

Official Docs: