-
Notifications
You must be signed in to change notification settings - Fork 0
/
caseGenerator_trace_chat.pas
1077 lines (952 loc) · 33.5 KB
/
caseGenerator_trace_chat.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit caseGenerator_trace_chat;
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, FMX.ScrollBox, FMX.Memo, caseGenerator_util,
FMX.Memo.Types;
type
TformTraceChat = class(TForm)
Label1: TLabel;
lbMessages: TListBox;
btnClose: TButton;
btnAddMessage: TButton;
btnRemoveMessage: TButton;
Label6: TLabel;
panelFrom: TPanel;
Label5: TLabel;
Label8: TLabel;
panelTo: TPanel;
cbAccountTo: TComboBox;
Label11: TLabel;
Label13: TLabel;
memoMessageText: TMemo;
btnAddAccountTo: TButton;
btnRemoveAccountTo: TButton;
lbAccountTo: TListBox;
cbSentMonth: TComboBox;
timeSent: TTimeEdit;
cbSentYear: TComboBox;
Label3: TLabel;
btnCancel: TButton;
btnModifyMessage: TButton;
cbSentDay: TComboBox;
Label2: TLabel;
Label4: TLabel;
cbMessageType: TComboBox;
edNewChatName: TEdit;
Label9: TLabel;
cbChatName: TComboBox;
btnNewChatName: TButton;
edNumMessages: TEdit;
cbAppName: TComboBox;
Label7: TLabel;
cbAccountFrom: TComboBox;
procedure btnAddMessageClick(Sender: TObject);
procedure btnRemoveMessageClick(Sender: TObject);
procedure btnCloseClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure btnAddAccountToClick(Sender: TObject);
procedure btnRemoveAccountToClick(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
procedure btnModifyMessageClick(Sender: TObject);
procedure lbAccountToChange(Sender: TObject);
procedure btnNewChatNameClick(Sender: TObject);
procedure cbChatNameChange(Sender: TObject);
procedure lbMessagesClick(Sender: TObject);
private
FuuidCase: string;
FpathCase: String;
FindexChat: Integer;
procedure SetuuidCase(const Value: string);
procedure SetpathCase(const Value: String);
procedure SetindexChat(const Value: Integer);
property uuidCase: string read FuuidCase write SetuuidCase;
property pathCase: String read FpathCase write SetpathCase;
property indexChat: Integer read FindexChat write SetindexChat;
function JsonTokenToString(const t: TJsonToken): string;
procedure readTraceFromFile;
procedure readTraceApplicationAccountFromFile;
procedure readTraceChatFromFile;
procedure readTraceChatMessagesFromFile;
procedure readTraceChatMessagesFromFileOLD;
procedure readTraceApplicationFromFile;
function extractID(line, charId: String): String;
function extractLastID(line: String): String;
function prepareItemMessage(operation: String): String;
{ Private declarations }
public
procedure ShowWithParamater(pathCase: String; uuidCase: String);
{ Public declarations }
end;
var
formTraceChat: TformTraceChat;
implementation
{$R *.fmx}
uses StrUtils;
type TChatMessage = record
id: String;
text: string;
appId: string;
dateTime: string;
fromId: string;
toId: string;
status: string;
outcome: string;
kind: string;
end;
const MAX_CHATS = 20;
const MAX_MSGS = 20;
type
// max 30 messages per Chat
TChatArray = array [1..MAX_CHATS] of String;
var
//max 20 Chats
chatMatrix : array[1..MAX_MSGS] of TChatArray;
{ TForm1 }
procedure TformTraceChat.btnRemoveMessageClick(Sender: TObject);
var
nMsg, idx: Integer;
begin
if lbMessages.ItemIndex > -1 then
begin
{ for idx := lbMessage.ItemIndex + 1 to MAX_MSGS do
chatMatrix[cbChatName.ItemIndex + 1][idx + 1] :=
chatMatrix[cbChatName.ItemIndex + 1][idx];
chatMatrix[cbChatName.ItemIndex + 1][MAX_MSGS] := '';
}
lbMessages.Items.Delete(lbMessages.ItemIndex);
nMsg := StrToInt(edNumMessages.Text);
Dec(nMsg);
edNumMessages.Text := IntToStr(nMsg);
end;
end;
procedure TformTraceChat.btnRemoveAccountToClick(Sender: TObject);
begin
lbAccountTo.Items.Delete(lbAccountTo.ItemIndex);
end;
procedure TformTraceChat.btnNewChatNameClick(Sender: TObject);
var
Uid: TGUID;
guidNoBraces: String;
begin
if (Trim(edNewChatName.Text) <> '') then
begin
CreateGUID(Uid);
guidNoBraces := 'kb:' + Copy(GuidToString(Uid), 2, Length(GuidToString(Uid)) - 2);
cbChatName.Items.Add(edNewChatName.Text + StringofChar(' ', 100) + '@' + guidNoBraces);
cbChatName.ItemIndex := cbChatName.Count - 1;
edNumMessages.Text := '0';
edNewChatName.Text := '';
end;
end;
procedure TformTraceChat.cbChatNameChange(Sender: TObject);
var
idx: Integer;
begin
if cbChatName.ItemIndex > -1 then
begin
// if the previous Index was linked to a Chat, I'll save the relative
// messages in the chatMatrix
if FIndexChat > - 1 then
begin
for idx := 0 to lbMessages.Count - 1 do
chatMatrix[FIndexChat + 1][idx + 1] := lbMessages.Items[idx];
for idx:= lbMessages.Count to MAX_MSGS do
chatMatrix[FIndexChat + 1][idx] := '';
end;
SetIndexChat(cbChatName.ItemIndex);
lbMessages.Items.Clear;
for idx:= 1 to Length(chatMatrix[cbChatName.ItemIndex + 1]) do
begin
if chatMatrix[cbChatName.ItemIndex + 1][idx] = '' then
break;
lbMessages.Items.Add(chatMatrix[cbChatName.ItemIndex + 1][idx]);
end;
edNumMessages.Text := IntToStr(lbMessages.Items.Count);
end;
end;
function TformTraceChat.extractID(line, charId: String): String;
begin
Result := Copy(line, Pos(charId, line) + Length(charId), Length(line));
end;
function TformTraceChat.extractLastID(line: String): String;
begin
Result := Copy(line, LastDelimiter('@', line) + 1, Length(line));
end;
procedure TformTraceChat.FormShow(Sender: TObject);
var
idx, idy: Integer;
begin
idy := 0;
for idx := 2020 to 2030 do
begin
cbSentYear.Items.Add(IntToStr(idx));
if (IntToStr(CurrentYear) = IntToStr(idx)) then
cbSentYear.ItemIndex := idy;
Inc(idy);
end;
cbAppName.Items.Clear;
memoMessageText.Text := '';
edNewChatName.Text := '';
cbSentDay.ItemIndex := 0;
cbSentMonth.ItemIndex := 0;
cbAccountFrom.ItemIndex := -1;
cbAccountTo.ItemIndex := -1;
lbAccountTo.Items.Clear;
cbChatName.ItemIndex := - 1;
SetindexChat(cbChatName.ItemIndex);
readTraceFromFile;
end;
function TformTraceChat.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 TformTraceChat.lbMessagesClick(Sender: TObject);
var
line, field, uuidTo, sentDate, sDay, sMonth, sYear, sDate, messageType: String;
idx, posTo : Integer;
begin
if lbMessages.ItemIndex > - 1 then
begin
line := lbMessages.Items[lbMessages.ItemIndex];
field := ExtractRefID(line, '"uco-observable:application":"');
for idx:=0 to cbAppName.Items.Count - 1 do
if Pos(field, cbAppName.Items[idx]) > 0 then
begin
cbAppName.ItemIndex := idx;
break;
end;
field := ExtractField(line, '"uco-observable:messageType":"');
for idx := 0 to cbMessageType.Count - 1 do
begin
if cbMessageType.Items[idx] = field then
cbMessageType.ItemIndex := idx;
end;
memoMessageText.Lines.Text := ExtractField(line, '"uco-observable:messageText":"');
field := ExtractRefId(line, '"uco-observable:from":{');
for idx:=0 to cbAccountFrom.Items.Count - 1 do
begin
if AnsiContainsStr(cbAccountFrom.Items[idx], field) then
begin
cbAccountFrom.ItemIndex := idx;
break;
end;
end;
//accountTo := TStringList.Create;
field := ExtractRefId(line, '"uco-observable:to":[');
{ accountTo := ExtractArray(lbMessages.Items[lbMessages.ItemIndex], '"uco-observable:to":[');
if accountTo.Count > 0 then
begin
for idx:=0 to accountTo.Count - 1 do
lbAccountTo.Items.Add('"' + accountTo[idx] + '"');
lbAccountTo.ItemIndex := 0;
end;
}
lbAccountTo.Items.Clear;
for idx:=0 to cbAccountTo.Count - 1 do
if Pos(field, cbAccountTo.Items[idx]) > 0 then
begin
cbAccountTo.ItemIndex := idx;
break;
end;
lbAccountTo.Items.Add(cbAccountTo.Items[cbAccountTo.ItemIndex]);
sentDate := ExtractDateValue(lbMessages.Items[lbMessages.ItemIndex], '"uco-observable:sentTime":{');
sDate := Copy(sentDate, 1, 10);
sDay := Copy(sDate, 7, 2);
for idx:=0 to cbSentDay.Items.Count - 1 do
begin
if cbSentDay.Items[idx] = sDay then
begin
cbSentDay.ItemIndex := idx;
break;
end;
end;
sMonth := Copy(sDate, 5, 2);
for idx:=0 to cbSentMonth.Items.Count - 1 do
begin
if cbSentMonth.Items[idx] = sMonth then
begin
cbSentMonth.ItemIndex := idx;
break;
end;
end;
sYear := Copy(sDate, 1, 4);
for idx:=0 to cbSentYear.Items.Count - 1 do
begin
if cbSentYear.Items[idx] = sYear then
begin
cbSentYear.ItemIndex := idx;
break;
end;
end;
timeSent.Text := Copy(sentDate, 10, 8);
field := ExtractField(lbMessages.Items[lbMessages.ItemIndex], '"uco-observable:messageType":"');
for idx:=0 to cbMessageType.Items.Count - 1 do
begin
if cbMessageType.Items[idx] = field then
begin
cbMessageType.ItemIndex := idx;
break;
end;
end;
end;
end;
procedure TformTraceChat.lbAccountToChange(Sender: TObject);
//var
//line: String;
//idx: Integer;
begin
{
if lbAccount.ItemIndex > - 1 then
begin
line := lbMobile.Items[lbMobile.ItemIndex];
line := stringreplace(line, '"', '',[rfReplaceAll]);
for idx:=0 to cbMobileTo.Count - 1 do
begin
if AnsiContainsStr(cbMobileTo.Items[idx], line) then
begin
cbMobileTo.ItemIndex := idx;
break
end;
end;
end;
}
end;
function TformTraceChat.prepareItemMessage(operation: String): String;
var
line, recSep, indent, guidNoBraces, sId, sValue: string;
Uid: TGUID;
idx: Integer;
begin
idx:= 0;
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(lbMessages.Items[lbMessages.ItemIndex], '"@id":"');
line := line + indent + '"@id":"' + guidNoBraces + '",' + recSep;
line := line + indent + '"@type":"uco-observable:CyberItem",' + recSep;
line := line + indent + '"uco-core:facets":[' + recSep;
line := line + indent + '{' + recSep;
line := line + RepeatString(indent, 2) + '"@type":"uco-observable:Message",' + recSep;
line := line + RepeatString(indent, 2) + '"uco-observable:messageText":"' + memoMessageText.Text + '", ' + recSep;
sId := extractID(cbAppName.Items[cbAppName.ItemIndex], '@');
line := line + RepeatString(indent, 2) + '"uco-observable:application":' + recSep;
line := line + RepeatString(indent, 3) + '{' + recSep;
line := line + RepeatString(indent, 3) + '"@id":"' + sId + '"' + recSep;
line := line + RepeatString(indent, 3) + '},' + recSep;
line := line + '"uco-observable:sentTime":{' + recSep;
line := line + '"@type":"xsd:dateTime",' + recSep;
line := line + '"@value":"' + cbSentYear.Items[cbSentYear.ItemIndex];
line := line + cbSentMonth.Items[cbSentMonth.ItemIndex];
line := line + cbSentDay.Items[cbSentDay.ItemIndex] + 'T' + timeSent.Text + '"},' + recSep;
sId := ExtractID(cbAccountFrom.Items[cbAccountFrom.ItemIndex], '@id');
line := line + RepeatString(indent, 2) + '"uco-observable:from":{"' + sId + '"}, ' + recSep;
sId := ExtractID(cbAccountTo.Items[cbAccountFrom.ItemIndex], '@id');
line := line + RepeatString(indent, 2) + '"uco-observable:to":[' + recSep;
idx := 0;
for idx:=0 to lbAccountTo.Items.Count - 2 do
begin
sId := ExtractID(lbAccountTo.Items[idx], '@id');
line := line + RepeatString(indent, 2) + '{"@id":"' + sId + '"},';
end;
sId := ExtractID(lbAccountTo.Items[idx], '@id');
line := line + RepeatString(indent, 2) + '{"@id":"' + sId + '"}],' + recSep;
line := line + indent + '"uco-observable:allocationStatus":"Intact",' + recSep;
line := line + indent + '"uco-observable:drafting:outcome":"",' + recSep;
sValue := cbMessageType.Items[cbMessageType.ItemIndex];
line := line + indent + '"uco-observable:messageType":"' + sValue + '"' + recSep;
line := line + indent + '}]' + recSep + '}' + recSep;
Result := line;
end;
procedure TformTraceChat.readTraceFromFile;
begin
readTraceApplicationFromFile;
readTraceApplicationAccountFromFile;
// readTraceChatFromFile; traceCHAT is read in ShowWithParameter method
readTraceChatMessagesFromFile;
end;
procedure TformTraceChat.readTraceApplicationAccountFromFile;
var
json, recSep, crlf: string;
sreader: TStringReader;
jreader: TJsonTextReader;
inID, inIssuer, inIdentifier, inDigitalAccount, firstId: Boolean;
id, issuer, identifier, digitalAccount: string;
listTrace: TStringList;
idx, nHypens: integer;
begin
//dir := GetCurrentDir;
recSep := #30 + #30;
crlf := #13 + #10;
firstId := false;
// read file JSON uuidCase-identity.json: fill in cbSourceIdentity component
if FileExists(FpathCase + FuuidCase + '-traceAPPLICATION_ACCOUNT.json') then
begin
listTrace := TStringList.Create;
listTrace.LoadFromFile(FpathCase + FuuidCase + '-traceAPPLICATION_ACCOUNT.json');
//JSON string here
json := stringreplace(listTrace.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 = '@id' then
begin
if not firstId then
begin
inID := True;
firstId := true
end
else
inID := False;
end
else
inID := false;
if jreader.Value.AsString = 'uco-observable:accountIssuer' then
inIssuer := True
else
inIssuer := False;
if jreader.Value.AsString = 'uco-observable:applicationIdentifier' then
inIdentifier := True
else
inIdentifier := False;
if jreader.Value.AsString = 'uco-observable:displayName' then
inDigitalAccount := True
else
inDigitalAccount := False;
end;
if JsonTokenToString(jreader.TokenType) = 'String' then
begin
if inID then
id := jreader.Value.AsString;
if inIssuer then
issuer := jreader.Value.AsString;
if inIdentifier then
identifier := jreader.Value.AsString;
if inDigitalAccount then
begin
digitalAccount := jreader.Value.AsString;
nHypens := CountOccurrences('-', id);
(*--- if nHypens > 4 then it is the case of id related to @type inside an Object,
for instance for Identity it can be @id:"...-...-SimpleName" ---*)
if nHypens > 4 then
id := Copy(id, 1, LastDelimiter('-', id) - 1);
cbAccountFrom.Items.Add(issuer + ' ' + identifier + ' ' + digitalAccount + '@id' + id);
cbAccountTo.Items.Add(issuer + ' ' + identifier + ' ' + digitalAccount + '@id' + id);
firstId := false;
id := '';
end;
end;
end;
finally
jreader.Free;
sreader.Free;
end;
end;
end;
procedure TformTraceChat.readTraceApplicationFromFile
;
var
json, recSep, crlf: string;
sreader: TStringReader;
jreader: TJsonTextReader;
inID, inAppName: Boolean;
id, appName: string;
listTrace: TStringList;
idx, nHypens: Integer;
begin
//dir := GetCurrentDir;
recSep := #30 + #30;
crlf := #13 + #10;
// read file JSON uuidCase-identity.json: fill in cbSourceIdentity component
if FileExists(FpathCase + FuuidCase + '-traceAPPLICATION.json') then
begin
listTrace := TStringList.Create;
listTrace.LoadFromFile(FpathCase + FuuidCase + '-traceAPPLICATION.json');
//JSON string here
json := stringreplace(listTrace.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 = '@id' then
inID := True
else
inID := False;
if jreader.Value.AsString = 'uco-core:name' then
inAppName := True
else
inAppName := False;
end;
if JsonTokenToString(jreader.TokenType) = 'String' then
begin
if inID then
id := jreader.Value.AsString;
if inAppName then
begin
nHypens := CountOccurrences('-', id);
(*--- if nHypens > 4 then it is the case of id related to @type inside an Object,
for instance for Identity it can be @id:"...-...-SimpleName" ---*)
if nHypens > 4 then
id := Copy(id, 1, LastDelimiter('-', id) - 1);
appName := jreader.Value.AsString;
cbAppName.Items.Add(appName + StringofChar(' ', 100) + '@' + id);
end;
end;
end;
finally
jreader.Free;
sreader.Free;
end;
end;
end;
procedure TformTraceChat.readTraceChatFromFile;
var
json, recSep, crlf: string;
sreader: TStringReader;
jreader: TJsonTextReader;
inChatName, inChatNumMsg, inID: Boolean;
chatName, chatNumMsg, id: string;
listTrace: TStringList;
idx, nHypens: integer;
begin
//dir := GetCurrentDir;
recSep := #30 + #30;
crlf := #13 + #10;
// read file JSON uuidCase-identity.json: fill in cbSourceIdentity component
if FileExists(FpathCase + FuuidCase + '-traceCHAT.json') then
begin
listTrace := TStringList.Create;
listTrace.LoadFromFile(FpathCase + FuuidCase + '-traceCHAT.json');
//JSON string here
json := stringreplace(listTrace.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-observable:displayName' then
inChatName := True
else
inChatName := False;
if jreader.Value.AsString = 'olo:length' then
inChatNumMsg := True
else
inChatNumMsg := False;
if jreader.Value.AsString = '@id' then
inID := True
else
inID := False;
end;
if JsonTokenToString(jreader.TokenType) = 'String' then
begin
if inID then
id := jreader.Value.AsString;
if inChatName then
chatName := jreader.Value.AsString;
if inChatNumMsg then
begin
chatNumMsg := jreader.Value.AsString;
nHypens := CountOccurrences('-', id);
(*--- if nHypens > 4 then it is the case of id related to @type inside an Object,
for instance for Identity it can be @id:"...-...-SimpleName" ---*)
if nHypens > 4 then
id := Copy(id, 1, LastDelimiter('-', id) - 1);
cbChatName.Items.Add(chatName + stringOfChar(' ', 30)+ ' @' + id);
end;
end;
end;
finally
jreader.Free;
sreader.Free;
end;
end;
end;
procedure TformTraceChat.readTraceChatMessagesFromFile;
var
listMessages: TStringList;
i, j, nMsg: integer;
chatFile, chatNum, line: String;
begin
for i := 1 to cbChatName.Count do
begin
if i < 10 then
chatNum := '0' + IntToStr(i)
else
chatNum := IntToStr(i);
chatFile := FpathCase + FuuidCase + '-traceCHAT_MESSAGES_' + chatNum + '.json';
if FileExists(chatFile) then
begin
listMessages := TStringList.Create;
listMessages.LoadFromFile(chatFile);
// iterate over messages of the Chat n. i
nMsg := 0;
for j:=0 to listMessages.Count - 1 do
begin
line := listMessages[j];
if (line = '{') or (line = '}') or (line = ']') or (AnsiContainsStr(line, 'OBJECTS_')) then // first or last line or root element
continue;
Inc(nMsg);
chatMatrix[i][nMsg] := listMessages[j];
end;
end;
end;
{
if cbChatName.Count > 0 then
for i := 1 to Length(chatMatrix[1]) do
lbMessages.Items.Add(chatMatrix[1][i]);
}
end;
procedure TformTraceChat.readTraceChatMessagesFromFileOLD;
var
json, recSep, crlf: string;
sreader: TStringReader;
jreader: TJsonTextReader;
inChatText, inChatAppId, inChat, inChatDateTime, inChatFromId: Boolean;
inChatToId, inChatKind, inChatStatus, inChatOutcome, inId: Boolean;
chatText, chatAppId, chatDateTime, chatFromId, chatToId: String;
chatKind, chatStatus, chatOutcome, id, sChat: string;
listTrace: TStringList;
i, idx, nHypens, numMessage: integer;
begin
//dir := GetCurrentDir;
recSep := #30 + #30;
crlf := #13 + #10;
// read all files JSON related to chat messages of each Chat,
// each file corresponds to the messages of a Chat
for i:=0 to cbChatName.Count - 1 do
begin
if i < 10 then
sChat := '0' + IntToStr(i)
else
sChat := IntToSTr(i);
if FileExists(FpathCase + FuuidCase + '-traceCHAT_MESSAGES_' + sChat + '.json') then
begin
listTrace := TStringList.Create;
listTrace.LoadFromFile(FpathCase + FuuidCase + '-traceCHAT_MESSAGES_' + sChat + '.json');
numMessage := 0;
//JSON string here
json := stringreplace(listTrace.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-observable:messageText' then
inChatText := True
else
inChatText := False;
if jreader.Value.AsString = 'uco-observable:application' then
inChatAppId := True
else
inChatAppId := False;
if jreader.Value.AsString = '@value' then
inChatDateTime := True
else
inChatDateTime := False;
if jreader.Value.AsString = 'uco-observable:from' then
inChatFromId := True
else
inChatFromId := False;
if jreader.Value.AsString = 'uco-observable:to' then
inChatToId := True
else
inChatToId := False;
if jreader.Value.AsString = 'uco-observable:allocationStatus' then
inChatStatus := True
else
inChatStatus := False;
if jreader.Value.AsString = 'uco-observable:messageType' then
inChatKind := True
else
inChatKind := False;
if jreader.Value.AsString = '@id' then
inID := True
else
inID := False;
end;
if JsonTokenToString(jreader.TokenType) = 'String' then
begin
if inID then
id := jreader.Value.AsString;
if inChatText then
chatText := jreader.Value.AsString;
if inChatAppId then
chatAppId := jreader.Value.AsString;
if inChatDateTime then
chatDateTime := jreader.Value.AsString;
if inChatFromId then
chatFromId := jreader.Value.AsString;
if inChatToId then
chatToId := jreader.Value.AsString;
if inChatStatus then
chatStatus := jreader.Value.AsString;
if inChatKind then
begin
chatKind := jreader.Value.AsString;
nHypens := CountOccurrences('-', id);
(*--- if nHypens > 4 then it is the case of id related to @type inside an Object,
for instance for Identity it can be @id:"...-...-SimpleName" ---*)
if nHypens > 4 then
id := Copy(id, 1, LastDelimiter('-', id) - 1);
chatMatrix[i][numMessage] := chatText + ' ' + chatAppId +
chatDatetime + chatFromId + chatToId + chatStatus + chatKind + '@' + id;
Inc(numMessage);
end;
end;
end;
finally
jreader.Free;
sreader.Free;
end;
end;
end;
if cbChatName.Count > 0 then
begin
for i:=0 to length(chatMatrix[1]) do
lbMessages.Items.Add(chatMatrix[1][i+1]);
end;
end;
procedure TformTraceChat.btnAddAccountToClick(Sender: TObject);
var
line: String;
begin
if cbAccountFrom.ItemIndex = cbAccountTo.ItemIndex then
if cbAccountFrom.ItemIndex > -1 then
ShowMessage('Application account FROM and TO are equal')
else
begin
line := cbAccountTo.Items[cbAccountTo.ItemIndex];
lbAccountTo.Items.Add(line);
end;
end;
procedure TformTraceChat.btnCancelClick(Sender: TObject);
begin
formTraceChat.Close;
end;
procedure TformTraceChat.btnCloseClick(Sender: TObject);
var
fileJSON: TextFile;
line, chatThreadId, chatThreadName, chatId, recSep, crlf: String;
chatFromId, chatToId, chatNum, chatMessagesFile: String;
idx, idy, idz, nMsgs, idPos: integer;
begin
// save current Chat messages into chatMatrix
for idx:=0 to lbMessages.Count - 1 do
begin
chatMatrix[cbChatName.ItemIndex + 1][idx + 1] :=
lbMessages.Items[idx];
end;
recSep := #30 + #30;
crlf := #13 + #10;
AssignFile(fileJSON, FpathCase + FuuidCase + '-traceCHAT.json', CP_UTF8);
Rewrite(fileJSON); // create new file
WriteLn(fileJSON, '{');
line := #9 + '"OBJECTS_CHAT":[';
WriteLn(fileJSON, line);
if cbChatName.Items.Count > 0 then
begin
idx := 0;
for idx:=0 to cbChatName.Count -1 do
begin
idPos := Pos('@', cbChatName.Items[idx]);
chatThreadName := Trim(Copy(cbChatName.Items[idx], 1, idPos - 1));
chatThreadId := Copy(cbChatName.Items[idx], idPos + 1, Length(cbChatName.Items[idx]));
line := '{"@id":"' + chatThreadId + '",' + recSep;
line := line + '"@type":"uco-observable:CyberItem", ' + recSep;
line := line + '"uco-core:facets":[' + recSep;
line := line + stringOfChar(#9, 2) + '{' + recSep;
line := line + stringOfChar(#9, 2) + '"@type":"uco-observable:MessageThread",' + recSep;
line := line + stringOfChar(#9, 2) + '"uco-observable:displayName":"' + chatThreadName + '",' + recSep;
line := line + stringOfChar(#9, 2) + '"uco-observable:messages":{' + recSep;
nMsgs := 0;
for idz:= 1 to Length(chatMatrix[idx + 1]) do
begin
if chatMatrix[idx + 1][idz] <> '' then
Inc(nMsgs)
else
break;
end;
line := line + stringOfChar(#9, 2) + '"olo:length":"' +
IntToStr(nMsgs) + '",' + recSep;
line := line + stringOfChar(#9, 2) + '"olo:slot":[' + recSep;
for idy:=1 to Length(chatMatrix[idx + 1]) do
begin
if chatMatrix[idx + 1][idy] = '' then
break;
line := line + stringOfChar(#9, 3) + '{' + recSep;
line := line + stringOfChar(#9, 3) + '"olo:index":"' + IntToStr(idy) + '",' + recSep;
line := line + stringOfChar(#9, 3) + '"olo:item": {' + recSep;
chatId := ExtractField(chatMatrix[idx + 1][idy], '@id":"');
line := line + stringOfChar(#9, 4) + '"@id":"' + chatId + '"}'+ recSep;
line := line + stringOfChar(#9, 3) + '},' + recSep;
end;
// get rid of last comma
line := Copy(line, 1, Length(line) - 3) + recSep;
line := line + stringOfChar(#9, 2) + ']' + recSep;
line := line + #9 + '},' + recSep;
line := line + '"uco-observable:participants":[' + recSep;
chatFromId := ExtractField(chatMatrix[idx +1][1], '"uco-observable:from":{"');
chatToId := ExtractField(chatMatrix[idx +1][1], '"uco-observable:to":[' + recSep + '{"');
line := line + '{"@id":"' + chatFromId + '"},' + recSep;
line := line + '{"@id":"' + chatToId + '"}]' + recSep;
line := line + stringOfChar(#9, 3) + '}' + recSep;
line := line + stringOfChar(#9, 2) + ']' + recSep;
line := line + #9 + '}' + recSep;
WriteLn(fileJSON, UTF8Encode(line));
end;
CloseFile(fileJSON);
end
else
deleteFile(FpathCase + FuuidCase + '-traceCHAT.json');
// write traceCHAT_MESSAGES_xx files
if cbChatName.Items.Count > 0 then
begin
for idx:=1 to cbChatName.Count do
begin
if idx < 10 then
chatNum := '0' + IntToStr(idx)
else
chatNum := IntToStr(idx);
chatMessagesFile := FpathCase + FuuidCase + '-traceCHAT_MESSAGES_' + chatNum + '.json';
AssignFile(fileJSON, chatMessagesFile, CP_UTF8);
Rewrite(fileJSON); // create new file
WriteLn(fileJSON, '{');
line := #9 + '"OBJECTS_CHAT_MESSAGES":[';
WriteLn(fileJSON, line);
for idy := 1 to Length(chatMatrix[idx]) do
begin
if chatMatrix[idx][idy] = '' then
break;
WriteLn(fileJSON, chatMatrix[idx][idy]);
end;
CloseFile(fileJSON);
end;
end;
formTraceChat.Close;
end;
procedure TformTraceChat.btnModifyMessageClick(Sender: TObject);
begin
if lbMessages.ItemIndex > - 1 then
lbMessages.Items[lbMessages.ItemIndex] := prepareItemMessage('modify');
end;
procedure TformTraceChat.btnAddMessageClick(Sender: TObject);
var
line, recSep, idLine: string;
Uid: TGUID;
idx: Integer;
begin
if (lbAccountTo.Items.Count = -1) or (cbAccountFrom.ItemIndex = -1) then
begin
ShowMessage('Mobile FROM or/and Mobile Source empty!');