-
Notifications
You must be signed in to change notification settings - Fork 0
/
caseGenerator_trace_application_account.pas
338 lines (299 loc) · 10.8 KB
/
caseGenerator_trace_application_account.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
unit caseGenerator_trace_application_account;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
System.JSON, System.JSON.Types, System.JSON.Readers,
FMX.DateTimeCtrls, FMX.Calendar, FMX.Edit, FMX.StdCtrls, FMX.Layouts,
FMX.ListBox, FMX.Controls.Presentation, caseGenerator_util;
type
TformTraceApplicationAccount = class(TForm)
Label1: TLabel;
lbAppAccount: TListBox;
btnClose: TButton;
btnAddTrace: TButton;
ù: TButton;
btnCancel: TButton;
btnModifyTrace: TButton;
Label2: TLabel;
edAppName: TEdit;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
edAppDisplayName: TEdit;
Label6: TLabel;
edAccountIdentifier: TEdit;
cbAppID: TComboBox;
procedure btnAddTraceClick(Sender: TObject);
procedure ùClick(Sender: TObject);
procedure btnCloseClick(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
procedure btnModifyTraceClick(Sender: TObject);
procedure lbAppAccountChange(Sender: TObject);
procedure FormShow(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 prepareTrace(operation: String): String;
procedure readTraceApplication;
function JsonTokenToString(const t: TJsonToken): string;
{ Private declarations }
public
procedure ShowWithParamater(pathCase: String; uuidCase: String);
{ Public declarations }
end;
var
formTraceApplicationAccount: TformTraceApplicationAccount;
implementation
{$R *.fmx}
uses StrUtils;
{ TForm1 }
procedure TformTraceApplicationAccount.ùClick(Sender: TObject);
begin
lbAppAccount.Items.Delete(lbAppAccount.ItemIndex);
end;
procedure TformTraceApplicationAccount.btnModifyTraceClick(Sender: TObject);
begin
if lbAppAccount.ItemIndex > - 1 then
lbAppAccount.Items[lbAppAccount.ItemIndex] := prepareTrace('modify');
end;
procedure TformTraceApplicationAccount.FormShow(Sender: TObject);
begin
cbAppId.Items.Clear;
readTraceApplication;
end;
function TformTraceApplicationAccount.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 TformTraceApplicationAccount.lbAppAccountChange(Sender: TObject);
var
line: String;
appID: String;
i: Integer;
begin
if lbAppAccount.ItemIndex > - 1 then
begin
line := lbAppAccount.Items[lbAppAccount.ItemIndex];
appID := ExtractField(line, '"uco-observable:accountID":"');
for i := 0 to cbAppId.Count - 1 do
begin
if cbAppId.Items[i] = appID then
begin
cbAppId.ItemIndex := i;
break;
end;
end;
edAppName.Text := ExtractField(line, '"uco-core:name":"');
edAccountIdentifier.Text := ExtractField(line, '"uco-observable:accountIdentifier":"');
edAppDisplayName.Text:= ExtractField(line, '"uco-observable:displayName":"');
end;
end;
function TformTraceApplicationAccount.prepareTrace(operation: String): String;
var
line, recSep, indent, guidNoBraces, idApp: string;
Uid: TGUID;
idx, posId: Integer;
begin
recSep := #30 + #30; // record separator, not printable
indent := ' ';
if (cbAppID.ItemIndex = - 1) then
ShowMessage('Application ID is missing!')
else
begin
line := '{' + recSep;
if operation = 'add' then
begin
CreateGUID(Uid);
guidNoBraces := 'kb:' + Copy(GuidToString(Uid), 2, Length(GuidToString(Uid)) - 2);
end
else
guidNoBraces := ExtractField(lbAppAccount.Items[lbAppAccount.ItemIndex], '"@id":"');
line := line + indent + '"@id":"' + guidNoBraces + '", ' + recSep;
line := line + indent + '"@type":"uco-observable:CyberItem", ' + recSep;
line := line + indent + '"uco-core:name":"' + edAppName.Text + '",' + recSep;
line := line + indent + '"uco-core:facets":[' + recSep;
line := line + indent + '{' + recSep;
line := line + RepeatString(indent, 2) + '"@type":"uco-observable:Account", ' + recSep;
line := line + RepeatString(indent, 2) + '"uco-observable:accountIssuer":"' + edAppName.Text + '", ' + recSep;
line := line + RepeatString(indent, 2) + '"uco-observable:accountIdentifier":"' + edAccountIdentifier.Text + '", ' + recSep;
line := line + RepeatString(indent, 2) + '"uco-observable:isActive":"true"' + recSep ;
line := line + indent + '},' + recSep;
line := line + indent + '{' + recSep;
line := line + RepeatString(indent, 2) + '"@type":"uco-observable:ApplicationAccount", ' + recSep;
line := line + RepeatString(indent, 2) + '"uco-observable:application":{"@id":"';
idApp := cbAppId.Items[cbAppId.ItemIndex];
posId := Pos('@', idApp);
idApp := Copy(idApp, posId + 1, Length(idApp));
line := line + idApp + '"}' + recSep;
line := line + indent + '},' + recSep;
line := line + indent + '{' + recSep;
line := line + RepeatString(indent, 2) + '"@type":"uco-observable:DigitalAccount", ' + recSep;
line := line + RepeatString(indent, 2) + '"uco-observable:displayName":"' +
edAppDisplayName.Text + '"' + recSep;
line := line + indent + '}' + recSep;
line := line + indent + ']' + recSep + '}';
end;
Result := line;
end;
procedure TformTraceApplicationAccount.readTraceApplication;
var
json, recSep, crlf: string;
sreader: TStringReader;
jreader: TJsonTextReader;
inAppID, inAppName: Boolean;
appID, appName: string;
listTrace: TStringList;
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
inAppID := True
else
inAppID := False;
if jreader.Value.AsString = 'uco-core:name' then
inAppName := True
else
inAppName := False;
end;
if JsonTokenToString(jreader.TokenType) = 'String' then
begin
if inAppID then
appID := jreader.Value.AsString;
if inAppName then begin
appName := jreader.Value.AsString;
cbAppID.Items.Add(appName + StringofChar(' ', 50) + '@' + appID);
end;
end;
end;
finally
jreader.Free;
sreader.Free;
end;
end;
end;
procedure TformTraceApplicationAccount.btnCancelClick(Sender: TObject);
begin
formTraceApplicationAccount.Close;
end;
procedure TformTraceApplicationAccount.btnCloseClick(Sender: TObject);
var
fileJSON: TextFile;
line:string;
idx: integer;
begin
//dir := GetCurrentDir;
// create file JSON uuidCase-phone_account.json
AssignFile(fileJSON, FpathCase + FuuidCase + '-traceAPPLICATION_ACCOUNT.json');
if lbAppAccount.Items.Count > 0 then
begin
idx := 0;
Rewrite(fileJSON); // create new file
WriteLn(fileJSON, '{');
line := #9 + '"OBJECTS_APPLICATION_ACCOUNT":[';
WriteLn(fileJSON, line);
for idx:= 0 to lbAppAccount.Items.Count - 2 do
WriteLn(fileJSON, #9#9 + lbAppAccount.Items[idx] + ',');
WriteLn(fileJSON, #9#9 + lbAppAccount.Items[idx]);
WriteLn(fileJSON, #9#9 + ']');
Write(fileJSON,'}');
CloseFile(fileJSON);
end
else
deleteFile(FpathCase + FuuidCase + '-traceAPPLICATION_ACCOUNT.json');
formTraceApplicationAccount.Close;
end;
procedure TformTraceApplicationAccount.btnAddTraceClick(Sender: TObject);
begin
lbAppAccount.Items.Add(prepareTrace('add'));
cbAppID.ItemIndex := -1;
edAppName.Text := '';
edAccountIdentifier.Text:= '';
end;
procedure TformTraceApplicationAccount.SetpathCase(const Value: String);
begin
FpathCase := Value;
end;
procedure TformTraceApplicationAccount.SetuuidCase(const Value: string);
begin
FuuidCase := Value;
end;
procedure TformTraceApplicationAccount.ShowWithParamater(pathCase: String; uuidCase: String);
var
fileJSON: TextFile;
line, subLine:string;
begin
SetUuidCase(uuidCase);
SetPathCase(pathCase);
//dir := GetCurrentDir;
// read file JSON uuidCase-tracePHONE_ACCOUNT.json
if FileExists(FpathCase + FuuidCase + '-traceAPPLICATION_ACCOUNT.json') then
begin
AssignFile(fileJSON, FpathCase + FuuidCase + '-traceAPPLICATION_ACCOUNT.json');
Reset(fileJSON);
lbAppAccount.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);
lbAppAccount.Items.Add(line);
end;
end;
CloseFile(fileJSON);
end;
// else
// ShowMessage(dir + uuidCase + '-identity.json' + ' doesn''t exist');
formTraceApplicationAccount.ShowModal;
end;
end.