PlotRequestObject.js 5.41 KB
/**
 * Project      : AMDA-NG
 * Name         : PlotRequestObject.js
 * @class   amdaPlotObj.PlotRequestObject
 * @extends amdaModel.AmdaTimeObject
 * @brief   Plot Request Business Object Definition 
 * @author  Benjamin Renard
 * @version $Id: PlotRequestObject.js benjamin $
 ******************************************************************************
 *    FT Id     :   Date   : Name - Description
 ******************************************************************************
 *	:           :21/07/2015: BRE  - file creation
 */
 
 		
Ext.define('amdaPlotObj.PlotRequestObject', {
	extend: 'amdaModel.AmdaTimeObject',
	idProperty: 'id',
	
	requires: [
	           'amdaPlotObj.PlotObjectConfig',
	           'amdaPlotObj.PlotTabObject'
	],
	
	fields : [
	          {name: 'id', type:'string'},
              {name: 'file-format', type: 'string'},
              {name: 'file-output', type: 'string'},
              {name: 'file-prefix', type: 'string'},
              {name: 'one-file-per-interval', type: 'boolean'},
              {name: 'last-plotted-tab', type: 'int', defaultValue: 0},
              {name: 'last-tab-id', type: 'int', defaultValue: 0},
	      {name: 'active-tab-id', type: 'int', defaultValue: 1}
    ],
    
    hasMany: {
    		model : 'amdaPlotObj.PlotTabObject',
    		name  : 'tabs',
    		associationKey:'tabs'
    },
    
    constructor: function(){
    	var me = this;
    	me.callParent(arguments);
        if ((arguments.length > 0) && arguments[0] && arguments[0].tabs)
        {
        	if (arguments[0].tabs)
        		me.loadTabs(arguments[0].tabs);
        }
        else
        {
        	//new object, set default fields values
        	me.setDefaultValues();
        	//New object, force the creation of the first tab
        	me.createNewTab();
        }
    },
    
    loadTabs: function(tabs)
    {
       this.tabs().loadData(tabs);
    },
    
    setDefaultValues: function()
    {
    	this.set('file-format', amdaPlotObj.PlotObjectConfig.defaultValues.file.format);
    	this.set('file-output', amdaPlotObj.PlotObjectConfig.defaultValues.file.output);
    	this.set('file-prefix', '');
    	this.set('one-file-per-interval', amdaPlotObj.PlotObjectConfig.defaultValues.file.oneFilePerInterval);
    	this.set('last-plotted-tab', 0);
    	this.set('name', '');
    },
    
    createNewTab: function() {
    	this.set('last-tab-id', this.get('last-tab-id') + 1);
	var data = {id: this.get('last-tab-id')};
	var applyDefault = true;
	if ((arguments.length > 0) && arguments[0]) {
		data = arguments[0];
		data['id'] = this.get('last-tab-id');
		applyDefault = false;
	}
    	var recs = this.tabs().add(data);
	if (applyDefault) {
    		recs[0].setDefaultValues();
	}
    	this.dirty = true;
	return recs[0];
    },
    
    removeTabById: function(tabId) {
    	//Retrieve tab record
    	var tabRecord = this.tabs().getById(tabId);
    	if (tabRecord == null)
    		return false;
    	this.tabs().remove(tabRecord);
    	this.dirty = true;
    	return true;
    },
    
    isDirty : function()
    {
    	if (this.get('id') == '')
    		return false;
    	
    	if (this.dirty)
    		return true;
    	
    	var d = false;
    	this.tabs().each(function (tab, index) {
    		if (tab.isDirty())
    			d = true;
    	});
    	return d;
    },
    
    getJsonValues : function(hasId) 
    {
    	var requestValues  = new Object();
    	
    	requestValues['nodeType'] = 'request';
    	
    	if (hasId) {
    		requestValues['id'] = this.get('id');
        }
    	
    	requestValues['leaf'] = true;
    	
    	requestValues['file-format'] = this.get('file-format');
    	requestValues['file-output'] = this.get('file-output');
    	requestValues['file-prefix'] = this.get('file-prefix');
    	requestValues['one-file-per-interval'] = this.get('one-file-per-interval');
    	requestValues['last-plotted-tab'] = this.get('last-plotted-tab');
    	requestValues['name'] = this.get('name');
		
    	requestValues['timesrc'] =  this.get('timesrc'); 
			
        // if there's at least one timeTable name into 'timeTables' collection
        if (this.get('timesrc') == amdaModel.AmdaTimeObject.inputTimeSrc[0] && this.get('timeTables') && this.get('timeTables').length){
            // get complete timeTables collection
            var timeTables = this.get('timeTables');	    
            // init an empty array for timeTables
            requestValues['timeTables'] = [];
            // for each interval record
            Ext.Array.each(timeTables, function(item, index, all){
            	if (!item.$className) {
            		requestValues['timeTables'][index] = {timeTableName : item.timeTableName, id : item.id};
            	}
            	// get Json simplified value 
            	else {
            		requestValues['timeTables'][index] = item.getJsonValues();
            	}
            });            
        } else {
				requestValues['startDate'] = this.get('startDate');	
				requestValues['stopDate'] = this.get('stopDate'); 
				requestValues['durationDay'] = this.get('durationDay');
				requestValues['durationHour'] = this.get('durationHour');
				requestValues['durationMin'] = this.get('durationMin');
				requestValues['durationSec'] = this.get('durationSec');
			}
    	
	requestValues['tabs'] = [];

	this.tabs().each(function (tab, index) {
		requestValues['tabs'][index] = tab.getJsonValues();
	});
			
	requestValues['active-tab-id'] = this.get('active-tab-id');
	requestValues['last-tab-id'] = this.get('last-tab-id');
		
	return requestValues;
    }
});