unit srvTiendaWeb_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, uDAScriptingProvider, uDABusinessProcessor,
  uDAClasses, uDADataTable, uDABINAdapter,
  uDAInterfaces;

const
  MYSQL_DATEFORMAT = 'yyyy-mm-dd hh:mm:ss';

type
  { TsrvTiendaWeb }
  TsrvTiendaWeb = class(TDARemoteService, IsrvTiendaWeb)
    DABINAdapter: TDABINAdapter;
    Diagrams: TDADiagrams;
    bpTiendaWeb: TDABusinessProcessor;
    bp_osc_Customers: TDABusinessProcessor;
    bp_osc_AddressBook: TDABusinessProcessor;
    bp_osc_Manufacturers: TDABusinessProcessor;
    bp_osc_Products: TDABusinessProcessor;
    bp_osc_Orders: TDABusinessProcessor;
    bp_osc_Orders_status_history: TDABusinessProcessor;
    schTiendaWeb: TDASchema;
    DataDictionary: TDADataDictionary;
    procedure DARemoteServiceBeforeGetDatasetData(const Dataset: IDADataset;
      const IncludeSchema: Boolean; const MaxRecords: Integer);
    procedure DARemoteServiceCreate(Sender: TObject);
    procedure bp_osc_CustomersGenerateSQL(Sender: TDABusinessProcessor;
      ChangeType: TDAChangeType; const ReferencedStatement: TDAStatement;
      const aDelta: IDADelta; var SQL: string);
  private
    procedure AsignarConexion (const AConnectionName: string);
  protected
    function GetNextAutoInc(const GeneratorName: String): Integer;
    function GetNextAutoIncOSC(const TableName: String): Integer;
    function GetDatasetSchemaOSC(const aDatasetName: String): Binary;
    function GetDatasetDataExOSC(const DatasetName: String; const Params: TDADatasetParamArray; const UserFilter: String; const IncludeSchema: Boolean;
                                 const MaxRecords: Integer): Binary;
    function UpdateDataOSC(const Delta: Binary): Binary;
  end;

implementation

{$R *.dfm}
uses
  {Generated:} FactuGES_Invk, uDataModuleServer, schTiendaWebClient_Intf,
  uDatabaseUtils, uRestriccionesUsuarioUtils, Dialogs, uBizTiendaWebServer,
  uBizOscCustomerServer, uBizOscAddressBookServer, uBizOscManufacturerServer,
  uBizOscProductServer, uUsersManager, uBizOscOrderServer;

procedure Create_srvTiendaWeb(out anInstance : IUnknown);
begin
  anInstance := TsrvTiendaWeb.Create(NIL);
end;

{ srvTiendaWeb }
procedure TsrvTiendaWeb.AsignarConexion(const AConnectionName: string);
begin
  // En el caso de conexión MySQL también solicito otra conexión nueva
  // para evitar problemas.
  if (AConnectionName <> Self.ConnectionName) or (AConnectionName = 'MySQL')  then
  begin
    if Connection.InTransaction then
      Connection.CommitTransaction;

    Self.ConnectionName := AConnectionName;
    Connection := dmServer.ConnectionManager.NewConnection(AConnectionName, False);
  end;
end;

procedure TsrvTiendaWeb.bp_osc_CustomersGenerateSQL(
  Sender: TDABusinessProcessor; ChangeType: TDAChangeType;
  const ReferencedStatement: TDAStatement; const aDelta: IDADelta;
  var SQL: string);
begin
  dmServer.EscribirLog(SQL + #10#13 + #10#13);
end;

procedure TsrvTiendaWeb.DARemoteServiceBeforeGetDatasetData(
  const Dataset: IDADataset; const IncludeSchema: Boolean;
  const MaxRecords: Integer);
begin
  if DataSet.Name = nme_TiendaWeb then
  begin
    { Aquí se asegura que el usuario sólo accede a los RemesasCliente
      de las empresas a las que tiene permiso para acceder
      filtrando DataSet por ID_EMPRESA. }
    FiltrarAccesoUsuario(Session, dmServer.DarNuevaConexion, schTiendaWeb, DataSet, fld_TiendaWebID_EMPRESA);
  end;
end;

procedure TsrvTiendaWeb.DARemoteServiceCreate(Sender: TObject);
begin
  SessionManager := dmServer.SessionManager;
  bpTiendaWeb.BusinessRulesID := BIZ_SERVER_TIENDA_WEB;
  bp_osc_Customers.BusinessRulesID := BIZ_SERVER_OSC_CUSTOMER;
  bp_osc_AddressBook.BusinessRulesID := BIZ_SERVER_OSC_ADDRESSBOOK;
  bp_osc_Manufacturers.BusinessRulesID := BIZ_SERVER_OSC_MANUFACTURER;
  bp_osc_Products.BusinessRulesID := BIZ_SERVER_OSC_PRODUCT;
  bp_osc_Orders.BusinessRulesID := BIZ_SERVER_OSC_ORDER;
end;

function TsrvTiendaWeb.GetDatasetDataExOSC(const DatasetName: String;
  const Params: TDADatasetParamArray; const UserFilter: String;
  const IncludeSchema: Boolean; const MaxRecords: Integer): Binary;
begin
  AsignarConexion('MySQL');
  Result := GetDatasetDataEx(DatasetName, Params, UserFilter, IncludeSchema, MaxRecords);
end;

function TsrvTiendaWeb.GetDatasetSchemaOSC(const aDatasetName: String): Binary;
begin
  AsignarConexion('MySQL');
  Result := GetDatasetSchema(aDatasetName);
end;

function TsrvTiendaWeb.GetNextAutoInc(const GeneratorName: String): Integer;
begin
  Result := uDatabaseUtils.GetNextAutoInc(GeneratorName)
end;

function TsrvTiendaWeb.GetNextAutoIncOSC(const TableName: String): Integer;
var
  vConn : IDAConnection;
  ds: IDADataset;
begin
  vConn := dmServer.ConnectionManager.NewConnection('MySQL');
  vConn.BeginTransaction;
  try
    ds := vConn.NewDataset(Format('SHOW TABLE STATUS WHERE NAME=''%s''', [TableName]));
    ds.Open;
    Result := ds.FieldByName('Auto_increment').AsInteger;
  finally
    vConn.CommitTransaction;
    ds := NIL;
  end;
end;

function TsrvTiendaWeb.UpdateDataOSC(const Delta: Binary): Binary;
begin
  AsignarConexion('MySQL');
  Result := UpdateData(Delta);
end;

initialization
  TROClassFactory.Create('srvTiendaWeb', Create_srvTiendaWeb, TsrvTiendaWeb_Invoker);

finalization

end.
