unit ClientForm;
interface
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, Data.SqlExpr, Data.DBXDataSnap,
  IndyPeerImpl, Data.DBXCommon, Vcl.StdCtrls, Data.DBXTrace,
  Datasnap.DSClientRest, Data.FMTBcd, Vcl.Grids, Vcl.DBGrids, Vcl.ExtCtrls,
  Vcl.DBCtrls, Datasnap.DBClient, Datasnap.Provider, Datasnap.DSConnect;

type
  TForm1 = class(TForm)
    SQLConnection1: TSQLConnection;
    Button1: TButton;
    SQLMonitor1: TSQLMonitor;
    Button2: TButton;
    DSRestConnection1: TDSRestConnection;
    ClientDataSet1: TClientDataSet;
    DBNavigator1: TDBNavigator;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    DSProviderConnection1: TDSProviderConnection;
    btnApplyUpdates: TButton;
    btnUndoLast: TButton;
    btnUndoCurrent: TButton;
    btnUndoAll: TButton;
    btnSave: TButton;
    btnLoad: TButton;
    btnOpen: TButton;
    ClientDataSet1DEPT_NO: TStringField;
    ClientDataSet1DEPARTMENT: TStringField;
    ClientDataSet1HEAD_DEPT: TStringField;
    ClientDataSet1MNGR_NO: TSmallintField;
    ClientDataSet1BUDGET: TFMTBCDField;
    ClientDataSet1LOCATION: TStringField;
    ClientDataSet1PHONE_NO: TStringField;
    ClientDataSet1sqlEmployees: TDataSetField;
    DBGrid2: TDBGrid;
    ClientDataSet2: TClientDataSet;
    DataSource2: TDataSource;
    btnUndoLast2: TButton;
    btnUndoCurrent2: TButton;
    btnUndoAll2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure SQLMonitor1Trace(Sender: TObject; TraceInfo: TDBXTraceInfo;
      var LogTrace: Boolean);
    procedure SQLConnection1AfterConnect(Sender: TObject);
    procedure SQLConnection1AfterDisconnect(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ClientDataSet1AfterPost(DataSet: TDataSet);
    procedure ClientDataSet1AfterDelete(DataSet: TDataSet);
    procedure ClientDataSet1ReconcileError(DataSet: TCustomClientDataSet;
      E: EReconcileError; UpdateKind: TUpdateKind;
      var Action: TReconcileAction);
    procedure btnApplyUpdatesClick(Sender: TObject);
    procedure btnUndoLastClick(Sender: TObject);
    procedure btnUndoCurrentClick(Sender: TObject);
    procedure btnUndoAllClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure btnLoadClick(Sender: TObject);
    procedure btnOpenClick(Sender: TObject);
    procedure ClientDataSet1BeforeGetRecords(Sender: TObject;
      var OwnerData: OleVariant);
    procedure btnUndoLast2Click(Sender: TObject);
    procedure btnUndoCurrent2Click(Sender: TObject);
    procedure btnUndoAll2Click(Sender: TObject);
  private
    { Private declarations }
    const
      BriefcaseFile = 'Briefcase.xml';
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
uses
  ServerMethodsClient,
  CodeSiteLogging,
  DataSnap.DSHTTPLayer, ClientClassesUnit1, Vcl.RecError; // to add https communication layer

{$R *.dfm}

procedure TForm1.btnApplyUpdatesClick(Sender: TObject);
begin
  if ClientDataSet1.ApplyUpdates(0) = 0 then
    if FileExists(BriefcaseFile) then
      DeleteFile(BriefcaseFile)
end;

procedure TForm1.btnLoadClick(Sender: TObject);
begin
  ClientDataSet1.LoadFromFile(BriefcaseFile);
end;

procedure TForm1.btnOpenClick(Sender: TObject);
begin
  ClientDataSet1.Open;
  ClientDataSet1.First
end;

procedure TForm1.btnSaveClick(Sender: TObject);
begin
  ClientDataSet1.SaveToFile(BriefcaseFile, dfXMLUTF8);
end;

procedure TForm1.btnUndoAll2Click(Sender: TObject);
begin
  ClientDataSet2.CancelUpdates
end;

procedure TForm1.btnUndoAllClick(Sender: TObject);
begin
  ClientDataSet1.CancelUpdates
end;

procedure TForm1.btnUndoCurrent2Click(Sender: TObject);
begin
  ClientDataSet2.RevertRecord
end;

procedure TForm1.btnUndoCurrentClick(Sender: TObject);
begin
  ClientDataSet1.RevertRecord
end;

procedure TForm1.btnUndoLast2Click(Sender: TObject);
begin
  ClientDataSet2.UndoLastChange(True)
end;

procedure TForm1.btnUndoLastClick(Sender: TObject);
begin
  ClientDataSet1.UndoLastChange(True)
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Server: ServerMethodsClient.TMyServerMethodsClient;
begin
  SQLConnection1.Connected := True; // just in case...
  Server := ServerMethodsClient.TMyServerMethodsClient.Create(SQLConnection1.DBXConnection);
  try
    ShowMessage(Server.ReverseString( DateTimeToStr(Server.ServerTime)) )
  finally
    Server.Free
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  MyServerMethodsClient: ClientClassesUnit1.TMyServerMethodsClient;
begin
  MyServerMethodsClient := ClientClassesUnit1.TMyServerMethodsClient.Create(DSRestConnection1);
  try
    ShowMessage(MyServerMethodsClient.ReverseString('abc'));
  finally
    MyServerMethodsClient.Free
  end;
end;

procedure TForm1.ClientDataSet1AfterDelete(DataSet: TDataSet);
begin
//ClientDataSet1.ApplyUpdates(0);
end;

procedure TForm1.ClientDataSet1AfterPost(DataSet: TDataSet);
begin
//ClientDataSet1.ApplyUpdates(0);
end;

procedure TForm1.ClientDataSet1BeforeGetRecords(Sender: TObject;
  var OwnerData: OleVariant);
//var
//  CurRec: TBookmark;
//  OldFetchOnDemand: Boolean;
begin
//  with Sender as TClientDataSet do if Active then // Client already active?
//  begin
//    OldFetchOnDemand := FetchOnDemand; // save
//    CurRec := GetBookmark;
//    try
//      FetchOnDemand := False;
//      Last;
//      OwnerData := FieldbyName('EMP_NO').AsString;
//      CodeSite.Send('TClientDataSet EMP_NO = ' + OwnerData);
//      GotoBookmark(CurRec)
//    finally
//      FetchOnDemand := OldFetchOnDemand; // restore
//      FreeBookmark(CurRec)
//    end
//  end
end;

procedure TForm1.ClientDataSet1ReconcileError(DataSet: TCustomClientDataSet;
  E: EReconcileError; UpdateKind: TUpdateKind; var Action: TReconcileAction);
begin
//ShowMessage(E.Message)
  Action := HandleReconcileError(DataSet, UpdateKind, E)
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
//  ClientDataSet1.Open
end;

procedure TForm1.SQLConnection1AfterConnect(Sender: TObject);
begin
  CodeSite.Send('After Connect');
end;

procedure TForm1.SQLConnection1AfterDisconnect(Sender: TObject);
begin
  CodeSite.Send('After Disconnect');
end;

procedure TForm1.SQLMonitor1Trace(Sender: TObject; TraceInfo: TDBXTraceInfo;
  var LogTrace: Boolean);
begin
  CodeSite.SendMsg('SQLMonitor.Trace: ' + TraceInfo.Message);
  LogTrace := True;
end;

end.
