218 lines
5.2 KiB
Plaintext
218 lines
5.2 KiB
Plaintext
{*******************************************************}
|
|
{ }
|
|
{ ManagerPolicy }
|
|
{ }
|
|
{ Copyright (C) 2025 kku }
|
|
{ }
|
|
{*******************************************************}
|
|
|
|
unit ManagerPolicy;
|
|
|
|
interface
|
|
|
|
uses
|
|
Tocsg.Obj, System.SysUtils, System.Classes, System.Generics.Collections,
|
|
Winapi.Windows;
|
|
|
|
const
|
|
DAT_PO = 'MgPo.dat';
|
|
PASS_PO = 'q1xUAq9=%~<vkhdM';
|
|
|
|
type
|
|
// TPoEntValType = (pvtInt, pvtBool, pbtStr);
|
|
PPoEnt = ^TPoEnt;
|
|
TPoEnt = record
|
|
nCode: Integer;
|
|
sKey,
|
|
sDesc,
|
|
sCate,
|
|
sUpDate,
|
|
sComment: String;
|
|
// pVal: Pointer;
|
|
// ValT: TPoEntValType
|
|
end;
|
|
TDcPoEnt = TDictionary<String,PPoEnt>;
|
|
TPoEntEnumerator = TEnumerator<PPoEnt>;
|
|
|
|
TManagerPolicy = class(TTgObject)
|
|
private
|
|
DcPoEnt_: TDcPoEnt;
|
|
sDataPath_: String;
|
|
procedure OnPoEntValNotify(Sender: TObject; const Item: PPoEnt; Action: TCollectionNotification);
|
|
public
|
|
Constructor Create(sDataPath: String = '');
|
|
Destructor Destroy; override;
|
|
function GetPoEntEnumerator: TPoEntEnumerator;
|
|
|
|
function ExtractPolicyDoc(sPath: String): Boolean;
|
|
|
|
function GetPoKeyCount: Integer;
|
|
function GetPoKey(sKey: String; bCodePo: Boolean): String;
|
|
|
|
procedure Save;
|
|
procedure Load;
|
|
end;
|
|
|
|
implementation
|
|
|
|
uses
|
|
superobject, Tocsg.Exception, Tocsg.Safe, Tocsg.Path;
|
|
|
|
{ TManagerPolicy }
|
|
|
|
Constructor TManagerPolicy.Create(sDataPath: String = '');
|
|
begin
|
|
Inherited Create;
|
|
DcPoEnt_ := TDcPoEnt.Create;
|
|
DcPoEnt_.OnValueNotify := OnPoEntValNotify;
|
|
sDataPath_ := sDataPath;
|
|
if sDataPath_ = '' then
|
|
sDatapath_ := GetRunExePathDir + DAT_PO;
|
|
Load;
|
|
end;
|
|
|
|
Destructor TManagerPolicy.Destroy;
|
|
begin
|
|
FreeAndNil(DcPoEnt_);
|
|
Inherited;
|
|
end;
|
|
|
|
procedure TManagerPolicy.OnPoEntValNotify(Sender: TObject; const Item: PPoEnt; Action: TCollectionNotification);
|
|
begin
|
|
if Action = cnRemoved then
|
|
Dispose(Item);
|
|
end;
|
|
|
|
function TManagerPolicy.GetPoEntEnumerator: TPoEntEnumerator;
|
|
begin
|
|
Result := DcPoEnt_.Values.GetEnumerator;
|
|
end;
|
|
|
|
function TManagerPolicy.ExtractPolicyDoc(sPath: String): Boolean;
|
|
var
|
|
O: ISuperObject;
|
|
i: TSuperObjectIter;
|
|
pEnt: PPoEnt;
|
|
begin
|
|
Result := false;
|
|
try
|
|
DcPoEnt_.Clear;
|
|
|
|
if not LoadJsonObjFromFile(O, sPath, TEncoding.UTF8) then
|
|
exit;
|
|
|
|
if O.DataType <> stObject then
|
|
exit;
|
|
|
|
if not ObjectFindFirst(O, i) then
|
|
exit;
|
|
|
|
try
|
|
repeat
|
|
New(pEnt);
|
|
ZeroMemory(pEnt, SizeOf(TPoEnt));
|
|
pEnt.nCode := StrToIntDef(i.key, 0);
|
|
pEnt.sKey := i.val.S['name'];
|
|
pEnt.sDesc := i.val.S['desc'];
|
|
pEnt.sCate := i.val.S['category'];
|
|
pEnt.sUpDate := i.val.S['updatedAt'];
|
|
pEnt.sComment := i.val.S['cmt'];
|
|
|
|
if (pEnt.sKey <> '') and not DcPoEnt_.ContainsKey(pEnt.sKey) then
|
|
DcPoEnt_.Add(pEnt.sKey, pEnt);
|
|
until not ObjectFindNext(i);
|
|
finally
|
|
ObjectFindClose(i);
|
|
end;
|
|
|
|
Result := true;
|
|
Save;
|
|
except
|
|
on E: Exception do
|
|
ETgException.TraceException(Self, E, 'Fail .. ExtractPolicyDoc()');
|
|
end;
|
|
end;
|
|
|
|
function TManagerPolicy.GetPoKeyCount: Integer;
|
|
begin
|
|
Result := DcPoEnt_.Count;
|
|
end;
|
|
|
|
function TManagerPolicy.GetPoKey(sKey: String; bCodePo: Boolean): String;
|
|
begin
|
|
if bCodePo and DcPoEnt_.ContainsKey(sKey) then
|
|
Result := IntToStr(DcPoEnt_[sKey].nCode)
|
|
else
|
|
Result := sKey;
|
|
end;
|
|
|
|
procedure TManagerPolicy.Save;
|
|
var
|
|
enum: TPoEntEnumerator;
|
|
O, OA: ISuperObject;
|
|
begin
|
|
try
|
|
OA := TSuperObject.Create(stArray);
|
|
|
|
Guard(enum, GetPoEntEnumerator);
|
|
while enum.MoveNext do
|
|
begin
|
|
O := SO;
|
|
O.I['C'] := enum.Current.nCode;
|
|
O.S['K'] := enum.Current.sKey;
|
|
// O.S['D'] := enum.Current.sDesc;
|
|
// O.S['T'] := enum.Current.sCate;
|
|
// O.S['U'] := enum.Current.sUpDate;
|
|
// O.S['M'] := enum.Current.sComment;
|
|
OA.AsArray.Add(O);
|
|
end;
|
|
|
|
O := SO;
|
|
O.O['List'] := OA;
|
|
SaveJsonObjToEncFile(O, sDataPath_, PASS_PO);
|
|
|
|
CopyFile(PChar(sDataPath_), 'C:\taskToCSG\eCrmHE\OUT_Debug - Win64\conf\' + DAT_PO, false);
|
|
CopyFile(PChar(sDataPath_), 'C:\taskToCSG\eCrmHE\OUT_Release - Win64\conf\' + DAT_PO, false);
|
|
except
|
|
on E: Exception do
|
|
ETgException.TraceException(Self, E, 'Fail .. Save()');
|
|
end;
|
|
end;
|
|
|
|
procedure TManagerPolicy.Load;
|
|
var
|
|
O: ISuperObject;
|
|
i: Integer;
|
|
pEnt: PPoEnt;
|
|
begin
|
|
try
|
|
if not FileExists(sDataPath_) then
|
|
exit;
|
|
|
|
if not LoadJsonObjFromEncFile(O, sDataPath_, PASS_PO) then
|
|
exit;
|
|
|
|
if (O.O['List'] = nil) or (O.O['List'].DataType <> stArray) then
|
|
exit;
|
|
|
|
DcPoEnt_.Clear;
|
|
for i := 0 to O.A['List'].Length - 1 do
|
|
begin
|
|
New(pEnt);
|
|
ZeroMemory(pEnt, SizeOf(TPoEnt));
|
|
pEnt.nCode := O.A['List'].O[i].I['C'];
|
|
pEnt.sKey := O.A['List'].O[i].S['K'];
|
|
pEnt.sDesc := O.A['List'].O[i].S['D'];
|
|
pEnt.sCate := O.A['List'].O[i].S['T'];
|
|
pEnt.sUpDate := O.A['List'].O[i].S['U'];
|
|
pEnt.sComment := O.A['List'].O[i].S['M'];
|
|
DcPoEnt_.Add(pEnt.sKey, pEnt);
|
|
end;
|
|
except
|
|
on E: Exception do
|
|
ETgException.TraceException(Self, E, 'Fail .. Load()');
|
|
end;
|
|
end;
|
|
|
|
end.
|