DownloadNode.js 8.15 KB
/** 
 * Project  : AMDA-NG4
 * Name     : DownloadNode.js
 * @class   amdaModel.DownloadNode
 * @extends Ext.data.Model
 * @brief   Basic Model of Node corresponding to a download request
 * @author  Myriam
 * @version $Id: DownloadNode.js 2949 2015-06-23 10:25:59Z elena $
 */

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;
	},
      
	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 {
			myDesktopApp.errorMsg(t.action + "." + t.method + " : No parameter '"
				+this.get('name')+"' found!");
			 // EXCEPTION : parameter not found !?			 
		}
	},
      
	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 i = 0;		
		fullObject.tabs().each(function (tab) {
			// only active tab			
			if (tab.get('id') == fullObject.get('last-plotted-tab'))
			{			 
				tab.panels().each(function (panel) {
					panel.params().each(function (param) {
						var myParam = new Object();
						myParam.paramid =  param.get('paramid');
						myParam.type =  param.get('type');
						myParam['dim1-is-range'] = param.get('dim1-is-range'),
						myParam['dim1-min-range'] =  param.get('dim1-min-range'),
						myParam['dim1-max-range'] =  param.get('dim1-max-range'),
						myParam['dim1-index'] = param.get('dim1-index'),
						myParam['dim2-is-range'] = param.get('dim2-is-range'),
						myParam['dim2-min-range'] =  param.get('dim2-min-range'),
						myParam['dim2-max-range'] =  param.get('dim2-max-range'),
						myParam['dim2-index'] = param.get('dim2-index'),
						myParam.template_args = param.get('template_args');
						if (!param.get('plotonly')) {
							myValues.list[i] = myParam;
							++i;
						}
						else 
							alert('Parameter '+ myParam.paramid + ' is PlotOnly');						
					});
				});				
				if (tab.get('multi-plot-linked'))
				{
					var object =  fullObject.data;
				}
				else 
				{
					var object =  tab.data;
				}				
				myValues.timesrc = object.timesrc;
				// if there's at least one timeTable name into 'timeTables' collection
				if (myValues.timesrc == amdaModel.AmdaTimeObject.inputTimeSrc[0]
					&& object.timeTables 
					&& object.timeTables.length ){
					// get complete timeTables collection
					var timeTables =  object.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 = object.startDate;
					myValues.stopDate = object.stopDate;
					myValues.durationDay = object.durationDay;
					myValues.durationHour = object.durationHour;
					myValues.durationMin = object.durationMin;
					myValues.durationSec = object.durationSec;
				}
			}	
		});
	  
		myValues.name = fullObject.get('name');
		return myValues;
	},
      
	encodeObject: function() {
	},
  
	loadJobTree : function(sendToSamp, clientId) {   
		//TBD - 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(sendToSamp, clientId); 
			});
	},   
    	   
	execute : function(sendToSamp, clientId) {
	
		if (!amdaModel.ExecutableNode.jobTreeLoaded) this.loadJobTree(sendToSamp, clientId);
		else this.realExecute(sendToSamp, clientId); 		
	},
       
/**
* Method to execute this node 
*/
	realExecute : function(sendToSamp, clientId) {
		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;
		}

		if (sendToSamp) {
			jsonObject.sendToSamp = true;
		}
	
		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;
			}
			//TBD if such condition is OK ?
			if (!res.id && res.download) {
			      if (res.compression) {					
					window.location.href = res.download;
				}
				else {
					window.open(res.download, '_blank');
				}
				return;
			}

			if (logExecTime && res.exectime && (res.exectime != 0)) {
				console.log("CMD EXEC TIME FOR "+res.id+" = "+res.exectime+"ms");
			}
			
			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 :
					if (!res.sendToSamp) {
						// New tab, non-interactive session
						var isInteractive = false;
						var isNewTab = true; 
						newNode.createJobNode(true);
						newNode.editNode(isNewTab, isInteractive);
					}
					else {
						var files = res.result.split(",");
						Ext.each(files, function(file) {
							var href = 'data/'+sessionID+'/RES/'+ res.folder + '/' + file;
							myDesktopApp.getLoadedModule(myDesktopApp.dynamicModules.interop.id, true, function (module) {
								module.sendVOTable(href, clientId ? clientId : 'hub');
							});
						});
					}
					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;  
	}
});