RemoteDataCenterClass.php 5.5 KB
<?php
/**
 * @class RemoteDataCenterClass  
 * @brief   Server
 * @details
 */
abstract class RemoteDataCenterClass
{
	protected $url = null, $WSDL = null, $treeXML = null;	
		
	protected $needsArgs = false;
	protected $hasAccessUrl = false;

	public $domAmda = null, $dataCenter = null;  
	public $domAmdaName = "base.xml";
	public $additionalArgs = null;
	public $baseID = null;
	public $location;
	
	public $tree = null;
	protected $DDserverDir;
	
   public static $MAX_NAME_LENGTH = 31;
   public static $MAX_VI_NAME_LENGTH = 16;
   
	/*
	 * @brief Constructor
	*/
	function __construct()
	{		
		$this->baseID = get_class($this);
		$this->location = RemoteData."/bases/".$this->baseID;
		date_default_timezone_set('UTC');
		$this->DDserverDir = RemoteData."/DDServer/".$this->baseID;
	}
	
	/*    Function to change  External Bases stuff in case of existing AMDA aliases.
		Uses file  DICTIONARY_DIR.mapBaseID.xml */

	protected function alias($Dictionary, $name) {

		$xmldoc =  new DomDocument("1.0");
		$xmldoc->load($DictionaryFile);
		$item = $xmldoc->getElementById(strtoupper($name));
		
		if (!$item) return $name;      
		$alias =  $item->nodeValue;

	return $alias;
	}
	
	
	public function param2dd($paramID) 
	{
		$pairs = array(" " => "_","-" => "_","/" => "_","%" => "_","\\" => "_","$" => "_",":" => "_","+" =>"_","#" => "_",
							"@" => "_","." => "_", ">" => "_", "<" => "_", "," => "_", ")" => "", "(" => "_"); 

		return strtr($paramID,$pairs); 
	}
	
	public function saveProxy()
	{		
		$this->domAmda->save($this->location."/".$this->domAmdaName);		 		
	}
	
	abstract public function init(); 
	abstract protected function getRemoteTree();
	abstract protected function setDataCenterAttributes();
	abstract protected function makeArgumentsList();
//	abstract public function getDatasetInfo($ds);	
//	abstract public function getData($ds, $start, $stop);
//	abstract protected function convert2nc();
// abstract protected function createDatasetNodes();
// abstract protected function createParameterNodes();
// abstract protected function createDatasetGroupNodes($data);
	
	public function initProxy() 
	{	 
		if (!is_dir($this->location))
				mkdir($this->location, 0775, true);
		
		if (!is_dir($this->DDserverDir)) 
				mkdir($this->DDserverDir, 0775, true);		
		
		$this->init();
		
		$this->getRemoteTree();	
	} 
	
	public function makeProxy()
	{	
		$this->domAmda = new DOMDocument('1.0', 'utf-8');
		$this->domAmda->formatOutput = TRUE;
		$this->domAmda->preserveWhiteSpace = FALSE;
		
		$dataRoot = $this->domAmda->createElement('dataRoot');
		$dataRoot->setAttribute('xml:id', 'myRemoteData-treeRootNode');
		$this->domAmda->appendChild($dataRoot);	
		
		$this->dataCenter=$this->domAmda->createElement('dataCenter');

		$this->dataCenter->setAttribute('xml:id', $this->baseID);
		$this->dataCenter->setAttribute('name', $this->baseID);		 
		
		$dataRoot->appendChild($this->dataCenter);
		
		$this->setDataCenterAttributes();	
		
		$missionNodes = $this->createMissionNodes();
		
		foreach ($missionNodes as $missionNode)
			$this->dataCenter->appendChild($missionNode);
	}

	protected function getIdFromSpase($spaseId) 
	{
			$temp = explode('/',$spaseId);
			return $temp[count($temp)-1]; 
	}
	
	public function monitor()
	{		 		
		$ch = curl_init($this->url."/".$this->WSDL);  
		curl_setopt($ch, CURLOPT_TIMEOUT, 3);  
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);  
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
		$data = curl_exec($ch);  
		$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);		
		curl_close($ch); 
		
		if($httpcode >= 200 && $httpcode < 300)
		{  
			return true;  
		} else 
		{  
			return false;  
		}  
	}
	public function updateBufferDataBase()
	{
        $DDsys = new DomDocument("1.0");
        
        if (file_exists(DDBASE."DDsys.xml") && $DDsys->load(DDBASE."DDsys.xml")) {
        
            $currDir = getcwd();
            $xpath = new domxpath($DDsys);        
            $datasets = $xpath->query("//NAME[@base='".$this->baseID."']");

            foreach ($datasets as $ds) {
                $vi = $ds->parentNode;
                $id = $ds->nodeValue;

                foreach ($vi->childNodes as $pp) {
                    if ($pp->nodeName == "LOCATION")
                        $loc = $pp->nodeValue;
                    if ($pp->nodeName == "INFO")
                        $info = $pp->nodeValue;
                }
                if (file_exists($loc.$info)) {
                    $infoXml = str_replace(".nc",".xml", $info);
                    if (is_dir($this->DDserverDir)) {
                        $ddInfoXml = $this->DDserverDir."/".strtoupper($id).".xml";
                        if (file_exists($ddInfoXml)) {                                                    
                            chdir($loc);
                            copy($ddInfoXml,$infoXml);
                            system("infoLocal2nc ".$infoXml." ".$info);
                            chdir($currDir); 
                        }
                        else {
                            echo 'Cannot update '.$id.' in DDBASE : No '.$ddInfoXml.PHP_EOL; 
                        }
                    }
                    else {
                        echo 'Cannot update '.$tid.' in DDBASE : No '.$loc.$info.PHP_EOL; 
                    }
                } 
                else {
                    echo 'Cannot update '.$this->baseID.' in DDBASE : No DDServer INFO Dir'.PHP_EOL;
                }
            }
        }
        else {
          echo 'Cannot update '.$this->baseID.' in DDBASE : Cannot load DDsys.xml'.PHP_EOL;        
        }
	}
}
?>