Newer
Older
<?php
namespace SmartDataContext\Controller;
use SmartDataContext\Persistence\MinIOClient;
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
use SmartDataContext\Persistence\SmartDataContextCrud;
use SmartDataContext\Persistence\DBManager;
use SmartDataContext\Domain\SmartDataContext;
use Slim\Psr7\Request;
use Slim\Psr7\Response;
class Crud {
private function _basicValidation($params, $request, $response) {
# Basic validation
$body = $request->getParsedBody();
$errors = [];
if (! $body) {
$errors[] = "missing body";
}
$domain = $this->_get_value_or_default($params, 'domain', null);
if (! $domain) {
$errors[] = "missing domain";
}
return [$domain, $body, $errors];
}
function create(Request $request, Response $response, array $params) {
# Basic validation
[$domain, $body, $errors] = $this->_basicValidation($params, $request, $response);
if ($errors) {
return $this->return_error($response, $errors, 400);
}
# Parameters
$content = $this->_get_value_or_default($body, 'content', []);
$features = $this->_get_value_or_default($body, 'features', []);
$t0 = $this->_get_value_or_default($body, 't0', -1);
$t1 = $this->_get_value_or_default($body, 't1', -1);
$smartDataSources = $this->_get_value_or_default($body, 'smartDataSources', []);
$smartDataUnits = $this->_get_value_or_default($body, 'smartDataUnits', []);
# Validate parameters
$errors = array_merge(
SmartDataContext::ValidateContent($content),
SmartDataContext::ValidateFeatures($features),
SmartDataContext::ValidateTimestamp($t0,$t1),
SmartDataContext::ValidateSmartDataSources($smartDataSources),
SmartDataContext::ValidateSmartDataUnits($smartDataUnits));
if ($errors) {
return $this->return_error($response, $errors, 400);
}
# Create new SmartDataContext
$persistence = new SmartDataContextCrud($this->_get_db());
$result = $persistence->create(domain: $domain, t0: $t0, t1: $t1, content: $content, features: $features, smartDataSources: $smartDataSources,
smartDataUnits: $smartDataUnits);
if ($result['success']) {
return $this->return_json($response, $result["contents"]);
} else {
return $this->return_error($response, $result["errors"], 400);
}
}
function associate(Request $request, Response $response, array $params) {
# Basic validation
[$domain, $body, $errors] = $this->_basicValidation($params, $request, $response);
if ($errors) {
return $this->return_error($response, $errors, 400);
}
$smartdatacontextids = $this->_get_value_or_default($body, 'smartDataContextIds', null);
if (! isset($smartdatacontextids)) {
return $this->return_error($response, ['Missing smartDataContextIds'], 400);
}
if (! is_array($smartdatacontextids)) {
$smartdatacontextids = [$smartdatacontextids];
}
$smartDataSources = $this->_get_value_or_default($body, 'smartDataSources', []);
$smartDataUnits = $this->_get_value_or_default($body, 'smartDataUnits', []);
$errors = array_merge(
SmartDataContext::ValidateSmartDataSources($smartDataSources),
SmartDataContext::ValidateSmartDataUnits($smartDataUnits));
if ($errors) {
return $this->return_error($response, $errors, 400);
}
$persistence = new SmartDataContextCrud($this->_get_db());
$fullResult = [];
foreach ($smartdatacontextids as $id) {
$fullResult[] = $persistence->associate($domain, $id, $smartDataSources, $smartDataUnits);
}
return $this->return_json($response, $fullResult);
}
function unassociate(Request $request, Response $response, array $params) {
# Basic validation
[$domain, $body, $errors] = $this->_basicValidation($params, $request, $response);
if ($errors) {
return $this->return_error($response, $errors, 400);
}
$smartdatacontextids = $this->_get_value_or_default($body, 'smartDataContextIds', null);
if (! isset($smartdatacontextids)) {
return $this->return_error($response, ['Missing smartDataContextIds'], 400);
}
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
if (! is_array($smartdatacontextids)) {
$smartdatacontextids = [$smartdatacontextids];
}
$smartDataSources = $this->_get_value_or_default($body, 'smartDataSources', []);
$smartDataUnits = $this->_get_value_or_default($body, 'smartDataUnits', []);
$errors = array_merge(
SmartDataContext::ValidateSmartDataSources($smartDataSources),
SmartDataContext::ValidateSmartDataUnits($smartDataUnits));
if ($errors) {
return $this->return_error($response, $errors, 400);
}
$persistence = new SmartDataContextCrud($this->_get_db());
$fullResult = [];
foreach ($smartdatacontextids as $id) {
$fullResult[] = $persistence->unassociate($domain, $id, $smartDataSources, $smartDataUnits);
}
return $this->return_json($response, $fullResult);
}
function get(Request $request, Response $response, array $params) {
$domain = $this->_get_value_or_default($params, 'domain', null);
$id = $this->_get_value_or_default($params, 'id', null);
if (isset($domain) && isset($id)) {
$persistence = new SmartDataContextCrud($this->_get_db());
$result = $persistence->get(domain: $domain, id: $id);
if ($result['success']) {
return $this->return_json($response, $result["contents"]);
} else {
return $this->return_error($response, $result["errors"], 400);
}
} elseif (! $id) {
return $this->return_error($response, ["missing SmartDataContextID"], 400);
}
elseif (! $domain) {
return $this->return_error($response, ["missing domain"], 400);
}
}
function contexts(Request $request, Response $response, array $params) {
# Basic validation
[$domain, $body, $errors] = $this->_basicValidation($params, $request, $response);
if ($errors) {
return $this->return_error($response, $errors, 400);
}
$smartDataSources = $this->_get_value_or_default($body, 'smartDataSources', []);
$smartDataUnits = $this->_get_value_or_default($body, 'smartDataUnits', []);
if (count($smartDataSources) + count($smartDataUnits) == 0) {
return $this->return_error($response, ["at least one smartDataSource or smartDataUnit must be provided"], 400);
}
$t0 = $this->_get_value_or_default($body, 't0', -1);
$t1 = $this->_get_value_or_default($body, 't1', -1);
$sourceFilter = [];
foreach ($smartDataUnits as $smartDataUnit) {
$sourceFilter[] = ["smartDataUnits" => $smartDataUnit];
}
foreach ($smartDataSources as $smartDataSource) {
$sourceFilter[] = ["smartDataSources" => $smartDataSource];
}
$sourceFilter = ['$or' => $sourceFilter];
$timeFilter = [];
if (isset($t0) && isset($t1)) {
$timeFilter = ['$and' => [['t0' => ['$lte' => $t0]], ['$or' => [['t1' => ['$gte' => $t1]], ["t1" => -1]]]]];
}
if ($timeFilter) {
$filter = ['$and' => [$sourceFilter, $timeFilter]];
} else {
$filter = $sourceFilter;
}
$persistence = new SmartDataContextCrud($this->_get_db());
$result = $persistence->query(domain: $domain, query: $filter);
if ($result['success']) {
return $this->return_json($response, $result["contents"]);
} else {
return $this->return_error($response, $result["errors"], 400);
}
}
function query(Request $request, Response $response, array $params) {
# Basic validation
[$domain, $query, $errors] = $this->_basicValidation($params, $request, $response);
if ($errors) {
return $this->return_error($response, $errors, 400);
}
$persistence = new SmartDataContextCrud($this->_get_db());
$result = $persistence->query(domain: $domain, query: $query);
if ($result['success']) {
return $this->return_json($response, $result["contents"]);
} else {
return $this->return_error($response, $result["errors"], 400);
}
}
private function _get_db() {
return DBManager::GetMongoDBConnection();
}
private function return_json(Response $response, mixed $object, $status = 200) {
$result = array("result" => $object);
$response = $response->withAddedHeader("Content-Type", "application/json")->withStatus($status);
$response->getBody()->write(json_encode($result));
return $response;
}
private function return_error(Response $response, mixed $errorList, $status = 200) {
$result = array("errors" => $errorList);
$response = $response->withAddedHeader("Content-Type", "application/json")->withStatus($status);
$response->getBody()->write(json_encode($result));
return $response;
}
private function _get_value_or_default($body, $key, $default) {
if (! array_key_exists($key, $body)) {
return $default;
} else {
return $body[$key];
}
}
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
function update(Request $request, Response $response, array $params) {
// Basic validation
[$domain, $body, $errors] = $this->_basicValidation($params, $request, $response);
if ($errors) {
return $this->return_error($response, $errors, 400);
}
$id = $this->_get_value_or_default($params, 'id', null);
if (!$id) {
return $this->return_error($response, ["missing SmartDataContextID"], 400);
}
// Validate properties only present in the request body
$updateData = [];
if (isset($body['content'])) {
$contentErrors = SmartDataContext::ValidateContent($body['content']);
if ($contentErrors) {
$errors = array_merge($errors, $contentErrors);
}
$updateData['content'] = $body['content'];
}
if (isset($body['features'])) {
$featuresErrors = SmartDataContext::ValidateFeatures($body['features']);
if ($featuresErrors) {
$errors = array_merge($errors, $featuresErrors);
}
$updateData['features'] = $body['features'];
}
$t0Set = isset($body['t0']);
$t1Set = isset($body['t1']);
if ($t0Set && $t1Set) {
$timestampErrors = SmartDataContext::ValidateTimestamp($body['t0'], $body['t1']);
if ($timestampErrors) {
$errors = array_merge($errors, $timestampErrors);
}
$updateData['t0'] = $body['t0'];
$updateData['t1'] = $body['t1'];
} elseif ($t0Set || $t1Set) {
// Error if only one of t0 or t1 is set
$errors[] = "Both t0 and t1 must be provided together.";
}
if (isset($body['smartDataSources'])) {
$smartDataSourcesErrors = SmartDataContext::ValidateSmartDataSources($body['smartDataSources']);
if ($smartDataSourcesErrors) {
$errors = array_merge($errors, $smartDataSourcesErrors);
}
$updateData['smartDataSources'] = $body['smartDataSources'];
}
if (isset($body['smartDataUnits'])) {
$smartDataUnitsErrors = SmartDataContext::ValidateSmartDataUnits($body['smartDataUnits']);
if ($smartDataUnitsErrors) {
$errors = array_merge($errors, $smartDataUnitsErrors);
}
$updateData['smartDataUnits'] = $body['smartDataUnits'];
}
if ($errors) {
return $this->return_error($response, $errors, 400);
}
// Call the update method from SmartDataContextCrud
$persistence = new SmartDataContextCrud($this->_get_db());
$result = $persistence->update($domain, $id, $updateData);
if ($result['success']) {
return $this->return_json($response, $result['contents']);
} else {
return $this->return_error($response, $result['errors'], 400);
}
}
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
function addUnstructuredData(Request $request, Response $response, array $params) {
$domain = $this->_get_value_or_default($params, 'domain', null);
$errors = [];
if (! $domain) {
$errors[] = "missing domain";
}
$smartdatacontextid = $this->_get_value_or_default($params, 'id', null);
if (! $smartdatacontextid) {
$errors[] = "missing SmartDataContext ID";
}
$contentype = $request->getHeader("Content-Type")[0];
if (! $contentype) {
$errors[] = "missing Header Content-Type";
}
$filename = $request->getHeader("Filename")[0];
if (! $filename) {
$errors[] = "missing Header Filename";
}
if ($errors) {
return $this->return_error($response, $errors, 400);
}
$persistence = new SmartDataContextCrud($this->_get_db());
$minio = new MinIOClient();
$objectid = $minio->storeUnstructuredData($domain, $request->getBody());
$result = $persistence->addUnstructuredData($domain, $smartdatacontextid, $objectid, $contentype, $filename);
if ($result['success']) {
return $this->return_json($response, ["objectId" => $objectid]);
} else {
$minio->deleteObject($domain, $objectid);
return $this->return_error($response, $result['errors'], 400);
}
}
function removeUnstructuredData(Request $request, Response $response, array $params) {
[$domain, $body, $errors] = $this->_basicValidation($params, $request, $response);
if ($errors) {
return $this->return_error($response, $errors, 400);
}
$errors = [];
$smartdatacontextid = $this->_get_value_or_default($body, 'smartDataContextId', null);
if (! isset($smartdatacontextid)) {
$errors[] = 'Missing smartDataContextId';
}
$objectid = $this->_get_value_or_default($body, 'objectId', null);
if (! isset($objectid)) {
$errors[] = 'Missing objectId';
}
if ($errors) {
return $this->return_error($response, $errors, 400);
}
$minio = new MinIOClient();
$persistence = new SmartDataContextCrud($this->_get_db());
$result = $persistence->removeUnstructuredData($domain, $smartdatacontextid, $objectid);
if ($result['success']) {
$minio->deleteObject($domain, $objectid);
return $this->return_json($response, $result['result']);
} else {
return $this->return_error($response, $result['errors'], 400);
}
}
function getUnstructuredData(Request $request, Response $response, array $params) {
[$domain, $body, $errors] = $this->_basicValidation($params, $request, $response);
$smartdatacontextid = $this->_get_value_or_default($body, 'smartDataContextId', null);
if (! isset($domain)) {
$errors[] = "missing smartDataContextId";
}
$objectid = $this->_get_value_or_default($body, 'objectId', null);
if (! isset($domain)) {
$errors[] = "missing objectId";
}
if ($errors) {
return $this->return_error($response, $errors, 400);
}
$persistence = new SmartDataContextCrud($this->_get_db());
$smartdatacontext = $persistence->get($domain, $smartdatacontextid);
if (!$smartdatacontext['success']) {
return $this->return_error($response, ['SmartDataContext not found!'], 500);
}
if (!$smartdatacontext['contents']['unstructuredData']) {
return $this->return_error($response, ['SmartDataContext without unstructured data!'], 500);
}
$objectMetaData = null;
foreach ($smartdatacontext['contents']['unstructuredData'] as $unstructuredData) {
if ($unstructuredData['id'] == $objectid) {
$objectMetaData = $unstructuredData;
break;
}
}
if (!$objectMetaData) {
return $this->return_error($response, ['objectId not found!'], 500);
}
$minio = new MinIOClient();
$objectStream = $minio->getObject($domain, $objectid);
if ($objectStream) {
$response = $response
->withHeader('Content-Type', $objectMetaData['mimetype'])
->withHeader('Content-Disposition', 'attachment; filename="' . $objectMetaData['filename'] . '"');
return $response->withBody($objectStream);
} else {
return $this->return_error($response, ["Failed to retrieve object from MinIO"], 500);
}
}