WSInputOutputParamsDownloadClass.php 5.74 KB
<?php

/**
 * @class WSInputOutputParamsDownloadClass
 * @brief Implementation of IHMInputOutputParamsAbstractClass to treat download request
 * @details
*/
class WSInputOutputParamsDownloadClass extends IHMInputOutputParamsAbstractClass
{
	/*
	 * @brief Constructor
	*/
	function __construct()
	{
		$this->paramManager     = new IHMParamManagerClass();
		$this->jobsManager      = new WSJobsManagerClass();
	}
	/*
	 * @brief method to unmarshall a download request
	*/
	protected function unmarshallRequest($input)
	{
		$requestNode = $this->paramsData->addRequestNode();
		
		$paramsNode = $requestNode->getParamsNode();

		//unmarshall time definition
		$this->unmarshallTimeDefinition($input, 0);

		//unmarshall download output definition
		$outputsNode = $requestNode->getOutputsNode();
		$downloadNode = $outputsNode->addNewOutput(RequestOutputTypeEnum::DOWNLOAD);

		switch ($input->timeformat)
		{
			case 'ISO08601' :
				$downloadNode->setTimeFormat(RequestOutputDownloadTimeFormatEnum::ISO);
				break;
			case 'unixtime' :
				$downloadNode->setTimeFormat(RequestOutputDownloadTimeFormatEnum::TIMESTAMP);
				break;
			default :
				$downloadNode->setTimeFormat(RequestOutputDownloadTimeFormatEnum::ISO);
		}

		switch ($input->fileformat)
		{
			case "CDF" :
				$downloadNode->setFileFormat(RequestOutputDownloadFileFormatEnum::CDF);
				$formatExtension = ".cdf";
				break;	
			case "ASCII" :
				$downloadNode->setFileFormat(RequestOutputDownloadFileFormatEnum::ASCII);
				$formatExtension = ".txt";
				break;				
			case "VOTable" :
				$downloadNode->setFileFormat(RequestOutputDownloadFileFormatEnum::VOTABLE);
				$formatExtension = ".vot";
				break;
			case "netCDF" :
				throw new Exception('File format '.$input->fileformat.' not implemented yet');
				break;
			default :
				$downloadNode->setFileFormat(RequestOutputDownloadFileFormatEnum::ASCII);
				$formatExtension = ".txt";
		}

		//add params to output
		$resultFile = WSInputOutputClass::getService();
		$isFirst = true;
		
		foreach ($input->list as $param)
		{
			$paramInfo = $this->paramManager->addExistingParam($param->paramid, $this->paramsData, $param->template_args);
		//	$this->paramManager->applyRangesAndIndexes($this->paramsData, $param, FALSE, $paramInfo);

			$downloadNode->addParam($paramInfo['id'],$paramInfo['indexes'],$paramInfo['calib_infos']);
			$paramsNode->addParam($paramInfo['id']);

			if ( WSInputOutputClass::getService() == WSConfigClass::DATASET && $isFirst) 
			{
				$resultFile .= "_".$this->getDatasetIdFromParamInfo($param->paramid);
				$isFirst = false;
			}
			elseif  ( WSInputOutputClass::getService() != WSConfigClass::DATASET )
			{
				$resultFile .= "_".$paramInfo['id'];
				if (!empty($paramInfo['indexes']))
					foreach ($paramInfo['indexes'] as $index)
						$resultFile .= "_".$index;
			}
			
		}

		if ($input->sampling)
		{	
			$downloadNode->setSamplingTime($input->sampling);
			$downloadNode->setStructure(RequestOutputDownloadStructureEnum::ONE_FILE);
		}	
		else {
				$downloadNode->setStructure(RequestOutputDownloadStructureEnum::ONE_FILE_PER_PARAMETER_PER_INTERVAL);
		}
		
		switch ($input->compression)
		{
			case "1" :
				$extension = $formatExtension.".gz";
				$downloadNode->addPostProcessing(RequestOutputPostProcessingEnumClass::GZIP);
				$resultFilePrefix = "output-";
				break;
			case "0" :
			default :
				$extension = $formatExtension;
				$resultFilePrefix = "output-";
		}
		
		$resultFile .= "_".date("Ymd\THis", strtotime($input->startDate))."_".date("Ymd\THis",strtotime($input->stopDate));

		$this->paramsData->addWaitingResult(WSInputOutputClass::getService(), $resultFile.$extension);
		
		// internal kernel output
		$postProcessCmd  = "mv ".$resultFilePrefix."*";
		
		$postProcessCmd .= $extension;
		$postProcessCmd .= " ".WSConfigClass::getWsResultDir().$resultFile.$extension;
	
		$this->paramsData->setPostCmd($postProcessCmd);
		
		
		$this->paramsData->setBatchEnable(WSConfigClass::$enableBatch);

		return $this->paramsData;
	}
	
	/*
	 * @brief Unmarshall the time definition from the WS request
	*/
	protected function unmarshallTimeDefinition($input, $requestIndex)
	{
		$requestNodes = $this->paramsData->getRequestNodes();
		$timesNode = $requestNodes[$requestIndex]->getTimesNode();

		date_default_timezone_set('UTC');
		$timeStamp = strtotime($input->startDate);
		$start = CommonClass::timeStampToDDTime($timeStamp);
		$timeStamp = strtotime($input->stopDate) - strtotime($input->startDate);
		$duration = CommonClass::timeStampToDDTime($timeStamp);
		$timesNode->addInterval($start, $duration);			
	}
	
	/*
	* @brief Add a job to the job manager
	*/
	protected function addToJobsFile($data,$resultKey)
	{
		$waitingResult = $data->getWaitingResult($resultKey);
		
		return $this->jobsManager->addJob(
				$this->input,
				$data->getId(),
				$this->getWorkingDirName(),
				$data->getStatus() == ProcessStatusEnumClass::RUNNING,
				$data->getStart(),
				$waitingResult,
				$data->getErrorCode()); 
	}
	
	protected function getDatasetIdFromParamInfo($paramId)
	{
		$paramInfoFilePath = IHMConfigClass::getLocalParamDBPath().$paramId.".xml";
		$dom = new DomDocument("1.0");
		
		$dom->load($paramInfoFilePath);
		
		return $dom->getElementsByTagName("dataset_id")->item(0)->nodeValue;
		 
	
	}
	
	/*
	 * @brief method to marshall the result of a download request
	*/
	protected function marshallResult($data)
	{
		if (!$data->getSuccess())
			return array(
					'success' => false,
					'message' => $data->getLastErrorMessage());

		switch ($data->getStatus())
		{
			case ProcessStatusEnumClass::ERROR :
			case ProcessStatusEnumClass::RUNNING :
			case ProcessStatusEnumClass::DONE :
				return $this->addToJobsFile($data, WSInputOutputClass::getService());
			default :
				return array(
				'success' => false,
				'message'   => 'Unknown Process Status');
		}
	}
}
?>