Blame view

php/WebServices/WebServer.php 20.6 KB
16035364   Benjamin Renard   First commit
1
<?php
b8502f4d   Elena.Budnik   getStatus(), thro...
2
3
4
5
/** 
*   @file WebServer.php
*   @brief  Web services AMDA
*/
16035364   Benjamin Renard   First commit
6
7
8

class WebServer
{
b8502f4d   Elena.Budnik   getStatus(), thro...
9
	private $isSoap = false;
1ba8d04c   Elena.Budnik   time tables list,...
10
	private $userID, $userPWD = null, $sessionID = null, $IPclient;
b8502f4d   Elena.Budnik   getStatus(), thro...
11
	private $dataFileName;
8385c27c   Elena.Budnik   interim commit
12
13
	private $requestManager = null;
	private $paramLoader = null;
4bc7749a   Elena.Budnik   getParameter (sim...
14
	private $service;
1ba8d04c   Elena.Budnik   time tables list,...
15
	private $requestTime;
8a92ee19   Elena.Budnik   timeTobatch speci...
16
	
b8502f4d   Elena.Budnik   getStatus(), thro...
17
18
	function __construct() 
	{
a55dc57a   Elena.Budnik   interim commit
19
		if (!is_dir(WSConfigClass::getWsResultDir())) mkdir(WSConfigClass::getWsResultDir(), 0775);
b8502f4d   Elena.Budnik   getStatus(), thro...
20
	}
b8502f4d   Elena.Budnik   getStatus(), thro...
21

8385c27c   Elena.Budnik   interim commit
22
	protected function init($data) 
b8502f4d   Elena.Budnik   getStatus(), thro...
23
	{
1ba8d04c   Elena.Budnik   time tables list,...
24
25
26
27
28
29
30
		$this->requestTime = date('Ymd',time());
		
		if (!isset($data)) {
			$this->userID  = 'impex';
			return array('success' => true);
		}
		
b8502f4d   Elena.Budnik   getStatus(), thro...
31
32
33
34
35
36
37
		if(is_object($data)){
			$vars = get_object_vars($data);
			$this->isSoap = true; 
		}
		else {
			$vars = $data;
		}
8f6112a7   Nathanael Jourdane   Reformat webserver
38

b8502f4d   Elena.Budnik   getStatus(), thro...
39
40
41
42
43
44
45
46
47
48
49
50
51
		if (isset($vars['userID'])){
			$this->userID  = $vars['userID'];
		}
		else {
			$this->userID  = 'impex';
		}
		
		$this->sessionID = $this->userID;
		
		if (isset($vars['password']))
			$this->userPWD = $vars['password'];
		else 
			$this->userPWD = 'impexfp7';
8a92ee19   Elena.Budnik   timeTobatch speci...
52
		
b8502f4d   Elena.Budnik   getStatus(), thro...
53
54
55
		return array('success' => true, 'vars' => $vars);
	}
	
1ba8d04c   Elena.Budnik   time tables list,...
56
57
58
59
60
61
62
63
64
65
	private function initUserMgr($setPatOnly = false) 
	{
		$wsUserMgr = new WSUserMgr();
		$wsUserMgr->init($this->userID, $this->userPWD, $this->sessionID,  $setPatOnly, $this->isSoap);
		
		$this->IPclient = $wsUserMgr->getIPClient();
		
		return array('success' => true);
	}
	
b8502f4d   Elena.Budnik   getStatus(), thro...
66
67
68
69
70
71
72
73
	private function throwError($errorType, $msg)
	{
		if ($this->isSoap) 
			throw new SoapFault($errorType, $msg); 
		else 
			return array("error" => $msg);
	}
	
b8194303   Elena.Budnik   getPlot final
74
75
76
77
78
	private function isGetPlotRequest($name){

		return (substr($name,0,7) == 'getplot');
	}
	
cd1dd332   Elena.Budnik   getTimeTable
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
	private function xsl2vot($inputName, $outputName) 
	{   
		// Load Time table
		$xml = new DomDocument("1.0");
		if (!@$xml->load($inputName))
			$this->throwError("wokrspaceError", "Cannot load time table $inputName for ".$this->userID);

		// Load XSL file
		$xsl = new DomDocument("1.0");
		if (!@$xsl->load(WSConfigClass::getXslDir()."xml2vot.xsl"))
			$this->throwError("systemError", "Cannot load xsl file");
	
		// Import XSL and write output file in vot format
		$xslt = new XSLTProcessor();
		$xslt->importStylesheet($xsl);
		$vot = new DomDocument("1.0");
		if (!@$vot->loadXML($xslt->transformToXML($xml)))
				$this->throwError("systemError", "Cannot convert time table to VOtable");
		
		if (!$vot->save(WSConfigClass::getWsResultDir().$outputName))
			$this->throwError("systemError", "Cannot save time table to result dir");
	}
8f6112a7   Nathanael Jourdane   Reformat webserver
101

1922ad04   Elena.Budnik   getOrbites()
102
103
104
105
106
107
108
109
110
111
112
113
	private function getDatasetInfo($id)
	{
		$dataSetXml = WSConfigClass::getDataSetInfoDir().$id.".xml";
      
		if (!file_exists($dataSetXml))
			$this->throwError("systemError", "Cannot find info file for dataset ".$id); 
			
		$dataSetDom = new DomDocument("1.0");
		
		if (!@$dataSetDom->load($dataSetXml))
			$this->throwError("systemError", "Cannot load info file for dataset ".$id); 
			
b84e2792   Elena.Budnik   getdataSetInfo re...
114
		return $dataSetDom;
1922ad04   Elena.Budnik   getOrbites()
115
116
	}
	
1ba8d04c   Elena.Budnik   time tables list,...
117
118
119
120
/*
*  get user TimeTables list; Shared for impex
*/
	private function getTimeTablesCatalogsList($object) 
b8502f4d   Elena.Budnik   getStatus(), thro...
121
	{
1ba8d04c   Elena.Budnik   time tables list,...
122
		$this->initUserMgr(true);
b8502f4d   Elena.Budnik   getStatus(), thro...
123
		$dom = new DOMDocument("1.0");
1ba8d04c   Elena.Budnik   time tables list,...
124
		
b8502f4d   Elena.Budnik   getStatus(), thro...
125
126
127
		if ($this->userID == 'impex') {
			$sharedObjMgr = new SharedObjectsMgr();
			if (!@$dom->load($sharedObjMgr->getTreeFilePath()))
1ba8d04c   Elena.Budnik   time tables list,...
128
					$this->throwError("workspaceError", "Workspace Error : Cannot load Shared TimeTable list");
b8502f4d   Elena.Budnik   getStatus(), thro...
129
130
		}
		else {
1ba8d04c   Elena.Budnik   time tables list,...
131
132
				if (!@$dom->load(USERWSDIR.'Tt.xml'))
					$this->throwError("workspaceError", "Workspace Error : Cannot load TimeTable list for ".$this->userID);
b8502f4d   Elena.Budnik   getStatus(), thro...
133
134
		}
    
1ba8d04c   Elena.Budnik   time tables list,...
135
136
		$tagName = $object == "timetables" ? "timetabList" : "catalogList";
		$timetabNode = $dom->getElementsByTagName($tagName);
8f6112a7   Nathanael Jourdane   Reformat webserver
137

b8502f4d   Elena.Budnik   getStatus(), thro...
138
		if ($timetabNode->length < 1){
1ba8d04c   Elena.Budnik   time tables list,...
139
				$this->throwError("workspaceWarning", "Workspace Warning : No $object");
b8502f4d   Elena.Budnik   getStatus(), thro...
140
141
142
143
144
145
146
147
		}
		
		$outDOM = new DOMDocument("1.0");
		$outDOM->formatOutput = TRUE;
		$outDOM->preserveWhiteSpace = FALSE;
    
		$newNode = $outDOM->importNode($timetabNode->item(0),TRUE);
		$outDOM->appendChild($newNode);
1ba8d04c   Elena.Budnik   time tables list,...
148
149
150
151
152
		
		$ttListResult = $object.'_'.$this->userID.'_'.$this->requestTime.'.xml';
		
		if (!$outDOM->save(WSConfigClass::getWsResultDir().$ttListResult))
			$this->throwError("workspaceError", "Workspace Error : problem while saving $object list file");
8f6112a7   Nathanael Jourdane   Reformat webserver
153

1ba8d04c   Elena.Budnik   time tables list,...
154
		return WSConfigClass::getUrl().$ttListResult;
b8502f4d   Elena.Budnik   getStatus(), thro...
155
	}
1922ad04   Elena.Budnik   getOrbites()
156

b8502f4d   Elena.Budnik   getStatus(), thro...
157
/*
1922ad04   Elena.Budnik   getOrbites()
158
159
160
*  Get corresponding orbit parameter ID
*/
	private function getOrbitParameter($orbitRequest) 
b8502f4d   Elena.Budnik   getStatus(), thro...
161
	{
1922ad04   Elena.Budnik   getOrbites()
162
163
164
165
		if (!file_exists(WSConfigClass::getOrbitsXml()))
			$this->throwError('systemError', "No AMDA system orbits file");
			
		$orbitsXml = new DomDocument();
b8502f4d   Elena.Budnik   getStatus(), thro...
166

1922ad04   Elena.Budnik   getOrbites()
167
168
169
170
171
		if (!@$orbitsXml->load(WSConfigClass::getOrbitsXml()))
			$this->throwError('systemError', "Cannot load AMDA system orbits file");
			
		$spacecraft = strtolower($orbitRequest['spacecraft']);
		$spacecraft = str_replace('-', '', $spacecraft);
b8502f4d   Elena.Budnik   getStatus(), thro...
172
		
1922ad04   Elena.Budnik   getOrbites()
173
174
		$xpath = new DOMXpath($orbitsXml);
		$path = '//orbites[@mission="'.$spacecraft.'" and @coordinate_system="'.$orbitRequest['coordinateSystem'].'" and @units="'.$orbitRequest['units'].'" ] ';
8f6112a7   Nathanael Jourdane   Reformat webserver
175

1922ad04   Elena.Budnik   getOrbites()
176
		$orbits = $xpath->query($path);
b8502f4d   Elena.Budnik   getStatus(), thro...
177
		
1922ad04   Elena.Budnik   getOrbites()
178
179
180
181
182
183
184
		foreach ($orbits as $orbit)
		{
			$datasetID = strtr($orbit->getAttribute('dataset'),"_","-");
			$dataSetDom = $this->getDatasetInfo($datasetID); 
  
			$paramStart = strtotime($dataSetDom->getElementsByTagName('global_start')->item(0)->nodeValue);
			$paramStop =  strtotime($dataSetDom->getElementsByTagName('global_stop')->item(0)->nodeValue);
b8502f4d   Elena.Budnik   getStatus(), thro...
185

1922ad04   Elena.Budnik   getOrbites()
186
			if(($paramStart <= strtotime($orbitRequest['startTime']) && (strtotime($orbitRequest['stopTime'])) <= $paramStop)) { 
8f6112a7   Nathanael Jourdane   Reformat webserver
187

1922ad04   Elena.Budnik   getOrbites()
188
189
190
				return array('success' => true, 
								 'parameterID' => $orbit->getAttribute('xml:id')   
				);
b8502f4d   Elena.Budnik   getStatus(), thro...
191
			}
b8502f4d   Elena.Budnik   getStatus(), thro...
192
		}
1922ad04   Elena.Budnik   getOrbites()
193
194
195
		
		$this->throwError('systemError', 
		"Cannot find orbit data for ".$orbitRequest['spacecraft']." for ".$orbitRequest['startTime']."-".$orbitRequest['stopTime']." in ".$orbitRequest['units']."  ".$orbitRequest['coordinateSystem']."($paramStart  - $paramStop)");
b8502f4d   Elena.Budnik   getStatus(), thro...
196
	}
1922ad04   Elena.Budnik   getOrbites()
197
198
	
	private function doDownloadRequest($interval, $paramList, $formatInfo) 
b8502f4d   Elena.Budnik   getStatus(), thro...
199
	{
b8502f4d   Elena.Budnik   getStatus(), thro...
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
		if (!isset($this->paramLoader))
			$this->paramLoader = new IHMUserParamLoaderClass();

		//Build parameter list
		$params = array();
		
		//TODO template arguments to implement ?
		foreach ($paramList['params'] as $paramId)
		{
			$param = new stdClass;
			
			if (preg_match("#^ws_#",$paramId))
			{
				$res = $this->paramLoader->getDerivedParameterNameFromId($paramId);
				
				if (!$res["success"]) {
					$this->throwError("serverError", "Not available derived parameter $paramId");
				}
				$param->paramid = "ws_".$res['name'];
			}
			else if (preg_match("#^wsd_#",$paramId))
			{
				$res = $this->paramLoader->getUploadedParameterNameFromId($paramId);
				
				if (!$res["success"]){
					$this->throwError("serverError", "Not available parameter $paramId");
				}
				$param->paramid = "wsd_".$res['name'];
			}
4bdd7ad9   Elena.Budnik   interim commit
229
			else {
b8502f4d   Elena.Budnik   getStatus(), thro...
230
231
				$param->paramid = $paramId;
			}
b8502f4d   Elena.Budnik   getStatus(), thro...
232
233
			$params[] = $param;
		}
8385c27c   Elena.Budnik   interim commit
234

b8502f4d   Elena.Budnik   getStatus(), thro...
235
		$obj = (object)array(
8a92ee19   Elena.Budnik   timeTobatch speci...
236
						"sampling" => $interval['sampling'],
8a92ee19   Elena.Budnik   timeTobatch speci...
237
238
239
						"startDate" => $interval['startTime'],
						"stopDate" => $interval['stopTime'],
						"list" => $params,
4bdd7ad9   Elena.Budnik   interim commit
240
241
242
						"fileformat" => $formatInfo['format'],
						"timeformat" => $formatInfo['timeFormat'],
						"compression" => $formatInfo['gzip']
8385c27c   Elena.Budnik   interim commit
243
244
			);	
			
b8502f4d   Elena.Budnik   getStatus(), thro...
245
246
		if (!isset($this->requestManager))
			$this->requestManager = new RequestManagerClass();
8385c27c   Elena.Budnik   interim commit
247
		 
b8502f4d   Elena.Budnik   getStatus(), thro...
248
		try {
4bc7749a   Elena.Budnik   getParameter (sim...
249
			$downloadResult = $this->requestManager->runWSRequest($this->userID, $this->IPclient, FunctionTypeEnumClass::PARAMS, $this->service, $obj);
b8502f4d   Elena.Budnik   getStatus(), thro...
250
		} catch (Exception $e) {
8385c27c   Elena.Budnik   interim commit
251
			$this->throwError("executionError", "Exception detected : ".$e->getMessage()); 
b8502f4d   Elena.Budnik   getStatus(), thro...
252
253
254
255
256
257
258
		}
			
		if (!$downloadResult['success']) {
			$this->throwError("serverError", $downloadResult['message']);
		}
		
		if($downloadResult['status'] == 'in_progress') {
8385c27c   Elena.Budnik   interim commit
259
260
261
			return ['success' => true, 'status' => 'in progress', 'id' => $downloadResult['id']];
		} elseif ($downloadResult['status'] == 'done') 
		{
4bc7749a   Elena.Budnik   getParameter (sim...
262
			$this->deleteProcess($downloadResult['id']);
4bdd7ad9   Elena.Budnik   interim commit
263
			return array('success' => true, 'status' => 'done', 'dataFileURLs' => WSConfigClass::getUrl().$downloadResult['result']);
b8502f4d   Elena.Budnik   getStatus(), thro...
264
265
266
267
268
		} else {
			return ['success' => false, 'message' => 'Unknown status ' . $downloadResult['status']];
		} 
	}
	
4bc7749a   Elena.Budnik   getParameter (sim...
269
/*
2b26bbdd   Elena.Budnik   getPlot partial c...
270
*         delete process after execution : 
4bc7749a   Elena.Budnik   getParameter (sim...
271
272
273
274
*         delete temporary files/folders ( JOBS/process_xxxx, RES/DDxxx ); 
*         delete job node in processManager.xml
*/
	private function deleteProcess($id)
b8502f4d   Elena.Budnik   getStatus(), thro...
275
	{
4bdd7ad9   Elena.Budnik   interim commit
276
		$obj = (object)array('id' => $id);
b8502f4d   Elena.Budnik   getStatus(), thro...
277
278
279
280
   
		if (!isset($this->requestManager))
			$this->requestManager = new RequestManagerClass();

4bdd7ad9   Elena.Budnik   interim commit
281
		try {
4bc7749a   Elena.Budnik   getParameter (sim...
282
			$downloadResult = $this->requestManager->runWSRequest($this->userID, $this->IPclient, FunctionTypeEnumClass::PROCESSDELETE, null, $obj);
4bdd7ad9   Elena.Budnik   interim commit
283
		} catch (Exception $e) {
4bc7749a   Elena.Budnik   getParameter (sim...
284
			$this->throwError("deleteProcessError", $e->getMessage());
4bdd7ad9   Elena.Budnik   interim commit
285
		}
b8502f4d   Elena.Budnik   getStatus(), thro...
286
	}
8a92ee19   Elena.Budnik   timeTobatch speci...
287

2b26bbdd   Elena.Budnik   getPlot partial c...
288
289
290
291
292
293
294
295
296
297
298
/*
*   generate AUTH token for access to REST services 
*/
	public function getNewToken()
	{
		// generate token from timeStamp and some salt
		$newToken = md5(1321 * (int)( time() / WSConfigClass::$timeLimitQuery));
		
		return array('success' => true, 'token' => $newToken);
	}

1ba8d04c   Elena.Budnik   time tables list,...
299
/************************** WEB SERVICES **************************************/	
cd1dd332   Elena.Budnik   getTimeTable
300
301
302
303
304
305
306
307
308
309

/*
*   public data only : user impex
*/
	public function getObsDataTree() 
	{         
		$res = $this->init();	
		$this->initUserMgr();
		
		$locParamSrc = USERWSDIR.'LocalParams.xml'; 
1922ad04   Elena.Budnik   getOrbites()
310
		$locParamDst = substr(strtolower(__FUNCTION__),3).'_'.$this->userID.'_'.$this->requestTime.'_AmdaLocalDataBaseParameters.xml';
cd1dd332   Elena.Budnik   getTimeTable
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330

		if (!copy($locParamSrc,WSConfigClass::getWsResultDir().$locParamDst))
			$this->throwError('workspaceError', 'No Amda Local DataBase Parameters description file');   

		return  array('success' => true,'WorkSpace' => array("LocalDataBaseParameters" => WSConfigClass::getUrl().$locParamDst));
	}

/*
*   get Parameter List for given user
*/
	public function getParameterList($data) 
	{         
		$res = $this->init($data);
		$this->initUserMgr(); 

		$vars = $res['vars'];

		$locParamSrc = USERWSDIR.'LocalParams.xml'; 
		$wsParamSrc =  USERWSDIR.'WsParams.xml';
		
1922ad04   Elena.Budnik   getOrbites()
331
332
		$locParamDst = substr(strtolower(__FUNCTION__),3).'_'.$this->userID.'_'.$this->requestTime.'_AmdaLocalDataBaseParameters.xml';
		$wsParamDst = substr(strtolower(__FUNCTION__),3).'_'.$this->userID.'_'.$this->requestTime.'_UserDefinedParameters.xml';
cd1dd332   Elena.Budnik   getTimeTable
333
334
335
336
337
338
339
340
341
342
343
344
345

		if (!copy($locParamSrc, WSConfigClass::getWsResultDir().$locParamDst))
			$this->throwError('workspaceError', 'No Amda Local DataBase Parameters description file for '.$this->userID);
		
		if (!copy($wsParamSrc, WSConfigClass::getWsResultDir().$wsParamDst))
			$this->throwError('workspaceError', 'No User Defined Parameters description file for '.$this->userID);  
  
		return  array('success' => true,'ParameterList' => 
					array("UserDefinedParameters" => WSConfigClass::getUrl().$wsParamDst, 
							"LocalDataBaseParameters" =>  WSConfigClass::getUrl().$locParamDst, 
							"RemoteDataBaseParameters" => "not implemented"));
	}

1ba8d04c   Elena.Budnik   time tables list,...
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
*   getParameter 
*/
	public function getParameter($data) 
	{
		$res = $this->init($data);
    
		if (!$res['success']){
			$this->throwError("requestError", "Cannot parse request"); 
		}

		$this->initUserMgr();

		$vars = $res['vars'];

		if (strtotime($vars["stopTime"]) <= strtotime($vars["startTime"])){
			$this->throwError("requestError", "Requested time interval should be greater than 0");
		}
		
		$paramId = array();
		array_push($paramId, $vars["parameterID"]);

		if (!$vars["timeFormat"])
			$timeFormat = "ISO8601";
		else
			$timeFormat = $vars["timeFormat"];

		if (!$vars["gzip"])
			$gzip = 0;
		else
			$gzip = $vars["gzip"];

		if (!$vars["stream"])
			$stream = 0;
		else
			$stream = $vars["stream"];
			
		$this->service = strtolower(__FUNCTION__);

		$res = $this->doDownloadRequest(
					array("startTime" => $vars["startTime"], "stopTime" => $vars["stopTime"], "sampling" => $vars["sampling"]),
					array("params" => $paramId),
					array("format" => $vars["outputFormat"], "timeFormat"=> $timeFormat, "gzip"=>$gzip, "stream"=>$stream));
 
		if ($res['success']) 
			return $res;
	
		$this->throwError("serverError", $res['message']);   	
	}
	
/*
*  get user Catalogs list; Shared for impex
*/
	public function getCatalogsList($data) 
	{
		$this->init($data);
		$this->init($data);
		return array('success' => true, 'CatalogsList' => $this->getTimeTablesCatalogsList('catalogs'));
	}

/*
*  get user TimeTables list; Shared for impex
*/
	public function getTimeTablesList($data) 
	{
		$this->init($data);
		 
		return array('success' => true, 'TimeTablesList' => $this->getTimeTablesCatalogsList('timetables'));
	}
	
	public function isAlive()
	{ 
		return true;
	}
    
/*
*   get Dataset
*/
	public function getDataset($data) 
	{
		$res = $this->init($data);
		
		if (!$res['success']){
			$this->throwError("requestError", "Cannot parse request"); 
		}
		
50fd9404   Elena.Budnik   getDataset()
432
		$this->initUserMgr();
1ba8d04c   Elena.Budnik   time tables list,...
433
434
435
436
437
438

		$vars = $res['vars'];

		if (strtotime($vars["stopTime"]) <= strtotime($vars["startTime"])){
			$this->throwError("requestError", "Requested time interval should be greater than 0");
		}
50fd9404   Elena.Budnik   getDataset()
439
		
1922ad04   Elena.Budnik   getOrbites()
440
		$dataSetDom = $this->getDatasetInfo($vars['datasetID']);
50fd9404   Elena.Budnik   getDataset()
441
		
50fd9404   Elena.Budnik   getDataset()
442
443
444
445
446
		$params = $dataSetDom->getElementsByTagName("parameter");
		
		if ($params->length == 0)
			$this->throwError("systemError", "Cannot find parameter list for dataset ".$vars['datasetID']); 
      
1ba8d04c   Elena.Budnik   time tables list,...
447
		$paramId = array();
50fd9404   Elena.Budnik   getDataset()
448
449
450
	  
		foreach ($params as $p)
				$paramId[] =  $p->nodeValue;
1ba8d04c   Elena.Budnik   time tables list,...
451
452
 
		if (!$vars["sampling"])
50fd9404   Elena.Budnik   getDataset()
453
454
		{ 
			$sampling = $dataSetDom->getElementsByTagName('min_sampling')->item(0)->nodeValue;
1ba8d04c   Elena.Budnik   time tables list,...
455

50fd9404   Elena.Budnik   getDataset()
456
457
458
459
460
461
462
463
464
			$units = substr($sampling,-1);
			$sampling =  substr($sampling,0,strlen($sampling)-1);
			
			switch ($units) {        
// 					case 'S':            
// 						$sampling = floatval($sampling);
// 						break; 
					case 'M':            
						$sampling = floatval($sampling)*60;
1ba8d04c   Elena.Budnik   time tables list,...
465
						break;
50fd9404   Elena.Budnik   getDataset()
466
467
					case 'H':            
						$sampling = floatval($sampling)*60*60;
1ba8d04c   Elena.Budnik   time tables list,...
468
469
470
471
						break;
					default: 
			}
		}
50fd9404   Elena.Budnik   getDataset()
472
473
474
		else {
			$sampling = $vars["sampling"];
		}
1ba8d04c   Elena.Budnik   time tables list,...
475
476
477
478
479
480
481
482
483
484
485
 
		if (!$vars["timeFormat"])
			$timeFormat = "ISO8601";
		else
			$timeFormat = $vars["timeFormat"];

		if (!$vars["gzip"])
			$gzip = 0;
		else
			$gzip = $vars["gzip"];

50fd9404   Elena.Budnik   getDataset()
486
487
488
489
490
491
492
		if (!$vars["stream"])
			$stream = 0;
		else
			$stream = $vars["stream"];
		
		$this->service = strtolower(__FUNCTION__);

1ba8d04c   Elena.Budnik   time tables list,...
493
494
495
496
497
498
499
500
501
		$res = $this->doDownloadRequest(
					array("startTime" => $vars["startTime"], "stopTime" => $vars["stopTime"], "sampling" => $sampling),
					array("params" => $paramId),
					array("format" => $vars["outputFormat"], "timeFormat"=> $timeFormat, "gzip"=>$gzip, "stream"=>$stream));
  
		if ($res['success']) return $res;

		$this->throwError("serverError", $res['message']); 
	}
cd1dd332   Elena.Budnik   getTimeTable
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
	
/*
*   get status for jobs in batch
*/
	public function getStatus($data) 
	{
		$result = $this->init($data);
		
		$id = $result['vars']['id'];
		
		if (!isset($this->requestManager))
			$this->requestManager = new RequestManagerClass();
			
		try 
		{
			$res = $this->requestManager->runWSRequest('nobody', 'nobody', FunctionTypeEnumClass::PROCESSGETINFO, null, $id);
		} 
		catch (Exception $e) 
		{
			// after first getStatus() call  process is deleted
			$jobsManager = new WSJobsManagerClass();
			
			try 
			{
				$res = $jobsManager->getResultFromProcessId($id);
				if (!$res['success']) {
					$this->throwError("processError","Cannot retrieve process $id info");
				}
				
b8194303   Elena.Budnik   getPlot final
531
532
533
				$resultTag = $this->isGetPlotRequest($res['result']) ? 'plotURL' : 'dataFileURLs';
				
				return  array('success' => true, 'status' => 'done',  $resultTag => WSConfigClass::getUrl().$res['result']);
cd1dd332   Elena.Budnik   getTimeTable
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
			} 
			catch (Exception $e) 
			{
				$this->throwError("getResultFromProcessIdError", "Exception detected : ".$e->getMessage());
			}
		}
		
		if (!$res['success']) {
			$this->throwError("processError","Cannot retrieve process $id info");
		}

		if ($res['status'] == 'in_progress') {
			return array('success' => true, 'status' => 'in progress');
		}
		
		if ($res['error']) {
			$this->throwError("processError","Process $id error code");
		}
		
		$this->deleteProcess($res['id']);
b8194303   Elena.Budnik   getPlot final
554
555
556
557
		
		$resultTag = $this->isGetPlotRequest($res['result']) ? 'plotURL' : 'dataFileURLs';
		
		return  array('success' => true, 'status' => $res['status'],  $resultTag => WSConfigClass::getUrl().$res['result']);
cd1dd332   Elena.Budnik   getTimeTable
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
	}
	
/*
*    TODO Can be done by TTCONVERT function of AMDA_Kernel - more hard !!!
*    TODO Think about this if merge/union will be also done by AMDA_Kernel
*
*    get Time Table : shared for impex ; user' for user
*/
	public function getTimeTable($data) 
	{
		$res = $this->init($data);
		
		if (!$res['success']){
			$this->throwError("requestError", "Cannot parse request"); 
		}
		
		$this->initUserMgr(true);
		
		$ttID = $res['vars']['ttID'];

		if ($this->userID == 'impex') {
			$sharedObjMgr = new SharedObjectsMgr();
			$ttSrc = $sharedObjMgr->getDataFilePath('timeTable', $ttID);
		}
		else
			$ttSrc = USERTTDIR.$ttID.'.xml';

		if (!file_exists($ttSrc)) {
			$this->throwError("workspaceError", "No such table ".$ttID." for user ".$this->userID);
		}

		$ttDst = substr(strtolower(__FUNCTION__), 3)."_".$this->userID."_".$this->requestTime."_$ttID.xml"; 

		//TODO can be done by
		// $res = $this->requestManager->runWSRequest($this->userID, $this->IPclient,FunctionTypeEnumClass::TTCONVERT, null, $ttID);
		$this->xsl2vot($ttSrc,$ttDst);

		return array('success' => true, 'ttFileURL' => WSConfigClass::getUrl().$ttDst);
	}
1922ad04   Elena.Budnik   getOrbites()
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657

/*
*   get Orbits
*/ 
	public function getOrbites($data) 
	{
		$res = $this->init($data);
		
		if (!$res['success']){
			$this->throwError("requestError", "Cannot parse request"); 
		}
		
		$this->initUserMgr();

		$vars = $res['vars'];

		if (strtotime($vars["stopTime"]) <= strtotime($vars["startTime"])){
			$this->throwError("requestError", "Requested time interval should be greater than 0");
		}
         
		$spacecraft = $vars["spacecraft"];
		$coordinateSystem = $vars["coordinateSystem"];

		if (!$vars["units"])
			$units = "km";
		else
			$units = $vars["units"];

		$orbitRequest = array("startTime" => $vars["startTime"],
				"stopTime"  => $vars["stopTime"],
				"spacecraft" => $spacecraft,
				"coordinateSystem" => $coordinateSystem,
				"units" => $units
				);
  
		$orbitParam = $this->getOrbitParameter($orbitRequest);
		
		$paramId = array();
		array_push($paramId, $orbitParam['parameterID']);

		if (!$vars["timeFormat"])
			$timeFormat = "ISO8601";
		else
			$timeFormat = $vars["timeFormat"];

		if (!$vars["gzip"])
			$gzip = 0;
		else
			$gzip = $vars["gzip"];
			
      $this->service = strtolower(__FUNCTION__);
      
		$res = $this->doDownloadRequest(
					array("startTime" => $vars["startTime"], "stopTime" => $vars["stopTime"], "sampling" => $vars["sampling"]),
					array("params" => $paramId),
					array("format" => $vars["outputFormat"], "timeFormat"=> $timeFormat, "gzip"=>$gzip, "stream"=>$stream));
 	 
		if ($res['success']) return $res;
 
		$this->throwError("serverError",$res['message']);    
	}
2b26bbdd   Elena.Budnik   getPlot partial c...
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673

/*
*  getPlot : predefined;  by mission
*/
	public function getPlot($data) 
	{
		$res = $this->init($data);
		
		if (!$res['success']){
			$this->throwError("requestError", "Cannot parse request"); 
		}
		
		$this->initUserMgr();
		
		$vars = $res['vars'];
		$mission = $vars["missionID"];
b8194303   Elena.Budnik   getPlot final
674
675
676
677
678
		
		$resultFilePrefix = strtolower(__FUNCTION__)."_".$mission."_".date("YmdHms",strtotime($vars["startTime"]))."_".date("YmdHms",strtotime($vars["stopTime"]));
		
		if ($this->userID != "impex") 
			$resultFilePrefix .= "_".$this->userID;
2b26bbdd   Elena.Budnik   getPlot partial c...
679
680

		$dom = new DomDocument("1.0");
e48a45d7   Elena.Budnik   WebServices folder
681
682
		if (!@$dom->load(WSConfigClass::getXslDir()."AmdaPlots.xml"))
			$this->throwError("systemError", "Cannot load predefined plot definition"); ;
2b26bbdd   Elena.Budnik   getPlot partial c...
683
684
685
686
687
688
689
690
691
692
693

		$missionTag = $dom->getElementById($mission);
		$params = $missionTag->getElementsByTagName('param');

		$paramsList = array();
		foreach ($params as $param)
			$paramsList[] = $param->getAttribute('name');

		$requestObject = (Object) array(
			"nodeType" => "request",
			"file-format" => "PNG",
b8194303   Elena.Budnik   getPlot final
694
			"result-file" => $resultFilePrefix,
2b26bbdd   Elena.Budnik   getPlot partial c...
695
696
697
			"timesrc" => "Interval",
			"startDate" => $vars["startTime"],
			"stopDate" => $vars["stopTime"],
b8194303   Elena.Budnik   getPlot final
698
			"parameters" => array()
2b26bbdd   Elena.Budnik   getPlot partial c...
699
700
		);
    
2b26bbdd   Elena.Budnik   getPlot partial c...
701
702
		foreach ($paramsList as $paramToPlot)
		{
2b26bbdd   Elena.Budnik   getPlot partial c...
703
			$paramObject = (Object) array(
b8194303   Elena.Budnik   getPlot final
704
				"paramid" => $paramToPlot
2b26bbdd   Elena.Budnik   getPlot partial c...
705
			);
2b26bbdd   Elena.Budnik   getPlot partial c...
706
			
b8194303   Elena.Budnik   getPlot final
707
			$requestObject->{"parameters"}[] = $paramObject;
2b26bbdd   Elena.Budnik   getPlot partial c...
708
		}
b8194303   Elena.Budnik   getPlot final
709

2b26bbdd   Elena.Budnik   getPlot partial c...
710
711
712
713
714
715
716
717
718
719
		$this->service = strtolower(__FUNCTION__);
		
		if (!isset($this->requestManager))
			$this->requestManager = new RequestManagerClass();
		
		try {
			$plotResult = $this->requestManager->runWSRequest($this->userID, $this->IPclient, FunctionTypeEnumClass::PARAMS, $this->service, $requestObject);
		} catch (Exception $e) {
				$this->throwError("plotError", "Exeption detected : ".$e->getMessage());
		}
b8194303   Elena.Budnik   getPlot final
720
721
722
723
724
725
726
727
728
729
730
731
732
733
		
		if (!$plotResult['success']) {
			$this->throwError("serverError", $plotResult['message']);
		}
		
		if($plotResult['status'] == 'in_progress') {
			return ['success' => true, 'status' => 'in progress', 'id' => $plotResult['id']];
		} elseif ($plotResult['status'] == 'done') 
		{
			$this->deleteProcess($plotResult['id']);
			return array('success' => true, 'status' => 'done', 'plotFileURL' => WSConfigClass::getUrl().$plotResult['result']);
		} else {
			return ['success' => false, 'message' => 'Unknown status ' . $plotResult['status']];
		} 
2b26bbdd   Elena.Budnik   getPlot partial c...
734
	}
16035364   Benjamin Renard   First commit
735
}
cd1dd332   Elena.Budnik   getTimeTable
736
?>