unit uBizOscManufacturerServer;

interface

uses
  schTiendaWebServer_Intf, uDAInterfaces, SysUtils,
  uDADataTable, uDABusinessProcessor, uBusinessUtils;

const
  BIZ_SERVER_OSC_MANUFACTURER= 'Server.OSC.Manufacturer';

type
  TBizOscManufacturerServer = class(Tosc_ManufacturersBusinessProcessorRules)
  protected
    procedure Insert_Manufacturers_Info(aChange: TDADeltaChange);
    procedure Update_Manufacturers_Info(aChange: TDADeltaChange);
    procedure Delete_Manufacturers_Info(aChange: TDADeltaChange);

    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, Variants, uDataModuleServer, uDAClasses, DARemoteService_Impl,
  schTiendaWebClient_Intf, uROClasses;

{ TBizOscManufacturerServer }

procedure TBizOscManufacturerServer.AfterProcessChange(Sender: TDABusinessProcessor;
  aChange: TDADeltaChange; Processed: Boolean; var CanRemoveFromDelta: Boolean);
var
  ASchema : TDASchema;
  ACurrentConn : IDAConnection;
  ADataset : IDADataset;
begin
  ASchema := Sender.Schema;
  ACurrentConn := GetBusinessProcessorConnection(Sender);

  inherited;

  case aChange.ChangeType of
    ctInsert: begin
                ADataSet := ASchema.NewDataset(ACurrentConn, 'GetLastID', [], [], True);
                aChange.NewValueByName[fld_osc_Manufacturersmanufacturers_id] := ADataSet.FieldByName('last_id').AsInteger;
                Insert_Manufacturers_Info(aChange);
              end;

    ctUpdate: Update_Manufacturers_Info(aChange);
    ctDelete: Delete_Manufacturers_Info(aChange);
  end;

  CanRemoveFromDelta := False;
end;

procedure TBizOscManufacturerServer.Delete_Manufacturers_Info(
  aChange: TDADeltaChange);
var
  ASchema : TDASchema;
  ACurrentConn : IDAConnection;
  ACommand : IDASQLCommand;
  AIdiomasDataSet : IDADataset;
  I: Integer;
begin
  // Borramos una fila por cada idioma que hay en OsCommerce y
  // por cada fabricante que hay.  
  ASchema := BusinessProcessor.Schema;
  ACurrentConn := GetBusinessProcessorConnection(BusinessProcessor);

  ACommand := ASchema.NewCommand(ACurrentConn, 'Delete_osc_Manufacturers_info');
  AIdiomasDataSet := ASchema.NewDataset(ACurrentConn, nme_osc_Languages);
  try
    AIdiomasDataSet.Open;
    for I := 0 to AIdiomasDataSet.RecordCount - 1 do
    begin
      with ACommand do
      begin
        ParamByName('OLD_manufacturers_id').Value := aChange.OldValueByName[fld_osc_Manufacturersmanufacturers_id];
        ParamByName('OLD_languages_id').Value := AIdiomasDataSet.FieldByName(fld_osc_Languageslanguages_id).Value;
        Execute;
      end;
      AIdiomasDataSet.Next;
    end;
    AIdiomasDataSet.Close;
  finally
    ACommand := NIL;
    AIdiomasDataSet := NIL;
  end;
end;

procedure TBizOscManufacturerServer.Insert_Manufacturers_Info(
  aChange: TDADeltaChange);
var
  ASchema : TDASchema;
  ACurrentConn : IDAConnection;
  ACommand : IDASQLCommand;
  AIdiomasDataSet : IDADataset;
  I: Integer;
begin
  // Aquí insertamos una fila por cada idioma que hay en OsCommerce y
  // por cada fabricante que hay. Se hace para que no falle la tienda 
  ASchema := BusinessProcessor.Schema;
  ACurrentConn := GetBusinessProcessorConnection(BusinessProcessor);

  ACommand := ASchema.NewCommand(ACurrentConn, 'Insert_osc_Manufacturers_info');
  AIdiomasDataSet := ASchema.NewDataset(ACurrentConn, nme_osc_Languages);
  try
    AIdiomasDataSet.Open;
    for I := 0 to AIdiomasDataSet.RecordCount - 1 do
    begin
      with ACommand do
      begin
        ParamByName('manufacturers_id').Value := aChange.NewValueByName[fld_osc_Manufacturersmanufacturers_id];
        ParamByName('languages_id').Value := AIdiomasDataSet.FieldByName(fld_osc_Languageslanguages_id).Value;
        ParamByName('manufacturers_url').Value := '';
        ParamByName('url_clicked').Value := 0;
        ParamByName('date_last_click').Value := null;
        Execute;
      end;
      AIdiomasDataSet.Next;
    end;
    AIdiomasDataSet.Close;
  finally
    ACommand := NIL;
    AIdiomasDataSet := NIL;
  end;
end;

procedure TBizOscManufacturerServer.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 TBizOscManufacturerServer.Update_Manufacturers_Info(
  aChange: TDADeltaChange);
begin
  // Aquí no hace falta hacer nada por que la información
  // que hay en manufacturers_info no la necesitamos.
end;

initialization
  RegisterBusinessProcessorRules(BIZ_SERVER_OSC_MANUFACTURER, TBizOscManufacturerServer);

end.
