-
Notifications
You must be signed in to change notification settings - Fork 0
/
caseGenerator_trace_application.pas
198 lines (170 loc) · 5.71 KB
/
caseGenerator_trace_application.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
unit caseGenerator_trace_application;
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, caseGenerator_util;
type
TformTraceApplication = class(TForm)
Label1: TLabel;
lbAppAccount: TListBox;
btnClose: TButton;
btnAddTrace: TButton;
btnDeleteTrace: TButton;
btnCancel: TButton;
btnModifyTrace: TButton;
Label2: TLabel;
edAppName: TEdit;
Label4: TLabel;
procedure btnAddTraceClick(Sender: TObject);
procedure btnDeleteTraceClick(Sender: TObject);
procedure btnCloseClick(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
procedure btnModifyTraceClick(Sender: TObject);
procedure lbAppAccountChange(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;
{ Private declarations }
public
procedure ShowWithParamater(pathCase: String; uuidCase: String);
{ Public declarations }
end;
var
formTraceApplication: TformTraceApplication;
implementation
{$R *.fmx}
uses StrUtils;
{ TForm1 }
procedure TformTraceApplication.btnDeleteTraceClick(Sender: TObject);
begin
lbAppAccount.Items.Delete(lbAppAccount.ItemIndex);
end;
procedure TformTraceApplication.btnModifyTraceClick(Sender: TObject);
begin
if lbAppAccount.ItemIndex > - 1 then
lbAppAccount.Items[lbAppAccount.ItemIndex] := prepareTrace('modify');
end;
procedure TformTraceApplication.lbAppAccountChange(Sender: TObject);
var
line: String;
begin
if lbAppAccount.ItemIndex > - 1 then
begin
line := lbAppAccount.Items[lbAppAccount.ItemIndex];
edAppName.Text := ExtractField(line, '"uco-core:drafting:appName":"');
end;
end;
function TformTraceApplication.prepareTrace(operation: String): String;
var
line, recSep, indent, guidNoBraces: string;
Uid: TGUID;
idx: Integer;
begin
recSep := #30 + #30; // record separator, not printable
indent := ' ';
if (Trim(edAppName.Text) = '') then
ShowMessage('Application name 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:facets":[' + recSep;
line := line + indent + '{' + recSep;
line := line + RepeatString(indent, 2) + '"@type":"uco-observable:Application",' + recSep;
line := line + indent + '"uco-core:drafting:appName":"' + edAppName.Text + '"' + recSep;;
line := line + indent + '}' + recSep;
line := line + indent + ']' + recSep + '}';
end;
Result := line;
end;
procedure TformTraceApplication.btnCancelClick(Sender: TObject);
begin
formTraceApplication.Close;
end;
procedure TformTraceApplication.btnCloseClick(Sender: TObject);
var
fileJSON: TextFile;
line:string;
idx: integer;
begin
AssignFile(fileJSON, FpathCase + FuuidCase + '-traceAPPLICATION.json');
if lbAppAccount.Items.Count > 0 then
begin
idx := 0;
Rewrite(fileJSON); // create new file
WriteLn(fileJSON, '{');
line := #9 + '"OBJECTS_APPLICATION":[';
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 + '-traceAPP_ACCOUNT.json');
formTraceApplication.Close;
end;
procedure TformTraceApplication.btnAddTraceClick(Sender: TObject);
begin
lbAppAccount.Items.Add(prepareTrace('add'));
edAppName.Text := '';
end;
procedure TformTraceApplication.SetpathCase(const Value: String);
begin
FpathCase := Value;
end;
procedure TformTraceApplication.SetuuidCase(const Value: string);
begin
FuuidCase := Value;
end;
procedure TformTraceApplication.ShowWithParamater(pathCase: String; uuidCase: String);
var
fileJSON: TextFile;
line, subLine:string;
begin
SetUuidCase(uuidCase);
SetPathCase(pathCase);
//dir := GetCurrentDir;
// read file JSON uuidCase-traceAPPLICATION.json
if FileExists(FpathCase + FuuidCase + '-traceAPPLICATION.json') then
begin
AssignFile(fileJSON, FpathCase + FuuidCase + '-traceAPPLICATION.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');
formTraceApplication.ShowModal;
end;
end.