-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMetadataOnboard.php
More file actions
208 lines (178 loc) · 8.12 KB
/
Copy pathMetadataOnboard.php
File metadata and controls
208 lines (178 loc) · 8.12 KB
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
199
200
201
202
203
204
205
206
207
208
<?php
namespace App\Http\Traits;
use Config;
use Exception;
use App\Models\Dataset;
use App\Jobs\DatasetSqlLinkageJob;
use App\Jobs\TermExtraction;
use Illuminate\Support\Str;
use MetadataManagementController as MMC;
trait MetadataOnboard
{
/**
* Create new Dataset, calling translation service if necessary
*
* @return array
*/
public function metadataOnboard(
array $input,
array $team,
string | null $inputSchema,
string | null $inputVersion,
bool $elasticIndexing
): array {
$isCohortDiscovery = array_key_exists('is_cohort_discovery', $input) ?
$input['is_cohort_discovery'] : false;
//send the payload to traser
// - traser will return the input unchanged if the data is
// already in the GWDM with GWDM_CURRENT_VERSION
// - if it is not, traser will try to work out what the metadata is
// and translate it into the GWDM
// - otherwise traser will return a non-200 error
$payload = $input['metadata'];
$payload['extra'] = [
'id' => 'placeholder',
'pid' => 'placeholder',
'datasetType' => 'Health and disease',
'publisherId' => $team['pid'],
'publisherName' => $team['name']
];
$traserResponse = MMC::translateDataModelType(
json_encode($payload),
Config::get('metadata.GWDM.name'),
Config::get('metadata.GWDM.version'),
$inputSchema,
$inputVersion,
$input['status'] !== Dataset::STATUS_DRAFT, // Disable input validation if it's a draft
$input['status'] !== Dataset::STATUS_DRAFT // Disable output validation if it's a draft
);
if ($traserResponse['wasTranslated']) {
$input['metadata']['original_metadata'] = $input['metadata']['metadata'];
$input['metadata']['metadata'] = $traserResponse['metadata'];
$mongo_object_id = array_key_exists('mongo_object_id', $input) ? $input['mongo_object_id'] : null;
$mongo_id = array_key_exists('mongo_id', $input) ? $input['mongo_id'] : null;
$mongo_pid = array_key_exists('mongo_pid', $input) ? $input['mongo_pid'] : null;
$datasetid = array_key_exists('datasetid', $input) ? $input['datasetid'] : null;
$pid = array_key_exists('pid', $input) ? $input['pid'] : (string) Str::uuid();
$dataset = MMC::createDataset([
'user_id' => $input['user_id'],
'team_id' => $input['team_id'],
'mongo_object_id' => $mongo_object_id,
'mongo_id' => $mongo_id,
'mongo_pid' => $mongo_pid,
'datasetid' => $datasetid,
'created' => now(),
'updated' => now(),
'submitted' => now(),
'pid' => $pid,
'create_origin' => $input['create_origin'],
'status' => $input['status'],
'is_cohort_discovery' => $isCohortDiscovery,
]);
$publisher = null;
$revisions = [
[
"url" => env('GATEWAY_URL') . '/dataset' .'/' . $dataset->id . '?version=1.0.0',
'version' => $this->formatVersion(1)
]
];
$required = [
'gatewayId' => strval($dataset->id), //note: do we really want this in the GWDM?
'gatewayPid' => $dataset->pid,
'issued' => $dataset->created,
'modified' => $dataset->updated,
'revisions' => $revisions
];
// -------------------------------------------------------------------
// * Create a new 'required' section for the metadata to be saved
// - otherwise this section is filled with placeholders by all translations to GWDM
// * Force correct publisher field based on the team associated with
//
// Note:
// - This is hopefully a rare scenario when the BE has to be changed due to an update
// to the GWDM
// - future releases of the GWDM will hopefully not modify anything that we need to
// set via the MMC
// - we can't pass the publisherId nor the gatewayPid of the dataset to traser before
// they have been created, this is why we are doing this..
// - GWDM >= 1.1 versions have a change related to these sections of the GWDM
// - addition of the field 'version' in the required field
// - restructure of the 'publisher' in the summary field
// - publisher.publisherId --> publisher.gatewayId
// - publisher.publisherName --> publisher.name
// -------------------------------------------------------------------
if(version_compare(Config::get('metadata.GWDM.version'), '1.1', '<')) {
$publisher = [
'publisherId' => $team['pid'],
'publisherName' => $team['name'],
];
} else {
$version = $this->formatVersion(1);
if(array_key_exists('version', $input['metadata']['metadata']['required'])) {
$version = $input['metadata']['metadata']['required']['version'];
}
$required['version'] = $version;
$publisher = [
'gatewayId' => $team['pid'],
'name' => $team['name'],
];
}
$input['metadata']['metadata']['required'] = $required;
$input['metadata']['metadata']['summary']['publisher'] = $publisher;
//include a note of what the metadata was (i.e. which GWDM version)
$input['metadata']['gwdmVersion'] = Config::get('metadata.GWDM.version');
$version = MMC::createDatasetVersion([
'dataset_id' => $dataset->id,
'metadata' => json_encode($input['metadata']),
'version' => 1,
]);
// Dispatch the dataset to update the SQL linkages tables
// DatasetVersionHasTool
// DatasetVersionHasDatasetVersion
// PublicationHasDatasetVersion
// DatasetVersionHasSpatialCoverage
// This uses the input metadata and new dataset version to create the indexes.
//$this->createSqlLinkageFromDataset($input['metadata'], $dataset, false);
DatasetSqlLinkageJob::dispatch($input['metadata'], $dataset, false);
// Dispatch term extraction to a subprocess if the dataset is marked as active
if($input['status'] === Dataset::STATUS_ACTIVE && Config::get('ted.enabled')) {
$tedData = Config::get('ted.use_partial') ? $input['metadata']['metadata']['summary'] : $input['metadata']['metadata'];
TermExtraction::dispatch(
$dataset->id,
$version->id,
'1',
base64_encode(gzcompress(gzencode(json_encode($tedData)))),
$elasticIndexing,
Config::get('ted.use_partial')
);
}
return [
'translated' => true,
'dataset_id' => $dataset->id,
'version_id' => $version->id,
];
} else {
return [
'translated' => false,
'response' => $traserResponse,
];
}
}
public function getVersion(int $version)
{
if($version > 999) {
throw new Exception('too many versions');
}
$version = max(0, $version);
$hundreds = floor($version / 100);
$tens = floor(($version % 100) / 10);
$units = $version % 10;
$formattedVersion = "{$hundreds}.{$tens}.{$units}";
return $formattedVersion;
}
public function formatVersion(int $version)
{
$formattedVersion = "{$version}.0.0";
return $formattedVersion;
}
}