unit srvContactos_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:} uROXMLIntf, uROClientIntf, uROTypes, uROServer, uROServerIntf, uROSessions,
  {Required:} uRORemoteDataModule,
  {Ancestor Implementation:} DataAbstractService_Impl,
  {Used RODLs:} DataAbstract4_Intf,
  {Generated:}   uDABusinessProcessor, uDABin2DataStreamer, uDADataStreamer,
  uDAScriptingProvider, uDAClasses,
  Etiquetas_Intf, uDAInterfaces;

type
  { TsrvContactos }
  TsrvContactos = class(TDataAbstractService, IsrvContactos)
    Diagrams: TDADiagrams;
    bpContactos: TDABusinessProcessor;
    bpDatosBancarios: TDABusinessProcessor;
    bpPacientes: TDABusinessProcessor;
    bpDireccionesContacto: TDABusinessProcessor;
    Bin2DataStreamer: TDABin2DataStreamer;
    bpPersonalContacto: TDABusinessProcessor;
    schContactos: TDASchema;
    DataDictionary: TDADataDictionary;
    procedure DARemoteServiceBeforeGetDatasetData(const Dataset: IDADataset;
      const IncludeSchema: Boolean; const MaxRecords: Integer);
    procedure DARemoteServiceCreate(Sender: TObject);
    procedure DataAbstractServiceBeforeAcquireConnection(aSender: TObject;
      var aConnectionName: string);
  public
    function GenerarInformeFichaPaciente(const ListaID: TIntegerArray): Binary;
    function GenerarInformeEtiquetas(const ListaID: TIntegerArray): Binary;
  end;

implementation

{$R *.dfm}
uses
  {Generated:} Etiquetas_Invk, Dialogs, uBizContactosServer,
  uDataModuleServer, uDatabaseUtils, uUsersManager,
  schContactosClient_Intf,   uRestriccionesUsuarioUtils,
  uStringsUtils, Variants,
  uRptFichasPaciente_Server, uRptEtiquetasContacto_Server, uBizPacientesServer;

procedure Create_srvContactos(out anInstance : IUnknown);
begin
  anInstance := TsrvContactos.Create(NIL);
end;

{ TsrvContactos }

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

procedure TsrvContactos.DARemoteServiceCreate(Sender: TObject);
begin
  //SessionManager := dmServer.SessionManager;

  bpContactos.BusinessRulesID := BIZ_SERVER_CONTACTO;
  bpPacientes.BusinessRulesID := BIZ_SERVER_PACIENTE;
end;

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

function TsrvContactos.GenerarInformeEtiquetas(const ListaID: TIntegerArray): Binary;
var
  AReportGenerator : TRptEtiquetasContacto;
begin
  AReportGenerator := TRptEtiquetasContacto.Create(nil);
  try
    Result := AReportGenerator.GenerarInforme(ListaID);
  finally
    FreeAndNIL(AReportGenerator);
  end;
end;

function TsrvContactos.GenerarInformeFichaPaciente(
  const ListaID: TIntegerArray): Binary;
var
  AReportGenerator : TRptFichasPaciente;
begin
  AReportGenerator := TRptFichasPaciente.Create(nil);
  try
    Result := AReportGenerator.GenerarFicha(ListaID);
  finally
    FreeAndNIL(AReportGenerator);
  end;
end;

var
  fClassFactory: IROClassFactory;
initialization
  fClassFactory := TROClassFactory.Create('srvContactos', Create_srvContactos, TsrvContactos_Invoker);

finalization
  UnRegisterClassFactory(fClassFactory);
  fClassFactory := nil;

end.
