Advertisement
filhotecmail

Carga de Tabelas OMvcBrOdataserver Versao Corrigida

Aug 17th, 2017
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.25 KB | None | 0 0
  1. { //************************************************************// }
  2. { //                                                            // }
  3. { //         Carlos Alberto Dias da S. Filho                    // }
  4. { //                                                            // }
  5. { //         Projeto Leopard Report
  6. {    Objetivo: Criar os Objetos de conexao com o MVCbrODataserver  }
  7. {    e gerar um JsonObject do Dataset para Alimentar               }
  8. {    o FDquery e as classes/ Objetos responsaveis por todas as dinãmicas
  9. {     da carga no PDV.                                             }
  10. { //                                                            // }
  11. { //************************************************************// }
  12. { // Data: 15/08/2017 21:17:45                                  // }
  13. { //************************************************************// }
  14.  
  15. Unit CargadeTabelas.PersistentModel;
  16.  
  17.  
  18. interface
  19.  
  20. { .$I ..\inc\mvcbr.inc }
  21. uses System.SysUtils, {$IFDEF FMX} FMX.Forms, {$ELSE} VCL.Forms, {$ENDIF} System.Classes, MVCBr.Interf, MVCBr.PersistentModel,
  22.   CargadeTabelas.PersistentModel.Interf, // %Interf,
  23.   MVCBr.Controller, FireDAC.Phys.FBDef, Dialogs,
  24.   IPPeerClient, Data.DB,
  25.   FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS,
  26.   FireDAC.Phys.Intf, FireDAC.DApt.Intf, FireDAC.Comp.DataSet,
  27.   FireDAC.Comp.Client, MVCBr.ODataDatasetAdapter, oData.Comp.Client,
  28.   FireDAC.Stan.Intf, FireDAC.Stan.Option, MVCBr.IdHTTPRestClient,
  29.   VCL.Controls, VCL.StdCtrls,Json,
  30.   FireDAC.UI.Intf, FireDAC.VCLUI.Wait, FireDAC.Comp.UI,
  31.   Datasnap.DBClient, MVCBr.ODataDatasetBuilder, MVCBr.ODataFDMemTable,
  32.   MVCBr.HTTPRestClient.Common,FireDAC.Stan.StorageJson;
  33.  
  34. Type
  35.  
  36.   TCargadeTabelasPersistentModel = class(TPersistentModelFactory,
  37.     ICargadeTabelasPersistentModel, IThisAs<TCargadeTabelasPersistentModel>)
  38.   private
  39.     FTableProdutos: TFDquery;
  40.     FTFDStorageFormat: TFDStorageFormat;
  41.     FFDGUIxWaitCursor1: TFDGUIxWaitCursor;
  42.     FOdataBuilder: TODataBuilder;
  43.     FHttpRest: TIdHTTPRestClient;
  44.     FOdataMemTable: TODataFDMemTable;
  45.     FAdapterOdata: TODataDatasetAdapter;
  46.     FFDStanStorageBinLink1: TFDStanStorageJsonLink;
  47.  
  48.   protected
  49.   public
  50.     Constructor Create; override;
  51.     Destructor Destroy; override;
  52.     class function new(): ICargadeTabelasPersistentModel; overload;
  53.     class function new(const AController: IController)
  54.       : ICargadeTabelasPersistentModel; overload;
  55.     function ThisAs: TCargadeTabelasPersistentModel;
  56.  
  57.     property FDStorageFormat: TFDStorageFormat read FTFDStorageFormat write FTFDStorageFormat;
  58.     property FDGUIxWaitCursor1: TFDGUIxWaitCursor read FFDGUIxWaitCursor1 write FFDGUIxWaitCursor1;
  59.  
  60.     property AdapterOdata: TODataDatasetAdapter read FAdapterOdata write FAdapterOdata;
  61.     property OdataBuilder: TODataBuilder read FOdataBuilder write FOdataBuilder;
  62.     property HttpRest: TIdHTTPRestClient read FHttpRest write FHttpRest;
  63.     property OdataMemTable: TODataFDMemTable read FOdataMemTable write FOdataMemTable;
  64.     /// <summary> Cria os objetos TODataDatasetAdapter,TODataBuilder,TIdHTTPRestClient,TODataFDMemTable  </summary>
  65.     /// <summary> parametrizando os Objetos e conectando ao servidor.  </summary>
  66.     Function RetornaDatasetJSon: TJSONObject;
  67.     /// <summary> Cria e parametriza o TODataDatasetAdapter, o Objeto está sendo referenciado na property FAdapterOdata  </summary>
  68.     procedure CreateAdapterOdata;
  69.     /// <summary> Cria e parametriza o TODataBuilder, o Objeto está sendo referenciado na property FOdataBuilder  </summary>
  70.     procedure CreateOdataBuilder;
  71.     /// <summary> Cria e parametriza o TIdHTTPRestClient, o Objeto está sendo referenciado na property FHttpRest  </summary>
  72.     procedure CreateOdataTIdHTTPRestClient;
  73.     /// <summary> Cria e parametriza o TODataFDMemTable, o Objeto está sendo referenciado na property FOdataMemTable  </summary>
  74.     procedure CreateDataMemtable;
  75.     // implementaçoes
  76.   end;
  77.  
  78. Implementation
  79.  
  80. uses DtmConexao;
  81.  
  82. constructor TCargadeTabelasPersistentModel.Create;
  83. var
  84.   oStr: TJsonObject;
  85. begin
  86.   inherited;
  87.   ModelTypes := [mtPersistent];
  88.   oStr := RetornaDatasetJSon;
  89. end;
  90.  
  91. procedure TCargadeTabelasPersistentModel.CreateAdapterOdata;
  92. begin
  93.   FAdapterOdata := TODataDatasetAdapter.Create(nil);
  94.   FAdapterOdata.RootElement := 'value';
  95.   FAdapterOdata.ResponseType := pureJSON;
  96. end;
  97.  
  98. procedure TCargadeTabelasPersistentModel.CreateDataMemtable;
  99. begin
  100.   FOdataMemTable := TODataFDMemTable.Create(Nil);
  101.   FOdataMemTable.Active := False;
  102.   FOdataMemTable.AutoCalcFields := False;
  103.   FOdataMemTable.ResourceOptions.SilentMode := True;
  104.   FOdataMemTable.ResourceOptions.AssignedValues := [rvSilentMode];
  105.   FOdataMemTable.ObjectView := True;
  106.   FOdataMemTable.StoreDefs := True;
  107.   FOdataMemTable.AutoCalcFields := True;
  108.   FOdataMemTable.UpdateOptions.CheckRequired := False;
  109.   FOdataMemTable.UpdateOptions.RequestLive := True;
  110.   FOdataMemTable.UpdateOptions.UpdateChangedFields := True;
  111. end;
  112.  
  113. procedure TCargadeTabelasPersistentModel.CreateOdataBuilder;
  114. begin
  115.   FOdataBuilder := TODataBuilder.Create(Nil);
  116.   FOdataBuilder.Service := '/OData.svc';
  117.   FOdataBuilder.ServicePrefix := '/OData';
  118.   FOdataBuilder.Count := True;
  119.   FOdataBuilder.Resource.Add;
  120.   FOdataBuilder.Resource.Items[0].Resource:= 'CADPRODUTOS';
  121.   FOdataBuilder.ResourceName := 'CADPRODUTOS';
  122.   FOdataBuilder.SkipRows := 0;
  123.   FOdataBuilder.TopRows := 100;
  124.   FOdataBuilder.BaseURL := 'http://localhost:8080';
  125.  
  126. end;
  127.  
  128. procedure TCargadeTabelasPersistentModel.CreateOdataTIdHTTPRestClient;
  129.  
  130. begin
  131.   FHttpRest := TIdHTTPRestClient.Create(Nil);
  132.   FHttpRest.Accept := 'application/json;odata.metadata=minimal';
  133.   FHttpRest.AcceptCharset := 'UTF-8';
  134.   FHttpRest.AcceptEncoding := 'gzip';
  135.   FHttpRest.BaseURL := 'http://localhost:8080';
  136.   FHttpRest.Method := rmGET;
  137.   FHttpRest.Timeout := 360000;
  138. end;
  139.  
  140. destructor TCargadeTabelasPersistentModel.Destroy;
  141. begin
  142.   inherited;
  143. end;
  144.  
  145. function TCargadeTabelasPersistentModel.ThisAs: TCargadeTabelasPersistentModel;
  146. begin
  147.   result := self;
  148. end;
  149.  
  150. class function TCargadeTabelasPersistentModel.new()
  151.   : ICargadeTabelasPersistentModel;
  152. begin
  153.   result := new(nil);
  154. end;
  155.  
  156. class function TCargadeTabelasPersistentModel.new(const AController
  157.   : IController): ICargadeTabelasPersistentModel;
  158. begin
  159.   result := TCargadeTabelasPersistentModel.Create;
  160.   result.Controller(AController);
  161. end;
  162.  
  163. function TCargadeTabelasPersistentModel.RetornaDatasetJSon: TJSONObject;
  164. var
  165.   oStr: TJSONObject;
  166. begin
  167.   try
  168.  
  169.     CreateAdapterOdata;
  170.     CreateOdataBuilder;
  171.     CreateOdataTIdHTTPRestClient;
  172.     self.CreateDataMemtable;
  173.  
  174.     FOdataBuilder.RestClient := FHttpRest;
  175.     FAdapterOdata.Builder := FOdataBuilder;
  176.     FAdapterOdata.ResponseJSON := FHttpRest;
  177.     FAdapterOdata.RootElement := 'value';
  178.     FAdapterOdata.DataSet := self.FOdataMemTable;
  179.     FAdapterOdata.execute;
  180.     FAdapterOdata.ClearChanges;
  181.  
  182.     FOdataMemTable.SaveTofile('C:\Leopard\TMP\J.Json', sfJson);
  183.     result := oStr;
  184.   finally
  185.     FAdapterOdata.DisposeOf;
  186.     self.FHttpRest.DisposeOf;
  187.     self.FOdataMemTable.DisposeOf;
  188.     self.FOdataBuilder.DisposeOf;
  189.   end;
  190. end;
  191.  
  192. Initialization
  193.  
  194. TMVCRegister.RegisterType<ICargadeTabelasPersistentModel,
  195.   TCargadeTabelasPersistentModel>
  196.   (TCargadeTabelasPersistentModel.classname, True);
  197.  
  198. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement