Name

global.PatternExporter

Description

PatternExporter implements the business logic of pattern export process

Script

var PatternExporter = Class.create();
PatternExporter.prototype = {
  
  exporterlogger: {
      
      init: function() {
          this.exportlog = '';
      },
      
      log: function(logMessage) {
          var formattedMsg = 'LOG: ' + logMessage + '\n';
          this.exportlog = this.exportlog + formattedMsg; 
          gs.log(formattedMsg);
      },
      error: function(errorMessage){
          var formattedMsg = 'ERROR: ' + errorMessage + '\n';   
          this.exportlog = this.exportlog + formattedMsg; 
          gs.log(formattedMsg);
      },
      
      getLog: function(){
          return this.exportlog;
      }
      
  }, 
  
  initialize: function() {
      //get the tracker so we can send progress updates
      
      this.tracker = SNC.GlideExecutionTracker.getLastRunning();
  	this.exporterlogger.init();
      //Publish data to the cureent UpdateSet
      this.newUpdateSetName = 'PatternExport ' + new GlideDateTime().toString();
      this.publisher = new UpdateSetPublisher(this.newUpdateSetName, this.exporterlogger);
      this.isPartial = false;
      this.intervalPercent = Math.floor((100 / this.patternList.length) / 12);
      this.removeUpdateSet = false;
  	this.advancedOnly = false;
      this.completeFlag = true;
  },
  
  exportPatternBasic: function(patternListStr, currentMode) {
  	this.isPartial = currentMode;
  	var patternList = patternListStr.split(',');
      var basicExporter = new PatternDependecyExporter(patternList, this.publisher, this.tracker, this.intervalPercent, this.exporterlogger);
      try{
          basicExporter.exportData();
          this.publisher.splitUpdateSetScopes();
      }catch(error){
          this.exporterlogger.error('Fail to export due to:' + error);
          this.exportComplete(false);
          if(this.removeUpdateSet === true)
              UpdateSetPublisher.deleteAllUpdateSets(this.publisher.getUpdateSetId());
      }
      
      if (this.isPartial == undefined || this.isPartial == false) {
          this.exportComplete(true);
          if(this.removeUpdateSet === true)
              UpdateSetPublisher.deleteAllUpdateSets(this.publisher.getUpdateSetId());
      }
          
      
  },
  
  exportPatternAdvanced: function(patternListStr, options) {
      this.isPartial = true;
  	var patternList = patternListStr.split(',');
      this.intervalPercent = Math.floor((100 / patternList.length) / 29);
      var cmdbExporter = new PatternCMDBExporter(patternList, this.publisher, this.tracker, this.intervalPercent, this.exporterlogger);
      try{
          cmdbExporter.exportData(options);
          this.publisher.splitUpdateSetScopes();
      }catch(error){
          this.exporterlogger.error('Fail to export due to:' + error);
          this.exportComplete(false);
          if(this.removeUpdateSet == true)
              UpdateSetPublisher.deleteAllUpdateSets(this.publisher.getUpdateSetId());
      }
      if (this.advancedOnly === false)
  		this.exportPatternBasic(patternListStr, true);
 
      this.exportComplete(true);
      if(this.removeUpdateSet === true)
          UpdateSetPublisher.deleteAllUpdateSets(this.publisher.getUpdateSetId());
  },
  
  exportComplete: function(isSuccess){
      this.exporterlogger.log('exportComplete');        
      if (isSuccess){ 
          this.publisher.complete();
          this.exporterlogger.log('UpdateSet id is: ' + this.publisher.getUpdateSetId());
          var gr = GlideRecord('sys_update_set');
          gr.addQuery("sys_id", this.publisher.getUpdateSetId());
          gr.addQuery("state", "complete");
          gr.query();
          
          var updateSetExport = new UpdateSetExport();
          if(gr.hasNext()) {
              gr.next();
              var currentGr = gr;
              this.exporterlogger.log('Exporting UpdateSet: ' + this.newUpdateSetName + ' to file');
              this.exportSysId = updateSetExport.exportHierarchy(currentGr); 
              this.exporterlogger.log('Export XML sys_id: ' + this.exportSysId);
              var self = this;
              this.tracker.success('Export success');
              this.tracker.updateResult(
                  {
                      exportResult: self.exporterlogger.getLog(),
                      exportSysId: self.exportSysId,
                      updateSetId: self.publisher.getUpdateSetId()
                  });

          }else{
              this.exporterlogger.error('Exporting UpdateSet: ' + this.newUpdateSetName + ' to file failed');
              this.tracker.fail("Export failed");  
          }
          
      }     
      else{
          this.tracker.fail("Export failed");  
          this.tracker.updateResult(
              {
                  exportResult: this.exporterlogger.getLog(),
              });
      }
      
      gs.log(this.exporterlogger.getLog());
      this.publisher.accomplish();
               
  },
  
  /*
  This flag added for testing purpose 
  By setting this flag to false we are not removing the created UpdateSet after export is completed
  */
  setRemoveUpdateSet: function(removeUpdateSet){
      this.removeUpdateSet = removeUpdateSet;
  },
  
  /*
  This flag added for testing purpose
  By setting this flag to true we are not exporting the data that included in Basic Export
  */
  setAdvancedOnly: function(advancedOnly){
      this.advancedOnly = advancedOnly;
  },
  
  type: 'PatternExporter'
};

Sys ID

2f4230e1938203008a2e35bb357ffb50

Offical Documentation

Official Docs: