unit XData.Design.WebDatasetEditor;

interface

uses
  Classes, DBReg, DSDesign, DB,
  XData.Web.Dataset;

type
  TBaseXDataWebDatasetEditor = class(TDataSetEditor)
  protected
    function Dataset: TXDataWebDataset;
    function GetDSDesignerClass: TDSDesignerClass; override;
  public
//    procedure ExecuteVerb(Index: Integer); override;
//    function GetVerb(Index: Integer): string; override;
//    function GetVerbCount: Integer; override;
  end;

  TXDataWebDataSetEditor = class(TBaseXDataWebDatasetEditor)
  public
    procedure ExecuteVerb(Index: Integer); override;
    function GetVerb(Index: Integer): string; override;
    function GetVerbCount: Integer; override;
  end;

  TJsonDatasetDesigner = class(TDSDesigner)
  private
    FSaveFieldDefs: TXDataWebDataset;
  public
    constructor Create(DataSet: TDataSet); override;
    destructor Destroy; override;
    procedure BeginUpdateFieldDefs; override;
    procedure EndUpdateFieldDefs; override;
  end;

  TEntitySetNameProperty = class(TDBStringProperty)
  public
    procedure GetValueList(List: TStrings); override;
  end;

implementation

uses
  SysUtils, IOUtils, TypInfo,
  XData.Model.Classes,
  XData.Web.Connection,
  XData.Design.DatasetDesigner;

{ TXDataDataSetEditor }

procedure TXDataWebDataSetEditor.ExecuteVerb(Index: Integer);
begin
  case Index - inherited GetVerbCount of
    0: TfmXDataFieldLoader.Execute(GetComponent as TDataset);
  else
    inherited ExecuteVerb(Index);
  end;
end;

function TXDataWebDataSetEditor.GetVerb(Index: Integer): string;
begin
  case Index - inherited GetVerbCount of
    0: Result := 'Load from XData server...';
  else
    Result := inherited GetVerb(Index);
  end;
end;

function TXDataWebDataSetEditor.GetVerbCount: Integer;
begin
  Result := 1 + inherited GetVerbCount;
end;

{ TBaseXDataWebDatasetEditor }

function TBaseXDataWebDatasetEditor.Dataset: TXDataWebDataset;
begin
  Result := GetComponent as TXDataWebDataset;
end;

function TBaseXDataWebDatasetEditor.GetDSDesignerClass: TDSDesignerClass;
begin
  Result := TJsonDatasetDesigner;
end;

{ TJsonDatasetDesigner }

procedure TJsonDatasetDesigner.BeginUpdateFieldDefs;
begin
  // Do not update field defs. Just save what's there and retrieve it back after update
  // This code, together with EndUpdateFieldDefs, just avoids that FieldDefs are updated by the designer
  if Dataset <> nil then
    FSaveFieldDefs.FieldDefs.Assign(Dataset.FieldDefs);
  inherited;
end;

constructor TJsonDatasetDesigner.Create(DataSet: TDataSet);
begin
  inherited;
  FSaveFieldDefs := TXDataWebDataset.Create(nil);
end;

destructor TJsonDatasetDesigner.Destroy;
begin
  FSaveFieldDefs.Free;
  inherited;
end;

procedure TJsonDatasetDesigner.EndUpdateFieldDefs;
begin
  inherited;
  if Dataset <> nil then
    Dataset.FieldDefs.Assign(FSaveFieldDefs.FieldDefs);
end;

{ TEntitySetNameProperty }

procedure TEntitySetNameProperty.GetValueList(List: TStrings);
var
  Connection: TXDataWebConnection;
  Model: TXDataModel;
  EntitySet: TXDataEntitySet;
begin
  Connection := GetObjectProp(GetComponent(0), 'Connection') as TXDataWebConnection;
  if (Connection <> nil) then
  begin
    Model := Connection.Model;
    if Model = nil then
      Model := Connection.RetrieveModel;
    try
      if Assigned(Model.DefaultEntityContainer) then
        for EntitySet in Model.DefaultEntityContainer.EntitySets do
          List.Add(EntitySet.Name);
    finally
      if Model <> Connection.Model then
        Model.Free;
    end;
  end;
end;

end.
