initial commit
[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                         if (!this.taskContracts[id])
202                         {
203                                 console.log('! No contract for: ' + id);
204                                 return false;
205                         }
206                         this.taskContracts[id]["Callback"]      = callback;
207                         this.taskContracts[id]["ErrorCallback"] = errorCallback;
208                         data = (data)
209                                 ? data
210                                 : {};
211                         options = (options)
212                                 ? options
213                                 : {};
214                         if (!method)
215                         {
216                                 method = 'JSON';
217                         }
218                         var packet = {
219                                         "id"   : id,
220                             "url"  : 'workspace/execute-task',
221                             "data" : [{"Contract" : this.taskContracts[id]["Hash"], "Packet": data, "Options": options}]
222                         };
223                         if (direct)
224                         {
225                                 packet.direct = true;
226                         }
227                         App.Ajax[method](
228                                 packet,
229                         $.proxy(this._execTaskSuccess, this),
230                         $.proxy(this._execTaskError, this)
231                 );
232                 },
233
234                 _execTaskSuccess : function( id, response )
235                 {
236                         response = response[0];
237                         this.taskExecutions[id] = response;
238                         if (!this.taskContracts[id])
239                         {
240                                 console.log('Problem with contract execution, likely duplicate execution on UseOnce contract.');
241                                 console.log(id);
242                                 console.log(this.taskContracts);
243                         }
244                         if (response.Status && 'Success' != response.Status)
245                         {
246                                 if (this.taskContracts[id] && this.taskContracts[id]['ErrorCallback'])
247                                 {
248                                         this.taskContracts[id]['ErrorCallback'](response);
249                                 }
250                         }
251                         else
252                         {
253                                 var callback = this.taskContracts[id]['Callback']
254                                         ? this.taskContracts[id]['Callback']
255                                         : false;
256                                 if (this.taskContracts[id] && this.taskContracts[id]['LifeTime'] != 'Recurring')
257                                 {
258                                         delete this.taskContracts[id];
259                                 }
260                                 if (callback)
261                                 {
262                                         callback(response);
263                                 }
264                         }
265                 },
266
267                 _execTaskError : function( id, error )
268                 {
269                         this.taskExecutions[id] = error;
270                         if (this.taskContracts[id]['ErrorCallback'])
271                         {
272                                 this.taskContracts[id]['ErrorCallback'](error);
273                         }
274                 },
275
276
277
278         /**
279          * Execute a Task Contract against server.
280          * @param id
281          * @param data
282          * @param options
283          * @param callback
284          * @param errorCallback
285          * @returns boolean|void
286          */
287                 execTasks: function( contracts, callback )
288                                 //id, data, options, callback, errorCallback, method, direct )
289                 {
290                         var gid = this.requestId;
291                 var requests = [];
292                 this.requests.push({});
293                 this.requestCallback.push({
294                         callback: callback
295                 });
296                 this.requestId++;
297                         for (var i in contracts)
298                         {
299                                 this.requests[gid][i] = contracts[i].id;
300                                 this.taskContracts[contracts[i].id]["Callback"]      = contracts[i].callback;
301                                 this.taskContracts[contracts[i].id]["ErrorCallback"] = contracts[i].errorCallback;
302                                 requests.push({
303                                         "Contract" : this.taskContracts[contracts[i].id]["Hash"],
304                                         "Packet": contracts[i].data,
305                                         "Options": contracts[i].options
306                                 });
307                         }
308                         App.Ajax.JSON(
309                                         {
310                                                 "id"   : gid,
311                                     "url"  : 'workspace/execute-task',
312                                     "data" : requests
313                                 },
314                                 $.proxy(this._execTasksSuccess, this),
315                                 $.proxy(this._execTaskError, this)
316                         );
317                 },
318
319                 _execTasksSuccess : function( gid, responses )
320                 {
321                         for (var i in responses)
322                         {
323                                 response = responses[i];
324                                 var id = this.requests[gid][i];
325                                 this.taskExecutions[id] = response;
326                                 if (!this.taskContracts[id])
327                                 {
328                                         console.log('Problem with contract execution, likely duplicate execution on UseOnce contract.');
329                                         console.log(id);
330                                         console.log(this.taskContracts);
331                                 }
332                                 if (response.Status && 'Success' != response.Status)
333                                 {
334                                         if (this.taskContracts[id] && this.taskContracts[id]['ErrorCallback'])
335                                         {
336                                                 this.taskContracts[id]['ErrorCallback'](response);
337                                         }
338                                 }
339                                 else
340                                 {
341                                         if (this.taskContracts[id]['Callback'])
342                                         {
343                                                 this.taskContracts[id]['Callback'](response);
344                                         }
345                                         if (this.taskContracts[id]['LifeTime'] != 'Recurring')
346                                         {
347                                                 delete this.taskContracts[id];
348                                         }
349                                 }
350                         }
351                         if (undefined != this.requestCallback[gid])
352                         {
353                                 this.requestCallback[gid].callback(responses);
354                         }
355                         delete this.requests[gid];
356                         delete this.requestCallback[gid];
357                 },
358
359         /**
360          * Execute a Task Contract against server.
361          * @param id
362          * @param data
363          * @param options
364          * @param callback
365          * @param errorCallback
366          * @returns boolean|void
367          */
368                 execTaskMulti: function( id, data, options, callback, errorCallback, method, direct )
369                 {
370                         if (!this.taskContracts[id])
371                         {
372                                 console.log('! No contract for: ' + id);
373                                 return false;
374                         }
375                         this.taskContracts[id]["Callback"]      = callback;
376                         this.taskContracts[id]["ErrorCallback"] = errorCallback;
377                         if (!data)
378                         {
379                                 return false;
380                         }
381                         options = (options)
382                                 ? options
383                                 : [];
384                         if (!method)
385                         {
386                                 method = 'JSON';
387                         }
388                         var packed = [];
389                         for (var i = 0; i < data.length; i++)
390                         {
391                                 packed.push({
392                                         "Contract" : this.taskContracts[id]["Hash"],
393                                         "Packet": data[i],
394                                         "Options": options[i] ? options[i] : {}
395                                 });
396                         }
397                         var packet = {
398                                         "id"   : id,
399                             "url"  : 'workspace/execute-task',
400                             "data" : packed
401                         };
402                         if (direct)
403                         {
404                                 packet.direct = true;
405                         }
406                         App.Ajax[method](
407                                 packet,
408                         $.proxy(this._execTaskSuccessMulti, this),
409                         $.proxy(this._execTaskError, this)
410                 );
411                 },
412
413                 _execTaskSuccessMulti : function( id, responses )
414                 {
415                         var success = true;
416                         this.taskExecutions[id] = [];
417                         for (var i = 0; i < responses.length; i++)
418                         {
419                                 response = responses[i];
420                                 this.taskExecutions[id].push(response);
421                                 if (!this.taskContracts[id])
422                                 {
423                                         console.log('Problem with contract execution, likely duplicate execution on UseOnce contract.');
424                                         console.log(id);
425                                         console.log(this.taskContracts);
426                                 }
427                                 if (response.Status && 'Success' != response.Status)
428                                 {
429                                         success = false;
430                                 }
431                         }
432                         if (!success)
433                         {
434                                 if (this.taskContracts[id]['ErrorCallback'])
435                                 {
436                                         this.taskContracts[id]['ErrorCallback'](response);
437                                 }
438                         }
439                         else
440                         {
441                                 if (this.taskContracts[id]['Callback'])
442                                 {
443                                         this.taskContracts[id]['Callback'](this.taskExecutions[id]);
444                                 }
445                                 if (this.taskContracts[id]['LifeTime'] != 'Recurring')
446                                 {
447                                         delete this.taskContracts[id];
448                                 }
449                         }
450                 },
451
452                 _execTaskError : function( id, error )
453                 {
454                         this.taskExecutions[id] = error;
455                         if (this.taskContracts[id]['ErrorCallback'])
456                         {
457                                 this.taskContracts[id]['ErrorCallback'](error);
458                         }
459                 },
460
461
462
463
464
465
466
467
468
469
470         /* ------------------------------------- ROUTE HANDLING ------------------------------------- */
471         /**
472          * Directly route an item to a new sate.
473          * @param id
474          * @param workspace
475          * @param route
476          * @param jobId
477          * @param data
478          * @param callback
479          * @param errorCallback
480          */
481                 directRoute : function( id, workspace, route, jobId, data, callback, errorCallback )
482         {
483                         this.routeContracts[id] = {
484                                 "Callback" : callback,
485                                 "ErrorCallback" : errorCallback
486                         };
487                         jobId = (jobId)
488                                 ? jobId
489                                 : null;
490                         data = (data)
491                                 ? data
492                                 : {};
493                         App.Ajax.JSON({
494                                         "id"   : id,
495                                         "url"  : 'workspace/direct-route',
496                                         "data" : [{"Workspace": workspace, "Route": route, "JobId": jobId, "Packet": data}]
497                                 },
498                                 $.proxy(this._directRouteSuccess, this),
499                                 $.proxy(this._directRouteError, this)
500                         );
501                 },
502
503                 _directRouteSuccess : function( id, response )
504                 {
505                         response = response[0];
506                         this.routeExecutions[id] = response;
507                         if (response.Status && 'Success' != response.Status)
508                         {
509                                 if (this.routeContracts[id]['ErrorCallback'])
510                                 {
511                                         this.routeContracts[id]['ErrorCallback'](response);
512                                 }
513                         }
514                         else
515                         {
516                                 if (this.routeContracts[id]['Callback'])
517                                 {
518                                         this.routeContracts[id]['Callback'](response);
519                                 }
520                                 delete this.routeContracts[id];
521                         }
522                 },
523
524                 _directRouteError : function( id, error )
525                 {
526                         this.routeExecutions[id] = error;
527                         if (this.routeContracts[id]['ErrorCallback'])
528                         {
529                                 this.routeContracts[id]['ErrorCallback'](error);
530                         }
531                 },
532
533         /**
534          * Establish a Route Contract with server.
535          * @param id
536          * @param workspace
537          * @param route
538          * @param jobId
539          * @param data
540          * @param callback
541          * @param errorCallback
542          */
543                 getRoute : function( id, workspace, route, jobId, data, callback, errorCallback )
544         {
545                         this.routeContracts[id] = {
546                                 "Callback" : callback,
547                                 "ErrorCallback" : errorCallback
548                         };
549                         jobId = (jobId)
550                                 ? jobId
551                                 : null;
552                         data = (data)
553                                 ? data
554                                 : {};
555                         App.Ajax.JSON({
556                                         "id"   : id,
557                                         "url"  : 'workspace/contract-route',
558                                         "data" : [{"Workspace": workspace, "Route": route, "JobId": jobId, "Packet": data}]
559                                 },
560                                 $.proxy(this._getRouteSuccess, this),
561                                 $.proxy(this._getRouteError, this)
562                         );
563                 },
564
565         /**
566          * Retrieve a Route Contract established with server.
567          * @param id
568          * @returns object|null
569          */
570                 routeContract : function( id )
571                 {
572                         return this.routeContracts[id]
573                                 ? this.routeContracts[id]
574                                 : null;
575                 },
576
577                 _getRouteSuccess : function( id, response )
578                 {
579                         response = response[0];
580                         this.routeContracts[id]["Response"] = response;
581                         if (response.Status && 'Success' != response.Status)
582                         {
583                                 if (this.routeContracts[id]['ErrorCallback'])
584                                 {
585                                         this.routeContracts[id]['ErrorCallback'](response);
586                                 }
587                         }
588                         else
589                         {
590                                 this.routeContracts[id]["Hash"] = response.Hash;
591                                 this.routeContracts[id]["LifeTime"] = response.LifeTime;
592                                 if (this.routeContracts[id]['Callback'])
593                                 {
594                                         this.routeContracts[id]['Callback'](response);
595                                 }
596                         }
597                 },
598
599                 _getRouteError : function( id, error )
600                 {
601                         this.routeContracts[id]["Response"] = error;
602                         if (this.routeContracts[id]['ErrorCallback'])
603                         {
604                                 this.routeContracts[id]['ErrorCallback'](error);
605                         }
606                 },
607
608         /**
609          * Execute a Route Contract against server.
610          * @param id
611          * @param data
612          * @param options
613          * @param callback
614          * @param errorCallback
615          * @returns boolean|void
616          */
617                 execRoute: function( id, data, options, callback, errorCallback )
618                 {
619                         if (!this.routeContracts[id])
620                         {
621                                 return false;
622                         }
623                         this.routeContracts[id]["Callback"]      = callback;
624                         this.routeContracts[id]["ErrorCallback"] = errorCallback;
625                         data = (data)
626                                 ? data
627                                 : {};
628                         options = (options)
629                                 ? options
630                                 : {};
631                         App.Ajax.JSON({
632                                         "id"   : id,
633                             "url"  : 'workspace/execute-route',
634                             "data" : [{"Contract" : this.routeContracts[id]["Hash"], "Packet": data, "Options": options}]
635                         },
636                         $.proxy(this._execRouteSuccess, this),
637                         $.proxy(this._execRouteError, this)
638                 );
639                 },
640
641                 _execRouteSuccess : function( id, response )
642                 {
643                         response = response[0];
644                         this.routeExecutions[id] = response;
645                         if (response.Status && 'Success' != response.Status)
646                         {
647                                 if (this.routeContracts[id]['ErrorCallback'])
648                                 {
649                                         this.routeContracts[id]['ErrorCallback'](response);
650                                 }
651                         }
652                         else
653                         {
654                                 if (this.routeContracts[id]['Callback'])
655                                 {
656                                         this.routeContracts[id]['Callback'](response);
657                                 }
658                                 if (this.routeContracts[id]['LifeTime'] != 'Recurring')
659                                 {
660                                         delete this.routeContracts[id];
661                                 }
662                         }
663                 },
664
665                 _execRouteError : function( id, error )
666                 {
667                         this.routeExecutions[id] = error;
668                         if (this.routeContracts[id]['ErrorCallback'])
669                         {
670                                 this.routeContracts[id]['ErrorCallback'](error);
671                         }
672                 }
673         };
674
675 })();