ParamInfoRequestClass.php 5.15 KB
<?php 
/**
 * @class ParamInfoRequestClass
 * @brief Implementation of a ParamInfoRequestClass for a param info request
 * @details
 */
class ParamInfoRequestClass extends RequestAbstractClass
{
	/*
	 * @brief Init a param info request
	*/
	public function init()
	{
		if (!isset($this->requestData))
			return false;

		if ($this->requestData->getType() == ParamInfoTypeEnumClass::PLOTINIT
			|| $this->requestData->getType() == ParamInfoTypeEnumClass::IMPEXPLOTINIT) {
			//Force load of node files to init the NodeFactory
			foreach (glob(dirname(__FILE__)."/ParamsRequestImpl/Nodes/Requests/*NodeClass.php") as $filename) {
				require_once $filename;
			}
		}
		
		return TRUE;
	}
	
	/*
	 * @brief Run a param info request
	 */
	public function run()
	{
		if ($this->requestData->getType() == ParamInfoTypeEnumClass::IMPEXINFO)
		{
		   $this->requestData->setSuccess(true);
		   
		   $result = array();
		   $result['dimensions'] = array(
				'dim1' => 1,
				'dim2' => 1
			);
			
		   $this->requestData->setResult($result);
		   return $this->requestData->getSuccess();
		}

		$filePath = $this->requestData->getFilePath();
		if (!empty($filePath)) {
			//Load info from a XML file
			$dom = new DOMDocument("1.0","UTF-8");
			$dom->preserveWhiteSpace = false;
			$dom->formatOutput = true;
		
			$res = @$dom->load($this->requestData->getFilePath());
 		
			$this->requestData->setSuccess(false);
			
			if (!$res) {
				$this->requestData->setLastErrorMessage("Cannot load file".$this->requestData->getFilePath());
				return false;
			}

			switch ($this->requestData->getType()) {
				case ParamInfoTypeEnumClass::IMPEXPLOTINIT :
				case ParamInfoTypeEnumClass::PLOTINIT :	
					$this->runFromParamFile($dom);					
					break;			
				case ParamInfoTypeEnumClass::PARAMINFO :
					$this->runFromParamInfoFile($dom);
					break;
				default :
					$this->requestData->setLastErrorMessage("Unknown ParamInfo type ");
			}
		}
		else {
			//Use some internal info - Nothing to do
			$this->requestData->setSuccess(true);
		}
		 
		return $this->requestData->getSuccess();
	}

	
	private function runFromParamFile($dom) {
		$paramNode = new ParamNodeClass();
		
		try {
			$paramNode->loadFromNode($dom->documentElement);
		} catch (Exception $e) {
			$this->requestData->setLastErrorMessage("Error to parse parameter file : Exception detected : ".$e->getMessage());
			return false;
		}
		
		switch ($this->requestData->getType()) {
			case ParamInfoTypeEnumClass::IMPEXPLOTINIT :
			case ParamInfoTypeEnumClass::PLOTINIT :
				$outputNode = $paramNode->getOutput();
				if (!isset($outputNode)) {
					$this->requestData->setLastErrorMessage("Cannot parse output node");
					return false;
				}			 
				$plotNode = $outputNode->getChildInstanceByName("plot");
				if (!isset($plotNode)) {
					$this->requestData->setLastErrorMessage("Cannot parse plot node");
					return false;
				}				
				$this->requestData->setResult($plotNode);
				$this->requestData->setSuccess(true);
				return true;
			default :
				$this->requestData->setLastErrorMessage("Unknown param info request");
		}
		
		return false;
	}
	
	private function runFromParamInfoFile($dom) {
		$result = array();
		
		// Dimensions
		$dimsNodes = $dom->getElementsByTagName("dimensions");
		if ($dimsNodes->length > 0) {
			$dimsNode = $dimsNodes->item(0);
			$result['dimensions'] = array(
				'dim1' => $dimsNode->getAttribute("dim_1"),
				'dim2' => $dimsNode->getAttribute("dim_2")
			);
		}
		
		// Components
		$compNodes = $dom->getElementsByTagName("components");
		if ($compNodes->length > 0) {
			$compNode = $compNodes->item(0);
			$result['components'] = array();
			foreach ($compNode->getElementsByTagName("component") as $componentNode) {
				$result['components'][] = array(
					'index_1' => $componentNode->getAttribute("index_1"),
					'index_2' => $componentNode->getAttribute("index_2"),
					'name' => $componentNode->getAttribute("name")
				);
			} 
		}
		
		// Tables
		$tablesNodes = $dom->getElementsByTagName("tables");
		if ($tablesNodes->length > 0) {
			$tablesNode = $tablesNodes->item(0);
			$result['tables'] = array();
			foreach ($tablesNode->getElementsByTagName("table") as $tableNode) {
				$tableResult = array(
						'relatedDim' => ($tableNode->getAttribute("relatedDim") == "dim_1" ? "dim1" : "dim2"),
						'name' => $tableNode->getAttribute("name"),
						'units' => $tableNode->getAttribute("units"),
						'variable' => ($tableNode->getAttribute("variable") === "true"),
						'channels' => array(),
						'minmax' => array()
				);
				
				if (!$tableResult['variable']) {
					$channelNodes = $tableNode->getElementsByTagName("channel");
					foreach ($channelNodes as $channelNode) {
						$tableResult['channels'][] = array(
							'min' => $channelNode->getAttribute("min"),
							'max' => $channelNode->getAttribute("max")
						);
					}
				}
				else {
					if ($tableNode->hasAttribute("minValue")) {
						$tableResult['minmax'] = array(
							'min' => $tableNode->getAttribute("minValue"),
							'max' => $tableNode->getAttribute("maxValue")
						);
					}
				}
				
				$result['tables'][] = $tableResult;
			}
		}
		
		$this->requestData->setResult($result);
		$this->requestData->setSuccess(true);
		
		return true;
	}
}
?>