Blame view

src/Request/ParamsRequestImpl/ParamsRequestClass.php 7.87 KB
22521f1c   Benjamin Renard   First commit
1
<?php
8e9913e3   Menouard AZIB   PlotFunction uses...
2

22521f1c   Benjamin Renard   First commit
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
 * @class ParamsRequestClass
 * @brief Treats a param request. This class inherits from ProcessRequestClass
 * @details
 */
class ParamsRequestClass extends ProcessRequestClass
{
	/*
	 * @brief Init a params request
	*/
	public function init()
	{
		if (!isset($this->requestData))
			return false;
8e9913e3   Menouard AZIB   PlotFunction uses...
17

bda99a72   Benjamin Renard   Add kill plot req...
18
19
		if ($this->requestData->getType(ProcessTypeEnumClass::KILL))
			return true;
8e9913e3   Menouard AZIB   PlotFunction uses...
20

22521f1c   Benjamin Renard   First commit
21
		$this->requestData->setType(ProcessTypeEnumClass::RUN);
8e9913e3   Menouard AZIB   PlotFunction uses...
22
23
24

		switch ($this->requestData->getRequestType()) {
			case ParamsRequestTypeEnumClass::XMLREQUEST:
8c57155b   Benjamin Renard   Integration for t...
25
				$cmd = "";
8e9913e3   Menouard AZIB   PlotFunction uses...
26
				foreach ($this->requestData->getRequestNodes() as $request_index => $requestNode) {
8c57155b   Benjamin Renard   Integration for t...
27
28
					if ($cmd != "")
						$cmd .= " && ";
8e9913e3   Menouard AZIB   PlotFunction uses...
29
					$cmd .= KernelConfigClass::getKernelBinPath() . "amdaXMLRequestorTool " . $this->getRequestFilePath($request_index);
8c57155b   Benjamin Renard   Integration for t...
30
31
				}
				$this->requestData->setCmd($cmd);
3493f196   Benjamin Renard   Add request to ge...
32
				break;
8e9913e3   Menouard AZIB   PlotFunction uses...
33
34
35
36
37
			case ParamsRequestTypeEnumClass::PARAMGEN:
				$this->requestData->setCmd(KernelConfigClass::getKernelBinPath() . "amdaParameterGenerator -p " . $this->requestData->getParamId());
				break;
			case ParamsRequestTypeEnumClass::PARAMSINFOGEN:
				$this->requestData->setCmd(KernelConfigClass::getKernelBinPath() . "amdaParameterInfo -p " . $this->requestData->getParamId());
3493f196   Benjamin Renard   Add request to ge...
38
				break;
3493f196   Benjamin Renard   Add request to ge...
39
		}
22521f1c   Benjamin Renard   First commit
40
		$this->requestData->setEnvVars(KernelConfigClass::getExecEnvVarArray());
8e9913e3   Menouard AZIB   PlotFunction uses...
41

22521f1c   Benjamin Renard   First commit
42
43
		if (!parent::init())
			return false;
8e9913e3   Menouard AZIB   PlotFunction uses...
44

22521f1c   Benjamin Renard   First commit
45
46
47
		$this->requestData->setSuccess(false);
		$this->requestData->setLastErrorMessage('Cannot init params request');

8e9913e3   Menouard AZIB   PlotFunction uses...
48
		if ($this->requestData->getCompilationPath() == '') {
22521f1c   Benjamin Renard   First commit
49
50
51
52
			$this->requestData->setLastErrorMessage('Compilation path for params request not defined');
			return false;
		}

8e9913e3   Menouard AZIB   PlotFunction uses...
53
54
		if (!is_dir($this->requestData->getCompilationPath())) {
			if (!mkdir($this->requestData->getCompilationPath(), 0777)) {
22521f1c   Benjamin Renard   First commit
55
56
57
58
59
				$this->requestData->setLastErrorMessage('Cannot create compilation path for params request');
				return false;
			}
		}

8e9913e3   Menouard AZIB   PlotFunction uses...
60
61
		if ($this->requestData->getRequestType() == ParamsRequestTypeEnumClass::XMLREQUEST) {
			foreach ($this->requestData->getRequestNodes() as $request_index => $requestNode) {
b3343120   Benjamin Renard   Check parameters ...
62
				//inject time restriction
8a35f1e6   Benjamin Renard   Fix incompatibili...
63
				if (count($this->requestData->getParamsTimeRestrictions()) > 0) {
b3343120   Benjamin Renard   Check parameters ...
64
					//Here
8e9913e3   Menouard AZIB   PlotFunction uses...
65
					foreach ($this->requestData->getParamsTimeRestrictions() as $paramId => $timeRestriction) {
b3343120   Benjamin Renard   Check parameters ...
66
67
68
69
						$requestNode->getParamsTimeRestrictions()->addParamTimeRestriction($paramId, strtotime($timeRestriction));
					}
				}

8c57155b   Benjamin Renard   Integration for t...
70
71
72
73
74
				//create XML request file
				$doc = new DOMDocument("1.0", "UTF-8");
				$doc->preserveWhiteSpace = false;
				$doc->formatOutput = true;

8e9913e3   Menouard AZIB   PlotFunction uses...
75
				if (!$xmlNode = $requestNode->toXMLNode($doc)) {
7d84634e   Benjamin Renard   Fix plot request ...
76
					$this->requestData->setLastErrorMessage('Cannot create params request XML file for request');
8c57155b   Benjamin Renard   Integration for t...
77
78
					return false;
				}
8e9913e3   Menouard AZIB   PlotFunction uses...
79

8c57155b   Benjamin Renard   Integration for t...
80
81
				$doc->appendChild($xmlNode);

8e9913e3   Menouard AZIB   PlotFunction uses...
82
				if (!$doc->save($this->getRequestFilePath($request_index))) {
7d84634e   Benjamin Renard   Fix plot request ...
83
					$this->requestData->setLastErrorMessage('Cannot save params request XML file for request');
8c57155b   Benjamin Renard   Integration for t...
84
85
					return false;
				}
8e9913e3   Menouard AZIB   PlotFunction uses...
86
87
88
89
90
91
92
93
94
95
96
97
98

				libxml_use_internal_errors(true);
				if (!$doc->schemaValidate(KernelConfigClass::getXSDRequestFilePath())) {
					$error_msg = "";
					$errors = libxml_get_errors();
					foreach ($errors as $error) {
						if ($error_msg != "")
							$error_msg .= PHP_EOL;
						$error_msg .= 'XML error "' . $error->message . '" [' . $error->level . '] (Code ' . $error->code . ') in ' . $error->file . ' on line ' . $error->line . ' column ' . $error->column;
					}
					libxml_clear_errors();

					$this->requestData->setLastErrorMessage('Params request XML file not valid for request' . ' (' . $error_msg . ')');
8c57155b   Benjamin Renard   Integration for t...
99
100
					return false;
				}
6ee28339   Benjamin Renard   Remove debug info
101
				//libxml_use_internal_errors(false);
3493f196   Benjamin Renard   Add request to ge...
102
			}
22521f1c   Benjamin Renard   First commit
103
104
105
		}

		//create config files
944199fe   Benjamin Renard   Use table definit...
106
		KernelConfigClass::write($this->requestData->getWorkingPath(), $this->requestData->getCompilationPath(), $this->requestData->getLocalBasePath());
22521f1c   Benjamin Renard   First commit
107
108

		//copy parameters files
8e9913e3   Menouard AZIB   PlotFunction uses...
109
110
111
		foreach ($this->requestData->getParamsToCopy() as $key => $value) {
			if (!file_exists($value)) {
				$this->requestData->setLastErrorMessage('Cannot find param definition file for ' . $key);
22521f1c   Benjamin Renard   First commit
112
113
				return false;
			}
8e9913e3   Menouard AZIB   PlotFunction uses...
114
115
116
117

			$destinationFile = KernelConfigClass::getRequestParamsPath($this->requestData->getWorkingPath()) . $key . ".xml";
			if (!copy($value, $destinationFile)) {
				$this->requestData->setLastErrorMessage('Cannot copy param definition file for ' . $key);
22521f1c   Benjamin Renard   First commit
118
119
				return false;
			}
59eb1b9c   Elena.Budnik   new ama stat
120
			// get datasetID for statistics
8e9913e3   Menouard AZIB   PlotFunction uses...
121
			$doc = new DOMDocument("1.0", "UTF-8");
59eb1b9c   Elena.Budnik   new ama stat
122
123
124
			if (@$doc->load($value)) {
				$dsID = $doc->getElementsByTagName('dataset_id');
				if ($dsID->length > 0) {
81df5ad9   Elena.Budnik   wsd param
125
126
127
128
129
130
					if ($dsID->item(0)->nodeValue)
						$dsName = $dsID->item(0)->nodeValue;
					elseif (strpos($key, 'spase_') !== false)
						$dsName = 'impex';
					else
						$dsName = 'unknown';
8e9913e3   Menouard AZIB   PlotFunction uses...
131

81df5ad9   Elena.Budnik   wsd param
132
					$this->requestData->addDatasetForStat($dsName);
59eb1b9c   Elena.Budnik   new ama stat
133
134
				}
			}
8e9913e3   Menouard AZIB   PlotFunction uses...
135

286f7924   Benjamin Renard   Derived parameter...
136
			touch($destinationFile, filemtime($value));
22521f1c   Benjamin Renard   First commit
137
138
139
		}

		//create processed params files
8e9913e3   Menouard AZIB   PlotFunction uses...
140
		foreach ($this->requestData->getProcessParamsToCreate() as $key => $value) {
22521f1c   Benjamin Renard   First commit
141
142
143
144
			$doc = new DOMDocument("1.0", "UTF-8");
			$doc->preserveWhiteSpace = false;
			$doc->formatOutput = true;

8e9913e3   Menouard AZIB   PlotFunction uses...
145
146
			if (!$xmlNode = $value["param"]->toXMLNode($doc)) {
				$this->requestData->setLastErrorMessage('Cannot create params XML file for ' . $key);
22521f1c   Benjamin Renard   First commit
147
148
				return false;
			}
8e9913e3   Menouard AZIB   PlotFunction uses...
149

22521f1c   Benjamin Renard   First commit
150
151
152
153
154
155
156
157
			$doc->appendChild($xmlNode);

			/*if (!$doc->schemaValidate(KernelConfigClass::getXSDParameterFilePath()))
			 {
			$this->requestData->setLastErrorMessage('Params XML file not valid for '.$key);
			return false;
			}*/

8e9913e3   Menouard AZIB   PlotFunction uses...
158
159
160
			$destinationFile = KernelConfigClass::getRequestParamsPath($this->requestData->getWorkingPath()) . $key . ".xml";
			if (!$doc->save($destinationFile)) {
				$this->requestData->setLastErrorMessage('Cannot save params XML file for ' . $key);
22521f1c   Benjamin Renard   First commit
161
162
				return false;
			}
8e9913e3   Menouard AZIB   PlotFunction uses...
163

286f7924   Benjamin Renard   Derived parameter...
164
			touch($destinationFile, $value["dateModif"]);
22521f1c   Benjamin Renard   First commit
165
		}
8e9913e3   Menouard AZIB   PlotFunction uses...
166

944199fe   Benjamin Renard   Use table definit...
167
		//create local params files
8e9913e3   Menouard AZIB   PlotFunction uses...
168
		foreach ($this->requestData->getLocalParamsToCreate() as $key => $value) {
944199fe   Benjamin Renard   Use table definit...
169
170
171
172
			$doc = new DOMDocument("1.0", "UTF-8");
			$doc->preserveWhiteSpace = false;
			$doc->formatOutput = true;

81df5ad9   Elena.Budnik   wsd param
173

8e9913e3   Menouard AZIB   PlotFunction uses...
174
175
			if (!$xmlNode = $value["param"]->toXMLNode($doc)) {
				$this->requestData->setLastErrorMessage('Cannot create params XML file for ' . $key);
944199fe   Benjamin Renard   Use table definit...
176
177
				return false;
			}
8e9913e3   Menouard AZIB   PlotFunction uses...
178

944199fe   Benjamin Renard   Use table definit...
179
180
			$doc->appendChild($xmlNode);

8e9913e3   Menouard AZIB   PlotFunction uses...
181
182
183
			$destinationFile = KernelConfigClass::getRequestParamsPath($this->requestData->getWorkingPath()) . $key . ".xml";
			if (!$doc->save($destinationFile)) {
				$this->requestData->setLastErrorMessage('Cannot save params XML file for ' . $key);
944199fe   Benjamin Renard   Use table definit...
184
185
				return false;
			}
8e9913e3   Menouard AZIB   PlotFunction uses...
186

81df5ad9   Elena.Budnik   wsd param
187
			$this->requestData->addDatasetForStat('uploadedData');
8e9913e3   Menouard AZIB   PlotFunction uses...
188

944199fe   Benjamin Renard   Use table definit...
189
190
			touch($destinationFile, $value["dateModif"]);
		}
ffc5cb81   Elena.Budnik   temporary commit
191
		//create and copy local params files
8e9913e3   Menouard AZIB   PlotFunction uses...
192
		foreach ($this->requestData->getLocalParamsToCreateAndCopy() as $key => $value) {
ffc5cb81   Elena.Budnik   temporary commit
193
194
195
			$doc = new DOMDocument("1.0", "UTF-8");
			$doc->preserveWhiteSpace = false;
			$doc->formatOutput = true;
22521f1c   Benjamin Renard   First commit
196

8e9913e3   Menouard AZIB   PlotFunction uses...
197
198
			if (!$xmlNode = $value["param"]->toXMLNode($doc)) {
				$this->requestData->setLastErrorMessage('Cannot create params XML file for ' . $key);
ffc5cb81   Elena.Budnik   temporary commit
199
200
				return false;
			}
8e9913e3   Menouard AZIB   PlotFunction uses...
201

ffc5cb81   Elena.Budnik   temporary commit
202
			$doc->appendChild($xmlNode);
8e9913e3   Menouard AZIB   PlotFunction uses...
203
204
205
206
207
208

			$originFile =  IHMConfigClass::getUserWSPath() . $key . ".xml";
			$destinationFile = KernelConfigClass::getRequestParamsPath($this->requestData->getWorkingPath()) . $key . ".xml";

			if (!$doc->save($destinationFile) || !$doc->save($originFile)) {
				$this->requestData->setLastErrorMessage('Cannot save params XML file for ' . $key);
ffc5cb81   Elena.Budnik   temporary commit
209
210
				return false;
			}
8e9913e3   Menouard AZIB   PlotFunction uses...
211

ffc5cb81   Elena.Budnik   temporary commit
212
213
214
			touch($originFile, $value["dateModif"]);
			touch($destinationFile, $value["dateModif"]);
		}
22521f1c   Benjamin Renard   First commit
215
216
		return true;
	}
22521f1c   Benjamin Renard   First commit
217
218
219
220
221
	/*
	 * @brief Run a params request
	*/
	public function run()
	{
a5413d3a   Benjamin Renard   Add user name in ...
222
		$amdaStat = new AmdaStats($this->requestData->getUser());
8e9913e3   Menouard AZIB   PlotFunction uses...
223

81df5ad9   Elena.Budnik   wsd param
224
225
		if (count($this->requestData->getDatasetForStat()) > 0)
			$amdaStat->addTask(IHMConfigClass::getUserName(), $this->requestData->getTask(), $this->requestData->getDatasetForStat());
8e9913e3   Menouard AZIB   PlotFunction uses...
226

22521f1c   Benjamin Renard   First commit
227
228
229
230
231
232
		return parent::run();
	}

	/*
	 * @brief Get the request file path for AMDA_Kernel
	*/
7d14181a   Benjamin Renard   Fix multi-request...
233
	private function getRequestFilePath($request_index)
22521f1c   Benjamin Renard   First commit
234
	{
8e9913e3   Menouard AZIB   PlotFunction uses...
235
		return $this->requestData->getWorkingPath() . "request_" . $request_index . ".xml";
22521f1c   Benjamin Renard   First commit
236
237
	}
}