TimeTable.js 6.23 KB
/**
 * Project      : AMDA-NG
 * Name         : timeTable.js
 * Description  : TimeTable Business Object Definition
 * @class amdaModel.TimeTable
 * @extends amdaModel.AmdaObject
 *
 * @author cdarmon
 * @version $Id: TimeTable.js 1907 2013-11-25 15:59:42Z myriam $
 ******************************************************************************
 *    FT Id     :   Date   : Name - Description
 ******************************************************************************
 *	:           :24/03/2011: CDA – creation
 *      :           :22/06/2011: elena - extends generic class AmdaObject
 */


Ext.define('amdaModel.Interval', {
    extend: 'amdaModel.AmdaObject',
    fields: [
        {
            name: 'id',
            convert: function(value, rec){
                // generate id only if start and stop fields exists
                return rec.get('start')&rec.get('stop')?Ext.id():null;
            },
            persist: false
        },
        { name: 'start', type : 'date', defaultValue : new Date(),
	  convert: function(value,rec) {
		   if (!Ext.isDate(value)){
                                                return new Date(value);
		   }
		   return value;
		   }
	},
        { name: 'stop', type : 'date', defaultValue : new Date(),
	  convert: function(value,rec) {
		     if (!Ext.isDate(value)){
                                                return new Date(value);
		   }
		   return value;
		   }
	},
         {
            name: 'durationDay',
            type: 'float',
            convert: function(value, rec){
                if (rec.get('stop') && rec.get('start') && (rec.get('stop')-rec.get('start'))>=0) {
                    return (rec.get('stop') - rec.get('start'))/3600000.0/24.0;
                }
            },
            persist: false
        },
        {
            name: 'durationHour',
            type: 'float',
            convert: function(value, rec){
                if (rec.get('stop') && rec.get('start') && (rec.get('stop')-rec.get('start'))>=0) {
                    return (rec.get('stop') - rec.get('start'))/3600000.0;
                }
            },
            persist: false
        },
        {
            name: 'durationMin',
            type: 'float',
            convert: function(value, rec){
                if (rec.get('stop') && rec.get('start') && (rec.get('stop')-rec.get('start'))>=0) {
                    return (rec.get('stop') - rec.get('start'))/60000.0;
                }
            },
            persist: false
        },
        {
            name: 'durationSec',
            type: 'float',
            convert: function(value, rec){
                if (rec.get('stop') && rec.get('start') && (rec.get('stop')-rec.get('start'))>=0) {
                    return (rec.get('stop') - rec.get('start'))/1000.0;
                }
            },
            persist: false
        },
        { name: 'cacheId', type : 'int'},
        { name: 'isNew', type : 'boolean', defaultValue: false},
        { name: 'isModified', type : 'boolean', defaultValue: false}
    ],

    getJsonValues : function () {
        var values  = new Object();
        values.start = this.get('start');
        values.stop = this.get('stop');
        return values;
    },

    proxy: {
        type: 'direct',
        api :
        {
              read   :  AmdaAction.readCacheIntervals
        },
        extraParams : {'typeTT' : ''},
        reader:
        {
          type: 'json',
          root: 'intervals',
          totalProperty : 'totalCount'
        }
    }

 /*
    validations: [
        {type: 'presence',  field: 'age'},
        {type: 'length',    field: 'name',     min: 2},
        {type: 'inclusion', field: 'gender',   list: ['Male', 'Female']},
        {type: 'exclusion', field: 'username', list: ['Admin', 'Operator']},
        {type: 'format',    field: 'username', matcher: /([a-z]+)[0-9]{2,3}/}
    ],
*/

});


Ext.define('amdaModel.TimeTable', {

    extend: 'amdaModel.AmdaObject',

    fields : [
            {name: 'fromPlugin', type: 'boolean', defaultValue: false},
            {name: 'created', type: 'date'},
            {name: 'description', type: 'string'},
            {name: 'contact', type: 'string', defaultValue:sessionID},
            {name: 'surveyStart', type: 'date'},
             {name: 'surveyStop', type: 'date'},
            {name: 'history', type: 'string'},
            {name: 'nbIntervals', type: 'int'},
            {name: 'objName', type: 'string', defaultValue: ""},
            {name: 'objFormat', type: 'string', defaultValue: ""},
            {name: 'folderId', type: 'string', defaultValue: ""},
            {name: 'cacheToken', type: 'string', defaultValue: ""},
            {name: 'relatedCatalogId', type: 'string', defaultValue: ""}
	],

	/*commit: function(){
	    this.callParent(arguments);

	    // fix the modifications for its intervals
	    Ext.Array.each(this.get('intervals'), function(item, index, all) {
//TODO check if JSON object should be processed
	       if (item.$className) {
	                   item.commit();
	       }
        });

	},*/

//	hasMany: {model: 'amdaModel.Interval', name: 'intervals'},

    /**
     * get TimeTable values to sent to Server
     * @return object
     */
	//TODO utf8_encode() and Ext.urlEncode() ???
    getJsonValues : function (hasId) {
    	var values  = new Object();
    	if (hasId) {
    		values.id = this.get('id');
    	}
    	values.name = this.get('name');
    	values.created = this.get('created');
    	if (this.get('description').match(/[a-z,0-9]/gi) != null) {
    		values.description = this.get('description');
    	}
    	if (this.get('history').match(/[a-z,0-9]/gi) != null) {
    		values.history = this.get('history');
    	}
    	values.objName = this.get('objName');
    	values.objFormat = this.get('objFormat');
                    values.folderId = this.get('folderId');
    	values.nbIntervals = this.get('nbIntervals');
    	values.cacheToken = this.get('cacheToken');

    	values.leaf = true;
    	values.nodeType = amdaModel.TimeTableNode.nodeType;
        
                   if (this.get('contact').match(/[a-z,0-9]/gi) != null) {
    		values.contact = this.get('contact');
    	}
                    values.surveyStart = this.get('surveyStart');
                    values.surveyStop = this.get('surveyStop');                
    	return values;
    }

});