ExecutableNode.js 5.41 KB
/** 
 * Project  : AMDA-NG4
 * Name     : ExecutableNode.js
 * @class   amdaModel.ExecutableNode
 * @extends amdaModel.InteractiveNode
 * @brief   Generic Model of Executable Node
 * @author  elena
 * @version $Id: ExecutableNode.js 1662 2013-07-02 15:10:22Z benjamin $
 * @todo
 *******************************************************************************
 *    FT Id     :   Date   : Name - Description
 *******************************************************************************
 *  :           :12/07/2011: elena - creation
 * 
 */

Ext.define('amdaModel.ExecutableNode', {
    extend: 'amdaModel.InteractiveNode',
    
//TODO use resultModel in execute method Is it needed???
    
    fields: [ 'jobNode', 'resultModel'],

    statics: { jobTreeLoaded : false },
    
    constructor : function(config) {
        this.callParent(arguments);        
	 this.set('ownerTreeId',amdaUI.ExplorerUI.OPE_TAB.TREE_ID);
        this.set('jobNode', amdaModel.BkgJobNode.$className);		 
    },
    
    loadJobTree : function() {            
	var rootNode = Ext.getCmp(amdaUI.ExplorerUI.JOB_TAB.TREE_ID).getRootNode();	
	var me = this;
	amdaModel.InteractiveNode.preloadNodes(rootNode,
    		function()
            	{
					amdaModel.ExecutableNode.jobTreeLoaded = true;
					me.realExecute(); 
            	});
    },   
    	   
    execute : function(isDirty) {
      
	if (!amdaModel.ExecutableNode.jobTreeLoaded) this.loadJobTree();
	else this.realExecute(); 
	   
    },
   
    /**
     * Method to execute this node 
     */
    realExecute : function() {
	  
        var tabId = this.get('nodeType') == 'request' ? this.get('object').get('tabId') : null; 
       
	if (!loadMask.isMasked())
                    loadMask.show(tabId);
        
        AmdaAction.execute({nodeType : this.get('nodeType')}, this.get('object').getJsonValues(true), 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 request');
     		    return;
     	    }
     	   
     	    if (!res.success)
     	    {
     		    myDesktopApp.errorMsg(res.message);
     		    return;
     	    }
     	    
     	    var newobj = this.createJobObject(res);	    
     	    var newNode = Ext.create(this.get('jobNode'), 
     	    		{
     	    			info : res.info,
     	    			jobType : this.get('nodeType'),
     	    			processId : res.id,
     	    			id : res.id,
     	    			text : res.name,
     	    			status : res.status,
     	    			start : res.start,
     	    			stop : res.stop,
     	    			tabId : res.tabId,
     	    			leaf : true,
     	    			object : newobj});
     	    
     	    
     	    newNode.get('object').on('execute', function() {
                // Then call the node creation method
                this.execute(arguments);
            }, newNode);
     	    
     	    // new Tab
     	    switch (res.status)
     	    {
     	    case amdaModel.BkgJobNode.STATUS_LIST.DONE :
     	    	//New tab, non-interactive session
            	var isInteractive = false;
            	var isNewTab = true;
            	if (newobj.get('format') &&  newobj.get('format') == 'PNG')
            	{
            		isInteractive = true;
            		//TODO each new plot create new BkgJobNode => delete old!!! 			      
            		// plot window (1-5) 			       
            		//      newNode.set('tabId', this.get('object').get('tabId'));
	      
            		// if it is plot for TimeTable	      
            		if (res.tableName) {				 
            			newobj.set('intervalN', res.intervalN); 
            			newobj.set('totalN', res.totalN);
            			newobj.set('ttName', res.tableName); 
            		}  
            	}
            	
            	if (!isInteractive)
            		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.resultId = res.result;	
           newobj.folderId = res.folder;
           newobj.processId= res.id;
           newobj.tabId = res.tabId;
           newobj.name = res.name;
		   if (this.get('object').$className == 'amdaModel.Plot' && obj.format == 'PNG') {
		      if (obj.timesrc == amdaModel.AmdaTimeObject.inputTimeSrc[0]) {
			    newobj.intervalN = res.intervalN;
			    newobj.ttName = res.tableName;
			    newobj.totalN = res.totalN;
			    newobj.startDate = res.startDate;
			    newobj.stopDate = res.stopDate;
			  }
			  else {
			    newobj.stopDate = this.get('object').get('stopDate');
			  }
//TODO implement xmin/xmax, ymin/ymax for all panels	
			    newobj.xMin = res.children[0].xmin;
			    newobj.xMax = res.children[0].xmax;
		    }    
                    newobj = Ext.create(this.get('object').$className, newobj);
		 return   newobj;  
	},
    /**
     * override isExecutable to return true 
     */
    isExecutable: function(){
        return true;
    }

});