unit srvObras_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, uRORemoteDataModule, uROClient;

type
  { TsrvObras }
  TsrvObras = class(TDataAbstractService, IsrvObras)
    Diagrams: TDADiagrams;
    Bin2DataStreamer: TDABin2DataStreamer;
    bpObras: TDABusinessProcessor;
    bpObrasEjecuciones: TDABusinessProcessor;
    bpObrasEjecucionesPresupuestos: TDABusinessProcessor;
    bpObrasEjecucionesPedidosProv: TDABusinessProcessor;
    schObras: TDASchema;
    DataDictionary: TDADataDictionary;
    procedure DataAbstractServiceBeforeAcquireConnection(aSender: TObject;
      var aConnectionName: string);
    procedure DARemoteServiceAfterGetDatasetData(const Dataset: IDADataset;
      const IncludeSchema: Boolean; const MaxRecords: Integer);
    procedure DARemoteServiceCreate(Sender: TObject);
  protected
    function DarListaObras(const IDEmpresa: Integer): Binary;
  end;

implementation

{$R *.dfm}
uses
  {Generated:} FactuGES_Invk, uDataModuleServer, uROClasses,
  uDatabaseUtils, schObrasClient_Intf, uRestriccionesUsuarioUtils,
  uBizObrasServer, Variants;

procedure Create_srvObras(out anInstance : IUnknown);
begin
  anInstance := TsrvObras.Create(NIL);
end;

{ srvObras }
procedure TsrvObras.DARemoteServiceAfterGetDatasetData(
  const Dataset: IDADataset; const IncludeSchema: Boolean;
  const MaxRecords: Integer);
begin
  if Assigned(Dataset.FindField(fld_ObrasID_EMPRESA)) then
  begin
    { Aquí se asegura que el usuario sólo accede a los Obras
      de las empresas a las que tiene permiso para acceder
      filtrando DataSet por ID_EMPRESA. }
    FiltrarAccesoUsuario(Session, Connection, schObras, DataSet, fld_ObrasID_EMPRESA);
  end;
end;

procedure TsrvObras.DARemoteServiceCreate(Sender: TObject);
begin
//  SessionManager := dmServer.SessionManager;
  bpObras.BusinessRulesID := BIZ_SERVER_OBRA;
end;

procedure TsrvObras.DataAbstractServiceBeforeAcquireConnection(
  aSender: TObject; var aConnectionName: string);
begin
  inherited;
  ConnectionName := dmServer.ConnectionName;
end;


function TsrvObras.DarListaObras(const IDEmpresa: Integer): Binary;
var
  ASchema : TDASchema;
  AConn : IDAConnection;
  dsData: IDADataset;
  ALista : TStringList;
begin
  Result := Binary.Create;

  ASchema := schObras;
  AConn := dmServer.ConnectionManager.NewConnection(dmServer.ConnectionManager.GetDefaultConnectionName);

  try
    dsData := ASchema.NewDataset(AConn, 'ListaObras', ['ID_EMPRESA'], [IntToStr(IDEmpresa)]);
  except
    RaiseError('No existe la tabla ALMACENES');
  end;

  ALista := TStringList.Create;
  try
    dsData.Active := True;
//    ALista.Sorted := True;
    while not dsData.EOF do
    begin
      ALista.Add(Format('%s=%d', [dsData.Fields[1].AsString, dsData.Fields[0].AsInteger]));
      dsData.Next;
    end;

    ALista.SaveToStream(Result);
  finally
    FreeANDNIL(ALista);
    dsData := NIL;
    AConn := NIL;
  end;
end;

initialization
  TROClassFactory.Create('srvObras', Create_srvObras, TsrvObras_Invoker);

finalization

end.
