unit uBizContactosServer;

interface

uses
  SysUtils, schContactosServer_Intf, uDAInterfaces,
  uDADataTable, uDABusinessProcessor;

const
  BIZ_SERVER_CONTACTO = 'Server.Contacto';

type
  TBizContactosServer = class(TContactosBusinessProcessorRules)
  protected
    procedure Insert_Datos_Contacto(aChange: TDADeltaChange); virtual;
    procedure Update_Datos_Contacto(aChange: TDADeltaChange); virtual;
    procedure Delete_Datos_Contacto(aChange: TDADeltaChange); virtual;

    procedure AfterProcessChange(Sender: TDABusinessProcessor;
      aChange: TDADeltaChange; Processed: Boolean;
      var CanRemoveFromDelta: Boolean); override;

    procedure ProcessError(Sender: TDABusinessProcessor;
      aChangeType: TDAChangeType; aChange: TDADeltaChange;
      const aCommand: IDASQLCommand; var CanRemoveFromDelta: Boolean;
      Error: Exception); override;


  end;

implementation

uses
  Dialogs, uDataModuleServer, uDAClasses, DARemoteService_Impl,
  schContactosClient_Intf, uBusinessUtils;

{ TBizContactosServer }

procedure TBizContactosServer.AfterProcessChange(Sender: TDABusinessProcessor;
  aChange: TDADeltaChange; Processed: Boolean; var CanRemoveFromDelta: Boolean);
begin
  inherited;

  case aChange.ChangeType of
    ctInsert: Insert_Datos_Contacto(aChange);
    ctUpdate: Update_Datos_Contacto(aChange);
    ctDelete: Delete_Datos_Contacto(aChange);
  end;

  CanRemoveFromDelta := True;
end;

procedure TBizContactosServer.Delete_Datos_Contacto(aChange: TDADeltaChange);
var
  ASchema : TDASchema;
  ACurrentConn : IDAConnection;
  ACommand : IDASQLCommand;
begin
  ASchema := BusinessProcessor.Schema;
  ACurrentConn := GetBusinessProcessorConnection(BusinessProcessor);

  ACommand := ASchema.NewCommand(ACurrentConn, 'Delete_ContactosEmpresas');
  try
    with ACommand do
    begin
      ParamByName('OLD_ID_CONTACTO').Value := aChange.OldValueByName[fld_ContactosID];
      ParamByName('OLD_ID_EMPRESA').Value := aChange.OldValueByName[fld_ContactosID_EMPRESA];
      Execute;
    end;
  finally
    ACommand := NIL;
  end;
end;

procedure TBizContactosServer.Insert_Datos_Contacto(aChange: TDADeltaChange);
var
  ASchema : TDASchema;
  ACurrentConn : IDAConnection;
  ACommand : IDASQLCommand;
begin
  ASchema := BusinessProcessor.Schema;
  ACurrentConn := GetBusinessProcessorConnection(BusinessProcessor);

  ACommand := ASchema.NewCommand(ACurrentConn, 'Insert_ContactosEmpresas');
  try
    with ACommand do
    begin
      ParamByName('ID_CONTACTO').Value := aChange.NewValueByName[fld_ContactosID];
      ParamByName('ID_EMPRESA').Value := aChange.NewValueByName[fld_ContactosID_EMPRESA];
      Execute;
    end;
  finally
    ACommand := NIL;
  end;
end;

procedure TBizContactosServer.ProcessError(Sender: TDABusinessProcessor;
  aChangeType: TDAChangeType; aChange: TDADeltaChange;
  const aCommand: IDASQLCommand; var CanRemoveFromDelta: Boolean;
  Error: Exception);
begin
  inherited;
  //IMPORTANTE ESTO HACE QUE EL CLIENTE SE ENTERE DEL ERROR Y LOS BP ASOCIADOS EN EL
  //SCHEMA HAGAN ROLLBACK TAMBIEN
  CanRemoveFromDelta := True;
  raise Exception.Create(Error.Message);
end;

procedure TBizContactosServer.Update_Datos_Contacto(aChange: TDADeltaChange);
var
  ASchema : TDASchema;
  ACurrentConn : IDAConnection;
  ACommand : IDASQLCommand;
begin
  ASchema := BusinessProcessor.Schema;
  ACurrentConn := GetBusinessProcessorConnection(BusinessProcessor);

  ACommand := ASchema.NewCommand(ACurrentConn, 'Update_ContactosEmpresas');
  try
    with ACommand do
    begin
      ParamByName('ID_CONTACTO').Value := aChange.NewValueByName[fld_ContactosID];
      ParamByName('OLD_ID_CONTACTO').Value := aChange.OldValueByName[fld_ContactosID];

      ParamByName('ID_EMPRESA').Value := aChange.NewValueByName[fld_ContactosID_EMPRESA];
      ParamByName('OLD_ID_EMPRESA').Value := aChange.OldValueByName[fld_ContactosID_EMPRESA];
      Execute;
    end;
  finally
    ACommand := NIL;
  end;
end;

initialization
  RegisterBusinessProcessorRules(BIZ_SERVER_CONTACTO, TBizContactosServer);

end.
