Blame view

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

class RemoteParamManager
{ 
	protected $Bases, $basesDom;	 	
	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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 
	function __construct() 
	{
		if (!file_exists(RemoteData."Bases.xml"))
				return array("err" => "No Remote Data Bases");

		$this->basesDom = new DomDocument("1.0");
		if (!$this->basesDom->load(RemoteData."Bases.xml"))
				return array("err" => "Can not load Remote Data Bases definitions");
		$bases = $this->basesDom->getElementsByTagName('dataCenter');
		
		foreach ($bases as $base) 
			$this->Bases[] = $base->getAttribute('xml:id');
		
		if (!file_exists(USERWSDIR."RemoteParams.xml"))
				return array("err" => "No RemoteParams.xml");
				
0dc31ba8   Elena.Budnik   remoteParamManage...
33
		if (!is_dir(RemoteData."/PARAMS"))
05961422   Elena.Budnik   permissions on dirs
34
				mkdir(RemoteData."/PARAMS", 0775, true);
0dc31ba8   Elena.Budnik   remoteParamManage...
35
36
				
		if (!is_dir(RemoteData."/PARAMS_INFO"))
05961422   Elena.Budnik   permissions on dirs
37
				mkdir(RemoteData."/PARAMS_INFO", 0775, true);
0dc31ba8   Elena.Budnik   remoteParamManage...
38
				
74b77f58   Elena.Budnik   init remote branch
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
		$this->xmlName = USERWSDIR."RemoteParams.xml";
		$this->xmlDom = new DomDocument("1.0");
		$this->xmlDom->load($this->xmlName);
	}

	/*
	*  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...
62
		$this->center = new $baseId(); 	
74b77f58   Elena.Budnik   init remote branch
63
	}
25f87ff8   Elena.Budnik   info for remote
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
	
// <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
88
89
90
91
92
	public function makeInternalParamXml()
	{
		if (!$this->center->ViId) return false;
		if (!$this->center->ParamId) return false;
		
d3f8a6d0   Elena.Budnik   internal paramID
93
	//	$this->paramId = strtolower($this->center->baseID."_".$this->center->ViId."_".$this->center->ParamId);
92edb3d1   Elena.Budnik   copy remote xml t...
94
95
96
97
98
99
100
101
		
		$xmlNameRemote = RemoteData."/PARAMS/".$this->paramId.".xml";
		$xmlNameTemp = PARAMS_LOCALDB_DIR."/".$this->paramId.".xml";
		
		if (file_exists($xmlNameRemote)) {
					return copy($xmlNameRemote, $xmlNameTemp);
		}
		
74b77f58   Elena.Budnik   init remote branch
102
103
104
105
106
		$xml = new DomDocument("1.0");
		$paramNode = $xml->createElement("param");
		$xml->appendChild($paramNode);
    
		$paramNode->setAttribute("xml:id", $this->paramId);
25f87ff8   Elena.Budnik   info for remote
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
		
		$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...
123
		
25f87ff8   Elena.Budnik   info for remote
124
125
126
127
128
129
130
131
132
133
		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...
134
135
		$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
136
137
138
		
		$getNode = $xml->createElement("get");
		$viNode = $xml->createElement("vi");
74b77f58   Elena.Budnik   init remote branch
139
140
141
142
		$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
143
144
145
		$getNode->appendChild($viNode);
		
		$paramNode->appendChild($infoNode);
74b77f58   Elena.Budnik   init remote branch
146
147
148
149
		$paramNode->appendChild($getNode);
		$paramNode->appendChild($xml->createElement("process"));
		$paramNode->appendChild($xml->createElement("output"));
		 
92edb3d1   Elena.Budnik   copy remote xml t...
150
		$res = $xml->save($xmlNameRemote);
05961422   Elena.Budnik   permissions on dirs
151

92edb3d1   Elena.Budnik   copy remote xml t...
152
153
		if ($res) 
				return copy($xmlNameRemote, $xmlNameTemp);
74b77f58   Elena.Budnik   init remote branch
154
		
92edb3d1   Elena.Budnik   copy remote xml t...
155
		return $res;
74b77f58   Elena.Budnik   init remote branch
156
157
	}
	
74b77f58   Elena.Budnik   init remote branch
158
159
	protected function makeComponents($node, $size, $components)
 	{
25f87ff8   Elena.Budnik   info for remote
160
		$compArray = explode(",",$components);
74b77f58   Elena.Budnik   init remote branch
161
162
163
164
165
166
167
168
169
170
171
172
173
174
		
		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
175
176
177
		$this->paramId = strtr(strtolower($id),":","_");
		
		if ($this->xmlDom->getElementById($this->paramId)) return true;
74b77f58   Elena.Budnik   init remote branch
178
179
180

		// Node to be added
		$nodeRemote = $this->center->baseDom->getElementById($id);
74b77f58   Elena.Budnik   init remote branch
181
182
183
184
185
186
187
		// No such node in base.xml
		if (!$nodeRemote) return false;

		if ($nodeRemote->tagName == 'dataset') 
		{
			$this->center->setViId($nodeRemote->getAttribute('name'));
			$status = $this->center->addViToDD();
05961422   Elena.Budnik   permissions on dirs
188
		 	
74b77f58   Elena.Budnik   init remote branch
189
			if (!$status) return false;
f286b43c   Elena.Budnik   process response ...
190

b7741da9   Benjamin Renard   Define DDService ...
191
			$remoteDatasetInfo = DDSERVICE."/BASE/INFO/bases/".$this->center->baseID."/".$this->center->infoFile; 
74b77f58   Elena.Budnik   init remote branch
192
			$localDatasetInfo = RemoteData.$this->center->baseID."/".$this->center->infoFile;
05961422   Elena.Budnik   permissions on dirs
193
	
74b77f58   Elena.Budnik   init remote branch
194
195
196
197
198
199
200
201
202
203
			if (!copy($remoteDatasetInfo,$localDatasetInfo)) return false;
		}

		$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
204

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

74b77f58   Elena.Budnik   init remote branch
207
208
209
210
211
212
			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...
213
214
215
			
			// convert remote paramID into AMDA paramID			
			$node->setAttribute("xml:id", strtr(strtolower($node->getAttribute("xml:id")), ":","_"));
74b77f58   Elena.Budnik   init remote branch
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
		}
	
		$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...
267
	{	   
74b77f58   Elena.Budnik   init remote branch
268
269
270
271
272
273
		if (count($obj) == 1) 
		{	    
			$id = $obj->id;
			
			if ($id == 'root') return array('res' => 'ok');

d3f8a6d0   Elena.Budnik   internal paramID
274
275
			$this->setBaseId($id);
						
74b77f58   Elena.Budnik   init remote branch
276
277
278
279
280
281
282
283
284
285
286
287
288
			$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
289
				
74b77f58   Elena.Budnik   init remote branch
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
				$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;
		
		while ( $tagName != "dataRoot" ) // "dataCenter" ?
		{
			$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...
329
330
331
332
333
334
335
336
	
	public function getInfoName($datasetId) {
	  
// 	      if ($this->baseId == 'CDAWEB')
// 				return  strtolower($datasetId)."_00000000_v01.cdf";
		return  $datasetId.'.xml';

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