Name
global.RecalculationHelper
Description
Recalculation Helper help processing the delayed recalculation requeusts
Script
// Recalculation Helper
var RecalculationHelper = Class.create();
RecalculationHelper.prototype = {
RECALCULATION_STACK: "recalculation_stack_",
initialize: function() {
this.json = new JSON();
},
getStack: function(sysId) {
if(JSUtil.notNil(sysId)) {
var recalculationStack = gs.getSession().getProperty(this.RECALCULATION_STACK + sysId);
if(JSUtil.nil(recalculationStack)) {
recalculationStack = "[]";
}
return this.json.decode(recalculationStack);
}
},
putStack: function(sysId, recalculationStack) {
var recalculationStackString = this.json.encode(recalculationStack);
gs.getSession().putProperty(this.RECALCULATION_STACK + sysId, recalculationStackString);
},
clearStack: function(sysId) {
var recalculationStack = this.getStack(sysId);
if(JSUtil.notNil(recalculationStack)) {
gs.getSession().clearProperty(this.RECALCULATION_STACK + sysId);
} // else do nothing
},
addToStack: function(recalculationStack, sysId) {
var arrayUtil = new ArrayUtil();
if(arrayUtil.indexOf(recalculationStack, sysId) == -1) {
recalculationStack.push(sysId);
}
},
areStatesInSameBucket: function(current, previous) {
var currentTaskStateUtil = new PlannedTaskStateUtil(current);
var previousTaskStateUtil = new PlannedTaskStateUtil(previous);
var currentStateBucket = currentTaskStateUtil.getBucketForState(current.state);
var previousStateBucket = previousTaskStateUtil.getBucketForState(previous.state);
if(currentStateBucket == previousStateBucket)
return true;
else
return false;
},
// Place(s) which trigger the recalculation push the same to recalculationStack
process: function(gr, previousGr) {
if(this.areStatesInSameBucket(gr, previousGr))
return;
var arrayUtil = new ArrayUtil();
// Check if the top task exists in recalculation Stack
var sysId = gr.getValue("sys_id");
var topTask = gr.getValue("top_task");
PPMDebug.log("RecalculationHelper.process: sysId: " + sysId + " -- topTask: " + topTask);
var recalculationStack;
if( JSUtil.notNil(topTask) ) {
recalculationStack = this.getStack(topTask);
if( JSUtil.notNil(recalculationStack) ) {
this.addToStack(recalculationStack, sysId);
} else {
recalculationStack = [sysId];
}
PPMDebug.log("RecalculationHelper.process -> recalculationStack over topTask: " + recalculationStack.join(","));
this.putStack(topTask, recalculationStack);
} else { // Ideally there should be a task with no Top Task
recalculationStack = [sysId];
PPMDebug.log("RecalculationHelper.process -> recalculationStack over sysId: " + recalculationStack.join(","));
this.putStack(sysId, recalculationStack);
}
},
// Validate and Recalculate
recalculate: function(gr) {
var arrayUtil = new ArrayUtil();
// Check if the top task exists in recalculation Stack
var sysId = gr.getValue("sys_id");
var topTask = gr.getValue("top_task");
PPMDebug.log("RecalculationHelper.recalculate: sysId: " + sysId + " -- topTask: " + topTask);
var recalculationStack = [];
if( JSUtil.notNil(topTask) ) {
recalculationStack = this.getStack(topTask);
if( JSUtil.notNil(recalculationStack) ) {
// Check If the First of Stack is this Caller
PPMDebug.log("RecalculationHelper.recalculate -> recalculationStack: " + recalculationStack.join(","));
if(arrayUtil.indexOf(recalculationStack, sysId) == 0) {
PPMDebug.log("RecalculationHelper.Triggering the Recalculation as this task was triggering task for recalculate");
return true; // recalculate here
} else {
PPMDebug.log("RecalculationHelper - Ignoring the Recalculation! for task: " + gr.getValue("number") + "-" + gr.getValue("short_description"));
this.addToStack(recalculationStack, sysId);
this.putStack(topTask, recalculationStack);
return false; // dont recalculate
}
}
}
PPMDebug.log("RecalculationHelper - *** Unable to fetch the recalculationStack!! --> Triggering the Recalculation");
return true;
},
tasksToRecalculate: function(gr) {
var arrayUtil = new ArrayUtil();
// Check if the top task exists in recalculation Stack
var sysId = gr.getValue("sys_id");
var topTask = gr.getValue("top_task");
PPMDebug.log("RecalculationHelper.tasksToRecalculate: sysId: " + sysId + " -- topTask: " + topTask);
var recalculationStack = [];
if( JSUtil.notNil(topTask) ) {
recalculationStack = this.getStack(topTask);
if( JSUtil.notNil(recalculationStack) ) {
PPMDebug.log("RecalculationHelper.tasksToRecalculate over topTask: " + recalculationStack.join(","));
this.clearStack(topTask);
return recalculationStack;
}
} else {
recalculationStack = this.getStack(sysId);
PPMDebug.log("RecalculationHelper.tasksToRecalculate over sysId: " + recalculationStack.join(","));
this.clearStack(sysId);
return recalculationStack;
}
},
type: 'RecalculationHelper'
};
Sys ID
a3f687909f231200598a5bb0657fcf57