unit srvConfiguracion_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;

type
  { TsrvConfiguracion }
  TsrvConfiguracion = class(TDataAbstractService, IsrvConfiguracion)
    Bin2DataStreamer: TDABin2DataStreamer;
    schConfiguracion: TDASchema;
    procedure DARemoteServiceCreate(Sender: TObject);
    procedure DataAbstractServiceBeforeAcquireConnection(aSender: TObject;
      var aConnectionName: string);
  protected
    { IsrvConfiguracion methods }
    function DarValor(const CODIGO: String; const ValorPorDefecto: String = ''): String;
  end;

implementation

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

procedure Create_srvConfiguracion(out anInstance : IUnknown);
begin
  anInstance := TsrvConfiguracion.Create(NIL);
end;

{ srvConfiguracion }
procedure TsrvConfiguracion.DARemoteServiceCreate(Sender: TObject);
begin
  //SessionManager := dmServer.SessionManager;
end;

function TsrvConfiguracion.DarValor(const CODIGO: String; const ValorPorDefecto: String = ''): String;
var
   ADataSet : IDADataset;
   ACurrentConn : IDAConnection;
   ACommand : IDASQLCommand;
begin
  try
    ADataSet := schConfiguracion.NewDataset(Connection, 'darValor', ['CODIGO'], [CODIGO]);
    ADataSet.Open;
    if ADataSet.Dataset.RecordCount > 0 then
       Result := ADataSet.Dataset.Fields[0].AsVariant
    else
    begin
       Result := ValorPorDefecto;
       //Creamos la variable de configuración con su valor por defecto
       ACurrentConn := dmServer.ConnectionManager.NewConnection(dmServer.ConnectionManager.GetDefaultConnectionName);
       ACommand := schConfiguracion.NewCommand(ACurrentConn, 'InsertarValor');
       try
         with ACommand do
         begin
           ParamByName('CODIGO').Value := CODIGO;
           ParamByName('VALOR').Value := ValorPorDefecto;
           ParamByName('ID_EMPRESA').Value := Null;
           Execute;
           ACurrentConn.CommitTransaction;
         end;
       except
         ACurrentConn.RollbackTransaction;
       end;
       ACommand := NIL;       
//       RaiseError('Falta variable de configuracion: ' + CODIGO);
    end;
  finally
    ADataSet.Close;
  end;
end;

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

initialization
  TROClassFactory.Create('srvConfiguracion', Create_srvConfiguracion, TsrvConfiguracion_Invoker);

finalization

end.
