unit srvImpresiones_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:} DARemoteService_Impl,
  {Used RODLs:} DataAbstract_Intf,
  {Generated:} FactuGES_Intf, uDADataTable, uDABINAdapter, uDAClasses,
  uDAScriptingProvider, uDABusinessProcessor;

type
  { TsrvImpresiones }
  TsrvImpresiones = class(TDARemoteService, IsrvImpresiones)
    Diagrams: TDADiagrams;
    DABINAdapter: TDABINAdapter;
    bpImpresiones: TDABusinessProcessor;
    schImpresiones: TDASchema;
    DataDictionary: TDADataDictionary;
    procedure DARemoteServiceCreate(Sender: TObject);
    procedure DARemoteServiceBeforeAcquireConnection(Sender: TDARemoteService;
      var ConnectionName: string);
  public
    { IsrvImpresiones methods }
    function AnadirMarcaImpresion(const NombreTabla: String; const IdsTabla: String): Boolean;
  end;

implementation

{$R *.dfm}
uses
  {Generated:} FactuGES_Invk, uDataModuleServer, uDatabaseUtils, uDAInterfaces,
  uROClasses;

procedure Create_srvImpresiones(out anInstance : IUnknown);
begin
  anInstance := TsrvImpresiones.Create(NIL);
end;

{ srvImpresiones }
procedure TsrvImpresiones.DARemoteServiceBeforeAcquireConnection(
  Sender: TDARemoteService; var ConnectionName: string);
begin
  ConnectionName := dmServer.ConnectionName;
end;

procedure TsrvImpresiones.DARemoteServiceCreate(Sender: TObject);
begin
  SessionManager := dmServer.SessionManager;
end;

function TsrvImpresiones.AnadirMarcaImpresion(const NombreTabla: String; const IdsTabla: String): Boolean;
var
  ASchema : TDASchema;
  AConn : IDAConnection;
  dsCommand: IDASQLCommand;
  ListaIds: TStringList;
  i: Integer;

begin
  Result := False;

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

  try
    try
      ListaIds := TStringList.Create;
      ListaIds.CommaText := IdsTabla;

      //Vamos generando todas y cada una de las marcas de impresion
      for i := 0 to ListaIds.Count - 1 do
      begin
        dsCommand := ASchema.NewCommand(AConn, 'AnadirMarcaImpresion');
        with dsCommand do
        begin
          ParamByName('TABLA').AsString := NombreTabla;
          ParamByName('IDTABLA').AsString := ListaIds.Strings[i];
        end;
        dsCommand.Execute;
        AConn.CommitTransaction;
      end;

      FreeAndNil(ListaIds);
      Result := True;
    except
      RaiseError('Error al aņadir una nueva marca de impresion ' + NombreTabla + ' en tablas');
      AConn.RollbackTransaction;
    end;
  finally
    dsCommand := NIL;
  end;
end;

initialization
  TROClassFactory.Create('srvImpresiones', Create_srvImpresiones, TsrvImpresiones_Invoker);

finalization

end.
