Name

global.PlanningConsolePreferenceValidator

Description

No description available

Script

var PlanningConsolePreferenceValidator = Class.create();
PlanningConsolePreferenceValidator.prototype = {
  // Default Entities definitions
  DEFAULT_PROJECT_ENTITY: "project",
  DEFAULT_PORTFOLIO_ENTITY: "portfolio",
  DEFAULT_PROGRAM_ENTITY: "program",
  DEFAULT_RELEASE_ENTITY: "release",
  DEFAULT_PRODUCT_ENTITY: "product",
  DEFAULT_PROJECT_ENTITY_CLASS_NAME: "pm_project",
  DEFAULT_PORTFOLIO_ENTITY_CLASS_NAME: "pm_portfolio",
  DEFAULT_PROGRAM_ENTITY_CLASS_NAME: "pm_program",
  CUSTOM: 'custom',
  
  // Planning Console - Preference prefix
  PROJECT_ENTITY_PREFIX: "com.snc.project_management.pc.project_entity.",
  CONSOLE_PREFERENCE_PREFIX: "com.snc.project_management.pc.console_preference.",
  // PROJECT_ENTITY_ID_PREFIX: "com.snc.project_management.pc.project_entity_id.", 
  PORTFOLIO_ENTITY_PREFIX: "com.snc.project_management.pc.portfolio_entity.",
  // PORTFOLIO_ENTITY_ID_PREFIX: "com.snc.project_management.pc.portfolio_entity_id.", 

  // Tables
  PLANNED_TASK_TABLE: "planned_task",
  PROJECT_TABLE: "pm_project",
  PROJECT_TASK_TABLE: "pm_project_task",
  PORTFOLIO_TABLE: "pm_portfolio",
  PLANNED_TASK_BASELINE_TABLE: "planned_task_baseline",
  PROGRAM_TABLE: "pm_program",
  RELEASE_TABLE: "rm_release",
  PRODUCT_TABLE: "rm_product",
  PLANNED_TASK_CUSTOM_CONSOLE_TABLE: "planned_task_custom_console",
  FISCAL_PERIOD_TABLE : 'fiscal_period',

  // Table Columns
  SYS_CLASS_NAME: "sys_class_name",

  initialize: function() {
  },

  nil: function(param) {
      if( !param || typeof param == 'undefined' || param == null || param == '') {
          return true;
      }
      return false;
  },
  
  validateClassName: function(className) {
      var gr = new GlideRecord(className);
      if ( !gr.isValid() ) 
          return '';
          
      if ( gr.instanceOf('pm_project') )
          return className;
      if ( gr.instanceOf('pm_portfolio') )
          return className;
      if ( gr.instanceOf('pm_program') )
          return className;
      if ( gr.instanceOf('rm_release') )
          return 'rm_release';
  	if ( gr.instanceOf('rm_task') )
          return 'rm_release';
  	if ( gr.instanceOf('rm_product') )
  		return 'rm_product';
      //if project task convert to project
      if ( gr.instanceOf('pm_project_task') )
          return SNC.PPMConfig.getProjectTable(className);
      else if ( PPMTableCheck.isPlannedTask(className) )
          return 'planned_task';
      else 
          return '';  
  },

  getEntity: function(entityClassName) {
      var entityGr = new GlideRecord(entityClassName);
      if( entityGr.instanceOf(this.PROJECT_TABLE) ) {
          return this.DEFAULT_PROJECT_ENTITY;
      } else if( entityGr.instanceOf(this.PORTFOLIO_TABLE) ) {
          return this.DEFAULT_PORTFOLIO_ENTITY;
      } else if (entityGr.instanceOf(this.PROGRAM_TABLE)) {
          return this.DEFAULT_PROGRAM_ENTITY;
      } else if ( entityGr.instanceOf(this.RELEASE_TABLE)) {
          return this.DEFAULT_RELEASE_ENTITY;
      } else if(entityGr.instanceOf(this.PRODUCT_TABLE)) {
  		return this.DEFAULT_PRODUCT_ENTITY;
  	}
      return;
  },

  getProjectPreference: function(entityClassName) {
      return gs.getUser().getPreference(this.PROJECT_ENTITY_PREFIX + entityClassName);
  },

  getPortfolioPrefence: function(entityClassName) {
      return gs.getUser().getPreference(this.PORTFOLIO_ENTITY_PREFIX + entityClassName);
  },

  lastOpenedEntityId: function(entity, entityClassName) {
      if( entity == this.DEFAULT_PROJECT_ENTITY ) {
          return this.getProjectPreference(entityClassName);
      } else if( entity == this.DEFAULT_PORTFOLIO_ENTITY ) {
          return this.getPortfolioPrefence(entityClassName);
      }
      return;
  },

  validateFiscalYear : function(fiscalYearId){
      if(fiscalYearId == 'all') return true;
      var fiscalYearGr = new GlideRecord(this.FISCAL_PERIOD_TABLE);
      if ( fiscalYearGr.get(fiscalYearId) )
          return true;
      else
          return false;
  },

  lastOpenedFiscalYear: function(entityClassName, fiscalYearId, budgetAnalysis){
      //If budget analysis mode is on and fiscal year all is passed change the fiscal period to current
      if(budgetAnalysis == 'true' && fiscalYearId == 'all')
          return PPMFiscalPeriod.getFiscalYearForDate(new GlideDateTime()).getValue('sys_id');
      if ( !GlidePluginManager.isActive('com.snc.financial_planning_pmo') )
          return 'all';
      
      if(JSUtil.notNil(fiscalYearId) && this.validateFiscalYear(fiscalYearId)){
          this.saveConsolePreference(entityClassName, 'FISCALYEAR', fiscalYearId);
          return fiscalYearId;
      } else if( JSUtil.nil(fiscalYearId) ) {
          var lastOpenedFiscalYear = this.getConsolePreference('FISCALYEAR', entityClassName);
          if(JSUtil.nil(lastOpenedFiscalYear)){
              var newDate = new GlideDateTime();
              return PPMFiscalPeriod.getFiscalYearForDate(newDate).getValue('sys_id');
          }
          return lastOpenedFiscalYear;
      }
  },

  getTopTaskId: function(entityId) {
      var gr = new GlideRecord(this.PLANNED_TASK_TABLE);
      if ( gr.get(entityId) )
          return gr.getValue('top_task');
      else
          return '';
  },

  validate: function(entity, entityClassName, entityId, entityBaselineId) {
      var valid = false;
      if( !this.nil(entity) && !this.nil(entityClassName) && !this.nil(entityId)) { 
          valid = this.validateEntity(entity, entityClassName, entityId, entityBaselineId);
      }
      return valid;
  },

  validateEntity: function(entity, entityClassName, entityId, entityBaselineId) {
      if( entity == this.DEFAULT_PROJECT_ENTITY ) {
          return this.validateProject(entity, entityClassName, entityId, entityBaselineId);
      } else if( entity == this.DEFAULT_PORTFOLIO_ENTITY ) {
          return this.validatePortfolio(entity, entityClassName, entityId, entityBaselineId);
      } else if (entity == this.DEFAULT_PROGRAM_ENTITY) {
          return this.validateProgram(entityId);
      } else if ( entity == this.DEFAULT_RELEASE_ENTITY ) {
          return this.validateRelease(entityId);
      } else if ( entity == this.CUSTOM ) {
          return true;
      }
      return false;
  },
  
  validateProgram: function(entityId) {
      var gr = new GlideRecord(this.PROGRAM_TABLE);
      return gr.get(entityId);
  },
  
  validateProject: function(entity, entityClassName, entityId, entityBaselineId) {
      var plannedTaskGr = new GlideRecord(this.PLANNED_TASK_TABLE);
      if( plannedTaskGr.get(entityId) ) {
          // Verify If SysClassName == entityClassName
          var sysClassName = plannedTaskGr.getValue(this.SYS_CLASS_NAME);
          if( entityClassName == sysClassName ) {
              // Verify if entityBaselineId Exists
              if( this.nil( entityBaselineId ) ) {
                  return true;
              } else {
                  return this.validateBaselineId(entity, entityClassName, entityId, entityBaselineId);
              }
          }
      } 
      return false;
  },

  validateBaselineId: function(entity, entityClassName, entityId, entityBaselineId) {
      var baselineGr = new GlideRecord(this.PLANNED_TASK_BASELINE_TABLE);
      if( baselineGr.get(entityBaselineId) ) {
          if(  baselineGr.top_task == entityId &&
              baselineGr.top_task.sys_class_name == entityClassName ) {
              return true;
          }
      }
      return false;
  },

  validatePortfolio: function(entity, entityClassName, entityId, entityBaselineId) {
      var portfolioGr = new GlideRecord(this.PORTFOLIO_TABLE);
      if( portfolioGr.get(entityId) ) {
          // Verify If SysClassName == entityClassName
          var sysClassName = portfolioGr.getValue(this.SYS_CLASS_NAME);
          if( entityClassName == sysClassName ) {
              return true;
          }
      } 
      return false;
  },
  
  validateRelease: function(entityId) {
      var releaseGr = new GlideRecord(this.RELEASE_TABLE);
      if( releaseGr.get(entityId) ) {
          return true;
      } 
      return false;
  },
  
  validateConsoleId: function(consoleId) {
      var consoleGr = new GlideRecord(this.PLANNED_TASK_CUSTOM_CONSOLE_TABLE);
      if ( consoleGr.get(consoleId) )
          return true;
      else
          return false;
  },
  
  getEntityIdsForCustomConsole: function(consoleId) {
      var p = new PlannedTaskCustomConsole(consoleId);
      var matchingRecordIds = p.matchingRecordIds();
      var ids = [];
      for (var i = 0; i < matchingRecordIds.length; i++) {
          ids.push(matchingRecordIds[i].sys_id);
      }
      return ids;
  },
  
  getEntityClassNameForCustomConsole: function(consoleId) {
      var p = new PlannedTaskCustomConsole(consoleId);
      return p.table();       
  },

  saveEntityPreference: function(entity, entityClassName, entityId) {
      if( entity == this.DEFAULT_PROJECT_ENTITY ) {
          return this.saveProjectPreference(entityClassName, entityId);
      } else if( entity == this.DEFAULT_PORTFOLIO_ENTITY ) {
          return this.savePortfolioPreference(entityClassName, entityId);
      }
  },

  saveProjectPreference: function(entityClassName, entityId) {
      gs.getUser().savePreference(this.PROJECT_ENTITY_PREFIX + entityClassName, entityId);
  },

  savePortfolioPreference: function(entityClassName, entityId) {
      gs.getUser().savePreference(this.PORTFOLIO_ENTITY_PREFIX + entityClassName, entityId);
  },

  /* getProjectUrl: function(entity, entityClassName, entityId, entityBaselineId) {
      if(!entity, entityClassName, entityId, entityBaselineId) {
          return "gantt.do?sysparm_nostack=yes&sysparm_format_gantt=true&sysparm_entity=" + this.DEFAULT_PROJECT_ENTITY +
          "&sysparm_sys_class_name=" + this.DEFAULT_PROJECT_ENTITY_CLASS_NAME + "&sysparm_sys_id=";
      } else if( !this.nil(entity) && this.nil(entityClassName) &&
          !this.nil(entityId) && this.nil(entityBaselineId)  ){
         return "gantt.do?sysparm_nostack=yes&sysparm_format_gantt=true&sysparm_entity=" + entity +
          "&sysparm_sys_class_name=" + entityClassName + "&sysparm_sys_id=" + entityId;
      } else if( !this.nil(entity) && this.nil(entityClassName) &&
          !this.nil(entityId) && !this.nil(entityBaselineId) ) {
          return "gantt.do?sysparm_nostack=yes&sysparm_format_gantt=true&sysparm_entity=" + entity +
          "&sysparm_sys_class_name=" + entityClassName + "&sysparm_sys_id=" + entityId + 
          "" + entityBaselineId;
      }
  }, */

  getConsolePreference: function(consolePreferenceName, entityClassName) {
      var preferenceName;
      if ( JSUtil.nil(entityClassName) )
          preferenceName = this.CONSOLE_PREFERENCE_PREFIX + consolePreferenceName;
      else
          preferenceName = this.CONSOLE_PREFERENCE_PREFIX + entityClassName + "." + consolePreferenceName;
      
      var preferenceValue = gs.getUser().getPreference(preferenceName);
      return preferenceValue;
  },

  saveConsolePreference: function(entityClassName, consolePreferenceName, consolePreferenceValue) {
      gs.getUser().savePreference(this.CONSOLE_PREFERENCE_PREFIX + entityClassName + "." + consolePreferenceName, consolePreferenceValue);
  },

  consolePreferences: function(entityClassName) {
      var preferences = {
          showCriticalpath : this.getConsolePreference(entityClassName, 'SHOWCRITICALPATH'),
          showDateChange : this.getConsolePreference(entityClassName, 'SHOWDATECHANGE'),
          showDurationChange : this.getConsolePreference(entityClassName, 'SHOWDURATIONCHANGE'),
          deferSave : this.getConsolePreference(entityClassName, 'DEFERREDSAVE'),
          zoom : this.getConsolePreference(entityClassName, 'ZOOM'),
          columns : this.getConsolePreference(entityClassName, 'COLUMNS'),
          showWeekendShadow : this.getConsolePreference(entityClassName, 'SHOWWEEKENDSHADOW'),
          intro : this.getConsolePreference(entityClassName, 'INTRO'),
          fiscalYear : this.getConsolePreference(entityClassName, 'FISCALYEAR')
      };
      return preferences;
  },

  type: 'PlanningConsolePreferenceValidator'
};

Sys ID

9e7723a99f020200598a5bb0657fcff4

Offical Documentation

Official Docs: