IHMInputOutputParamsPlotClass.php
9.5 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
153
154
155
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
define ("PLOT_RESULT_FILE_KEY","plot");
/**
* @class IHMInputOutputParamsPlotClass
* @brief Implementation of IHMInputOutputParamsAbstractClass to treat plot request
* @details
*/
class IHMInputOutputParamsPlotClass extends IHMInputOutputParamsAbstractClass
{
private $isInteractiveRequest = false;
/*
* @brief method to unmarshall a plot request
*/
protected function unmarshallRequest($input)
{
//save request
$this->saveIHMRequest($input);
//Request
$requestIndex = 0;
$this->isInteractiveRequest = ($input->{'file-output'} == 'INTERACTIVE');
$postProcessCmd = "";
foreach ($input->tabs as $tab)
{
$requestNode = $this->paramsData->addRequestNode();
$outputsNode = $requestNode->getOutputsNode();
//unmarshall time definition
$this->unmarshallTimeDefinition($input, $requestIndex);
$plotOutputNode = $outputsNode->addNewOutput(RequestOutputTypeEnum::PLOT);
$plotOutputNode->setWriteContextFile($this->isInteractiveRequest ? "true" : "false");
$compression = "";
if (!$this->isInteractiveRequest)
{
switch ($input->{'file-output'})
{
case 'TGZ' :
$plotOutputNode->addPostProcessing(RequestOutputPostProcessingEnumClass::TAR);
$plotOutputNode->addPostProcessing(RequestOutputPostProcessingEnumClass::GZIP);
$compression = ".tar.gz";
break;
case 'ZIP' :
$plotOutputNode->addPostProcessing(RequestOutputPostProcessingEnumClass::ZIP);
$compression = ".zip";
break;
default:
throw new Exception('Compression not implemented.');
}
}
if ($input->{'one-file-per-interval'})
$plotOutputNode->setStructure(RequestOutputPlotStructureEnum::ONE_FILE_PER_INTERVAL);
else
$plotOutputNode->setStructure(RequestOutputPlotStructureEnum::ONE_FILE);
//prefix
$filePrefix = "plot";
if ($input->{'file-prefix'} && ($input->{'file-prefix'} != ""))
$filePrefix = $input->{'file-prefix'};
$filePrefix .= $requestIndex;
$plotOutputNode->setFilePrefix($filePrefix);
//page
$pageNode = $plotOutputNode->getPage();
$fileFormat = RequestOutputPlotPageFormatEnum::PNG;
$extension = ".png";
switch ($input->{'file-format'})
{
case 'PNG' :
$fileFormat = RequestOutputPlotPageFormatEnum::PNG;
$extension = ".png";
break;
case 'PDF' :
$fileFormat = RequestOutputPlotPageFormatEnum::PDF;
$extension = ".pdf";
break;
case 'PS' :
$fileFormat = RequestOutputPlotPageFormatEnum::PS;
$extension = ".ps";
break;
case 'SVG' :
$fileFormat = RequestOutputPlotPageFormatEnum::SVG;
$extension = ".svg";
break;
default:
throw new Exception('File format not implemented.');
}
$pageNode->setFormat($fileFormat);
$this->unmarshallTitle($tab, 'page-title', $pageNode->getTitle());
$isPortrait = false;
switch ($tab->{'page-orientation'})
{
case 'landscape' :
$pageNode->setOrientation(RequestOutputPlotPageOrientationEnum::LANDSCAPE);
break;
case 'portrait' :
$pageNode->setOrientation(RequestOutputPlotPageOrientationEnum::PORTRAIT);
$isPortrait = true;
break;
}
switch ($tab->{'page-dimension'})
{
case 'ISO A4' :
$pageNode->setDimension(RequestOutputPlotPageDimensionEnum::ISO_A4);
break;
case 'US letter' :
$pageNode->setDimension(RequestOutputPlotPageDimensionEnum::US_LETTER);
break;
default:
throw new Exception('Page dimension not implemented.');
}
switch ($tab->{'page-mode'})
{
case 'color' :
$pageNode->setMode(RequestOutputPlotPageModeEnum::COLOR);
break;
case 'grayscale' :
$pageNode->setMode(RequestOutputPlotPageModeEnum::GRAYSCALE);
break;
default:
throw new Exception('Page mode not implemented.');
}
if ($tab->{'page-margins-activated'})
{
$pageNode->getMargins()->setHorizontal($tab->{'page-margin-x'});
$pageNode->getMargins()->setVertical($tab->{'page-margin-y'});
}
if ($tab->{'page-font-activated'})
$this->unmarshallFont($tab, 'page', $pageNode->getFont());
$pageNode->getLayout()->setType(RequestOutputPlotLayoutTypeEnum::VERTICAL);
if ($this->isInteractiveRequest)
{
$resultFile = $filePrefix."_*".$extension;
$waitingResultFile = $filePrefix.$extension;
}
else
{
$resultFile = $filePrefix."_*".$compression;
$waitingResultFile = $filePrefix.$compression;
}
$this->paramsData->addWaitingResult(PLOT_RESULT_FILE_KEY."_".$requestIndex, $waitingResultFile);
//Remove old result files
foreach (glob($this->paramsData->getWorkingPath().$resultFile) as $oldFile) {
unlink($oldFile);
}
//Post process command to apply to the result file
if ($postProcessCmd != "")
$postProcessCmd .= " | ";
$postProcessCmd .= "mv ".$resultFile." ".$waitingResultFile;
if ($this->isInteractiveRequest && $isPortrait)
$postProcessCmd .= " | convert ".$waitingResultFile." -rotate -90 ".$waitingResultFile;
++$requestIndex;
}
$this->paramsData->setBatchEnable(!(($fileFormat == RequestOutputPlotPageFormatEnum::PNG) && $this->isInteractiveRequest));
$this->paramsData->setPostCmd($postProcessCmd);
//determine extension and add post processing if needed
return $this->paramsData;
}
protected function unmarshallTitle($inputData, $keyPrefix, $titleNode)
{
if ($inputData->{$keyPrefix.'-text'} != '')
{
$titleNode->setText($inputData->{$keyPrefix.'-text'});
switch ($inputData->{$keyPrefix.'-position'})
{
case 'top' :
$titleNode->setPosition(RequestOutputPlotTitlePosition::TOP);
break;
case 'bottom' :
$titleNode->setPosition(RequestOutputPlotTitlePosition::BOTTOM);
break;
}
switch ($inputData->{$keyPrefix.'-alignment'})
{
case 'center' :
$titleNode->setAlign(RequestOutputPlotTitleAlign::CENTER);
break;
case 'left' :
$titleNode->setAlign(RequestOutputPlotTitleAlign::LEFT);
break;
case 'right' :
$titleNode->setAlign(RequestOutputPlotTitleAlign::RIGHT);
break;
}
if ($inputData->{$keyPrefix.'-color'} != '')
$titleNode->setColor($this->hexColor2KernelColor($inputData->{$keyPrefix.'-color'}));
$this->unmarshallLabel($inputData, $keyPrefix, $titleNode);
}
}
protected function unmarshallLabel($inputData, $keyPrefix, $labelNode)
{
if ($inputData->{$keyPrefix.'-font-activated'})
{
$labelNode->setFontName($inputData->{$keyPrefix.'-font-name'});
$labelNode->setFontSize($inputData->{$keyPrefix.'-font-size'});
$style = "";
if ($inputData->{$keyPrefix.'-font-bold'})
{
if ($style != "")
$style .= ",";
$style .= "bold";
}
if ($inputData->{$keyPrefix.'-font-italic'})
{
if ($style != "")
$style .= ",";
$style .= "italic";
}
if ($style != "")
$labelNode->setFontStyle($style);
}
}
protected function unmarshallFont($inputData, $keyPrefix, $fontNode)
{
if ($inputData->{$keyPrefix.'-font-activated'})
{
$fontNode->setFontName($inputData->{$keyPrefix.'-font-name'});
$fontNode->setSize($inputData->{$keyPrefix.'-font-size'});
if ($inputData->{$keyPrefix.'-font-italic'})
$fontNode->setStyle(RequestOutputPlotFontStyle::ITALIC);
else
$fontNode->setStyle(RequestOutputPlotFontStyle::UPRIGHT);
if ($inputData->{$keyPrefix.'-font-bold'})
$fontNode->setWeight(RequestOutputPlotFontWeight::BOLD);
else
$fontNode->setWeight(RequestOutputPlotFontWeight::MEDIUM);
}
}
/*
* @brief method to marshall the result of a download request
*/
protected function marshallResult($data)
{
if (!$this->isInteractiveRequest)
{
//add to job
$commonRes = $this->commonMarshallResult($data,PLOT_RESULT_FILE_KEY."_0");
return $commonRes;
}
if (!$data->getSuccess())
return array(
'success' => false,
'message' => $data->getLastErrorMessage());
switch ($data->getStatus())
{
case ProcessStatusEnumClass::DONE :
$result = array();
foreach ($data->getWaitingResults() as $key => $waitingResult)
{
$contextResult = str_replace(".png","_context.xml",$waitingResult);
$result[] = array(
"id" => $key,
"context" => IHMPlotContextFileClass::parse($this->getWorkingPath().$contextResult),
"plot" => $waitingResult
);
}
return array(
'success' => true,
'id' => $data->getId(),
'folder' => $this->getWorkingDirName(),
'result' => $result
);
default :
return array(
'success' => false,
'message' => 'Error to process the interractive request');
}
}
private function hexColor2KernelColor($hex) {
$hex = str_replace("#", "", $hex);
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
return "[".$r.",".$g.",".$b."]";
}
private function saveIHMRequest($input)
{
$path = $this->getWorkingPath()."ihm.request";
if (!is_dir($this->getWorkingPath()))
mkdir($this->getWorkingPath(),0777);
$file = fopen($path, 'w');
fwrite($file, json_encode($input));
fclose($file);
}
private function loadIHMRequest()
{
$path = $this->getWorkingPath()."ihm.request";
if (!file_exists($path))
return NULL;
return json_decode(file_get_contents($path));
}
}
?>