unit uFamiliasController;

interface


uses
  Classes, SysUtils, uDADataTable, uControllerBase, uEditorDBItem,
  uIDataModuleFamilias, uBizFamilias;

type
  IFamiliasController = interface(IControllerBase)
  ['{3987874F-57A8-4C71-8B7E-CBD2D1580CBC}']
    function BuscarTodos: IBizFamilia;
    procedure VerTodos(AFamilias: IBizFamilia);
    procedure Anadir(AFamilia : IBizFamilia);
    function Eliminar(AFamilia : IBizFamilia): Boolean;
    function Guardar(AFamilia : IBizFamilia): Boolean;
    procedure DescartarCambios(AFamilia : IBizFamilia);
    function Localizar(AFamilias: IBizFamilia; ADescripcion:String): Boolean;
    function DarListaFamilias: TStringList;
  end;

  TFamiliasController = class(TControllerBase, IFamiliasController)
  protected
    FDataModule : IDataModuleFamilias;

    procedure RecibirAviso(ASujeto: ISujeto; ADataTable: IDAStronglyTypedDataTable); override;
    function CreateEditor(const AName : String; const IID: TGUID; out Intf): Boolean;

    function ValidarFamilia(AFamilia: IBizFamilia): Boolean;

    //Estos son los tres métodos a sobre escribir si se desea heredar toda la logica de
    //este controller
    procedure AsignarDataModule; virtual;

  public
    constructor Create; override;
    destructor Destroy; override;

    function Eliminar(AFamilia : IBizFamilia): Boolean;
    function Guardar(AFamilia : IBizFamilia): Boolean;
    procedure DescartarCambios(AFamilia : IBizFamilia); virtual;
    procedure Anadir(AFamilia : IBizFamilia);
    function BuscarTodos: IBizFamilia;
    procedure VerTodos(AFamilias: IBizFamilia);
    function Localizar(AFamilias: IBizFamilia; ADescripcion:String): Boolean;
    function DarListaFamilias: TStringList;
  end;

implementation

uses
  cxControls, DB, uEditorRegistryUtils, schFamiliasClient_Intf,
  uIEditorFamilias, uDataModuleFamilias,
  uDAInterfaces, uDataTableUtils,
  uDateUtils, uROTypes, DateUtils, Controls, Windows;

{ TFamiliasController }

procedure TFamiliasController.Anadir(AFamilia: IBizFamilia);
begin
  AFamilia.Insert;
end;

procedure TFamiliasController.AsignarDataModule;
begin
  FDataModule := TDataModuleFamilias.Create(Nil);
end;

function TFamiliasController.BuscarTodos: IBizFamilia;
begin
  Result := FDataModule.GetItems;
end;

constructor TFamiliasController.Create;
begin
  inherited;
  AsignarDataModule;
end;

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

function TFamiliasController.DarListaFamilias: TStringList;
var
  AFamilias: IBizFamilia;
begin
  AFamilias := BuscarTodos;
  AFamilias.DataTable.Active := True;
  Result := TStringList.Create;
  try
    with Result do
    begin
      AFamilias.DataTable.First;
      while not AFamilias.DataTable.EOF do
      begin
        Add(AFamilias.DESCRIPCION);
        AFamilias.DataTable.Next;
      end;
    end;
  finally
    AFamilias := NIL;
  end;
end;

procedure TFamiliasController.DescartarCambios(AFamilia: IBizFamilia);
begin
  if not Assigned(AFamilia) then
    raise Exception.Create ('Almacen no asignado');

  ShowHourglassCursor;
  try
    if (AFamilia.State in dsEditModes) then
      AFamilia.Cancel;

    AFamilia.DataTable.CancelUpdates;
  finally
    HideHourglassCursor;
  end;
end;

destructor TFamiliasController.Destroy;
begin
  FDataModule := Nil;
  inherited;
end;

function TFamiliasController.ValidarFamilia(AFamilia: IBizFamilia): Boolean;
begin
  Result := False;
  
  if not Assigned(AFamilia) then
    raise Exception.Create ('Familia no asignada');

  if (AFamilia.DataTable.State in dsEditModes) then
      AFamilia.DataTable.Post;

  Result := True;
end;

procedure TFamiliasController.VerTodos(AFamilias: IBizFamilia);
var
  AEditor : IEditorFamilias;
begin
  AEditor := NIL;

  CreateEditor('EditorFamilias', IEditorFamilias, AEditor);
  if Assigned(AEditor) then
    try
      AEditor.Controller := Self; //OJO ORDEN MUY IMPORTANTE
      AEditor.Familias := AFamilias;
      AEditor.ShowModal;
    finally
      AEditor.Release;
      AEditor := NIL;
    end;
end;

function TFamiliasController.Eliminar(AFamilia: IBizFamilia): Boolean;
begin
  if not Assigned(AFamilia) then
    raise Exception.Create ('Almacen no asignada');

  ShowHourglassCursor;
  try
    if (AFamilia.State in dsEditModes) then
      AFamilia.Cancel;

    AFamilia.Delete;
    AFamilia.DataTable.ApplyUpdates;
    HideHourglassCursor;
    Result := True;
  finally
    HideHourglassCursor;
  end;
end;

procedure TFamiliasController.RecibirAviso(ASujeto: ISujeto; ADataTable: IDAStronglyTypedDataTable);
begin
  inherited;
//
end;

function TFamiliasController.Guardar(AFamilia: IBizFamilia): Boolean;
begin
  Result := False;

  if not Assigned(AFamilia) then
    raise Exception.Create ('Almacen no asignada');

  if ValidarFamilia(AFamilia) then
  begin
    ShowHourglassCursor;
    try
      AFamilia.DataTable.ApplyUpdates;

      Result := True;
    finally
      HideHourglassCursor;
    end;
  end;
end;

function TFamiliasController.Localizar(AFamilias: IBizFamilia; ADescripcion: String): Boolean;
begin
  Result := True;
  ShowHourglassCursor;
  try
    with AFamilias.DataTable do
    begin
      DisableControls;
      First;
      if not Locate(fld_FamiliasDESCRIPCION, ADescripcion, []) then
         Result := False;
      EnableControls;
    end;
  finally
    HideHourglassCursor;
  end;
end;

end.
