unit Unit1;

interface

uses
  System.SysUtils, System.Classes, WebLib.Graphics, WebLib.Forms,
  Vcl.Controls, WebLib.DBCtrls,
  Aurelius.Bind.Dataset, XData.Web.Dataset, Data.DB,
  XData.Model.Classes,
  TypInfo,
  Bcl.TypInfo.Common,
  WEBLib.StdCtrls, Vcl.StdCtrls, XData.Web.Connection, XData.Web.JsonDataset,
  WEBLib.DB, WEBLib.Controls;

type
  TForm1 = class(TWebForm)
    XDataWebDataset1: TXDataWebDataset;
    WebDBEdit1: TWebDBEdit;
    WebDataSource1: TWebDataSource;
    WebDBNavigator1: TWebDBNavigator;
    btLoadData: TWebButton;
    XDataWebConnection1: TXDataWebConnection;
    cbEntitySets: TWebComboBox;
    WebLabel1: TWebLabel;
    btPrior: TWebButton;
    btNext: TWebButton;
    btEdit: TWebButton;
    btCancel: TWebButton;
    btDelete: TWebButton;
    btInsert: TWebButton;
    btPost: TWebButton;
    btApplyUpdates: TWebButton;
    lbState: TWebLabel;
    lbEOF: TWebLabel;
    procedure Log(const Msg: string);
    procedure btLoadDataClick(Sender: TObject);
    procedure XDataWebConnection1Connect(Sender: TObject);
    procedure WebFormCreate(Sender: TObject);
    procedure cbEntitySetsChange(Sender: TObject);
    procedure btPriorClick(Sender: TObject);
    procedure btNextClick(Sender: TObject);
    procedure btEditClick(Sender: TObject);
    procedure btInsertClick(Sender: TObject);
    procedure btPostClick(Sender: TObject);
    procedure btCancelClick(Sender: TObject);
    procedure btDeleteClick(Sender: TObject);
    procedure btApplyUpdatesClick(Sender: TObject);
    procedure WebDataSource1StateChange(Sender: TObject);
    procedure XDataWebDataset1AfterScroll(DataSet: TDataSet);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btNextClick(Sender: TObject);
begin
  XDataWebDataset1.Next;
end;

procedure TForm1.btPostClick(Sender: TObject);
begin
  XDataWebDataset1.Post;
end;

procedure TForm1.btPriorClick(Sender: TObject);
begin
  XDataWebDataset1.Prior;
end;

procedure TForm1.cbEntitySetsChange(Sender: TObject);
begin
  btLoadData.Enabled := cbEntitySets.ItemIndex >= 0;
end;

procedure TForm1.Log(const Msg: string);
begin
  WriteLn(Msg);
end;

procedure TForm1.btApplyUpdatesClick(Sender: TObject);
begin
  XDataWebDataset1.ApplyUpdates;
end;

procedure TForm1.btCancelClick(Sender: TObject);
begin
  XDataWebDataset1.Cancel;
end;

procedure TForm1.btDeleteClick(Sender: TObject);
begin
  XDataWebDataset1.Delete;
end;

procedure TForm1.btEditClick(Sender: TObject);
begin
  XDataWebDataset1.Edit;
end;

procedure TForm1.btInsertClick(Sender: TObject);
begin
  XDataWebDataset1.Insert;
end;

procedure TForm1.btLoadDataClick(Sender: TObject);
begin
  if cbEntitySets.ItemIndex = -1 then Exit;
  XDataWebDataset1.Close;
  XDataWebDataset1.EntitySetName := cbEntitySets.Items[cbEntitySets.ItemIndex];
  XDataWebDataset1.Load;
end;

procedure TForm1.WebDataSource1StateChange(Sender: TObject);
begin
  lbState.Caption := GetEnumName_(TypeInfo(TDatasetState), Ord(XDataWebDataset1.State));
end;

procedure TForm1.WebFormCreate(Sender: TObject);
begin
  XDataWebConnection1.Open;
end;

procedure TForm1.XDataWebConnection1Connect(Sender: TObject);
var
  I: Integer;
  Names: TStrings;
  Container: TXDataEntityContainer;
begin
  Names := cbEntitySets.Items;
  Container := XDataWebConnection1.Model.DefaultEntityContainer;
  if Container <> nil then
    for I := 0 to Container.EntitySets.Count - 1 do
      Names.Add(Container.EntitySets[I].Name);
end;

procedure TForm1.XDataWebDataset1AfterScroll(DataSet: TDataSet);
begin
  lbEOF.Caption := BoolToStr(XDataWebDataset1.Eof, True);
end;

end.
