IHMInputOutputTTClass.php 4.72 KB
<?php

/**
 * @class IHMInputOutputTTClass
 * @brief 
 * @details
*/
class IHMInputOutputTTClass implements InputOutputInterface
{
 
/*
	* @brief Constructor
*/
	function __construct()
	{
		 
	}

/*
	* @brief translate input data from IHM client to AMDA_Integration module for a tt process request
*/
	public function getInputData($input, $function, $requestId="")
	{
		if (isset($this->processDatas))
			unset($this->processDatas);

		$sendToSamp = isset($input->sendToSamp) && $input->sendToSamp;
		$compression = !$sendToSamp && ($input->compression !== 'none');
		$format = $sendToSamp ? 'vot' : $input->fileformat;
			
		$final = "";
		
		if (is_dir(IHMConfigClass::getDownloadTmpPath())) {
			foreach (glob(IHMConfigClass::getDownloadTmpPath().'*.zip') as $filename)  unlink($filename);
			foreach (glob(IHMConfigClass::getDownloadTmpPath().'*.tar.gz') as $filename)  unlink($filename);
		}
		
		foreach($input->list as $tt)
		{
			//create a list of data to treat
			$processData = new TTRequestDataClass();
			
			if (strpos($tt->id,"sharedtimeTable_") === 0 || strpos($tt->id,"sharedcatalog_") === 0) {
				//Shared TimeTable /Catalog
				$objectType = strpos($tt->id,"timeTable") != 0 ? "timeTable" : "catalog";
				$result = IHMSharedObjectsClass::getPath($objectType, $tt->id);
				if (!$result['success'])
					throw new Exception('Shared '.$objectType.' '.$tt->name.' not found');
				$ttName = $result['path'];
			}
			else {
				//User TimeTable / Catalog
				$ttName = IHMConfigClass::getUserTTPath().$tt->id.'.xml';
			}
			if (!file_exists($ttName))
				throw new Exception('TimeTable '.$tt->name.' not found');
			$processData->setInputFileName($ttName);
			$processData->setInputFileFormat(TTRequestFileFormatEnum::INTERNAL);			 
			$processData->setOutputDir(IHMConfigClass::getDownloadTmpPath());
			
			switch ($format) {
				case 'vot' :	
					$processData->setOutputFileFormat(TTRequestFileFormatEnum::VOTABLE);
					$outputName = $tt->name.'.xml';
					break;
				default    :
					$processData->setOutputFileFormat(TTRequestFileFormatEnum::ASCII);
					$outputName = $tt->name;
			}	
			
		//	$processData->setOutputTimeFormat($input->timeformat);
			$processData->setOutputFileName($outputName);
			$final .= " ".$outputName;
			$processData->setType($function);
			$processData->setCmd("ttConversion");
			$processData->setSendToSamp($sendToSamp);
									
			$this->processDatas[] = $processData;
		}
		
		$this->postProcessTT = new PostProcessTTClass();
		$this->postProcessTT->setOutputDirAlias($this->processDatas[0]->getOutputDir());
		
		if ($compression) {
			$this->postProcessTT->compression = true;
			$archive = "Timetable_archive_".date('Ymd_Hi');
			switch ($input->compression) {
				case 'zip' :
					$postCmd = "zip -Dj ".$archive.".zip ".$final." 1> /dev/null 2> /dev/null";
					$outputFileName = $archive.".zip";					
					break;
				case 'tar+gzip':				       
				default :
					$postCmd="tar cfz ".$archive.".tar.gz --directory=".$this->processDatas[0]->getOutputDir()." ".$final;
					$outputFileName = $archive.".tar.gz";
					break;				
			}				
			$this->postProcessTT->setPostCmd($postCmd);
			$this->postProcessTT->setOutputFileName($outputFileName);
			$this->postProcessTT->setOutputDir($this->processDatas[0]->getOutputDir());			
		}
		
		return $this->processDatas;
	}

/*
	* @brief translate output data from AMDA_Integration module to IHM client  request
*/
	public function getOutput($data)
	{
		if ( $this->postProcessTT->compression ) {
		
		      $timeStart = time();
		      
			$cmd = $this->postProcessTT->getPostCmd();			
			system($cmd);
			
		 	while (!file_exists($this->postProcessTT->getOutputFileName()) && ( time() - $timeStart ) < KernelConfigClass::getTimeToBatchMode())
						sleep(1);	 
				 		 	                     
			
			foreach ($data as $d)
			{
				$rawFileName = $d->getOutputDir().$d->getOutputFileName();				
				if (file_exists($rawFileName)) 							 
					unlink($rawFileName);
			}
			
			$result  = array('success'=>true,
				'download'=>$this->postProcessTT->getOutputDirAlias().$this->postProcessTT->getOutputFileName(), 
				'compression' => $this->postProcessTT->compression);
				
			
		}
		else if ($data[0]->getSendToSamp()) {
			$donwload = array();
			foreach ($data as $d) {
				$donwload[] = $this->postProcessTT->getOutputDirAlias().$d->getOutputFileName();
			}
			$result  = array('success'=>$data[0]->getSuccess(), 'sendToSamp' => TRUE,
				'download' => $donwload);
		}
		else {
			$result  = array('success'=>$data[0]->getSuccess(), 
				'download' => $this->postProcessTT->getOutputDirAlias().$data[0]->getOutputFileName());
		}
				 		
		return $result;
	}
} 

?>