TransformationDefinitionStep.js 11.2 KB
Ext.define('treps.controller.Steps.TransformationDefinitionStep', {
	extend: 'treps.controller.Steps.BaseStep',

	requires: [
		'treps.controller.Data.Store.DataStoreManager'
	],

	views: [
		'Steps.TransformationDefinition.TransformationDefinitionPanel'
	],

	refs: [
		{
			ref: 'vectorsGrid',
			selector: 'dataselect_container > container > vecdef_grid'
                },
		{
			ref: 'timePanel',
			selector: 'dataselect_container > container > panel'
		},
		{
			ref: 'framesPanel',
			selector: 'frames_panel'
		},
		{
			ref: 'footer',
			selector: 'footer_toolbar'
		}
        ],

	init: function() {
		var me = this;

		this.application.on(
			{
				editvector: me.onEditVector,
				scope: me
			}
                );

		this.application.on(
			{
				selectvector: me.onSelectVector,
				scope: me
			}
		);

		this.application.on(
			{
				resetvector: me.onResetVectorSelection,
				scope: me
			}
		);

	},
	
	onEditVector: function(vec)
	{
		var me = this;

		this.getVectorsGrid().setDisabled(true);
		this.getTimePanel().setDisabled(true);
		this.getFramesPanel().setDisabled(true);
		this.getFooter().setDisabled(true);

		var gridController = treps.app.getController('Data.Grid.SourceDataGrid');

		gridController.startVectorSelection(function (success, dataIndex1,dataIndex2,dataIndex3)
			{
				me.getVectorsGrid().setDisabled(false);
				me.getTimePanel().setDisabled(false);
				me.getFramesPanel().setDisabled(false);
				me.getFooter().setDisabled(false);

				if (success)
				{
					vec.set('comp_1',dataIndex1);
					vec.set('comp_2',dataIndex2);
					vec.set('comp_3',dataIndex3);
				}
			});
	},

	onSelectVector: function(vec)
	{
		var gridController = treps.app.getController('Data.Grid.SourceDataGrid');
		gridController.selectVector(vec.get('comp_1'),vec.get('comp_2'),vec.get('comp_3'));
	},

	onResetVectorSelection: function()
	{
		var gridController = treps.app.getController('Data.Grid.SourceDataGrid');
		gridController.resetVectorSelection();
	},

	onAfterLoad: function(store, records, successful, eOpts)
	{
		var gridController = treps.app.getController('Data.Grid.SourceDataGrid');

		gridController.setTotalInHeader(store.totalCount);
	},

	applySelectTime: function(enabled)
	{
		var val = false;

		var gridController   = treps.app.getController('Data.Grid.SourceDataGrid');
		var vecDefController = treps.app.getController('Vectors.VecDefGrid');

		if (enabled)
		{
			var dataGrid = gridController.getSourceGrid();
			var firstColumn = gridController.getFirstColumnDataIndex(dataGrid);
			val = !vecDefController.componentInUse(firstColumn);
			if (!val)
			{
				treps.Messages.showError("First column already in use to define a vector component. Cannot use it for time definition.");
				return false;
			}
		}

		gridController.selectTime(val);

		return val;
	},

	isValidStep: function() {
		 var framesDefController = treps.app.getController('Frames.FramesSelection');

		if (!framesDefController.isValid())
			return false;

		var timeDefController = treps.app.getController('Times.SourceTimeSelection');

		if (!timeDefController.isValid())
			return false;


		var framesDefController = treps.app.getController('Frames.FramesSelection');
		var noTrans = (framesDefController.getSrcFrame() == framesDefController.getDstFrame());
		
		var vecDefController    = treps.app.getController('Vectors.VecDefGrid');

		if (!vecDefController.isValid(noTrans))
			return false;

		treps.model.Session.set('step2_srcFrame',framesDefController.getSrcFrame());
		treps.model.Session.set('step2_dstFrame',framesDefController.getDstFrame());
		var lStore = vecDefController.getVectorsGrid().getStore();
		//create a copy of current store
		var lNewStore = Ext.create("treps.store.Vectors");
		lStore.each(function (vec) {
			console.log('****** Add store with '+lStore.getCount()+' vector(s) '+vec.get('comp_1')+','+vec.get('comp_2')+','+vec.get('comp_3'));
			var record = Ext.create("treps.model.Vector");
			record.set('comp_1',vec.get('comp_1'));
			record.set('comp_2',vec.get('comp_2'));
			record.set('comp_3',vec.get('comp_3'));
			record = lNewStore.add(record);
		});
		treps.model.Session.set('step2_vecs',lNewStore);

		return true;
	},

	callRequest: function(args, afterrequest)
	{
		var timeDefController   = treps.app.getController('Times.SourceTimeSelection');

		treps.LoadMask.showStatus(TREPSAction.getStatusOperation);
		TREPSAction.runOperation(args, function(result, event)
			{
				treps.LoadMask.hide();

				if (!result)
				{
					treps.Messages.showError("Internal error during runOperation request");
					return;
				}
				else if (!result.success)
				{
					treps.Messages.showError(result.error);
					return;
				}

				treps.model.Session.set('sourceTimeFormat',timeDefController.getTimeFormat());
				treps.model.Session.set('sourceTimePattern',timeDefController.getTimePattern());

				afterrequest.call();
			});
	},
	
	doRequest: function(afterrequest) {
		var me =this;

		var gridController      = treps.app.getController('Data.Grid.SourceDataGrid');
		var framesDefController = treps.app.getController('Frames.FramesSelection');
		var vecDefController    = treps.app.getController('Vectors.VecDefGrid');
		var timeDefController   = treps.app.getController('Times.SourceTimeSelection');
		timeDefController.init();

		var dataGrid = gridController.getSourceGrid();

		var args = {
			'id'           : treps.model.Session.get('id'),
			'srcframe'     : framesDefController.getSrcFrame(),
			'dstframe'     : framesDefController.getDstFrame(),
			'vectors'      : vecDefController.getVectorsStr(),
			'timeformatid' : timeDefController.getTimeFormat(),
			'timepattern'  : timeDefController.getTimePattern(),
			'timefieldid'  : gridController.getFirstColumnDataIndex(dataGrid)
		};

		var infoMsg = [];

		if ((args.srcframe == null) && (args.dstframe == null))
			infoMsg.push("No frames selected.");
		else
		{
			if  (args.srcframe == args.dstframe)
				infoMsg.push("Source and destination frames are identical.");

 			if ((args.srcframe != args.dstframe) && (args.timeformatid == ""))
				infoMsg.push("No time field selected. You must be sure that selected transformation don't need to use the time.");

			if (args.vectors == "")
				infoMsg.push("No vector defined. The full source data will be used during the next step.");
		}

		if (infoMsg.length == 0)
			this.callRequest(args,afterrequest);
		else
		{
			var questionMsg = "Please confirm your selection :<br/>";
			Ext.each(infoMsg, function(info)
				{
					questionMsg += (" * "+info+"<br/>");
				});
			questionMsg += "Do you want to continue?";
			treps.Messages.showQuestion(
				questionMsg,
				function ()
					{
						me.callRequest(args,afterrequest);
					}
			);
		}var session = treps.model.Session;

	},
	
	doInitStep: function() {
		var me = this;
                var session = treps.model.Session;

		//init source time info in session
		treps.model.Session.set('sourceTimeFormat','');
		treps.model.Session.set('sourceTimePattern','');

		//load associated controllers
		var gridController      = treps.app.getController('Data.Grid.SourceDataGrid');
		var vecDefController    = treps.app.getController('Vectors.VecDefGrid');
		var framesDefController = treps.app.getController('Frames.FramesSelection');
		var timeDefController   = treps.app.getController('Times.SourceTimeSelection');

		var dataStoreManager = treps.controller.Data.Store.DataStoreManager;

		//reset vectors
		vecDefController.clearStore();
		vecDefController.initRenderer();

		//Load frames list
		treps.LoadMask.show("Load frames list. Please wait ...");
		framesDefController.initStore(function (framesStore)
			{
				treps.LoadMask.hide();
				//load times list
				treps.LoadMask.show("Load times formats. Please wait ...");
				timeDefController.initStore(function (timesStore)
					{
						treps.LoadMask.hide();
						timeDefController.selectFirst();

						//get source data info, create source data store, and reconfigure the data drid
						treps.LoadMask.show("Get source data info. Please wait ...");
						TREPSAction.getSourceInfo({'id' : session.get('id')}, function(result, event)
							{
								treps.LoadMask.hide();

								if (!result)
								{
									treps.Messages.showError("Internal error during sourceInfo request");
									return;
								}
								else if (!result.success)
								{
									treps.Messages.showError(result.error,
										function()
										{
											me.application.fireEvent('initapp');
										}
									);
									return;
								}

								//create model fields
								var modelFields = dataStoreManager.createModelFields(result.info.fields);

								//create source data model
								dataStoreManager.defineModel(gridController.getDataModelName(),modelFields);

								//reconfigure the source data store
								var store = dataStoreManager.reconfigureStore(
									gridController.getDataStoreId(),
									gridController.getDataModelName(),
									TREPSAction.getSourceData,
									me.onAfterLoad
								);

								if (!store)
								{
									treps.Messages.showError("Error during source data store reconfiguration");
									return;
								}

								//reconfigure grid
								gridController.reconfigureDataGrid(result.info.nbRecords, result.info.fields);

								//set source Frame
								if(treps.model.Session.get('step2_srcFrame')!=null) {
									framesDefController.setSrcFrame(treps.model.Session.get('step2_srcFrame'));
								}else {
									if (result.info.frames.length > 1)
										treps.Messages.showInfo("More than one frame has been detected in the input file.");
									if (result.info.frames.length > 0)
										framesDefController.setSrcFrame(result.info.frames[0]);
								}

								//set dest Frame
								if(treps.model.Session.get('step2_dstFrame')!=null) {
									//console.log('****** set Dst frame'+treps.model.Session.get('step2_dstFrame'));
									framesDefController.getDestCombo().setValue(treps.model.Session.get('step2_dstFrame'));
								}

								//set grid order: TODO
								

								//set time
								timeDefController.setTime(gridController.extractTimeFormat(result.info.nbRecords,result.info.fields));

								//set Vector def
								if(treps.model.Session.get('step2_vecs')!=null) {
									var storeVecs = treps.model.Session.get('step2_vecs');
									console.log('****** get store vector '+(typeof storeVecs)+' '+storeVecs.getCount());
										storeVecs.each(function(vec) {
											console.log('****** set vector back to store '+vec
												+' '+vec.get('comp_1')+','+vec.get('comp_2')+','+vec.get('comp_3'));
											vecDefController.addVector(vec.get('comp_1'),vec.get('comp_2'),vec.get('comp_3'));
										}
										);
								} else {
									//add predefined vectors
									if (result.info.vectors != null)
										Ext.each(result.info.vectors, function(vector) {
											vecDefController.addVector(vector.comp_1,vector.comp_2,vector.comp_3);
										}
										);
								}

							});
					});
                        });
		
	}
});