Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- { //************************************************************// }
- { // // }
- { // Carlos Alberto Dias da S. Filho // }
- { // // }
- { // Projeto Leopard Report
- { Objetivo: Criar os Objetos de conexao com o MVCbrODataserver }
- { e gerar um JsonObject do Dataset para Alimentar }
- { o FDquery e as classes/ Objetos responsaveis por todas as dinãmicas
- { da carga no PDV. }
- { // // }
- { //************************************************************// }
- { // Data: 15/08/2017 21:17:45 // }
- { //************************************************************// }
- Unit CargadeTabelas.PersistentModel;
- 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,Json,
- FireDAC.UI.Intf, FireDAC.VCLUI.Wait, FireDAC.Comp.UI,
- Datasnap.DBClient, MVCBr.ODataDatasetBuilder, MVCBr.ODataFDMemTable,
- MVCBr.HTTPRestClient.Common,FireDAC.Stan.StorageJson;
- Type
- TCargadeTabelasPersistentModel = class(TPersistentModelFactory,
- ICargadeTabelasPersistentModel, IThisAs<TCargadeTabelasPersistentModel>)
- private
- FTableProdutos: TFDquery;
- FTFDStorageFormat: TFDStorageFormat;
- FFDGUIxWaitCursor1: TFDGUIxWaitCursor;
- FOdataBuilder: TODataBuilder;
- FHttpRest: TIdHTTPRestClient;
- FOdataMemTable: TODataFDMemTable;
- FAdapterOdata: TODataDatasetAdapter;
- FFDStanStorageBinLink1: TFDStanStorageJsonLink;
- protected
- public
- Constructor Create; override;
- Destructor Destroy; override;
- class function new(): ICargadeTabelasPersistentModel; overload;
- class function new(const AController: IController)
- : ICargadeTabelasPersistentModel; overload;
- function ThisAs: TCargadeTabelasPersistentModel;
- property FDStorageFormat: TFDStorageFormat read FTFDStorageFormat write FTFDStorageFormat;
- property FDGUIxWaitCursor1: TFDGUIxWaitCursor read FFDGUIxWaitCursor1 write FFDGUIxWaitCursor1;
- property AdapterOdata: TODataDatasetAdapter read FAdapterOdata write FAdapterOdata;
- property OdataBuilder: TODataBuilder read FOdataBuilder write FOdataBuilder;
- property HttpRest: TIdHTTPRestClient read FHttpRest write FHttpRest;
- property OdataMemTable: TODataFDMemTable read FOdataMemTable write FOdataMemTable;
- /// <summary> Cria os objetos TODataDatasetAdapter,TODataBuilder,TIdHTTPRestClient,TODataFDMemTable </summary>
- /// <summary> parametrizando os Objetos e conectando ao servidor. </summary>
- Function RetornaDatasetJSon: TJSONObject;
- /// <summary> Cria e parametriza o TODataDatasetAdapter, o Objeto está sendo referenciado na property FAdapterOdata </summary>
- procedure CreateAdapterOdata;
- /// <summary> Cria e parametriza o TODataBuilder, o Objeto está sendo referenciado na property FOdataBuilder </summary>
- procedure CreateOdataBuilder;
- /// <summary> Cria e parametriza o TIdHTTPRestClient, o Objeto está sendo referenciado na property FHttpRest </summary>
- procedure CreateOdataTIdHTTPRestClient;
- /// <summary> Cria e parametriza o TODataFDMemTable, o Objeto está sendo referenciado na property FOdataMemTable </summary>
- procedure CreateDataMemtable;
- // implementaçoes
- end;
- Implementation
- uses DtmConexao;
- constructor TCargadeTabelasPersistentModel.Create;
- var
- oStr: TJsonObject;
- begin
- inherited;
- ModelTypes := [mtPersistent];
- oStr := RetornaDatasetJSon;
- end;
- procedure TCargadeTabelasPersistentModel.CreateAdapterOdata;
- begin
- FAdapterOdata := TODataDatasetAdapter.Create(nil);
- FAdapterOdata.RootElement := 'value';
- FAdapterOdata.ResponseType := pureJSON;
- end;
- procedure TCargadeTabelasPersistentModel.CreateDataMemtable;
- begin
- FOdataMemTable := TODataFDMemTable.Create(Nil);
- FOdataMemTable.Active := False;
- FOdataMemTable.AutoCalcFields := False;
- FOdataMemTable.ResourceOptions.SilentMode := True;
- FOdataMemTable.ResourceOptions.AssignedValues := [rvSilentMode];
- FOdataMemTable.ObjectView := True;
- FOdataMemTable.StoreDefs := True;
- FOdataMemTable.AutoCalcFields := True;
- FOdataMemTable.UpdateOptions.CheckRequired := False;
- FOdataMemTable.UpdateOptions.RequestLive := True;
- FOdataMemTable.UpdateOptions.UpdateChangedFields := True;
- end;
- procedure TCargadeTabelasPersistentModel.CreateOdataBuilder;
- begin
- FOdataBuilder := TODataBuilder.Create(Nil);
- FOdataBuilder.Service := '/OData.svc';
- FOdataBuilder.ServicePrefix := '/OData';
- FOdataBuilder.Count := True;
- FOdataBuilder.Resource.Add;
- FOdataBuilder.Resource.Items[0].Resource:= 'CADPRODUTOS';
- FOdataBuilder.ResourceName := 'CADPRODUTOS';
- FOdataBuilder.SkipRows := 0;
- FOdataBuilder.TopRows := 100;
- FOdataBuilder.BaseURL := 'http://localhost:8080';
- end;
- procedure TCargadeTabelasPersistentModel.CreateOdataTIdHTTPRestClient;
- begin
- FHttpRest := TIdHTTPRestClient.Create(Nil);
- FHttpRest.Accept := 'application/json;odata.metadata=minimal';
- FHttpRest.AcceptCharset := 'UTF-8';
- FHttpRest.AcceptEncoding := 'gzip';
- FHttpRest.BaseURL := 'http://localhost:8080';
- FHttpRest.Method := rmGET;
- FHttpRest.Timeout := 360000;
- end;
- destructor TCargadeTabelasPersistentModel.Destroy;
- begin
- inherited;
- 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: TJSONObject;
- var
- oStr: TJSONObject;
- begin
- try
- CreateAdapterOdata;
- CreateOdataBuilder;
- CreateOdataTIdHTTPRestClient;
- self.CreateDataMemtable;
- FOdataBuilder.RestClient := FHttpRest;
- FAdapterOdata.Builder := FOdataBuilder;
- FAdapterOdata.ResponseJSON := FHttpRest;
- FAdapterOdata.RootElement := 'value';
- FAdapterOdata.DataSet := self.FOdataMemTable;
- FAdapterOdata.execute;
- FAdapterOdata.ClearChanges;
- FOdataMemTable.SaveTofile('C:\Leopard\TMP\J.Json', sfJson);
- result := oStr;
- finally
- FAdapterOdata.DisposeOf;
- self.FHttpRest.DisposeOf;
- self.FOdataMemTable.DisposeOf;
- self.FOdataBuilder.DisposeOf;
- end;
- end;
- Initialization
- TMVCRegister.RegisterType<ICargadeTabelasPersistentModel,
- TCargadeTabelasPersistentModel>
- (TCargadeTabelasPersistentModel.classname, True);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement