unit uDataModuleArticulos;

interface

uses
  SysUtils, Classes, DB, uDADataTable, uDABINAdapter,
  uDAScriptingProvider, uROWinInetHttpChannel, uROTypes,
  uRORemoteService, uROClient, uROBinMessage,
  uDADesigntimeCall,
  uIDataModuleArticulos, uBizArticulos, uDataModuleBase, uDARemoteDataAdapter,
  uDADataStreamer, uDABin2DataStreamer, uDAInterfaces, uDAMemDataTable;

type
  TDataModuleArticulos = class(TDataModuleBase, IDataModuleArticulos)
    RORemoteService: TRORemoteService;
    Bin2DataStreamer: TDABin2DataStreamer;
    rda_Articulos: TDARemoteDataAdapter;
    tbl_Articulos: TDAMemDataTable;
    ds_Articulos: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);

  protected
    procedure AsignarClaseNegocio(AArticulo: TDADataTable); virtual;

  public
    function GetItems : IBizArticulo;
    function GetItem(const ID : Integer) : IBizArticulo;
    function NewItem : IBizArticulo;
  end;

implementation

{$R *.DFM}

uses
  Etiquetas_Intf, uDataModuleConexion, uDataTableUtils, cxControls,
  schArticulosClient_Intf;

{ TdmArticulos }

procedure TDataModuleArticulos.AsignarClaseNegocio(AArticulo: TDADataTable);
begin
  AArticulo.BusinessRulesID := BIZ_CLIENT_ARTICULO;
end;

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

function TDataModuleArticulos.NewItem: IBizArticulo;
begin
  Result := GetItem(ID_NULO)
end;

function TDataModuleArticulos.GetItem(const ID: Integer): IBizArticulo;
var
  Condicion: TDAWhereExpression;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetItems;

    with Result.DataTable.DynamicWhere do
    begin
      // (ID = :ID)
      Condicion := NewBinaryExpression(NewField('', fld_ArticulosID), NewConstant(ID, datInteger), dboEqual);

      if IsEmpty then
         Expression := Condicion
      else
         Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
    end;

  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleArticulos.GetItems: IBizArticulo;
var
  AArticulo : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AArticulo := CloneDataTable(tbl_Articulos);
    AsignarClaseNegocio(AArticulo);
    Result := (AArticulo as IBizArticulo);
  finally
    HideHourglassCursor;
  end;
end;

end.
