unit uDelegacionesController; interface uses Classes, SysUtils, uDADataTable, uControllerBase, uEditorDBItem, uIDataModuleDelegaciones, uBizDelegaciones; type IDelegacionesController = interface(IControllerBase) ['{D3B7113C-C525-4400-8A21-B05BF4341ED8}'] function BuscarTodos: IBizDelegacion; procedure VerTodos(ADelegaciones: IBizDelegacion); procedure Anadir(ADelegacion : IBizDelegacion); function Eliminar(ADelegacion : IBizDelegacion): Boolean; function Guardar(ADelegacion : IBizDelegacion): Boolean; procedure DescartarCambios(ADelegacion : IBizDelegacion); function Localizar(ADelegaciones: IBizDelegacion; ADescripcion:String): Boolean; function DarListaDelegaciones: TStringList; end; TDelegacionesController = class(TControllerBase, IDelegacionesController) protected FDataModule : IDataModuleDelegaciones; procedure RecibirAviso(ASujeto: ISujeto; ADataTable: IDAStronglyTypedDataTable); override; function CreateEditor(const AName : String; const IID: TGUID; out Intf): Boolean; function ValidarDelegacion(ADelegacion: IBizDelegacion): 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(ADelegacion : IBizDelegacion): Boolean; function Guardar(ADelegacion : IBizDelegacion): Boolean; procedure DescartarCambios(ADelegacion : IBizDelegacion); virtual; procedure Anadir(ADelegacion : IBizDelegacion); function BuscarTodos: IBizDelegacion; procedure VerTodos(ADelegaciones: IBizDelegacion); function Localizar(ADelegaciones: IBizDelegacion; ADescripcion:String): Boolean; function DarListaDelegaciones: TStringList; end; implementation uses cxControls, DB, uEditorRegistryUtils, schDelegacionesClient_Intf, uIEditorDelegaciones, uDataModuleDelegaciones, uDAInterfaces, uDataTableUtils, uDateUtils, uROTypes, DateUtils, Controls, Windows; { TDelegacionesController } procedure TDelegacionesController.Anadir(ADelegacion: IBizDelegacion); begin ADelegacion.Insert; end; procedure TDelegacionesController.AsignarDataModule; begin FDataModule := TDataModuleDelegaciones.Create(Nil); end; function TDelegacionesController.BuscarTodos: IBizDelegacion; begin Result := FDataModule.GetItems; end; constructor TDelegacionesController.Create; begin inherited; AsignarDataModule; end; function TDelegacionesController.CreateEditor(const AName: String; const IID: TGUID; out Intf): Boolean; begin Result := Supports(EditorRegistry.CreateEditor(AName), IID, Intf); end; function TDelegacionesController.DarListaDelegaciones: TStringList; var ADelegaciones: IBizDelegacion; begin ADelegaciones := BuscarTodos; ADelegaciones.DataTable.Active := True; Result := TStringList.Create; try with Result do begin ADelegaciones.DataTable.First; while not ADelegaciones.DataTable.EOF do begin Add(ADelegaciones.DESCRIPCION); ADelegaciones.DataTable.Next; end; end; finally ADelegaciones := NIL; end; end; procedure TDelegacionesController.DescartarCambios(ADelegacion: IBizDelegacion); begin if not Assigned(ADelegacion) then raise Exception.Create ('Delegación no asignada'); ShowHourglassCursor; try if (ADelegacion.State in dsEditModes) then ADelegacion.Cancel; ADelegacion.DataTable.CancelUpdates; finally HideHourglassCursor; end; end; destructor TDelegacionesController.Destroy; begin FDataModule := Nil; inherited; end; function TDelegacionesController.ValidarDelegacion(ADelegacion: IBizDelegacion): Boolean; begin Result := False; if not Assigned(ADelegacion) then raise Exception.Create ('Delegación no asignada'); if (ADelegacion.DataTable.State in dsEditModes) then ADelegacion.DataTable.Post; Result := True; end; procedure TDelegacionesController.ValidarObjetos; begin // end; procedure TDelegacionesController.VerTodos(ADelegaciones: IBizDelegacion); var AEditor : IEditorDelegaciones; begin AEditor := NIL; CreateEditor('EditorDelegaciones', IEditorDelegaciones, AEditor); if Assigned(AEditor) then try AEditor.Controller := Self; //OJO ORDEN MUY IMPORTANTE AEditor.Delegaciones := ADelegaciones; AEditor.ShowModal; finally AEditor.Release; AEditor := NIL; end; end; function TDelegacionesController.Eliminar(ADelegacion: IBizDelegacion): Boolean; begin Result := False; if not Assigned(ADelegacion) then raise Exception.Create ('Delegación no asignada'); ShowHourglassCursor; try if (ADelegacion.State in dsEditModes) then ADelegacion.Cancel; ADelegacion.Delete; ADelegacion.DataTable.ApplyUpdates; HideHourglassCursor; Result := True; finally HideHourglassCursor; end; end; procedure TDelegacionesController.RecibirAviso(ASujeto: ISujeto; ADataTable: IDAStronglyTypedDataTable); begin inherited; // end; function TDelegacionesController.Guardar(ADelegacion: IBizDelegacion): Boolean; begin Result := False; if not Assigned(ADelegacion) then raise Exception.Create ('Delegación no asignada'); ValidarObjetos; if ValidarDelegacion(ADelegacion) then begin ShowHourglassCursor; try ADelegacion.DataTable.ApplyUpdates; Result := True; finally HideHourglassCursor; end; end; end; function TDelegacionesController.Localizar(ADelegaciones: IBizDelegacion; ADescripcion: String): Boolean; begin Result := True; ShowHourglassCursor; try with ADelegaciones.DataTable do begin DisableControls; First; if not Locate(fld_DelegacionesDESCRIPCION, ADescripcion, []) then Result := False; EnableControls; end; finally HideHourglassCursor; end; end; end.