unit SEP.Storage.Match;
interface
uses
SEP.Match,
System.Generics.Collections,
Spring.Container,
XML.XMLDoc,
XMLIntf,
Winapi.ActiveX,
System.SysUtils;
type
IMatchStorage =
interface
['
{0541D06C-3C1B-40B6-923F-AA715713CDB9}']
function AddMatchToStorage: TMatch;
function GetMatchList: TObjectList<TMatch>;
function GetMatchListAsDictionary: TDictionary<Integer, TMatch>;
function GetMatchByID(aID: Integer): TMatch;
function DeleteMatchFromStorage(aID: Integer): Boolean;
procedure Save;
procedure Load;
end;
TMatchStorage =
class(TInterfacedObject, IMatchStorage)
strict Private
fMatch: TDictionary<Integer, TMatch>;
fXML: IXMLDocument;
public
function AddMatchToStorage: TMatch;
function GetMatchList: TObjectList<TMatch>;
function GetMatchListAsDictionary: TDictionary<Integer, TMatch>;
function DeleteMatchFromStorage(aID: Integer): Boolean;
function GetMatchByID(aID: Integer): TMatch;
procedure Save;
procedure Load;
constructor Create;
destructor Destroy;
override;
end;
implementation
{ TMatchStorage }
function TMatchStorage.AddMatchToStorage: TMatch;
var
lIndex: Integer;
begin
Result := TMatch.Create;
Result.ID := fMatch.Count + 1;
if not fMatch.ContainsKey(Result.ID)
then
begin
fMatch.Add(Result.ID, Result);
Exit;
end;
for lIndex := 0
to fMatch.Count
do
begin
if not fMatch.ContainsKey(lIndex + 1)
then
begin
Result.ID := lIndex + 1;
fMatch.Add(Result.ID, Result);
Exit;
end;
end;
end;
constructor TMatchStorage.Create;
begin
fMatch := TDictionary<Integer, TMatch>.Create();
OleInitialize(
nil);
fXML := NewXMLDocument();
end;
function TMatchStorage.DeleteMatchFromStorage(aID: Integer): Boolean;
begin
fMatch.Remove(aID);
Result := true;
end;
destructor TMatchStorage.Destroy;
var
lIndex: Integer;
begin
for lIndex := 0
to fMatch.Count - 1
do
fMatch.Values.ToArray[lIndex].Free;
fMatch.Free;
inherited;
end;
function TMatchStorage.GetMatchByID(aID: Integer): TMatch;
begin
Result := fMatch.Items[aID];
end;
function TMatchStorage.GetMatchList: TObjectList<TMatch>;
var
lIndex: Integer;
begin
Result := TObjectList<TMatch>.Create(False);
for lIndex := 0
to fMatch.Count - 1
do
Result.Add(fMatch.Values.ToArray[lIndex])
end;
function TMatchStorage.GetMatchListAsDictionary: TDictionary<Integer, TMatch>;
begin
end;
procedure TMatchStorage.Load;
var
lMatch: TMatch;
lNode: IXMLNode;
lChildNode: IXMLNode;
lIndex: Integer;
begin
fMatch.Clear;
fXML.LoadFromFile('
Match.xml');
lNode := fXML.DocumentElement;
for lIndex := 0
to lNode.ChildNodes.Count - 1
do
begin
lChildNode := lNode.ChildNodes.Nodes[lIndex];
lMatch := TMatch.Create;
lMatch.ID := Integer(lChildNode.GetAttributeNS('
ID', '
'));
lMatch.Team1.ID := Integer(lChildNode.GetAttributeNS('
Team1', '
'));
lMatch.Team2.ID := Integer(lChildNode.GetAttributeNS('
Team2', '
'));
lMatch.Goals1 := Integer(lChildNode.GetAttributeNS('
Goals1', '
'));
lMatch.Goals2 := Integer(lChildNode.GetAttributeNS('
Goals2', '
'));
fMatch.Add(lMatch.ID, lMatch);
end;
end;
procedure TMatchStorage.Save;
var
lMatch: TMatch;
lNode: IXMLNode;
lTeamNode: IXMLNode;
lKey:
string;
begin
if not Assigned(fXML.DocumentElement)
then
begin
lNode := fXML.AddChild('
Match');
end
else
begin
lNode := fXML.DocumentElement;
end;
lNode.ChildNodes.Clear;
for lMatch
in fMatch.Values
do
begin
lKey := '
Match' + intToStr(lMatch.ID);
lTeamNode := lNode.ChildNodes.FindNode(lKey);
if not Assigned(lTeamNode)
then
lTeamNode := lNode.AddChild(lKey);
lTeamNode.SetAttributeNS('
Team1', '
', lMatch.Team1.ID);
lTeamNode.SetAttributeNS('
Team2', '
', lMatch.Team2.ID);
lTeamNode.SetAttributeNS('
ID', '
', lMatch.ID);
lTeamNode.SetAttributeNS('
Goals1', '
', lMatch.Goals1);
lTeamNode.SetAttributeNS('
Goals2', '
', lMatch.Goals2);
end;
fXML.SaveToFile('
Match.xml');
end;
end.