Name

global.ShadowRelationProcessor

Description

No description available

Script

var ShadowRelationProcessor = Class.create();
ShadowRelationProcessor.prototype = {
  initialize: function(shadowTask, originalTask) {
      PPMDebug.log("Into ShadowRelationProcessor.initialize -> " + shadowTask.getValue("sys_id") + " - " + originalTask.getValue("sys_id"));
      this.shadowTask = shadowTask;
      this.originalTask = originalTask;
  },

  relationsAreSame: function() {
      if (JSUtil.notNil(this.shadowTask.relation_applied) && JSUtil.notNil(this.originalTask.relation_applied)) {
          var predProjectRelation = ShadowTaskQueryHelper.relationRecord(this.shadowTask.relation_applied);
          PPMDebug.log("Into ShadowRelationProcessor.relationsAreSame: predProjectRelation  -> " +
              predProjectRelation.getValue("sys_id") + " - " + predProjectRelation.getValue("orig_sys_id"));
          var succProjectRelation = this.originalTask.relation_applied.getRefRecord();
          PPMDebug.log("Into ShadowRelationProcessor.relationsAreSame: succProjectRelation  -> " +
              succProjectRelation.getValue("sys_id") + " - " + succProjectRelation.getValue("orig_sys_id"));
          if (predProjectRelation.getValue("orig_sys_id") == succProjectRelation.getValue("orig_sys_id"))
              return true;
      }
      return false;
  },

  externalRelationCountOnTask: function() {
      var s = new PlannedTaskRelationDBService();
      return s.externalRelationsCount(this.originalTask.getValue('sys_id'));
  },

  compareShadowTaskDateToOrigTaskDate: function() {
      // Refetch the Original and Shadow Task - For big project the sync is missing
  	if (!this.originalTask.getValue("sys_id") || !this.shadowTask.getValue("sys_id")){
  		return -1;
  	}
      var origTask = new GlideRecord("planned_task");
      origTask.get(this.originalTask.getValue("sys_id"));
      var shadTask = new GlideRecord("planned_task");
      shadTask.get(this.shadowTask.getValue("sys_id"));

      var taskStartDate = new GlideDateTime(origTask.getValue('start_date'));
      var shadowTaskStartDate = new GlideDateTime(shadTask.getValue('start_date'));

      return (shadowTaskStartDate.compareTo(taskStartDate));
  },

  internalRelationCountOnTask: function() {
      var s = new PlannedTaskRelationDBService();
      return s.internalRelationsCount(this.originalTask.getValue('sys_id'));
  },

  getLatestStartDate: function() {
      PPMDebug.log('ShadowRelationProcessor.getLatestStartDate');
      var api = new SNC.PlannedTaskAPI();
      var response = api.getLatestStartDateWithShadows(this.originalTask.getValue("sys_id"), true);
      PPMDebug.log('ShadowRelationProcessor.getLatestStartDate - response: ' + response);
      return (new JSON()).decode(response);
  },

  areRelationsRelated: function(rel1, rel2) {
      if (JSUtil.notNil(rel1) && JSUtil.notNil(rel2)) {
          var gr1 = new GlideRecord("planned_task_rel_planned_task");
          if (gr1.get(rel1)) {
              var gr2 = new GlideRecord("planned_task_rel_planned_task");
              if (gr2.get(rel2)) {
                  if (gr1.getValue("orig_sys_id") == gr2.getValue("orig_sys_id"))
                      return true;
              }
          }
      }
      return false;
  },

  shouldShadowRelationBeProcessed: function() {
      PPMDebug.log('ShadowRelationProcessor shouldShadowBeProcessed');
      var noOfRelations = this.internalRelationCountOnTask() + this.externalRelationCountOnTask();
      PPMDebug.log('ShadowRelationProcessor internalRelationCount ' + noOfRelations + ' ' + this.compareShadowTaskDateToOrigTaskDate());

      // task start date matches to shadow task with other relation
      if (this.compareShadowTaskDateToOrigTaskDate() == 0) {
          PPMDebug.log('ShadowRelationProcessor shouldShadowBeProcessed - Dates are same');
          return false;
      }

      // number of internal + external relations = 1
      // if the relation applied is external - return true
      if (noOfRelations == 1 && this.relationsAreSame()) {
          PPMDebug.log('ShadowRelationProcessor shouldShadowBeProcessed - Relation count 1 AND Relation Applied is external');
          return true;
      }

      var latestStartDate = this.getLatestStartDate();
      PPMDebug.log('ShadowRelationProcessor shouldShadowBeProcessed - getLatestStartDate ' + (new JSON()).encode(latestStartDate));
      var relationAppliedOnShadow = this.shadowTask.relation_applied;
      var relationAppliedOnLatestStart = latestStartDate.relation_applied;
      PPMDebug.log('ShadowRelationProcessor shouldShadowBeProcessed - relationAppliedOnShadow: ' + relationAppliedOnShadow +
          " | relationAppliedOnLatestStart -> " + relationAppliedOnLatestStart);
      // Shadow Relation is deriving the latest start date
      if (this.areRelationsRelated(relationAppliedOnShadow, relationAppliedOnLatestStart)) {
          PPMDebug.log('ShadowRelationProcessor shouldShadowBeProcessed - Relation Applied == Extenal Relation ');
          return true;
      } else if (this.relationsAreSame()) {
          // previously if the relation applied is external
          // create a notification - just to re-adjust the dates as per start date
          PPMDebug.log('ShadowRelationProcessor shouldShadowBeProcessed - Relation Applied != Extenal Relation - Re-Adjust the dates');
          return true;
      }
      PPMDebug.log('ShadowRelationProcessor shouldShadowBeProcessed - Return false');
      return false;
  },

  type: 'ShadowRelationProcessor'
};

Sys ID

b7f594e30b513200acc30e7363673a21

Offical Documentation

Official Docs: