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;

type
  { TsrvObras }
  TsrvObras = class(TDataAbstractService, IsrvObras)
    Diagrams: TDADiagrams;
    Bin2DataStreamer: TDABin2DataStreamer;
    bpObras: TDABusinessProcessor;
    bpObrasEjecuciones: TDABusinessProcessor;
    bpObrasEjecucionesPresupuestos: TDABusinessProcessor;
    bpObrasEjecucionesPedidosProv: TDABusinessProcessor;
    schObras: TDASchema;
    DataDictionary: TDADataDictionary;
    procedure DataAbstractServiceAfterAcquireConnection(aSender: TObject;
      const aConnectionName: string;
      const aAcquiredConnection: IDAConnection);
    procedure DataAbstractServiceAfterExecuteCommand(aSender: TObject;
      const aCommand: IDASQLCommand; aRowsAffacted: Integer);
    procedure DataAbstractServiceAfterGetDatasetSchema(aSender: TObject;
      const aDataset: IDADataset);
    procedure DataAbstractServiceAfterProcessDeltas(aSender: TObject;
      aDeltaStructs: TDADeltaStructList);
    procedure DataAbstractServiceAfterReleaseConnection(aSender: TObject;
      const aConnectionName: string);
    procedure DataAbstractServiceBeforeExecuteCommand(aSender: TObject;
      const aCommand: IDASQLCommand);
    procedure DataAbstractServiceBeforeGetDatasetData(aSender: TObject;
      const aDataset: IDADataset; const aIncludeSchema: Boolean;
      const aMaxRecords: Integer);
    procedure DataAbstractServiceBeforeGetDatasetSchema(aSender: TObject;
      const aDataset: IDADataset);
    procedure DataAbstractServiceBeforeProcessDeltas(aSender: TObject;
      aDeltaStructs: TDADeltaStructList);
    procedure DataAbstractServiceBeforeReleaseConnection(aSender: TObject;
      const aConnectionName: string;
      const aAcquiredConnection: IDAConnection);
    procedure DataAbstractServiceGetSchemaAsXMLEvent(aSender: TObject;
      var aSchemaXML: string);
    procedure DataAbstractServiceProcessDeltasError(aSender: TObject;
      aDeltaStructs: TDADeltaStructList; aError: Exception;
      var aDoRaise: Boolean);
    procedure DataAbstractServiceUpdateDataBeginTransaction(
      Sender: TObject; var aUseDefaultTransactionLogic: Boolean);
    procedure DataAbstractServiceUpdateDataCommitTransaction(
      Sender: TObject; var aUseDefaultTransactionLogic: Boolean);
    procedure DataAbstractServiceUpdateDataRollBackTransaction(
      Sender: TObject; var aUseDefaultTransactionLogic: Boolean);
    procedure DataAbstractServiceValidateCommandExecution(Sender: TObject;
      const aConnection: IDAConnection; const aDatasetName: string;
      const aParamNames: array of string;
      const aParamValues: array of Variant; aSchema: TDASchema;
      var Allowed: Boolean);
    procedure DataAbstractServiceValidateDatasetAccess(Sender: TObject;
      const aConnection: IDAConnection; const aDatasetName: string;
      const aParamNames: array of string;
      const aParamValues: array of Variant; aSchema: TDASchema;
      var Allowed: Boolean);
    procedure DataAbstractServiceValidateDirectSQLAccess(Sender: TObject;
      const aConnection: IDAConnection; const aSQLText: string;
      const aParamNames: array of string;
      const aParamValues: array of Variant; var Allowed: Boolean);
    procedure DataAbstractServiceAcquireConnectionFailure(aSender: TObject;
      const aConnectionName: string; aError: Exception);
    procedure DataAbstractServiceDestroy(Sender: TObject);
    procedure DataAbstractServiceActivate(const aClientID: TGUID;
      aSession: TROSession; const aMessage: IROMessage);
    procedure DataAbstractServiceDeactivate(const aClientID: TGUID;
      aSession: TROSession);
    procedure DataAbstractServiceBusinessProcessorAutoCreated(
      aSender: TRORemoteDataModule;
      BusinessProcessor: TDABusinessProcessor);

    procedure DataAbstractServiceBeforeAcquireConnection(aSender: TObject;
      var aConnectionName: string);
    procedure DARemoteServiceAfterGetDatasetData(const Dataset: IDADataset;
      const IncludeSchema: Boolean; const MaxRecords: Integer);
    procedure DARemoteServiceCreate(Sender: TObject);
    procedure DataAbstractServiceAfterGetDatasetData(aSender: TObject;
      const aDataset: IDADataset; const aIncludeSchema: Boolean;
      const aMaxRecords: Integer);
  private
    procedure Log(Astr: string);
  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
