DownloadNode.js 9.47 KB
/** 
 * Project  : AMDA-NG4
 * Name     : DownloadNode.js
 * @class   amdaModel.DownloadNode
 * @extends amdaModel.ExecutableNode
 * @brief   Basic Model of Node corresponding to a download request
 * @author  Myriam
 * @version $Id: DownloadNode.js 2949 2015-06-23 10:25:59Z elena $
 */
//TODO amdaModel.SingletonNode class ???
Ext.define('amdaModel.DownloadNode', {

    extend: 'Ext.data.Model',
    
    singleton: true,
        
	fields:[{name : 'downloadType', type : 'string'},
		{name: 'object', type: 'object'},
		{name: 'realLinkedNode', type: 'amdaModel.AmdaNode'},
		{name: 'moduleId', type: 'string', defaultValue:'down-win'},
		{name: 'nodeType', type: 'string', defaultValue: 'download'},
		{name: 'objectDataModel', type: 'string', defaultValue:'amdaModel.Download'},
		{name: 'jobNode', type: 'string', defaultValue: 'amdaModel.BkgJobNode'}
	], 
    
     isExecutable: function(){
        return true;
      },
      
//TODO MIXINS
      
      getObjectCallback : function(result,remoteEvent){  
        var t = remoteEvent.getTransaction();
        if (result) {            
             var paramObj = Ext.create(this.get('objectDataModel'), result);
            // set parameter into node
            this.set('object', paramObj);
	    var downObject = amdaModel.DownloadNode.decodeObject();
            // Edition	 ;
	      amdaModel.DownloadNode.set('object',Ext.create('amdaModel.Download',downObject));
	      amdaModel.DownloadNode.editInModule();              
        } 
        else {
            // EXCEPTION : parameter not found !?
            alert(t.action + "." + t.method + " : No parameter '"
                +this.get('name')+"' found!");
            //TODO: adding an error log
        }
      },
      
        editInModule : function () {
        	var me = this;
        	myDesktopApp.getLoadedModule(this.get('moduleId'), true, function (module) {
        		// If the node to edit is not already linked to this module

                if (module.getLinkedNode() != me) {
                	// set relative node into parameter Module
                	module.setLinkedNode(me);        	
                	}
                else {// the node to edit is already edited
                    //TODO: TBD : message to user
                    //Sol1: msg alert: "warning this node is already edited! If you want to get the original, please press the 'reset' button"->'OK'
                    //Sol2: msg with user choice: "warning this node is already edited! Would you confirm this action and lost your modification?"->'Confirm','Cancel'
                }
                // Opening parameter window         
                module.createWindow();
        	});
    },
    
      decodeObject: function(obj) {
	  var myValues  = new Object(); 
	  myValues.list=[];
	 
	  if (!obj) {
	      var fullObject = this.get('realLinkedNode').get('object');	      
	  }
	  else {
	    var fullObject = obj;
	  }
	  
	 var childrenArray = []; 
         if (fullObject.childNodes){
            // use internal node attribute of NodeInterface to get children
            Ext.Array.each(fullObject.childNodes, function(panelItem) {	                 
                childrenArray.push(panelItem.getJsonValues());
            });
	  } else {
	      // else if a json config has been past to the Plot constructor
	      childrenArray = fullObject.get('children');              
	  }
      
	  var panels = childrenArray;
	  var i = 0;	    
	  Ext.each(panels, function(item){	  
	      var params = item.children;
	      Ext.each(params,function(param){  
                if (!param.plotOnly) {  
                    if (param.downloadParamArgs){
                        myValues.list[i] = this.parseArgs(param);  
                    }
                    else {
                        myValues.list[i] = param.name;
                    }
                    i++;
                }
	    }, this);
	  }, this);
	  myValues.name = fullObject.get('name');
	  myValues.timesrc = fullObject.get('timesrc');
	  // if there's at least one timeTable name into 'timeTables' collection
	  if (myValues.timesrc == amdaModel.AmdaTimeObject.inputTimeSrc[0] 
		    && fullObject.get('timeTables') 
		    && fullObject.get('timeTables').length ){
	      // get complete timeTables collection
	      var timeTables =  fullObject.get('timeTables');	    
	      // init an empty array for timeTables
	      myValues.timeTables=[];
	      // for each interval record
	      Ext.Array.each(timeTables, function(item, index, all){
		if (!item.$className) {
		      myValues.timeTables[index] = {timeTableName : item.timeTableName, id : item.id};
		}
		  // get Json simplified value 
		else {
		  myValues.timeTables[index] = item.getJsonValues();
		}
	      });            
	  } else {
	      myValues.startDate = fullObject.get('startDate');
	      myValues.stopDate = fullObject.get('stopDate');
	      myValues.durationDay = fullObject.get('durationDay');
	      myValues.durationHour = fullObject.get('durationHour');
	      myValues.durationMin = fullObject.get('durationMin');
	      myValues.durationSec = fullObject.get('durationSec');
	  } 
	return myValues;
      },
      
      encodeObject: function() {
      },
      
/*
 * Method to parse special parameters cases with arguments (spectra && MODELS)
 * TODO to rewrite with amda new kernel
 */      
      parseArgs: function(param) {	
	 var name = param.name;
	 var args = param.downloadParamArgs;
	 if (args.energyMax){
	   var nameToSent = name + '_' + args.energyMin +  '_' + args.energyMax;
	  return nameToSent;
	 }
	 
	 if (args.PA && args.Energy){
	   var nameToSent = name + '_' + args.PA +  '_' + args.Energy;
	  return nameToSent;	    
	 }
	 
	 if (args.PA) {
	     var pa = args.PA - 1;
	     var nameToSent = name + '(' +  pa + ')';
	   return nameToSent;	 
	 }
	 
	 if (args.Energy) {
	    var en = args.Energy - 1;
//TODO temporary	    
	    if (name.substr(name.length-2, 2) == '_E') name = name.substr(0,name.length-2);
	    var nameToSent = name + '(' +  en + ')';
	   return nameToSent;	
	 } 
	 // T96; A2000
	 if (args.SW_Monitor) {
              var nameToSent = name + '_' + args.Spacecraft +  '_' + args.SW_Monitor;
              return nameToSent;    
         } 
         // Cain ; Morschhauser
         if (args.Spacecraft && args.Sampling) {
              var nameToSent = name + '_' + args.Spacecraft +  '_' + args.Sampling;
              return nameToSent;    
         }
         // LATMOS
         if (args.ClockAngle && args.Satellite) {
              var nameToSent = name + '_' + args.Satellite +  '_' + args.ClockAngle;
              return nameToSent;    
         }
         
         // FMI HYBRID
        if (args.Satellite && Ext.Object.getSize(args) === 1) {
            var nameToSent = name + '_' + args.Satellite;
            return nameToSent;    
        }
        
      },
      
  
    loadJobTree : function() {   
    	  //ToDo - BRE - fix code duplication with ExecutableNode if possible
	var rootNode = Ext.getCmp(amdaUI.ExplorerUI.JOB_TAB.TREE_ID).getRootNode();
	var me = this;
	amdaModel.InteractiveNode.preloadNodes(rootNode,
    		function()
            	{
					amdaModel.InteractiveNode.jobTreeLoaded = true;
					me.realExecute(); 
            	});
    },   
    	   
    execute : function() {
      
	if (!amdaModel.ExecutableNode.jobTreeLoaded) this.loadJobTree();
	else this.realExecute(); 
	   
    },
 
      
    /**
     * Method to execute this node 
     */
    realExecute : function() {

        var jsonObject = this.get('object').getJsonValues();
            if (!jsonObject.list && !jsonObject.images) {
		    myDesktopApp.warningMsg('Please select at least one Parameter (Get Data) or one Time Table (Get Time Table)');
        	return;
        }
	
	loadMask.show(); 
        AmdaAction.execute({nodeType : this.get('nodeType')}, jsonObject, function(res,e){
	
	   loadMask.hide();

	   //AKKA - Rework of the result treatment for the integration with the new kernel
	   if (!e.status)
	   {
		   myDesktopApp.errorMsg('Internal error during download request');
		   return;
	   }
	   
	   if (!res.success)
	   {
		   myDesktopApp.errorMsg(res.message);
		   return;
	   }
	   
       var newobj = this.createJobObject(res);
       var newNode = Ext.create(this.get('jobNode'), 
    		   {
    	   			id : res.id,
    	   			info : res.info,
    	   			jobType : this.get('nodeType'),
    	   			processId : res.id,
    	   			text : res.name,
    	   			status : res.status,
    	   			stop : res.stop,
    	   			leaf : true,
                    object : newobj});
         
 // new Tab
       switch (res.status)
       {
       case amdaModel.BkgJobNode.STATUS_LIST.DONE :
    	   // New tab, non-interactive session
    	   var isInteractive = false;
    	   var isNewTab = true; 
    	   newNode.createJobNode(true);
    	   newNode.editNode(isNewTab, isInteractive);
    	   break;
       case amdaModel.BkgJobNode.STATUS_LIST.IN_PROGRESS :
    	   newNode.createJobNode(false);
    	   break;
       default:
    	   newNode.createJobNode(true);
       }
        }, this );

},  

    createJobObject: function(res) {
      
		    var obj =  this.get('object').getJsonValues();		    
//TODO text, name, outputName - if all is needed        
	             //new object to attach to new bkgJobNode	

//TODO Ext.clone()	
		    
		    var newobj = Ext.copyTo({}, obj, this.get('object').propertiesToCopy);
	 	    		newobj.id        = res.id;	
                    newobj.resultId  = res.result;	
                    newobj.folderId  = res.folder;
                    newobj = Ext.create(this.get('object').$className, newobj);
		 return   newobj;  
	}

});