Name

global.InterProjectRelationDeleteHandler

Description

Called when external relation record is deleted in planned_task_rel_planned_task table, it goes and deletes the shadow tasks/relations and trigger full recalculate in both the projects

Script

var InterProjectRelationDeleteHandler = Class.create();
InterProjectRelationDeleteHandler.prototype = {
  initialize: function(_current) {
      this.relation = _current;
      this._tasksToDelete = [];
      this._relationsToDelete = [];
  },
  
  performCleanup: function() {
      PPMDebug.log("Into InterProjectRelationDeleteHandler.performCleanup");
      var self = this;
      var api = new SNC.PlannedTaskAPI();

      this._findTasksToDelete();
      this._findRelationsToDelete();
      
      this.notificationsToCleanup().forEach(function(notif) { 
          PPMDebug.log("Into InterProjectRelationDeleteHandler.notificationsToCleanup - deleting -> " + notif.getValue("sys_id"));
          notif.deleteRecord(false);
      });
      
      this._relationsToDelete.forEach(function(rel) { 
          PPMDebug.log("Into InterProjectRelationDeleteHandler._relationsToDelete - deleting -> " + rel.record.getValue("sys_id"));
          rel.record.deleteRecord(false); 
      }); 
      
      this._tasksToDelete.forEach(function(task) { 
          PPMDebug.log("Into InterProjectRelationDeleteHandler._tasksToDelete - deleting -> " + task.getValue("sys_id") + " - " + task.getValue("top_task"));
          task.deleteRecord(false);
      });         
      
      this._relationsToDelete.forEach(function(rel) { 
          PPMDebug.log("Into InterProjectRelationDeleteHandler._relationsToDelete - validate wbs -> " + rel.affectedProjectId);
          if(rel.affectedProjectId)
              api.validateWbs(rel.affectedProjectId);
          InterProjectEventManager.raiseFullRecalculateEvent(rel.affectedProjectId);
      });
  },
  
  notificationsToCleanup: function() {
      return PlannedTaskNotificationService.getAllNotificationsForTask(this.relation.getValue('child'));
  },
      
  _findTasksToDelete: function() {
      var task = this.taskToDeleteInParentProject();
      PPMDebug.log("Into InterProjectRelationDeleteHandler._findTasksToDelete - taskToDeleteInParentProject -> " + 
          task.getValue("short_description") + " | hasMoreThanOneIncomingRelation ->" + task.hasMoreThanOneIncomingRelation());
      if ( JSUtil.notNil(task) && !task.hasMoreThanOneIncomingRelation() ) {
          PPMDebug.log("Into InterProjectRelationDeleteHandler._findTasksToDelete - adding 1 -> " + 
              task.getValue("short_description"));
          this._tasksToDelete.push(task);
      }
      task = this.taskToDeleteInChildProject();
      PPMDebug.log("Into InterProjectRelationDeleteHandler._findTasksToDelete - taskToDeleteInChildProject -> " + 
          task.getValue("short_description") + " | hasMoreThanOneOutgoingRelation -> " + task.hasMoreThanOneOutgoingRelation());
      if ( JSUtil.notNil(task) &&  !task.hasMoreThanOneOutgoingRelation() ) {
          PPMDebug.log("Into InterProjectRelationDeleteHandler._findTasksToDelete - adding 2 -> " + 
              task.getValue("short_description"));
          this._tasksToDelete.push(task);
      }
  },
  
  _findRelationsToDelete: function() {
      var rels = [];
      var rel = {};
      rel.record = new PlannedTaskRelation(ShadowTaskQueryHelper.findUpstreamShadowRelation(this.relation.sys_id));
      rel.affectedProjectId = rel.record.record().parent.top_task;
      rel.affectedTaskId = rel.record.getValue('parent');
      rels.push(rel);
      rel = {};
      rel.record = new PlannedTaskRelation(ShadowTaskQueryHelper.findDownstreamShadowRelation(this.relation.sys_id));
      rel.affectedProjectId = rel.record.record().child.top_task;
      rel.affectedTaskId = rel.record.getValue('child');
      rels.push(rel);
      this._relationsToDelete = rels;             
  },
  
  taskToDeleteInParentProject: function() {
      var rel = new PlannedTaskRelation(ShadowTaskQueryHelper.findUpstreamShadowRelation(this.relation.sys_id));
      return rel.getChild();
  },
  
  taskToDeleteInChildProject: function() {
      var rel = new PlannedTaskRelation(ShadowTaskQueryHelper.findDownstreamShadowRelation(this.relation.sys_id));
      return rel.getParent();
  },
  
  type: 'InterProjectRelationDeleteHandler'
};

Sys ID

3216df419f122200598a5bb0657fcfae

Offical Documentation

Official Docs: