unit uFabricantesController; interface uses Classes, SysUtils, uDADataTable, uControllerBase, uEditorDBItem, uIDataModuleFabricantes, uBizFabricantes; type IFabricantesController = interface(IControllerBase) ['{8D56360B-0AAA-41FA-9C48-90C586B07E37}'] function BuscarTodos: IBizFabricante; procedure VerTodos(AFabricantes: IBizFabricante); procedure Anadir(AFabricante : IBizFabricante); function Eliminar(AFabricante : IBizFabricante): Boolean; function Guardar(AFabricante : IBizFabricante): Boolean; procedure DescartarCambios(AFabricante : IBizFabricante); function Localizar(AFabricantes: IBizFabricante; ADescripcion:String): Boolean; function DarListaFabricantes: TStringList; end; TFabricantesController = class(TControllerBase, IFabricantesController) protected FDataModule : IDataModuleFabricantes; procedure RecibirAviso(ASujeto: ISujeto; ADataTable: IDAStronglyTypedDataTable); override; function CreateEditor(const AName : String; const IID: TGUID; out Intf): Boolean; function ValidarFabricante(AFabricante: IBizFabricante): Boolean; //Estos son los tres métodos a sobre escribir si se desea heredar toda la logica de //este controller procedure AsignarDataModule; virtual; procedure ValidarObjetos; virtual; public constructor Create; override; destructor Destroy; override; function Eliminar(AFabricante : IBizFabricante): Boolean; function Guardar(AFabricante : IBizFabricante): Boolean; procedure DescartarCambios(AFabricante : IBizFabricante); virtual; procedure Anadir(AFabricante : IBizFabricante); function BuscarTodos: IBizFabricante; procedure VerTodos(AFabricantes: IBizFabricante); function Localizar(AFabricantes: IBizFabricante; ADescripcion:String): Boolean; function DarListaFabricantes: TStringList; end; implementation uses cxControls, DB, uEditorRegistryUtils, schFabricantesClient_Intf, uIEditorFabricantes, uDataModuleFabricantes, uDAInterfaces, uDataTableUtils, uDateUtils, uROTypes, DateUtils, Controls, Windows; { TFabricantesController } procedure TFabricantesController.Anadir(AFabricante: IBizFabricante); begin AFabricante.Insert; end; procedure TFabricantesController.AsignarDataModule; begin FDataModule := TDataModuleFabricantes.Create(Nil); end; function TFabricantesController.BuscarTodos: IBizFabricante; begin Result := FDataModule.GetItems; end; constructor TFabricantesController.Create; begin inherited; AsignarDataModule; end; function TFabricantesController.CreateEditor(const AName: String; const IID: TGUID; out Intf): Boolean; begin Result := Supports(EditorRegistry.CreateEditor(AName), IID, Intf); end; function TFabricantesController.DarListaFabricantes: TStringList; var AFabricantes: IBizFabricante; begin AFabricantes := BuscarTodos; AFabricantes.DataTable.Active := True; Result := TStringList.Create; try with Result do begin AFabricantes.DataTable.First; while not AFabricantes.DataTable.EOF do begin Add(AFabricantes.DESCRIPCION); AFabricantes.DataTable.Next; end; end; finally AFabricantes := NIL; end; end; procedure TFabricantesController.DescartarCambios(AFabricante: IBizFabricante); begin if not Assigned(AFabricante) then raise Exception.Create ('Almacen no asignado'); ShowHourglassCursor; try if (AFabricante.State in dsEditModes) then AFabricante.Cancel; AFabricante.DataTable.CancelUpdates; finally HideHourglassCursor; end; end; destructor TFabricantesController.Destroy; begin FDataModule := Nil; inherited; end; function TFabricantesController.ValidarFabricante(AFabricante: IBizFabricante): Boolean; begin Result := False; if not Assigned(AFabricante) then raise Exception.Create ('Fabricante no asignado'); if (AFabricante.DataTable.State in dsEditModes) then AFabricante.DataTable.Post; Result := True; end; procedure TFabricantesController.ValidarObjetos; begin // end; procedure TFabricantesController.VerTodos(AFabricantes: IBizFabricante); var AEditor : IEditorFabricantes; begin AEditor := NIL; CreateEditor('EditorFabricantes', IEditorFabricantes, AEditor); if Assigned(AEditor) then try AEditor.Controller := Self; //OJO ORDEN MUY IMPORTANTE AEditor.Fabricantes := AFabricantes; AEditor.ShowModal; finally AEditor.Release; AEditor := NIL; end; end; function TFabricantesController.Eliminar(AFabricante: IBizFabricante): Boolean; begin Result := False; if not Assigned(AFabricante) then raise Exception.Create ('Almacen no asignada'); ShowHourglassCursor; try if (AFabricante.State in dsEditModes) then AFabricante.Cancel; AFabricante.Delete; AFabricante.DataTable.ApplyUpdates; HideHourglassCursor; Result := True; finally HideHourglassCursor; end; end; procedure TFabricantesController.RecibirAviso(ASujeto: ISujeto; ADataTable: IDAStronglyTypedDataTable); begin inherited; // end; function TFabricantesController.Guardar(AFabricante: IBizFabricante): Boolean; begin Result := False; if not Assigned(AFabricante) then raise Exception.Create ('Almacen no asignada'); ValidarObjetos; if ValidarFabricante(AFabricante) then begin ShowHourglassCursor; try AFabricante.DataTable.ApplyUpdates; Result := True; finally HideHourglassCursor; end; end; end; function TFabricantesController.Localizar(AFabricantes: IBizFabricante; ADescripcion: String): Boolean; begin Result := True; ShowHourglassCursor; try with AFabricantes.DataTable do begin DisableControls; First; if not Locate(fld_FabricantesDESCRIPCION, ADescripcion, []) then Result := False; EnableControls; end; finally HideHourglassCursor; end; end; end.