Blame view

src/Request/ParamsRequestImpl/ParamsRequestClass.php 8.03 KB
22521f1c   Benjamin Renard   First commit
1
<?php
22521f1c   Benjamin Renard   First commit
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * @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;
bc284189   Erdogan Furkan   Revert "Merge bra...
16
			
bda99a72   Benjamin Renard   Add kill plot req...
17
18
		if ($this->requestData->getType(ProcessTypeEnumClass::KILL))
			return true;
bc284189   Erdogan Furkan   Revert "Merge bra...
19
		
22521f1c   Benjamin Renard   First commit
20
		$this->requestData->setType(ProcessTypeEnumClass::RUN);
bc284189   Erdogan Furkan   Revert "Merge bra...
21
22
23
24
		
		switch ($this->requestData->getRequestType())
		{
			case ParamsRequestTypeEnumClass::XMLREQUEST :
8c57155b   Benjamin Renard   Integration for t...
25
				$cmd = "";
bc284189   Erdogan Furkan   Revert "Merge bra...
26
27
				foreach ($this->requestData->getRequestNodes() as $request_index => $requestNode)
				{
8c57155b   Benjamin Renard   Integration for t...
28
29
					if ($cmd != "")
						$cmd .= " && ";
bc284189   Erdogan Furkan   Revert "Merge bra...
30
					$cmd .= KernelConfigClass::getKernelBinPath()."amdaXMLRequestorTool ".$this->getRequestFilePath($request_index);
8c57155b   Benjamin Renard   Integration for t...
31
32
				}
				$this->requestData->setCmd($cmd);
3493f196   Benjamin Renard   Add request to ge...
33
				break;
bc284189   Erdogan Furkan   Revert "Merge bra...
34
35
			case ParamsRequestTypeEnumClass::PARAMGEN :
				$this->requestData->setCmd(KernelConfigClass::getKernelBinPath()."amdaParameterGenerator -p ".$this->requestData->getParamId());
3493f196   Benjamin Renard   Add request to ge...
36
				break;
bc284189   Erdogan Furkan   Revert "Merge bra...
37
38
39
			case ParamsRequestTypeEnumClass::PARAMSINFOGEN :
                                $this->requestData->setCmd(KernelConfigClass::getKernelBinPath()."amdaParameterInfo -p ".$this->requestData->getParamId());
                                break;
3493f196   Benjamin Renard   Add request to ge...
40
		}
22521f1c   Benjamin Renard   First commit
41
		$this->requestData->setEnvVars(KernelConfigClass::getExecEnvVarArray());
bc284189   Erdogan Furkan   Revert "Merge bra...
42
			
22521f1c   Benjamin Renard   First commit
43
44
		if (!parent::init())
			return false;
bc284189   Erdogan Furkan   Revert "Merge bra...
45
			
22521f1c   Benjamin Renard   First commit
46
47
48
		$this->requestData->setSuccess(false);
		$this->requestData->setLastErrorMessage('Cannot init params request');

bc284189   Erdogan Furkan   Revert "Merge bra...
49
50
		if ($this->requestData->getCompilationPath() == '')
		{
22521f1c   Benjamin Renard   First commit
51
52
53
54
			$this->requestData->setLastErrorMessage('Compilation path for params request not defined');
			return false;
		}

bc284189   Erdogan Furkan   Revert "Merge bra...
55
56
57
58
		if (!is_dir($this->requestData->getCompilationPath()))
		{
			if (!mkdir($this->requestData->getCompilationPath(),0777))
			{
22521f1c   Benjamin Renard   First commit
59
60
61
62
63
				$this->requestData->setLastErrorMessage('Cannot create compilation path for params request');
				return false;
			}
		}

