TransformationDefinitionStep.js 12.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
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 me = this;
		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 srcStore = treps.controller.Data.Store.DataStoreManager.getStore('SourceDataStore');
		var timeCol = gridController.getFirstColumnDataIndex(gridController.getSourceGrid());

		var dataGrid = gridController.getSourceGrid();

		 
		

		var srcFrame = framesDefController.getSrcFrame();
		var dstFrame = framesDefController.getDstFrame()
		var srcCenter = framesDefController.getSrcCenter();
		var dstCenter = framesDefController.getDstCenter();
		
		console.log('found srccenter: '+srcCenter+' / '+dstCenter);

		var infoMsg = [];

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

 			if ((srcFrame != dstFrame) && (timeDefController.getTimeFormat() == ""))
				infoMsg.push("No time field selected. You must be sure that the selected transformation does not need the time. Otherwise, it will be set to 01/01/1970.");

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

		//get unit scale in km
		vecDefController.getScale();

			
		var args = {
			'id'           : treps.model.Session.get('id'),
			'srcframe'     : srcFrame,
			'srccenter'    : srcCenter,
			'dstframe'     : dstFrame,
			'dstcenter'    : dstCenter,
			'starttime'    : srcStore.getAt(0).get(timeCol), 
			'stoptime'     : srcStore.getAt(srcStore.getCount()-1).get(timeCol),
			'vectors'      : vecDefController.getVectorsStr(),
			'timeformatid' : timeDefController.getTimeFormat(),
			'timepattern'  : timeDefController.getTimePattern(),
			'timefieldid'  : gridController.getFirstColumnDataIndex(dataGrid)
		};



		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 units list
		treps.LoadMask.show("Load units list. Please wait ...");
		vecDefController.initStore(function (unitsStore)
		{
			treps.LoadMask.hide();
			//not working
			//vecDefController.getUnitsCombo().selectedIndex = 3;
			vecDefController.selectDefaultUnit();//working
		});
		
		//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);
										}
										);
								}

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