forked from CPsoftBE/BackupOfCromis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cromis.MVC.Controller.pas
313 lines (264 loc) · 8.46 KB
/
Cromis.MVC.Controller.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
unit Cromis.MVC.Controller;
interface
uses
Windows, SysUtils, Classes,
// MVC cromis units
Cromis.MVC.Common,
Cromis.MVC.Events,
Cromis.MVC.Model,
Cromis.MVC.View,
// other cromis units
Cromis.Cryptography;
type
IMVCController = Interface(IInterface)
['{748A20D1-8CE7-4E4C-A930-D9A9A59620AB}']
// getters and setters
function GetName: string;
// properties of the interface
property Name: string read GetName;
// functions and procedure of the interface
function GetModel: IMVCModel;
function GetView(const Name: string): IMVCView;
procedure RegisterView(const Name: string; const View: IMVCView);
procedure DeactivateViews(const Data: IMVCData = nil);
procedure Cleanup;
end;
// dummy definition
ViewClass = class of TObject;
IMVCFactory = Interface(IInterface)
['{297F0EC8-E06F-4FC6-BC12-6A631F6BEFB7}']
function GetDefaultControler: IMVCController;
procedure SetDefaultControler(const Value: IMVCController);
property DefaultControler: IMVCController read GetDefaultControler write SetDefaultControler;
function RegisterControler(const Name: string; const Model: TClass; const Default: Boolean = False): IMVCController;
procedure DeactivateView(const Controler, View: string; const Data: IMVCData = nil);
procedure ActivateView(const Controler, View: string; const Data: IMVCData = nil);
function GetControler(const Index: Integer): IMVCController; overload;
function GetControler(const Name: string): IMVCController; overload;
procedure Assign(const Factory: IMVCFactory);
function ControllerCount: Integer;
procedure Cleanup;
end;
// acuires the main factory interface
function AcquireMVCFactory(const SessionData: ISessionData = nil): IMVCFactory;
function AcquireEventID: string;
implementation
uses
Cromis.MVC.Model.Impl;
type
TViewWrapper = class
private
FView: IMVCView;
public
destructor Destroy; override;
constructor Create(const View: IMVCView);
property View: IMVCView read FView;
end;
TMVCControler = class(TInterfacedObject, IMVCController)
private
FName: string;
FModel: IMVCModel;
FOwner: IMVCFactory;
FViews: TStringList;
function GetName: string;
public
constructor Create(const Owner: IMVCFactory;
const Model: TClass;
const Name: string);
// properties of the interface
property Name: string read GetName;
// functions and procedure of the interface
function GetView(const Name: string): IMVCView;
procedure RegisterView(const Name: string; const View: IMVCView);
procedure DeactivateViews(const Data: IMVCData = nil);
function GetModel: IMVCModel;
procedure Cleanup;
end;
TMVCFactory = class(TInterfacedObject, IMVCFactory)
private
FSessionData: ISessionData;
FControlerList: TInterfaceList;
FDefaultControler: IMVCController;
function GetDefaultControler: IMVCController;
procedure SetDefaultControler(const Value: IMVCController);
public
destructor Destroy; override;
procedure Assign(const Factory: IMVCFactory);
constructor Create(const SessionData: ISessionData);
property DefaultControler: IMVCController read GetDefaultControler write SetDefaultControler;
function RegisterControler(const Name: string; const Model: TClass; const Default: Boolean = False): IMVCController;
procedure DeactivateView(const Controler, View: string; const Data: IMVCData = nil);
procedure ActivateView(const Controler, View: string; const Data: IMVCData = nil);
function GetControler(const Index: Integer): IMVCController; overload;
function GetControler(const Name: string): IMVCController; overload;
function ControllerCount: Integer;
procedure Cleanup;
end;
function AcquireEventID: string;
begin
Result := GetNewFormatedGUID;
end;
function AcquireMVCFactory(const SessionData: ISessionData): IMVCFactory;
begin
Result := TMVCFactory.Create(SessionData);
end;
{ TMVCControler }
procedure TMVCControler.Cleanup;
var
I: Integer;
begin
for I := 0 to FViews.Count - 1 do
begin
TViewWrapper(FViews.Objects[I]).View.Cleanup;
FViews.Objects[I].Free;
end;
FreeAndNil(FViews);
FModel := nil;
end;
constructor TMVCControler.Create(const Owner: IMVCFactory;
const Model: TClass;
const Name: string);
begin
FName := Name;
// owner factory
FOwner := Owner;
// create views hash table
FViews := TStringList.Create;
// create the model from class
FModel := TModelClass(Model).Create(FName, FOwner);
end;
function TMVCControler.GetModel: IMVCModel;
begin
Result := FModel;
end;
function TMVCControler.GetView(const Name: string): IMVCView;
begin
case FViews.IndexOf(Name) > -1 of
True: Result := TViewWrapper(FViews.Objects[FViews.IndexOf(Name)]).View;
False: Result := nil;
end;
end;
procedure TMVCControler.DeactivateViews(const Data: IMVCData);
var
I: Integer;
begin
for I := 0 to FViews.Count - 1 do
TViewWrapper(FViews.Objects[I]).View.Deactivate(Data);
end;
procedure TMVCControler.RegisterView(const Name: string; const View: IMVCView);
begin
FViews.AddObject(Name, TViewWrapper.Create(View));
View.SessionData := FModel.SessionData;
View.SetViewModel(FModel);
View.SetViewID(Name);
// register the view as observer
FModel.RegisterObserver(View);
end;
function TMVCControler.GetName: string;
begin
Result := FName;
end;
{ TMVCFactory }
constructor TMVCFactory.Create(const SessionData: ISessionData);
begin
FControlerList := TInterfaceList.Create;
FSessionData := SessionData;
end;
procedure TMVCFactory.DeactivateView(const Controler, View: string; const Data: IMVCData);
var
MVCControler: IMVCController;
begin
MVCControler := GetControler(Controler);
if MVCControler <> nil then
begin
MVCControler.GetView(View).Deactivate(Data);
MVCNotifications.Notify(meViewDeactivated, AcquireEventID, Controler, View, Data);
end;
end;
destructor TMVCFactory.Destroy;
begin
FreeAndNil(FControlerList);
inherited;
end;
function TMVCFactory.GetControler(const Index: Integer): IMVCController;
begin
if Index < FControlerList.Count then
Result := IMVCController(FControlerList[Index])
else
Result := nil;
end;
function TMVCFactory.GetControler(const Name: string): IMVCController;
var
I: Integer;
begin
Result := nil;
for I := 0 to FControlerList.Count - 1 do
begin
if IMVCController(FControlerList[I]).Name = Name then
begin
Result := IMVCController(FControlerList[I]);
Exit;
end;
end;
end;
function TMVCFactory.GetDefaultControler: IMVCController;
begin
Result := FDefaultControler;
end;
procedure TMVCFactory.ActivateView(const Controler, View: string; const Data: IMVCData = nil);
var
MVCControler: IMVCController;
begin
MVCControler := GetControler(Controler);
if MVCControler <> nil then
begin
// signal the view change to listeners
MVCControler.GetView(View).Activate(Data);
MVCNotifications.Notify(meViewActivated, AcquireEventID, Controler, View, Data);
end;
end;
procedure TMVCFactory.Assign(const Factory: IMVCFactory);
var
I: Integer;
begin
for I := 0 to Factory.ControllerCount - 1 do
FControlerList.Add(Factory.GetControler(I));
end;
procedure TMVCFactory.Cleanup;
var
I: Integer;
begin
for I := 0 to FControlerList.Count - 1 do
IMVCController(FControlerList[I]).Cleanup;
FControlerList.Clear;
end;
function TMVCFactory.ControllerCount: Integer;
begin
Result := FControlerList.Count;
end;
function TMVCFactory.RegisterControler(const Name: string;
const Model: TClass;
const Default: Boolean
): IMVCController;
begin
Result := TMVCControler.Create(Self, Model, Name);
Result.GetModel.SessionData := FSessionData;
FControlerList.Add(Result);
if Default then
FDefaultControler := Result;
end;
procedure TMVCFactory.SetDefaultControler(const Value: IMVCController);
begin
FDefaultControler := Value;
end;
{ TViewWrapper }
constructor TViewWrapper.Create(const View: IMVCView);
begin
FView := View;
end;
destructor TViewWrapper.Destroy;
begin
FView := nil;
inherited;
end;
end.