summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/fcl-web/src/webdata/fpextjs.pp
blob: 1d74653b5d40049e87b3c7aa4a7d75a2e3a058f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
unit fpextjs;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, fphttp, db, httpdefs, fpwebdata;

Type


  { TExtJSDataFormatter }

  TExtJSDataFormatter = Class(TCustomHTTPDataContentProducer)
  private
    FMP: String;
    FPLP: String;
    FPSP: String;
    FRP: String;
    FSP: String;
    FTP: String;
    function IsMessageStored: boolean;
    function IsPageLimitStored: boolean;
    function IsPageStartStored: boolean;
    function IsRowsStored: boolean;
    Function IsSuccessStored : Boolean;
    function IsTotalStored: boolean;
  Public
    Constructor Create(AOwner : TComponent); override;
    Procedure DoReadRecords(Stream : TStream); override;
    Function ProduceContent : String; override;
    Property SuccessProperty : String Read FSP Write FSP stored IsSuccessStored;
    Property PageStartProperty : String Read FPSP Write FPSP stored IsPageStartStored;
    Property PageLimitProperty : String Read FPLP Write FPLP stored IsPageLimitStored;
    Property RowsProperty : String Read FRP Write FRP stored IsRowsStored;
    Property MessageProperty : String Read FMP Write FMP stored IsMessageStored;
    Property TotalProperty : String Read FTP Write FTP stored IsTotalStored;
  end;

Const
  // Do not localize these constants.
  DefRowsProperty      = 'rows';
  DefPageLimitProperty = 'limit';
  DefPageStartProperty = 'start';
  DefSuccessProperty   = 'success';
  DefMessageProperty   = 'message';
  DefTotalProperty     = 'total';

implementation

Resourcestring
  SErrNoAdaptor = 'No adaptor available';

{ TExtJSDataFormatter }

function TExtJSDataFormatter.IsSuccessStored: Boolean;
begin
  Result:=(SuccessProperty<>DefSuccessProperty);
end;

function TExtJSDataFormatter.IsTotalStored: boolean;
begin
  Result:=(TotalProperty<>DefTotalProperty);
end;

function TExtJSDataFormatter.IsMessageStored: boolean;
begin
  Result:=(MessageProperty<>DefMessageProperty);
end;

function TExtJSDataFormatter.IsPageLimitStored: boolean;
begin
  Result:=(PageLimitProperty<>DefPageLimitProperty);
end;

function TExtJSDataFormatter.IsPageStartStored: boolean;
begin
  Result:=(PageStartProperty<>DefPageStartProperty);
end;

function TExtJSDataFormatter.IsRowsStored: boolean;
begin
  Result:=(RowsProperty<>DefRowsProperty);
end;

constructor TExtJSDataFormatter.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  RowsProperty:=DefRowsProperty;
  PageLimitProperty:=DefPageLimitProperty;
  PageStartProperty:=DefPageStartProperty;
  SuccessProperty:=DefSuccessProperty;
  MessageProperty:=DefMessageProperty;
  TotalProperty:=DefTotalProperty;
end;

procedure TExtJSDataFormatter.DoReadRecords(Stream: TStream);

Var
  I : Integer;
  L : TStrings;
  S : String;
begin

  If AllowPageSize then
    begin
    If Not Assigned(Adaptor) then
      Raise EFPHTTPError.Create(SErrNoAdaptor);
    if Adaptor.TryFieldValue(PageStartProperty,S) then
      begin
      I:=StrToIntDef(S,-1);
      If I<>-1 then
        PageStart:=I;
      end;
    if Adaptor.TryFieldValue(PageLimitProperty,S) then
      begin
      I:=StrToIntDef(S,-1);
      If I<>-1 then
        PageSize:=I;
      end;
    end;
  Inherited DoReadRecords(Stream);
end;

function TExtJSDataFormatter.ProduceContent: String;

Var
  S : TStringStream;

begin
  S:=TStringStream.Create('');
  try
    ContentToStream(S);
    Result:=S.DataString;
  finally
    FreeAndNil(S);
  end;
end;


end.