-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathMVCBr.ObjectConfigList.pas
914 lines (793 loc) · 26.2 KB
/
MVCBr.ObjectConfigList.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
/// <summary>
/// Objetivo: é um gerenciador de componentes de configura a serem persistindo
/// localmente pro INI ou JSON
/// Alterações:
/// 02/04/2017 - por: amarildo lacerda
/// + Adicionado suporte a JsonFile para a gravação dos dados
/// + Adicionado interface para IniFile e JSONFile
/// </summary>
unit MVCBr.ObjectConfigList;
{ *************************************************************************** }
{ }
{ MVCBr é o resultado de esforços de um grupo }
{ }
{ Copyright (C) 2017 MVCBr }
{ }
{ amarildo lacerda }
{ http://www.tireideletra.com.br }
{ }
{ }
{ *************************************************************************** }
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{ *************************************************************************** }
interface
{$D+}
uses System.Classes, System.SysUtils, System.RTTI,
System.JsonFiles, System.IniFiles, Data.DB,
{$IFDEF LINUX} {$ELSE}VCL.StdCtrls, VCL.Controls, {$ENDIF} MVCBr.Model,
MVCBr.Component, System.ThreadSafe,
System.Generics.Collections;
{$I translate/MVCBr.Translate.inc}
type
{$IFDEF LINUX}
TButton = TComponent;
TGroupBox = TComponent;
TComboBox = class(TComponent)
public
text: string;
end;
TLabel = class(TComponent)
public
Caption: string;
end;
TCheckBox = class(TComponent)
public
checked: boolean;
end;
TEdit = class(TComponent)
public
text: string;
end;
TMemo = class(TComponent)
Lines: TstringList;
end;
{$ENDIF}
TObjectConfigContentType = (ctIniFile, ctJsonFile, ctDataset);
IObjectConfigListItem = interface;
TObjectConfigListItem = class;
IObjectConfigListComum = interface
['{372B1554-E8BF-493A-AC15-1E7D415B3640}']
function This: TObject;
procedure ReadConfig;
procedure WriteConfig;
procedure RegisterControl(ASection: string; AText: string; AControl:
{$IFDEF LINUX} TComponent {$ELSE} TControl{$ENDIF}); overload;
procedure Add(AControl: {$IFDEF LINUX} TComponent
{$ELSE} TControl{$ENDIF}); overload;
function Count: integer;
function GetItems(idx: integer): TObjectConfigListItem;
procedure SetItems(idx: integer; const Value: TObjectConfigListItem);
property Items[idx: integer]: TObjectConfigListItem read GetItems
write SetItems;
end;
IConfigFile = interface;
IObjectConfigList = interface(IObjectConfigListComum)
['{302562A0-2A11-43F6-A249-4BD42E1133F2}']
procedure SetFileName(const Value: string);
function GetFileName: string;
property FileName: string read GetFileName write SetFileName;
Function GetContentFile: IConfigFile;
procedure SetContentType(const Value: TObjectConfigContentType);
// property ContentType: TObjectConfigContentType read FContentType
end;
IDBObjectConfigList = interface(IObjectConfigListComum)
['{7D11135B-D9E7-44FB-BF40-4DB182C85ECD}']
procedure SetDataset(const Value: TDataset);
function GetDataset: TDataset;
property Dataset: TDataset read GetDataset write SetDataset;
function GetGroupID: string;
procedure SetGroupID(const Value: string);
property GroupID: string read GetGroupID write SetGroupID;
end;
IObjectConfigListItem = interface
['{AD786625-5C1A-4486-B000-2F61BB0C0A27}']
function This: TObjectConfigListItem;
procedure SetSection(const Value: string);
procedure SetItem(const Value: string);
procedure SetValue(const Value: TValue);
function GetValue: TValue;
function GetItem: string;
function GetSection: string;
property Section: string read GetSection write SetSection;
property Item: string read GetItem write SetItem;
property Value: TValue read GetValue write SetValue;
end;
IConfigFile = interface
['{136C5B69-9402-4B25-A772-A6425DBF16DD}']
function ReadString(const Section, Ident, Default: string): string;
procedure WriteString(const Section, Ident, Value: String);
procedure WriteDateTime(const Section, Ident: string; Value: TDateTime);
procedure WriteBool(const Section, Ident: string; Value: boolean);
procedure WriteInteger(const Section, Ident: string; Value: integer);
function ReadInteger(const Section, Ident: string;
Default: integer): integer;
function ReadBool(const Section, Ident: string; Default: boolean): boolean;
function ReadDatetime(const Section, Ident: string; Default: TDateTime)
: TDateTime;
function ConfigFile: TObject;
end;
TObjectConfigListItem = class(TInterfacedObject, IObjectConfigListItem)
private
FControl: {$IFDEF LINUX} TComponent {$ELSE} TControl{$ENDIF};
FSection: string;
FItem: string;
procedure SetSection(const Value: string);
procedure SetItem(const Value: string);
procedure SetValue(const Value: TValue);
function GetValue: TValue;
function GetItem: string;
function GetSection: string;
public
class function new(ASection, AItem: string; AControl:
{$IFDEF LINUX} TComponent
{$ELSE} TControl{$ENDIF}): IObjectConfigListItem;
property Section: string read GetSection write SetSection;
property Item: string read GetItem write SetItem;
property Value: TValue read GetValue write SetValue;
function This: TObjectConfigListItem;
end;
TConfigIniFile = class(TInterfacedObject, IConfigFile)
private
FIniFile: TIniFile;
public
constructor create(AFileName: string);
destructor Destroy; override;
function ReadString(const Section, Ident, Default: string): string;
procedure WriteString(const Section, Ident, Value: String);
procedure WriteDateTime(const Section, Ident: string; Value: TDateTime);
procedure WriteBool(const Section, Ident: string; Value: boolean);
procedure WriteInteger(const Section, Ident: string; Value: integer);
function ReadInteger(const Section, Ident: string;
Default: integer): integer;
function ReadBool(const Section, Ident: string; Default: boolean): boolean;
function ReadDatetime(const Section, Ident: string; Default: TDateTime)
: TDateTime;
function ConfigFile: TObject;
end;
TConfigJsonFile = class(TInterfacedObject, IConfigFile)
private
FJsonFile: TJsonFile;
protected
public
constructor create(AFileName: string); overload; virtual;
destructor Destroy; override;
function ReadString(const Section, Ident, Default: string): string;
procedure WriteString(const Section, Ident, Value: String);
procedure WriteDateTime(const Section, Ident: string; Value: TDateTime);
procedure WriteBool(const Section, Ident: string; Value: boolean);
procedure WriteInteger(const Section, Ident: string; Value: integer);
function ReadInteger(const Section, Ident: string;
Default: integer): integer;
function ReadBool(const Section, Ident: string; Default: boolean): boolean;
function ReadDatetime(const Section, Ident: string; Default: TDateTime)
: TDateTime;
function ConfigFile: TObject;
end;
TObjectConfigModelCustom = class(TComponentFactory)
private
FList: TThreadedList<TObject>;
FProcRead: TProc<TObjectConfigListItem>;
FProcWrite: TProc<TObjectConfigListItem>;
FComponentFullPath: boolean;
function GetItems(idx: integer): TObjectConfigListItem;
procedure SetItems(idx: integer; const Value: TObjectConfigListItem);
Function GetContentFile: IConfigFile; virtual;
procedure SetComponentFullPath(const Value: boolean);
function GetComponentFullPath: boolean;
protected
procedure ReadConfig; virtual; abstract;
procedure WriteConfig; virtual; abstract;
procedure WriteItem(ASection, AItem: string; AValue: TValue); virtual;
procedure ReadItem(ASection, AItem: string; out AValue: TValue); virtual;
public
constructor create(AOwner: TComponent); overload; override;
destructor Destroy; override;
function This: TObject; override;
function Count: integer; virtual;
procedure Clear; virtual;
property Items[idx: integer]: TObjectConfigListItem read GetItems
write SetItems;
procedure RegisterControl(ASection: string; AText: string; AControl:
{$IFDEF LINUX} TComponent {$ELSE} TControl{$ENDIF}); overload; virtual;
procedure Add(AControl: {$IFDEF LINUX} TComponent
{$ELSE} TControl{$ENDIF}); overload; virtual;
property ComponentFullPath: boolean read GetComponentFullPath
write SetComponentFullPath;
end;
TObjectConfigModel = Class(TObjectConfigModelCustom, IObjectConfigList)
private
FFileName: string;
FContentFile: IConfigFile;
FContentType: TObjectConfigContentType;
function MemoToString(Value: TMemo): string;
procedure StringToMemo(text: string; memo: TMemo);
protected
procedure SetFileName(const Value: string);
function GetFileName: string;
Function GetContentFile: IConfigFile; override;
procedure SetContentType(const Value: TObjectConfigContentType);
public
constructor create(AOwner: TComponent); override;
class function new: IObjectConfigList; static;
property FileName: string read GetFileName write SetFileName;
property ContentType: TObjectConfigContentType read FContentType
write SetContentType;
procedure WriteConfig; override;
procedure ReadConfig; override;
procedure WriteItem(ASection, AItem: string; AValue: TValue); override;
procedure ReadItem(ASection, AItem: string; out AValue: TValue); override;
function ToString: String; override;
end;
TDBConfigColumnNames = record
ColumnIdent: string;
ColumnSection: string;
ColumnItem: string;
ColumnValue: string;
end;
TDBObjectConfigModel = class(TObjectConfigModelCustom, IObjectConfigListComum,
IDBObjectConfigList)
private
FGroupID: string;
FDataset: TDataset;
FColumnNames: TDBConfigColumnNames;
procedure SetDataset(const Value: TDataset);
function GetDataset: TDataset;
function GetGroupID: string;
procedure SetGroupID(const Value: string);
procedure SetColumnNames(const Value: TDBConfigColumnNames);
function FilterBuilder(ASection: string; AItem: string): string;
protected
procedure WriteConfig; override;
procedure ReadConfig; override;
public
constructor create(AOwner: TComponent); override;
class function new: IDBObjectConfigList; static;
procedure WriteItem(ASection, AItem: string; AValue: TValue); override;
procedure ReadItem(ASection, AItem: string; out AValue: TValue); override;
published
property Dataset: TDataset read GetDataset write SetDataset;
property GroupID: string read GetGroupID write SetGroupID;
property ColumnNames: TDBConfigColumnNames read FColumnNames
write SetColumnNames;
end;
implementation
uses System.Classes.Helper ;
type
TValueHelper = record helper for TValue
function isBoolean: boolean;
function isDatetime: boolean;
end;
{ TObjectConfig }
procedure TObjectConfigModelCustom.Clear;
begin
FList.Clear;
end;
function TObjectConfigModelCustom.Count: integer;
begin
result := FList.Count;
end;
constructor TObjectConfigModelCustom.create(AOwner: TComponent);
begin
inherited;
FComponentFullPath := false;
FList := TThreadedList<TObject>.create;
FProcRead := procedure(sender: TObjectConfigListItem)
var
FOut: TValue;
begin
{$D+}
FOut := sender.Value;
ReadItem(sender.Section, sender.Item, FOut);
sender.Value := FOut;
end;
FProcWrite := procedure(sender: TObjectConfigListItem)
begin
{$D+}
WriteItem(sender.Section, sender.Item, sender.Value);
end;
end;
constructor TObjectConfigModel.create(AOwner: TComponent);
begin
inherited create(AOwner);
FContentType := ctJsonFile;
FFileName := paramStr(0) + '.config';
end;
destructor TObjectConfigModelCustom.Destroy;
begin
FList.DisposeOf;
inherited;
end;
function TObjectConfigModel.GetFileName: string;
begin
result := FFileName;
end;
{$D+}
function TObjectConfigModelCustom.GetComponentFullPath: boolean;
begin
result := FComponentFullPath;
end;
Function TObjectConfigModelCustom.GetContentFile: IConfigFile;
begin
end;
{$D-}
function TObjectConfigModelCustom.GetItems(idx: integer): TObjectConfigListItem;
begin
result := TObjectConfigListItem(FList.Items[idx]);
end;
class function TObjectConfigModel.new: IObjectConfigList;
begin
result := TObjectConfigModel.create(nil);
end;
procedure TObjectConfigModel.ReadConfig;
var
i: integer;
begin
TThread.NameThreadForDebugging('Config.ReadConfig');
if FContentType = ctJsonFile then
TJsonFile(GetContentFile.ConfigFile).LoadValues;
if assigned(FProcRead) then
for i := 0 to FList.Count - 1 do
FProcRead(TObjectConfigListItem(FList.Items[i]));
end;
procedure TObjectConfigModel.ReadItem(ASection, AItem: string;
out AValue: TValue);
begin
TThread.NameThreadForDebugging('Config.ReadItem');
with GetContentFile do
if AValue.isBoolean then
AValue := ReadBool(ASection, AItem, AValue.AsBoolean)
else if AValue.isDatetime then
AValue := ReadDatetime(ASection, AItem, AValue.AsExtended)
else if (AValue.IsObject) and AValue.AsObject.InheritsFrom(TMemo) then
StringToMemo(ReadString(ASection, AItem, ''), TMemo(AValue.AsObject))
else
AValue := ReadString(ASection, AItem, AValue.AsString);
end;
procedure TObjectConfigModelCustom.Add(AControl: {$IFDEF LINUX} TComponent
{$ELSE} TControl{$ENDIF});
var
LItem: string;
begin
if FComponentFullPath then
LItem := AControl.className + '.' + AControl.name
else
LItem := AControl.name;
RegisterControl('Config', LItem, AControl);
end;
procedure TObjectConfigModelCustom.ReadItem(ASection, AItem: string;
out AValue: TValue);
begin
/// inherits
end;
procedure TObjectConfigModelCustom.RegisterControl(ASection: string;
AText: string; AControl: {$IFDEF LINUX} TComponent
{$ELSE} TControl{$ENDIF});
var
obj: TObjectConfigListItem;
begin
obj := TObjectConfigListItem.create;
obj.Section := ASection;
obj.Item := AText;
obj.FControl := AControl;
FList.Add(obj);
end;
procedure TObjectConfigModelCustom.SetComponentFullPath(const Value: boolean);
begin
FComponentFullPath := Value;
end;
procedure TObjectConfigModel.SetContentType(const Value
: TObjectConfigContentType);
begin
FContentFile := nil;
FContentType := Value;
end;
procedure TObjectConfigModel.SetFileName(const Value: string);
begin
FFileName := Value;
end;
procedure TObjectConfigModel.StringToMemo(text: string; memo: TMemo);
var
str: TstringList;
begin
str := TstringList.create;
try
str.Delimiter := '|';
str.DelimitedText := text;
memo.Lines.text := str.text;
finally
str.free;
end;
end;
procedure TObjectConfigModelCustom.SetItems(idx: integer;
const Value: TObjectConfigListItem);
begin
FList.Items[idx] := Value.This;
end;
function TObjectConfigModelCustom.This: TObject;
begin
result := self;
end;
function TObjectConfigModel.ToString: String;
begin
case ContentType of
ctIniFile:
result := TConfigIniFile(FContentFile.ConfigFile).FIniFile.ToString;
{ TODO: ??? }
ctJsonFile:
result := TConfigJsonFile(FContentFile.ConfigFile).FJsonFile.ToJson;
end;
end;
procedure TObjectConfigModelCustom.WriteItem(ASection, AItem: string;
AValue: TValue);
begin
// inherits
end;
procedure TObjectConfigModel.WriteConfig;
var
i: integer;
begin
if assigned(FProcRead) then
for i := 0 to FList.Count - 1 do
FProcWrite(TObjectConfigListItem(FList.Items[i]));
if ContentType = ctJsonFile then
with TJsonFile(GetContentFile.ConfigFile) do
begin
FileName := self.FFileName;
UpdateFile;
end
else
with TIniFile(GetContentFile.ConfigFile) do
begin
// FileName := self.FFileName;
UpdateFile;
end;
end;
function TObjectConfigModel.MemoToString(Value: TMemo): string;
var
str: TstringList;
begin
str := TstringList.create;
try
str.Assign(Value.Lines);
str.Delimiter := '|';
result := str.DelimitedText;
finally
str.free;
end;
end;
procedure TObjectConfigModel.WriteItem(ASection, AItem: string; AValue: TValue);
begin
with GetContentFile do
if AValue.isBoolean then
WriteBool(ASection, AItem, AValue.AsBoolean)
else if AValue.isDatetime then
WriteDateTime(ASection, AItem, AValue.AsExtended)
else if (AValue.IsObject) and AValue.AsObject.InheritsFrom(TMemo) then
WriteString(ASection, AItem, MemoToString(TMemo(AValue.AsObject)))
else
WriteString(ASection, AItem, AValue.AsString);
end;
{ TObjectConfigItem }
function TObjectConfigListItem.GetItem: string;
begin
result := FItem;
end;
function TObjectConfigListItem.GetSection: string;
begin
result := FSection;
end;
function TObjectConfigListItem.GetValue: TValue;
var
s: TValue;
begin
result := TValue.Empty;
if not assigned(FControl) then
exit;
if FControl.InheritsFrom(TEdit) then
result := TEdit(FControl).text
else if FControl.InheritsFrom(TCheckBox) then
result := TCheckBox(FControl).checked
else if FControl.InheritsFrom(TComboBox) then
result := TComboBox(FControl).text
else if FControl.IsContextProperty('text') then
begin
result := FControl.ContextProperties['text'];
end
else if FControl.IsContextProperty('value') then
begin
result := FControl.ContextProperties['value'];
end
else if FControl.IsContextProperty('checked') then
begin
result := FControl.ContextProperties['checked'];
end;
//showMessage(FControl.name+': '+result.asString);
end;
class function TObjectConfigListItem.new(ASection, AItem: string; AControl:
{$IFDEF LINUX} TComponent
{$ELSE} TControl{$ENDIF}): IObjectConfigListItem;
var
obj: TObjectConfigListItem;
begin
obj := TObjectConfigListItem.create;
obj.FControl := AControl;
obj.FSection := ASection;
obj.FItem := AItem;
result := obj;
end;
procedure TObjectConfigListItem.SetItem(const Value: string);
begin
FItem := Value;
end;
procedure TObjectConfigListItem.SetSection(const Value: string);
begin
FSection := Value;
end;
procedure TObjectConfigListItem.SetValue(const Value: TValue);
begin
if FControl.InheritsFrom(TEdit) then
TEdit(FControl).text := Value.AsString
else if FControl.InheritsFrom(TCheckBox) then
TCheckBox(FControl).checked := Value.AsBoolean
else if FControl.InheritsFrom(TComboBox) then
TComboBox(FControl).text := Value.AsString
else if FControl.IsContextProperty('text') then
FControl.ContextProperties['text'] := Value.AsString
else if FControl.IsContextProperty('value') then
FControl.ContextProperties['value'] := Value
else if FControl.IsContextProperty('checked') then
FControl.ContextProperties['checked'] := Value;
//showMessage(FControl.name+': '+value.asString);
end;
function TObjectConfigListItem.This: TObjectConfigListItem;
begin
result := self;
end;
function TValueHelper.isBoolean: boolean;
begin
result := TypeInfo = System.TypeInfo(boolean);
end;
function TValueHelper.isDatetime: boolean;
begin
result := TypeInfo = System.TypeInfo(TDateTime);
end;
{ TConfigIniFile }
constructor TConfigIniFile.create(AFileName: string);
begin
inherited create;
FIniFile := TIniFile.create(AFileName);
end;
destructor TConfigIniFile.Destroy;
begin
FIniFile.free;
inherited;
end;
function TConfigIniFile.ReadBool(const Section, Ident: string;
Default: boolean): boolean;
begin
result := FIniFile.ReadBool(Section, Ident, Default);
end;
function TConfigIniFile.ReadDatetime(const Section, Ident: string;
Default: TDateTime): TDateTime;
begin
result := FIniFile.ReadDatetime(Section, Ident, Default);
end;
function TConfigIniFile.ReadInteger(const Section, Ident: string;
Default: integer): integer;
begin
result := FIniFile.ReadInteger(Section, Ident, Default);
end;
function TConfigIniFile.ReadString(const Section, Ident,
Default: string): string;
begin
result := FIniFile.ReadString(Section, Ident, Default);
end;
function TConfigIniFile.ConfigFile: TObject;
begin
result := FIniFile;
end;
procedure TConfigIniFile.WriteBool(const Section, Ident: string;
Value: boolean);
begin
FIniFile.WriteBool(Section, Ident, Value);
end;
procedure TConfigIniFile.WriteDateTime(const Section, Ident: string;
Value: TDateTime);
begin
FIniFile.WriteDateTime(Section, Ident, Value);
end;
procedure TConfigIniFile.WriteInteger(const Section, Ident: string;
Value: integer);
begin
FIniFile.WriteInteger(Section, Ident, Value);
end;
procedure TConfigIniFile.WriteString(const Section, Ident, Value: String);
begin
FIniFile.WriteString(Section, Ident, Value);
end;
{ TConfigJsonFile }
constructor TConfigJsonFile.create(AFileName: string);
begin
inherited create;
FJsonFile := TJsonFile.create(AFileName);
end;
destructor TConfigJsonFile.Destroy;
begin
FJsonFile.free;
inherited;
end;
function TConfigJsonFile.ReadBool(const Section, Ident: string;
Default: boolean): boolean;
begin
result := FJsonFile.ReadBool(Section, Ident, Default);
end;
function TConfigJsonFile.ReadDatetime(const Section, Ident: string;
Default: TDateTime): TDateTime;
begin
result := FJsonFile.ReadDatetime(Section, Ident, Default);
end;
function TConfigJsonFile.ReadInteger(const Section, Ident: string;
Default: integer): integer;
begin
result := FJsonFile.ReadInteger(Section, Ident, Default);
end;
function TConfigJsonFile.ReadString(const Section, Ident,
Default: string): string;
begin
result := FJsonFile.ReadString(Section, Ident, Default);
end;
function TConfigJsonFile.ConfigFile: TObject;
begin
result := FJsonFile;
end;
procedure TConfigJsonFile.WriteBool(const Section, Ident: string;
Value: boolean);
begin
FJsonFile.WriteBool(Section, Ident, Value);
end;
procedure TConfigJsonFile.WriteDateTime(const Section, Ident: string;
Value: TDateTime);
begin
FJsonFile.WriteDateTime(Section, Ident, Value);
end;
procedure TConfigJsonFile.WriteInteger(const Section, Ident: string;
Value: integer);
begin
FJsonFile.WriteInteger(Section, Ident, Value);
end;
procedure TConfigJsonFile.WriteString(const Section, Ident, Value: String);
begin
FJsonFile.WriteString(Section, Ident, Value);
end;
{ TDBObjectConfigModel }
constructor TDBObjectConfigModel.create(AOwner: TComponent);
begin
inherited;
FGroupID := msgocl_SectionGroup;
FColumnNames.ColumnIdent := msgocl_ColumanNames_File;
FColumnNames.ColumnSection := msgocl_ColumanNames_Section;
FColumnNames.ColumnItem := msgocl_ColumanNames_Item;
FColumnNames.ColumnValue := msgocl_ColumanNames_Value;
end;
function TDBObjectConfigModel.GetDataset: TDataset;
begin
result := FDataset;
end;
function TDBObjectConfigModel.GetGroupID: string;
begin
result := FGroupID;
end;
class function TDBObjectConfigModel.new: IDBObjectConfigList;
begin
result := TDBObjectConfigModel.create(nil);
end;
procedure TDBObjectConfigModel.ReadConfig;
var
i: integer;
begin
inherited;
if csDesigning in ComponentState then
exit;
assert(assigned(FDataset), msgocl_AssertDataSet);
if not FDataset.active then
FDataset.active := true;
for i := 0 to FList.Count - 1 do
FProcRead(TObjectConfigListItem(FList.Items[i]));
end;
procedure TDBObjectConfigModel.ReadItem(ASection, AItem: string;
out AValue: TValue);
begin
FDataset.Filter := FilterBuilder(ASection, AItem);
FDataset.Filtered := true;
if not FDataset.eof then
begin
with FDataset do
if AValue.isBoolean then
AValue := FieldByname(msgocl_ColumanNames_Value).AsBoolean
else if AValue.isDatetime then
AValue := FieldByname(msgocl_ColumanNames_Value).asDateTime
else
AValue := FieldByname(msgocl_ColumanNames_Value).AsString;
end;
end;
function TDBObjectConfigModel.FilterBuilder(ASection: string;
AItem: string): string;
begin
result := format(' %s = %s and %s = %s and %s = %s ',
[msgocl_ColumanNames_File, quotedStr(FGroupID), msgocl_ColumanNames_Section,
quotedStr(ASection), msgocl_ColumanNames_Item, quotedStr(AItem)]);
end;
procedure TDBObjectConfigModel.SetColumnNames(const Value
: TDBConfigColumnNames);
begin
FColumnNames := Value;
end;
procedure TDBObjectConfigModel.SetDataset(const Value: TDataset);
begin
FDataset := Value;
end;
procedure TDBObjectConfigModel.SetGroupID(const Value: string);
begin
FGroupID := Value;
end;
procedure TDBObjectConfigModel.WriteConfig;
begin
inherited;
if csDesigning in ComponentState then
exit;
if FDataset.State in dsEditModes then
FDataset.post;
end;
procedure TDBObjectConfigModel.WriteItem(ASection, AItem: string;
AValue: TValue);
begin
FDataset.Filter := FilterBuilder(ASection, AItem);
FDataset.Filtered := true;
if FDataset.eof then
begin
FDataset.append;
FDataset.FieldByname(msgocl_ColumanNames_File).AsString := FGroupID;
FDataset.FieldByname(msgocl_ColumanNames_Section).AsString := ASection;
FDataset.FieldByname(msgocl_ColumanNames_Item).AsString := AItem;
end
else
FDataset.edit;
FDataset.FieldByname(msgocl_ColumanNames_Value).Value := AValue.AsVariant;
if FDataset.State in dsEditModes then
FDataset.post;
end;
{ TObjectConfigModel }
function TObjectConfigModel.GetContentFile: IConfigFile;
begin
if not assigned(FContentFile) then
case ContentType of
ctIniFile:
FContentFile := TConfigIniFile.create(FFileName);
ctJsonFile:
FContentFile := TConfigJsonFile.create(FFileName);
end;
result := FContentFile;
end;
end.