unit NewService_Impl;

{----------------------------------------------------------------------------}
{ This unit was automatically generated by the RemObjects SDK after reading  }
{ the RODL file associated with this project .                               }
{                                                                            }
{ This is where you are supposed to code the implementation of your objects. }
{----------------------------------------------------------------------------}

interface

uses
  {vcl:} Classes, SysUtils,
  {RemObjects:} uROClientIntf, uROTypes, uROServer, uROServerIntf, uROSessions,
  {Ancestor Implementation:} DataAbstractService_Impl,
  {Used RODLs:} DataAbstract4_Intf,
  {Generated:}  uDABusinessProcessor, uDABin2DataStreamer, uDADataStreamer,
  uDAScriptingProvider, uDAClasses,
  FactuGES_Intf, uDAInterfaces, uDADataTable, uDAMemDataTable, uDABinAdapter;


const
  { Dataset names for Schema }
  ds_Employees = 'Employees';
  ds_Customers = 'Customers';
  ds_Regions = 'Regions';

type
  { TNewService }
  TNewService = class(TDataAbstractService, INewService)
    dtComboDataset: TDAMemDataTable;
    dtWindowsDir: TDAMemDataTable;
    DABinDataStreamer1: TDABinDataStreamer;
    Schema: TDASchema;
    procedure DataAbstractServiceBeforeGetDatasetData(aSender: TObject;
      const aDataset: IDADataset; const aIncludeSchema: Boolean;
      const aMaxRecords: Integer);
    procedure DataAbstractServiceValidateDatasetAccess(Sender: TObject;
      const aConnection: IDAConnection; const aDatasetName: string;
      const aParamNames: array of string;
      const aParamValues: array of Variant; aSchema: TDASchema;
      var Allowed: Boolean);
  private
    procedure FillComboDataset(const aDataset: IDAEditableDataset);
    procedure FillWinDirDataset(const aDataset: IDAEditableDataset);
  protected
    { INewService methods }
  end;

implementation

{$R *.dfm}
uses
  {Generated:} FactuGES_Invk, uDataModuleServer, uDatabaseUtils;

procedure Create_NewService(out anInstance: IUnknown);
begin
  anInstance := TNewService.Create(nil);
end;

procedure TNewService.FillComboDataset(const aDataset: IDAEditableDataset);
var
  employees,
    customers,
    regions: IDADataset;
  textfield: TDAField;
begin
{
  with aDataset do begin
    // Opens the in memory data table
    Open;
    textfield := FieldByName('TextField');

    // Adds 3 dummy records
    Insert;
    textfield.AsString := 'String A';
    Post;

    Insert;
    textfield.AsString := 'String B';
    Post;

    Insert;
    textfield.AsString := 'String C';
    Post;

    // Fetches data from other datasets, according to the value of the parameter "CustomParameter"
    if (Pos('EMPLOYEES', UpperCase(ParamByName('CustomParameter').AsString)) > 0) then begin
      employees := Schema.NewDataset(Connection, ds_Employees, [], [], TRUE);
      while not employees.EOF do begin
        Insert;
        textfield.AsString := employees.FieldByName('FirstName').AsString + ' ' + employees.FieldByName('LastName').AsString;
        Post;

        employees.Next;
      end;
      employees:=nil;
    end;
    if (Pos('CUSTOMERS', UpperCase(ParamByName('CustomParameter').AsString)) > 0) then begin
      customers := Schema.NewDataset(Connection, ds_Customers, [], [], TRUE);
      while not customers.EOF do begin
        Insert;
        textfield.AsString := customers.FieldByName('CompanyName').AsString;
        Post;

        customers.Next;
      end;
      customers := nil;
    end;
    if (Pos('REGIONS', UpperCase(ParamByName('CustomParameter').AsString)) > 0) then begin
      regions := Schema.NewDataset(Connection, ds_Regions, [], [], TRUE);
      while not regions.EOF do begin
        Insert;
        textfield.AsString := regions.FieldByName('RegionDescription').AsString;
        Post;

        regions.Next;
      end;
      regions := nil;
    end;
  end;
}  
end;

procedure TNewService.FillWinDirDataset(const aDataset: IDAEditableDataset);
var
  dirinfo: TSearchRec;
begin
  aDataset.Open;
{
  if (FindFirst(IncludeTrailingPathDelimiter(aDataset.ParamByName('Directory').AsString) + '*.*', faArchive, dirinfo) = 0) then repeat
      aDataset.Insert;

      aDataset.FieldByName('FileName').AsString := dirinfo.Name;
      aDataset.FieldByName('FileSize').AsInteger := dirinfo.Size;

      aDataset.Post;
    until (FindNext(dirinfo) <> 0);
}    
end;

procedure TNewService.DataAbstractServiceBeforeGetDatasetData(
  aSender: TObject; const aDataset: IDADataset;
  const aIncludeSchema: Boolean; const aMaxRecords: Integer);
begin
  if SameText(aDataset.LogicalName, dtComboDataset.LogicalName) then
    FillComboDataset((dtComboDataset as IDAEditableDataset))
  else if SameText(aDataset.LogicalName, dtWindowsDir.LogicalName) then
    FillWinDirDataset((dtWindowsDir as IDAEditableDataset));
end;

procedure TNewService.DataAbstractServiceValidateDatasetAccess(
  Sender: TObject; const aConnection: IDAConnection;
  const aDatasetName: string; const aParamNames: array of string;
  const aParamValues: array of Variant; aSchema: TDASchema;
  var Allowed: Boolean);

  procedure FillParameters(aDataSet: TDAMemDataTable);
  var
    i: integer;
  begin
    for i := Low(aParamNames) to High(aParamNames) do
      aDataset.ParamByName(aParamNames[i]).Value := aParamValues[i];
  end;

begin
  if SameText(aDatasetName, dtComboDataset.LogicalName) then
    FillParameters(dtComboDataset)
  else if SameText(aDatasetName, dtWindowsDir.LogicalName) then
    FillParameters(dtWindowsDir)
end;

initialization
  TROClassFactory.Create('NewService', Create_NewService, TNewService_Invoker);

finalization

end.

