Blame view

php/WebServices/WebServer.php 30.1 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

039d19ac   Benjamin Renard   Fix getParameter ...
22
	protected function init($data = NULL) 
b8502f4d   Elena.Budnik   getStatus(), thro...
23
	{
1ba8d04c   Elena.Budnik   time tables list,...
24
25
26
		$this->requestTime = date('Ymd',time());
		
		if (!isset($data)) {
98c5bd44   Benjamin Renard   Introduce anonymo...
27
			$this->userID  = WSConfigClass::getAnonymousUserName();
1ba8d04c   Elena.Budnik   time tables list,...
28
29
30
			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

b954fc79   Benjamin Renard   Fix WebService wh...
39
		if (isset($vars['userID']) && !empty($vars['userID'])){
b8502f4d   Elena.Budnik   getStatus(), thro...
40
			$this->userID  = $vars['userID'];
4bf776ac   Elena.Budnik   init user
41
			IHMConfigClass::setUserName($this->userID);
b8502f4d   Elena.Budnik   getStatus(), thro...
42
43
		}
		else {
98c5bd44   Benjamin Renard   Introduce anonymo...
44
			$this->userID  = WSConfigClass::getAnonymousUserName();
b8502f4d   Elena.Budnik   getStatus(), thro...
45
46
47
48
49
50
51
		}
		
		$this->sessionID = $this->userID;
		
		if (isset($vars['password']))
			$this->userPWD = $vars['password'];
		else 
98c5bd44   Benjamin Renard   Introduce anonymo...
52
			$this->userPWD = WSConfigClass::getAnonymousUserPwd();
641a1a72   Benjamin Renard   Add the possibili...
53
54
55
56
57

		if (isset($vars['referenceTime']))
			$this->referenceTime = $vars['referenceTime'];
		else
			$this->referenceTime = NULL;
b8502f4d   Elena.Budnik   getStatus(), thro...
58
59
60
		return array('success' => true, 'vars' => $vars);
	}
	
b64d304e   Elena.Budnik   implement groups ...
61
	private function initUserMgr($setPathOnly = false) 
1ba8d04c   Elena.Budnik   time tables list,...
62
63
	{
		$wsUserMgr = new WSUserMgr();
039d19ac   Benjamin Renard   Fix getParameter ...
64
		$wsUserMgr->initWS($this->userID, $this->userPWD, $this->sessionID,  $setPathOnly, $this->isSoap);
1ba8d04c   Elena.Budnik   time tables list,...
65
66
67
68
69
70
		
		$this->IPclient = $wsUserMgr->getIPClient();
		
		return array('success' => true);
	}
	
b8502f4d   Elena.Budnik   getStatus(), thro...
71
72
73
74
75
	private function throwError($errorType, $msg)
	{
		if ($this->isSoap) 
			throw new SoapFault($errorType, $msg); 
		else 
fe5a16b4   Elena.Budnik   error for rest
76
			exit(json_encode(array("error" => $msg)));
b8502f4d   Elena.Budnik   getStatus(), thro...
77
78
	}
	
b8194303   Elena.Budnik   getPlot final
79
80
81
82
83
	private function isGetPlotRequest($name){

		return (substr($name,0,7) == 'getplot');
	}
	
cd1dd332   Elena.Budnik   getTimeTable
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
	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
106

1922ad04   Elena.Budnik   getOrbites()
107
108
109
110
111
112
113
114
115
116
117
118
	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...
119
		return $dataSetDom;
1922ad04   Elena.Budnik   getOrbites()
120
	}
641a1a72   Benjamin Renard   Add the possibili...
121
122
123
124
125
126

	private function getUserInfo($data) 
	{
		$res = $this->init($data);
		$this->initUserMgr(); 
	}
1922ad04   Elena.Budnik   getOrbites()
127
	
1ba8d04c   Elena.Budnik   time tables list,...
128
/*
98c5bd44   Benjamin Renard   Introduce anonymo...
129
*  get user TimeTables list; Shared for anonymous user ('impex')
1ba8d04c   Elena.Budnik   time tables list,...
130
131
*/
	private function getTimeTablesCatalogsList($object) 
