Blame view

src/InputOutput/IHMImpl/Params/IHMInputOutputParamsAbstractClass.php 6.39 KB
22521f1c   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
<?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;
	protected $expressionManager = null;
	protected $jobsManager       = null;
bf27ba04   Benjamin Renard   Add templated par...
13
	
22521f1c   Benjamin Renard   First commit
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
	protected $paramsData        = null;
	protected $requestID         = "";
	protected $requestDirPrefix  = "";
	private   $input             = null;

	/*
	 * @brief Constructor
	*/
	function __construct()
	{
		$this->paramManager     = new IHMParamManagerClass();
		$this->expressionParser = new IHMExpressionParserClass();
		$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().'/';
	}

	/*
	 * @brief Unmarshall the time definition from the IHM request
	*/
00a22067   Benjamin Renard   TT navigation
56
	protected function unmarshallTimeDefinition($input, $requestIndex, $ttFileIndex = -1, $ttIntIndex = -1)
22521f1c   Benjamin Renard   First commit
57
	{
f7093283   Benjamin Renard   Fix pb with PHP v...
58
59
		$requestNodes = $this->paramsData->getRequestNodes();
		$timesNode = $requestNodes[$requestIndex]->getTimesNode();
22521f1c   Benjamin Renard   First commit
60
61
62
63

		switch ($input->timesrc)
		{
			case "TimeTable" :
00a22067   Benjamin Renard   TT navigation
64
65
66
67
				if (($ttFileIndex >= 0) && ($ttFileIndex >= count($input->timeTables)))
					throw new Exception('Try to use a non-existing Time table index in the request.');
				
				$crtIndex = 0;
22521f1c   Benjamin Renard   First commit
68
69
				foreach ($input->timeTables as $tt)
				{
00a22067   Benjamin Renard   TT navigation
70
71
72
73
74
75
76
					if (($ttFileIndex >= 0 ) && ($ttFileIndex != $crtIndex))
					{
						// Do not plot this TT
						++$crtIndex;
						continue;
					}
						
a242857a   Benjamin Renard   Shared objects
77
78
79
80
81
82
83
84
85
86
87
88
89
					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
90
						$ttPath = IHMConfigClass::getUserTTPath();
a242857a   Benjamin Renard   Shared objects
91
92
93
94
						$ttPath .= ($tt->id.".xml");
					}
					
					if ((strpos($tt->id, "cat_") === 0) || (strpos($tt->id, "sharedcatalog_") === 0))
00a22067   Benjamin Renard   TT navigation
95
						$timesNode->addCatalog($ttPath, $tt->{'timeTableName'}, $ttIntIndex);
c0e7e5be   Benjamin Renard   Add integration f...
96
					else
00a22067   Benjamin Renard   TT navigation
97
98
						$timesNode->addTimeTable($ttPath, $tt->{'timeTableName'}, $ttIntIndex);
					++$crtIndex;
22521f1c   Benjamin Renard   First commit
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
				}
				break;
			case "Interval" :
				date_default_timezone_set('UTC');
				$timeStamp = strtotime($input->startDate);
				$start = CommonClass::timeStampToDDTime($timeStamp);
				$timeStamp = strtotime($input->stopDate) - strtotime($input->startDate);
				$duration = CommonClass::timeStampToDDTime($timeStamp);
				$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)
	{
		return $this->jobsManager->addJob(
				$this->input,
				$data->getId(),
				$this->getWorkingDirName(),
				$data->getStatus() == ProcessStatusEnumClass::RUNNING,
				$data->getStart(),
				$data->getWaitingResult($resultKey),
				$data->getErrorCode());
	}
	
	/*
	 * @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
			$deleteData = new ProcessRequestDataClass();
			$deleteData->setManagerFilePath(IHMConfigClass::getProcessManagerFilePath());
			$deleteData->setType(ProcessTypeEnumClass::DELETE);
			$deleteData->setId($jobId);
				
			$processRequest = new ProcessRequestClass();
			$processRequest->setData($deleteData);

			if ($processRequest->init())
				$processRequest->run();
			
			//delete job and working dir
			$this->jobsManager->deleteJob($jobId);
		}
	}
	/*
	 * @brief Marshall request result for the IHM client
	*/
	protected function commonMarshallResult($data,$resultKey)
	{
		if (!$data->getSuccess())
			return array(
					'success' => false,
					'message' => $data->getLastErrorMessage());

		switch ($data->getStatus())
		{
			case ProcessStatusEnumClass::ERROR :
			case ProcessStatusEnumClass::RUNNING :
			case ProcessStatusEnumClass::DONE :
				return $this->addToJobsFile($data,$resultKey);
			default :
				return array(
				'success' => false,
				'message'   => 'Unknown Process Status');
		}
	}

	/*
	 * @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
		{
			$this->requestDirPrefix = "";
			$this->requestID = $requestId;
			
		}
		$this->paramsData->setWorkingPath($this->getWorkingPath());
		$this->paramsData->setCompilationPath(IHMConfigClass::getCompilationPath());
944199fe   Benjamin Renard   Use table definit...
202
		$this->paramsData->setLocalBasePath(IHMConfigClass::getLocalBasePath());
22521f1c   Benjamin Renard   First commit
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
		$this->paramsData->setManagerFilePath(IHMConfigClass::getProcessManagerFilePath());
		$this->input = $input;

		return $this->unmarshallRequest($input);
	}

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

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

3493f196   Benjamin Renard   Add request to ge...
228
?>