Blame view

src/InputOutput/IHMImpl/Params/IHMInputOutputParamsAbstractClass.php 8.75 KB
22521f1c   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
<?php

/**
 * @class IHMInputOutputParamsAbstractClass
 * @brief Abstract class that's implement an InputOutputInterface used to treat input/output of a "param" (ie. download, data mining, plot, ...) request
 * @details
 */
abstract class IHMInputOutputParamsAbstractClass implements InputOutputInterface
{
	protected $paramManager      = null;
22521f1c   Benjamin Renard   First commit
11
	protected $jobsManager       = null;
bf27ba04   Benjamin Renard   Add templated par...
12
	
22521f1c   Benjamin Renard   First commit
13
14
15
	protected $paramsData        = null;
	protected $requestID         = "";
	protected $requestDirPrefix  = "";
d5a86709   Elena.Budnik   interim commit
16
17
	protected $input             = null;
	protected $sendToSamp        = FALSE;
22521f1c   Benjamin Renard   First commit
18
19
20
21
22
23
24

	/*
	 * @brief Constructor
	*/
	function __construct()
	{
		$this->paramManager     = new IHMParamManagerClass();
22521f1c   Benjamin Renard   First commit
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
		$this->jobsManager      = new IHMJobsManagerClass();
	}

	/*
	 * @brief Generate a unique ID for the current request
	*/
	protected function generateNewRequestId()
	{
		$this->requestID = CommonClass::generateRandomString(6);
	}

	/*
	 * @brief Get working dir name
	*/
	protected function getWorkingDirName()
	{
		return $this->requestDirPrefix.$this->requestID."_";
	}

	/*
	 * @brief Get full working dir path
	*/
	protected function getWorkingPath()
	{
		return IHMConfigClass::getRequestPath().$this->getWorkingDirName().'/';
	}

	/*
131e60ff   Benjamin Renard   Implement real co...
53
54
55
56
	 * @brief Get Task
	 */
	protected function getTask($input)
	{
53fc468c   Benjamin Renard   Fix for #7842
57
58
59
		if (isset($input->nodeType))
			return $input->nodeType;
		return NULL;
131e60ff   Benjamin Renard   Implement real co...
60
61
62
	}

	/*
22521f1c   Benjamin Renard   First commit
63
64
	 * @brief Unmarshall the time definition from the IHM request
	*/
00a22067   Benjamin Renard   TT navigation
65
	protected function unmarshallTimeDefinition($input, $requestIndex, $ttFileIndex = -1, $ttIntIndex = -1)
22521f1c   Benjamin Renard   First commit
66
	{
f7093283   Benjamin Renard   Fix pb with PHP v...
67
68
		$requestNodes = $this->paramsData->getRequestNodes();
		$timesNode = $requestNodes[$requestIndex]->getTimesNode();
22521f1c   Benjamin Renard   First commit
69
70
71
72

		switch ($input->timesrc)
		{
			case "TimeTable" :
00a22067   Benjamin Renard   TT navigation
73
				if (($ttFileIndex >= 0) && ($ttFileIndex >= count($input->timeTables)))
79d4f2a1   Hacene SI HADJ MOHAND   ok 7522
74
75
76
				        $ttFileIndex = 0;
                                if($ttFileIndex < 0)
                                        $ttFileIndex =  count($input->timeTables) -1; 
00a22067   Benjamin Renard   TT navigation
77
				$crtIndex = 0;
22521f1c   Benjamin Renard   First commit
78
79
				foreach ($input->timeTables as $tt)
				{
00a22067   Benjamin Renard   TT navigation
80
81
82
83
84
85
86
					if (($ttFileIndex >= 0 ) && ($ttFileIndex != $crtIndex))
					{
						// Do not plot this TT
						++$crtIndex;
						continue;
					}
						
a242857a   Benjamin Renard   Shared objects
87
88
89
90
91
92
93
94
95
96
97
98
99
					if (strpos($tt->id, "sharedtimeTable_") === 0) {
						$result = IHMSharedObjectsClass::getPath("timeTable", $tt->id);
						if (!$result['success'])
							throw new Exception($result['message']);
						$ttPath = $result['path'];
					}
					else if (strpos($tt->id, "sharedcatalog_") === 0) {
						$result = IHMSharedObjectsClass::getPath("catalog", $tt->id);
						if (!$result['success'])
							throw new Exception($result['message']);
						$ttPath = $result['path'];
					}
					else {
22521f1c   Benjamin Renard   First commit
100
						$ttPath = IHMConfigClass::getUserTTPath();
a242857a   Benjamin Renard   Shared objects
101
102
103
104
						$ttPath .= ($tt->id.".xml");
					}
					
					if ((strpos($tt->id, "cat_") === 0) || (strpos($tt->id, "sharedcatalog_") === 0))
00a22067   Benjamin Renard   TT navigation
105
						$timesNode->addCatalog($ttPath, $tt->{'timeTableName'}, $ttIntIndex);
c0e7e5be   Benjamin Renard   Add integration f...
106
					else
00a22067   Benjamin Renard   TT navigation
107
108
						$timesNode->addTimeTable($ttPath, $tt->{'timeTableName'}, $ttIntIndex);
					++$crtIndex;
22521f1c   Benjamin Renard   First commit
109
110
111
112
				}
				break;
			case "Interval" :
				date_default_timezone_set('UTC');
74fd500a   Hacene SI HADJ MOHAND   us #9874 ms zoo...
113
114
				$start = CommonClass::strTimeToDDTime($input->startDate);
				$duration = CommonClass::getDurationDDTime($input->stopDate, $input->startDate);
22521f1c   Benjamin Renard   First commit
115
116
117
118
119
120
121
122
123
124
125
126
				$timesNode->addInterval($start, $duration);
				break;
			default :
				throw new Exception('Time source type not implemented.');
		}
	}

