unit MainForm;

interface

uses
  System.SysUtils, System.Classes, WEBLib.Graphics, WEBLib.Forms, WEBLib.Dialogs,
  VCL.TMSFNCTypes, VCL.TMSFNCUtils, VCL.TMSFNCGraphics, VCL.TMSFNCGraphicsTypes,
  VCL.TMSFNCCustomComponent, VCL.TMSFNCCustomGrid,
  VCL.TMSFNCGridDatabaseAdapter, Vcl.Controls, VCL.TMSFNCCustomControl,
  VCL.TMSFNCCustomScrollControl, VCL.TMSFNCGridData, VCL.TMSFNCGrid, Data.DB,
  WEBLib.DBCtrls, XData.Web.JsonDataset, XData.Web.Dataset, XData.Web.Connection,
  XData.Model.Classes, JS,
  Vcl.StdCtrls, WEBLib.StdCtrls, XData.Web.Client, WEBLib.DB;

type
  TForm1 = class(TWebForm)
    XDataWebConnection1: TXDataWebConnection;
    XDataWebDataSet1: TXDataWebDataSet;
    WebDataSource1: TWebDataSource;
    TMSFNCGrid1: TTMSFNCGrid;
    TMSFNCGridDatabaseAdapter1: TTMSFNCGridDatabaseAdapter;
    btConnect: TWebButton;
    WebLabel1: TWebLabel;
    cbEntitySets: TWebComboBox;
    WebLabel2: TWebLabel;
    cbOrderBy: TWebComboBox;
    WebLabel3: TWebLabel;
    WebLabel4: TWebLabel;
    cbPageSize: TWebComboBox;
    WebLabel5: TWebLabel;
    edPageNumber: TWebEdit;
    XDataWebClient1: TXDataWebClient;
    procedure btConnectClick(Sender: TObject);
    procedure WebFormCreate(Sender: TObject);
    procedure cbEntitySetsChange(Sender: TObject);
    procedure XDataWebClient1Load(Response: TXDataClientResponse);
    procedure XDataWebConnection1Connect(Sender: TObject);
    procedure XDataWebClient1Request(Request: TXDataClientRequest);
  private
    function BuildQueryString: string;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btConnectClick(Sender: TObject);
begin
//  XDataWebDataSet1.Close;
//  XDataWebDataSet1.EntitySetName := cbEntitySets.Items[cbEntitySets.ItemIndex];
//  XDataWebDataSet1.Load;

  XDataWebClient1.List(cbEntitySets.Items[cbEntitySets.ItemIndex], BuildQueryString);
end;

function TForm1.BuildQueryString: string;
var
  Query: string;

  procedure AddQueryParam(const Name, Value: string);
  begin
    if Query <> '' then
      Query := Query + '&';
    Query := Query + Name + '=' + Value;
  end;

var
  PageNo: Integer;
  PageSize: Integer;
begin
  Query := '';
  if cbOrderBy.ItemIndex > 0 then
    AddQueryParam('$orderby', cbOrderBy.Items[cbOrderBy.ItemIndex]);
  if cbPageSize.ItemIndex > 0 then
  begin
    PageSize := StrToInt(cbPageSize.Items[cbPageSize.ItemIndex]);
    AddQueryParam('$top', IntToStr(PageSize));
    PageNo := StrToInt(edPageNumber.Text);
    if PageNo > 1 then
      AddQueryParam('$skip', IntToStr((PageNo - 1) * PageSize));
  end;
  Result := Query;
end;

procedure TForm1.cbEntitySetsChange(Sender: TObject);
var
  EntityType: TXDataEntityType;
  I: Integer;
begin
  EntityType := XDataWebConnection1.Model.FindEntitySet(cbEntitySets.Items[cbEntitySets.ItemIndex]).EntityType;
  cbOrderBy.Items.Clear;
  cbOrderBy.Items.Add('(none)');
  for I := 0 to EntityType.Properties.Count - 1 do
    cbOrderBy.Items.Add(EntityType.Properties[I].Name);
  cbOrderBy.ItemIndex := 0;
end;

procedure TForm1.WebFormCreate(Sender: TObject);
begin
  cbPageSize.ItemIndex := 0;
  XDataWebConnection1.LoadModel;
end;

procedure TForm1.XDataWebConnection1Connect(Sender: TObject);
var
  Container: TXDataEntityContainer;
  I: Integer;
begin
  Container  := XDataWebConnection1.Model.DefaultEntityContainer;

  cbEntitySets.Items.Clear;
  for I := 0 to Container.EntitySets.Count - 1 do
    cbEntitySets.Items.Add(Container.EntitySets[I].Name);
  if cbEntitySets.Items.Count > 0 then
    cbEntitySets.ItemIndex := 0;
  cbEntitySetsChange(nil);
end;

procedure TForm1.XDataWebClient1Load(Response: TXDataClientResponse);
begin
  XDataWebDataSet1.Close;
  XDataWebDataSet1.EntitySetName := cbEntitySets.Items[cbEntitySets.ItemIndex];
  XDataWebDataset1.SetJsonData(Response.Result);
  XDataWebDataset1.Open;
end;

procedure TForm1.XDataWebClient1Request(Request: TXDataClientRequest);
begin
  // A quick and generic way to retrieve associated objects in expanded format
  Request.Request.Headers.SetValue('xdata-expand-level', '1');
  Request.Request.Headers.SetValue('xdata-proxy-load-depth', '1');
  Request.Request.Headers.SetValue('xdata-proxy-list-load-depth', '1');
end;

end.
