Advertisement
filhotecmail

Carga de Tabelas OMvcBrOdataserver

Aug 16th, 2017
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.89 KB | None | 0 0
  1. Unit  CargadeTabelas.PersistentModel;
  2. { MVCBr
  3.   www.tireideletra.com.br
  4.   amarildo lacerda
  5. }
  6.  
  7. interface
  8. {.$I ..\inc\mvcbr.inc}
  9. uses System.SysUtils,{$ifdef FMX} FMX.Forms,{$else} VCL.Forms,{$endif} System.Classes, MVCBr.Interf, MVCBr.PersistentModel,
  10.    CargadeTabelas.PersistentModel.Interf, //%Interf,
  11. MVCBr.Controller,FireDAC.Phys.FBDef,Dialogs,
  12.   IPPeerClient, Data.DB,
  13.   FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS,
  14.   FireDAC.Phys.Intf, FireDAC.DApt.Intf, FireDAC.Comp.DataSet,
  15.   FireDAC.Comp.Client, MVCBr.ODataDatasetAdapter, oData.Comp.Client,
  16.   FireDAC.Stan.Intf, FireDAC.Stan.Option, MVCBr.IdHTTPRestClient,
  17.   VCL.Controls,VCL.StdCtrls,
  18.   FireDAC.UI.Intf, FireDAC.VCLUI.Wait, FireDAC.Comp.UI,
  19.   Datasnap.DBClient, MVCBr.ODataDatasetBuilder, MVCBr.ODataFDMemTable, MVCBr.HTTPRestClient.Common;
  20.  
  21. Type
  22.  
  23.   TCargadeTabelasPersistentModel = class(TPersistentModelFactory,ICargadeTabelasPersistentModel,IThisAs<TCargadeTabelasPersistentModel>)
  24.   private
  25.     FTableProdutos: TFDquery;
  26.     FTFDStorageFormat: TFDStorageFormat;
  27.     FFDGUIxWaitCursor1: TFDGUIxWaitCursor;
  28.  
  29.   protected
  30.   public
  31.     Constructor Create; override;
  32.     Destructor Destroy; override;
  33.     class function new():ICargadeTabelasPersistentModel; overload;
  34.     class function new(const AController:IController):ICargadeTabelasPersistentModel;  overload;
  35.     function ThisAs:TCargadeTabelasPersistentModel;
  36.     Function ExtraiTableProdonserver:TDataset;
  37.     property TableProdutos: TFDquery read FTableProdutos write FTableProdutos;
  38.     property FDStorageFormat: TFDStorageFormat read FTFDStorageFormat write FTFDStorageFormat;
  39.     function aSql: String;
  40.     Function RetornaDatasetJSon: Tstream;
  41.     property FDGUIxWaitCursor1:  TFDGUIxWaitCursor read FFDGUIxWaitCursor1 write FFDGUIxWaitCursor1;
  42.  
  43.  
  44.     function CreateAdapterOdata(Dataset: TODataFDMemTable ): TODataDatasetAdapter;
  45.     function CrateOdataBuilder: TODataBuilder;
  46.     function CreateIdHTTPRestClient: TIdHTTPRestClient;
  47.     function CreateFdOdataMemTable: TODataFDMemTable;
  48.       // implementaçoes
  49.   end;
  50.  
  51. Implementation
  52.  
  53. uses DtmConexao;
  54.  
  55. function TCargadeTabelasPersistentModel.aSql: String;
  56. begin
  57.  result := 'select * from CADPRODUTOS  ';
  58. end;
  59.  
  60. function TCargadeTabelasPersistentModel.CrateOdataBuilder: TODataBuilder;
  61.  var Builder: TODataBuilder;
  62. begin
  63.   Builder:= TODataBuilder.Create(Nil);
  64.   Builder.Service := '/OData.svc';
  65.   Builder.ServicePrefix:= '/OData';
  66.   Builder.Count:= True;
  67.   Builder.Resource.addResource('CADPRODUTOS');
  68.   Builder.ResourceName:= 'CADPRODUTOS';
  69.   Builder.SkipRows:= 100;
  70.   Builder.TopRows:= 100;
  71.   Builder.BaseURL := 'http://'+DtmConexao.TDtmConexao.IPServidor+':8080';
  72.   try
  73.   result := Builder;
  74.   finally
  75.    Builder.DisposeOf;
  76.   end;
  77. end;
  78.  
  79. constructor TCargadeTabelasPersistentModel.Create;
  80.  var oStr: TStream;
  81. begin
  82.   inherited;
  83.   ModelTypes := [mtPersistent];
  84. //  ExtraiTableProdonserver;
  85.    //TableProdutos:= TFDquery.Create(Nil);
  86.  
  87.    oStr := RetornaDatasetJSon;
  88.    ShowMessage('Teste');
  89. end;
  90.  
  91. function TCargadeTabelasPersistentModel.CreateAdapterOdata(Dataset: TODataFDMemTable ): TODataDatasetAdapter;
  92.   var AdapterNull: TODataDatasetAdapter;
  93. begin
  94.    try
  95.    Adapternull:= TODataDatasetAdapter.Create(Nil);
  96.    if dataset <> nil then begin
  97.    Adapternull.Dataset:= Dataset;
  98.    end;
  99.    Adapternull.Active:= False;
  100.    Adapternull.RootElement := 'Value';
  101.    Adapternull.ResponseType:= pureJSON;
  102.    finally
  103.    result:= Adapternull;
  104.    Adapternull.DisposeOf;
  105.    end;
  106. end;
  107.  
  108. function TCargadeTabelasPersistentModel.CreateFdOdataMemTable: TODataFDMemTable;
  109.   var t: TODataFDMemTable;
  110. begin
  111.   try
  112.     t := TODataFDMemTable.Create(Nil);
  113.     t.CreateDataSet;
  114.     t.Active:= False;
  115.     t.AutoCalcFields:= False;
  116.     t.ResourceOptions.SilentMode:= True;
  117.     t.ResourceOptions.AssignedValues:= [rvSilentMode];
  118.     t.ObjectView:= True;
  119.     t.StoreDefs:= True;
  120.     t.AutoCalcFields:= true;
  121.     t.UpdateOptions.CheckRequired:= False;
  122.     t.UpdateOptions.RequestLive:= true;
  123.     t.UpdateOptions.UpdateChangedFields:= True;
  124.     result:= t;
  125.   finally
  126.     t.DisposeOf;
  127.   end;
  128. end;
  129.  
  130. function TCargadeTabelasPersistentModel.CreateIdHTTPRestClient: TIdHTTPRestClient;
  131.  var HTTP: TIdHTTPRestClient;
  132. begin
  133.      Http:= TIdHTTPRestClient.Create(Nil);
  134.      Http.Accept:= 'application/json';
  135.      Http.AcceptCharset:= 'UTF-8';
  136.      Http.AcceptEncoding:= 'gzip';
  137.      http.BaseURL:= 'http://'+DtmConexao.TDtmConexao.IPServidor+':8080';
  138.      http.Method:= rmGET;
  139.      http.Resource:= '/CADPRODUTOS?$skip=100&$count=true';
  140.      http.ResourcePrefix:= '/OData/OData.svc';
  141.      http.Timeout:= 360000;
  142.      try
  143.       result := Http;
  144.      finally
  145.        http.DisposeOf;
  146.      end;
  147. end;
  148.  
  149. destructor TCargadeTabelasPersistentModel.Destroy;
  150. begin
  151.   inherited;
  152. end;
  153. function TCargadeTabelasPersistentModel.ExtraiTableProdonserver: TDataset;
  154. begin
  155.  
  156.  try
  157.   DtmConexao.TDtmConexao.InvokeTlCon;
  158.  finally
  159.   if DtmConexao.TDtmConexao.InvokeTlCon = True then begin
  160.      FTFDStorageFormat:= sfJson;
  161. //     FFdStanStorage:= TFDStanStorageJSONLink.Create(Nil);
  162.      TableProdutos:= TFDquery.Create(Nil);
  163.      TableProdutos.Connection := DtmConexao.TDtmConexao.FDServidorConexao;
  164.      TableProdutos.SQL.Clear;
  165.      TableProdutos.SQL.Text := aSql;
  166.      TableProdutos.Open();
  167.      try
  168.      TableProdutos.SaveToFile('C:\Leopard\Tmp\Jsonteste.json');
  169.      finally
  170.       TableProdutos.Close;
  171.       TableProdutos.DisposeOf;
  172. //      FFdStanStorage.DisposeOf;
  173.       end;
  174.   end;
  175.  end;
  176. end;
  177.  
  178. function TCargadeTabelasPersistentModel.ThisAs: TCargadeTabelasPersistentModel;
  179. begin
  180.   result := self;
  181. end;
  182.  
  183. class function TCargadeTabelasPersistentModel.new():ICargadeTabelasPersistentModel;
  184. begin
  185.      result := new(nil);
  186. end;
  187.  
  188. class function TCargadeTabelasPersistentModel.new(const AController:IController):ICargadeTabelasPersistentModel;
  189. begin
  190.    result :=  TCargadeTabelasPersistentModel.create;
  191.    result.controller(AController);
  192. end;
  193.  
  194. function TCargadeTabelasPersistentModel.RetornaDatasetJSon: Tstream;
  195.   var oStr: TStream; var Adapterresp: TODataDatasetAdapter; Builderresp: TODataBuilder; HTTP: TIdHTTPRestClient;
  196.       T: TODataFDMemTable;
  197. begin
  198.  try
  199.   T := CreateFdOdataMemTable;
  200.   Adapterresp := CreateAdapterOdata(t);
  201.   Builderresp:=  CrateOdataBuilder;
  202.   Http:= Self.CreateIdHTTPRestClient;
  203.   Builderresp.RestClient:= Http;
  204.   Adapterresp.Builder := Builderresp;
  205.   Adapterresp.ResponseJSON:= Http;
  206.   Adapterresp.Execute;
  207.   Adapterresp.ClearChanges;
  208.   Adapterresp.Active:= true;
  209.  
  210.   Adapterresp.CreateDatasetFromJson(Http.Content);
  211.   showMessage(Http.Content);
  212.   T.Active := False;
  213.   T.SaveToStream(oStr, sfBinary);
  214.  
  215.   Result := oStr;
  216.  finally
  217.     Adapterresp.DisposeOf;
  218.     Builderresp.DisposeOf;
  219.     http.DisposeOf;
  220.     T.DisposeOf;
  221.  end;
  222. end;
  223.  
  224.  
  225. Initialization
  226. TMVCRegister.RegisterType<ICargadeTabelasPersistentModel,TCargadeTabelasPersistentModel>(TCargadeTabelasPersistentModel.classname,true);
  227. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement