
/////////////////////////////////////////////////////////
//                                                     //
//              Bold for Delphi                        //
//    Copyright (c) 2003 Borland                       //
//                                                     //
/////////////////////////////////////////////////////////

{ Global compiler directives }
{$include bold.inc}
unit BoldPersistenceHandleACR;

interface

uses
  Classes,
  ACRMain,
  BoldDBInterfaces,
  BoldDatabaseAdapterACR,
  BoldPersistenceHandleDB,
  BoldPersistenceHandleDB_deprecated,
  BoldACRInterfaces;

type
  { forward declarations }
  TBoldPersistenceHandleACC = class;

  { TBoldPersistenceHandleACC }
  TBoldPersistenceHandleACC = class(TBoldDBPersistenceHandle)
  private
    FDatabaseName: string;
    FSDDatabase: TACRDatabase;
    FDatabaseAdapter: TBoldACCDatabase;
    FEffectiveDataBase: TACRDatabase;
    function GetEffectiveDatabase: TACRDatabase;
    procedure SetDatabaseAdapter(const Value: TBoldACCDatabase);
    procedure SetDatabase(const Value: TACRDatabase);
    procedure SetDatabaseName(const Value: string);
    procedure SetEffectiveDatabase(const Value: TACRDatabase);
  protected
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
    property DatabaseAdapter: TBoldACCDatabase read FDatabaseAdapter write SetDatabaseAdapter;
    property EffectiveDatabase: TACRDatabase read GetEffectiveDatabase write SetEffectiveDatabase;
    {$IFNDEF T2H}
    procedure InternalTransferproperties(const target: TBoldPersistenceHandleDB); override;
    {$ENDIF}
  public
    destructor destroy; override;
    function GetDataBaseInterface: IBoldDatabase; override;
  published
    property DatabaseName: string read FDatabaseName write SetDatabaseName;
    property SDDatabase: TACRDatabase read FSDDatabase write SetDatabase;
  end deprecated;

implementation

uses
  Dialogs,
  SysUtils,
  BoldRev;

{ TBoldPersistenceHandleACC }

destructor TBoldPersistenceHandleACC.destroy;
begin
  Active := false;
  FreeAndNil(FEffectiveDatabase);
  FreeAndNil(FDatabaseAdapter);
  inherited;
end;

function TBoldPersistenceHandleACC.GetDataBaseInterface: IBoldDatabase;
begin
  if not assigned(FDatabaseAdapter) then
    FDatabaseAdapter := TBoldACCDatabase.create(EffectiveDatabase, SQLDataBaseConfig);
  result := FDataBaseAdapter;
end;

function TBoldPersistenceHandleACC.GetEffectiveDatabase: TACRDatabase;
begin
  if assigned(FSDDatabase) then
    result := FSDDatabase
  else
    raise Exception.CreateFmt('%s.GetEffectiveDatabase: SDDatabase property must be assigned', [ClassName]);
end;

procedure TBoldPersistenceHandleACC.InternalTransferproperties(
  const target: TBoldPersistenceHandleDB);
var
  Adapter: TBoldDatabaseAdapterACR;
  DesInfo: longint;
begin
  inherited;
  DesInfo := Target.DesignInfo;
  if not assigned(Target.DatabaseAdapter) then
  begin
    Target.DatabaseAdapter := TBoldDatabaseAdapterACR.Create(Target.Owner);
    Target.DatabaseAdapter.Name := GetNewComponentName(Target.DatabaseAdapter, 'BoldDatabaseAdapterACC');
    LongRec(DesInfo).Lo := LongRec(DesInfo).lo+16;
    LongRec(DesInfo).Hi := LongRec(DesInfo).hi+16;
    Target.DatabaseAdapter.DesignInfo          := DesInfo;
    showmessage('Created a new DatabaseAdapterACC');
  end
  else if not (target.DatabaseAdapter is tBoldDatabaseAdapterACR) then
    raise Exception.CreateFmt('The persistencehandle is connected to a %s, properties can only be transfered to a TBoldDatabaseAdapterACC', [target.DatabaseAdapter.ClassName] );

  Adapter := target.DatabaseAdapter as tBoldDatabaseAdapterACR;
  if assigned(SDDatabase) then
    Adapter.DataBase := SDDatabase;

  if not assigned(Adapter.Database) then
  begin
    Adapter.DataBase := TACRDatabase.Create(Target.owner);
    Adapter.DataBase.Name := GetNewComponentName(Adapter.DataBase, 'SDDatabase');
    showmessage('Created a new Database');
    LongRec(DesInfo).Lo := LongRec(DesInfo).lo+16;
    LongRec(DesInfo).Hi := LongRec(DesInfo).hi+16;
    Adapter.DataBase.DesignInfo          := DesInfo;
  end;
end;

procedure TBoldPersistenceHandleACC.Notification(AComponent: TComponent; Operation: TOperation);
begin
  inherited;
  if (aComponent = FSDDatabase) and (Operation = opRemove) then
  begin
    if aComponent = EffectiveDatabase then
    begin
      Active := false;
      FDatabaseAdapter := nil;
    end;
    FSDDatabase := nil;
  end;
end;

procedure TBoldPersistenceHandleACC.SetDataBase(const Value: TACRDatabase);
begin
  if FSDDatabase <> Value then
  begin
    if assigned(FEffectiveDatabase) then
    begin
      FreeAndNil(FEffectiveDatabase);
      FreeAndNil(FDatabaseAdapter);
    end;

    FSDDatabase := Value;

    if assigned(FSDDatabase) then
      FSDDatabase.FreeNotification(self);
  end;
end;

procedure TBoldPersistenceHandleACC.SetDatabaseAdapter(const Value: TBoldACCDatabase);
begin
  FDatabaseAdapter := Value;
end;

procedure TBoldPersistenceHandleACC.SetDatabaseName(const Value: string);
begin
  FDatabaseName := Value;
end;

procedure TBoldPersistenceHandleACC.SetEffectiveDataBase(const Value: TACRDatabase);
begin
  FEffectiveDatabase := Value;
end;

initialization

end.