WSInputOutputParamsDownloadClass.php 6.02 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 Get Task
         */
	protected function getTask($input)
	{
		return WSInputOutputClass::getService();
	}

	/*
	 * @brief method to unmarshall a download request
	*/
	protected function unmarshallRequest($input)
	{
		//$this->jobsManager->cleanupResultDir();

		$requestNode = $this->paramsData->addRequestNode(0);
		
		$paramsNode = $requestNode->getParamsNode();

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

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

		// hash is used to be sure to have a unique file name for two differents requests
		$hash = "";

		switch (strtoupper($input->timeformat))
		{
			case 'ISO08601' :
				$downloadNode->setTimeFormat(RequestOutputDownloadTimeFormatEnum::ISO);
				break;
			case 'UNIXTIME' :
				$downloadNode->setTimeFormat(RequestOutputDownloadTimeFormatEnum::MS);
				break;
			default :
				$downloadNode->setTimeFormat(RequestOutputDownloadTimeFormatEnum::ISO);
		}
		$hash .= $input->timeformat;

		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();
		$resultFile .= "_".$input->file_info;
		$isFirst = true;
		$firstParam = "";

		
		foreach ($input->list as $param)
		{
			$id = $param->paramid;
			$parsedParam = $this->paramManager->getTemplateParamsManager()->parseTemplatedParam($id);
			$tempArgs = array();
			if ($parsedParam !== FALSE) {
				$id = $parsedParam['paramid'];
				foreach ($parsedParam['template_args'] as $key => $val) {
					$tempArgs[$key] = $val;
				}
			}
			$paramInfo = $this->paramManager->addExistingParam($id, $this->paramsData, $tempArgs);

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

			if ($isFirst) {
				$firstParam = $paramInfo['id'];
				$isFirst = FALSE;
			}
		}

		if (isset($input->sampling))
		{
			$downloadNode->setSamplingTime($input->sampling);
			$downloadNode->setStructure(RequestOutputDownloadStructureEnum::ONE_FILE);
			$hash .= $input->sampling;
		}
		else if (isset($input->ref_sampling_param))
		{
			$downloadNode->setStructure(RequestOutputDownloadStructureEnum::ONE_FILE_REFPARAM);
			$hash .= $input->ref_sampling_param;
		}
		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 .= '_' . md5($hash) . "_".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.$firstParam."*[0-9]";
		
		$postProcessCmd .= $extension;
		$postProcessCmd .= " ".WSConfigClass::getWsResultDir().$resultFile.$extension;
	
		$this->paramsData->setPostCmd($postProcessCmd);

		$this->paramsData->setBatchEnable(WSConfigClass::$enableBatch);

		$this->paramsData->setFromWS(true);

		return $this->paramsData;
	}
	
	/*
	 * @brief Unmarshall the time definition from the WS request
	*/
	protected function unmarshallTimeDefinition($input, $requestIndex, $ttFileIndex = -1, $ttIntIndex = -1)
	{
		$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);

		$result = $this->jobsManager->addJob(
				$this->input,
				$data->getId(),
				$this->getWorkingDirName(),
				$data->getStatus() == ProcessStatusEnumClass::RUNNING,
				$data->getStart(),
				$waitingResult,
				$data->getErrorCode(),
				$data->getExecTime());

		return $result;
	}
	
	/*
	 * @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');
		}
	}
}
?>