b8502f4d   Elena.Budnik   getStatus(), thro...
132
	{
7ac2915f   Myriam Bouchemit   call initUserMgr ...
133
		$this->initUserMgr();
b8502f4d   Elena.Budnik   getStatus(), thro...
134
		$dom = new DOMDocument("1.0");
1ba8d04c   Elena.Budnik   time tables list,...
135
		
98c5bd44   Benjamin Renard   Introduce anonymo...
136
		if ($this->userID == WSConfigClass::getAnonymousUserName()) {
b8502f4d   Elena.Budnik   getStatus(), thro...
137
138
			$sharedObjMgr = new SharedObjectsMgr();
			if (!@$dom->load($sharedObjMgr->getTreeFilePath()))
1ba8d04c   Elena.Budnik   time tables list,...
139
					$this->throwError("workspaceError", "Workspace Error : Cannot load Shared TimeTable list");
183fc1af   Myriam Bouchemit   getTimeTablesCata...
140
                        $tagName = $object == "timetables" ? "timeTableList" : "catalogList";
b8502f4d   Elena.Budnik   getStatus(), thro...
141
142
		}
		else {
56228bb6   Benjamin Renard   Create Tt.xml and...
143
144
145
146
147
			if (!file_exists(USERWSDIR.'Tt.xml')) {
				$ttMgr = new TimeTableMgr();
			}
			if (!@$dom->load(USERWSDIR.'Tt.xml'))
				$this->throwError("workspaceError", "Workspace Error : Cannot load TimeTable list for ".$this->userID);
183fc1af   Myriam Bouchemit   getTimeTablesCata...
148
                        $tagName = $object == "timetables" ? "timetabList" : "catalogList";
b8502f4d   Elena.Budnik   getStatus(), thro...
149
150
		}
    
1ba8d04c   Elena.Budnik   time tables list,...
151
		$timetabNode = $dom->getElementsByTagName($tagName);
8f6112a7   Nathanael Jourdane   Reformat webserver
152

b8502f4d   Elena.Budnik   getStatus(), thro...
153
		if ($timetabNode->length < 1){
1ba8d04c   Elena.Budnik   time tables list,...
154
				$this->throwError("workspaceWarning", "Workspace Warning : No $object");
b8502f4d   Elena.Budnik   getStatus(), thro...
155
156
157
158
159
160
161
162
		}
		
		$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,...
163
164
165
166
167
		
		$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
168

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

b8502f4d   Elena.Budnik   getStatus(), thro...
172
/*
1922ad04   Elena.Budnik   getOrbites()
173
174
175
*  Get corresponding orbit parameter ID
*/
	private function getOrbitParameter($orbitRequest) 
b8502f4d   Elena.Budnik   getStatus(), thro...
176
	{
1922ad04   Elena.Budnik   getOrbites()
177
178
179
180
		if (!file_exists(WSConfigClass::getOrbitsXml()))
			$this->throwError('systemError', "No AMDA system orbits file");
			
		$orbitsXml = new DomDocument();
b8502f4d   Elena.Budnik   getStatus(), thro...
181

1922ad04   Elena.Budnik   getOrbites()
182
183
184
185
186
		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...
187
		
1922ad04   Elena.Budnik   getOrbites()
188
189
		$xpath = new DOMXpath($orbitsXml);
		$path = '//orbites[@mission="'.$spacecraft.'" and @coordinate_system="'.$orbitRequest['coordinateSystem'].'" and @units="'.$orbitRequest['units'].'" ] ';
8f6112a7   Nathanael Jourdane   Reformat webserver
190

1922ad04   Elena.Budnik   getOrbites()
191
		$orbits = $xpath->query($path);
b8502f4d   Elena.Budnik   getStatus(), thro...
192
		
1922ad04   Elena.Budnik   getOrbites()
193
194
195
196
197
198
199
		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...
200

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

1922ad04   Elena.Budnik   getOrbites()
203
204
205
				return array('success' => true, 
								 'parameterID' => $orbit->getAttribute('xml:id')   
				);
b8502f4d   Elena.Budnik   getStatus(), thro...
206
			}
b8502f4d   Elena.Budnik   getStatus(), thro...
207
		}
1922ad04   Elena.Budnik   getOrbites()
208
209
210
		
		$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...
211
	}
1922ad04   Elena.Budnik   getOrbites()
212
	
9846ec70   Benjamin Renard   WS getDataset wit...
213
	private function doDownloadRequest($interval, $paramList, $formatInfo, $file_info) 
b8502f4d   Elena.Budnik   getStatus(), thro...
214
	{
b8502f4d   Elena.Budnik   getStatus(), thro...
215
		if (!isset($this->paramLoader))
14ae289b   Benjamin Renard   IHMUserParamLoade...
216
			$this->paramLoader = new IHMUserParamManagerClass();
b8502f4d   Elena.Budnik   getStatus(), thro...
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

		//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
244
			else {
b8502f4d   Elena.Budnik   getStatus(), thro...
245
246
				$param->paramid = $paramId;
			}
b8502f4d   Elena.Budnik   getStatus(), thro...
247
248
			$params[] = $param;
		}
8385c27c   Elena.Budnik   interim commit
249

61193896   Benjamin Renard   WS - getParameter...
250
		$ref_sampling_param = isset($interval['ref_sampling_param']) ? $interval['ref_sampling_param'] : NULL;
9846ec70   Benjamin Renard   WS getDataset wit...
251

b8502f4d   Elena.Budnik   getStatus(), thro...
252
		$obj = (object)array(
8a92ee19   Elena.Budnik   timeTobatch speci...
253
						"sampling" => $interval['sampling'],
8a92ee19   Elena.Budnik   timeTobatch speci...
254
255
256
						"startDate" => $interval['startTime'],
						"stopDate" => $interval['stopTime'],
						"list" => $params,
4bdd7ad9   Elena.Budnik   interim commit
257
258
						"fileformat" => $formatInfo['format'],
						"timeformat" => $formatInfo['timeFormat'],
9846ec70   Benjamin Renard   WS getDataset wit...
259
260
261
						"compression" => $formatInfo['gzip'],
						"ref_sampling_param" => $ref_sampling_param,
						"file_info" => $file_info,
8385c27c   Elena.Budnik   interim commit
262
			);	
039d19ac   Benjamin Renard   Fix getParameter ...
263
264


b8502f4d   Elena.Budnik   getStatus(), thro...
265
266
		if (!isset($this->requestManager))
			$this->requestManager = new RequestManagerClass();
8385c27c   Elena.Budnik   interim commit
267
		 
