unit uEtiquetasContactosReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModuleEtiquetasContactosReport,
  uEmpleadosController, uIntegerListUtils;

type
  IEtiquetasContactosReportController = interface(IControllerBase)
  ['{76BB35E3-554E-4D30-8D3E-54E488EF82E2}']
    procedure Preview(const ListaID : TIntegerList);
    procedure Print(const ListaID : TIntegerList);
  end;

  TEtiquetasContactosReportController = class(TControllerBase, IEtiquetasContactosReportController)
  private
    FDataModule : IDataModuleEtiquetasContactosReport;
    function CreateEditor(const AName: String; const IID: TGUID; out Intf): Boolean;
  public
    constructor Create; override;
    destructor Destroy; override;

    procedure Preview(const ListaID : TIntegerList);
    procedure Print(const ListaID : TIntegerList);
  end;


implementation

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

{ TEtiquetasContactosReportController }

constructor TEtiquetasContactosReportController.Create;
begin
  inherited;
  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 ListaID : TIntegerList);
var
  AStream: Binary;
  AEditor : IEditorEtiquetasContactosPreview;
begin
  AEditor := NIL;

  AStream := FDataModule.GetReport(ListaID);
  try
    CreateEditor('EditorEtiquetasContactosPreview', IEditorEtiquetasContactosPreview, AEditor);
    if Assigned(AEditor) then
    begin
      try
        AEditor.Controller := Self;
        AEditor.ListaID := ListaID;
        AEditor.LoadFromStream(AStream);
        AEditor.Preview;
      finally
        AEditor.Release;
      end;
    end;
  finally
    FreeAndNil(AStream);
    AEditor := Nil;
  end;
end;

procedure TEtiquetasContactosReportController.Print(const ListaID : TIntegerList);
var
  AStream: Binary;
  AEditor : IEditorEtiquetasContactosPreview;
begin
  AEditor := NIL;
  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(ListaID);
    try
      CreateEditor('EditorEtiquetasContactosPreview', IEditorEtiquetasContactosPreview, AEditor);
      if Assigned(AEditor) then
      begin
        try
          AEditor.Controller := Self;
          AEditor.ListaID := ListaID;
          AEditor.LoadFromStream(AStream);
          AEditor.Print;
        finally
          AEditor.Release;
        end;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

end.
