unit uEtiquetasContactosReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModuleEtiquetasContactosReport,
  uContactosController;

type
  IEtiquetasContactosReportController = interface
  ['{B8EB378E-E40E-4AFE-8CC9-C34DFB13B9EA}']
    procedure Preview(const AID : String);
    function Print(const AID : String): Boolean;
  end;

  TEtiquetasContactosReportController = class(TInterfacedObject, IEtiquetasContactosReportController)
  private
    FDataModule : IDataModuleEtiquetasContactosReport;
    function CreateEditor(const AName: String; const IID: TGUID; out Intf): Boolean;

  public
    constructor Create;
    destructor Destroy; override;

    procedure Preview(const AID : String);
    function Print(const AID : String): Boolean;
  end;


implementation

uses
  uROTypes, uEditorRegistryUtils, uIEditorEtiquetasContactosPreview,
  uEditorPreview, uDataModuleContactos, uEditorBase, cxControls;

{ TEtiquetasContactosReportController }

constructor TEtiquetasContactosReportController.Create;
begin
  FDataModule := TDataModuleContactos.Create(Nil);
end;

function TEtiquetasContactosReportController.CreateEditor(const AName: String;
  const IID: TGUID; out Intf): Boolean;
begin
  Result := Supports(EditorRegistry.CreateEditor(AName), IID, Intf);
end;


destructor TEtiquetasContactosReportController.Destroy;
begin
  FDataModule := NIL;
  inherited;
end;

procedure TEtiquetasContactosReportController.Preview(const AID : String);
var
  AStream: Binary;
  AEditor : IEditorEtiquetasContactosPreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetEtiquetasContactos(AID); //Lista de seleccionados (Array de integers)
    try
      CreateEditor('EditorEtiquetasContactosPreview', IEditorEtiquetasContactosPreview, AEditor);
      AEditor.LoadFromStream(AStream);
    finally
      AStream.Free;
    end;
  finally
    HideHourglassCursor;
  end;

  if Assigned(AEditor) then
    AEditor.Preview;
end;

function TEtiquetasContactosReportController.Print(const AID : String): Boolean;
var
  AStream: Binary;
  AEditor : IEditorEtiquetasContactosPreview;
begin
  Result := False;
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetEtiquetasContactos(AID);
    try
      CreateEditor('EditorEtiquetasContactosPreview', IEditorEtiquetasContactosPreview, AEditor);
      AEditor.LoadFromStream(AStream);
    finally
      AStream.Free;
    end;
  finally
    HideHourglassCursor;
  end;

  if Assigned(AEditor) then
     Result := AEditor.Print;
end;

end.
