Blame view

php/RemoteDataCenter/RemoteParamManager.php 9.64 KB
74b77f58   Elena.Budnik   init remote branch
1
2
3
4
5
6
7
8
<?php
/**
 * @class RemoteParamManager 
 * @brief Manage Remote Data Centers via DDServer  
 */

class RemoteParamManager
{ 
2b8ea2a7   Elena.Budnik   RemoteParamManage...
9
	public $Bases, $basesDom;	 	
74b77f58   Elena.Budnik   init remote branch
10
11
12
13
14
	protected $baseDom;
	
	protected $center;
	
	public $xmlDom, $xmlName;
0dc31ba8   Elena.Budnik   remoteParamManage...
15
	public $baseId, $paramId, $remoteViId, $localInfo, $paramDom, $paramXML;
74b77f58   Elena.Budnik   init remote branch
16
17
18
 
	function __construct() 
	{
2b8ea2a7   Elena.Budnik   RemoteParamManage...
19
20
21
22
		$this->basesDom = new DomDocument("1.0");

		if (!is_dir(RemoteData."/PARAMS"))
				mkdir(RemoteData."/PARAMS", 0775, true);
d444ba30   Elena.Budnik   mkdir...,0775 doe...
23
		chmod(RemoteData."/PARAMS",0775);
2b8ea2a7   Elena.Budnik   RemoteParamManage...
24
25
26
				
		if (!is_dir(RemoteData."/PARAMS_INFO"))
				mkdir(RemoteData."/PARAMS_INFO", 0775, true);
d444ba30   Elena.Budnik   mkdir...,0775 doe...
27
28
		chmod(RemoteData."/PARAMS_INFO",0775);
		
2b8ea2a7   Elena.Budnik   RemoteParamManage...
29
30
31
32
33
34
35
		$this->xmlName = USERWSDIR."RemoteParams.xml";
		$this->xmlDom = new DomDocument("1.0");
		
	}

	public function init() {
	
74b77f58   Elena.Budnik   init remote branch
36
37
		if (!file_exists(RemoteData."Bases.xml"))
				return array("err" => "No Remote Data Bases");
2b8ea2a7   Elena.Budnik   RemoteParamManage...
38
				
74b77f58   Elena.Budnik   init remote branch
39
40
		if (!$this->basesDom->load(RemoteData."Bases.xml"))
				return array("err" => "Can not load Remote Data Bases definitions");
2b8ea2a7   Elena.Budnik   RemoteParamManage...
41
		
74b77f58   Elena.Budnik   init remote branch
42
43
44
45
		$bases = $this->basesDom->getElementsByTagName('dataCenter');
		
		foreach ($bases as $base) 
			$this->Bases[] = $base->getAttribute('xml:id');
2b8ea2a7   Elena.Budnik   RemoteParamManage...
46
			
74b77f58   Elena.Budnik   init remote branch
47
		if (!file_exists(USERWSDIR."RemoteParams.xml"))
2b8ea2a7   Elena.Budnik   RemoteParamManage...
48
				return array("err" => "RemoteParams file error");
74b77f58   Elena.Budnik   init remote branch
49
				
2b8ea2a7   Elena.Budnik   RemoteParamManage...
50
51
52
53
		if (!$this->xmlDom->load($this->xmlName))
			return array("err" => "RemoteParams file error");
		
		return array("success" => true);
74b77f58   Elena.Budnik   init remote branch
54
	}
2b8ea2a7   Elena.Budnik   RemoteParamManage...
55
	
74b77f58   Elena.Budnik   init remote branch
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	/*
	*  get baseId from parameter descriptor
	*/
	protected function setBaseId($id) 
	{ 
		foreach ($this->Bases as $base) {
			// Special Themis case
			if (substr($id,0,2) == "th") {
				$baseId = "THEMIS";
				break;
			}
			if (strncmp($id, $base, strlen($base)) === 0) 
			{
				$baseId = $base;
				break;
			}
		}
		
92edb3d1   Elena.Budnik   copy remote xml t...
74
		$this->center = new $baseId(); 	
74b77f58   Elena.Budnik   init remote branch
75
	}
25f87ff8   Elena.Budnik   info for remote
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
	
// <param xml:id="imf">
//   <info>
//     <name>imf</name>
//     <short_name>b_gse</short_name>
//     <components>bx,by,bz</components>
//     <units>nT</units>
//     <coordinates_system>GSE</coordinates_system>
//     <tensor_order/>
//     <si_conversion>1e-9&gt;T</si_conversion>
//     <fill_value>-1.0E31</fill_value>
//     <ucd>phys.magField</ucd>
//     <dataset_id>ace-imf-all</dataset_id>
//     <instrument_id>ACE_MAG</instrument_id>
//   </info>
//   <get>
//      <vi name="ace:imf:all">
//         <baseParam name="IMF"/>
//     </vi>
//   </get>
//   <process/>
//   <output/>
// </param>

74b77f58   Elena.Budnik   init remote branch
100
101
102
103
104
	public function makeInternalParamXml()
	{
		if (!$this->center->ViId) return false;
		if (!$this->center->ParamId) return false;
		
d3f8a6d0   Elena.Budnik   internal paramID
105
	//	$this->paramId = strtolower($this->center->baseID."_".$this->center->ViId."_".$this->center->ParamId);
92edb3d1   Elena.Budnik   copy remote xml t...
106
107
108
109
		
		$xmlNameRemote = RemoteData."/PARAMS/".$this->paramId.".xml";
		$xmlNameTemp = PARAMS_LOCALDB_DIR."/".$this->paramId.".xml";
		
9c04c40e   Elena.Budnik   do not copy remot...
110
111
112
113
		if (file_exists($xmlNameTemp)) {
			return true;
		}
			
92edb3d1   Elena.Budnik   copy remote xml t...
114
		if (file_exists($xmlNameRemote)) {
9c04c40e   Elena.Budnik   do not copy remot...
115
			return copy($xmlNameRemote, $xmlNameTemp);
92edb3d1   Elena.Budnik   copy remote xml t...
116
117
		}
		
74b77f58   Elena.Budnik   init remote branch
118
119
120
121
122
		$xml = new DomDocument("1.0");
		$paramNode = $xml->createElement("param");
		$xml->appendChild($paramNode);
    
		$paramNode->setAttribute("xml:id", $this->paramId);
25f87ff8   Elena.Budnik   info for remote
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
		
		$infoNode = $xml->createElement("info");
		$infoNode->appendChild($xml->createElement("name",strtolower($this->center->ParamId)));
		$infoNode->appendChild($xml->createElement("short_name",strtolower($this->center->ParamId)));
		
		$size = $this->center->getParamSize();
		//TODO spectra components 
		if ($size > 1) 
		{	
			$components = strtolower($this->center->getParamComponents());	
		}
		else {
			$components = null;
		}
		
		$fillValue = $this->center->getParamFillValue();
fe74bf8e   Elena.Budnik   instrument id for...
139
		
25f87ff8   Elena.Budnik   info for remote
140
141
142
143
144
145
146
147
148
149
		if (!$fillValue) 
				$fillValue = null;
				
		$infoNode->appendChild($xml->createElement("components",$components));
		$infoNode->appendChild($xml->createElement("units",$this->center->getParamUnits()));
		$infoNode->appendChild($xml->createElement("coordinates_system"));
		$infoNode->appendChild($xml->createElement("tensor_order"));
		$infoNode->appendChild($xml->createElement("si_conversion"));
		$infoNode->appendChild($xml->createElement("fill_value", $fillValue));
		$infoNode->appendChild($xml->createElement("ucd"));
fe74bf8e   Elena.Budnik   instrument id for...
150
151
		$infoNode->appendChild($xml->createElement("dataset_id",strtolower($this->center->ViId)));
		$infoNode->appendChild($xml->createElement("instrument_id",strtolower($this->center->baseID." ".$this->center->ViId)));
25f87ff8   Elena.Budnik   info for remote
152
153
154
		
		$getNode = $xml->createElement("get");
		$viNode = $xml->createElement("vi");
74b77f58   Elena.Budnik   init remote branch
155
156
157
158
		$baseParamNode = $xml->createElement("baseParam");
		$baseParamNode->setAttribute("name", $this->center->ParamId);
		$viNode->setAttribute("name", strtolower(strtr($this->center->ViId,"_", ":")));
		$viNode->appendChild($baseParamNode);
25f87ff8   Elena.Budnik   info for remote
159
160
161
		$getNode->appendChild($viNode);
		
		$paramNode->appendChild($infoNode);
74b77f58   Elena.Budnik   init remote branch
162
163
164
165
		$paramNode->appendChild($getNode);
		$paramNode->appendChild($xml->createElement("process"));
		$paramNode->appendChild($xml->createElement("output"));
		 
92edb3d1   Elena.Budnik   copy remote xml t...
166
		$res = $xml->save($xmlNameRemote);
05961422   Elena.Budnik   permissions on dirs
167

92edb3d1   Elena.Budnik   copy remote xml t...
168
169
		if ($res) 
				return copy($xmlNameRemote, $xmlNameTemp);
74b77f58   Elena.Budnik   init remote branch
170
		
92edb3d1   Elena.Budnik   copy remote xml t...
171
		return $res;
74b77f58   Elena.Budnik   init remote branch
172
173
	}
	
74b77f58   Elena.Budnik   init remote branch
174
175
	protected function makeComponents($node, $size, $components)
 	{
25f87ff8   Elena.Budnik   info for remote
176
		$compArray = explode(",",$components);
74b77f58   Elena.Budnik   init remote branch
177
178
179
180
181
182
183
184
185
186
187
188
189
190
		
		for ($i = 0; $i < $size; $i++)
		{
			$compNode = $this->xmlDom->createElement("component");			
			$compNode->setAttribute('xml:id',$this->paramId."($i)");		
			$compNode->setAttribute('name',strtolower($compArray[$i])); // LABEL

			$node->appendChild($compNode);
		}
 	}
 	
