Statistic.js 3.17 KB
/**
 * Project      : AMDA-NG
 * Name         : Statistic.js 
 * Description  : Statistics Object Definition
 * @class amdaModel.Statistic
 * @extends amdaModel.AmdaTimeObject
 * @author  elena
 */

Ext.define('amdaModel.Statistic', {
	extend: 'amdaModel.AmdaTimeObject',
 
	requires: [
		"amdaModel.StatisticParam"
	],
   
	fields : [
		{name: 'type', type: 'string', defaultValue: 'Statistic'},
		{name: 'description', type: 'string'},
		{name: 'last_update', type: 'int', defaultValue: 0}
	],

	associations : [
		{
			type : 'hasMany',
			model : 'amdaModel.StatisticParam',
			name  : 'params'
		}
	],

	constructor: function(){
		var me = this;
		me.callParent(arguments);
		if ((arguments.length > 0) && arguments[0])
		{
			if (arguments[0].parameter)
				me.loadParams(arguments[0].parameter);
		}
		this.dirty = false;
	},

	loadParams: function(params)
	{
		/* Compatability mode */
		Ext.each(params, function(param, index) {
			if (param.hasOwnProperty('is-init')) {
				return;
			}
			params[index]['dim1-sum-type']  = param['dim1-is-range'] ? 1 : 0;
			params[index]['dim1-min-value'] = param['dim1-min-range'];
			params[index]['dim1-max-value'] = param['dim1-max-range'];
			params[index]['dim2-sum-type']  = param['dim2-is-range'] ? 1 : 0;
			params[index]['dim2-min-value'] = param['dim2-min-range'];
			params[index]['dim2-max-value'] = param['dim2-max-range'];
			params[index]['is-init'] = true;
		});
		this.params().loadData(params);
        },

	isDirty : function() {
		if (this.dirty)
			return true;

		var d = false;

		this.params().each(function (param, index) {
			if (param.dirty)
				d = true;
		});
		return d;
	},
    
	getJsonValues : function () 
	{
		var values  = new Object();	    
                values.nodeType =  'statistic';
                values.type = this.get('type');
                values.name = this.get('name');
		values.timesrc = this.get('timesrc');
		values.description = this.get('description');
        	
		// if there's at least one parameter
		values.parameter = [];
                this.params().each(function (param, index) {
			values.parameter[index] = param.getJsonValues();
		});
		
		if (values.timesrc == amdaModel.AmdaTimeObject.inputTimeSrc[0])
		{
			// get complete timeTables collection
			var timeTables = this.get('timeTables');	    
			// init an empty array for timeTables
			values.timeTables=[];
			// for each interval record
			Ext.Array.each(timeTables, function(item, index, all)
			{
				if (!item.$className) {
					values.timeTables[index] = {timeTableName : item.timeTableName, id : item.id};
				}
				// get Json simplified value 
				else {
					values.timeTables[index] = item.getJsonValues();
				}
			});            
		} else 
		{
			values.startDate =  Ext.Date.format(this.get('startDate'), 'Y-m-d\\TH:i:s.u');	
			values.stopDate =  Ext.Date.format(this.get('stopDate'), 'Y-m-d\\TH:i:s.u');	
			values.durationDay = this.get('durationDay');
			values.durationHour = this.get('durationHour');
			values.durationMin = this.get('durationMin');
			values.durationSec = this.get('durationSec');
                                                            values.durationMs = this.get('durationMs');
		} 
	 
		values.leaf = true;

		return values;
	}
});