Blame view

src/InputOutput/IHMImpl/TimeTables/IHMInputOutputTTClass.php 7.04 KB
1ff81371   Vincent Cephirins   RM7926 - Evol - C...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?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');
		$formatstr = $sendToSamp ? 'vot' : $input->fileformat;
		$tmp = new TTRequestDataClass(); // just to be sure that TTRequestDataClass is loaded with the 'autoload' => TTRequestDataClass is defined
		$outputformat = $this->getFormatFromString(!empty($formatstr) ? $formatstr : 'vot');
			
		$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 (!isset($tt->id) && !empty($tt->path))  {
				$ttName = $tt->path;
				$inputFileFormat = $this->getFormatFromString(!empty($tt->format) ? $tt->format : TTRequestFileFormatEnum::INTERNAL);
			}
			else 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'];
				$inputFileFormat = TTRequestFileFormatEnum::INTERNAL;
			}
			else {
				//User TimeTable / Catalog
				$ttName = IHMConfigClass::getUserTTPath().$tt->id.'.xml';
				$inputFileFormat = TTRequestFileFormatEnum::INTERNAL;
			}
			if (!file_exists($ttName))
				throw new Exception('TimeTable or Catalog '.$tt->name.' not found');
			$processData->setInputFileName($ttName);
			$processData->setInputFileFormat($inputFileFormat);			 
			$processData->setOutputDir(IHMConfigClass::getDownloadTmpPath());
			$processData->setOutputFileFormat($outputformat);
			
			switch ($outputformat) {
				case TTRequestFileFormatEnum::VOTABLE :	
					$outputName = $tt->name.'.xml';
					break;
				case TTRequestFileFormatEnum::ASCII :
20263d07   Hacene SI HADJ MOHAND   new tt format ok
75
                                case TTRequestFileFormatEnum:: SPACE :
1ff81371   Vincent Cephirins   RM7926 - Evol - C...
76
77
					$outputName = $tt->name.'.txt';
					break;
1ff81371   Vincent Cephirins   RM7926 - Evol - C...
78
79
80
81
82
83
84
				default    :
					$outputName = $tt->name.'.xml';
			}	
			
               		//timeformatData    : [['Y-m-dTH:i:s', 'YYYY-MM-DDThh:mm:ss'], ['DD Time', 'DD Time'], ['Timestamp', 'Timestamp']],
	                switch ($input->timeformat)
        	        {
bcd0e909   Benjamin Renard   Fix
85
86
			case 'YYYY-MM-DDThh:mm:ss' :
			case 'YYYY-MM-DDThh:mm:ss.u' :
1ff81371   Vincent Cephirins   RM7926 - Evol - C...
87
88
        	                        $processData->setOutputTimeFormat(TTRequestTimeFormatEnum::ISO);
                	                break;
815676b7   Hacene SI HADJ MOHAND   I have changed DD...
89
 	                       case 'DOY TIME' :
196268c9   Benjamin Renard   DDTIME -> DOYTIME
90
	                                $processData->setOutputTimeFormat(TTRequestTimeFormatEnum::DOYTIME);
1ff81371   Vincent Cephirins   RM7926 - Evol - C...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
        	                        break;
	                        case 'Timestamp' :
        	                        $processData->setOutputTimeFormat(TTRequestTimeFormatEnum::TIMESTAMP);
                	                break;
                        	case 'Timestamp-with-milliseconds' :
 	                               $processData->setOutputTimeFormat(TTRequestTimeFormatEnum::MS);
        	                        break;
                	        case 'YYYY MM DD hh mm ss' :
                        	        $processData->setOutputTimeFormat(TTRequestTimeFormatEnum::SPACES);
                                	break;

 	                       default :
        	                        throw new Exception('Time format not implemented.');
                	}

			$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 Get format from input string
	 */
	private function getFormatFromString($str) {
		switch (strtolower($str)) {
			case 'vot':
			case 'votable':
				return TTRequestFileFormatEnum::VOTABLE;
			case 'ascii':
			case 'text':
			case 'txt':
			case 'asc':
				return TTRequestFileFormatEnum::ASCII;
643f4026   Hacene SI HADJ MOHAND   adding space format
153
154
155
                                                            case 'space':
                                                            case 'SPACE':
                                                                                return TTRequestFileFormatEnum::SPACE;
1ff81371   Vincent Cephirins   RM7926 - Evol - C...
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
		}
		return TTRequestFileFormatEnum::INTERNAL;
	}

/*
	* @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;
	}
} 

?>