unit Aurelius.Plugin;

interface

uses
  SysUtils, UITypes, Classes, Generics.Collections,
  Aurelius.Drivers.Interfaces, Aurelius.Plugin.Options;

type
  TAureliusPlugin = class
  strict private
    FConnection: IDBConnection;
    FSourceCode: string;
    procedure InternalLoadPluginLibrary;
    function GetInstallDir: string;
  public
    constructor Create(AConnection: IDBConnection);
    destructor Destroy; override;
    function Execute(APluginOptions: IASPluginOptions): Boolean;
    property SourceCode: string read FSourceCode;
  end;

implementation

uses
  IOUtils, Registry,

  Aurelius.Plugin.Connection,
  Aurelius.Plugin.ExportForm,
  Aurelius.Plugin.Import,
  Aurelius.Plugin.LoadingForm,
  Aurelius.Plugin.Types,
  Aurelius.Plugin.Database;

{ TAureliusPlugin }

constructor TAureliusPlugin.Create(AConnection: IDBConnection);
begin
  FConnection := AConnection;
end;

destructor TAureliusPlugin.Destroy;
begin
  UnloadAureliusPluginLibrary;
  inherited;
end;

function TAureliusPlugin.Execute(APluginOptions: IASPluginOptions): Boolean;
var
  PluginConnection: IASPluginConnection;
  LoadingForm: TfmLoading;
  ExportForm: TfmAureliusExport;
  Dictionary: HDICTIONARY;
  Database: IPluginDatabase;
begin
  InternalLoadPluginLibrary;

  Result := False;
  PluginConnection := TASPluginConnection.Create(FConnection);
  Dictionary := nil;
  PluginCheck(CreateDictionary(FConnection.SqlDialect, Dictionary));
  try
    LoadingForm := TfmLoading.Create(PluginConnection, Dictionary);
    try
      if LoadingForm.ShowModal = mrOk then
      begin
        Database := nil;
        PluginCheck(RetrieveDatabase(Dictionary, Database));
        ExportForm := TfmAureliusExport.Create(Dictionary, Database, APluginOptions);
        try
          Result := ExportForm.ShowModal = mrOk;
          if Result then
            FSourceCode := ExportForm.Source;
        finally
          FreeAndNil(ExportForm);
          Database := nil;
        end;
      end;
    finally
      LoadingForm.Release;
    end;
  finally
    FreeDictionary(Dictionary);
    UnloadAureliusPluginLibrary;
  end;
end;

function TAureliusPlugin.GetInstallDir: string;
var
  Reg: TRegistry;
begin
  Result := '';
  Reg := TRegistry.Create;
  try
    if Reg.OpenKey('software\tmssoftware\TMS Aurelius', False) then
      if Reg.ValueExists('InstallDir') then
        Result := Reg.ReadString('InstallDir');
  finally
    Reg.Free;
  end;
end;

procedure TAureliusPlugin.InternalLoadPluginLibrary;
var
  LibFile: string;
  InstallDir: string;
begin
  {$IFDEF DEBUG}
  LibFile := '..\..\..\Win32\Debug\AureliusPluginLibrary.dll';
  if FileExists(LibFile) then
    if LoadAureliusPluginLibrary(LibFile) then
      Exit
    else
      RaiseLastOSError;
  {$ENDIF}

  InstallDir := GetInstallDir;
  LibFile := TPath.Combine(InstallDir, 'bin\AureliusPluginLibrary.dll');
  if FileExists(LibFile) then
  begin
    if not LoadAureliusPluginLibrary(LibFile) then
      RaiseLastOSError;
  end
  else
  if not LoadAureliusPluginLibrary then
    RaiseLastOSError;
end;

end.