source: branches/test1/Client/Application/UDataTypes.pas

Last change on this file was 52, checked in by chronos, 13 years ago
  • Modified: Code related to persistence layer abstraction moved to separated package PersistentData.
File size: 13.8 KB
Line 
1unit UDataTypes;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, Controls, Spin, StdCtrls, ExtCtrls, MaskEdit, EditBtn,
9 UPDClient;
10
11type
12
13 { TDataType }
14
15 TDataType = class
16 Name: string;
17 CustomType: Integer;
18 Control: TWinControl;
19 function CreateControl(Owner: TComponent): TWinControl; virtual;
20 procedure SetupControl(Control: TWinControl); virtual;
21 function GetControlValue(Control: TWinControl): string; virtual;
22 procedure Load(CellValue: string); virtual;
23 procedure LoadDef(ACustomType: Integer); virtual;
24 procedure SetDefault; virtual;
25 function GetStringValue: string; virtual;
26 constructor Create; virtual;
27 end;
28
29 { TDataTypeNumber }
30
31 TDataTypeNumber = class(TDataType)
32 Value: Integer;
33 Default: Integer;
34 Min: Integer;
35 Max: Integer;
36 function CreateControl(Owner: TComponent): TWinControl; override;
37 procedure SetupControl(Control: TWinControl); override;
38 function GetControlValue(Control: TWinControl): string; override;
39 procedure Load(CellValue: string); override;
40 procedure LoadDef(CustomType: Integer); override;
41 constructor Create; override;
42 end;
43
44 { TDataTypeString }
45
46 TDataTypeString = class(TDataType)
47 Value: string;
48 Default: string;
49 MaxLength: Integer;
50 function CreateControl(Owner: TComponent): TWinControl; override;
51 procedure SetupControl(Control: TWinControl); override;
52 function GetControlValue(Control: TWinControl): string; override;
53 procedure Load(CellValue: string); override;
54 constructor Create; override;
55 end;
56
57 { TDataTypePassword }
58
59 TDataTypePassword = class(TDataType)
60 Value: string;
61 Default: string;
62 MaxLength: Integer;
63 function CreateControl(Owner: TComponent): TWinControl; override;
64 procedure SetupControl(Control: TWinControl); override;
65 procedure Load(CellValue: string); override;
66 constructor Create; override;
67 end;
68
69 { TDataTypeBoolean }
70
71 TDataTypeBoolean = class(TDataType)
72 Value: Boolean;
73 Default: Boolean;
74 function CreateControl(Owner: TComponent): TWinControl; override;
75 function GetControlValue(Control: TWinControl): string; override;
76 procedure SetupControl(Control: TWinControl); override;
77 procedure Load(CellValue: string); override;
78 constructor Create; override;
79 end;
80
81 { TDataTypeRelationOne }
82
83 TDataTypeRelationOne = class(TDataType)
84 private
85 procedure ButtonClickExecute(Sender: TObject);
86 public
87 ObjectId: Integer;
88 SelectedItemName: string;
89 function CreateControl(Owner: TComponent): TWinControl; override;
90 procedure SetupControl(Control: TWinControl); override;
91 function GetControlValue(Control: TWinControl): string; override;
92 procedure Load(CellValue: string); override;
93 procedure LoadDef(ACustomType: Integer); override;
94 constructor Create; override;
95 end;
96
97 { TDataTypeRelationMany }
98
99 TDataTypeRelationMany = class(TDataType)
100 ObjectId: Integer;
101 PropertyID: Integer;
102 PropertyName: string;
103 SelectedItemName: string;
104 function CreateControl(Owner: TComponent): TWinControl; override;
105 procedure SetupControl(Control: TWinControl); override;
106 procedure Load(CellValue: string); override;
107 procedure LoadDef(ACustomType: Integer); override;
108 constructor Create; override;
109 end;
110
111 { TDataTypeDateTime }
112
113 TDataTypeDate = class(TDataType)
114 Value: TDateTime;
115 Default: TDateTime;
116 Min: TDateTime;
117 Max: TDateTime;
118 function CreateControl(Owner: TComponent): TWinControl; override;
119 function GetControlValue(Control: TWinControl): string; override;
120 procedure SetupControl(Control: TWinControl); override;
121 procedure Load(CellValue: string); override;
122 constructor Create; override;
123 end;
124
125 TDataTypeDateTime = class(TDataType)
126 Value: TDateTime;
127 Default: TDateTime;
128 Min: TDateTime;
129 Max: TDateTime;
130 function CreateControl(Owner: TComponent): TWinControl; override;
131 function GetControlValue(Control: TWinControl): string; override;
132 procedure SetupControl(Control: TWinControl); override;
133 procedure Load(CellValue: string); override;
134 constructor Create; override;
135 end;
136
137 { TDataTypeFloat }
138
139 TDataTypeFloat = class(TDataType)
140 Value: Double;
141 Default: Double;
142 Min: Double;
143 Max: Double;
144 function CreateControl(Owner: TComponent): TWinControl; override;
145 function GetControlValue(Control: TWinControl): string; override;
146 procedure SetupControl(Control: TWinControl); override;
147 procedure Load(CellValue: string); override;
148 constructor Create; override;
149 end;
150
151 function GetDataType(ACustomType: Integer): TDataType;
152
153
154implementation
155
156uses
157 USqlDatabase, UFormItemSelect, UFormMain, UCore, USystem;
158
159function GetDataType(ACustomType: Integer): TDataType;
160var
161 Proxy: TListProxy;
162 BaseType: Integer;
163begin
164 try
165 Proxy := TListProxy.Create;
166 Proxy.Client := Core.System.Client;
167 Proxy.ObjectName := CustomTypeTableName;
168 Proxy.Condition := 'Id=' + IntToStr(ACustomType);
169 Proxy.Load;
170 BaseType := StrToInt(TObjectProxy(Proxy.Objects[0]).Properties.Values['Type']);
171 finally
172 Proxy.Free;
173 end;
174
175 if BaseType = Integer(vtInteger) then begin
176 Result := TDataTypeNumber.Create;
177 end else
178 if BaseType = Integer(vtDate) then begin
179 Result := TDataTypeDate.Create;
180 end else
181 if BaseType = Integer(vtDateTime) then begin
182 Result := TDataTypeDateTime.Create;
183 end else
184 if BaseType = Integer(vtFloat) then begin
185 Result := TDataTypeFloat.Create;
186 end else
187 if BaseType = Integer(vtString) then begin
188 Result := TDataTypeString.Create;
189 end else
190 if BaseType = Integer(vtPassword) then begin
191 Result := TDataTypePassword.Create;
192 end else
193 if BaseType = Integer(vtBoolean) then begin
194 Result := TDataTypeBoolean.Create;
195 end else
196 if BaseType = Integer(vtRelationOne) then begin
197 Result := TDataTypeRelationOne.Create;
198 end else
199 if BaseType = Integer(vtRelationMany) then begin
200 Result := TDataTypeRelationMany.Create;
201 end else begin
202 Result := TDataTypeString.Create;
203 end;
204 Result.CustomType := ACustomType;
205end;
206
207{ TDataTypeDateTime }
208
209function TDataTypeDateTime.CreateControl(Owner: TComponent): TWinControl;
210begin
211 Result := TDateEdit.Create(Owner);
212end;
213
214function TDataTypeDateTime.GetControlValue(Control: TWinControl): string;
215begin
216 Result := DateTimeToSQL(TDateEdit(Control).Date);
217end;
218
219procedure TDataTypeDateTime.SetupControl(Control: TWinControl);
220begin
221 TDateEdit(Control).Date := Value;
222end;
223
224procedure TDataTypeDateTime.Load(CellValue: string);
225begin
226 Value := SQLToDateTime(CellValue);
227end;
228
229constructor TDataTypeDateTime.Create;
230begin
231 inherited Create;
232 Name := 'DateType';
233end;
234
235{ TDataTypeRelationMany }
236
237function TDataTypeRelationMany.CreateControl(Owner: TComponent): TWinControl;
238begin
239 Result := inherited CreateControl(Owner);
240end;
241
242procedure TDataTypeRelationMany.SetupControl(Control: TWinControl);
243begin
244 inherited SetupControl(Control);
245end;
246
247procedure TDataTypeRelationMany.Load(CellValue: string);
248begin
249 inherited Load(CellValue);
250end;
251
252procedure TDataTypeRelationMany.LoadDef(ACustomType: Integer);
253var
254 Proxy: TListProxy;
255 BaseType: Integer;
256begin
257 try
258 Proxy := TListProxy.Create;
259 Proxy.Client := Core.System.Client;
260 Proxy.ObjectName := TypeRelationMany;
261 Proxy.Condition := 'CustomType=' + IntToStr(ACustomType);
262 Proxy.Load;
263 PropertyID := StrToInt(TObjectProxy(Proxy.Objects[0]).Properties.Values['ObjectProperty']);
264 finally
265 Proxy.Free;
266 end;
267 try
268 Proxy := TListProxy.Create;
269 Proxy.Client := Core.System.Client;
270 Proxy.ObjectName := PropertyTable;
271 Proxy.Condition := 'Id=' + IntToStr(PropertyID);
272 Proxy.Load;
273 ObjectId := StrToInt(TObjectProxy(Proxy.Objects[0]).Properties.Values['Object']);
274 PropertyName := TObjectProxy(Proxy.Objects[0]).Properties.Values['ColumnName'];
275 finally
276 Proxy.Free;
277 end;
278end;
279
280constructor TDataTypeRelationMany.Create;
281begin
282 inherited Create;
283 Name := 'RelationMany';
284end;
285
286{ TDataTypeRelationOne }
287
288procedure TDataTypeRelationOne.ButtonClickExecute(Sender: TObject);
289var
290 NewSelectForm: TItemSelectForm;
291begin
292 try
293 NewSelectForm := TItemSelectForm.Create(MainForm);
294 NewSelectForm.ObjectId := ObjectId;
295 NewSelectForm.ShowModal;
296 TEditButton(Control).Text := IntToStr(NewSelectForm.SelectedId)
297 finally
298 ItemSelectForm.Free;
299 end;
300end;
301
302function TDataTypeRelationOne.CreateControl(Owner: TComponent): TWinControl;
303begin
304 Result := TEditButton.Create(Owner);
305 TEditButton(Result).Enabled := False;
306 TEditButton(Result).Button.Enabled := True;
307 TEditButton(Result).Button.OnClick := ButtonClickExecute;
308 Control := Result;
309end;
310
311procedure TDataTypeRelationOne.SetupControl(Control: TWinControl);
312begin
313 TEditButton(Control).Text := SelectedItemName;
314end;
315
316function TDataTypeRelationOne.GetControlValue(Control: TWinControl): string;
317begin
318 Result := TEditButton(Control).Text;
319end;
320
321procedure TDataTypeRelationOne.Load(CellValue: string);
322begin
323 SelectedItemName := CellValue;
324end;
325
326procedure TDataTypeRelationOne.LoadDef(ACustomType: Integer);
327var
328 Proxy: TListProxy;
329begin
330 try
331 Proxy := TListProxy.Create;
332 Proxy.Client := Core.System.Client;
333 Proxy.ObjectName := TypeRelationOne;
334 Proxy.Condition := 'CustomType=' + IntToStr(ACustomType);
335 Proxy.Load;
336 ObjectId := StrToInt(TObjectProxy(Proxy.Objects[0]).Properties.Values['Object']);
337 finally
338 Proxy.Free;
339 end;
340end;
341
342constructor TDataTypeRelationOne.Create;
343begin
344 inherited Create;
345 Name := 'RelationOne';
346end;
347
348
349{ TDataTypePassword }
350
351function TDataTypePassword.CreateControl(Owner: TComponent): TWinControl;
352begin
353 Result := TMaskEdit.Create(Owner);
354end;
355
356procedure TDataTypePassword.SetupControl(Control: TWinControl);
357begin
358 TMaskEdit(Control).Text := Value;
359end;
360
361procedure TDataTypePassword.Load(CellValue: string);
362begin
363 Value := CellValue;
364end;
365
366constructor TDataTypePassword.Create;
367begin
368 inherited Create;
369 Name := 'Password';
370end;
371
372{ TDataTypeFloat }
373
374function TDataTypeFloat.CreateControl(Owner: TComponent): TWinControl;
375begin
376 Result := TFloatSpinEdit.Create(Owner);
377end;
378
379function TDataTypeFloat.GetControlValue(Control: TWinControl): string;
380begin
381 Result := MySQLFloatToStr(TFloatSpinEdit(Control).Value);
382end;
383
384procedure TDataTypeFloat.SetupControl(Control: TWinControl);
385begin
386 TFloatSpinEdit(Control).Value := Value;
387end;
388
389procedure TDataTypeFloat.Load(CellValue: string);
390begin
391 Value := MySQLStrToFloat(CellValue);
392end;
393
394constructor TDataTypeFloat.Create;
395begin
396 inherited Create;
397 Name := 'Float';
398end;
399
400{ TDataTypeDateTime }
401
402function TDataTypeDate.CreateControl(Owner: TComponent): TWinControl;
403begin
404 Result := TDateEdit.Create(Owner);
405end;
406
407function TDataTypeDate.GetControlValue(Control: TWinControl): string;
408begin
409 Result := DateTimeToSQL(TDateEdit(Control).Date);
410end;
411
412procedure TDataTypeDate.SetupControl(Control: TWinControl);
413begin
414 TDateEdit(Control).Date := Value;
415end;
416
417procedure TDataTypeDate.Load(CellValue: string);
418begin
419 Value := SQLToDateTime(CellValue);
420end;
421
422constructor TDataTypeDate.Create;
423begin
424 inherited Create;
425 Name := 'Date';
426end;
427
428{ TDataTypeBoolean }
429
430function TDataTypeBoolean.CreateControl(Owner: TComponent): TWinControl;
431begin
432 Result := TCheckBox.Create(Owner);
433end;
434
435function TDataTypeBoolean.GetControlValue(Control: TWinControl): string;
436begin
437 Result := IntToStr(Integer(TCheckBox(Control).Checked));
438end;
439
440procedure TDataTypeBoolean.SetupControl(Control: TWinControl);
441begin
442 TCheckBox(Control).Checked := Value;
443end;
444
445procedure TDataTypeBoolean.Load(CellValue: string);
446begin
447 Value := Boolean(StrToInt(CellValue));
448end;
449
450constructor TDataTypeBoolean.Create;
451begin
452 inherited Create;
453 Name := 'Boolean';
454end;
455
456{ TDataTypeNumber }
457
458function TDataTypeNumber.CreateControl(Owner: TComponent): TWinControl;
459begin
460 Result := TSpinEdit.Create(Owner);
461end;
462
463procedure TDataTypeNumber.SetupControl(Control: TWinControl);
464begin
465 inherited SetupControl(Control);
466 TSpinEdit(Control).Value := Value;
467end;
468
469function TDataTypeNumber.GetControlValue(Control: TWinControl): string;
470begin
471 Result := IntToStr(TSpinEdit(Control).Value);
472end;
473
474procedure TDataTypeNumber.Load(CellValue: string);
475begin
476 inherited Load(CellValue);
477 Value := StrToInt(CellValue);
478end;
479
480procedure TDataTypeNumber.LoadDef(CustomType: Integer);
481begin
482
483end;
484
485constructor TDataTypeNumber.Create;
486begin
487 inherited Create;
488 Name := 'Number';
489end;
490
491{ TDataTypeString }
492
493function TDataTypeString.CreateControl(Owner: TComponent): TWinControl;
494begin
495 Result := TEdit.Create(Owner);
496end;
497
498procedure TDataTypeString.SetupControl(Control: TWinControl);
499begin
500 TEdit(Control).Text := Value;
501end;
502
503function TDataTypeString.GetControlValue(Control: TWinControl): string;
504begin
505 Result := TEdit(Control).Text;
506end;
507
508procedure TDataTypeString.Load(CellValue: string);
509begin
510 Value := CellValue;
511end;
512
513constructor TDataTypeString.Create;
514begin
515 inherited Create;
516 Name := 'String';
517end;
518
519{ TDataType }
520
521function TDataType.CreateControl(Owner: TComponent): TWinControl;
522begin
523
524end;
525
526procedure TDataType.SetupControl(Control: TWinControl);
527begin
528
529end;
530
531function TDataType.GetControlValue(Control: TWinControl): string;
532begin
533
534end;
535
536procedure TDataType.Load(CellValue: string);
537begin
538
539end;
540
541procedure TDataType.LoadDef(ACustomType: Integer);
542begin
543
544end;
545
546procedure TDataType.SetDefault;
547begin
548
549end;
550
551function TDataType.GetStringValue: string;
552begin
553 Result := '';
554end;
555
556constructor TDataType.Create;
557begin
558 inherited;
559end;
560
561end.
562
Note: See TracBrowser for help on using the repository browser.