-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathWebServiceCaller.as
More file actions
470 lines (406 loc) · 12.1 KB
/
WebServiceCaller.as
File metadata and controls
470 lines (406 loc) · 12.1 KB
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package webService.webCallers
{
import flash.events.ErrorEvent;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.utils.clearTimeout;
import flash.utils.setTimeout;
import mx.rpc.AsyncToken;
import mx.rpc.soap.Operation;
import webService.WebEvent;
import webService.WebServiceSaver;
import webService.myWebService;
/**This event dispatches when ofline or server data is ready to use.*/
[Event(name="complete", type="flash.events.Event")]
/**This event dispatches when there is no internet connection and there is no ofline data to use. So you have to notify user to controll internet connection.*/
[Event(name="unload", type="flash.events.Event")]
/**This event tells that server send an error because of wrong input datas on load() function. It can happens after register or login, because of wrong login or register information.<br><br>
*
* This evetn will dispatches befor unload dispatches.*/
[Event(name="error", type="flash.events.ErrorEvent")]
/**This event dispatches when an update happend whe you used ofline datas. for example you called and service and requseted offline datas and ofline data was available and it was dispatched with COMPLETE event. then web service will try to load the server datas again and the cahnes between last data and new one is discovered. so it will update the data vlue then this event dispatches.*/
[Event(name="change", type="flash.events.Event")]
/**Server response received*/
[Event(name="connect", type="flash.events.Event")]
/**Sever data is same as cashed data*/
[Event(name="clear", type="flash.events.Event")]
public class WebServiceCaller extends EventDispatcher
{
//private var reLoader:Timer ;
private var timerId:uint ;
public var connected:Boolean = false ;
private var myToken:AsyncToken;
private var myParam:Array ;
//public var data:Vector.<GetPhotos>;
//#0
public var justLoadOffline:Boolean,
offlineDataIsOK:Boolean;
//private var myServiceFunction:Function ;
private var myOperation:Operation ;
public var PureData:String = '' ;
protected var myServiceName:String ;
private var offlineDate:Date,
LoadForDoubleControll:Boolean = false,
offlineValuesToSend:String = null;
private var func_onDataRetrived:Function,
func_onConnectionError:Function,
func_onConnected:Function,
func_onError:Function;
private var reloadIfNotConnected:Boolean = false ;
/**This function will change the maximomOfflineData value to new date<br>
* null changes with new Date();*/
public function changeOfflineDate(newDate:Date=null):void
{
if(newDate == null)
{
newDate = new Date();
}
offlineDate = newDate ;
}
public function catchThenRealad():void
{
reloadIfNotConnected = true ;
}
public function then(onDataRetrived:Function):WebServiceCaller
{
func_onDataRetrived = onDataRetrived;
return this ;
}
public function catch2(onError:Function):WebServiceCaller
{
func_onConnectionError = onError;
func_onError = onError;
return this ;
}
public function onConnected2(onConnectedFunc:Function):WebServiceCaller
{
func_onConnected = onConnectedFunc;
return this ;
}
/**If you dont enter a date, it will take current date as maximomOFflieneDate*/
public function WebServiceCaller(myWebServiceName:String,offlineDataIsOK_v:Boolean=true,justLoadOfline_v:Boolean=false,maximomOfflineData:Date = null)
{
//TODO: implement function
//#4
if(justLoadOfline_v && maximomOfflineData == null)
{
maximomOfflineData = new Date();
}
offlineDate = maximomOfflineData ;
myServiceName = myWebServiceName ;
myWebService.activateOperation(myWebServiceName);
offlineDataIsOK = offlineDataIsOK_v ;
justLoadOffline = justLoadOfline_v ;
func_onDataRetrived = null ;
func_onConnectionError = null ;
func_onError = null ;
super();
}
/**Make this service reloads again<br>
* After few tests, I noticed that the 10 second delay is not enaugh*/
public function reLoad(delay:uint=20000):void
{
//I prefer to dont cansel the current webservice
//cansel();
clearTimeout(timerId);
LoadForDoubleControll = false,
offlineValuesToSend = null;
changeOfflineDate();
justLoadOffline = false ;
//clearTimeout(timerId);
/*if(reLoader!=null)
{
reLoader.reset();
}*/
if(delay==0)
{
reLoadLastRequest(/*null*/);
}
else
{
timerId = setTimeout(reLoadLastRequest,delay);
/*reLoader = new Timer(delay,1);
reLoader.addEventListener(TimerEvent.TIMER_COMPLETE,reLoadLastRequest);
reLoader.start();*/
}
}
/**It is tile to reload service*/
private function reLoadLastRequest(/*e:TimerEvent*/):void
{
SaffronLogger.log("Service reloaded");
loadParams.apply(this,myParam);
}
/**Create load function on extended class to call this function with its own parameters*/
protected function loadParams(...params):void
{
cansel();
offlineValuesToSend = null ;
//You have permition to dispatch events now.
//doNotDispatchEventsAgain = false ;
myParam = params ;
//#1
if(justLoadOffline)
{
var cashedData:String = WebServiceSaver.load(this,myParam);
if(offlineDate!=null && cashedData!=null)
{
var controll:Boolean = WebServiceSaver.isExpired(this,myParam,offlineDate);
LoadForDoubleControll = controll ;
}
if(cashedData != null)
{
setTimeout(function():void
{
generateDataAndDispatchEvent(cashedData,true);
//doNotDispatchEventsAgain = true ;
offlineValuesToSend = cashedData ;
if(LoadForDoubleControll)
{
myWebService.Connect(onConnected,noInternet);
}
},0);
}
else
{
myWebService.Connect(onConnected,noInternet);
}
}
else
{
myWebService.Connect(onConnected,noInternet);
}
}
public function cansel()
{
//TODO: implement function
clearTimeout(timerId);
/*if(reLoader!=null)
{
reLoader.reset();
reLoader = null ;
}*/
myWebService.eventListen.removeEventListener(WebEvent.EVENT_DISCONNECTED,noInternet);
myWebService.eventListen.removeEventListener(WebEvent.Result,loaded);
myWebService.Disconnect(onConnected,noInternet);
myWebService.CanselThisToken(myToken);
}
private function onConnected():void
{
SaffronLogger.log("connected");
//TODO: implement function
myWebService.eventListen.addEventListener(WebEvent.EVENT_DISCONNECTED,noInternet);
myWebService.eventListen.addEventListener(WebEvent.Result,loaded);
myToken = myWebService.callFunction(myServiceName,myParam);
}
private function noInternet(e:WebEvent=null)
{
//TODO: implement function
if(e == null || myToken == e.token)
{
cansel();
//#2
if(offlineDataIsOK)
{
generateDataAndDispatchEvent(null);
}
else
{
event_noInternet();
}
}
}
private function loaded(e:WebEvent)
{
//TODO: implement function
if(myToken == e.token)
{
connected = true ;
cansel();
generateDataAndDispatchEvent(e.pureData);
/*if(func_onDataRetrived!=null)
{
func_onDataRetrived();
}*/
this.dispatchEvent(new Event(Event.CONNECT));
if(func_onConnected!=null)func_onConnected();
}
}
private function generateDataAndDispatchEvent(pureData:String,dontSaveItAgain:Boolean = false ):void
{
//#3
if(pureData==null)
{
pureData = WebServiceSaver.load(this,myParam);
//SaffronLogger.log('cash loads : '+pureData);
}
else if(offlineDataIsOK && pureData!=null && !dontSaveItAgain)//dontSaveItAgain added to prevent oversaving the cashed data again. it was destroies the save date
{
WebServiceSaver.save(this,myParam,pureData);
}
if(pureData==null)
{
//Why did I remove this event dispatcher? It may forgoten, because it is cause of bug on no connection
//dispatchEveryWhere(Event.UNLOAD);
event_noInternet();
return ;
}
//you can even controll the data value from overrided function
var parsedSituation:Boolean = manageData(pureData);
PureData = pureData ;
if(parsedSituation)
{
//SaffronLogger.log("Load complete");
//SaffronLogger.log("offlineValuesToSend : "+offlineValuesToSend);
//SaffronLogger.log("pureData : "+pureData);
if(offlineValuesToSend == null && pureData!=null)
{
offlineValuesToSend = pureData ;
//SaffronLogger.log("It is the first dispatching time");
//dispatchEveryWhere(Event.COMPLETE,false);
event_data();
}
else if(controllChange(pureData))
{
//There is no need to send update
SaffronLogger.log(">Server data is changed");
//dispatchEveryWhere(Event.COMPLETE,true);
event_dataUpdated()
}
else
{
SaffronLogger.log(">Server data is steal same as old dispatched data");
event_dataWasUpdated()
}
}
else
{
var hasErrorListener:Boolean = event_wrongInputs();
if(!hasErrorListener)
{
event_noInternet();
}
else
{
SaffronLogger.log("User is listening to Error Event, So there is no need to dispatch netError");
}
//dispatchEveryWhere(ErrorEvent.ERROR);
//dispatchEveryWhere(Event.UNLOAD);
}
}
/**Returns true if the service got any changes*/
protected function controllChange(pureData:String):Boolean
{
return offlineValuesToSend != pureData ;
}
/**Manage your special data here*/
protected function manageData(pureData:String):Boolean
{
//TODO: implement function
//1- For first type of values
// data = Constants.checkWebServiceBoolean(pureData);
//2- For secend type of values
/*data = new Vector.<GetPhotos>();
var cahsedData:Array = WebServiceParser.pars(pureData,GetPhotos);
for(var i = 0 ; i<cahsedData.length ; i++)
{
data.push(cahsedData[i]);
}*/
return true ;
}
//private function eventDispatcher(completedAtTheFirstTime:Boolean=false,Updated:Boolean=false,DataError:Boolean){};
/////////////////////////New Managed events
private function event_noInternet():void
{
if(func_onConnectionError!=null)
{
func_onConnectionError();
}
if(reloadIfNotConnected)
{
reLoad(4000);
}
this.dispatchEvent(new Event(Event.UNLOAD));
}
/**Returns true if someone listenning to it*/
private function event_wrongInputs():Boolean
{
var hasErrorListener:Boolean = this.hasEventListener(ErrorEvent.ERROR);
if(hasErrorListener)
{
this.dispatchEvent(new ErrorEvent(ErrorEvent.ERROR));
return true ;
}
return false;
}
private function event_dataUpdated():void
{
if(this.hasEventListener(Event.CHANGE))
{
this.dispatchEvent(new Event(Event.CHANGE));
}
else
{
event_data();
}
}
/**Server data is repeating*/
private function event_dataWasUpdated():void
{
this.dispatchEvent(new Event(Event.CLEAR));
}
private function event_data():void
{
if(func_onDataRetrived!=null)
{
func_onDataRetrived();
}
this.dispatchEvent(new Event(Event.COMPLETE));
}
//Removed to make any situation dispatchs its own event.
/*private function dispatchEveryWhere(eventName:String,sendChangeIfSentErlier:Boolean = false )
{
//below meand ofline data is not dispatched yet.
if(offlineValuesToSend==null)//!doNotDispatchEventsAgain)
{
if(eventName == ErrorEvent.ERROR)
{
this.dispatchEvent(new ErrorEvent(ErrorEvent.ERROR));
}
else
{
this.dispatchEvent(new Event(eventName));
}
}
else if(sendChangeIfSentErlier)
{
this.dispatchEvent(new Event(Event.CHANGE));
SaffronLogger.log("Service Content is Updated");
}
else
{
SaffronLogger.log("I cannot dispatch my events any more : "+eventName);
}
}*/
/////////////////////////////////////////Comparition function
public function isFalse(str:String):Boolean
{
str = str.toLowerCase();
if(str == 'false')
{
return true ;
}
var numericValue:Number = Number(str);
if(isNaN(numericValue))
{
return false ;
}
if(numericValue<0)
{
return true ;
}
return false ;
}
}
}