debugging click to refresh on live
[namibia] / public / js / app / api.js
1 ;(function(){
2
3         _App.API = function()
4         {
5         this.initialize();
6         };
7
8         _App.API.prototype =
9         {
10
11                 /**
12                  * Internal storage for established contracts and execution responses.
13                  */
14                 taskContracts   : {},
15                 taskExecutions  : {},
16                 routeContracts  : {},
17                 routeExecutions : {},
18                 requestId               : 0,
19                 requests                : [],
20                 requestCallback : [],
21
22         initialize : function()
23         {
24                 // Nothing to do here.
25         },
26
27
28
29
30
31         /* ------------------------------------- TASK HANDLING ------------------------------------- */
32         /**
33          * Establish a Task Contract with server.
34          * @param id
35          * @param workspace
36          * @param task
37          * @param jobId
38          * @param data
39          * @param callback
40          * @param errorCallback
41          */
42         getTask : function( id, workspace, task, jobId, data, callback, errorCallback )
43         {
44                         this.taskContracts[id] = {
45                                 "Callback" : callback,
46                                 "ErrorCallback" : errorCallback
47                         };
48                         jobId = (jobId)
49                                 ? jobId
50                                 : null;
51                         data = (data)
52                                 ? data
53                                 : {};
54                         App.Ajax.JSON({
55                                         "id"   : id,
56                                         "url"  : 'workspace/contract-task',
57                                         "data" : [{"Workspace": workspace, "Task": task, "JobId": jobId, "Packet": data}]
58                                 },
59                                 $.proxy(this._getTaskSuccess, this),
60                                 $.proxy(this._getTaskError, this)
61                         );
62                 },
63
64         /**
65          * Retrieve a Task Contract established with server.
66          * @param id
67          * @returns object|null
68          */
69                 taskContract : function( id )
70                 {
71                         return this.taskContracts[id]
72                                 ? this.taskContracts[id]
73                                 : null;
74                 },
75
76                 _getTaskSuccess : function( id, response )
77                 {
78                         response = response[0];
79                         if (!this.taskContracts[id])
80                         {
81                                 return;
82                         }
83                         this.taskContracts[id]["Response"] = response;
84                         if (response.Status && 'Success' != response.Status)
85                         {
86                                 if (this.taskContracts[id]['ErrorCallback'])
87                                 {
88                                         this.taskContracts[id]['ErrorCallback'](response);
89                                 }
90                         }
91                         else
92                         {
93                                 this.taskContracts[id]["Hash"] = response.Hash;
94                                 this.taskContracts[id]["LifeTime"] = response.LifeTime;
95                                 if (this.taskContracts[id]['Callback'])
96                                 {
97                                         this.taskContracts[id]['Callback'](response);
98                                 }
99                         }
100                 },
101
102                 _getTaskError : function( id, error )
103                 {
104                         this.taskContracts[id]["Response"] = error;
105                         if (this.taskContracts[id]['ErrorCallback'])
106                         {
107                                 this.taskContracts[id]['ErrorCallback'](error);
108                         }
109                 },
110
111         /**
112          * Establish multiple Task Contracts with server.
113          * @param contracts
114          */
115         getTasks : function( contracts, callback )
116         {
117                 var id = this.requestId;
118                 var requests = [];
119                 this.requestId++;
120                 this.requests.push({});
121                 this.requestCallback.push({
122                         callback: callback
123                 });
124                 for (var i in contracts)
125                 {
126                         this.requests[id][i] = contracts[i].id;
127                                 this.taskContracts[contracts[i].id] = {
128                                         "Callback" : contracts[i].callback,
129                                         "ErrorCallback" : contracts[i].errorCallback
130                                 };
131                                 jobId = (contracts[i].jobId)
132                                         ? contracts[i].jobId
133                                         : null;
134                                 data = (contracts[i].data)
135                                         ? contracts[i].data
136                                         : {};
137                                 requests.push({
138                                         "Workspace": contracts[i].workspace,
139                                         "Task": contracts[i].task,
140                                         "JobId": jobId,
141                                         "Packet": data
142                                 });
143                 }
144                         App.Ajax.JSON({
145                                         "id"   : id,
146                                         "url"  : 'workspace/contract-task',
147                                         "data" : requests
148                                 },
149                                 $.proxy(this._getTasksSuccess, this),
150                                 $.proxy(this._getTaskError, this)
151                         );
152                 },
153
154                 _getTasksSuccess : function( gid, responses )
155                 {
156                         for (var i in responses)
157                         {
158                                 response = responses[i];
159                                 var id = this.requests[gid][i];
160                                 if (!this.taskContracts[id])
161                                 {
162                                         return;
163                                 }
164                                 this.taskContracts[id]["Response"] = response;
165                                 if (undefined != response.Status && 'Success' != response.Status)
166                                 {
167                                         if (this.taskContracts[id]['ErrorCallback'])
168                                         {
169                                                 this.taskContracts[id]['ErrorCallback'](response);
170                                         }
171                                 }
172                                 else
173                                 {
174                                         this.taskContracts[id]["Hash"] = response.Hash;
175                                         this.taskContracts[id]["LifeTime"] = response.LifeTime;
176                                         if (this.taskContracts[id]['Callback'])
177                                         {
178                                                 this.taskContracts[id]['Callback'](response);
179                                         }
180                                 }
181                         }
182                         if (undefined != this.requestCallback[gid])
183                         {
184                                 this.requestCallback[gid].callback();
185                         }
186                         delete this.requests[gid];
187                         delete this.requestCallback[gid];
188                 },
189
190         /**
191          * Execute a Task Contract against server.
192          * @param id
193          * @param data
194          * @param options
195          * @param callback
196          * @param errorCallback
197          * @returns boolean|void
198          */
199                 execTask: function( id, data, options, callback, errorCallback, method, direct )
200                 {
201                         console.log('executetask',data);
202                         if (!this.taskContracts[id])
203                         {
204                                 console.log('! No contract for: ' + id);
205                                 return false;
206                         }
207                         this.taskContracts[id]["Callback"]      = callback;
208                         this.taskContracts[id]["ErrorCallback"] = errorCallback;
209                         data = (data)
210                                 ? data
211                                 : {};
212                         options = (options)
213                                 ? options
214                                 : {};
215                         if (!method)
216                         {
217                                 method = 'JSON';
218                         }
219                         var packet = {
220                                         "id"   : id,
221                             "url"  : 'workspace/execute-task',
222                             "data" : [{"Contract" : this.taskContracts[id]["Hash"], "Packet": data, "Options": options}]
223                         };
224                         if (direct)
225                         {
226                                 packet.direct = true;
227                         }
228                         App.Ajax[method](
229                                 packet,
230                         $.proxy(this._execTaskSuccess, this),
231                         $.proxy(this._execTaskError, this)
232                 );
233                 },
234
235                 _execTaskSuccess : function( id, response )
236                 {
237                         response = response[0];
238                         this.taskExecutions[id] = response;
239                         if (!this.taskContracts[id])
240                         {
241                                 console.log('Problem with contract execution, likely duplicate execution on UseOnce contract.');
242                                 console.log(id);
243                                 console.log(this.taskContracts);
244                         }
245                         if (response.Status && 'Success' != response.Status)
246                         {
247                                 if (this.taskContracts[id] && this.taskContracts[id]['ErrorCallback'])
248                                 {
249                                         this.taskContracts[id]['ErrorCallback'](response);
250                                 }
251                         }
252                         else
253                         {
254                                 var callback = this.taskContracts[id]['Callback']
255                                         ? this.taskContracts[id]['Callback']
256                                         : false;
257                                 if (this.taskContracts[id] && this.taskContracts[id]['LifeTime'] != 'Recurring')
258                                 {
259                                         delete this.taskContracts[id];
260                                 }
261                                 if (callback)
262                                 {
263                                         callback(response);
264                                 }
265                         }
266                 },
267
268                 _execTaskError : function( id, error )
269                 {
270                         this.taskExecutions[id] = error;
271                         if (this.taskContracts[id]['ErrorCallback'])
272                         {
273                                 this.taskContracts[id]['ErrorCallback'](error);
274                         }
275                 },
276
277
278
279         /**
280          * Execute a Task Contract against server.
281          * @param id
282          * @param data
283          * @param options
284          * @param callback
285          * @param errorCallback
286          * @returns boolean|void
287          */
288                 execTasks: function( contracts, callback )
289                                 //id, data, options, callback, errorCallback, method, direct )
290                 {
291                         var gid = this.requestId;
292                 var requests = [];
293                 this.requests.push({});
294                 this.requestCallback.push({
295                         callback: callback
296                 });
297                 this.requestId++;
298                         for (var i in contracts)
299                         {
300                                 this.requests[gid][i] = contracts[i].id;
301                                 this.taskContracts[contracts[i].id]["Callback"]      = contracts[i].callback;
302                                 this.taskContracts[contracts[i].id]["ErrorCallback"] = contracts[i].errorCallback;
303                                 requests.push({
304                                         "Contract" : this.taskContracts[contracts[i].id]["Hash"],
305                                         "Packet": contracts[i].data,
306                                         "Options": contracts[i].options
307                                 });
308                         }
309                         App.Ajax.JSON(
310                                         {
311                                                 "id"   : gid,
312                                     "url"  : 'workspace/execute-task',
313                                     "data" : requests
314                                 },
315                                 $.proxy(this._execTasksSuccess, this),
316                                 $.proxy(this._execTaskError, this)
317                         );
318                 },
319
320                 _execTasksSuccess : function( gid, responses )
321                 {
322                         for (var i in responses)
323                         {
324                                 response = responses[i];
325                                 var id = this.requests[gid][i];
326                                 this.taskExecutions[id] = response;
327                                 if (!this.taskContracts[id])
328                                 {
329                                         console.log('Problem with contract execution, likely duplicate execution on UseOnce contract.');
330                                         console.log(id);
331                                         console.log(this.taskContracts);
332                                 }
333                                 if (response.Status && 'Success' != response.Status)
334                                 {
335                                         if (this.taskContracts[id] && this.taskContracts[id]['ErrorCallback'])
336                                         {
337                                                 this.taskContracts[id]['ErrorCallback'](response);
338                                         }
339                                 }
340                                 else
341                                 {
342                                         if (this.taskContracts[id]['Callback'])
343                                         {
344                                                 this.taskContracts[id]['Callback'](response);
345                                         }
346                                         if (this.taskContracts[id]['LifeTime'] != 'Recurring')
347                                         {
348                                                 delete this.taskContracts[id];
349                                         }
350                                 }
351                         }
352                         if (undefined != this.requestCallback[gid])
353                         {
354                                 this.requestCallback[gid].callback(responses);
355                         }
356                         delete this.requests[gid];
357                         delete this.requestCallback[gid];
358                 },
359
360         /**
361          * Execute a Task Contract against server.
362          * @param id
363          * @param data
364          * @param options
365          * @param callback
366          * @param errorCallback
367          * @returns boolean|void
368          */
369                 execTaskMulti: function( id, data, options, callback, errorCallback, method, direct )
370                 {
371                         if (!this.taskContracts[id])
372                         {
373                                 console.log('! No contract for: ' + id);
374                                 return false;
375                         }
376                         this.taskContracts[id]["Callback"]      = callback;
377                         this.taskContracts[id]["ErrorCallback"] = errorCallback;
378                         if (!data)
379                         {
380                                 return false;
381                         }
382                         options = (options)
383                                 ? options
384                                 : [];
385                         if (!method)
386                         {
387                                 method = 'JSON';
388                         }
389                         var packed = [];
390                         for (var i = 0; i < data.length; i++)
391                         {
392                                 packed.push({
393                                         "Contract" : this.taskContracts[id]["Hash"],
394                                         "Packet": data[i],
395                                         "Options": options[i] ? options[i] : {}
396                                 });
397                         }
398                         var packet = {
399                                         "id"   : id,
400                             "url"  : 'workspace/execute-task',
401                             "data" : packed
402                         };
403                         if (direct)
404                         {
405                                 packet.direct = true;
406                         }
407                         App.Ajax[method](
408                                 packet,
409                         $.proxy(this._execTaskSuccessMulti, this),
410                         $.proxy(this._execTaskError, this)
411                 );
412                 },
413
414                 _execTaskSuccessMulti : function( id, responses )
415                 {
416                         var success = true;
417                         this.taskExecutions[id] = [];
418                         for (var i = 0; i < responses.length; i++)
419                         {
420                                 response = responses[i];
421                                 this.taskExecutions[id].push(response);
422                                 if (!this.taskContracts[id])
423                                 {
424                                         console.log('Problem with contract execution, likely duplicate execution on UseOnce contract.');
425                                         console.log(id);
426                                         console.log(this.taskContracts);
427                                 }
428                                 if (response.Status && 'Success' != response.Status)
429                                 {
430                                         success = false;
431                                 }
432                         }
433                         if (!success)
434                         {
435                                 if (this.taskContracts[id]['ErrorCallback'])
436                                 {
437                                         this.taskContracts[id]['ErrorCallback'](response);
438                                 }
439                         }
440                         else
441                         {
442                                 if (this.taskContracts[id]['Callback'])
443                                 {
444                                         this.taskContracts[id]['Callback'](this.taskExecutions[id]);
445                                 }
446                                 if (this.taskContracts[id]['LifeTime'] != 'Recurring')
447                                 {
448                                         delete this.taskContracts[id];
449                                 }
450                         }
451                 },
452
453                 _execTaskError : function( id, error )
454                 {
455                         this.taskExecutions[id] = error;
456                         if (this.taskContracts[id]['ErrorCallback'])
457                         {
458                                 this.taskContracts[id]['ErrorCallback'](error);
459                         }
460                 },
461
462
463
464
465
466
467
468
469
470
471         /* ------------------------------------- ROUTE HANDLING ------------------------------------- */
472         /**
473          * Directly route an item to a new sate.
474          * @param id
475          * @param workspace
476          * @param route
477          * @param jobId
478          * @param data
479          * @param callback
480          * @param errorCallback
481          */
482                 directRoute : function( id, workspace, route, jobId, data, callback, errorCallback )
483         {
484                         this.routeContracts[id] = {
485                                 "Callback" : callback,
486                                 "ErrorCallback" : errorCallback
487                         };
488                         jobId = (jobId)
489                                 ? jobId
490                                 : null;
491                         data = (data)
492                                 ? data
493                                 : {};
494                         App.Ajax.JSON({
495                                         "id"   : id,
496                                         "url"  : 'workspace/direct-route',
497                                         "data" : [{"Workspace": workspace, "Route": route, "JobId": jobId, "Packet": data}]
498                                 },
499                                 $.proxy(this._directRouteSuccess, this),
500                                 $.proxy(this._directRouteError, this)
501                         );
502                 },
503
504                 _directRouteSuccess : function( id, response )
505                 {
506                         response = response[0];
507                         this.routeExecutions[id] = response;
508                         if (response.Status && 'Success' != response.Status)
509                         {
510                                 if (this.routeContracts[id]['ErrorCallback'])
511                                 {
512                                         this.routeContracts[id]['ErrorCallback'](response);
513                                 }
514                         }
515                         else
516                         {
517                                 if (this.routeContracts[id]['Callback'])
518                                 {
519                                         this.routeContracts[id]['Callback'](response);
520                                 }
521                                 delete this.routeContracts[id];
522                         }
523                 },
524
525                 _directRouteError : function( id, error )
526                 {
527                         this.routeExecutions[id] = error;
528                         if (this.routeContracts[id]['ErrorCallback'])
529                         {
530                                 this.routeContracts[id]['ErrorCallback'](error);
531                         }
532                 },
533
534         /**
535          * Establish a Route Contract with server.
536          * @param id
537          * @param workspace
538          * @param route
539          * @param jobId
540          * @param data
541          * @param callback
542          * @param errorCallback
543          */
544                 getRoute : function( id, workspace, route, jobId, data, callback, errorCallback )
545         {
546                         this.routeContracts[id] = {
547                                 "Callback" : callback,
548                                 "ErrorCallback" : errorCallback
549                         };
550                         jobId = (jobId)
551                                 ? jobId
552                                 : null;
553                         data = (data)
554                                 ? data
555                                 : {};
556                         App.Ajax.JSON({
557                                         "id"   : id,
558                                         "url"  : 'workspace/contract-route',
559                                         "data" : [{"Workspace": workspace, "Route": route, "JobId": jobId, "Packet": data}]
560                                 },
561                                 $.proxy(this._getRouteSuccess, this),
562                                 $.proxy(this._getRouteError, this)
563                         );
564                 },
565
566         /**
567          * Retrieve a Route Contract established with server.
568          * @param id
569          * @returns object|null
570          */
571                 routeContract : function( id )
572                 {
573                         return this.routeContracts[id]
574                                 ? this.routeContracts[id]
575                                 : null;
576                 },
577
578                 _getRouteSuccess : function( id, response )
579                 {
580                         response = response[0];
581                         this.routeContracts[id]["Response"] = response;
582                         if (response.Status && 'Success' != response.Status)
583                         {
584                                 if (this.routeContracts[id]['ErrorCallback'])
585                                 {
586                                         this.routeContracts[id]['ErrorCallback'](response);
587                                 }
588                         }
589                         else
590                         {
591                                 this.routeContracts[id]["Hash"] = response.Hash;
592                                 this.routeContracts[id]["LifeTime"] = response.LifeTime;
593                                 if (this.routeContracts[id]['Callback'])
594                                 {
595                                         this.routeContracts[id]['Callback'](response);
596                                 }
597                         }
598                 },
599
600                 _getRouteError : function( id, error )
601                 {
602                         this.routeContracts[id]["Response"] = error;
603                         if (this.routeContracts[id]['ErrorCallback'])
604                         {
605                                 this.routeContracts[id]['ErrorCallback'](error);
606                         }
607                 },
608
609         /**
610          * Execute a Route Contract against server.
611          * @param id
612          * @param data
613          * @param options
614          * @param callback
615          * @param errorCallback
616          * @returns boolean|void
617          */
618                 execRoute: function( id, data, options, callback, errorCallback )
619                 {
620                         if (!this.routeContracts[id])
621                         {
622                                 return false;
623                         }
624                         this.routeContracts[id]["Callback"]      = callback;
625                         this.routeContracts[id]["ErrorCallback"] = errorCallback;
626                         data = (data)
627                                 ? data
628                                 : {};
629                         options = (options)
630                                 ? options
631                                 : {};
632                         App.Ajax.JSON({
633                                         "id"   : id,
634                             "url"  : 'workspace/execute-route',
635                             "data" : [{"Contract" : this.routeContracts[id]["Hash"], "Packet": data, "Options": options}]
636                         },
637                         $.proxy(this._execRouteSuccess, this),
638                         $.proxy(this._execRouteError, this)
639                 );
640                 },
641
642                 _execRouteSuccess : function( id, response )
643                 {
644                         response = response[0];
645                         this.routeExecutions[id] = response;
646                         if (response.Status && 'Success' != response.Status)
647                         {
648                                 if (this.routeContracts[id]['ErrorCallback'])
649                                 {
650                                         this.routeContracts[id]['ErrorCallback'](response);
651                                 }
652                         }
653                         else
654                         {
655                                 if (this.routeContracts[id]['Callback'])
656                                 {
657                                         this.routeContracts[id]['Callback'](response);
658                                 }
659                                 if (this.routeContracts[id]['LifeTime'] != 'Recurring')
660                                 {
661                                         delete this.routeContracts[id];
662                                 }
663                         }
664                 },
665
666                 _execRouteError : function( id, error )
667                 {
668                         this.routeExecutions[id] = error;
669                         if (this.routeContracts[id]['ErrorCallback'])
670                         {
671                                 this.routeContracts[id]['ErrorCallback'](error);
672                         }
673                 }
674         };
675
676 })();