-
Notifications
You must be signed in to change notification settings - Fork 15
/
rpcompobase.pas
453 lines (409 loc) · 10.9 KB
/
rpcompobase.pas
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
{*******************************************************}
{ }
{ Report Manager }
{ }
{ rpcompobase }
{ Report component base }
{ }
{ }
{ Copyright (c) 1994-2019 Toni Martir }
{ }
{ This file is under the MPL license }
{ If you enhace this file you must provide }
{ source code }
{ }
{ }
{*******************************************************}
unit rpcompobase;
interface
{$I rpconf.inc}
uses Classes,Sysutils,rpreport,rpmdconsts,
rpalias,rpsubreport,rpsection,rpprintitem,rptypes,
rpdatainfo,
rphtmldriver,rpcsvdriver,rpsvgdriver,
{$IFDEF USEINDY}
rpmdrepclient,rpparams,SyncObjs,
{$ENDIF}
rpmetafile;
type
TCBaseReport=class(TComponent)
private
FFilename:TFilename;
FConnectionName:String;
FReport:TRpReport;
FPreview:Boolean;
FShowProgress:Boolean;
FTitle:WideString;
FAliasList:TRpAlias;
FShowPrintDialog:boolean;
FLanguage:integer;
FOnBeforePrint:TNotifyEvent;
FReportName:String;
FAsyncExecution:Boolean;
{$IFDEF USEINDY}
FRemoteError:Boolean;
FRemoteMessage:WideString;
procedure OnRemoteError(Sender:TObject;aMessage:WideString);
procedure OnGetParams(astream:TMemoryStream);
{$ENDIF}
procedure InternalSetBeforePrint;
function GetReport:TRpReport;
procedure SetFileName(Value:TFilename);
procedure SetOnBeforePrint(NewValue:TNotifyEvent);
procedure SetConnectionName(Value:String);
procedure SetReportName(Value:String);
procedure SetAsyncExecution(avalue:boolean);
procedure SetLanguage(Value:Integer);
protected
procedure Notification(AComponent: TComponent; Operation: TOperation);override;
procedure InternalExecuteRemote(metafile:TRpMetafileReport);virtual;
public
function Execute:boolean;virtual;
procedure PrinterSetup;virtual;abstract;
constructor Create(AOwner:TComponent);override;
procedure CheckLoaded;
function ShowParams:boolean;virtual;abstract;
function PrintRange(frompage:integer;topage:integer;
copies:integer;collate:boolean):boolean;virtual;abstract;
// Defined as public but will be published in descendants
procedure SaveToText(filename:string;textdriver:String='');virtual;abstract;
procedure SaveToHTML(filename:string);
procedure SaveToHTMLSingle(filename:string);
procedure SaveToCustomText(filename:string);
procedure SaveToSVG(filename:string);
procedure SaveToCSV(filename:string;separator:string=',');
procedure LoadFromFile(AFilename:string);
procedure LoadFromStream(stream:TStream);
procedure ExecuteRemote(hostname:String;port:integer;user,password,aliasname,reportname:String);
procedure GetRemoteParams(hostname:String;port:integer;user,password,aliasname,reportname:String);
property Report:TRpReport read GetReport;
property Preview:Boolean read FPreview write FPreview default true;
property ShowProgress:boolean read FShowProgress write FShowProgress
default true;
property Title:widestring read FTitle write FTitle;
property ShowPrintDialog:boolean read FShowPrintDialog
write FShowPrintDialog default true;
property AliasList:TRpAlias read FAliasList write FAliasList;
property Language:integer read FLanguage write SetLanguage default -1;
published
property Filename:TFilename read FFilename write SetFilename;
property ConnectionName:String read FConnectionName write SetConnectionName;
property AsyncExecution:Boolean read FAsyncExecution write SetAsyncExecution;
property ReportName:String read FReportName write SetReportName;
property OnBeforePrint:TNotifyEvent read FOnBeforePrint
write SetOnBeforePrint;
end;
implementation
constructor TCBaseReport.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
FShowProgress:=true;
FFilename:='';
FPreview:=true;
FTitle:=SRpUntitled;
FShowPrintDialog:=True;
FLanguage:=-1;
end;
procedure TCBaseReport.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited Notification(AComponent,Operation);
if Assigned(AComponent) then
if Operation=OpRemove then
if AComponent=FAliasList then
FAliasList:=nil;
end;
procedure TCBaseReport.SetFileName(Value:TFilename);
begin
if (csloading in ComponentState) then
begin
FFilename:=Value;
exit;
end;
if FFilename<>Value then
begin
if Assigned(FReport) then
begin
FReport.free;
FReport:=nil;
end;
FFilename:=Value;
end;
end;
function TCBaseReport.GetReport:TRpReport;
begin
CheckLoaded;
Result:=FReport;
end;
procedure TCBaseReport.CheckLoaded;
var
dblist:TRpDatabaseInfoList;
astream:TStream;
begin
// Loads the report
if Assigned(FReport) then
begin
FReport.AliasList:=AliasList;
if FLanguage>=0 then
FReport.Language:=FLanguage;
exit;
end;
if Length(FFilename)>0 then
begin
LoadFromFile(FFilename);
end
else
begin
if Length(ConnectionName)<1 then
Raise Exception.Create(SRpNoFilename);
if Length(ReportName)<1 then
Raise Exception.Create(SRpNoFilename);
if Not Assigned(AliasList) then
Raise Exception.Create(SRpPRpAliasRequired);
dblist:=AliasList.Connections;
astream:=dblist.GetReportStream(ConnectionName,ReportName,nil);
try
LoadFromStream(astream);
finally
astream.free;
end;
end;
InternalSetBeforePrint;
if Assigned(FReport) then
begin
FReport.AliasList:=AliasList;
end;
end;
procedure TCBaseReport.SetOnBeforePrint(NewValue:TNotifyEvent);
begin
FOnBeforePrint:=NewValue;
InternalSetBeforePrint;
end;
procedure TCBaseReport.SetLanguage(Value:Integer);
begin
FLanguage:=Value;
if Assigned(FReport) then
if FLanguage>=0 then
FReport.Language:=FLanguage;
end;
function TCBaseReport.Execute:boolean;
begin
CheckLoaded;
Result:=false;
end;
procedure TCBaseReport.LoadFromFile(AFilename:string);
var
astream:TFileStream;
begin
astream:=TFileStream.Create(AFilename,fmOpenRead or fmShareDenyNone);
try
LoadFromStream(astream);
finally
astream.free;
end;
end;
procedure TCBaseReport.LoadFromStream(stream:TStream);
begin
if Assigned(FReport) then
begin
FReport.Free;
FReport:=nil;
end;
FReport:=TRpReport.Create(Self);
try
FReport.LoadFromStream(Stream);
InternalSetBeforePrint;
Freport.AsyncExecution:=FAsyncExecution;
except
FReport.Free;
FReport:=nil;
raise;
end;
end;
procedure TCBaseReport.InternalSetBeforePrint;
var
i,j,k:integer;
subrep:TRpSubReport;
sec:TRpSection;
begin
If not Assigned(FReport) then
exit;
for i:=0 to FReport.SubReports.Count-1 do
begin
subrep:=FReport.SubReports.Items[i].SubReport;
for j:=0 to subrep.Sections.Count-1 do
begin
sec:=subrep.Sections.Items[j].Section;
sec.OnBeforePrint:=FOnBeforePrint;
for k:=0 to sec.ReportComponents.Count-1 do
begin
TRpCommonComponent(sec.ReportComponents.Items[k].Component).OnBeforePrint:=FOnBeforePrint;
end;
end;
end;
end;
procedure TCBaseReport.InternalExecuteRemote(metafile:TRpMetafileReport);
begin
// Implemented in derived classes
end;
{$IFDEF USEINDY}
procedure TCBaseReport.OnRemoteError(Sender:TObject;aMessage:WideString);
begin
Fremoteerror:=true;
Fremotemessage:=aMessage;
end;
{$ENDIF}
{$IFDEF USEINDY}
procedure TCBaseReport.OnGetParams(astream:TMemoryStream);
var
acompo:TRpParamComp;
reader:TReader;
begin
acompo:=TRpParamComp.Create(nil);
try
reader:=TReader.Create(astream,4096);
try
reader.ReadRootComponent(acompo);
if assigned(FReport) then
begin
FReport.free;
FReport:=nil;
end;
FReport:=TRpReport.Create(Self);
FReport.Params.Assign(acompo.params);
finally
reader.free;
end;
finally
acompo.free;
end;
end;
{$ENDIF}
procedure TCBaseReport.GetRemoteParams(hostname:String;port:integer;user,password,aliasname,reportname:String);
{$IFDEF USEINDY}
var
client:Tmodclient;
{$ENDIF}
begin
{$IFDEF USEINDY}
client:=Connect(hostname,user,password,port);
try
client.threadsafeexec:=true;
client.OnError:=OnRemoteError;
client.OnGetParams:=OnGetParams;
client.OpenReport(aliasname,reportname);
client.GetParams;
finally
Disconnect(client);
end;
{$ENDIF}
end;
procedure TCBaseReport.ExecuteRemote(hostname:String;port:integer;user,password,aliasname,reportname:String);
{$IFDEF USEINDY}
var
client:Tmodclient;
metafile:TRpMetafileReport;
acompo:TRpParamComp;
{$ENDIF}
begin
{$IFDEF USEINDY}
client:=Connect(hostname,user,password,port);
try
metafile:=TRpMetafileReport.Create(nil);
try
client.threadsafeexec:=true;
Fremoteerror:=false;
client.OnError:=OnRemoteError;
client.OpenReport(aliasname,reportname);
if Assigned(FReport) then
begin
acompo:=TRpParamComp.Create(nil);
try
acompo.Params.Assign(FReport.Params);
client.ModifyParams(acompo);
finally
acompo.free;
end;
end;
client.Execute;
// client.Execute(aliasname,reportname);
if Fremoteerror then
Raise Exception.Create(Fremotemessage);
client.Stream.Seek(0,soFromBeginning);
metafile.LoadFromStream(client.Stream);
InternalExecuteRemote(metafile);
finally
metafile.free;
end;
finally
Disconnect(client);
end;
{$ENDIF}
end;
procedure TCBaseReport.SetAsyncExecution(avalue:boolean);
begin
FAsyncExecution:=aValue;
if Assigned(FReport) then
begin
FReport.AsyncExecution:=FAsyncExecution;
end;
end;
procedure TCBaseReport.SetReportName(Value:String);
begin
if (csloading in ComponentState) then
begin
FReportName:=Value;
exit;
end;
if FReportname<>Value then
begin
if Assigned(FReport) then
begin
FReport.free;
FReport:=nil;
end;
FReportName:=Value;
if Length(FReportName)>0 then
FFilename:='';
end;
end;
procedure TCBaseReport.SetConnectionName(Value:String);
begin
if (csloading in ComponentState) then
begin
FConnectionName:=Value;
exit;
end;
if FConnectionName<>Value then
begin
if Assigned(FReport) then
begin
FReport.free;
FReport:=nil;
end;
FConnectionName:=Value;
if Length(FConnectionName)>0 then
FFilename:='';
end;
end;
procedure TCBaseReport.SaveToHTML(filename:string);
begin
ExportReportToHtml(report,filename,showprogress);
end;
procedure TCBaseReport.SaveToHTMLSingle(filename:string);
begin
ExportReportToHtmlSingle(report,filename);
end;
procedure TCBaseReport.SaveToCustomText(filename:string);
begin
ExportReportToTextPro(report,filename,showprogress);
end;
procedure TCBaseReport.SaveToSVG(filename:string);
begin
ExportReportToSVG(report,filename,showprogress);
end;
procedure TCBaseReport.SaveToCSV(filename:string;separator:string=',');
begin
ExportReportToCSV(report,filename,showprogress,separator);
end;
end.