forked from bravesoftdz/CustomDatabaseManagement
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudSMSConfigs.pas
567 lines (519 loc) · 18.3 KB
/
udSMSConfigs.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
unit udSMSConfigs;
{
July 2016 Code By: Daniel Campbell
}
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, Grids, DBGrids, SMDBGrid, DB, kbmMemTable, Buttons,
ufDBEDatabase, IniFiles;
type
TdSMSConfigs = class(TForm)
dbgConfigs: TSMDBGrid;
mtbConfigs: TkbmMemTable;
dsConfigs: TDataSource;
mtbConfigssmsconfig_mnemonic: TStringField;
mtbConfigsdefault_value: TStringField;
mtbConfigslow_value: TStringField;
mtbConfigshigh_value: TStringField;
mtbConfigsdescription: TStringField;
btnOK: TSpeedButton;
mtbConfigsexists: TBooleanField;
pb1: TProgressBar;
btnNewConfig: TSpeedButton;
mtbConfigsIndex: TIntegerField;
btnRemove: TSpeedButton;
btnUpdate: TSpeedButton;
mtbConfigsSMSdefault_value: TStringField;
mtbConfigsobsolete: TStringField;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormActivate(Sender: TObject);
procedure mtbConfigsdescriptionChange(Sender: TField);
procedure btnOKClick(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure dbgConfigsDrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
procedure mtbConfigsBeforePost(DataSet: TDataSet);
procedure btnNewConfigClick(Sender: TObject);
procedure btnRemoveClick(Sender: TObject);
procedure mtbConfigsBeforeDelete(DataSet: TDataSet);
procedure btnUpdateClick(Sender: TObject);
procedure mtbConfigsSMSdefault_valueChange(Sender: TField);
private
fLoading, fUpdating, fCommitToSVN : Boolean;
fSMSIni : TIniFile;
procedure LoadConfigsFromIni;
procedure SaveLastChange;
procedure CanNowCommit;
procedure SetupDevConnection(var aDBEDatabase: TDBEDatabase);
procedure HideProgress;
procedure ShowProgress;
procedure AppendConfig(aConfig, aDesc: String; aIndex : Integer = -1);
procedure DetectAndSaveNewConfigsFromGRef;
public
end;
implementation
uses
ufISQLQuery, uuGlobals, StdCtrls, DBCtrls, StrUtils, ufReference;
const
C_SECTION = 'smsconfigs';
D_SECTION = 'smsconfigs_desc';
DV_SECTION = 'smsconfigs_DV';
{$R *.dfm}
procedure TdSMSConfigs.ShowProgress;
begin
pb1.Visible := True;
pb1.Position := 0;
pb1.BringToFront;
end;
procedure TdSMSConfigs.HideProgress;
begin
pb1.Visible := False;
pb1.Position := 0;
end;
procedure TdSMSConfigs.FormActivate(Sender: TObject);
var
lLevelCode : string;
begin
fUpdating := False;
ShowProgress;
lLevelCode := GetRegistry('location_code', POS_DIRECTORY) +
GetRegistry('register_code', POS_DIRECTORY);
Self.Caption := 'SMS Configuration Settings ' + lLevelCode;
gRef.LoadConfig(lLevelCode, True);
LoadConfigsFromIni;
HideProgress;
dbgConfigs.FocusFilterBar;
end;
//Used when loading Ini, pressing 'New', updating from dev, updating from gRef
//Note: When loading from Ini, we already know the Index, and that's the only time
procedure TdSMSConfigs.AppendConfig(aConfig, aDesc : String; aIndex : Integer = -1);
var
lStr : string;
begin
if mtbConfigs.Locate('smsconfig_mnemonic',aConfig,[loCaseInsensitive]) then
ShowMessage('Duplicate config found in SMSDataEditor.ini: ' + ^M + aConfig)
else
begin
mtbConfigs.Append;
mtbConfigssmsconfig_mnemonic.AsString := aConfig;
if aIndex < 0 then //not loading from Ini
begin
mtbConfigsIndex.AsInteger := mtbConfigs.RecordCount + 1;
//If dev/Gref gives us an empty string but there is something in the config guide,
//we don't want to override the desc's we have manually entered. e.g. direct configs
if aDesc <> '' then
mtbConfigsDescription.AsString := aDesc;
end
else if fLoading then
begin
//We want to get the desc from the Ini only when loading from Ini
//Not when updating from gRef/dev/'New'
mtbConfigsIndex.AsInteger := aIndex;
if Assigned(fSMSIni) then
begin
lStr := fSMSIni.ReadString(D_SECTION, 'D' + IntToStr(mtbConfigsIndex.AsInteger), '');
lStr := StringReplace(lStr, INI_ENTER, #13#10,[rfReplaceAll]);
mtbConfigsdescription.AsString := lStr;
lStr := fSMSIni.ReadString(DV_SECTION, 'V' + IntToStr(mtbConfigsIndex.AsInteger), '');
mtbConfigsSMSdefault_value.AsString := lStr;
end;
end
else //This shouldn't happen
begin
mtbConfigs.Cancel;
ShowMessage('Unexpected error when appending config: ' + aConfig);
Exit;
end;
if gRef.Exists(aConfig) then
begin
mtbConfigsdefault_value.AsString := gRef.Get(aConfig, '');
mtbConfigslow_value.AsString := gRef.GetLow(aConfig, '');
mtbConfigshigh_value.AsString := gRef.GetHigh(aConfig, '');
mtbConfigsexists.AsBoolean := True;
end
else
mtbConfigsexists.AsBoolean := False;
mtbConfigs.Post;
if not fLoading then
CanNowCommit;
end;
end;
procedure TdSMSConfigs.LoadConfigsFromIni;
var
lEntries : TStringList;
I, lPos : Integer;
lConfig : string;
lConfigIndex : string;
begin
fLoading := True;
mtbConfigs.Close;
mtbConfigs.Open;
fSMSIni := TIniFile.Create(gAppPath + MAIN_INI);
lEntries := TStringList.Create;
mtbConfigs.DisableControls;
try
fSMSIni.ReadSectionValues('smsconfigs', lEntries);
pb1.Max := lEntries.Count;
for I := 0 to lEntries.Count - 1 do
begin
lConfig := lEntries[I];
lPos := AnsiPos('=', lConfig);
if AnsiStartsStr('C', lConfig) then
begin
lConfigIndex := lConfig;
Delete(lConfig, 1, lPos); //delete the 'C23='
Delete(lConfigIndex, lPos, Length(lConfigIndex)); //delete '=CONFIG'
Delete(lConfigIndex, 1, 1); //delete the 'C'
if (Trim(lConfig) <> '') and (lConfigIndex <> '') then
AppendConfig(Trim(lConfig), '', StrToInt(lConfigIndex));
end;
pb1.StepBy(1);
Application.ProcessMessages;
end;
finally
mtbConfigs.SortOn('smsconfig_mnemonic', [mtcoCaseInsensitive]);
mtbConfigs.First;
mtbConfigs.EnableControls;
fSMSIni.Free;
FreeAndNil(lEntries);
HideProgress;
fLoading := False;
end;
end;
procedure TdSMSConfigs.mtbConfigsBeforeDelete(DataSet: TDataSet);
var
SMSIni : TIniFile;
lIndex : Integer;
begin
lIndex := mtbConfigsIndex.AsInteger;
SMSIni := TIniFile.Create(gAppPath + MAIN_INI);
try
SMSIni.DeleteKey(C_SECTION, 'C' + IntToStr(lIndex));
SMSIni.DeleteKey(D_SECTION, 'D' + IntToStr(lIndex));
SMSIni.DeleteKey(DV_SECTION, 'V' + IntToStr(lIndex));
finally
SMSIni.Free;
end;
end;
//Called when loading from Ini, pressing 'New', updating from dev, updating from gRef
procedure TdSMSConfigs.mtbConfigsBeforePost(DataSet: TDataSet);
procedure InsertSMSConfig(aName, aDefaultValue, aLowValue, aHighValue : String);
var
lQry : ISQLQuery;
lLevelCode : string;
begin
lQry := gDataBase.NewQuery;
try
try
//First check if there is already a register level setting
//If there is, then change it, otherwise create a new register level setting
lLevelCode := GetRegistry('location_code', POS_DIRECTORY) +
GetRegistry('register_code', POS_DIRECTORY);
lQry.SQL.Clear;
lQry.SQL.Add('select * from smsconfig where smsconfig_mnemonic = ' + QuotedStr(aName) +
' and active_ind = ''Y'' and level_ind = ''R'' and level_code = ' + QuotedStr(lLevelCode));
lQry.Open;
if not lQry.eof then
begin
lQry.Edit;
lQry.FieldByName('default_value').AsString := aDefaultValue;
lQry.FieldByName('low_value').AsString := aLowValue;
lQry.FieldByName('high_value').AsString := aHighValue;
lQry.Post;
end
else
begin
lQry.Append;
lQry.FieldByName('smsconfig_id').Value := gDatabase.GetNextPrimaryKey('smsconfig', 'smsconfig_id');
lQry.FieldByName('smsconfig_mnemonic').Value := aName;
lQry.FieldByName('active_ind').Value := 'Y';
lQry.FieldByName('default_value').AsString := aDefaultValue;
lQry.FieldByName('low_value').AsString := aLowValue;
lQry.FieldByName('high_value').AsString := aHighValue;
lQry.FieldByName('level_ind').Value := 'R';
lQry.FieldByName('level_code').Value := lLevelCode;
lQry.Post;
end;
except
On E : Exception do
ShowMessage('Failed to insert smsconfig: ' + E.Message);
end;
finally
if Assigned(lQry) and lQry.Active then
lQry.Close;
lQry := nil;
end;
end;
procedure InsertIntoIni(aName, aDesc, aDefaultValue : string; aIndex : Integer);
var
SMSIni : TIniFile;
begin
aDesc := StringReplace(aDesc, #13#10, INI_ENTER,[rfReplaceAll]);
SMSIni := TIniFile.Create(gAppPath + MAIN_INI);
try
if SMSIni.ReadString(C_SECTION, 'C' + IntToStr(aIndex), '') = '' then
SMSIni.WriteString(C_SECTION, 'C' + IntToStr(aIndex), aName);
SMSIni.WriteString(D_SECTION, 'D' + IntToStr(aIndex), aDesc);
SMSIni.WriteString(DV_SECTION, 'V' + IntToStr(aIndex), aDefaultValue);
finally
SMSIni.Free;
end;
end;
begin
if not fLoading then
begin
if mtbConfigssmsconfig_mnemonic.AsString <> '' then
begin
//We don't want to insert records into smsconfig if updating from Gref/dev
//But we do want to insert them into the Ini, that's the whole point
if not fUpdating then
begin
InsertSMSConfig(mtbConfigssmsconfig_mnemonic.AsString,
mtbConfigsdefault_value.AsString,
mtbConfigslow_value.AsString,
mtbConfigshigh_value.AsString);
end;
InsertIntoIni(mtbConfigssmsconfig_mnemonic.AsString,
mtbConfigsdescription.AsString,
mtbConfigsSMSdefault_value.AsString,
mtbConfigsIndex.AsInteger);
end;
end;
end;
procedure TdSMSConfigs.mtbConfigsdescriptionChange(Sender: TField);
begin
if not fLoading then
begin
CanNowCommit;
end;
end;
procedure TdSMSConfigs.mtbConfigsSMSdefault_valueChange(Sender: TField);
begin
if not fLoading then
begin
CanNowCommit;
end;
end;
procedure TdSMSConfigs.CanNowCommit;
begin
fCommitToSVN := True;
btnOK.Caption := 'Commit To SVN';
end;
procedure TdSMSConfigs.btnNewConfigClick(Sender: TObject);
var
lConfigName : string;
begin
SaveLastChange;
lConfigName := Trim(InputBox('New SMS Configuration Setting', 'smsconfig_mnemonic?', ''));
if lConfigName <> '' then
begin
//We dont want to insert into smsconfig when adding a new config
fUpdating := True;
AppendConfig(lConfigName, '');
fUpdating := False;
mtbConfigs.Locate('smsconfig_mnemonic',lConfigName,[loCaseInsensitive]);
end;
end;
procedure TdSMSConfigs.btnOKClick(Sender: TObject);
begin
SaveLastChange;
if fCommitToSVN then
CommitSMSIni;
ModalResult := mrOk;
end;
procedure TdSMSConfigs.btnRemoveClick(Sender: TObject);
begin
SaveLastChange;
mtbConfigs.Delete;
CanNowCommit;
end;
procedure TdSMSConfigs.DetectAndSaveNewConfigsFromGRef;
var
lConfigFromGRef : string;
lAdded : TStringList;
lShowM : string;
I : Integer;
begin
lShowM := '';
ShowProgress;
pb1.Max := gRef.MT.RecordCount;
Application.ProcessMessages;
lAdded := TStringList.Create;
try
gRef.MT.First;
while not gRef.MT.eof do
begin
lConfigFromGRef := UpperCase(Trim(gRef.MT.FieldByName('smsconfig_mnemonic').AsString));
if (lConfigFromGRef <> '') and (not mtbConfigs.Locate('smsconfig_mnemonic', lConfigFromGRef, [loCaseInsensitive])) then
begin
AppendConfig(lConfigFromGRef, '');
lAdded.Add(lConfigFromGRef);
end;
gRef.MT.Next;
pb1.StepBy(1);
Application.ProcessMessages;
end;
if lAdded.Count > 0 then
begin
lShowM := IntToStr(lAdded.Count) + ' configs added to SMSDataEditor.ini from this database.' + ^M +
'Please commit to SVN if they are not rubbish.' + ^M;
for I := 0 to lAdded.Count - 1 do
begin
lShowM := lShowM + ^M + lAdded[I];
end;
ShowMessage(lShowM);
CanNowCommit;
end;
finally
lAdded.Free;
HideProgress;
end;
end;
procedure TdSMSConfigs.btnUpdateClick(Sender: TObject);
var
lDBEDatabase : TDBEDatabase;
lQuery : ISQLQuery;
lSMSConfig : string;
lRecCount : Integer;
lAdded : TStringList;
lShowM : string;
I : Integer;
lDesc : string;
begin
SaveLastChange;
fUpdating := True;
mtbConfigs.DisableControls;
DetectAndSaveNewConfigsFromGRef;
lDBEDatabase := TDBEDatabase.Create;
try
SetupDevConnection(lDBEDatabase);
try
lDBEDatabase.Connected := True;
except
on E : SysUtils.Exception do
begin
ShowMessage(E.Message);
lDBEDatabase.Free;
Exit;
end;
end;
lQuery := lDBEDatabase.NewQuery;
lQuery.SQL.Clear;
lQuery.SQL.Add('select count(1) as reccount from smsconfigassist sa with (nolock)');
lQuery.SQL.Add(' left join smsconfigtemp st with (nolock) on (sa.smsconfig_mnemonic = st.mnemonic)');
lQuery.Open;
try
lRecCount := lQuery.FieldByName('reccount').AsInteger;
finally
lQuery.Close;
end;
lQuery.SQL.Clear;
lQuery.SQL.Add('select sa.smsconfig_mnemonic, sa.description, st.default_value, st.obsolete from smsconfigassist sa with (nolock)');
lQuery.SQL.Add(' left join smsconfigtemp st with (nolock) on (sa.smsconfig_mnemonic = st.mnemonic)');
lQuery.Open;
pb1.Max := lRecCount;
ShowProgress;
lAdded := TStringList.Create;
try
lQuery.First;
while not lQuery.eof do
begin
lSMSConfig := UpperCase(Trim(lQuery.FieldByName('smsconfig_mnemonic').AsString));
lDesc := lQuery.FieldByName('description').AsVariant;
if mtbConfigs.Locate('smsconfig_mnemonic',lSMSConfig,[locaseinsensitive]) then
begin
mtbConfigs.Edit;
//will .AsString only give 255 chars?
if Trim(lDesc) <> '' then
mtbConfigsdescription.AsString := Trim(lDesc);
mtbConfigsSMSdefault_value.AsString := lQuery.FieldByName('default_value').AsString;
//Might use this to colour obsolete configs red
mtbConfigsobsolete.AsString := lQuery.FieldByName('obsolete').AsString;
mtbConfigs.Post;
end
else if lDesc <> '' then
begin
//Only add the config if it has a description, there are silly configs
//in dev that have come from register and store tables, etc
AppendConfig(lSMSConfig, lDesc);
lAdded.Add(lSMSConfig);
end;
pb1.StepBy(1);
Application.ProcessMessages;
lQuery.Next;
end;
if lAdded.Count > 0 then
begin
lShowM := IntToStr(lAdded.Count) + ' configs added to SMSDataEditor.ini from MSSQL Dev Test.' + ^M +
'Please commit to SVN if they are not rubbish.' + ^M;
for I := 0 to lAdded.Count - 1 do
begin
lShowM := lShowM + ^M + lAdded[I];
end;
ShowMessage(lShowM);
end
else
ShowMessage('No new configs from dev test. Descriptions and default values have been overriden.');
finally
lQuery.Close;
mtbConfigs.First;
lAdded.Free;
HideProgress;
end;
finally
lQuery := nil;
lDBEDatabase.Free;
fUpdating := False;
mtbConfigs.EnableControls;
end;
end;
procedure TdSMSConfigs.SetupDevConnection(var aDBEDatabase : TDBEDatabase);
var
SMSDataEditorIni : TIniFile;
begin
SMSDataEditorIni := TIniFile.Create(gAppPath + MAIN_INI);
try
aDBEDatabase.ServerName := SMSDataEditorIni.ReadString('dev', 'Server', '');
aDBEDatabase.AliasName := SMSDataEditorIni.ReadString('dev', 'AliasName', '');
aDBEDatabase.UserName := SMSDataEditorIni.ReadString('dev', 'Username', '');
aDBEDatabase.Password := SMSDataEditorIni.ReadString('dev', 'Password', '');
finally
SMSDataEditorIni.Free;
end;
end;
procedure TdSMSConfigs.SaveLastChange;
begin
if mtbConfigs.State in [dsEdit] then
mtbConfigs.Post;
end;
procedure TdSMSConfigs.dbgConfigsDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
lGrid : TSMDBGrid;
lDataSet : TDataSet;
begin
lGrid := (Sender as TSMDBGrid);
lDataSet := lGrid.DataSource.DataSet;
if Assigned(lDataSet) then
begin
if lDataSet.FieldByName('exists').AsBoolean then
lGrid.Canvas.Font.Color := clWindowText
else
lGrid.Canvas.Font.Color := clGray;
end;
lGrid.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
procedure TdSMSConfigs.FormClose(Sender: TObject; var Action: TCloseAction);
begin
//Don't save last change
mtbConfigs.Close;
FreeAndNil(gRef);
end;
procedure TdSMSConfigs.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_ESCAPE then
Close;
end;
end.