bc284189   Erdogan Furkan   Revert "Merge bra...
64
65
66
67
		if ($this->requestData->getRequestType() == ParamsRequestTypeEnumClass::XMLREQUEST)
		{
			foreach ($this->requestData->getRequestNodes() as $request_index => $requestNode)
			{
b3343120   Benjamin Renard   Check parameters ...
68
				//inject time restriction
8a35f1e6   Benjamin Renard   Fix incompatibili...
69
				if (count($this->requestData->getParamsTimeRestrictions()) > 0) {
b3343120   Benjamin Renard   Check parameters ...
70
					//Here
bc284189   Erdogan Furkan   Revert "Merge bra...
71
72
					foreach ($this->requestData->getParamsTimeRestrictions() as $paramId => $timeRestriction)
					{
b3343120   Benjamin Renard   Check parameters ...
73
74
75
76
						$requestNode->getParamsTimeRestrictions()->addParamTimeRestriction($paramId, strtotime($timeRestriction));
					}
				}

8c57155b   Benjamin Renard   Integration for t...
77
78
79
80
81
				//create XML request file
				$doc = new DOMDocument("1.0", "UTF-8");
				$doc->preserveWhiteSpace = false;
				$doc->formatOutput = true;

bc284189   Erdogan Furkan   Revert "Merge bra...
82
83
				if (!$xmlNode = $requestNode->toXMLNode($doc))
				{
7d84634e   Benjamin Renard   Fix plot request ...
84
					$this->requestData->setLastErrorMessage('Cannot create params request XML file for request');
8c57155b   Benjamin Renard   Integration for t...
85
86
					return false;
				}
bc284189   Erdogan Furkan   Revert "Merge bra...
87
			
8c57155b   Benjamin Renard   Integration for t...
88
89
				$doc->appendChild($xmlNode);

bc284189   Erdogan Furkan   Revert "Merge bra...
90
91
				if (!$doc->save($this->getRequestFilePath($request_index)))
				{
7d84634e   Benjamin Renard   Fix plot request ...
92
					$this->requestData->setLastErrorMessage('Cannot save params request XML file for request');
8c57155b   Benjamin Renard   Integration for t...
93
94
					return false;
				}
bc284189   Erdogan Furkan   Revert "Merge bra...
95
96
				
				//libxml_use_internal_errors(true);
8e9913e3   Menouard AZIB   PlotFunction uses...
97
				if (!$doc->schemaValidate(KernelConfigClass::getXSDRequestFilePath())) {
bc284189   Erdogan Furkan   Revert "Merge bra...
98
99
100
101
102
					//$error_msg = "";
					//$errors = libxml_get_errors();
					//foreach ($errors as $error) {
					//	if ($error_msg != "")
					//		$error_msg .= PHP_EOL;
c1f6c108   Benjamin Renard   Remove debug log
103
					//	$error_msg .= 'XML error "' . $error->message . '" [' . $error->level . '] (Code ' . $error->code . ') in ' . $error->file . ' on line ' . $error->line . ' column ' . $error->column;
bc284189   Erdogan Furkan   Revert "Merge bra...
104
105
					//}
					//libxml_clear_errors();
c1f6c108   Benjamin Renard   Remove debug log
106
107

					$this->requestData->setLastErrorMessage('Params request XML file not valid for request' /*. ' (' . $error_msg . ')'*/);
8c57155b   Benjamin Renard   Integration for t...
108
109
					return false;
				}
6ee28339   Benjamin Renard   Remove debug info
110
				//libxml_use_internal_errors(false);
3493f196   Benjamin Renard   Add request to ge...
111
			}
22521f1c   Benjamin Renard   First commit
112
113
114
		}

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

		//copy parameters files
bc284189   Erdogan Furkan   Revert "Merge bra...
118
119
120
121
122
		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
123
124
				return false;
			}
bc284189   Erdogan Furkan   Revert "Merge bra...
125
126
127
128
129
				
			$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
130
131
				return false;
			}
59eb1b9c   Elena.Budnik   new ama stat
132
			// get datasetID for statistics
bc284189   Erdogan Furkan   Revert "Merge bra...
133
			$doc = new DOMDocument("1.0", "UTF-8"); 
59eb1b9c   Elena.Budnik   new ama stat
134
135
136
			if (@$doc->load($value)) {
				$dsID = $doc->getElementsByTagName('dataset_id');
				if ($dsID->length > 0) {
81df5ad9   Elena.Budnik   wsd param
137
138
139
140
141
142
					if ($dsID->item(0)->nodeValue)
						$dsName = $dsID->item(0)->nodeValue;
					elseif (strpos($key, 'spase_') !== false)
						$dsName = 'impex';
					else
						$dsName = 'unknown';
bc284189   Erdogan Furkan   Revert "Merge bra...
143
						
81df5ad9   Elena.Budnik   wsd param
144
					$this->requestData->addDatasetForStat($dsName);
59eb1b9c   Elena.Budnik   new ama stat
145
146
				}
			}
bc284189   Erdogan Furkan   Revert "Merge bra...
147
			
286f7924   Benjamin Renard   Derived parameter...
148
			touch($destinationFile, filemtime($value));
22521f1c   Benjamin Renard   First commit
149
150
151
		}

		//create processed params files
bc284189   Erdogan Furkan   Revert "Merge bra...
152
153
		foreach ($this->requestData->getProcessParamsToCreate() as $key => $value)
		{
22521f1c   Benjamin Renard   First commit
154
155
156
157
			$doc = new DOMDocument("1.0", "UTF-8");
			$doc->preserveWhiteSpace = false;
			$doc->formatOutput = true;

bc284189   Erdogan Furkan   Revert "Merge bra...
158
159
160
			if (!$xmlNode = $value["param"]->toXMLNode($doc))
			{
				$this->requestData->setLastErrorMessage('Cannot create params XML file for '.$key);
22521f1c   Benjamin Renard   First commit
161
162
				return false;
			}
bc284189   Erdogan Furkan   Revert "Merge bra...
163
				
22521f1c   Benjamin Renard   First commit
164
165
166
167
168
169
170
171
			$doc->appendChild($xmlNode);

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

bc284189   Erdogan Furkan   Revert "Merge bra...
172
173
174
175
			$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
176
177
				return false;
			}