	protected function addNode($id)
	{
		// Node exists already 
d3f8a6d0   Elena.Budnik   internal paramID
191
192
193
		$this->paramId = strtr(strtolower($id),":","_");
		
		if ($this->xmlDom->getElementById($this->paramId)) return true;
74b77f58   Elena.Budnik   init remote branch
194
195
196

		// Node to be added
		$nodeRemote = $this->center->baseDom->getElementById($id);
74b77f58   Elena.Budnik   init remote branch
197
198
199
200
201
202
203
		// No such node in base.xml
		if (!$nodeRemote) return false;

		if ($nodeRemote->tagName == 'dataset') 
		{
			$this->center->setViId($nodeRemote->getAttribute('name'));
			$status = $this->center->addViToDD();
9c04c40e   Elena.Budnik   do not copy remot...
204

74b77f58   Elena.Budnik   init remote branch
205
			if (!$status) return false;
f286b43c   Elena.Budnik   process response ...
206

b7741da9   Benjamin Renard   Define DDService ...
207
			$remoteDatasetInfo = DDSERVICE."/BASE/INFO/bases/".$this->center->baseID."/".$this->center->infoFile; 
74b77f58   Elena.Budnik   init remote branch
208
			$localDatasetInfo = RemoteData.$this->center->baseID."/".$this->center->infoFile;
9c04c40e   Elena.Budnik   do not copy remot...
209

74b77f58   Elena.Budnik   init remote branch
210
			if (!copy($remoteDatasetInfo,$localDatasetInfo)) return false;
9c04c40e   Elena.Budnik   do not copy remot...
211
		}			 	
74b77f58   Elena.Budnik   init remote branch
212
213
214
215
216
217
218
219

		$node = $this->xmlDom->importNode($nodeRemote);

		if ($nodeRemote->tagName == 'parameter') 
		{
			$this->center->setParamId($nodeRemote->getAttribute('name'));
			$this->center->setViId($nodeRemote->parentNode->getAttribute('name'));
			$this->center->setInfoFile();
05961422   Elena.Budnik   permissions on dirs
220

74b77f58   Elena.Budnik   init remote branch
221
			if (!$this->makeInternalParamXml()) return false;
05961422   Elena.Budnik   permissions on dirs
222

74b77f58   Elena.Budnik   init remote branch
223
224
225
226
227
228
			if (($size = $this->center->getParamSize()) > 1)
			{	
				// make components and args
				$components = $this->center->getParamComponents();	
				$this->makeComponents($node, $size, $components); // return false;							
			}
92edb3d1   Elena.Budnik   copy remote xml t...
229
230
231
			
			// convert remote paramID into AMDA paramID			
			$node->setAttribute("xml:id", strtr(strtolower($node->getAttribute("xml:id")), ":","_"));
74b77f58   Elena.Budnik   init remote branch
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
		}
	
		$parentRemote= $nodeRemote->parentNode;		  
		$parentRemoteId = $parentRemote->getAttribute('xml:id');

		$parent = $this->xmlDom->getElementById($parentRemoteId);
		
		if (!$parent) 
		{
			$parent = $this->xmlDom->importNode($parentRemote); 		      
		}
		
		$parent->appendChild($node); 
                 
		$toAddDataCentertToDoc = false;

		while ($parent->tagName != 'dataCenter') 
		{
			$node = $parent;	
			$parentRemote = $parentRemote->parentNode;        
			$parentRemoteId = $parentRemote->getAttribute('xml:id');
			$parent = $this->xmlDom->getElementById($parentRemoteId);

			if (!$parent) 
			{
				if ($parentRemote->tagName == 'dataCenter') 
					$toAddDataCenterToDoc = true;
				$parent = $this->xmlDom->importNode($parentRemote);			   
				$parent->appendChild($node); 		      
			}          		        		     
		}	

		if ($toAddDataCenterToDoc) 
		{
			//TODO if this is necessary ???
			// special bases 'hand-made' descriptions
			$basesDom = new DomDocument("1.0");
			$basesDom -> load(RemoteData.'Bases.xml');
			$theBase = $basesDom->getElementById($parent->getAttribute('xml:id'));

			if ($theBase) $parent -> setAttribute('name', $theBase->getAttribute('name'));
			$this->xmlDom->documentElement->appendChild($parent);
		}
		
		return true;
	}
	
/*
*         PUBLIC FUNCTIONS
*/
	public function saveTree($obj) 
92edb3d1   Elena.Budnik   copy remote xml t...
283
	{	   
74b77f58   Elena.Budnik   init remote branch
284
285
286
287
288
289
		if (count($obj) == 1) 
		{	    
			$id = $obj->id;
			
			if ($id == 'root') return array('res' => 'ok');

d3f8a6d0   Elena.Budnik   internal paramID
290
291
			$this->setBaseId($id);
						
74b77f58   Elena.Budnik   init remote branch
292
293
294
295
296
297
298
299
300
301
302
303
304
			$res = $this->addNode($id);	
			
			if ($res === false) return array("err" => "Cannot add node : $id");
		}
		else 
		{ 
			foreach ($obj as $o) 
			{
				$id = $o->id;	
				
				if ($id == 'root') continue;
				
				if (!$this->baseId) $this->setBaseId($id);				
d3f8a6d0   Elena.Budnik   internal paramID
305
				
74b77f58   Elena.Budnik   init remote branch
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
				$res = $this->addNode($id);
				
				if ($res === false) return array("err" => "Cannot add node : $id");

			}
		}

		if (!$this->xmlDom->save($this->xmlName))
			return array("err" => "Cannot save RemoteParams.xml");
			
		return array('res' => 'ok');
	}
	
