Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Unit CargadeTabelas.PersistentModel;
- { MVCBr
- www.tireideletra.com.br
- amarildo lacerda
- }
- interface
- {.$I ..\inc\mvcbr.inc}
- uses System.SysUtils,{$ifdef FMX} FMX.Forms,{$else} VCL.Forms,{$endif} System.Classes, MVCBr.Interf, MVCBr.PersistentModel,
- CargadeTabelas.PersistentModel.Interf, //%Interf,
- MVCBr.Controller,FireDAC.Phys.FBDef,Dialogs,
- IPPeerClient, Data.DB,
- FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS,
- FireDAC.Phys.Intf, FireDAC.DApt.Intf, FireDAC.Comp.DataSet,
- FireDAC.Comp.Client, MVCBr.ODataDatasetAdapter, oData.Comp.Client,
- FireDAC.Stan.Intf, FireDAC.Stan.Option, MVCBr.IdHTTPRestClient,
- VCL.Controls,VCL.StdCtrls,
- FireDAC.UI.Intf, FireDAC.VCLUI.Wait, FireDAC.Comp.UI,
- Datasnap.DBClient, MVCBr.ODataDatasetBuilder, MVCBr.ODataFDMemTable, MVCBr.HTTPRestClient.Common;
- Type
- TCargadeTabelasPersistentModel = class(TPersistentModelFactory,ICargadeTabelasPersistentModel,IThisAs<TCargadeTabelasPersistentModel>)
- private
- FTableProdutos: TFDquery;
- FTFDStorageFormat: TFDStorageFormat;
- FFDGUIxWaitCursor1: TFDGUIxWaitCursor;
- protected
- public
- Constructor Create; override;
- Destructor Destroy; override;
- class function new():ICargadeTabelasPersistentModel; overload;
- class function new(const AController:IController):ICargadeTabelasPersistentModel; overload;
- function ThisAs:TCargadeTabelasPersistentModel;
- Function ExtraiTableProdonserver:TDataset;
- property TableProdutos: TFDquery read FTableProdutos write FTableProdutos;
- property FDStorageFormat: TFDStorageFormat read FTFDStorageFormat write FTFDStorageFormat;
- function aSql: String;
- Function RetornaDatasetJSon: Tstream;
- property FDGUIxWaitCursor1: TFDGUIxWaitCursor read FFDGUIxWaitCursor1 write FFDGUIxWaitCursor1;
- function CreateAdapterOdata(Dataset: TODataFDMemTable ): TODataDatasetAdapter;
- function CrateOdataBuilder: TODataBuilder;
- function CreateIdHTTPRestClient: TIdHTTPRestClient;
- function CreateFdOdataMemTable: TODataFDMemTable;
- // implementaçoes
- end;
- Implementation
- uses DtmConexao;
- function TCargadeTabelasPersistentModel.aSql: String;
- begin
- result := 'select * from CADPRODUTOS ';
- end;
- function TCargadeTabelasPersistentModel.CrateOdataBuilder: TODataBuilder;
- var Builder: TODataBuilder;
- begin
- Builder:= TODataBuilder.Create(Nil);
- Builder.Service := '/OData.svc';
- Builder.ServicePrefix:= '/OData';
- Builder.Count:= True;
- Builder.Resource.addResource('CADPRODUTOS');
- Builder.ResourceName:= 'CADPRODUTOS';
- Builder.SkipRows:= 100;
- Builder.TopRows:= 100;
- Builder.BaseURL := 'http://'+DtmConexao.TDtmConexao.IPServidor+':8080';
- try
- result := Builder;
- finally
- Builder.DisposeOf;
- end;
- end;
- constructor TCargadeTabelasPersistentModel.Create;
- var oStr: TStream;
- begin
- inherited;
- ModelTypes := [mtPersistent];
- // ExtraiTableProdonserver;
- //TableProdutos:= TFDquery.Create(Nil);
- oStr := RetornaDatasetJSon;
- ShowMessage('Teste');
- end;
- function TCargadeTabelasPersistentModel.CreateAdapterOdata(Dataset: TODataFDMemTable ): TODataDatasetAdapter;
- var AdapterNull: TODataDatasetAdapter;
- begin
- try
- Adapternull:= TODataDatasetAdapter.Create(Nil);
- if dataset <> nil then begin
- Adapternull.Dataset:= Dataset;
- end;
- Adapternull.Active:= False;
- Adapternull.RootElement := 'Value';
- Adapternull.ResponseType:= pureJSON;
- finally
- result:= Adapternull;
- Adapternull.DisposeOf;
- end;
- end;
- function TCargadeTabelasPersistentModel.CreateFdOdataMemTable: TODataFDMemTable;
- var t: TODataFDMemTable;
- begin
- try
- t := TODataFDMemTable.Create(Nil);
- t.CreateDataSet;
- t.Active:= False;
- t.AutoCalcFields:= False;
- t.ResourceOptions.SilentMode:= True;
- t.ResourceOptions.AssignedValues:= [rvSilentMode];
- t.ObjectView:= True;
- t.StoreDefs:= True;
- t.AutoCalcFields:= true;
- t.UpdateOptions.CheckRequired:= False;
- t.UpdateOptions.RequestLive:= true;
- t.UpdateOptions.UpdateChangedFields:= True;
- result:= t;
- finally
- t.DisposeOf;
- end;
- end;
- function TCargadeTabelasPersistentModel.CreateIdHTTPRestClient: TIdHTTPRestClient;
- var HTTP: TIdHTTPRestClient;
- begin
- Http:= TIdHTTPRestClient.Create(Nil);
- Http.Accept:= 'application/json';
- Http.AcceptCharset:= 'UTF-8';
- Http.AcceptEncoding:= 'gzip';
- http.BaseURL:= 'http://'+DtmConexao.TDtmConexao.IPServidor+':8080';
- http.Method:= rmGET;
- http.Resource:= '/CADPRODUTOS?$skip=100&$count=true';
- http.ResourcePrefix:= '/OData/OData.svc';
- http.Timeout:= 360000;
- try
- result := Http;
- finally
- http.DisposeOf;
- end;
- end;
- destructor TCargadeTabelasPersistentModel.Destroy;
- begin
- inherited;
- end;
- function TCargadeTabelasPersistentModel.ExtraiTableProdonserver: TDataset;
- begin
- try
- DtmConexao.TDtmConexao.InvokeTlCon;
- finally
- if DtmConexao.TDtmConexao.InvokeTlCon = True then begin
- FTFDStorageFormat:= sfJson;
- // FFdStanStorage:= TFDStanStorageJSONLink.Create(Nil);
- TableProdutos:= TFDquery.Create(Nil);
- TableProdutos.Connection := DtmConexao.TDtmConexao.FDServidorConexao;
- TableProdutos.SQL.Clear;
- TableProdutos.SQL.Text := aSql;
- TableProdutos.Open();
- try
- TableProdutos.SaveToFile('C:\Leopard\Tmp\Jsonteste.json');
- finally
- TableProdutos.Close;
- TableProdutos.DisposeOf;
- // FFdStanStorage.DisposeOf;
- end;
- end;
- end;
- end;
- function TCargadeTabelasPersistentModel.ThisAs: TCargadeTabelasPersistentModel;
- begin
- result := self;
- end;
- class function TCargadeTabelasPersistentModel.new():ICargadeTabelasPersistentModel;
- begin
- result := new(nil);
- end;
- class function TCargadeTabelasPersistentModel.new(const AController:IController):ICargadeTabelasPersistentModel;
- begin
- result := TCargadeTabelasPersistentModel.create;
- result.controller(AController);
- end;
- function TCargadeTabelasPersistentModel.RetornaDatasetJSon: Tstream;
- var oStr: TStream; var Adapterresp: TODataDatasetAdapter; Builderresp: TODataBuilder; HTTP: TIdHTTPRestClient;
- T: TODataFDMemTable;
- begin
- try
- T := CreateFdOdataMemTable;
- Adapterresp := CreateAdapterOdata(t);
- Builderresp:= CrateOdataBuilder;
- Http:= Self.CreateIdHTTPRestClient;
- Builderresp.RestClient:= Http;
- Adapterresp.Builder := Builderresp;
- Adapterresp.ResponseJSON:= Http;
- Adapterresp.Execute;
- Adapterresp.ClearChanges;
- Adapterresp.Active:= true;
- Adapterresp.CreateDatasetFromJson(Http.Content);
- showMessage(Http.Content);
- T.Active := False;
- T.SaveToStream(oStr, sfBinary);
- Result := oStr;
- finally
- Adapterresp.DisposeOf;
- Builderresp.DisposeOf;
- http.DisposeOf;
- T.DisposeOf;
- end;
- end;
- Initialization
- TMVCRegister.RegisterType<ICargadeTabelasPersistentModel,TCargadeTabelasPersistentModel>(TCargadeTabelasPersistentModel.classname,true);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement