-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathWebServiceCaller2.as
More file actions
321 lines (272 loc) · 8.12 KB
/
WebServiceCaller2.as
File metadata and controls
321 lines (272 loc) · 8.12 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
package webService2
{
import contents.Contents;
import flash.events.ErrorEvent;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.utils.clearTimeout;
import flash.utils.setTimeout;
import mx.rpc.AsyncToken;
[Event(name="complete", type="flash.events.Event")]
[Event(name="unload", type="flash.events.Event")]
/**On server error*/
[Event(name="error", type="flash.events.ErrorEvent")]
//Global event direcly from myWebService ↓
/**This will not dispatches*/
[Event(name="NO_CONNECTTION", type="webService2.WebEvent2")]
public class WebServiceCaller2 extends EventDispatcher
{
/**Global event dispatcher*/
public static var globalEventDispatcher:WebServiceCaller2 = new WebServiceCaller2(null);
/**This is reload time out id*/
private var timerId:int ;
public var connected:Boolean = false ;
private var myToken:AsyncToken;
private var myParam:Array ;
//public var data:Vector.<GetPhotos>;
//#0
private var offlineDate:Date,
_justLoadOffline:Boolean,
offlineDataIsOK:Boolean;
//private var myServiceFunction:Function ;
private var serviceName:String ;
private var connectinError:WebEvent2;
private var offlineDataDispatchedOnce:Boolean=false;
public function get justLoadOffline():Boolean
{
return _justLoadOffline;
}
public function set justLoadOffline(value:Boolean):void
{
_justLoadOffline = value;
if(_justLoadOffline && offlineDate == null)
{
offlineDate = new Date();
offlineDate.minutes-=uint(Contents.config.max_available_service_data);
}
else
{
offlineDate = null ;
}
}
/**This function will change the maximomOfflineData value to new date<br>
* null will remove date controller condition*/
public function changeOfflineDate(newDate:Date=null):void
{
offlineDate = newDate ;
}
public function WebServiceCaller2(webServiceName:String,offlineDataIsOK_v:Boolean=true,justLoadOfline_v:Boolean=false,maximomOfflineData:Date=null)
{
//TODO: implement function
//#4
//myServiceFunction = myWebService ;
//New line ↓
/*if(justLoadOfline_v && maximomOfflineData == null)
{
maximomOfflineData = new Date();
maximomOfflineData.minutes-= uint(Contents.config.max_available_service_data);
}
offlineDate = maximomOfflineData ;*/
myWebService2.setOperation(webServiceName);
serviceName = webServiceName ;
offlineDataIsOK = offlineDataIsOK_v ;
_justLoadOffline = justLoadOfline_v ;
super();
}
protected function loadParams(...params):void
{
trace(serviceName+' - offlineDataIsOK:'+offlineDataIsOK+' _justLoadOffline:'+_justLoadOffline);
connectinError = null ;
offlineDataDispatchedOnce = false ;
myParam = params ;
//#1
reLoadLastRequest();
}
/**I created this function to make reload available*/
private function reLoadLastRequest()
{
var needReload:Boolean ;
if(_justLoadOffline)
{
//Cashed data sent to user class on generateDataAndDispatchEvent function
needReload = generateDataAndDispatchEvent(null,false,true);
}
if(needReload || offlineDate==null)
{
//If no oflinte data requests or no oflinte data avaiable
myWebService2.Connect(onConnected,noInternet);
}
}
/**Make this service reloads again<br>
* After few tests, I noticed that the 10 second delay is not enaugh
* <br>
* This is on beta testing yet*/
public function reLoad(delay:uint=20000):void
{
//I prefer to dont cansel the current webservice
//cansel();
clearTimeout(timerId);
//LoadForDoubleControll = false,
// offlineValuesToSend = null;
if(delay==0)
{
reLoadLastRequest();
}
else
{
timerId = setTimeout(reLoadLastRequest,delay);
}
}
public function cansel()
{
clearTimeout(timerId);
//TODO: implement function
myWebService2.eventListen.removeEventListener(WebEvent2.NO_CONNECTTION,noInternet);
myWebService2.eventListen.removeEventListener(WebEvent2.RESULT,loaded);
myWebService2.Disconnect(onConnected,noInternet);
myWebService2.CanselThisToken(myToken);
}
private function onConnected()
{
//trace("connected");
//TODO: implement function
myWebService2.eventListen.addEventListener(WebEvent2.NO_CONNECTTION,noInternet) ;
myWebService2.eventListen.addEventListener(WebEvent2.RESULT,loaded) ;
myToken = myWebService2.sentParamsToOperation(serviceName,myParam) ;
//myToken = myServiceFunction.apply(this,myParam)
}
private function noInternet(e:WebEvent2=null)
{
//TODO: implement function
if(e == null || myToken == e.token)
{
cansel();
connectinError = new WebEvent2(WebEvent2.NO_CONNECTTION) ;
if(e!=null)
{
connectinError.problemHint = e.problemHint ;
connectinError.connectionProblemCode = e.connectionProblemCode ;
}
//#2
if(offlineDataIsOK)
{
//I will save conntectionError to dispatch it if no cashed data will load
if(!offlineDataDispatchedOnce)
{
generateDataAndDispatchEvent(null,false);
}
}
else
{
//dispatch pure event on global variable to manage error type ↓
this.dispatchEvent(connectinError) ;
//This should call after local connection error
globalEventDispatcher.dispatchEvent(connectinError.clone()) ;
dispatchEveryWhere(Event.UNLOAD);
}
}
}
private function loaded(e:WebEvent2)
{
//TODO: implement function
if(myToken == e.token)
{
connected = true ;
cansel();
generateDataAndDispatchEvent(e.pureData);
}
}
/**returns true if the proccess was successfuly complete*/
private function generateDataAndDispatchEvent(pureData:Array,dispatchConnectionErrorNow:Boolean=true,controllCashDate:Boolean=false):Boolean
{
//#3
if(pureData==null)
{
//This was on version 1 of WebServiceSaver
//var jsonString:String = WebServiceSaver.load(this,myParam) ;
/*if(jsonString!=null)
{
//null will return on pureData whenever ofline data is requests and it is the first call of it
pureData = JSON.parse(jsonString) as Array;
}*/
var cashingDate:Date ;
if(controllCashDate)
{
cashingDate = offlineDate ;
}
pureData = WebServiceSaver2.load(this,myParam,cashingDate) as Array ;
trace("try to load cash : ");
trace("pure data loaded : "+pureData);
//From now , if no null receved from services , it will replace pureData with [] array
}
else if(offlineDataIsOK && pureData!=null)
{
//Versoin 1 of the pureData
WebServiceSaver2.save(this,myParam,/*JSON.stringify(*/pureData/*)*/);
}
trace("pureData is null : "+(pureData==null)+' >>>>> '+pureData);
if(pureData==null)
{
trace("Close this sevice");
if(dispatchConnectionErrorNow)
{
dispatchEveryWhere(Event.UNLOAD);
//This should call after local connection error ↓
if(connectinError!=null)
{
globalEventDispatcher.dispatchEvent(connectinError.clone()) ;
}
}
else
{
trace("Try to load onlie data");
}
return false ;
}
if(manageData(pureData))
{
offlineDataDispatchedOnce = true ;
dispatchEveryWhere(Event.COMPLETE);
}
else
{
if(this.hasEventListener(ErrorEvent.ERROR))
{
this.dispatchEvent(new ErrorEvent(ErrorEvent.ERROR));
}
else
{
trace("No error connection listener created");
dispatchEveryWhere(Event.UNLOAD)
}
}
return true ;
}
/**Manage your special data here*/
protected function manageData(pureData:Array):Boolean
{
/*var cash:Array = WebServiceParser2.pars(pureData,FolderCount);
data = new Vector.<FolderCount>();
for(var i = 0 ; i < cash.length ; i++)
{
data[i] = cash[i];
}*/
return true;
}
/**Add header*/
public function addHeader(userName:String, password:String, tocken:String, uid:String):void
{
myWebService2.setHeader(userName,password,tocken,uid);
}
/**Clear header*/
public function clearHeader():void
{
myWebService2.clearHeader();
}
private function dispatchEveryWhere(eventName:String)
{
//TODO: implement function
this.dispatchEvent(new Event(eventName));
}
}
}