Newer
Older
1
2
3
4
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
<?php
namespace SmartDataContext\Persistence;
use Exception;
use MongoDB\Database;
use MongoDB\Collection;
use Ramsey\Uuid\Uuid;
use SmartDataContext\Domain\SmartDataContext;
class SmartDataContextCrud {
private Database $connection;
private static array $collections = [];
private function generateID():string {
return Uuid::uuid4()->toString();
}
function __construct(Database $dbconn) {
$this->connection = $dbconn;
SmartDataContextCrud::$collections = [];
}
function create($domain, $t0, $t1, $content, $features, $smartDataSources, $smartDataUnits):array {
if (! array_key_exists('identifier', $features)) {
$features['identifier'] = $this->build_identifier($content);
}
$id = $this->generateID();
$this->getCollection($domain)->insertOne([
"id" => $id,
"t0" => $t0,
"t1" => $t1,
"content" => $content,
"features" => $features,
"smartDataSources" => $smartDataSources,
"smartDataUnits" => $smartDataUnits
]
);
return array("success" => true, "contents" => array("smartDataContextId" => $id));
}
private function getCollection($domain): Collection {
if (in_array($domain, SmartDataContextCrud::$collections)) {
return $this->connection->{$domain};
} else {
$cols = $this->connection->listCollectionNames(["name" => $domain]);
$cols->next();
if (! $cols->valid()) {
$this->createCollection($domain);
} else {
SmartDataContextCrud::$collections[] = $domain;
}
return $this->connection->{$domain};
}
}
private function createCollection($domain) {
$this->connection->createCollection($domain);
$this->connection->selectCollection($domain)->createIndex(["id" => 1]);
$this->connection->selectCollection($domain)->createIndex(["features" => 1]);
$this->connection->selectCollection($domain)->createIndex(["tags" => 1]);
$this->connection->selectCollection($domain)->createIndex(["smartDataSources" => 1]);
$this->connection->selectCollection($domain)->createIndex(["smartDataUnits" => 1]);
$this->connection->selectCollection($domain)->createIndex(["t0" => 1, "t1" => 1, "smartDataUnits" => 1]);
$this->connection->selectCollection($domain)->createIndex(["t0" => 1, "t1" => 1, "smartDataSources" => 1]);
}
private function build_identifier($content) {
return sha1(json_encode($content));
}
public function get($domain, $id):mixed {
$smartdatacontext = $this->getCollection($domain)->findOne(["id" => $id]);
if (! isset($smartdatacontext)) {
return ["success" => false, "errors" => ["not found"]];
} else {
return ["success" => true, "contents" => json_decode(json_encode($smartdatacontext), true)];
}
}
public function associate(mixed $domain, mixed $id, mixed $smartDataSources, mixed $smartDataUnits)
{
$errors = array_merge(SmartDataContext::ValidateSmartDataSources($smartDataSources),
SmartDataContext::ValidateSmartDataUnits($smartDataUnits));
if ($errors) {
return ["success" => false, "errors" => $errors];
}
$smartdatacontext = json_decode(json_encode($this->getCollection($domain)->findOne(["id" => $id])), true);
if ($smartdatacontext) {
$smartDataUnitsToAdd = [];
foreach ($smartDataUnits as $smartDataUnit) {
if (! in_array($smartDataUnit, $smartdatacontext['smartDataUnits'])) {
$smartDataUnitsToAdd[] = $smartDataUnit;
}
}
$smartDataSourcesToAdd = [];
foreach ($smartDataSources as $smartDataSource) {
if (! in_array($smartDataSource, $smartdatacontext['smartDataSources'])) {
$smartDataSourcesToAdd[] = $smartDataSource;
}
}
$updateData = [
'$push' => [
'smartDataSources' => [
'$each' => $smartDataSourcesToAdd
],
'smartDataUnits' => [
'$each' => $smartDataUnitsToAdd
]
]
];
$this->getCollection($domain)->updateOne(["id" => $id], $updateData);
return ["success" => true, "result" => json_decode(json_encode($this->getCollection($domain)->findOne(["id" => $id])), true)];
} else {
return ["success" => false, "errors" => ["smartdatacontext id not found"]];
}
}
public function unassociate(mixed $domain, mixed $id, mixed $smartDataSources, mixed $smartDataUnits)
{
$errors = array_merge(SmartDataContext::ValidateSmartDataSources($smartDataSources),
SmartDataContext::ValidateSmartDataUnits($smartDataUnits));
if ($errors) {
return ["success" => false, "errors" => $errors];
}
$smartdatacontext = json_decode(json_encode($this->getCollection($domain)->findOne(["id" => $id])), true);
if ($smartdatacontext) {
$smartDataUnitsToRemove = [];
foreach ($smartDataUnits as $smartDataUnit) {
if (in_array($smartDataUnit, $smartdatacontext['smartDataUnits'])) {
$smartDataUnitsToRemove[] = $smartDataUnit;
}
}
$smartDataSourcesToRemove = [];
foreach ($smartDataSources as $smartDataSource) {
if (in_array($smartDataSource, $smartdatacontext['smartDataSources'])) {
$smartDataSourcesToRemove[] = $smartDataSource;
}
}
if ($smartDataSourcesToRemove && $smartDataUnitsToRemove) {
$updateData = [
'$pull' => [
'smartDataSources' => ['$in' => $smartDataSourcesToRemove],
'smartDataUnits' => ['$in' => $smartDataUnitsToRemove]
]
];
} else if ($smartDataSourcesToRemove) {
$updateData = [
'$pull' => [
'smartDataSources' => ['$in' => $smartDataSourcesToRemove]
]
];
} else if ($smartDataUnitsToRemove) {
$updateData = [
'$pull' => [
'smartDataUnits' => ['$in' => $smartDataUnitsToRemove]
]
];
} else {
return ["success" => true, "result" => json_decode(json_encode($this->getCollection($domain)->findOne(["id" => $id])), true)];
}
$this->getCollection($domain)->updateOne(["id" => $id], $updateData);
return ["success" => true, "result" => json_decode(json_encode($this->getCollection($domain)->findOne(["id" => $id])), true)];
} else {
return ["success" => false, "errors" => ["smartdatacontext id not found"]];
}
}
public function query(string $domain, mixed $query)
{
try {
$contentsMongo = $this->getCollection($domain)->find($query);
$contents = [];
foreach ($contentsMongo as $contentMongo) {
$contents[] = json_decode(json_encode($contentMongo), true);
}
return ["success" => true, "contents" => $contents];
} catch (Exception $exception) {
return ["success" => false, "errors" => [$exception->getMessage()]];
}
}
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
public function update($domain, $id, array $updateData): array {
// Find the document in the MongoDB collection using the id field value
$collection = $this->getCollection($domain);
$document = $collection->findOne(["id" => $id]);
// Check if the document exists
if ($document === null) {
return ["success" => false, "errors" => ["SmartDataContext with id $id not found"]];
}
// Update the document's properties using the associative array received
$updateFields = [];
foreach ($updateData as $key => $value) {
$updateFields[$key] = $value;
}
// Prepare the update query
$updateQuery = ['$set' => $updateFields];
// Update the document in the MongoDB collection
try {
$collection->updateOne(["id" => $id], $updateQuery);
$updatedDocument = $collection->findOne(["id" => $id]);
return ["success" => true, "contents" => json_decode(json_encode($updatedDocument), false)];
} catch (Exception $e) {
return ["success" => false, "errors" => [$e->getMessage()]];
}
}