b8502f4d   Elena.Budnik   getStatus(), thro...
268
		try {
4bc7749a   Elena.Budnik   getParameter (sim...
269
			$downloadResult = $this->requestManager->runWSRequest($this->userID, $this->IPclient, FunctionTypeEnumClass::PARAMS, $this->service, $obj);
b8502f4d   Elena.Budnik   getStatus(), thro...
270
		} catch (Exception $e) {
8385c27c   Elena.Budnik   interim commit
271
			$this->throwError("executionError", "Exception detected : ".$e->getMessage()); 
b8502f4d   Elena.Budnik   getStatus(), thro...
272
		}
039d19ac   Benjamin Renard   Fix getParameter ...
273

b8502f4d   Elena.Budnik   getStatus(), thro...
274
275
276
277
278
279
			
		if (!$downloadResult['success']) {
			$this->throwError("serverError", $downloadResult['message']);
		}
		
		if($downloadResult['status'] == 'in_progress') {
8385c27c   Elena.Budnik   interim commit
280
281
282
			return ['success' => true, 'status' => 'in progress', 'id' => $downloadResult['id']];
		} elseif ($downloadResult['status'] == 'done') 
		{
4bc7749a   Elena.Budnik   getParameter (sim...
283
			$this->deleteProcess($downloadResult['id']);
039d19ac   Benjamin Renard   Fix getParameter ...
284
			return array('success' => true, 'status' => 'done', 'dataFileURLs' => WSConfigClass::getUrl().$downloadResult['result'], 'exectime' => $downloadResult['exectime']);
b8502f4d   Elena.Budnik   getStatus(), thro...
285
286
287
288
289
		} else {
			return ['success' => false, 'message' => 'Unknown status ' . $downloadResult['status']];
		} 
	}
	
4bc7749a   Elena.Budnik   getParameter (sim...
290
/*
2b26bbdd   Elena.Budnik   getPlot partial c...
291
*         delete process after execution : 
4bc7749a   Elena.Budnik   getParameter (sim...
292
293
294
295
*         delete temporary files/folders ( JOBS/process_xxxx, RES/DDxxx ); 
*         delete job node in processManager.xml
*/
	private function deleteProcess($id)
b8502f4d   Elena.Budnik   getStatus(), thro...
296
	{
4bdd7ad9   Elena.Budnik   interim commit
297
		$obj = (object)array('id' => $id);
b8502f4d   Elena.Budnik   getStatus(), thro...
298
299
300
301
   
		if (!isset($this->requestManager))
			$this->requestManager = new RequestManagerClass();

4bdd7ad9   Elena.Budnik   interim commit
302
		try {
4bc7749a   Elena.Budnik   getParameter (sim...
303
			$downloadResult = $this->requestManager->runWSRequest($this->userID, $this->IPclient, FunctionTypeEnumClass::PROCESSDELETE, null, $obj);
4bdd7ad9   Elena.Budnik   interim commit
304
		} catch (Exception $e) {
4bc7749a   Elena.Budnik   getParameter (sim...
305
			$this->throwError("deleteProcessError", $e->getMessage());
4bdd7ad9   Elena.Budnik   interim commit
306
		}
b8502f4d   Elena.Budnik   getStatus(), thro...
307
	}
b64d304e   Elena.Budnik   implement groups ...
308
309
310
311
312
313
	
	private function excludePrivateNodes($locParamSrc, $locParamDst)
	{
		$locParamSrcDom = new DomDocument("1.0");
		$locParamSrcDom->preserveWhiteSpace = FALSE; /// Important !!! otherwise removeChild() leaves empty text nodes
		
9846ec70   Benjamin Renard   WS getDataset wit...
314
		if (!@$locParamSrcDom->load($locParamSrc))
b64d304e   Elena.Budnik   implement groups ...
315
316
317
318
			$this->throwError("getObsDataTree", "Cannot load  Amda Local DataBase Parameters description file".$this->userID);
 
		$xp =  new domxpath($locParamSrcDom);
		$restricted = $xp->query("//*[@group]");
2b26bbdd   Elena.Budnik   getPlot partial c...
319

b64d304e   Elena.Budnik   implement groups ...
320
		foreach ($restricted as $node) {
4746dc6e   Benjamin Renard   Allow TimeRetrict...
321
322
323
324
325
				$timeRestriction = $node->getAttribute('timeRestriction');
				if (!empty($timeRestriction)) {
					continue;
				}

b64d304e   Elena.Budnik   implement groups ...
326
327
328
329
330
331
332
333
334
335
336
337
338
339
				$parentNode = $node->parentNode;
				$parentNode->removeChild($node);
				
				if (!$parentNode->hasChildNodes()) {
					if ($parentNode->parentNode){
						$parentParentNode = $parentNode->parentNode;  
						$parentParentNode->removeChild($parentNode);
					}
				}
			}
 
		if (!$locParamSrcDom->save(WSConfigClass::getWsResultDir().$locParamDst))
			$this->throwError('workspaceError', 'Cannot save Amda Local DataBase Parameters description file'.$this->userID);   
	}
641a1a72   Benjamin Renard   Add the possibili...
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

    private function injectTemplateInfo($locParamFile) {
        $paramTemplateListFile = WSConfigClass::getParamTemplateListFilePath();

        $paramTemplateListFileDom = new DomDocument("1.0");
        if (!@$paramTemplateListFileDom->load($paramTemplateListFile))
			$this->throwError("getObsDataTree", "Cannot load  Amda Templated Parameters description file".$this->userID);

        $paramTemplateNodes = $paramTemplateListFileDom->getElementsByTagName('paramTemplate');

        $locParamFileDom = new DomDocument("1.0");
		$locParamFileDom->preserveWhiteSpace = FALSE; /// Important !!! otherwise removeChild() leaves empty text nodes
		
		if (!@$locParamFileDom->load(WSConfigClass::getWsResultDir().$locParamFile))
			$this->throwError("getObsDataTree", "Cannot load  Amda Local DataBase Parameters description file ".$this->userID);
 
		$xp =  new domxpath($locParamFileDom);

        foreach ($paramTemplateNodes as $paramTemplateNode) {
            $paramTemplateId = $paramTemplateNode->getAttribute('paramId');
            $paramFileName = $paramTemplateNode->getAttribute('fileName');
            $parameterNodes = $xp->query("//parameter[@template='$paramFileName']");
            $argumentsNodes = $paramTemplateNode->getElementsByTagName("arguments");
            if ($argumentsNodes->length < 1)
                continue;
            $argumentsNode = $argumentsNodes->item(0);
            foreach ($parameterNodes as $parameterNode) {
                $node = $locParamFileDom->importNode($argumentsNode, TRUE);
                $parameterNode->appendChild($node);
            }
        }

 
		if (!$locParamFileDom->save(WSConfigClass::getWsResultDir().$locParamFile))
			$this->throwError('workspaceError', 'Cannot save Amda Local DataBase Parameters description file'.$this->userID); 
    }
b64d304e   Elena.Budnik   implement groups ...
376
	
189a6f4f   Elena.Budnik   coorect timie
377
378
379
380
381
382
	private function checkInputTime($startTime, $stopTime) 
	{
		if ($stopTime <= $startTime ) 
			$this->throwError("requestError", "Requested time interval should be greater than 0"); 
	}
	
1ba8d04c   Elena.Budnik   time tables list,...
383
/************************** WEB SERVICES **************************************/	
189a6f4f   Elena.Budnik   coorect timie
384
385
386
387
388
389
390
391
392
393
394
/*
*   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);
	}
	
cd1dd332   Elena.Budnik   getTimeTable
395
/*
98c5bd44   Benjamin Renard   Introduce anonymo...
396
*   public data only : anonymous user (impex)
cd1dd332   Elena.Budnik   getTimeTable
397
*/
641a1a72   Benjamin Renard   Add the possibili...
398
	public function getObsDataTree($data) 
cd1dd332   Elena.Budnik   getTimeTable
399
	{         
641a1a72   Benjamin Renard   Add the possibili...
400
		$res = $this->init($data);	
cd1dd332   Elena.Budnik   getTimeTable
401
		$this->initUserMgr();
641a1a72   Benjamin Renard   Add the possibili...
402
403

        $injectTemplateInfo = array_key_exists('templateInfo', $data) && (!empty($data["templateInfo"]));
cd1dd332   Elena.Budnik   getTimeTable
404
405
		
		$locParamSrc = USERWSDIR.'LocalParams.xml'; 
b64d304e   Elena.Budnik   implement groups ...
406
		
641a1a72   Benjamin Renard   Add the possibili...
407
		$locParamDst = substr(strtolower(__FUNCTION__),3).'_'.$this->userID.'_'.($injectTemplateInfo ? 'withtemplate_' : '').$this->requestTime.'_AmdaLocalDataBaseParameters.xml';
cd1dd332   Elena.Budnik   getTimeTable
408

b64d304e   Elena.Budnik   implement groups ...
409
410
// 		if (!copy($locParamSrc,WSConfigClass::getWsResultDir().$locParamDst))
// 			$this->throwError('workspaceError', 'No Amda Local DataBase Parameters description file');   
cd1dd332   Elena.Budnik   getTimeTable
411

b64d304e   Elena.Budnik   implement groups ...
412
		$this->excludePrivateNodes($locParamSrc,$locParamDst);
641a1a72   Benjamin Renard   Add the possibili...
413
414
415
416
417

        if ($injectTemplateInfo) {
            $this->injectTemplateInfo($locParamDst);
        }

cd1dd332   Elena.Budnik   getTimeTable
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
		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()
434
435
		$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
436

b64d304e   Elena.Budnik   implement groups ...
437
438
439
440
// 		if (!copy($locParamSrc, WSConfigClass::getWsResultDir().$locParamDst))
// 			$this->throwError('workspaceError', 'No Amda Local DataBase Parameters description file for '.$this->userID);
		
		$this->excludePrivateNodes($locParamSrc,$locParamDst);
56228bb6   Benjamin Renard   Create Tt.xml and...
441
442
443
444

		if (!file_exists($wsParamSrc)) {
			$paramMgr = new DerivedParamMgr('derivedParam');
		}
cd1dd332   Elena.Budnik   getTimeTable
445
		
9846ec70   Benjamin Renard   WS getDataset wit...
446
		if (($this->userID == WSConfigClass::getAnonymousUserName()) ||  !copy($wsParamSrc, WSConfigClass::getWsResultDir().$wsParamDst))
7ac2915f   Myriam Bouchemit   call initUserMgr ...
447
448
			return  array('success' => true,'ParameterList' => 
					array("LocalDataBaseParameters" =>  WSConfigClass::getUrl().$locParamDst));
9846ec70   Benjamin Renard   WS getDataset wit...
449

f750cd4f   Benjamin Renard   Add size & displa...
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
		// Inject size & display_type
		//  => In derived parameters
		$dom = new DOMDocument( "1.0");
		$dom->formatOutput = TRUE;
		$dom->preserveWhiteSpace = FALSE;
		$dom->load(WSConfigClass::getWsResultDir().$wsParamDst);
		$paramNodes = $dom->getElementsByTagName("param");
		foreach ($paramNodes as $paramNode) {
			$dim_1 = $paramNode->getAttribute('dim_1');
			$dim_1 = empty($dim_1) ? 1 : intval($dim_1);
			$dim_2 = $paramNode->getAttribute('dim_2');
			$dim_2 = empty($dim_2) ? 1 : intval($dim_2);
			$paramNode->setAttribute('size', $dim_1 * $dim_2);

			$display_type = 'timeseries';
			if ($dim_1 > 1 && $dim_2 > 1) {
				$display_type = 'spectrogram';
			}
			else if ($dim_1 > 3 || $dim_2 > 3) {
				$display_type = 'spectrogram';
			}
			$paramNode->setAttribute('display_type', $display_type);
		}
		//  => In parameters from an uploaded file
		$paramNodes = $dom->getElementsByTagName("mydata");
		foreach ($paramNodes as $paramNode) {
			// size already exists
			$display_type = 'timeseries';
			if ($paramNode->getAttribute('plottype') == 'Spectra') {
				$display_type = 'spectrogram';
			}
			$paramNode->setAttribute('display_type', $display_type);
		}
		$dom->save(WSConfigClass::getWsResultDir().$wsParamDst);

cd1dd332   Elena.Budnik   getTimeTable
485
486
		return  array('success' => true,'ParameterList' => 
					array("UserDefinedParameters" => WSConfigClass::getUrl().$wsParamDst, 
7ac2915f   Myriam Bouchemit   call initUserMgr ...
487
							"LocalDataBaseParameters" =>  WSConfigClass::getUrl().$locParamDst));
cd1dd332   Elena.Budnik   getTimeTable
488
489
	}

1ba8d04c   Elena.Budnik   time tables list,...
490
491
492
493
494
/*
*   getParameter 
*/
	public function getParameter($data) 
	{
1ba8d04c   Elena.Budnik   time tables list,...
495
496
497
498
499
500
501
502
503
		$res = $this->init($data);
    
		if (!$res['success']){
			$this->throwError("requestError", "Cannot parse request"); 
		}

		$this->initUserMgr();

		$vars = $res['vars'];
039d19ac   Benjamin Renard   Fix getParameter ...
504
505
506
507
508
509
510
511

		if (empty($vars["startTime"])) {
			$this->throwError("requestError", "Missing startTime definition");
		}

		if (empty($vars["stopTime"])) {
			$this->throwError("requestError", "Missing stopTime definition");
		}
189a6f4f   Elena.Budnik   coorect timie
512
513
514
515
516
517
518
519
		
		if (is_numeric($vars["startTime"])) {
			$this->checkInputTime($vars["startTime"],$vars["stopTime"]);
			$vars["startTime"] = date("Y-m-d\TH:i:s", $vars["startTime"]);
			$vars["stopTime"] = date("Y-m-d\TH:i:s", $vars["stopTime"]);
		}
		else {
			$this->checkInputTime(strtotime($vars["startTime"]),strtotime($vars["stopTime"])); 
1ba8d04c   Elena.Budnik   time tables list,...
520
521
		}
		
039d19ac   Benjamin Renard   Fix getParameter ...
522
		if (empty($vars["timeFormat"])) {
1ba8d04c   Elena.Budnik   time tables list,...
523
			$timeFormat = "ISO8601";
189a6f4f   Elena.Budnik   coorect timie
524
525
		}
		else {
1ba8d04c   Elena.Budnik   time tables list,...
526
			$timeFormat = $vars["timeFormat"];
189a6f4f   Elena.Budnik   coorect timie
527
		}
039d19ac   Benjamin Renard   Fix getParameter ...
528

61193896   Benjamin Renard   WS - getParameter...
529
530
531
		$sampling = !empty($vars["sampling"]) ? $vars["sampling"] : NULL;
		$ref_sampling_param = NULL;
		$file_info = "";
039d19ac   Benjamin Renard   Fix getParameter ...
532
533
534
535

		if (empty($vars["parameterID"])) {
			$this->throwError("requestError", "Missing parameterID definition");
		}
189a6f4f   Elena.Budnik   coorect timie
536
		$paramId = array();
61193896   Benjamin Renard   WS - getParameter...
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
		if (strpos($vars["parameterID"], ',')) {
			$paramId = explode(',', $vars["parameterID"]);

			if (!isset($sampling))
			{
				$ref_sampling_param = trim($paramId[0]);
			}

			foreach ($paramId as &$id) {
				$id = trim($id);
				if (!empty($file_info)) {
					$file_info .= "-";
				}
				$file_info .= $id;
			}
		}
		else {
			array_push($paramId, $vars["parameterID"]);
			$file_info = $vars["parameterID"];
		}
1ba8d04c   Elena.Budnik   time tables list,...
557

039d19ac   Benjamin Renard   Fix getParameter ...
558
		if (empty($vars["gzip"]))
1ba8d04c   Elena.Budnik   time tables list,...
559
560
			$gzip = 0;
		else
039d19ac   Benjamin Renard   Fix getParameter ...
561
			$gzip = ($vars["gzip"] == 1);
1ba8d04c   Elena.Budnik   time tables list,...
562

039d19ac   Benjamin Renard   Fix getParameter ...
563
		$outputFormat = !empty($vars["outputFormat"]) ? $vars["outputFormat"] : 'ASCII'; 
1ba8d04c   Elena.Budnik   time tables list,...
564
565
566
567
			
		$this->service = strtolower(__FUNCTION__);

		$res = $this->doDownloadRequest(
61193896   Benjamin Renard   WS - getParameter...
568
569
					array("startTime" => $vars["startTime"], "stopTime" => $vars["stopTime"], "sampling" => $sampling,
						"ref_sampling_param" => $ref_sampling_param),
1ba8d04c   Elena.Budnik   time tables list,...
570
					array("params" => $paramId),
61193896   Benjamin Renard   WS - getParameter...
571
					array("format" => $outputFormat, "timeFormat"=> $timeFormat, "gzip"=>$gzip), $file_info);
1ba8d04c   Elena.Budnik   time tables list,...
572
573
574
575
 
		if ($res['success']) 
			return $res;
	
039d19ac   Benjamin Renard   Fix getParameter ...
576
		$this->throwError("serverError", $res['message']); 
1ba8d04c   Elena.Budnik   time tables list,...
577
578
579
	}
	
/*
98c5bd44   Benjamin Renard   Introduce anonymo...
580
*  get user Catalogs list; Shared for anonymous user (impex)
1ba8d04c   Elena.Budnik   time tables list,...
581
582
583
584
*/
	public function getCatalogsList($data) 
	{
		$this->init($data);
fc76cba2   Elena.Budnik   rm duplicated init()
585
	
1ba8d04c   Elena.Budnik   time tables list,...
586
587
588
589
		return array('success' => true, 'CatalogsList' => $this->getTimeTablesCatalogsList('catalogs'));
	}

/*
98c5bd44   Benjamin Renard   Introduce anonymo...
590
*  get user TimeTables list; Shared for anonymous user (impex)
1ba8d04c   Elena.Budnik   time tables list,...
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
*/
	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()
615
		$this->initUserMgr();
1ba8d04c   Elena.Budnik   time tables list,...
616
617

		$vars = $res['vars'];
50fd9404   Elena.Budnik   getDataset()
618
		
189a6f4f   Elena.Budnik   coorect timie
619
620
621
622
623
624
625
626
627
		if (is_numeric($vars["startTime"])) {
			$this->checkInputTime($vars["startTime"],$vars["stopTime"]);
			$vars["startTime"] = date("Y-m-d\TH:i:s", $vars["startTime"]);
			$vars["stopTime"] = date("Y-m-d\TH:i:s", $vars["stopTime"]);
		}
		else {
			$this->checkInputTime(strtotime($vars["startTime"]),strtotime($vars["stopTime"])); 
		}
	 
9846ec70   Benjamin Renard   WS getDataset wit...
628
		if (empty($vars["timeFormat"])) {
189a6f4f   Elena.Budnik   coorect timie
629
630
631
632
633
			$timeFormat = "ISO8601";
		}
		else {
			$timeFormat = $vars["timeFormat"];
		}
9846ec70   Benjamin Renard   WS getDataset wit...
634
635
636
637
638
639

		if (empty($vars['datasetID'])) {
			$this->throwError("requestError", "Missing datasetID definition");
		}
		$datasetId = $vars['datasetID'];
		$dataSetDom = $this->getDatasetInfo($datasetId);
50fd9404   Elena.Budnik   getDataset()
640
		
50fd9404   Elena.Budnik   getDataset()
641
642
643
		$params = $dataSetDom->getElementsByTagName("parameter");
		
		if ($params->length == 0)
9846ec70   Benjamin Renard   WS getDataset wit...
644
			$this->throwError("systemError", "Cannot find parameter list for dataset ".$datasetId); 
50fd9404   Elena.Budnik   getDataset()
645
      
1ba8d04c   Elena.Budnik   time tables list,...
646
		$paramId = array();
50fd9404   Elena.Budnik   getDataset()
647
648
649
	  
		foreach ($params as $p)
				$paramId[] =  $p->nodeValue;
1ba8d04c   Elena.Budnik   time tables list,...
650
 
9846ec70   Benjamin Renard   WS getDataset wit...
651
		if (empty($vars["sampling"]))
50fd9404   Elena.Budnik   getDataset()
652
		{ 
9846ec70   Benjamin Renard   WS getDataset wit...
653
654
			$sampling = NULL;
			$ref_sampling_param = $paramId[0];
1ba8d04c   Elena.Budnik   time tables list,...
655
		}
50fd9404   Elena.Budnik   getDataset()
656
657
		else {
			$sampling = $vars["sampling"];
9846ec70   Benjamin Renard   WS getDataset wit...
658
			$ref_sampling_param = NULL;
50fd9404   Elena.Budnik   getDataset()
659
		}
1ba8d04c   Elena.Budnik   time tables list,...
660
 
9846ec70   Benjamin Renard   WS getDataset wit...
661
		if (empty($vars["gzip"]))
1ba8d04c   Elena.Budnik   time tables list,...
662
663
			$gzip = 0;
		else
9846ec70   Benjamin Renard   WS getDataset wit...
664
665
666
			$gzip = ($vars["gzip"] == 1);

		$outputFormat = !empty($vars["outputFormat"]) ? $vars["outputFormat"] : 'ASCII';
1ba8d04c   Elena.Budnik   time tables list,...
667

50fd9404   Elena.Budnik   getDataset()
668
669
		$this->service = strtolower(__FUNCTION__);

1ba8d04c   Elena.Budnik   time tables list,...
670
		$res = $this->doDownloadRequest(
9846ec70   Benjamin Renard   WS getDataset wit...
671
					array("startTime" => $vars["startTime"], "stopTime" => $vars["stopTime"], "sampling" => $sampling, "ref_sampling_param" => $ref_sampling_param),
1ba8d04c   Elena.Budnik   time tables list,...
672
					array("params" => $paramId),
9846ec70   Benjamin Renard   WS getDataset wit...
673
					array("format" => $outputFormat, "timeFormat"=> $timeFormat, "gzip"=>$gzip), $datasetId);
1ba8d04c   Elena.Budnik   time tables list,...
674
675
676
677
678
  
		if ($res['success']) return $res;

		$this->throwError("serverError", $res['message']); 
	}
b2b3cdea   Benjamin Renard   Add script to bui...
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705

/*
*   get EPN-TAP granule
*/
	public function getGranule($data)
	{
		if (!array_key_exists('obs_id', $data) || !array_key_exists('start', $data) || !array_key_exists('stop', $data)) {
			$this->throwError("serverError", "Missing obs_id, start or stop definition");
		}
		$data['datasetID'] = $data['obs_id'];
		$data['startTime'] = $data['start'];
		$data['stopTime'] = $data['stop'];
		$data['outputFormat'] = "CDF_ISTP";
		if (array_key_exists('format', $data)) {
			$data['outputFormat'] = $data["format"];
		}
		$res = $this->getDataset($data);
		if (!$res['success']) {
			return $res;
		}
		if (array_key_exists('dataFileURLs', $res)) {
			$data = file_get_contents($res['dataFileURLs']);
			header("Location: ".$res['dataFileURLs']);
			die();
		}
		$this->throwError("serverError", "Unknown error");
	}
cd1dd332   Elena.Budnik   getTimeTable
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
	
/*
*   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) 
		{
39bae1d7   Benjamin Renard   Fix getStatus API
725
			$this->throwError("executionError", "Exception detected : ".$e->getMessage());
cd1dd332   Elena.Budnik   getTimeTable
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
		}
		
		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
741
742
743
744
		
		$resultTag = $this->isGetPlotRequest($res['result']) ? 'plotURL' : 'dataFileURLs';
		
		return  array('success' => true, 'status' => $res['status'],  $resultTag => WSConfigClass::getUrl().$res['result']);
cd1dd332   Elena.Budnik   getTimeTable
745
	}
0123b9fe   Benjamin Renard   Add APIs getCatal...
746

cd1dd332   Elena.Budnik   getTimeTable
747
748
749
750
/*
*    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
*
98c5bd44   Benjamin Renard   Introduce anonymo...
751
*    get Time Table : shared for anonymous user (impex) ; user' for user
cd1dd332   Elena.Budnik   getTimeTable
752
*/
0123b9fe   Benjamin Renard   Add APIs getCatal...
753
754
755
756
757
758
759
760
761
        public function getTimeTable($data)       
        {
		$res = $this->init($data);

		if (!$res['success']){
			$this->throwError("requestError", "Cannot parse request");
		}

		$ttID = $res['vars']['ttID'];
26dbe95d   Benjamin Renard   outputFormat for ...
762
		$format = !empty($res['vars']['outputFormat']) ? $res['vars']['outputFormat'] : 'VOTable';
0123b9fe   Benjamin Renard   Add APIs getCatal...
763

26dbe95d   Benjamin Renard   outputFormat for ...
764
		return $this->getTimeTableCatalog('timeTable', $ttID, $format);
0123b9fe   Benjamin Renard   Add APIs getCatal...
765
766
767
768
769
770
771
772
773
	}

/*
*    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 anonymous user (impex) ; user' for user
*/
	public function getCatalog($data)       
cd1dd332   Elena.Budnik   getTimeTable
774
775
	{
		$res = $this->init($data);
0123b9fe   Benjamin Renard   Add APIs getCatal...
776

cd1dd332   Elena.Budnik   getTimeTable
777
		if (!$res['success']){
0123b9fe   Benjamin Renard   Add APIs getCatal...
778
			$this->throwError("requestError", "Cannot parse request");
cd1dd332   Elena.Budnik   getTimeTable
779
		}
0123b9fe   Benjamin Renard   Add APIs getCatal...
780
781

		$catID = $res['vars']['catID'];
26dbe95d   Benjamin Renard   outputFormat for ...
782
		$format = !empty($res['vars']['outputFormat']) ? $res['vars']['outputFormat'] : 'VOTable';
0123b9fe   Benjamin Renard   Add APIs getCatal...
783

26dbe95d   Benjamin Renard   outputFormat for ...
784
		return $this->getTimeTableCatalog('catalog', $catID, $format);
0123b9fe   Benjamin Renard   Add APIs getCatal...
785
786
787
788
789
790
791
792
	}
	
/*
*    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 anonymous user (impex) ; user' for user
*/
26dbe95d   Benjamin Renard   outputFormat for ...
793
	private function getTimeTableCatalog($type, $id, $format) 
0123b9fe   Benjamin Renard   Add APIs getCatal...
794
	{
7ac2915f   Myriam Bouchemit   call initUserMgr ...
795
		$this->initUserMgr();
cd1dd332   Elena.Budnik   getTimeTable
796
		
98c5bd44   Benjamin Renard   Introduce anonymo...
797
		if ($this->userID == WSConfigClass::getAnonymousUserName()) {
cd1dd332   Elena.Budnik   getTimeTable
798
			$sharedObjMgr = new SharedObjectsMgr();
0123b9fe   Benjamin Renard   Add APIs getCatal...
799
			$objSrc = $sharedObjMgr->getDataFilePath($type, $id);
cd1dd332   Elena.Budnik   getTimeTable
800
801
		}
		else
0123b9fe   Benjamin Renard   Add APIs getCatal...
802
			$objSrc = USERTTDIR.$id.'.xml';
cd1dd332   Elena.Budnik   getTimeTable
803

0123b9fe   Benjamin Renard   Add APIs getCatal...
804
805
		if (!file_exists($objSrc)) {
			$this->throwError("workspaceError", "No such object ".$id." for user ".$this->userID);
cd1dd332   Elena.Budnik   getTimeTable
806
807
		}

26dbe95d   Benjamin Renard   outputFormat for ...
808
809
810
811
812
		$prefix = ($type == 'catalog') ? 'catalog' : 'timetable';

		$extension = (strtolower($format) == 'votable') ? "xml" : "txt";

		$objDst = $prefix."_".$this->userID."_".$this->requestTime."_$id.$extension"; 
0123b9fe   Benjamin Renard   Add APIs getCatal...
813
814
815
816
817
818
819

		if (!isset($this->requestManager))
			$this->requestManager = new RequestManagerClass();

                try {
			$obj = array(
				'inputFile' => $objSrc,
26dbe95d   Benjamin Renard   outputFormat for ...
820
				'outputFormat' => $format,
0123b9fe   Benjamin Renard   Add APIs getCatal...
821
822
823
824
825
826
				'outputFileName' => $objDst, 
			);
                        $res = $this->requestManager->runWSRequest($this->userID, $this->IPclient, FunctionTypeEnumClass::TTCONVERT, $this->service, $obj);
                } catch (Exception $e) {
                        return array('success' => false, 'message' => 'Exception detected : '.$e->getMessage());
                }
cd1dd332   Elena.Budnik   getTimeTable
827

0123b9fe   Benjamin Renard   Add APIs getCatal...
828
		$resKey = ($type == 'catalog') ? 'catFileURL' : 'ttFileURL';
cd1dd332   Elena.Budnik   getTimeTable
829

0123b9fe   Benjamin Renard   Add APIs getCatal...
830
		return array('success' => true, $resKey => WSConfigClass::getUrl().$objDst);
cd1dd332   Elena.Budnik   getTimeTable
831
	}
1922ad04   Elena.Budnik   getOrbites()
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846

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

		$vars = $res['vars'];
189a6f4f   Elena.Budnik   coorect timie
847
848
849
850
851
852
853
854
855
856
		
		if (is_numeric($vars["startTime"])) {
			$this->checkInputTime($vars["startTime"],$vars["stopTime"]);
			$vars["startTime"] = date("Y-m-d\TH:i:s", $vars["startTime"]);
			$vars["stopTime"] = date("Y-m-d\TH:i:s", $vars["stopTime"]);
		}
		else {
			$this->checkInputTime(strtotime($vars["startTime"]),strtotime($vars["stopTime"])); 
		}
	 
9846ec70   Benjamin Renard   WS getDataset wit...
857
		if (empty($vars["timeFormat"])) {
189a6f4f   Elena.Budnik   coorect timie
858
859
860
861
			$timeFormat = "ISO8601";
		}
		else {
			$timeFormat = $vars["timeFormat"];
1922ad04   Elena.Budnik   getOrbites()
862
		}
9846ec70   Benjamin Renard   WS getDataset wit...
863
864
865
866

		if (empty($vars["spacecraft"])) {
			$this->throwError("requestError", "Missing spacecraft definition");
		}
1922ad04   Elena.Budnik   getOrbites()
867
		$spacecraft = $vars["spacecraft"];
9846ec70   Benjamin Renard   WS getDataset wit...
868
869
870
871

		if (empty($vars["coordinateSystem"])) {
			$this->throwError("requestError", "Missing coordinateSystem definition");
		}
1922ad04   Elena.Budnik   getOrbites()
872
873
		$coordinateSystem = $vars["coordinateSystem"];

9846ec70   Benjamin Renard   WS getDataset wit...
874
		if (empty($vars["units"]))
1922ad04   Elena.Budnik   getOrbites()
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
			$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']);

9846ec70   Benjamin Renard   WS getDataset wit...
891
		if (empty($vars["gzip"]))
1922ad04   Elena.Budnik   getOrbites()
892
893
			$gzip = 0;
		else
9846ec70   Benjamin Renard   WS getDataset wit...
894
895
896
897
898
			$gzip = ($vars["gzip"] == 1);

		$sampling = !empty($vars["sampling"]) ? $vars["sampling"] : NULL;

		$outputFormat = !empty($vars["outputFormat"]) ? $vars["outputFormat"] : 'ASCII';
1922ad04   Elena.Budnik   getOrbites()
899
			
9846ec70   Benjamin Renard   WS getDataset wit...
900
		$this->service = strtolower(__FUNCTION__);
1922ad04   Elena.Budnik   getOrbites()
901
902
      
		$res = $this->doDownloadRequest(
9846ec70   Benjamin Renard   WS getDataset wit...
903
					array("startTime" => $vars["startTime"], "stopTime" => $vars["stopTime"], "sampling" => $sampling),
1922ad04   Elena.Budnik   getOrbites()
904
					array("params" => $paramId),
9846ec70   Benjamin Renard   WS getDataset wit...
905
					array("format" => $outputFormat, "timeFormat"=> $timeFormat, "gzip"=>$gzip), $orbitParam['parameterID']);
1922ad04   Elena.Budnik   getOrbites()
906
907
908
909
910
 	 
		if ($res['success']) return $res;
 
		$this->throwError("serverError",$res['message']);    
	}
2b26bbdd   Elena.Budnik   getPlot partial c...
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926

/*
*  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
927
		
189a6f4f   Elena.Budnik   coorect timie
928
929
930
931
932
933
934
935
936
		if (is_numeric($vars["startTime"])) {
			$this->checkInputTime($vars["startTime"],$vars["stopTime"]);
			$vars["startTime"] = date("Y-m-d\TH:i:s", $vars["startTime"]);
			$vars["stopTime"] = date("Y-m-d\TH:i:s", $vars["stopTime"]);
		}
		else {
			$this->checkInputTime(strtotime($vars["startTime"]),strtotime($vars["stopTime"])); 
		}
	 
b8194303   Elena.Budnik   getPlot final
937
938
		$resultFilePrefix = strtolower(__FUNCTION__)."_".$mission."_".date("YmdHms",strtotime($vars["startTime"]))."_".date("YmdHms",strtotime($vars["stopTime"]));
		
98c5bd44   Benjamin Renard   Introduce anonymo...
939
		if ($this->userID != WSConfigClass::getAnonymousUserName()) 
b8194303   Elena.Budnik   getPlot final
940
			$resultFilePrefix .= "_".$this->userID;
2b26bbdd   Elena.Budnik   getPlot partial c...
941
942

		$dom = new DomDocument("1.0");
e48a45d7   Elena.Budnik   WebServices folder
943
944
		if (!@$dom->load(WSConfigClass::getXslDir()."AmdaPlots.xml"))
			$this->throwError("systemError", "Cannot load predefined plot definition"); ;
2b26bbdd   Elena.Budnik   getPlot partial c...
945
946
947
948
949
950
951
952
953
954
955

		$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
956
			"result-file" => $resultFilePrefix,
2b26bbdd   Elena.Budnik   getPlot partial c...
957
958
959
			"timesrc" => "Interval",
			"startDate" => $vars["startTime"],
			"stopDate" => $vars["stopTime"],
b8194303   Elena.Budnik   getPlot final
960
			"parameters" => array()
2b26bbdd   Elena.Budnik   getPlot partial c...
961
962
		);
    
2b26bbdd   Elena.Budnik   getPlot partial c...
963
964
		foreach ($paramsList as $paramToPlot)
		{
2b26bbdd   Elena.Budnik   getPlot partial c...
965
			$paramObject = (Object) array(
b8194303   Elena.Budnik   getPlot final
966
				"paramid" => $paramToPlot
2b26bbdd   Elena.Budnik   getPlot partial c...
967
			);
2b26bbdd   Elena.Budnik   getPlot partial c...
968
			
b8194303   Elena.Budnik   getPlot final
969
			$requestObject->{"parameters"}[] = $paramObject;
2b26bbdd   Elena.Budnik   getPlot partial c...
970
		}
b8194303   Elena.Budnik   getPlot final
971

2b26bbdd   Elena.Budnik   getPlot partial c...
972
973
974
975
976
977
978
979
980
981
		$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
982
983
984
985
986
987
988
989
990
991
992
993
994
995
		
		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...
996
	}
16035364   Benjamin Renard   First commit
997
}
cd1dd332   Elena.Budnik   getTimeTable
998
?>