unit uFichasPacienteReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModuleFichasPacienteReport,
  uPacientesController, uIntegerListUtils;

type
  IFichasPacienteReportController = interface(IControllerBase)
  ['{8E43F55F-7D5A-4778-8523-7C105EAEB1AB}']
    procedure Preview(const ListaID : TIntegerList);
    procedure Print(const ListaID : TIntegerList);
  end;

  TFichasPacienteReportController = class(TControllerBase, IFichasPacienteReportController)
  private
    FDataModule : IDataModuleFichasPacienteReport;
    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, uIEditorFichasPacientePreview,
  uEditorPreview, uDataModulePacientes, uEditorBase, cxControls;

{ TFichasPacienteReportController }

constructor TFichasPacienteReportController.Create;
begin
  inherited;
  FDataModule := TDataModulePacientes.Create(Nil);
end;

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


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

procedure TFichasPacienteReportController.Preview(const ListaID : TIntegerList);
var
  AStream: Binary;
  AEditor : IEditorFichasPacientePreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(ListaID);
    try
      CreateEditor('EditorFichasPacientePreview', IEditorFichasPacientePreview, 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;
  finally
    HideHourglassCursor;
  end;
end;

procedure TFichasPacienteReportController.Print(const ListaID : TIntegerList);
var
  AStream: Binary;
  AEditor : IEditorFichasPacientePreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(ListaID);
    try
      CreateEditor('EditorFichasPacientePreview', IEditorFichasPacientePreview, 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.
