IHMInputOutputParamsDownloadClass.php 4.06 KB
<?php

define ("DOWNLOAD_RESULT_FILE_KEY","download");

/**
 * @class IHMInputOutputParamsDownloadClass
 * @brief Implementation of IHMInputOutputParamsAbstractClass to treat download request
 * @details
*/
class IHMInputOutputParamsDownloadClass extends IHMInputOutputParamsAbstractClass
{
	/*
	 * @brief method to unmarshall a download request
	*/
	protected function unmarshallRequest($input)
	{
		//{"nodeType":"download","type":"Download","downloadSrc":"0","structure":"2","sampling":600,"output":"",
		// "header":"0","timesrc":"TimeTable","timeTables":[{"timeTableName":"rzerzer","id":"tt_1"},{"timeTableName":"sqsdq","id":"tt_0"}],
		//"list":["dst"],"milli":false,"fileformat":"ASCII","timeformat":"YYYY-MM-DDThh:mm:ss","compression":"tar+gzip","leaf":true}

		$paramsNode = $this->paramsData->getRequestNode()->getParamsNode();

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

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

		//timeformatData    : [['Y-m-dTH:i:s', 'YYYY-MM-DDThh:mm:ss'], ['Y m d H i s', 'YYYY MM DD hh mm ss'], ['d m Y H i s', 'DD MM YYYY hh mm ss'], ['Y z H i s', 'YYYY DDD hh mm ss']],
		switch ($input->timeformat)
		{
			case 'YYYY-MM-DDThh:mm:ss' :
				$downloadNode->setTimeFormat(RequestOutputDownloadTimeFormatEnum::ISO);
				break;
			default :
				throw new Exception('Time format not implemented.');
		}

		//formatData    : [['ASCII', 'ASCII'],['vot', 'VOTable']],
		switch ($input->fileformat)
		{
			case "ASCII" :
				$downloadNode->setFileFormat(RequestOutputDownloadFileFormatEnum::ASCII);
				break;
				case "vot" :
				 $downloadNode->setFileFormat(RequestOutputDownloadFileFormatEnum::VOTABLE);
				break;
			default :
				throw new Exception('File format not implemented.');
		}

		if ($input->output != "")
			$downloadNode->setFileName($input->output);

		//add params to output
		foreach ($input->list as $param)
		{
			$paramInfo = $this->paramManager->addExistingParam($param,$this->paramsData);
			$downloadNode->addParam($paramInfo['id'],$paramInfo['indexes'],$paramInfo['calib_infos']);
			$paramsNode->addParam($paramInfo['id']);
		}

		//filestructureData : [['0','All In One File'], ['1','One File Per Time Interval'], ['2','One File Per Param/Interval']],
		switch ($input->structure)
		{
			case "0" :
				$downloadNode->setSamplingTime($input->sampling);
				$downloadNode->setStructure(RequestOutputDownloadStructureEnum::ONE_FILE);
				break;
			case "1" :
				$downloadNode->setSamplingTime($input->sampling);
				$downloadNode->setStructure(RequestOutputDownloadStructureEnum::ONE_FILE_PER_INTERVAL);
				break;
			case "2" :
				$downloadNode->setStructure(RequestOutputDownloadStructureEnum::ONE_FILE_PER_PARAMETER_PER_INTERVAL);
				break;
			default :
				throw new Exception('Structure type not implemented.');
		}

		//filecompressData  : [['zip', 'zip'], ['tar+gzip', 'tar+gzip']],
		$extension = "";
		switch ($input->compression)
		{
			case "zip" :
				$extension = ".zip";
				$downloadNode->addPostProcessing(RequestOutputPostProcessingEnumClass::ZIP);
				break;
			case "tar+gzip" :
				$extension = ".tar.gz";
				$downloadNode->addPostProcessing(RequestOutputPostProcessingEnumClass::TAR);
				$downloadNode->addPostProcessing(RequestOutputPostProcessingEnumClass::GZIP);
				break;
			default :
				throw new Exception('Compression type not implemented.');
		}

		$resultFile = "result_".$this->requestID;
		$this->paramsData->addWaitingResult(DOWNLOAD_RESULT_FILE_KEY, $resultFile);

		$postProcessCmd  = "mv download_*";
		$postProcessCmd .= $extension;
		$postProcessCmd .= " ".$resultFile.$extension;

		$this->paramsData->setPostCmd($postProcessCmd);

		return $this->paramsData;
	}

	/*
	 * @brief method to marshall the result of a download request
	*/
	protected function marshallResult($data)
	{
		return $this->commonMarshallResult($data,DOWNLOAD_RESULT_FILE_KEY);
	}
}
?>