	/*
	 * @brief Add a job to the job manager
	*/
	protected function addToJobsFile($data,$resultKey)
	{
d5a86709   Elena.Budnik   interim commit
127
128
129
130
131
132
133
		$waitingResult = $data->getWaitingResult($resultKey);
		if ($this->sendToSamp && ($data->getStatus() == ProcessStatusEnumClass::DONE)) {
			$waitingResult = explode("\n", file_get_contents(IHMConfigClass::getRequestPath()."/".$this->getWorkingDirName()."/".$waitingResult));
			$waitingResult = implode(',', $waitingResult);
			$waitingResult = rtrim($waitingResult, ',');
		}

22521f1c   Benjamin Renard   First commit
134
135
136
137
138
139
		return $this->jobsManager->addJob(
				$this->input,
				$data->getId(),
				$this->getWorkingDirName(),
				$data->getStatus() == ProcessStatusEnumClass::RUNNING,
				$data->getStart(),
5cf99396   Benjamin Renard   Add SAMP export i...
140
				$waitingResult,
ba82a624   Benjamin Renard   Get kernel execut...
141
142
				$data->getErrorCode(),
				$data->getExecTime());
22521f1c   Benjamin Renard   First commit
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
	}
	
	/*
	 * @brief Delete current working dir + delete jobs that use this working dir + delete process
	 */
	protected function deleteWorkingDir()
	{
		$res = $this->jobsManager->getJobsByWorkingDir($this->getWorkingDirName());
		
		if (!$res['success'])
			return;
		
		foreach ($res['jobs'] as $jobId)
		{
			//delete process
d5a86709   Elena.Budnik   interim commit
158
159
160
161
			$deleteData = new ProcessRequestDataClass();
			$deleteData->setManagerFilePath(IHMConfigClass::getProcessManagerFilePath());
			$deleteData->setType(ProcessTypeEnumClass::DELETE);
			$deleteData->setId($jobId);
a5413d3a   Benjamin Renard   Add user name in ...
162
			$deleteData->setUser(IHMConfigClass::getUserName());
d5a86709   Elena.Budnik   interim commit
163
164
165
166
167
				
			$processRequest = new ProcessRequestClass();
			$processRequest->setData($deleteData);

			if ($processRequest->init())
22521f1c   Benjamin Renard   First commit
168
169
				$processRequest->run();
			
d5a86709   Elena.Budnik   interim commit
170
			//delete job and working dir
22521f1c   Benjamin Renard   First commit
171
172
173
174
175
176
			$this->jobsManager->deleteJob($jobId);
		}
	}
	/*
	 * @brief Marshall request result for the IHM client
	*/
d5a86709   Elena.Budnik   interim commit
177
178
179
180
181
182
183
184
185
	protected function commonMarshallResult($data,$resultKey)
	{
		if (!$data->getSuccess())
			return array(
					'success' => false,
					'message' => $data->getLastErrorMessage());

		switch ($data->getStatus())
		{
22521f1c   Benjamin Renard   First commit
186
			case ProcessStatusEnumClass::ERROR :
d5a86709   Elena.Budnik   interim commit
187
			case ProcessStatusEnumClass::RUNNING :
22521f1c   Benjamin Renard   First commit
188
			case ProcessStatusEnumClass::DONE :
d5a86709   Elena.Budnik   interim commit
189
190
191
192
193
194
				return $this->addToJobsFile($data,$resultKey);
			default :
				return array(
				'success' => false,
				'message'   => 'Unknown Process Status');
		}
22521f1c   Benjamin Renard   First commit
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
	}

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

