forked from CPsoftBE/BackupOfCromis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cromis.MVC.Model.Impl.pas
279 lines (239 loc) · 8.25 KB
/
Cromis.MVC.Model.Impl.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
unit Cromis.MVC.Model.Impl;
interface
uses
Windows, Messages, SysUtils, Classes, Contnrs,
// cromis units
Cromis.MVC.Model,
Cromis.MVC.Common,
Cromis.MVC.Events,
Cromis.MVC.Controller;
type
TMVCModel = class(TInterfacedObject, IMVCModel)
private
FWinHandle: Cardinal;
FMVCFactory: IMVCFactory;
FUpdateCount: Integer;
FControllerID: string;
FSessionData: ISessionData;
FLastRequests: TStringList;
FObserverList: TInterfaceList;
procedure DeallocateHWnd(Wnd: HWND);
function GetSessionData: ISessionData;
procedure SetSessionData(const Value: ISessionData);
//procedure WndProc(var Msg: TMessage);
procedure NotifyUpdate(const Command, ID: string; const Data: IMVCData);
procedure EndUpdate(const Command, ID: string; const Data: IMVCData);
procedure BeginUpdate;
protected
//function ExecuteRequest(const Data: IMVCData; const RequestClass: TClass; const Name: string = ''): string;
procedure SendCommand(const Controler, Command, ID: string; const Data: IMVCData = nil); overload;
procedure SendCommand(const Controler, Command: string; const Data: IMVCData = nil); overload;
procedure ActivateView(const Controler, View: string; const Data: IMVCData = nil); overload;
procedure ActivateView(const View: string; const Data: IMVCData = nil); overload;
//procedure DoOnRequestBegin(const RequestData: IRequestData); virtual;
//procedure DoOnRequestDone(const RequestData: IRequestData); virtual;
property MVCFactory: IMVCFactory read FMVCFactory;
public
destructor Destroy; override;
property SessionData: ISessionData read GetSessionData write SetSessionData;
constructor Create(const ControllerID: string; const MVCFactory: IMVCFactory); virtual;
procedure ExecuteCommand(const Name, ID: string; const Data: IMVCData = nil); overload;
procedure ExecuteCommand(const Name: string; const Data: IMVCData = nil); overload;
procedure UnregisterObserver(const Observer: IMVCObserver);
procedure RegisterObserver(const Observer: IMVCObserver);
end;
// class definition for controler
TModelClass = class of TMVCModel;
implementation
{ TMVCModel }
constructor TMVCModel.Create(const ControllerID: string; const MVCFactory: IMVCFactory);
begin
FObserverList := TInterfaceList.Create;
FLastRequests := TStringList.Create;
FControllerID := ControllerID;
FMVCFactory := MVCFactory;
FUpdateCount := 0;
// create the dummy window for messages
//FWinHandle := AllocateHWND(WndProc);
end;
procedure TMVCModel.DeallocateHWnd(Wnd: HWND);
var
Instance: Pointer;
begin
Instance := Pointer(GetWindowLong(Wnd, GWL_WNDPROC));
if Instance <> @DefWindowProc then
begin
{ make sure we restore the default
windows procedure before freeing memory }
SetWindowLong(Wnd, GWL_WNDPROC, Longint(@DefWindowProc));
FreeObjectInstance(Instance);
end;
DestroyWindow(Wnd);
end;
destructor TMVCModel.Destroy;
begin
DeallocateHWnd(FWinHandle);
FreeAndNil(FObserverList);
FreeAndNil(FLastRequests);
inherited;
end;
procedure TMVCModel.EndUpdate(const Command, ID: string; const Data: IMVCData);
begin
Dec(FUpdateCount);
if FUpdateCount = 0 then
NotifyUpdate(Command, ID, Data);
end;
procedure TMVCModel.ExecuteCommand(const Name: string; const Data: IMVCData);
begin
ExecuteCommand(Name, '', Data);
end;
procedure TMVCModel.ActivateView(const Controler, View: string; const Data: IMVCData);
begin
FMVCFactory.ActivateView(Controler, View, Data);
end;
procedure TMVCModel.ActivateView(const View: string; const Data: IMVCData);
begin
FMVCFactory.ActivateView(FControllerID, View, Data);
end;
procedure TMVCModel.BeginUpdate;
begin
Inc(FUpdateCount);
end;
{
procedure TMVCModel.DoOnRequestBegin(const RequestData: IRequestData);
begin
// nothing to do here
end;
procedure TMVCModel.DoOnRequestDone(const RequestData: IRequestData);
begin
// nothing to do here
end;
}
procedure TMVCModel.ExecuteCommand(const Name, ID: string; const Data: IMVCData);
var
CommandID: string;
CommandResult: IMVCData;
CommandMethod: TCommandMethod;
begin
BeginUpdate;
try
CommandID := AcquireEventID;
MVCNotifications.Notify(meCommandStarted, CommandID, FControllerID, Name, Data);
try
TMethod(CommandMethod).Code := Self.MethodAddress(Format('Command_%s', [Name]));
CommandResult := AcquireMVCData;
if TMethod(CommandMethod).Code <> nil then
begin
TMethod(CommandMethod).Data := Self;
CommandMethod(ID, Data, CommandResult);
end;
finally
MVCNotifications.Notify(meCommandComplete, CommandID, FControllerID, Name, CommandResult);
end;
finally
EndUpdate(Name, ID, CommandResult);
end;
end;
function TMVCModel.GetSessionData: ISessionData;
begin
Result := FSessionData;
end;
{
function TMVCModel.ExecuteRequest(const Data: IMVCData; const RequestClass: TClass; const Name: string): string;
var
Request: TMVCRequest;
begin
Request := RequestPool.ExecuteRequest(Data, FWinHandle, RequestClass, Name);
Result := Request.RequestID;
if Name <> '' then
FLastRequests.Values[Name] := Result;
end;
}
procedure TMVCModel.NotifyUpdate(const Command, ID: string; const Data: IMVCData);
var
I: Integer;
begin
if Data.UpdateViews then
begin
for I := 0 to FObserverList.Count - 1 do
begin
if not Data.ActiveOnly or IMVCObserver(FObserverList[I]).IsActive then
begin
IMVCObserver(FObserverList[I]).UpdateView(Command, ID, Data);
MVCNotifications.Notify(meViewUpdated, AcquireEventID, FControllerID, Command, Data);
end;
end;
end;
end;
procedure TMVCModel.RegisterObserver(const Observer: IMVCObserver);
begin
FObserverList.Add(Observer);
end;
procedure TMVCModel.SendCommand(const Controler, Command, ID: string; const Data: IMVCData);
begin
FMVCFactory.GetControler(Controler).GetModel.ExecuteCommand(Command, ID, Data);
end;
procedure TMVCModel.SendCommand(const Controler, Command: string; const Data: IMVCData);
begin
SendCommand(Controler, Command, '', Data);
end;
procedure TMVCModel.SetSessionData(const Value: ISessionData);
begin
FSessionData := Value;
end;
procedure TMVCModel.UnregisterObserver(const Observer: IMVCObserver);
begin
FObserverList.Remove(Observer);
end;
(*
procedure TMVCModel.WndProc(var Msg: TMessage);
var
RequestData: IRequestData;
Parameters: ISimpleStorage;
begin
if (Msg.Msg >= WM_REQUEST_STARTED) and (Msg.Msg <= WM_REQUEST_FINISHED) then
begin
Parameters := ISimpleStorage(Msg.LParam);
RequestData := AcquireRequestData(Parameters.Get('Status').AsStringDef,
ISimpleStorage(Msg.WParam),
Parameters.Get('Name').AsStringDef,
Parameters.Get('ID').AsStringDef);
if FLastRequests.Values[RequestData.Name] = RequestData.ID then
begin
RequestData.Result := ModelData;
case Msg.Msg of
WM_REQUEST_FINISHED:
begin
RequestData.UpdateViews := True;
DoOnRequestDone(RequestData);
MVCNotifications.Notify(meRequestComplete,
RequestData.ID,
FControler.Name,
RequestData.Name,
RequestData.RootNode);
end;
WM_REQUEST_STARTED:
begin
RequestData.UpdateViews := False;
DoOnRequestBegin(RequestData);
MVCNotifications.Notify(meRequestStarted,
RequestData.ID,
FControler.Name,
RequestData.Name,
RequestData.RootNode)
end;
end;
// notify all views
if RequestData.UpdateViews then
NotifyUpdate(RequestData.Name, RequestData.Result);
end;
end
else
{
for all other messages call
the default window procedure
}
Msg.Result := DefWindowProc(FWinHandle, Msg.Msg, Msg.wParam, Msg.lParam);
end;
*)
end.