-
Notifications
You must be signed in to change notification settings - Fork 0
/
caseGenerator_warrant.pas
528 lines (473 loc) · 16 KB
/
caseGenerator_warrant.pas
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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
unit caseGenerator_warrant;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.DateTimeCtrls, FMX.Calendar, FMX.Edit, FMX.StdCtrls, FMX.Layouts,
FMX.ListBox, FMX.Controls.Presentation, System.JSON, System.JSON.Types,
System.JSON.Readers, caseGenerator_util;
type
TformWarrant = class(TForm)
Label1: TLabel;
lbWarrant: TListBox;
btnClose: TButton;
btnAddWarrant: TButton;
btnRemoveWarrant: TButton;
panelWarrant: TPanel;
Label5: TLabel;
Label8: TLabel;
Label9: TLabel;
cbAuthority: TComboBox;
edAuthorizationType: TEdit;
Label2: TLabel;
cbDay: TComboBox;
cbMonth: TComboBox;
cbYear: TComboBox;
Label3: TLabel;
edIdentifier: TEdit;
btnModify: TButton;
btnCancel: TButton;
procedure btnAddWarrantClick(Sender: TObject);
procedure btnRemoveWarrantClick(Sender: TObject);
procedure btnCloseClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure lbWarrantChange(Sender: TObject);
procedure btnModifyClick(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
private
FuuidCase: string;
FpathCase: String;
procedure SetuuidCase(const Value: string);
procedure SetpathCase(const Value: String);
property uuidCase: string read FuuidCase write SetuuidCase;
property pathCase: String read FpathCase write SetpathCase;
function JsonTokenToString(const t: TJsonToken): string;
function readIdRoleFromFile: TStringList;
function readIdentityFromRelationshipFile(idRoles: TStringList): TStringList;
function extractID(line: String): String;
function prepareItemWarrant(operation: String): String;
procedure readIdentityFromFile(idValues: TStringList);
{ Private declarations }
public
procedure ShowWithParamater(pathCase: String; uuidCase: String);
{ Public declarations }
end;
var
formWarrant: TformWarrant;
implementation
{$R *.fmx}
uses StrUtils;
{ TForm1 }
procedure TformWarrant.btnRemoveWarrantClick(Sender: TObject);
begin
lbWarrant.Items.Delete(lbWarrant.ItemIndex);
end;
function TformWarrant.extractID(line: String): String;
begin
Result := Copy(line, Pos('@', line) + 1, Length(line));
end;
procedure TformWarrant.FormShow(Sender: TObject);
var
idx: Integer;
idValues: TStringList;
begin
cbYear.Items.Clear;
for idx:=2020 to 2030 do
cbYear.Items.Add(IntToStr(idx));
{
- the cbAuthority values must be taken combining Role and Relationship:
1. extracting of the idRoles with name="Judge" or "Judicial Authority"
2. extracting idIdentities as value of the "source" property where the above
idRoles[idx] appears in the "target" property
3. extracting name and familyName of the idIdentities[idx] above identified
}
idValues := TStringList.Create;
// idRole Judge/Authority
idValues := readIdRoleFromFile;
//idIdentities of Judge/Authority
idValues := readIdentityFromRelationshipFile(idValues);
readIdentityFromFile(idValues);
end;
function TformWarrant.JsonTokenToString(const t: TJsonToken): string;
begin
case t of
TJsonToken.None: Result := 'None';
TJsonToken.StartObject: Result := 'StartObject' ;
TJsonToken.StartArray: Result := 'StartArray' ;
TJsonToken.StartConstructor: Result := 'StartConstructor' ;
TJsonToken.PropertyName: Result := 'PropertyName' ;
TJsonToken.Comment: Result := 'Comment' ;
TJsonToken.Raw: Result := 'Raw' ;
TJsonToken.Integer: Result := 'Integer' ;
TJsonToken.Float: Result := 'Float' ;
TJsonToken.String: Result := 'String' ;
TJsonToken.Boolean: Result := 'Boolean' ;
TJsonToken.Null: Result := 'Null' ;
TJsonToken.Undefined: Result := 'Undefined' ;
TJsonToken.EndObject: Result := 'EndObject' ;
TJsonToken.EndArray: Result := 'EndArray' ;
TJsonToken.EndConstructor: Result := 'EndConstructor' ;
TJsonToken.Date: Result := 'Date' ;
TJsonToken.Bytes: Result := 'Bytes' ;
TJsonToken.Oid: Result := 'Oid' ;
TJsonToken.RegEx: Result := 'RegEx' ;
TJsonToken.DBRef: Result := 'DBRef' ;
TJsonToken.CodeWScope: Result := 'CodeWScope' ;
TJsonToken.MinKey: Result := 'MinKey' ;
TJsonToken.MaxKey: Result := 'MaxKey' ;
end;
end;
procedure TformWarrant.lbWarrantChange(Sender: TObject);
var
line, issuedDate, sDate, sDay, sMonth, sYear: String;
idx: Integer;
begin
if lbWarrant.ItemIndex > - 1 then
begin
line := lbWarrant.Items[lbWarrant.ItemIndex];
edAuthorizationType.Text := ExtractField(line, '"uco-investigation:authorizationType":"');
edIdentifier.Text := ExtractField(line, '"uco-investigation:authorizationIdentifier":"');
issuedDate := ExtractField(line, '"uco-investigation:authorizationIssuedDate":"');
sDate := Copy(issuedDate, 1, 10);
sDay := Copy(sDate, 7, 2);
for idx:=0 to cbDay.Items.Count - 1 do
begin
if cbDay.Items[idx] = sDay then
begin
cbDay.ItemIndex := idx;
break;
end;
end;
sMonth := Copy(sDate, 5, 2);
for idx:=0 to cbMonth.Items.Count - 1 do
begin
if cbMonth.Items[idx] = sMonth then
begin
cbMonth.ItemIndex := idx;
break;
end;
end;
sYear := Copy(sDate, 1, 4);
for idx:=0 to cbYear.Items.Count - 1 do
begin
if cbYear.Items[idx] = sYear then
begin
cbYear.ItemIndex := idx;
break;
end;
end;
line := ExtractRefId(line, '"uco-investigation:authorizationAuthority":"');
for idx:=0 to cbAuthority.Items.Count - 1 do
begin
if AnsiContainsStr(cbAuthority.Items[idx], line) then
begin
cbAuthority.ItemIndex := idx;
break;
end;
end;
end;
end;
function TformWarrant.prepareItemWarrant(operation: String): String;
var
line, recSep, lineID, indent, guidNoBraces: string;
Uid: TGUID;
idx: Integer;
begin
recSep := #30 + #30;
indent := ' ';
line := '{' + recSep;
if operation = 'add' then
begin
CreateGUID(Uid);
guidNoBraces := 'kb:' + Copy(GuidToString(Uid), 2, Length(GuidToString(Uid)) - 2);
end
else
guidNoBraces := ExtractField(lbWarrant.Items[lbWarrant.ItemIndex], '"@id":"');
line := line + indent + '"@id":"' + guidNoBraces + '",' + recSep;
line := line + indent + '"@type":"uco-investigation:Authorization",' + recSep;
line := line + indent + '"facets":[' + recSep;
line := line + indent + '{' + recSep;
line := line + RepeatString(indent, 2) + '"uco-investigation:authorizationType":"' + edAuthorizationType.Text + '",' + recSep;
line := line + RepeatString(indent, 2) + '"uco-investigation:authorizationIdentifier":"' + edIdentifier.Text + '",' + recSep;
lineID := extractID(cbAuthority.Items[cbAuthority.ItemIndex]);
line := line + RepeatString(indent, 2) + '"uco-investigation:authorizationAuthority":{"@id":"' + lineID + '"},' + recSep;
line := line + RepeatString(indent, 2) + '"uco-investigation:authorizationIssuedDate":"' + cbYear.Items[cbYear.ItemIndex];
line := line + cbMonth.Items[cbMonth.ItemIndex];
line := line + cbDay.Items[cbDay.ItemIndex] + '"' + recSep;
line := line + indent + '}' + recSep;
line := line + indent + ']' + recSep + '}' + recSep;
Result := line;
end;
procedure TformWarrant.readIdentityFromFile(idValues: TStringList);
var
json, recSep, crlf: string;
sreader: TStringReader;
jreader: TJsonTextReader;
inName, inFamilyName, inID: Boolean;
name, familyName, idIdentity: string;
idIdentities: TStringList;
idx:integer;
begin
//dir := GetCurrentDir;
recSep := #30 + #30;
crlf := #13 + #10;
// read file JSON uuidCase-identity.json: fill in cbSourceIdentity component
if FileExists(FpathCase + FuuidCase + '-identity.json') then
begin
idIdentities := TStringList.Create;
idIdentities.LoadFromFile(FpathCase + FuuidCase + '-identity.json');
//JSON string here
json := stringreplace(idIdentities.Text, recSep, crlf,[rfReplaceAll]);
try
sreader := TStringReader.Create(json);
jreader := TJsonTextReader.Create(sreader);
while jreader.Read do
begin
if JsonTokenToString(jreader.TokenType) = 'PropertyName' then
begin
if jreader.Value.AsString = 'uco-identity:givenName' then
inName := True
else
inName := False;
if jreader.Value.AsString = 'uco-identity:familyName' then
inFamilyName := True
else
inFamilyName := False;
if jreader.Value.AsString = '@id' then
inID := True
else
inID := False;
end;
if JsonTokenToString(jreader.TokenType) = 'String' then
begin
if inID then begin
idIdentity := jreader.Value.AsString
end;
if inName then
name := jreader.Value.AsString;
if inFamilyName then
begin
familyName := jreader.Value.AsString;
for idx:=0 to idValues.Count - 1 do
if idValues[idx] = idIdentity then
cbAuthority.Items.Add(name + ' ' + familyName + '@' + idIdentity)
end;
end;
end;
finally
jreader.Free;
sreader.Free;
end;
end;
end;
function TformWarrant.readIdentityFromRelationshipFile(idRoles: TStringList): TStringList;
var
json, recSep, crlf: string;
sreader: TStringReader;
jreader: TJsonTextReader;
inSource, inTarget: Boolean;
source, target: string;
listRelationship, idIdentities: TStringList;
idx, nHypens:integer;
begin
//dir := GetCurrentDir;
recSep := #30 + #30;
crlf := #13 + #10;
idIdentities := TStringList.Create;
// read file JSON uuidCase-identity.json: fill in cbSourceIdentity component
if FileExists(FpathCase + FuuidCase + '-relationship.json') then
begin
listRelationship := TStringList.Create;
listRelationship.LoadFromFile(FpathCase + FuuidCase + '-relationship.json');
//JSON string here
json := stringreplace(listRelationship.Text, recSep, crlf,[rfReplaceAll]);
try
sreader := TStringReader.Create(json);
jreader := TJsonTextReader.Create(sreader);
inSource := False;
while jreader.Read do
begin
if JsonTokenToString(jreader.TokenType) = 'PropertyName' then
begin
if jreader.Value.AsString = 'uco-observable:source' then
inSource := True
else
if (jreader.Value.AsString <> '@id') then
inSource := False;
if jreader.Value.AsString = 'uco-observable:target' then
inTarget := True
else
if (jreader.Value.AsString <> '@id') then
inTarget := False;
end;
if JsonTokenToString(jreader.TokenType) = 'String' then
begin
if inSource then
source := jreader.Value.AsString;
if inTarget then
begin
target := jreader.Value.AsString;
for idx:=0 to idRoles.Count - 1 do
if idRoles[idx] = target then
idIdentities.Add(source);
inSource := False;
inTarget := False;
end;
end;
end;
finally
jreader.Free;
sreader.Free;
end;
end;
Result := idIdentities;
end;
function TformWarrant.readIdRoleFromFile: TSTringList;
var
json, recSep, crlf: string;
sreader: TStringReader;
jreader: TJsonTextReader;
inName, inID: Boolean;
id, name: string;
listRole, idRoles: TStringList;
idx:integer;
begin
//dir := GetCurrentDir;
idRoles := TStringList.Create;
recSep := #30 + #30;
crlf := #13 + #10;
// read file JSON uuidCase-identity.json: fill in cbSourceIdentity component
if FileExists(FpathCase + FuuidCase + '-role.json') then
begin
listRole := TStringList.Create;
listRole.LoadFromFile(FpathCase + FuuidCase + '-role.json');
//JSON string here
json := stringreplace(listRole.Text, recSep, crlf,[rfReplaceAll]);
try
sreader := TStringReader.Create(json);
jreader := TJsonTextReader.Create(sreader);
inName := False;
while jreader.Read do
begin
if JsonTokenToString(jreader.TokenType) = 'PropertyName' then
begin
if jreader.Value.AsString = 'uco-role:name' then
inName := True
else
inName := False;
if jreader.Value.AsString = '@id' then
inID := True
else
inID := False;
end;
if JsonTokenToString(jreader.TokenType) = 'String' then
begin
if inName then begin
name := jreader.Value.AsString;
if (name = 'Judge') or (name='Judicial Authority') then
//cbAuthority.Items.Add(name + ' ' + '@' + id);
idRoles.Add(id);
//if (name = 'Investigator') then
//cbApplicant.Items.Add(name + ' ' + '@' + id);
end;
if inID then
id := jreader.Value.AsString;
end;
end;
finally
jreader.Free;
sreader.Free;
end;
end;
Result := idRoles;
end;
procedure TformWarrant.btnCancelClick(Sender: TObject);
begin
formWarrant.Close;
end;
procedure TformWarrant.btnCloseClick(Sender: TObject);
var
fileJSON: TextFile;
line: String;
idx: Integer;
begin
if lbWarrant.Items.Count > 0 then
begin
//dir := GetCurrentDir;
idx := 0;
AssignFile(fileJSON, FpathCase + FuuidCase + '-warrant.json');
Rewrite(fileJSON); // create new file
WriteLn(fileJSON, '{');
line := #9 + '"OBJECTS_WARRANT":[';
WriteLn(fileJSON, line);
for idx:= 0 to lbWarrant.Items.Count - 2 do
WriteLn(fileJSON, #9#9 + lbWarrant.Items[idx] + ',');
WriteLn(fileJSON, #9#9 + lbWarrant.Items[idx]);
WriteLn(fileJSON, #9#9 + ']');
Write(fileJSON,'}');
CloseFile(fileJSON);
end
else
deleteFile(FpathCase + FuuidCase + '-warrant.json');
formWarrant.Close;
end;
procedure TformWarrant.btnModifyClick(Sender: TObject);
begin
if lbWarrant.ItemIndex > - 1 then
lbWarrant.Items[lbWarrant.ItemIndex] := prepareItemWarrant('modify');
end;
procedure TformWarrant.btnAddWarrantClick(Sender: TObject);
begin
if (cbAuthority.Items.Count = 0) or (edAuthorizationType.Text = '') then
ShowMessage('Authority and/or AuthorizationType are empty')
else
begin
lbWarrant.Items.Add(prepareItemWarrant('add'));
cbAuthority.ItemIndex := -1;
cbDay.ItemIndex := -1;
cbMonth.ItemIndex := -1;
cbYear.ItemIndex := -1;
end;
end;
procedure TformWarrant.SetpathCase(const Value: String);
begin
FpathCase := Value;
end;
procedure TformWarrant.SetuuidCase(const Value: string);
begin
FuuidCase := Value;
end;
procedure TformWarrant.ShowWithParamater(pathCase: String; uuidCase: String);
var
fileJSON: TextFile;
line, subLine: String;
begin
SetUuidCase(uuidCase);
SetPathCase(pathCase);
//dir := GetCurrentDir;
// read file JSON uuidCase-warrant.json
if FileExists(FpathCase + FuuidCase + '-warrant.json') then
begin
AssignFile(fileJSON, Fpathcase + FuuidCase + '-warrant.json');
Reset(fileJSON);
lbWarrant.Items.Clear;
while not Eof(fileJSON) do
begin
ReadLn(fileJSON, line);
line := Trim(line);
if (line = '{') or (line = '}') or (line = ']') or (AnsiContainsStr(line, 'OBJECTS_')) then // first or last line or root element
else
begin
subLine := Copy(line, Length(line), 1); // rule out of comma
if subLine = ',' then
line := Copy(line, 1, Length(line) - 1);
lbWarrant.Items.Add(line);
end;
end;
CloseFile(fileJSON);
end;
// else
// ShowMessage(dir + uuidCase + '-identity.json' + ' doesn''t exist');
formWarrant.ShowModal;
end;
end.