	public function deleteFromTree($obj) 
	{
		$id = $obj->id;
		$nodeToDelete = $this->xmlDom->getElementById($id);
		
		if (!$nodeToDelete) 
			return array("err" => "No such id : $id");
		
		$tagName = $nodeToDelete->tagName;
c344b37f   Elena.Budnik   process simuParam...
328
		$topTag = "dataCenter"; //"dataRoot";
74b77f58   Elena.Budnik   init remote branch
329
		
c344b37f   Elena.Budnik   process simuParam...
330
331
332
333
		if ($obj->nodeType && $obj->nodeType == "remoteSimuParam")  // FMI_GUMICS case
				$topTag = "simulationRegion";
		 
		while ( $tagName != $topTag ) // "dataCenter" ?
74b77f58   Elena.Budnik   init remote branch
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
		{
			$parentNode = $nodeToDelete->parentNode;
			$parentNode->removeChild($nodeToDelete);
			$otherChildren = $parentNode->getElementsByTagName($tagName);
			
			if ( $otherChildren->length > 0 ) 
					break;
					
			 $nodeToDelete = $parentNode;
			 $tagName = $nodeToDelete->tagName;
		}
		$this->xmlDom->save($this->xmlName);

		return array('res'=> $obj->id);
	}
0dc31ba8   Elena.Budnik   remoteParamManage...
349
350
351
352
353
354
355
356
	
	public function getInfoName($datasetId) {
	  
// 	      if ($this->baseId == 'CDAWEB')
// 				return  strtolower($datasetId)."_00000000_v01.cdf";
		return  $datasetId.'.xml';

	}
74b77f58   Elena.Budnik   init remote branch
357
}
b7741da9   Benjamin Renard   Define DDService ...
358
?>