bc284189   Erdogan Furkan   Revert "Merge bra...
178
			
286f7924   Benjamin Renard   Derived parameter...
179
			touch($destinationFile, $value["dateModif"]);
22521f1c   Benjamin Renard   First commit
180
		}
bc284189   Erdogan Furkan   Revert "Merge bra...
181
		
944199fe   Benjamin Renard   Use table definit...
182
		//create local params files
bc284189   Erdogan Furkan   Revert "Merge bra...
183
184
		foreach ($this->requestData->getLocalParamsToCreate() as $key => $value)
		{
944199fe   Benjamin Renard   Use table definit...
185
186
187
188
			$doc = new DOMDocument("1.0", "UTF-8");
			$doc->preserveWhiteSpace = false;
			$doc->formatOutput = true;

81df5ad9   Elena.Budnik   wsd param
189

bc284189   Erdogan Furkan   Revert "Merge bra...
190
191
192
			if (!$xmlNode = $value["param"]->toXMLNode($doc))
			{
				$this->requestData->setLastErrorMessage('Cannot create params XML file for '.$key);
944199fe   Benjamin Renard   Use table definit...
193
194
				return false;
			}
bc284189   Erdogan Furkan   Revert "Merge bra...
195
				
944199fe   Benjamin Renard   Use table definit...
196
197
			$doc->appendChild($xmlNode);

bc284189   Erdogan Furkan   Revert "Merge bra...
198
199
200
201
			$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...
202
203
				return false;
			}
bc284189   Erdogan Furkan   Revert "Merge bra...
204
			
81df5ad9   Elena.Budnik   wsd param
205
			$this->requestData->addDatasetForStat('uploadedData');
bc284189   Erdogan Furkan   Revert "Merge bra...
206
			
944199fe   Benjamin Renard   Use table definit...
207
208
			touch($destinationFile, $value["dateModif"]);
		}
ffc5cb81   Elena.Budnik   temporary commit
209
		//create and copy local params files
bc284189   Erdogan Furkan   Revert "Merge bra...
210
211
		foreach ($this->requestData->getLocalParamsToCreateAndCopy() as $key => $value)
		{
ffc5cb81   Elena.Budnik   temporary commit
212
213
214
			$doc = new DOMDocument("1.0", "UTF-8");
			$doc->preserveWhiteSpace = false;
			$doc->formatOutput = true;
22521f1c   Benjamin Renard   First commit
215

bc284189   Erdogan Furkan   Revert "Merge bra...
216
217
218
			if (!$xmlNode = $value["param"]->toXMLNode($doc))
			{
				$this->requestData->setLastErrorMessage('Cannot create params XML file for '.$key);
ffc5cb81   Elena.Budnik   temporary commit
219
220
				return false;
			}
bc284189   Erdogan Furkan   Revert "Merge bra...
221
				
ffc5cb81   Elena.Budnik   temporary commit
222
			$doc->appendChild($xmlNode);
bc284189   Erdogan Furkan   Revert "Merge bra...
223
224
225
226
227
228
229
			
			$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
230
231
				return false;
			}
bc284189   Erdogan Furkan   Revert "Merge bra...
232
			
ffc5cb81   Elena.Budnik   temporary commit
233
234
235
			touch($originFile, $value["dateModif"]);
			touch($destinationFile, $value["dateModif"]);
		}
22521f1c   Benjamin Renard   First commit
236
237
		return true;
	}
22521f1c   Benjamin Renard   First commit
238
239
240
241
242
	/*
	 * @brief Run a params request
	*/
	public function run()
	{
a5413d3a   Benjamin Renard   Add user name in ...
243
		$amdaStat = new AmdaStats($this->requestData->getUser());
bc284189   Erdogan Furkan   Revert "Merge bra...
244
		
81df5ad9   Elena.Budnik   wsd param
245
246
		if (count($this->requestData->getDatasetForStat()) > 0)
			$amdaStat->addTask(IHMConfigClass::getUserName(), $this->requestData->getTask(), $this->requestData->getDatasetForStat());
bc284189   Erdogan Furkan   Revert "Merge bra...
247
		
22521f1c   Benjamin Renard   First commit
248
249
250
251
252
253
		return parent::run();
	}

	/*
	 * @brief Get the request file path for AMDA_Kernel
	*/
7d14181a   Benjamin Renard   Fix multi-request...
254
	private function getRequestFilePath($request_index)
22521f1c   Benjamin Renard   First commit
255
	{
bc284189   Erdogan Furkan   Revert "Merge bra...
256
		return $this->requestData->getWorkingPath()."request_".$request_index.".xml";
22521f1c   Benjamin Renard   First commit
257
258
	}
}
bc284189   Erdogan Furkan   Revert "Merge bra...
259
?>