unit uDataModuleEmpresas;

interface

uses {vcl:} SysUtils, Classes, DB, DBClient, 
     {RemObjects:} uDAClientDataModule, uDADataTable, uDAScriptingProvider,
  uDACDSDataTable, uDABINAdapter, uROServiceComponent, uRORemoteService,
  uROClient, uROBinMessage, uROWinInetHttpChannel, uDADesigntimeCall,
  uIDataModuleEmpresas, uBizEmpresas;

type
  TDataModuleEmpresas = class(TDAClientDataModule, IDataModuleEmpresas)
    RORemoteService: TRORemoteService;
    DABinAdapter: TDABINAdapter;
    tbl_Empresas: TDACDSDataTable;
    ds_Empresas: TDADataSource;
    DADesigntimeCall1: TDADesigntimeCall;
    LoginRemoteService: TRORemoteService;
    ROChannel: TROWinInetHTTPChannel;
    ROMessage: TROBinMessage;
    procedure DAClientDataModuleCreate(Sender: TObject);
  private
    function _CloneDataTable (const ADataTable : TDACDSDataTable): TDACDSDataTable; 
  public
    function GetItem(const ID : Integer) : IBizEmpresa;
    function NewItem : IBizEmpresa;
    function GetItems : IBizEmpresa;
    function GetNextID(const DataSetName : String) : Integer;
  end;


implementation

{$R *.DFM}

uses
  uDataModuleConexion, uDataTableUtils, uDAInterfaces,
  FactuGES_Intf, schEmpresasClient_Intf, cxControls;

procedure TDataModuleEmpresas.DAClientDataModuleCreate(Sender: TObject);
begin
  RORemoteService.Channel := dmConexion.Channel;
  RORemoteService.Message := dmConexion.Message;
end;

function TDataModuleEmpresas.GetItem(const ID: Integer): IBizEmpresa;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetItems;
    with Result.DataTable.Where do
    begin
      if NotEmpty then
        AddOperator(opAND);
      OpenBraket;
      AddText('EMPRESAS.' + fld_EmpresasID + ' = ' + IntToStr(ID));
      CloseBraket;
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleEmpresas.GetItems: IBizEmpresa;
var
  AEmpresa : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AEmpresa := _CloneDataTable(tbl_Empresas);
    AEmpresa.BusinessRulesID := BIZ_CLIENT_EMPRESA;
    Result := (AEmpresa as IBizEmpresa);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleEmpresas.GetNextID(const DataSetName: String): Integer;
var
  aGeneratorName : String;
begin
  if DataSetName = nme_Empresas then
    aGeneratorName := 'GEN_EMPRESAS_ID';

  Result := (RORemoteService as IsrvEmpresas).GetNextAutoInc(aGeneratorName)
end;

function TDataModuleEmpresas.NewItem: IBizEmpresa;
begin
  Result := GetItem(ID_NULO)
end;

function TDataModuleEmpresas._CloneDataTable(
  const ADataTable: TDACDSDataTable): TDACDSDataTable;
begin
  Result := NIL;

  if not Assigned(ADataTable) then
    raise Exception.Create ('No se ha asignado la tabla de origen (CloneDataTable)');

  Result := TDACDSDataTable.Create(NIL);
  CloneDataTable(ADataTable, Result);
end;

initialization

end.