-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiappeFormsToRiver.pl
395 lines (352 loc) · 12.5 KB
/
miappeFormsToRiver.pl
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
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
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
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
#!/usr/bin/perl -w
use strict;
use Switch 'fallthrough';
use LWP::UserAgent;
use HTTP::Request;
my $ua = new LWP::UserAgent;
$ua->requests_redirectable( [ 'GET', 'HEAD', 'POST', 'PUT' ] );
$ua->agent("Perl API Client/1.0");
use JSON;
use Data::Dumper;
use Text::Slugify 'slugify';
use constant false => \0;
use constant true => \1;
my $url = "http://localhost:3005/";
# my $url = "https://api.river.serenity-sb.io/";
# my $url = "https://api.tomorrowstodayslater.xyz/";
my $response;
my $respHash;
my $inFile = "MIAPPE-v1.1-Serenity.mod.json";
###################################################
## Fetch existing Attribute/Model name/id pairs
my %attIDs;
my %modelIDs;
my %formIDs;
$response = $ua->get( $url . "_attributes" );
die "Request failed: \n", $response->content unless $response->is_success();
$respHash = JSON->new->decode( $response->content );
foreach my $attrHash ( @{ $respHash->{'data'}->{'attributes'} } ) {
$attIDs{ $attrHash->{'name'} } = $attrHash->{'id'};
}
print "Loaded " . scalar( keys %attIDs ) . " existing attribute name & ids\n";
$response = $ua->get( $url . "_models" );
die "Request failed: \n", $response->content unless $response->is_success();
$respHash = JSON->new->decode( $response->content );
foreach my $modelHash ( @{ $respHash->{'data'}->{'models'} } ) {
$modelIDs{ $modelHash->{'name'} } = $modelHash->{'id'};
}
print "Loaded " . scalar( keys %modelIDs ) . " existing model name & ids\n";
###################################################
## Load input Forms JSON file
my $formRawJSON = "";
open( INFILE, "<$inFile" ) or die("Couldn't open $inFile for input\n");
while (<INFILE>) {
$formRawJSON .= $_;
}
close INFILE;
my $formRawHash = JSON->new->decode($formRawJSON);
###################################################
## Establish load order
## Resolves prerequites from embedded references
## Checks which sub-forms are to be arrays
print "Establishing Form insert order...\n";
my %formIsArray;
my %formPreReqs;
my %formOrder;
foreach my $formModel ( @{ $formRawHash->{models} } ) {
$formPreReqs{ $formModel->{name} } = ();
foreach my $formFieldAtt ( @{ $formModel->{attributes} } ) {
if ( !( ref($formFieldAtt) eq 'ARRAY' ) ) {
if ( !defined( $formFieldAtt->{name} ) ) {
foreach my $formKey ( keys %{$formFieldAtt} ) {
if ( $formKey ne "required" ) {
if ( ref( $formFieldAtt->{$formKey} ) eq 'ARRAY' ) {
my ($prereq)
= ( $formFieldAtt->{$formKey}->[0]->{"\$ref"}
=~ /^.+\/(\S+)/ );
$formPreReqs{ $formModel->{name} }{$prereq}
= $formKey;
$formIsArray{$prereq} = 1;
}
else {
my ($prereq)
= ( $formFieldAtt->{$formKey}->{"\$ref"}
=~ /^.+\/(\S+)/ );
$formPreReqs{ $formModel->{name} }{$prereq}
= $formKey;
}
}
}
}
}
}
}
my $allOrdered = 1;
my $nextOrderNum = 1;
my $loopCount = 1;
do {
$allOrdered = 1;
foreach my $aForm ( keys %formPreReqs ) {
if ( !( defined( $formOrder{$aForm} ) ) ) {
my $goodToAdd = 1;
foreach my $prereq ( keys %{ $formPreReqs{$aForm} } ) {
if ( !( defined( $formOrder{$prereq} ) ) ) {
$goodToAdd = 0;
$allOrdered = 0;
last;
}
}
if ($goodToAdd) {
$formOrder{$aForm} = $nextOrderNum;
$nextOrderNum++;
}
}
}
$loopCount++;
if ( $loopCount > 5 ) {
warn "Failure to order Form inserts by prerequisites\n";
warn "PreReqs: " . Dumper(%formPreReqs);
warn "Order: " . Dumper(%formOrder);
die();
}
} until ($allOrdered);
print "Form insert order:\n";
foreach my $formModel (
sort { $formOrder{$a} <=> $formOrder{$b} }
keys %formOrder
)
{
print "$formModel ";
}
print "\n";
###################################################
## Parse JSON Models into Forms and _models
foreach my $formModelName (
sort { $formOrder{$a} <=> $formOrder{$b} }
keys %formOrder
)
{
my $formModel = ();
my $foundForm = 0;
foreach my $aFormModel ( @{ $formRawHash->{models} } ) {
if ( $aFormModel->{name} eq $formModelName ) {
$formModel = $aFormModel;
$foundForm = 1;
last;
}
}
if ( !$foundForm ) {
die("Failed to switch to form JSON section, $formModelName!\n");
}
my $modelUrl = "miappe " . $formModel->{name};
if ( $formModel->{name} eq "Experiment"
|| $formModel->{name} eq "MiappeExp" )
{
$modelUrl = $formModel->{name};
}
$modelUrl =~ s/([A-Z])/ $1/g;
$modelUrl = slugify($modelUrl);
my $arrayFormStr = "false";
my $arrayForm = false;
if ( defined( $formIsArray{ $formModel->{name} } ) ) {
$arrayFormStr = "true";
$arrayForm = true;
}
my %modelDef = (
"name" => $formModel->{name},
"url" => $modelUrl,
"attributes" => { "connect" => [] }
);
my %FormDef = (
"name" => $formModel->{name},
"data" => {
"name" => $formModel->{name},
"description" => $formModel->{definition},
"isArray" => $arrayFormStr,
"fields" => [],
"subsections" => []
}
);
###### Turn JSON attributes into FormFields and modelAttributes #####
foreach my $formFieldAtt ( @{ $formModel->{attributes} } ) {
my $arrayAttrStr = "false";
my $arrayAttr = false;
if ( ref($formFieldAtt) eq 'ARRAY' ) {
$formFieldAtt = ${$formFieldAtt}[0];
$arrayAttrStr = "true";
$arrayAttr = true;
}
if ( defined( $formFieldAtt->{name} ) ) {
if ( defined( $attIDs{ $formFieldAtt->{name} } ) ) {
push @{ $modelDef{"attributes"}{"connect"} },
{ "id" => $attIDs{ $formFieldAtt->{name} } };
}
else {
my %newAttr = (
"name" => $formFieldAtt->{name},
"hasModel" => false,
"isArray" => $arrayAttr,
"type" => "String"
);
my $newAttrID = POSTdata( \%newAttr, "_attributes" );
if ( !$newAttrID ) {
die("Attribute POST failure\n");
}
else {
push @{ $modelDef{"attributes"}{"connect"} },
{ "id" => $newAttrID };
$attIDs{ $formFieldAtt->{name} } = $newAttrID;
}
}
my $fieldRequired = "false";
if ( defined( $formFieldAtt->{required} ) ) {
$fieldRequired = "true";
}
my %newField = (
"name" => $formFieldAtt->{name},
"data" => {
"name" => $formFieldAtt->{name},
"label" => $formFieldAtt->{label},
"required" => "$fieldRequired",
"description" => $formFieldAtt->{definition},
"format" => $formFieldAtt->{format},
"example" => $formFieldAtt->{example},
"isArray" => $arrayAttrStr
}
);
my $fieldID = POSTdata( \%newField, "fields" );
if ( !$fieldID ) {
die("Field POST failure\n");
}
else {
push @{ $FormDef{"data"}{"fields"} }, { "id" => $fieldID };
}
}
}
###### Turn Form sub-Forms into subsections and modelAttributes #####
foreach my $prereq ( keys %{ $formPreReqs{ $formModel->{name} } } ) {
print "Connect $prereq to " . $formModel->{name} . "\n";
push @{ $FormDef{"data"}{"subsections"} },
{ "id" => $formIDs{$prereq} };
my $subFormArray = false;
if ( defined( $formIsArray{$prereq} ) ) {
$subFormArray = true;
}
my %newAttr = (
"name" => $formPreReqs{ $formModel->{name} }{$prereq},
"hasModel" => true,
"isArray" => $subFormArray,
"type" => "String",
"model" => { "connect" => { "id" => $modelIDs{$prereq} } }
);
my $newAttrID = POSTdata( \%newAttr, "_attributes" );
if ( !$newAttrID ) {
die("Attribute POST failure\n");
}
else {
push @{ $modelDef{"attributes"}{"connect"} },
{ "id" => $newAttrID };
## $attIDs{ $formFieldAtt->{name} } = $newAttrID;
}
}
###### Insert final Form and Model #####
my $formID = POSTdata( \%FormDef, "forms" );
if ( !$formID ) {
die("Form POST failure\n");
}
else {
$formIDs{ $formModel->{name} } = $formID;
}
my $modelID = POSTdata( \%modelDef, "_models" );
if ( !$modelID ) {
die("Model POST failure\n");
}
else {
$modelIDs{ $formModel->{name} } = $modelID;
}
}
###########################
###########################
##
## POST (or PUT) a hash-ref -> JSON, to an api path
## Returns object ID on success, or 0
sub POSTdata {
my $type = "POST";
if ( $#_ < 1 ) {
warn
"POSTdata usage: POSTdata( hash-ref-data, api-path, [ POST/PUT ] )";
return 0;
}
my ( $dataHashRef, $apiSection ) = ( $_[0], $_[1] );
if ( $#_ >= 2 ) {
$type = $_[2];
}
my $response;
print "$type to $apiSection...\n";
# print JSON->new->pretty->encode($dataHashRef);
if ( $type eq "POST" ) {
$response = $ua->post(
$url . $apiSection,
Content => JSON->new->encode($dataHashRef),
'Content-Type' => 'application/json'
);
}
elsif ( $type eq "PUT" ) {
$response = $ua->put(
$url . $apiSection,
Content => JSON->new->encode($dataHashRef),
'Content-Type' => 'application/json'
);
}
else {
warn
"POSTdata usage: POSTdata( hash-ref-data, api-path, [ POST/PUT ] )";
return 0;
}
my $respHash = {};
if ( $response->is_success ) {
print "$type success.\n";
$respHash = JSON->new->decode( $response->content );
if ( defined( $respHash->{'data'}->{'instance'} ) ) {
if ( defined( $respHash->{'data'}->{'instance'}->{'id'} ) ) {
print "$apiSection ID = "
. $respHash->{'data'}->{'instance'}->{'id'} . "\n\n";
return $respHash->{'data'}->{'instance'}->{'id'};
}
}
if ( defined( $respHash->{'data'}->{'createModel'} ) ) {
if ( defined( $respHash->{'data'}->{'createModel'}->{'id'} ) ) {
print "$apiSection ID = "
. $respHash->{'data'}->{'createModel'}->{'id'} . "\n\n";
return $respHash->{'data'}->{'createModel'}->{'id'};
}
}
if ( defined( $respHash->{'data'}->{'attribute'} ) ) {
if ( defined( $respHash->{'data'}->{'attribute'}->{'id'} ) ) {
print "$apiSection ID = "
. $respHash->{'data'}->{'attribute'}->{'id'} . "\n\n";
return $respHash->{'data'}->{'attribute'}->{'id'};
}
}
if ( defined( $respHash->{'data'}->{'updateModel'} ) ) {
if ( defined( $respHash->{'data'}->{'updateModel'}->{'id'} ) ) {
print "$apiSection ID = "
. $respHash->{'data'}->{'updateModel'}->{'id'} . "\n\n";
return $respHash->{'data'}->{'updateModel'}->{'id'};
}
}
}
else {
warn "$type failed!:\n";
warn "$type object: \n"
. JSON->new->pretty->encode($dataHashRef) . "\n";
warn $response->code . "\n";
warn $response->message . "\n";
warn "Response content: \n" . $response->content . "\n";
return 0;
}
warn "** Failed to parse a $type result id!! **\n";
warn "$type object: \n" . JSON->new->pretty->encode($dataHashRef) . "\n";
warn "Response content: \n" . JSON->new->pretty->encode($respHash) . "\n";
return 0;
}