unit uDataModuleMontajes;

interface

uses
  SysUtils, Classes, DB, uDAClientDataModule, uDADataTable, uDABINAdapter,
  uDAScriptingProvider, uDACDSDataTable, uROWinInetHttpChannel, uROTypes,
  uROServiceComponent, uRORemoteService, uROClient, uROBinMessage,
  uDADesigntimeCall,
  uIDataModuleMontajes, uBizMontajes;

type
  TDataModuleMontajes = class(TDAClientDataModule, IDataModuleMontajes)
    RORemoteService: TRORemoteService;
    DABinAdapter: TDABINAdapter;
    DADesigntimeCall1: TDADesigntimeCall;
    LoginRemoteService: TRORemoteService;
    ROWinInetHTTPChannel1: TROWinInetHTTPChannel;
    ROBinMessage1: TROBinMessage;
    tbl_Montajes: TDACDSDataTable;
    ds_Montajes: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);

  protected
    function _CloneDataTable (const ADataTable : TDACDSDataTable): TDACDSDataTable; overload;
    procedure AsignarClaseNegocio(var AMontaje: TDACDSDataTable); virtual;
    function darNombreGenerador(DataSetName: String): String; virtual;
  public
    function GetItems : IBizMontaje;
    function GetItem(const ID : Integer) : IBizMontaje;
    function GetNextID(const DataSetName : String) : Integer;
    function NewItem : IBizMontaje;
  end;

implementation

{$R *.DFM}

uses
  FactuGES_Intf, uDataModuleConexion, uDataTableUtils, cxControls, uDAInterfaces,
  schMontajesClient_Intf, uBizContactos;

{ TdmMontajes }

procedure TDataModuleMontajes.AsignarClaseNegocio(var AMontaje: TDACDSDataTable);
begin
  AMontaje.BusinessRulesID := BIZ_CLIENT_MONTAJE;
end;

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

function TDataModuleMontajes.darNombreGenerador(DataSetName: String): String;
begin
  if DataSetName = nme_Montajes then
    Result := 'GEN_MONTAJES_ID';
end;

function TDataModuleMontajes.GetNextID(const DataSetName: String): Integer;
var
  aGeneratorName : String;
begin
  aGeneratorName := darNombreGenerador(DataSetName);
  Result := (RORemoteService as IsrvMontajes).GetNextAutoInc(aGeneratorName)
end;

function TDataModuleMontajes.NewItem: IBizMontaje;
begin
  Result := GetItem(ID_NULO)
end;

function TDataModuleMontajes._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;

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

function TDataModuleMontajes.GetItems: IBizMontaje;
var
  AMontaje : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AMontaje := _CloneDataTable(tbl_Montajes);

    AsignarClaseNegocio(AMontaje);

    Result := (AMontaje as IBizMontaje);

  finally
    HideHourglassCursor;
  end;
end;


end.