		$this->paramsData = new ParamsRequestDataClass();
		if ($requestId == "")
		{
			$this->requestDirPrefix = "DD";
			$this->generateNewRequestId();
		}
		else
		{
d5a86709   Elena.Budnik   interim commit
213
			$this->requestDirPrefix = "";
22521f1c   Benjamin Renard   First commit
214
			$this->requestID = $requestId;
22521f1c   Benjamin Renard   First commit
215
216
217
		}
		$this->paramsData->setWorkingPath($this->getWorkingPath());
		$this->paramsData->setCompilationPath(IHMConfigClass::getCompilationPath());
944199fe   Benjamin Renard   Use table definit...
218
		$this->paramsData->setLocalBasePath(IHMConfigClass::getLocalBasePath());
22521f1c   Benjamin Renard   First commit
219
		$this->paramsData->setManagerFilePath(IHMConfigClass::getProcessManagerFilePath());
131e60ff   Benjamin Renard   Implement real co...
220
		$this->paramsData->setTask($this->getTask($input));
a5413d3a   Benjamin Renard   Add user name in ...
221
		$this->paramsData->setUser(IHMConfigClass::getUserName());
22521f1c   Benjamin Renard   First commit
222
223
		$this->input = $input;

b3343120   Benjamin Renard   Check parameters ...
224
225
226
227
228
		$this->unmarshallRequest($input);

		$this->checkLocalParamsRestriction();
		
		return $this->paramsData;
22521f1c   Benjamin Renard   First commit
229
230
231
232
233
234
235
236
237
238
239
	}

	/*
	 * @brief translate output data from AMDA_Integration module to IHM client for a "param" request
	*/
	public function getOutput($data)
	{
		return $this->marshallResult($data);
	}

	/*
b3343120   Benjamin Renard   Check parameters ...
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
	 * @brief check restriction access to local parameters
	 */
	private function checkLocalParamsRestriction()
	{
		$userLocalTreePath = IHMConfigClass::getUserLocalTreePath();
		if (!file_exists($userLocalTreePath)) {
			throw new Exception('Cannot retrieve user local tree.');
		}

		$xml = new DOMDocument();
		$xml->preserveWhiteSpace = TRUE;
		if (!@$xml->load($userLocalTreePath)) {
			throw new Exception('Cannot load user local tree.');
		}

		$xp = new DOMXpath($xml);

		foreach ($this->paramsData->getParamsToCopy() as $key => $value) {
4ea6a045   Benjamin Renard   Fix incompatibili...
258
259
260
261
262
			$paramId = $key;
			if ($info = $this->paramManager->getTemplateParamsManager()->parseTemplatedParam($key)) {
				$paramId = $info['paramid'];
			}
			$paramNodes = $xp->query("//parameter[@xml:id='".$paramId."']");
b3343120   Benjamin Renard   Check parameters ...
263
264
265
266
267
268
269
270
271
			if ($paramNodes->length == 0) {
				continue;
			}
			$paramNode = $paramNodes->item(0);
			$datasetNode = $paramNode->parentNode;
			$inGroup = FALSE;
			if (isset($datasetNode)) {
				$crtNode = $datasetNode;
				do {
b600ef36   Benjamin Renard   Fix incompatibili...
272
					if ($crtNode->hasAttribute("group") && ($crtNode->getAttribute("group") != "")) {
b3343120   Benjamin Renard   Check parameters ...
273
274
275
276
277
278
						$inGroup = TRUE;
					}
					$crtNode = $crtNode->parentNode;
				} while(isset($crtNode) && ($crtNode != $xml->documentElement) && !$inGroup);
			}
			if ($inGroup) {
b600ef36   Benjamin Renard   Fix incompatibili...
279
				if ($datasetNode->hasAttribute("timeRestriction") && ($datasetNode->getAttribute("timeRestriction") != "")) {
b3343120   Benjamin Renard   Check parameters ...
280
281
282
283
284
285
286
287
288
289
290
					$timeRestriction = $datasetNode->getAttribute("timeRestriction");
					$this->paramsData->addParamTimeRestriction($key, $timeRestriction);
				}
				else {
					throw new Exception('Not allowed. Access to parameter '.$key.' is restricted');
				}
			}
		}
	}

	/*
22521f1c   Benjamin Renard   First commit
291
292
293
294
295
296
297
298
299
300
	 * @brief Abstract method to unmarshall a "param" request
	*/
	abstract protected function unmarshallRequest($input);

	/*
	 * @brief Abstract method to marshall the result of a "param" request
	*/
	abstract protected function marshallResult($data);
}

d5a86709   Elena.Budnik   interim commit
301
?>