Advertisement
filhotecmail

Um Classe Criadora e Centralizadora

Sep 11th, 2017
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.41 KB | None | 0 0
  1. unit CentralConfigFactory;
  2.  
  3. interface
  4.  
  5.  uses Vcl.Forms,FrmConfigSat,  SatModelEngine,Dialogs,JvExComCtrls, JvComCtrls, JvDialogs,Types,Windows,
  6.       JvComponentBase, JvComputerInfoEx, FrmMiscelanias, Sistema.Model,FireDAC.Phys.FBDef, FireDAC.UI.Intf, FireDAC.VCLUI.Wait, FireDAC.Comp.UI,
  7.   FireDAC.Stan.Intf, FireDAC.Phys, FireDAC.Phys.IBBase, FireDAC.Phys.FB, FireDAC.Stan.Option, FireDAC.Stan.Param,
  8.   FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf, FireDAC.Stan.Async, FireDAC.DApt, FuncoesLibraryClass,
  9.   FireDAC.Stan.Def, FireDAC.Stan.Pool, Data.DB, FireDAC.Comp.Client, FireDAC.Comp.DataSet,System.SysUtils,System.Generics.Collections;
  10.  
  11. type
  12.   ICentral = interface
  13.   ['{21BC9C81-9302-4029-B46B-25E28070463F}']
  14.     function CreateConfigSatForm:TForm;
  15.     function CreateFrmMisc: TForm;
  16.     function CreateSat:TObject;
  17.     function CarregaDllSat: String;
  18.     function RetornaNomedoComputador:string;
  19.     function CriaInformacoesCpu: TJvComputerInfoEx;
  20.     function CriaConfigSistemas: TObject;
  21.     procedure LimpaMemoriaResidual;
  22.     procedure CreateConexaoBaseDados;
  23.   end;
  24.  
  25.   IAbstractCentral = interface
  26.   ['{3530B3E6-14C0-4C9E-B5F6-C76B28229A5D}']
  27.     function CreateIAbstractProduct: ICentral;
  28.   end;
  29.  
  30.   TCentralFactory = class(TInterfacedObject, ICentral)
  31.   private
  32.  
  33.   published
  34.     function CreateConfigSatForm:TForm;
  35.     function CreateFrmMisc: TForm;
  36.     function CreateSat:TObject;
  37.     function CarregaDllSat: String;
  38.     function RetornaNomedoComputador:string;
  39.     function CriaInformacoesCpu: TJvComputerInfoEx;
  40.     function CriaConfigSistemas: TObject;
  41.     procedure LimpaMemoriaResidual;
  42.     procedure CreateConexaoBaseDados;
  43.     function CriaObjetoEmail:TObject;
  44.  
  45.   end;
  46.  
  47.   TConcreteCentral = class(TInterfacedObject, IAbstractCentral)
  48.   public
  49.     function CreateIAbstractProduct: ICentral;
  50.   end;
  51.  
  52.   type TLib = Class sealed
  53.  
  54.    procedure FormataCamposDataset(aDataset: TDataSet; aMascara: string);
  55.    procedure Wait(Msg:String;time:Integer);
  56.   End;
  57.  
  58.   type TGlobalForm = Class sealed
  59.  
  60.       FrmConfigSatParam: TTfrmConfigSat;
  61.       FrmMisc: TTfrmMiscelanias;
  62.   end;
  63.  
  64.  type TGlobals = class sealed
  65.  
  66.    CentralFactory: TConcreteCentral;
  67.    Central:  TCentralFactory;
  68.    Sat: TSatTableDaoModel;
  69.    FInfoCpu: TJvComputerInfoEx;
  70.    SistemaConfig: TSistema;
  71.    FConnectionPdv: TFDConnection;
  72.    EmailClass: TConfigEmail;
  73.  end;
  74.  
  75.    type tipos = (aClasses,aForms,aTlibrary);
  76.    type Lista = class
  77.  
  78.    class function RetornaLista(arecord:tipos ):TObject;
  79.    class function destroilista: Boolean;
  80.      /// <summary>  Retorna os Objetos de classes </summary>
  81.    class var Objeto : TGlobals;
  82.      /// <summary>  Retorna os Objetos TForm </summary>
  83.    class var ObjetoForm : TGlobalForm;
  84.    class var Biblioteca : TLib;
  85.    class constructor Create;
  86.    class destructor destroy;
  87.    end;
  88.  
  89.     var List: Lista;
  90.  
  91. implementation
  92. { TCentralFactory }
  93.  
  94. function TCentralFactory.CarregaDllSat: String;
  95.  var  aopen: TJvOpenDialog;
  96. begin
  97.  
  98.       aopen:= TJvOpenDialog.Create(nil);
  99.   try
  100.    if aopen.Execute then
  101.    Result:=  aopen.FileName;
  102.   finally
  103.     aopen.DisposeOf;
  104.   end;
  105. end;
  106.  
  107. procedure TCentralFactory.CreateConexaoBaseDados;
  108. begin
  109.  
  110.   CarregaWait('Preparando Carregamento dos dados',200);
  111.   Lista.Objeto.FConnectionPdv:= TFDConnection.Create(Nil);
  112.   List.objeto.FConnectionPdv.Params.Clear;
  113.    CarregaWait('Tentando se conectar ao banco de dados',200);
  114.   List.objeto.FConnectionPdv.DriverName := 'FB';
  115.   List.objeto.FConnectionPdv.Params.Values['Server'] :=   '127.0.0.1';
  116.   List.objeto.FConnectionPdv.Params.Values['Database'] := 'C:\Leopard\PDV\PDV.FDB';
  117.   List.objeto.FConnectionPdv.Params.Values['User_Name'] := 'SYSDBA';
  118.   List.objeto.FConnectionPdv.Params.Values['Password'] := '1234';
  119.   List.objeto.FConnectionPdv.Params.Values['Port'] := '3050';
  120.   List.objeto.FConnectionPdv.Params.Values['DriveId'] := 'FB';
  121.   List.objeto.FConnectionPdv.LoginPrompt:= False;
  122.   CarregaWait('Tentando se conectar ao banco de dados',200);
  123.   try
  124.     try
  125.      List.objeto.FConnectionPdv.Connected:= True;
  126.     finally
  127.       CarregaWait('Conexao criada com sucesso!',200);
  128.     end;
  129.   except
  130.    on E: Exception do begin
  131.       raise Exception.Create('Não consegui conectar a base de dados do PDV, por favor revise as configurações ou'
  132.       +' verifique se o Arquivo PDV.FDB se encontra na pasta.');
  133.    end;
  134.   end;
  135.   end;
  136.  
  137. function TCentralFactory.CreateConfigSatForm: TForm;
  138. begin
  139.    CarregaWait('Carregando e criando dados do SAT',200);
  140.    List.ObjetoForm.FrmConfigSatParam:= TTfrmConfigSat.Create(Application);
  141.    Result:= List.ObjetoForm.FrmConfigSatParam;
  142.   List.ObjetoForm.FrmConfigSatParam.ShowModal;
  143. end;
  144.  
  145. function TCentralFactory.CreateFrmMisc: TForm;
  146. begin
  147.   CarregaWait('Carregando as Configurações da aplicação',500);
  148.   List.ObjetoForm.FrmMisc:= TTfrmMiscelanias.Create(Application);
  149.   Result := List.ObjetoForm.FrmMisc;
  150.   List.ObjetoForm.FrmMisc.ShowModal;
  151. end;
  152.  
  153. function TCentralFactory.CreateSat: TObject;
  154. begin
  155.     List.objeto.Sat:= TSatTableDaoModel.Create;
  156.     Result:= List.objeto.Sat;
  157. end;
  158.  
  159. function TCentralFactory.CriaConfigSistemas: TObject;
  160. begin
  161.    List.objeto.SistemaConfig:= TSistema.Create;
  162.    result:= List.objeto.SistemaConfig;
  163. end;
  164.  
  165. function TCentralFactory.CriaInformacoesCpu: TJvComputerInfoEx;
  166. begin
  167.  
  168.   Result:= TJvComputerInfoEx.Create(Nil);
  169.   List.objeto.FInfoCpu:= Result;
  170. end;
  171.  
  172. function TCentralFactory.CriaObjetoEmail: TObject;
  173. begin
  174.   Lista.Objeto.EmailClass:= TConfigEmail.Create;
  175.   Result:= Lista.Objeto.EmailClass;
  176. end;
  177.  
  178. procedure TCentralFactory.LimpaMemoriaResidual;
  179. var
  180. MainHandle : THandle;
  181. begin
  182. try
  183. MainHandle := OpenProcess(PROCESS_ALL_ACCESS, false, GetCurrentProcessID) ;
  184. SetProcessWorkingSetSize(MainHandle, $FFFFFFFF, $FFFFFFFF) ;
  185. CloseHandle(MainHandle) ;
  186. except
  187. end;
  188. Application.ProcessMessages;
  189. end;
  190.  
  191. function TCentralFactory.RetornaNomedoComputador: string;
  192. var ipbuffer : string;
  193.       nsize : dword;
  194. begin
  195.    nsize := 255;
  196.    SetLength(ipbuffer,nsize);
  197.    if GetComputerName(pchar(ipbuffer),nsize) then
  198.       result := ipbuffer;
  199. end;
  200.  
  201. { TConcreteCentral }
  202.  
  203. function TConcreteCentral.CreateIAbstractProduct: ICentral;
  204. begin
  205.   Result := TCentralFactory.Create();
  206. end;
  207.  
  208. {Lista}
  209. class constructor Lista.Create;
  210. begin
  211.   inherited;
  212.    RetornaLista(aclasses);
  213.    RetornaLista(aForms);
  214.    RetornaLista(aTlibrary);
  215. end;
  216.  
  217. class function Lista.destroilista: Boolean;
  218. begin
  219.  result := False;
  220.  if Lista.Objeto <> nil then begin
  221.     Lista.Objeto.DisposeOf;
  222.     end;
  223.  if Lista.ObjetoForm <> nil then begin
  224.     Lista.ObjetoForm.DisposeOf;
  225.  end;
  226.    if Lista.Biblioteca <> nil then begin
  227.       Lista.Biblioteca.DisposeOf;
  228.    end;
  229. end;
  230.  
  231. class destructor Lista.destroy;
  232. begin
  233.    Lista.destroilista;
  234. end;
  235.  
  236. class function Lista.RetornaLista(arecord: tipos): TObject;
  237. begin
  238.  case arecord of
  239.    aClasses: begin
  240.    Objeto := TGlobals.Create;
  241.    end;
  242.    aForms: begin
  243.    ObjetoForm := TGlobalForm.Create;
  244.    end;
  245.     aTlibrary : begin
  246.     Biblioteca := TLib.Create;
  247.    end;
  248.  end;
  249. end;
  250.  
  251. { TLib }
  252.  
  253. procedure TLib.wait(Msg: String; time: Integer);
  254. begin
  255.   CarregaWait(Msg,time);
  256. end;
  257.  
  258. procedure TLib.FormataCamposDataset(aDataset: TDataSet; aMascara: string);
  259. begin
  260.   FormataCamposFields(aDataset,aMascara);
  261. end;
  262.  
  263. initialization
  264.    CarregaWait('Objetos carregados com êxito',500);
  265.    List := Lista.create;
  266.  
  267. finalization
  268.    CarregaWait('Descarregando Objetos',500);
  269. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement