123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- unit wmusers;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, SysUtils, HTTPDefs, websession, fpHTTP, fpWeb,
- db, dbf, fpwebdata, fpextjs,extjsjson,extjsxml;
- type
- { TFPWebModule1 }
- TFPWebModule1 = class(TFPWebModule)
- Dbf1: TDbf;
- procedure TFPWebActions0Request(Sender: TObject; ARequest: TRequest;
- AResponse: TResponse; var Handled: Boolean);
- procedure TFPWebActions1Request(Sender: TObject; ARequest: TRequest;
- AResponse: TResponse; var Handled: Boolean);
- procedure TFPWebActions2Request(Sender: TObject; ARequest: TRequest;
- AResponse: TResponse; var Handled: Boolean);
- procedure TFPWebActions3Request(Sender: TObject; ARequest: TRequest;
- AResponse: TResponse; var Handled: Boolean);
- private
- { private declarations }
- procedure GetAdaptorAndFormatter(P : TFPWebDataProvider; Var F :TExtJSDataFormatter; ARequest : TRequest; AResponse : TResponse);
- public
- { public declarations }
- end;
- var
- FPWebModule1: TFPWebModule1;
- Var
- ResponseFileName : String; // Set to non empty to write request responses to a file.
- implementation
- {$R *.lfm}
- {$define wmdebug}
- {$ifdef wmdebug}
- uses dbugintf;
- {$endif}
- { TFPWebModule1 }
- Procedure SaveResponse(M : TStream);
- begin
- if (ResponseFileName<>'') then
- With TFileStream.Create(ResponseFileName,fmCreate) do
- try
- CopyFrom(M,0);
- finally
- Free;
- end;
- end;
- procedure TFPWebModule1.GetAdaptorAndFormatter(P : TFPWebDataProvider; Var F :TExtJSDataFormatter; ARequest : TRequest; AResponse : TResponse);
- begin
- If Request.QueryFields.values['format']='xml' then
- begin
- F:=TExtJSXMLDataFormatter.Create(Self);
- TExtJSXMLDataFormatter(F).TotalProperty:='total';
- AResponse.ContentType:='text/xml';
- P.Adaptor:=TExtJSXMLWebdataInputAdaptor.Create(Nil);
- end
- else
- begin
- P.Adaptor:=TExtJSJSonWebdataInputAdaptor.Create(Nil);
- F:=TExtJSJSONDataFormatter.Create(Self);
- end;
- P.Adaptor.Request:=ARequest;
- F.Adaptor:=P.Adaptor;
- F.Provider:=P;
- end;
- procedure TFPWebModule1.TFPWebActions0Request(Sender: TObject;
- ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
- Var
- PN : String;
- P : TFPWebDataProvider;
- F : TExtJSDataFormatter;
- DS : TDatasource;
- M : TMemoryStream;
- L : Text;
- begin
- // Providername;
- PN:=ARequest.GetNextPathInfo;
- P:=TFPWebDataProvider.Create(Self);
- try
- GetAdaptorAndFormatter(P,F,ARequest,AResponse);
- {$ifdef wmdebug} SendDebug(className+' '+F.ClassName);{$endif}
- try
- DS:=TDatasource.Create(Self);
- try
- DBF1.TableName:=ExtractFilePath(ParamStr(0))+'users.dbf';
- DS.Dataset:=DBf1;
- DBF1.Open;
- try
- P.Datasource:=DS;
- P.Adaptor.Action:=wdaRead;
- P.ApplyParams;
- M:=TMemoryStream.Create;
- try
- F.GetContent(ARequest,M,Handled);
- M.Position:=0;
- Response.ContentStream:=M;
- Response.SendResponse;
- Response.ContentStream:=Nil;
- SaveResponse(M);
- finally
- M.Free;
- end;
- finally
- DBF1.Close;
- end;
- finally
- DS.Free;
- end;
- finally
- F.Free;
- end;
- finally
- P.Free;
- end;
- end;
- procedure TFPWebModule1.TFPWebActions1Request(Sender: TObject;
- ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
- Var
- PN : String;
- P : TFPWebDataProvider;
- F : TExtJSDataFormatter;
- DS : TDatasource;
- M : TMemoryStream;
- L : Text;
- begin
- // Providername;
- PN:=ARequest.GetNextPathInfo;
- // P:=GetWebDataProvider(PN);
- P:=TFPWebDataProvider.Create(Self);
- try
- P.IDFieldName:='ID';
- GetAdaptorAndFormatter(P,F,ARequest,AResponse);
- {$ifdef wmdebug} SendDebug(className+' '+F.ClassName);{$endif}
- try
- DS:=TDatasource.Create(Self);
- try
- DBF1.TableName:=ExtractFilePath(ParamStr(0))+'users.dbf';
- DS.Dataset:=DBf1;
- DBF1.Open;
- try
- P.Datasource:=DS;
- P.Adaptor.Action:=wdaInsert;
- P.ApplyParams;
- M:=TMemoryStream.Create;
- try
- F.GetContent(ARequest,M,Handled);
- M.Position:=0;
- Response.ContentStream:=M;
- Response.SendResponse;
- Response.ContentStream:=Nil;
- SaveResponse(M);
- finally
- M.Free;
- end;
- finally
- DBF1.Close;
- end;
- finally
- DS.Free;
- end;
- finally
- F.Free;
- end;
- finally
- P.Free;
- end;
- end;
- procedure TFPWebModule1.TFPWebActions2Request(Sender: TObject;
- ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
- Var
- PN : String;
- P : TFPWebDataProvider;
- F : TExtJSDataFormatter;
- DS : TDatasource;
- M : TMemoryStream;
- L : Text;
- begin
- // Providername;
- {$ifdef wmdebug} SendDebug('Update request received');{$endif}
- PN:=ARequest.GetNextPathInfo;
- // P:=GetWebDataProvider(PN);
- P:=TFPWebDataProvider.Create(Self);
- try
- P.IDFieldName:='ID';
- GetAdaptorAndFormatter(P,F,ARequest,AResponse);
- {$ifdef wmdebug} SendDebug(className+' '+F.ClassName);{$endif}
- try
- DS:=TDatasource.Create(Self);
- try
- DBF1.TableName:=ExtractFilePath(ParamStr(0))+'users.dbf';
- DS.Dataset:=DBf1;
- DBF1.Open;
- try
- P.Datasource:=DS;
- P.Adaptor.Action:=wdaUpdate;
- P.ApplyParams;
- M:=TMemoryStream.Create;
- try
- F.GetContent(ARequest,M,Handled);
- M.Position:=0;
- Response.ContentStream:=M;
- Response.SendResponse;
- Response.ContentStream:=Nil;
- SaveResponse(M);
- finally
- M.Free;
- end;
- finally
- DBF1.Close;
- end;
- finally
- DS.Free;
- end;
- finally
- F.Free;
- end;
- finally
- P.Free;
- end;
- end;
- procedure TFPWebModule1.TFPWebActions3Request(Sender: TObject;
- ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);
- Var
- PN : String;
- P : TFPWebDataProvider;
- F : TExtJSDataFormatter;
- DS : TDatasource;
- M : TMemoryStream;
- L : Text;
- begin
- // Providername;
- PN:=ARequest.GetNextPathInfo;
- // P:=GetWebDataProvider(PN);
- P:=TFPWebDataProvider.Create(Self);
- try
- P.IDFieldName:='ID';
- GetAdaptorAndFormatter(P,F,ARequest,AResponse);
- {$ifdef wmdebug} SendDebug('className '+F.ClassName);{$endif}
- try
- DS:=TDatasource.Create(Self);
- try
- DBF1.TableName:=ExtractFilePath(ParamStr(0))+'users.dbf';
- DS.Dataset:=DBf1;
- DBF1.Open;
- try
- P.Datasource:=DS;
- P.Adaptor.Action:=wdaDelete;
- P.ApplyParams;
- M:=TMemoryStream.Create;
- try
- F.GetContent(ARequest,M,Handled);
- M.Position:=0;
- Response.ContentStream:=M;
- Response.SendResponse;
- Response.ContentStream:=Nil;
- SaveResponse(M);
- finally
- M.Free;
- end;
- finally
- DBF1.Close;
- end;
- finally
- DS.Free;
- end;
- finally
- F.Free;
- end;
- finally
- P.Free;
- end;
- end;
- initialization
- RegisterHTTPModule('Provider', TFPWebModule1);
- end.
|