//  Log('***Create***');
//  Log('************');
//  Log('');

  SessionManager := dmServer.SessionManager;
  bpObras.BusinessRulesID := BIZ_SERVER_OBRA;
end;

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

//  Log('***BeforeAcquireConnection***');
//  Log('aConnectionName:'#9 + aConnectionName);
//  Log('************************');
//  Log('');
end;


procedure TsrvObras.DataAbstractServiceAfterAcquireConnection(
  aSender: TObject; const aConnectionName: string;
  const aAcquiredConnection: IDAConnection);
begin
  inherited;
//  Log('***AfterAcquireConnection***');
//  Log('ConnectionName:'#9 + aConnectionName);
//  Log('aAcquiredConnection.Name:'#9 + aAcquiredConnection.Name);
//  Log('aAcquiredConnection.ConnectionString:'#9 + aAcquiredConnection.ConnectionString);
//  Log('****************************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceAfterExecuteCommand(
  aSender: TObject; const aCommand: IDASQLCommand; aRowsAffacted: Integer);
begin
  inherited;
//  Log('***AfterExecuteCommand***');
//  Log('aCommand.Name:'#9 + aCommand.Name);
//  Log('aCommand.SQL:'#9 + aCommand.SQL);
//  Log('aRowsAffacted:'#9 + IntToStr(aRowsAffacted));
//  Log('*************************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceAfterGetDatasetData(
  aSender: TObject; const aDataset: IDADataset;
  const aIncludeSchema: Boolean; const aMaxRecords: Integer);
begin
  inherited;
//  Log('***AfterGetDatasetData***');
//  Log('aDataset.Name:'#9 + aDataset.Name);
//  Log('aDataset.SQL:'#9 + aDataset.SQL);
//  Log('aIncludeSchema:'#9 + BoolStr[aIncludeSchema]);
//  Log('aMaxRecords:'#9 + IntToStr(aMaxRecords));
//  Log('*************************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceAfterGetDatasetSchema(
  aSender: TObject; const aDataset: IDADataset);
begin
  inherited;
//  Log('***AfterGetDatasetSchema***');
//  Log('aDataset.Name:'#9 + aDataset.Name);
//  Log('aDataset.SQL:'#9 + aDataset.SQL);
//  Log('***************************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceAfterProcessDeltas(
  aSender: TObject; aDeltaStructs: TDADeltaStructList);
begin
  inherited;
//  Log('***AfterProcessDeltas***');
//  Log('aDeltaStructs.Count:'#9 + IntToStr(aDeltaStructs.Count));
//  Log('************************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceAfterReleaseConnection(
  aSender: TObject; const aConnectionName: string);
begin
  inherited;
//  Log('***AfterReleaseConnection***');
//  Log('aConnectionName:'#9 + aConnectionName);
//  Log('****************************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceBeforeExecuteCommand(
  aSender: TObject; const aCommand: IDASQLCommand);
begin
  inherited;
//  Log('***BeforeExecuteCommand***');
//  Log('aCommand.Name:'#9 + aCommand.Name);
//  Log('aCommand.SQL:'#9 + aCommand.SQL);
//  Log('************************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceBeforeGetDatasetData(
  aSender: TObject; const aDataset: IDADataset;
  const aIncludeSchema: Boolean; const aMaxRecords: Integer);
begin
  inherited;
//  Log('***BeforeGetDatasetData***');
//  Log('aDataset.Name:'#9 + aDataset.Name);
//  Log('aDataset.SQL:'#9 + aDataset.SQL);
//  Log('aIncludeSchema:'#9 + BoolStr[aIncludeSchema]);
//  Log('aMaxRecords:'#9 + IntToStr(aMaxRecords));
//  Log('*************************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceBeforeGetDatasetSchema(
  aSender: TObject; const aDataset: IDADataset);
begin
  inherited;
//  Log('***BeforeGetDatasetSchema***');
//  Log('aDataset.Name:'#9 + aDataset.Name);
//  Log('aDataset.SQL:'#9 + aDataset.SQL);
//  Log('****************************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceBeforeProcessDeltas(
  aSender: TObject; aDeltaStructs: TDADeltaStructList);
begin
  inherited;
//  Log('***BeforeProcessDeltas***');
//  Log('aDeltaStructs.Count:'#9 + IntToStr(aDeltaStructs.Count));
//  Log('*************************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceBeforeReleaseConnection(
  aSender: TObject; const aConnectionName: string;
  const aAcquiredConnection: IDAConnection);
begin
  inherited;
//  Log('***BeforeReleaseConnection***');
//  Log('aConnectionName:'#9 + aConnectionName);
//  Log('aAcquiredConnection.Name:'#9 + aAcquiredConnection.Name);
//  Log('aAcquiredConnection.ConnectionString:'#9 + aAcquiredConnection.ConnectionString);
//  Log('*****************************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceGetSchemaAsXMLEvent(
  aSender: TObject; var aSchemaXML: string);
begin
  inherited;
//  Log('***GetSchemaAsXMLEvent***');
//  Log('Length(aSchemaXML):'#9 + intTostr(Length(aSchemaXML)));
//  Log('*************************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceProcessDeltasError(
  aSender: TObject; aDeltaStructs: TDADeltaStructList; aError: Exception;
  var aDoRaise: Boolean);
begin
  inherited;
//  Log('***ProcessDeltasError***');
//  Log('aDeltaStructs.Count:'#9 + IntToStr(aDeltaStructs.Count));
//  Log('aError.ClassName:'#9 + aError.ClassName);
//  Log('aError.Message:'#9 + aError.Message);
//  Log('aDoRaise:'#9 + BoolStr[aDoRaise]);
//  Log('*************************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceUpdateDataBeginTransaction(
  Sender: TObject; var aUseDefaultTransactionLogic: Boolean);
begin
  inherited;
//  Log('***UpdateDataBeginTransaction***');
//  Log('aUseDefaultTransactionLogic:'#9 + BoolStr[aUseDefaultTransactionLogic]);
//  Log('********************************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceUpdateDataCommitTransaction(
  Sender: TObject; var aUseDefaultTransactionLogic: Boolean);
begin
  inherited;
//  Log('***UpdateDataCommitTransaction***');
//  Log('aUseDefaultTransactionLogic:'#9 + BoolStr[aUseDefaultTransactionLogic]);
//  Log('*********************************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceUpdateDataRollBackTransaction(
  Sender: TObject; var aUseDefaultTransactionLogic: Boolean);
begin
  inherited;
//  Log('***UpdateDataRollBackTransaction***');
//  Log('aUseDefaultTransactionLogic:'#9 + BoolStr[aUseDefaultTransactionLogic]);
//  Log('***********************************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceValidateCommandExecution(
  Sender: TObject; const aConnection: IDAConnection;
  const aDatasetName: string; const aParamNames: array of string;
  const aParamValues: array of Variant; aSchema: TDASchema;
  var Allowed: Boolean);
var
  i: integer;
  SQLCommand: TDASQLCommand;
begin
  inherited;
//  Log('***ValidateCommandExecution***');
//  Log('aConnection.Name:'#9 + aConnection.Name);
//  Log('aDatasetName:'#9 + aDatasetName);
//  SQLCommand := aSchema.Commands.SQLCommandByName(aDatasetName);
//  if (SQLCommand <> nil) and (SQLCommand.Statements.Count > 0) then
//    Log('Command SQL:'#9 + SQLCommand.Statements[0].SQL);
//  Log('ParamCount:'#9 + intToStr(1 + ord(High(aParamNames)) - ord(Low(aParamNames))));
//  for i := Low(aParamNames) to High(aParamNames) do
//    Log(#9 + aParamNames[i] + ' = ' + VarToStr(aParamValues[i]));
//  Log('aSchema.Name:'#9 + aSchema.Name);
//  Log('Allowed:'#9 + BoolStr[Allowed]);
//  Log('******************************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceValidateDatasetAccess(
  Sender: TObject; const aConnection: IDAConnection;
  const aDatasetName: string; const aParamNames: array of string;
  const aParamValues: array of Variant; aSchema: TDASchema;
  var Allowed: Boolean);
var
  i: integer;
begin
  inherited;
//  Log('***ValidateDatasetAccess***');
//  Log('aConnection.Name:'#9 + aConnection.Name);
//  Log('aDatasetName:'#9 + aDatasetName);
//  Log('ParamCount:'#9 + intToStr(1 + ord(High(aParamNames)) - ord(Low(aParamNames))));
//  for i := Low(aParamNames) to High(aParamNames) do
//    Log(#9 + aParamNames[i] + ' = ' + VarToStr(aParamValues[i]));
//  Log('aSchema.Name:'#9 + aSchema.Name);
//  Log('Allowed:'#9 + BoolStr[Allowed]);
//  Log('***************************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceValidateDirectSQLAccess(
  Sender: TObject; const aConnection: IDAConnection;
  const aSQLText: string; const aParamNames: array of string;
  const aParamValues: array of Variant; var Allowed: Boolean);
var
  i: integer;
begin
  inherited;
//  Log('***ValidateDirectSQLAccess***');
//  Log('aConnection.Name:'#9 + aConnection.Name);
//  Log('aSQLText:'#9 + aSQLText);
//  Log('ParamCount:'#9 + intToStr(1 + ord(High(aParamNames)) - ord(Low(aParamNames))));
//  for i := Low(aParamNames) to High(aParamNames) do
//    Log(#9 + aParamNames[i] + ' = ' + VarToStr(aParamValues[i]));
//  Log('Allowed:'#9 + BoolStr[Allowed]);
//  Log('***************************');
//  Log('');
end;

procedure TsrvObras.Log(Astr: string);
begin
  dmServer.EscribirLog(Astr)
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;

procedure TsrvObras.DataAbstractServiceAcquireConnectionFailure(
  aSender: TObject; const aConnectionName: string; aError: Exception);
begin
  inherited;
//  Log('***AcquireConnectionFailure***');
//  Log('aConnectionName:'#9 + aConnectionName);
//  Log('aError.ClassName:'#9 + aError.ClassName);
//  Log('aError.Message:'#9 + aError.Message);
//  Log('******************************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceDestroy(
  Sender: TObject);
begin
//  Log('***Destroy***');
//  Log('*************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceActivate(
  const aClientID: TGUID; aSession: TROSession;
  const aMessage: IROMessage);
begin
//  Log('***Activate***');
//  Log('aClientID:'#9 + GUIDToString(aClientID));
//  //  Log('aSession:'#9 + aSession.ClassName);
//  //  Log('aError.Message:'#9 + aError.Message);
//  Log('**************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceDeactivate(
  const aClientID: TGUID; aSession: TROSession);
begin
//  Log('***Deactivate***');
//  Log('aClientID:'#9 + GUIDToString(aClientID));
//  Log('****************');
//  Log('');
end;

procedure TsrvObras.DataAbstractServiceBusinessProcessorAutoCreated(
  aSender: TRORemoteDataModule; BusinessProcessor: TDABusinessProcessor);
begin
//  Log('***BusinessProcessorAutoCreated***');
//  Log('****************');
//  Log('');
end;


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

finalization

end.
