Update is not using the loop per platform in JS, we don't manage partial answers...
[unfold.git] / manifoldapi / static / js / manifold.js
1 // utilities 
2 function debug_dict_keys (msg, o) {
3     var keys=[];
4     for (var k in o) keys.push(k);
5     messages.debug ("debug_dict_keys: " + msg + " keys= " + keys);
6 }
7 function debug_dict (msg, o) {
8     for (var k in o) messages.debug ("debug_dict: " + msg + " [" + k + "]=" + o[k]);
9 }
10 function debug_value (msg, value) {
11     messages.debug ("debug_value: " + msg + " " + value);
12 }
13 function debug_query (msg, query) {
14     if (query === undefined) messages.debug ("debug_query: " + msg + " -> undefined");
15     else if (query == null) messages.debug ("debug_query: " + msg + " -> null");
16     else if ('query_uuid' in query) messages.debug ("debug_query: " + msg + query.__repr());
17     else messages.debug ("debug_query: " + msg + " query= " + query);
18 }
19
20 // http://stackoverflow.com/questions/7837456/comparing-two-arrays-in-javascript
21 // attach the .equals method to Array's prototype to call it on any array
22 Array.prototype.equals = function (array) {
23     // if the other array is a falsy value, return
24     if (!array)
25         return false;
26
27     // compare lengths - can save a lot of time 
28     if (this.length != array.length)
29         return false;
30
31     for (var i = 0, l=this.length; i < l; i++) {
32         // Check if we have nested arrays
33         if (this[i] instanceof Array && array[i] instanceof Array) {
34             // recurse into the nested arrays
35             if (!this[i].equals(array[i]))
36                 return false;
37         }
38         else if (this[i] != array[i]) {
39             // Warning - two different object instances will never be equal: {x:20} != {x:20}
40             return false;
41         }
42     }
43     return true;
44 }
45
46 // http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
47 Object.toType = (function toType(global) {
48   return function(obj) {
49     if (obj === global) {
50       return "global";
51     }
52     return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase();
53   }
54 })(this);
55
56 /* ------------------------------------------------------------ */
57
58 // Constants that should be somehow moved to a plugin.js file
59 var FILTER_ADDED   = 1;
60 var FILTER_REMOVED = 2;
61 var CLEAR_FILTERS  = 3;
62 var FIELD_ADDED    = 4;
63 var FIELD_REMOVED  = 5;
64 var CLEAR_FIELDS   = 6;
65 var NEW_RECORD     = 7;
66 var CLEAR_RECORDS  = 8;
67
68 /**
69  * event: FIELD_STATE_CHANGED
70  *
71  * Parameters:
72  *   dict :
73  *      .state      : ???? used to be FIELD_REQUEST_ADD / FIELD_REQUEST_REMOVE
74  *      .key        : ??? the key fields of the record
75  *      .op         : the key of the record who has received an update
76  *      .value      : the new state of the record
77  */
78 var FIELD_STATE_CHANGED = 9;
79
80 var IN_PROGRESS    = 101;
81 var DONE           = 102; //XXX Should be harmonized with query state
82
83 /* Update requests related to subqueries */
84
85 /*
86 var SET_ADD        = 201;
87 var SET_REMOVED    = 202;
88 */
89
90 // request
91 /*
92 var FIELD_REQUEST_CHANGE  = 301;
93 var FIELD_REQUEST_ADD     = 302;
94 var FIELD_REQUEST_REMOVE  = 303;
95 var FIELD_REQUEST_ADD_RESET = 304;
96 var FIELD_REQUEST_REMOVE_RESET = 305;
97 */
98 // status (XXX Should be deprecated)
99 var FIELD_REQUEST_PENDING = 401;
100 var FIELD_REQUEST_SUCCESS = 402;
101 var FIELD_REQUEST_FAILURE = 403;
102 var STATUS_OKAY           = 404;
103 var STATUS_SET_WARNING    = 405;
104 var STATUS_ADD_WARNING    = 406;
105 var STATUS_REMOVE_WARNING = 407;
106 var STATUS_RESET          = 408;
107
108 /* Requests for query cycle */
109 var RUN_UPDATE     = 601;
110
111 /* MANIFOLD types */
112 var TYPE_VALUE  = 1;
113 var TYPE_RECORD = 2;
114 var TYPE_LIST_OF_VALUES = 3;
115 var TYPE_LIST_OF_RECORDS = 4;
116
117 /******************************************************************************
118  * QUERY STATUS (for manifold events)
119  ******************************************************************************/
120
121 var STATUS_NONE               = 500; // Query has not been started yet
122 var STATUS_GET_IN_PROGRESS    = 501; // Query has been sent, no result has been received
123 var STATUS_GET_RECEIVED       = 502; // Success
124 var STATUS_GET_ERROR          = 503; // Error
125 var STATUS_UPDATE_PENDING     = 504;
126 var STATUS_UPDATE_IN_PROGRESS = 505;
127 var STATUS_UPDATE_RECEIVED    = 506;
128 var STATUS_UPDATE_ERROR       = 507;
129
130 /******************************************************************************
131  * QUERY STATE (for query_store)
132  ******************************************************************************/
133
134 // XXX Rendundant with query status ?
135
136 var QUERY_STATE_INIT        = 0;
137 var QUERY_STATE_INPROGRESS  = 1;
138 var QUERY_STATE_DONE        = 2;
139
140 /******************************************************************************
141  * RECORD STATES (for query_store)
142  ******************************************************************************/
143
144 var STATE_SET       = 0;
145 var STATE_VALUE     = 1;
146 var STATE_WARNINGS  = 2;
147 var STATE_VISIBLE   = 3;
148
149 // ACTIONS
150 var STATE_SET_CHANGE = 0;
151 var STATE_SET_ADD    = 1;
152 var STATE_SET_REMOVE = 2;
153 var STATE_SET_CLEAR  = 3;
154
155 // STATE_SET : enum
156 var STATE_SET_IN            = 0;
157 var STATE_SET_OUT           = 1;
158 var STATE_SET_IN_PENDING    = 2;
159 var STATE_SET_OUT_PENDING   = 3;
160 var STATE_SET_IN_SUCCESS    = 4;
161 var STATE_SET_OUT_SUCCESS   = 5;
162 var STATE_SET_IN_FAILURE    = 6;
163 var STATE_SET_OUT_FAILURE   = 7;
164 var STATE_VALUE_CHANGE_PENDING    = 8;
165 var STATE_VALUE_CHANGE_SUCCESS    = 9;
166 var STATE_VALUE_CHANGE_FAILURE    = 10;
167
168 // STATE_WARNINGS : dict
169
170 // STATE_VISIBLE : boolean
171
172 /******************************************************************************
173  * CONSTRAINTS
174  ******************************************************************************/
175
176 var CONSTRAINT_RESERVABLE_LEASE     = 0;
177
178 var CONSTRAINT_RESERVABLE_LEASE_MSG = "Configuration required: this resource needs to be scheduled";
179
180 // A structure for storing queries
181
182 function QueryExt(query, parent_query_ext, main_query_ext, update_query_ext, disabled, domain_query_ext) {
183
184     /* Constructor */
185     if (typeof query == "undefined")
186         throw "Must pass a query in QueryExt constructor";
187     this.query                 = query;
188     this.parent_query_ext      = (typeof parent_query_ext      == "undefined") ? null  : parent_query_ext;
189     this.main_query_ext        = (typeof main_query_ext        == "undefined") ? null  : main_query_ext;
190     this.update_query_ext      = (typeof update_query_ext      == "undefined") ? null  : update_query_ext;
191     this.update_query_orig_ext = (typeof update_query_orig_ext == "undefined") ? null  : update_query_orig_ext;
192     this.disabled              = (typeof disabled              == "undefined") ? false : disabled;
193
194     // A domain query is a query that is issued to retrieve all possible values for a set
195     // eg. all resources that can be attached to a slice
196     // It is null unless we are a subquery for which a domain query has been issued
197     this.domain_query_ext      = (typeof domain_query_ext      == "undefined") ? null  : domain_query_ext;
198
199     // Set members to buffer until the domain query is completed
200     // A list of keys
201     this.set_members = [];
202
203     // The set query is the query for which the domain query has been issued.
204     // It is null unless the query is a domain query
205     this.set_query_ext         = (typeof set_query_ext         == "undefined") ? null  : domain_query_ext;
206     
207     this.query_state = QUERY_STATE_INIT;
208
209     // Results from a query consists in a dict that maps keys to records
210     this.records = new Hashtable();
211
212     // Status is a dict that maps keys to record status
213     this.state = new Hashtable();
214
215     // Filters that impact visibility in the local interface
216     this.filters = [];
217
218     // XXX Until we find a better solution
219     this.num_pending = 0;
220     this.num_unconfigured = 0;
221
222     // update_query null unless we are a main_query (aka parent_query == null); only main_query_fields can be updated...
223 }
224
225 function QueryStore() {
226
227     this.main_queries     = {};
228     this.analyzed_queries = {};
229
230     /* Insertion */
231
232     this.insert = function(query) {
233         // We expect only main_queries are inserted
234         
235         /* If the query has not been analyzed, then we analyze it */
236         if (query.analyzed_query == null) {
237             query.analyze_subqueries();
238         }
239
240         /* We prepare the update query corresponding to the main query and store both */
241         /* Note: they have the same UUID */
242
243         // XXX query.change_action() should become deprecated
244         update_query = query.clone();
245         update_query.action = 'update';
246         update_query.fields = [];
247         update_query.analyzed_query.action = 'update';
248         update_query.params = {};
249         update_query_ext = new QueryExt(update_query);
250
251         /* We remember the original query to be able to reset it */
252         update_query_orig_ext = new QueryExt(update_query.clone());
253
254
255         /* We store the main query */
256         query_ext = new QueryExt(query, null, null, update_query_ext, update_query_orig_ext, false);
257         manifold.query_store.main_queries[query.query_uuid] = query_ext;
258         /* Note: the update query does not have an entry! */
259
260
261         // The query is disabled; since it is incomplete until we know the content of the set of subqueries
262         // XXX unless we have no subqueries ???
263         // we will complete with params when records are received... this has to be done by the manager
264         // SET_ADD, SET_REMOVE will change the status of the elements of the set
265         // UPDATE will change also, etc.
266         // XXX We need a proper structure to store this information...
267
268         // We also need to insert all queries and subqueries from the analyzed_query
269         // XXX We need the root of all subqueries
270         query.iter_subqueries(function(sq, data, parent_query) {
271             var parent_query_ext;
272             if (parent_query) {
273                 parent_query_ext = manifold.query_store.find_analyzed_query_ext(parent_query.query_uuid);
274             } else {
275                 parent_query_ext = null;
276             }
277             // XXX parent_query_ext == false
278             // XXX main.subqueries = {} # Normal, we need analyzed_query
279             sq_ext = new QueryExt(sq, parent_query_ext, query_ext)
280
281             if (parent_query) {
282                 /* Let's issue a query for the subquery domain. This query will not need any update etc.
283                    eg. for resources in a slice, we also query all resources */
284                 var all_fields = manifold.metadata.get_field_names(sq.object);
285                 var domain_query = new ManifoldQuery('get', sq.object, 'now', [], {}, all_fields); 
286                 //var domain_query = new ManifoldQuery('get', sq.object); 
287
288                 console.log("Created domain query", domain_query);
289                 var domain_query_ext = new QueryExt(domain_query);
290
291                 domain_query_ext.set_query_ext = sq_ext;
292                 sq_ext.domain_query_ext = domain_query_ext;
293
294                 // One of these two is useless ?
295                 manifold.query_store.main_queries[domain_query.query_uuid] = domain_query_ext;
296                 manifold.query_store.analyzed_queries[domain_query.query_uuid] = domain_query_ext;
297
298                 // XXX This query is run before the plugins are initialized and listening
299                 manifold.run_query(domain_query);
300             }
301
302             manifold.query_store.analyzed_queries[sq.query_uuid] = sq_ext;
303         });
304
305         // XXX We have spurious update queries...
306     }
307
308     /* Searching */
309
310     this.find_query_ext = function(query_uuid)
311     {
312         return this.main_queries[query_uuid];
313     }
314
315     this.find_query = function(query_uuid) 
316     {
317         return this.find_query_ext(query_uuid).query;
318     }
319
320     this.find_analyzed_query_ext = function(query_uuid)
321     {
322         return this.analyzed_queries[query_uuid];
323     }
324
325     this.find_analyzed_query = function(query_uuid) 
326     {
327         return this.find_analyzed_query_ext(query_uuid).query;
328     }
329
330     this.state_dict_create = function(default_set)
331     {
332         default_set = (default_set === undefined) ? STATE_SET_OUT : default_set;
333         var state_dict = {};
334         // We cannot use constants in literal definition, so...
335         state_dict[STATE_WARNINGS] = {};
336         state_dict[STATE_SET] = default_set;
337         state_dict[STATE_VISIBLE] = true;
338         return state_dict;
339     }
340
341     // RECORDS
342
343     this.set_records = function(query_uuid, records, default_set)
344     {
345         default_set = (default_set === undefined) ? STATE_SET_OUT : default_set;
346
347         var self = this;
348         var key, object, query_ext, record_key;
349
350         query_ext = this.find_analyzed_query_ext(query_uuid);
351         object = query_ext.query.object;
352         if (object.indexOf(':') != -1) {
353             object = object.split(':')[1];
354         }
355         record_key = manifold.metadata.get_key(object);
356
357         // ["start_time", "resource", "end_time"]
358         // ["urn"]
359         
360         $.each(records, function(i, record) {
361             //var key = manifold.metadata.get_key(query_ext.query.object);
362             
363             var record_key_value = manifold.record_get_value(record, record_key);
364             query_ext.records.put(record_key_value, record);
365
366             if (!(query_ext.state.get(record_key_value)))
367                 query_ext.state.put(record_key_value, self.state_dict_create(default_set));
368         });
369     }
370
371     this.get_records = function(query_uuid)
372     {
373         var query_ext = this.find_analyzed_query_ext(query_uuid);
374         return query_ext.records.values();
375     }
376
377     this.get_record = function(query_uuid, record_key)
378     {
379         var query_ext = this.find_analyzed_query_ext(query_uuid);
380         return query_ext.records.get(record_key);
381     }
382
383     this.del_record = function(query_uuid, record_key)
384     {
385         var query_ext = this.find_analyzed_query_ext(query_uuid);
386         return query_ext.records.remove(record_key);
387     }
388
389     this.del_state = function(query_uuid, record_key)
390     {
391         var query_ext = this.find_analyzed_query_ext(query_uuid);
392         return query_ext.state.remove(record_key);
393     }
394
395     this.add_record = function(query_uuid, record, new_state)
396     {
397         var query_ext, key, record_key;
398         query_ext = this.find_analyzed_query_ext(query_uuid);
399         
400         if (typeof(record) == 'object') {
401             key = manifold.metadata.get_key(query_ext.query.object);
402             record_key = manifold.record_get_value(record, key);
403         } else {
404             record_key = record;
405         }
406
407         var record_entry = query_ext.records.get(record_key);
408         if (!record_entry)
409             query_ext.records.put(record_key, record);
410
411         manifold.query_store.set_record_state(query_uuid, record_key, STATE_SET, new_state);
412     }
413
414     this.remove_record = function(query_uuid, record, new_state)
415     {
416         var query_ext, key, record_key;
417         query_ext = this.find_analyzed_query_ext(query_uuid);
418         
419         if (typeof(record) == 'object') {
420             key = manifold.metadata.get_key(query_ext.query.object);
421             record_key = manifold.record_get_value(record, key);
422         } else {
423             record_key = record;
424         }
425         
426         if ((query_ext.query.object == 'lease') && (new_state == STATE_SET_OUT)) {
427             // Leases that are marked out are in fact leases from other slices
428             // We need to _remove_ leases that we mark as OUT
429             manifold.query_store.del_record(query_uuid, record_key);
430             manifold.query_store.del_state(query_uuid, record_key);
431         } else {
432             manifold.query_store.set_record_state(query_uuid, record_key, STATE_SET, new_state);
433         }
434     }
435
436     this.iter_records = function(query_uuid, callback)
437     {
438         var query_ext = this.find_analyzed_query_ext(query_uuid);
439         query_ext.records.each(callback);
440         //callback = function(record_key, record)
441     }
442
443     this.iter_visible_records = function(query_uuid, callback)
444     {
445         var query_ext = this.find_analyzed_query_ext(query_uuid);
446         query_ext.records.each(function(record_key, record) {
447             if (query_ext.state.get(record_key)[STATE_VISIBLE]) // .STATE_VISIBLE would be for the string key
448                 callback(record_key, record);
449         });
450         //callback = function(record_key, record)
451
452     }
453
454     // STATE
455
456     this.set_record_state = function(query_uuid, result_key, state, value)
457     {
458         var query_ext = this.find_analyzed_query_ext(query_uuid);
459         var state_dict = query_ext.state.get(result_key);
460         if (!state_dict)
461             state_dict = this.state_dict_create();
462
463         state_dict[state] = value;
464
465         query_ext.state.put(result_key, state_dict);
466     }
467
468     this.get_record_state = function(query_uuid, result_key, state)
469     {
470         var query_ext = this.find_analyzed_query_ext(query_uuid);
471         var state_dict = query_ext.state.get(result_key);
472         if (!state_dict)
473             return null;
474         return state_dict[state];
475     }
476
477     // FILTERS
478
479     this.add_filter = function(query_uuid, filter)
480     {
481         var query_ext = this.find_analyzed_query_ext(query_uuid);
482         // XXX When we update a filter
483         query_ext.filters.push(filter);
484
485         this.apply_filters(query_uuid);
486
487     }
488
489     this.update_filter = function(query_uuid, filter)
490     {
491         // XXX
492
493         this.apply_filters(query_uuid);
494     }
495
496     this.remove_filter = function(query_uuid, filter)
497     {
498         var query_ext = this.find_analyzed_query_ext(query_uuid);
499         query_ext.filters = $.grep(query_ext.filters, function(x) {
500             return !(x.equals(filter));
501         });
502
503         this.apply_filters(query_uuid);
504     }
505
506     this.get_filters = function(query_uuid)
507     {
508         var query_ext = this.find_analyzed_query_ext(query_uuid);
509         return query_ext.filters;
510     }
511
512     this.recount = function(query_uuid)
513     {
514         var query_ext;
515         var is_reserved, is_pending, in_set,  is_unconfigured;
516
517         query_ext = manifold.query_store.find_analyzed_query_ext(query_uuid);
518         query_ext.num_pending = 0;
519         query_ext.num_unconfigured = 0;
520
521         this.iter_records(query_uuid, function(record_key, record) {
522             var record_state = manifold.query_store.get_record_state(query_uuid, record_key, STATE_SET);
523             var record_warnings = manifold.query_store.get_record_state(query_uuid, record_key, STATE_WARNINGS);
524
525             is_reserved = (record_state == STATE_SET_IN) 
526                        || (record_state == STATE_SET_OUT_PENDING)
527                        || (record_state == STATE_SET_IN_SUCCESS)
528                        || (record_state == STATE_SET_OUT_FAILURE);
529
530             is_pending = (record_state == STATE_SET_IN_PENDING) 
531                       || (record_state == STATE_SET_OUT_PENDING);
532
533             in_set = (record_state == STATE_SET_IN) // should not have warnings
534                   || (record_state == STATE_SET_IN_PENDING)
535                   || (record_state == STATE_SET_IN_SUCCESS)
536                   || (record_state == STATE_SET_OUT_FAILURE); // should not have warnings
537
538             is_unconfigured = (in_set && !$.isEmptyObject(record_warnings));
539
540             /* Let's update num_pending and num_unconfigured at this stage */
541             if (is_pending)
542                 query_ext.num_pending++;
543             if (is_unconfigured)
544                 query_ext.num_unconfigured++;
545         });
546
547     }
548
549     this.apply_filters = function(query_uuid)
550     {
551         var start = new Date().getTime();
552
553         // Toggle visibility of records according to the different filters.
554
555         var self = this;
556         var filters = this.get_filters(query_uuid);
557         var col_value;
558         /* Let's update num_pending and num_unconfigured at this stage */
559
560         // Adapted from querytable._querytable_filter()
561
562         this.iter_records(query_uuid, function(record_key, record) {
563             var is_reserved, is_pending, in_set,  is_unconfigured;
564
565             /* By default, a record is visible unless a filter says the opposite */
566             var visible = true;
567
568             var record_state = manifold.query_store.get_record_state(query_uuid, record_key, STATE_SET);
569             var record_warnings = manifold.query_store.get_record_state(query_uuid, record_key, STATE_WARNINGS);
570
571             is_reserved = (record_state == STATE_SET_IN) 
572                        || (record_state == STATE_SET_OUT_PENDING)
573                        || (record_state == STATE_SET_IN_SUCCESS)
574                        || (record_state == STATE_SET_OUT_FAILURE);
575
576             is_pending = (record_state == STATE_SET_IN_PENDING) 
577                       || (record_state == STATE_SET_OUT_PENDING);
578
579             in_set = (record_state == STATE_SET_IN) // should not have warnings
580                   || (record_state == STATE_SET_IN_PENDING)
581                   || (record_state == STATE_SET_IN_SUCCESS)
582                   || (record_state == STATE_SET_OUT_FAILURE); // should not have warnings
583
584             is_unconfigured = (in_set && !$.isEmptyObject(record_warnings));
585
586             // We go through each filter and decide whether it affects the visibility of the record
587             $.each(filters, function(index, filter) {
588                 var key = filter[0];
589                 var op = filter[1];
590                 var value = filter[2];
591
592
593                 /* We do some special handling for the manifold:status filter
594                  * predicates. */
595
596                 if (key == 'manifold:status') {
597                     if (op != '=' && op != '==') {
598                         // Unsupported filter, let's ignore it
599                         console.log("Unsupported filter on manifold:status. Should be EQUAL only.");
600                         return true; // ~ continue
601                     }
602
603                     switch (value) {
604                         case 'reserved':
605                             // true  => ~ continue
606                             // false => ~ break
607                             visible = is_reserved;
608                             return visible;
609                         case 'unconfigured':
610                             visible = is_unconfigured;
611                             return visible;
612                         case 'pending':
613                             visible = is_pending;
614                             return visible;
615                     }
616                     return false; // ~ break
617                 }
618
619                 /* Normal filtering behaviour (according to the record content) follows... */
620                 col_value = manifold.record_get_value(record, key);
621
622                 // When the filter does not match, we hide the column by default
623                 if (col_value === 'undefined') {
624                     visible = false;
625                     return false; // ~ break
626                 }
627
628                 // XXX This should accept pluggable filtering functions.
629
630
631                 /* Test whether current filter is compatible with the column */
632                 if (op == '=' || op == '==') {
633                     if ( col_value != value || col_value==null || col_value=="" || col_value=="n/a")
634                         visible = false;
635
636                 }else if (op == 'included') {
637                     /* By default, the filter returns false unless the record
638                      * field match at least one value of the included statement
639                      */
640                     visible = false;
641                     $.each(value, function(i,x) {
642                       if(x == col_value){
643                           visible = true;
644                           return false; // ~ break
645                       }
646                     });
647                 }else if (op == '!=') {
648                     if ( col_value == value || col_value==null || col_value=="" || col_value=="n/a")
649                         visible = false;
650                 } else if(op=='<') {
651                     if ( parseFloat(col_value) >= value || col_value==null || col_value=="" || col_value=="n/a")
652                         visible = false;
653                 } else if(op=='>') {
654                     if ( parseFloat(col_value) <= value || col_value==null || col_value=="" || col_value=="n/a")
655                         visible = false;
656                 } else if(op=='<=' || op=='≤') {
657                     if ( parseFloat(col_value) > value || col_value==null || col_value=="" || col_value=="n/a")
658                         visible = false;
659                 } else if(op=='>=' || op=='≥') {
660                     if ( parseFloat(col_value) < value || col_value==null || col_value=="" || col_value=="n/a")
661                         visible = false;
662                 }else{
663                     // How to break out of a loop ?
664                     alert("filter not supported");
665                     return false; // break
666                 }
667
668             });
669
670             // Set the visibility status in the query store
671             self.set_record_state(query_uuid, record_key, STATE_VISIBLE, visible);
672         });
673
674         var end = new Date().getTime();
675         console.log("APPLY FILTERS [", filters, "] took", end - start, "ms");
676
677     }
678
679 }
680
681 /*!
682  * This namespace holds functions for globally managing query objects
683  * \Class Manifold
684  */
685 var manifold = {
686
687     /************************************************************************** 
688      * Helper functions
689      **************************************************************************/ 
690
691     separator: '__',
692
693     get_type: function(variable) {
694         switch(Object.toType(variable)) {
695             case 'number':
696             case 'string':
697                 return TYPE_VALUE;
698             case 'object':
699                 return TYPE_RECORD;
700             case 'array':
701                 if ((variable.length > 0) && (Object.toType(variable[0]) === 'object'))
702                     return TYPE_LIST_OF_RECORDS;
703                 else
704                     return TYPE_LIST_OF_VALUES;
705         }
706     },
707
708     /**
709      *  Args:
710      *      fields: A String instance (field name), or a set of String instances
711      *          (field names) # XXX tuple !!
712      *  Returns:
713      *      If fields is a String,  return the corresponding value.
714      *      If fields is a set, return a tuple of corresponding value.
715      *
716      *  Raises:
717      *      KeyError if at least one of the fields is not found
718      */
719     record_get_value: function(record, fields) 
720     {
721         if (typeof(fields) === 'string') {
722             if (fields.indexOf('.') != -1) {
723                 key_subkey = key.split('.', 2);
724                 key     = key_subkey[0]; 
725                 subkey  = key_subkey[1];
726
727                 if (record.indexOf(key) == -1) {
728                     return null;
729                 }
730                 // Tests if the following is an array (typeof would give object)
731                 if (Object.prototype.toString.call(record[key]) === '[object Array]') {
732                     // Records
733                     return $.map(record[key], function(subrecord) { return manifold.record_get_value(subrecord, subkey) });
734                 } else if (typeof(record) == 'object') {
735                     // Record
736                     return manifold.record_get_value(record[key], subkey);
737                 } else {
738                     console.log('Unknown field');
739                 }
740             } else {
741                 return record[fields];
742             }
743         } else {
744             // see. get_map_entries
745             if (fields.length == 1)
746                 return manifold.record_get_value(record, fields[0])
747
748             // Build a new record
749             var ret = {};
750             $.each(fields, function(i, field) {
751                 ret[field] = manifold.record_get_value(record, field);
752             });
753             ret.hashCode = record.hashCode;
754             ret.equals = record.equals;
755             return ret;
756             // this was an array, we want a dictionary
757             //return $.map(fields, function(x) { manifold.record_get_value(record, x) });
758                 
759         }
760     },
761
762     record_hashcode: function(key_fields)
763     {
764         return function() {
765             ret = "";
766             for (var i=0; i < key_fields.length; i++)
767                 ret += "@@" + this[key_fields[i]];
768             return ret;
769         };
770     },
771
772     _record_equals: function(self, other, key_fields)
773     {
774         if ((typeof self === "string") && (typeof other === "string")) {
775             return self == other;
776         }
777         for (var i=0; i < key_fields.length; i++) {
778             var this_value  = self[key_fields[i]];
779             var other_value = other[key_fields[i]];
780
781             var this_type = manifold.get_type(this_value);
782             var other_type = manifold.get_type(other_value);
783             if (this_type != other_type)
784                 return false;
785
786             switch (this_type) {
787                 case TYPE_VALUE:
788                 case TYPE_LIST_OF_VALUES:
789                 case TYPE_LIST_OF_RECORDS:
790                     if (this_value != other_value)
791                         return false;
792                     break;
793                 case TYPE_RECORD:
794                     if (!(_record_equals(this_value, other_value, key_fields)))
795                         return false;
796                     break;
797                 /*
798                 XXX WARNING = disabled for OpenFlow plugin !!!
799
800                 case TYPE_LIST_OF_RECORDS:
801                     if (this_value.length != other_value.length)
802                         return false;
803                     for (var j = 0; j < this_value.length; j++)
804                         if (!(_record_equals(this_value[j], other_value[j], key_fields)))
805                             return false;
806                     break;
807                 */
808             }
809         }
810         return true;
811     },
812
813     record_equals: function(key_fields)
814     {
815         return function(other) { 
816             return manifold._record_equals(this, other, key_fields); 
817         };
818     },
819
820     _in_array: function(element, array, key_fields)
821     {
822         if (key_fields.length > 1) {
823             for (var i = 0; i < array.length; i++) {
824                 if (manifold._record_equals(element, array[i], key_fields))
825                     return true;
826             }
827             return false;
828         } else {
829             // XXX TODO If we have a dict, extract the key first
830             return ($.inArray(element, array) != -1);
831         }
832     },
833
834     /************************************************************************** 
835      * Metadata management
836      **************************************************************************/ 
837
838      metadata: {
839
840         get_table: function(method) {
841             var table = MANIFOLD_METADATA[method];
842             return (typeof table === 'undefined') ? null : table;
843         },
844
845         get_columns: function(method) {
846             var table = this.get_table(method);
847             if (!table) {
848                 return null;
849             }
850
851             return (typeof table.column === 'undefined') ? null : table.column;
852         },
853
854         get_field_names: function(method)
855         {
856             var columns = this.get_columns(method);
857             if (!columns)
858                 return null;
859             return $.map(columns, function (x) { return x.name });
860         },
861
862         get_key: function(method) {
863             var table = this.get_table(method);
864             if (!table)
865                 return null;
866
867             return (typeof table.key === 'undefined') ? null : table.key;
868         },
869
870
871         get_column: function(method, name) {
872             var columns = this.get_columns(method);
873             if (!columns)
874                 return null;
875
876             $.each(columns, function(i, c) {
877                 if (c.name == name)
878                     return c
879             });
880             return null;
881         },
882
883         get_type: function(method, name) {
884             var table = this.get_table(method);
885             if (!table)
886                 return null;
887
888             var match = $.grep(table.column, function(x) { return x.name == name });
889             if (match.length == 0) {
890                 return undefined;
891             } else {
892                 return match[0].type;
893             }
894             return (typeof table.type === 'undefined') ? null : table.type;
895         }
896
897      },
898
899     /************************************************************************** 
900      * Query management
901      **************************************************************************/ 
902
903     query_store: new QueryStore(),
904
905     // XXX Remaining functions are deprecated since they are replaced by the query store
906
907     /*!
908      * Associative array storing the set of queries active on the page
909      * \memberof Manifold
910      */
911     all_queries: {},
912
913     /*!
914      * Insert a query in the global hash table associating uuids to queries.
915      * If the query has no been analyzed yet, let's do it.
916      * \fn insert_query(query)
917      * \memberof Manifold
918      * \param ManifoldQuery query Query to be added
919      */
920     insert_query : function (query) { 
921         // NEW API
922         manifold.query_store.insert(query);
923
924         // Run
925         $(document).ready(function() {
926         manifold.run_query(query);
927         });
928
929         // FORMER API
930         if (query.analyzed_query == null) {
931             query.analyze_subqueries();
932         }
933         manifold.all_queries[query.query_uuid]=query;
934     },
935
936     /*!
937      * Returns the query associated to a UUID
938      * \fn find_query(query_uuid)
939      * \memberof Manifold
940      * \param string query_uuid The UUID of the query to be returned
941      */
942     find_query : function (query_uuid) { 
943         return manifold.all_queries[query_uuid];
944     },
945
946     /************************************************************************** 
947      * Query execution
948      **************************************************************************/ 
949
950     // trigger a query asynchroneously
951     proxy_url : '/manifold/proxy/json/',
952
953     // reasonably low-noise, shows manifold requests coming in and out
954     asynchroneous_debug : true,
955     // print our more details on result publication and related callbacks
956     pubsub_debug : false,
957
958     /**
959      * \brief We use js function closure to be able to pass the query (array)
960      * to the callback function used when data is received
961      */
962     success_closure: function(query, publish_uuid, callback) {
963         return function(data, textStatus) {
964             manifold.asynchroneous_success(data, query, publish_uuid, callback);
965         }
966     },
967
968     run_query: function(query, callback)
969         {
970         // default value for callback = null
971         if (typeof callback === 'undefined')
972             callback = null; 
973
974         var query_ext = manifold.query_store.find_query_ext(query.query_uuid);
975         query_ext.query_state = QUERY_STATE_INPROGRESS;
976
977         /*
978             If the Query object concerns SFA AM objects, iterate on each AM platform
979             Loop per platform, allows a progressive loading per AM platform
980             Update is run on all platforms at the same time to get a final answer, we don't manage partial answers yet...
981         */
982         if((query.object == 'resource' || query.object == 'lease' || query.object == 'slice') && query.action != "update"){
983             var obj = query.object;
984             $.post("/rest/platform/", function( data ) {
985                 $.each(data, function(index, p) {
986                     query.object = p.platform+":"+obj;
987                     var query_json = JSON.stringify(query);
988
989                     // Inform plugins about the progress
990                     query.iter_subqueries(function (sq) {
991                         var sq_query_ext = manifold.query_store.find_analyzed_query_ext(sq.query_uuid);
992                         sq_query_ext.query_state = QUERY_STATE_INPROGRESS;
993                         manifold.raise_record_event(sq.query_uuid, IN_PROGRESS);
994                     });
995
996                     $.post(manifold.proxy_url, {'json': query_json} , manifold.success_closure(query, null, callback));
997
998                 });
999             });
1000
1001         }else{
1002             var query_json = JSON.stringify(query);
1003
1004             // Inform plugins about the progress
1005             query.iter_subqueries(function (sq) {
1006                 var sq_query_ext = manifold.query_store.find_analyzed_query_ext(sq.query_uuid);
1007                 sq_query_ext.query_state = QUERY_STATE_INPROGRESS;
1008                 manifold.raise_record_event(sq.query_uuid, IN_PROGRESS);
1009             });
1010
1011             $.post(manifold.proxy_url, {'json': query_json} , manifold.success_closure(query, null, callback));
1012
1013         }
1014     },
1015
1016     // XXX DEPRECATED
1017     // Executes all async. queries - intended for the javascript header to initialize queries
1018     // input queries are specified as a list of {'query_uuid': <query_uuid> }
1019     // each plugin is responsible for managing its spinner through on_query_in_progress
1020     asynchroneous_exec : function (query_exec_tuples) {
1021         
1022         // Loop through input array, and use publish_uuid to publish back results
1023         $.each(query_exec_tuples, function(index, tuple) {
1024             var query=manifold.find_query(tuple.query_uuid);
1025             var query_json=JSON.stringify (query);
1026             var publish_uuid=tuple.publish_uuid;
1027             // by default we publish using the same uuid of course
1028             if (publish_uuid==undefined) publish_uuid=query.query_uuid;
1029             if (manifold.pubsub_debug) {
1030                 messages.debug("sending POST on " + manifold.proxy_url + query.__repr());
1031             }
1032
1033             query.iter_subqueries(function (sq) {
1034                 manifold.raise_record_event(sq.query_uuid, IN_PROGRESS);
1035             });
1036
1037             // not quite sure what happens if we send a string directly, as POST data is named..
1038             // this gets reconstructed on the proxy side with ManifoldQuery.fill_from_POST
1039             $.post(manifold.proxy_url, {'json':query_json}, 
1040                    manifold.success_closure(query, publish_uuid, tuple.callback));
1041         })
1042     },
1043
1044     /**
1045      * \brief Forward a query to the manifold backend
1046      * \param query (dict) the query to be executed asynchronously
1047      * \param callback (function) the function to be called when the query terminates
1048      */
1049     forward: function(query, callback) {
1050         var query_json = JSON.stringify(query);
1051         $.post(manifold.proxy_url, {'json': query_json} , 
1052                manifold.success_closure(query, query.query_uuid, callback));
1053     },
1054
1055     /*!
1056      * Returns whether a query expects a unique results.
1057      * This is the case when the filters contain a key of the object
1058      * \fn query_expects_unique_result(query)
1059      * \memberof Manifold
1060      * \param ManifoldQuery query Query for which we are testing whether it expects a unique result
1061      */
1062     query_expects_unique_result: function(query) {
1063         /* XXX we need functions to query metadata */
1064         //var keys = MANIFOLD_METADATA[query.object]['keys']; /* array of array of field names */
1065         /* TODO requires keys in metadata */
1066         return true;
1067     },
1068
1069     /*!
1070      * Publish result
1071      * \fn publish_result(query, results)
1072      * \memberof Manifold
1073      * \param ManifoldQuery query Query which has received results
1074      * \param array results results corresponding to query
1075      */
1076     publish_result: function(query, result) {
1077         if (typeof result === 'undefined')
1078             result = [];
1079
1080         // NEW PLUGIN API
1081         manifold.raise_record_event(query.query_uuid, CLEAR_RECORDS);
1082         if (manifold.pubsub_debug)
1083             messages.debug(".. publish_result (1) ");
1084         var count=0;
1085         $.each(result, function(i, record) {
1086             manifold.raise_record_event(query.query_uuid, NEW_RECORD, record);
1087             count += 1;
1088         });
1089         if (manifold.pubsub_debug) 
1090             messages.debug(".. publish_result (2) has used NEW API on " + count + " records");
1091         manifold.raise_record_event(query.query_uuid, DONE);
1092         if (manifold.pubsub_debug) 
1093             messages.debug(".. publish_result (3) has used NEW API to say DONE");
1094
1095         // OLD PLUGIN API BELOW
1096         /* Publish an update announce */
1097         var channel="/results/" + query.query_uuid + "/changed";
1098         if (manifold.pubsub_debug) 
1099             messages.debug(".. publish_result (4) OLD API on channel" + channel);
1100
1101         $.publish(channel, [result, query]);
1102
1103         if (manifold.pubsub_debug) 
1104             messages.debug(".. publish_result (5) END q=" + query.__repr());
1105     },
1106
1107     store_records: function(query, records) {
1108         // Store records
1109         var query_ext = manifold.query_store.find_analyzed_query_ext(query.query_uuid);
1110         if (query_ext.set_query_ext) {
1111             // We have a domain query
1112             // The results are stored in the corresponding set_query
1113             manifold.query_store.set_records(query_ext.set_query_ext.query.query_uuid, records);
1114             
1115         } else if (query_ext.domain_query_ext) {
1116             // We have a set query, it is only used to determine which objects are in the set, we should only retrieve the key
1117             // Has it a domain query, and has it completed ?
1118             $.each(records, function(i, record) {
1119                 var key = manifold.metadata.get_key(query.object);
1120                 if ( typeof record === "string" ){
1121                     var record_key = record;
1122                 }else{
1123                     var record_key = manifold.record_get_value(record, key);
1124                 }
1125                 manifold.query_store.set_record_state(query.query_uuid, record_key, STATE_SET, STATE_SET_IN);
1126             });
1127
1128         } else {
1129             // We have a normal query
1130             manifold.query_store.set_records(query.query_uuid, records, STATE_SET_IN);
1131         }
1132     },
1133
1134     /*!
1135      * Recursively publish result
1136      * \fn publish_result_rec(query, result)
1137      * \memberof Manifold
1138      * \param ManifoldQuery query Query which has received result
1139      * \param array result result corresponding to query
1140      *
1141      * Note: this function works on the analyzed query
1142      */
1143     publish_result_rec: function(query, records) {
1144         /* If the result is not unique, only publish the top query;
1145          * otherwise, publish the main object as well as subqueries
1146          * XXX how much recursive are we ?
1147          */
1148         if (manifold.pubsub_debug)
1149              messages.debug (">>>>> publish_result_rec " + query.object);
1150         if (manifold.query_expects_unique_result(query)) {
1151             /* Also publish subqueries */
1152             $.each(query.subqueries, function(object, subquery) {
1153                 manifold.publish_result_rec(subquery, records[0][object]);
1154                 /* TODO remove object from result */
1155             });
1156         }
1157         if (manifold.pubsub_debug) 
1158             messages.debug ("===== publish_result_rec " + query.object);
1159
1160         var query_ext = manifold.query_store.find_analyzed_query_ext(query.query_uuid);
1161         query_ext.query_state = QUERY_STATE_DONE;
1162
1163         this.store_records(query, records);
1164
1165         var pub_query;
1166
1167         if (query_ext.set_query_ext) {
1168             if (query_ext.set_query_ext.query_state != QUERY_STATE_DONE)
1169                 return;
1170             pub_query = query_ext.set_query_ext.query;
1171         } else if (query_ext.domain_query_ext) {
1172             if (query_ext.domain_query_ext.query_state != QUERY_STATE_DONE)
1173                 return;
1174             pub_query = query;
1175         } else {
1176             pub_query = query;
1177         }
1178         // We can only publish results if the query (and its related domain query) is complete
1179         manifold.publish_result(pub_query, records);
1180
1181         if (manifold.pubsub_debug) 
1182             messages.debug ("<<<<< publish_result_rec " + query.object);
1183     },
1184
1185     setup_update_query: function(query, records) 
1186     {
1187         // We don't prepare an update query if the result has more than 1 entry
1188         if (records.length != 1)
1189             return;
1190         var query_ext = manifold.query_store.find_query_ext(query.query_uuid);
1191
1192         var record = records[0];
1193
1194         var update_query_ext = query_ext.update_query_ext;
1195
1196         if (!update_query_ext)
1197             return;
1198
1199         var update_query = update_query_ext.query;
1200         var update_query_ext = query_ext.update_query_ext;
1201         var update_query_orig = query_ext.update_query_orig_ext.query;
1202
1203         // Testing whether the result has subqueries (one level deep only)
1204         // iif the query has subqueries
1205         var count = 0;
1206         var obj = query.analyzed_query.subqueries;
1207         for (method in obj) {
1208             if (obj.hasOwnProperty(method)) {
1209                 var key = manifold.metadata.get_key(method);
1210                 if (!key)
1211                     continue;
1212                 var sq_keys = [];
1213                 var subrecords = record[method];
1214                 if (!subrecords)
1215                     continue
1216                 $.each(subrecords, function (i, subrecord) {
1217                     sq_keys.push(manifold.record_get_value(subrecord, key));
1218                 });
1219                 update_query.params[method] = sq_keys;
1220                 update_query_orig.params[method] = sq_keys.slice();
1221                 count++;
1222             }
1223         }
1224
1225         if (count > 0) {
1226             update_query_ext.disabled = false;
1227             update_query_orig_ext.disabled = false;
1228         }
1229     },
1230
1231     process_get_query_records: function(query, records) {
1232         this.setup_update_query(query, records);
1233         
1234         var query_ext = manifold.query_store.find_query_ext(query.query_uuid);
1235         query_ext.query_state = QUERY_STATE_DONE;
1236
1237         /* Publish full results */
1238         var tmp_query = manifold.query_store.find_analyzed_query(query.query_uuid);
1239         manifold.publish_result_rec(tmp_query, records);
1240     },
1241
1242     make_records: function(object, records)
1243     {
1244         $.each(records, function(i, record) {
1245             manifold.make_record(object, record);
1246         });
1247     },
1248
1249     make_record: function(object, record)
1250     {
1251         // To make an object a record, we just add the hash function
1252         var key, new_object;
1253
1254         if (object.indexOf(':') != -1) {
1255             new_object = object.split(':')[1];
1256         } else {
1257             new_object = object;
1258         }
1259
1260         key = manifold.metadata.get_key(new_object);
1261         if (!key){
1262             console.log("object type: " + new_object + " has no key");
1263             console.log(record);
1264             return;
1265         }
1266         record.hashCode = manifold.record_hashcode(key.sort());
1267         record.equals   = manifold.record_equals(key);
1268
1269         // Looking after subrecords
1270         for (var field in record) {
1271             var result_value = record[field];
1272
1273             switch (this.get_type(result_value)) {
1274                 case TYPE_RECORD:
1275                     var subobject = manifold.metadata.get_type(object, field);
1276                     // if (subobject) XXX Bugs with fields declared string while they are not : network.version is a dict in fact
1277                     if (subobject && subobject != 'string')
1278                         manifold.make_record(subobject, result_value);
1279                     break;
1280                 case TYPE_LIST_OF_RECORDS:
1281                     var subobject = manifold.metadata.get_type(object, field);
1282                     if (subobject)
1283                         manifold.make_records(subobject, result_value);
1284                     break;
1285             }
1286         }
1287     },
1288
1289     /**
1290      * 
1291      * What we need to do when receiving results from an update query:
1292      * - differences between what we had, what we requested, and what we obtained
1293      *    . what we had : update_query_orig (simple fields and set fields managed differently)
1294      *    . what we requested : update_query
1295      *    . what we received : records
1296      * - raise appropriate events
1297      *
1298      * The normal process is that results similar to Get will be pushed in the
1299      * pubsub mechanism, thus repopulating everything while we only need
1300      * diff's. This means we need to move the publish functionalities in the
1301      * previous 'process_get_query_records' function.
1302      */
1303     process_update_query_records: function(query, records) {
1304         // First issue: we request everything, and not only what we modify, so will will have to ignore some fields
1305         var query_uuid        = query.query_uuid;
1306         var query_ext         = manifold.query_store.find_analyzed_query_ext(query_uuid);
1307         var update_query      = query_ext.main_query_ext.update_query_ext.query;
1308         var update_query_orig = query_ext.main_query_ext.update_query_orig_ext.query;
1309         
1310         // Since we update objects one at a time, we can get the first record
1311         var record = records[0];
1312
1313         // Let's iterate over the object properties
1314         for (var field in record) {
1315             var result_value = record[field];
1316             switch (this.get_type(result_value)) {
1317                 case TYPE_VALUE:
1318                     // Did we ask for a change ?
1319                     var update_value = update_query[field];
1320                     if (!update_value)
1321                         // Not requested, if it has changed: OUT OF SYNC
1322                         // How we can know ?
1323                         // We assume it won't have changed
1324                         continue;
1325
1326                     if (!result_value)
1327                         throw "Internal error";
1328
1329                     data = {
1330                         state : STATE_SET,
1331                         key   : field,
1332                         op    : update_value,
1333                         value : (update_value == result_value) ? STATE_VALUE_CHANGE_SUCCESS : STATE_VALUE_CHANGE_FAILURE,
1334                     }
1335                     manifold.raise_record_event(query_uuid, FIELD_STATE_CHANGED, data);
1336
1337                     break;
1338                 case TYPE_RECORD:
1339                     throw "Not implemented";
1340                     break;
1341
1342                 /*
1343 case TYPE_LIST_OF_VALUES:
1344                     // Same as list of records, but we don't have to extract keys
1345                     
1346                     // The rest of exactly the same (XXX factorize)
1347                     var update_keys  = update_query_orig.params[field];
1348                     var query_keys   = update_query.params[field];
1349                     var added_keys   = $.grep(query_keys, function (x) { return $.inArray(x, update_keys) == -1 });
1350                     var removed_keys = $.grep(update_keys, function (x) { return $.inArray(x, query_keys) == -1 });
1351
1352
1353                     $.each(added_keys, function(i, key) {
1354                         if ($.inArray(key, result_value) == -1) {
1355                             data = {
1356                                 request: FIELD_REQUEST_ADD,
1357                                 key   : field,
1358                                 value : key,
1359                                 status: FIELD_REQUEST_FAILURE,
1360                             }
1361                         } else {
1362                             data = {
1363                                 request: FIELD_REQUEST_ADD,
1364                                 key   : field,
1365                                 value : key,
1366                                 status: FIELD_REQUEST_SUCCESS,
1367                             }
1368                         }
1369                         manifold.raise_record_event(query_uuid, FIELD_STATE_CHANGED, data);
1370                     });
1371                     $.each(removed_keys, function(i, key) {
1372                         if ($.inArray(key, result_keys) == -1) {
1373                             data = {
1374                                 request: FIELD_REQUEST_REMOVE,
1375                                 key   : field,
1376                                 value : key,
1377                                 status: FIELD_REQUEST_SUCCESS,
1378                             }
1379                         } else {
1380                             data = {
1381                                 request: FIELD_REQUEST_REMOVE,
1382                                 key   : field,
1383                                 value : key,
1384                                 status: FIELD_REQUEST_FAILURE,
1385                             }
1386                         }
1387                         manifold.raise_record_event(query_uuid, FIELD_STATE_CHANGED, data);
1388                     });
1389
1390
1391                     break;
1392                 */
1393                 case TYPE_LIST_OF_VALUES: // XXX Until fixed
1394                 case TYPE_LIST_OF_RECORDS:
1395                     var key, new_state, cur_query_uuid;
1396                     if($.inArray(field,Object.keys(query.analyzed_query.subqueries)) > -1){
1397                         cur_query_uuid = query.analyzed_query.subqueries[field].query_uuid;
1398                     }
1399
1400                     // example: slice.resource
1401                     //  - update_query_orig.params.resource = resources in slice before update
1402                     //  - update_query.params.resource = resource requested in slice
1403                     //  - keys from field = resources obtained
1404                 
1405                     if (field == 'lease') {
1406                          // lease_id has been added to be repeated when
1407                          // constructing request rspec. We don't want it for
1408                          // comparisons
1409                         key = ['start_time', 'end_time', 'resource'];
1410                     } else {
1411                         key = manifold.metadata.get_key(field);
1412                     }
1413                     if (!key)
1414                         continue;
1415                     /*
1416                     if (key.length > 1) {
1417                         throw "Not implemented";
1418                         continue;
1419                     }
1420                     key = key[0];
1421                     */
1422
1423                     /* XXX should be modified for multiple keys */
1424                     var result_keys  = $.map(record[field], function(x) { return manifold.record_get_value(x, key); });
1425
1426                     // XXX All this could be deduced from record state : STATE_IN_PENDING and STATE_OUT_PENDING
1427                     // what we had at the begining
1428                     var update_keys  = update_query_orig.params[field];
1429                     // what we asked
1430                     var query_keys   = update_query.params[field];
1431                     // what we added and removed
1432                     var added_keys   = $.grep(query_keys,  function (x) { return (!(manifold._in_array(x, update_keys, key))); });
1433                     var removed_keys = $.grep(update_keys, function (x) { return (!(manifold._in_array(x, query_keys,  key))); });
1434
1435                     // Send events related to parent query
1436                     $.each(added_keys, function(i, added_key) {
1437                         new_state = (manifold._in_array(added_key, result_keys, key)) ? STATE_SET_IN_SUCCESS : STATE_SET_IN_FAILURE;
1438
1439                         // Update record state for children queries
1440                         manifold.query_store.set_record_state(cur_query_uuid, added_key, STATE_SET, new_state);
1441
1442                         // XXX This could be optimized
1443                         manifold.query_store.recount(cur_query_uuid); 
1444
1445                         data = { state: STATE_SET, key  : field, op   : new_state, value: added_key }
1446                         manifold.raise_record_event(query_uuid, FIELD_STATE_CHANGED, data);
1447
1448                         // Inform subquery also
1449                         data.key = '';
1450                         manifold.raise_record_event(cur_query_uuid, FIELD_STATE_CHANGED, data);
1451                         // XXX Passing no parameters so that they can redraw everything would
1452                         // be more efficient but is currently not supported
1453                         // XXX We could also need to inform plugins about nodes IN (not pending) that are no more, etc.
1454                         // XXX refactor all this when suppressing update_queries, and relying on state instead !
1455                     });
1456                     $.each(removed_keys, function(i, removed_key) {
1457                         new_state = (manifold._in_array(removed_key, result_keys, key)) ? STATE_SET_OUT_FAILURE : STATE_SET_OUT_SUCCESS;
1458
1459                         // Update record state for children queries
1460                         manifold.query_store.set_record_state(cur_query_uuid, removed_key, STATE_SET, new_state);
1461
1462                         // XXX This could be optimized
1463                         manifold.query_store.recount(cur_query_uuid); 
1464
1465                         data = { state: STATE_SET, key  : field, op   : new_state, value: removed_key }
1466                         manifold.raise_record_event(query_uuid, FIELD_STATE_CHANGED, data);
1467
1468                         // Inform subquery also
1469                         data.key = '';
1470                         manifold.raise_record_event(cur_query_uuid, FIELD_STATE_CHANGED, data);
1471                     });
1472
1473                     break;
1474             }
1475         }
1476         
1477         // XXX Now we need to adapt 'update' and 'update_orig' queries as if we had done a get
1478         this.setup_update_query(query, records);
1479
1480         var query_ext = manifold.query_store.find_query_ext(query.query_uuid);
1481         query_ext.query_state = QUERY_STATE_DONE;
1482
1483         var tmp_query = manifold.query_store.find_analyzed_query(query.query_uuid);
1484         manifold.publish_result_rec(tmp_query, records);
1485
1486         // Send DONE message to plugins
1487         query.iter_subqueries(function(sq, data, parent_query) {
1488             manifold.raise_record_event(sq.query_uuid, DONE);
1489         });
1490
1491     },
1492
1493     process_query_records: function(query, records) {
1494         if (query.action == 'get') {
1495             this.process_get_query_records(query, records);
1496         } else if (query.action == 'update') {
1497             this.process_update_query_records(query, records);
1498         }
1499     },
1500
1501     // if set callback is provided it is called
1502     // most of the time publish_uuid will be query.query_uuid
1503     // however in some cases we wish to publish the result under a different uuid
1504     // e.g. an updater wants to publish its result as if from the original (get) query
1505     asynchroneous_success : function (data, query, publish_uuid, callback) {
1506         // xxx should have a nicer declaration of that enum in sync with the python code somehow
1507         
1508         var start = new Date();
1509         if (manifold.asynchroneous_debug)
1510             messages.debug(">>>>>>>>>> asynchroneous_success query.object=" + query.object);
1511
1512         if (data.code == 2) { // ERROR
1513             // We need to make sense of error codes here
1514             alert("Your session has expired, please log in again");
1515             localStorage.removeItem('user');
1516             window.location="/logout/";
1517             if (manifold.asynchroneous_debug) {
1518                 duration=new Date()-start;
1519                 messages.debug ("<<<<<<<<<< asynchroneous_success " + query.object + " -- error returned - logging out " + duration + " ms");
1520             }
1521             return;
1522         }
1523         if (data.code == 1) { // WARNING
1524             messages.error("Some errors have been received from the manifold backend at " + MANIFOLD_URL + " [" + data.description + "]");
1525             // publish error code and text message on a separate channel for whoever is interested
1526             if (publish_uuid)
1527                 $.publish("/results/" + publish_uuid + "/failed", [data.code, data.description] );
1528
1529         }
1530
1531         // If a callback has been specified, we redirect results to it 
1532         if (!!callback) { 
1533             callback(data); 
1534             if (manifold.asynchroneous_debug) {
1535                 duration=new Date()-start;
1536                 messages.debug ("<<<<<<<<<< asynchroneous_success " + query.object + " -- callback ended " + duration + " ms");
1537             }
1538             return; 
1539         }
1540
1541         if (manifold.asynchroneous_debug) 
1542             messages.debug ("========== asynchroneous_success " + query.object + " -- before process_query_records [" + query.query_uuid +"]");
1543
1544         // once everything is checked we can use the 'value' part of the manifoldresult
1545         var result=data.value;
1546         if (result) {
1547             /* Eventually update the content of related queries (update, etc) */
1548             manifold.make_records(query.object, result);
1549             this.process_query_records(query, result);
1550
1551             /* Publish results: disabled here, done in the previous call */
1552             //tmp_query = manifold.find_query(query.query_uuid);
1553             //manifold.publish_result_rec(tmp_query.analyzed_query, result);
1554         }
1555         if (manifold.asynchroneous_debug) {
1556             duration=new Date()-start;
1557             messages.debug ("<<<<<<<<<< asynchroneous_success " + query.object + " -- done " + duration + " ms");
1558         }
1559
1560     },
1561
1562     /************************************************************************** 
1563      * Plugin API helpers
1564      **************************************************************************/ 
1565
1566     raise_event_handler: function(type, query_uuid, event_type, value) {
1567         if (manifold.pubsub_debug)
1568             messages.debug("raise_event_handler, quuid="+query_uuid+" type="+type+" event_type="+event_type);
1569         if ((type != 'query') && (type != 'record'))
1570             throw 'Incorrect type for manifold.raise_event()';
1571         // xxx we observe quite a lot of incoming calls with an undefined query_uuid
1572         // this should be fixed upstream in manifold I expect
1573         if (query_uuid === undefined) {
1574             messages.warning("undefined query in raise_event_handler");
1575             return;
1576         }
1577
1578         // notify the change to objects that either listen to this channel specifically,
1579         // or to the wildcard channel
1580         var channels = [ manifold.get_channel(type, query_uuid), manifold.get_channel(type, '*') ];
1581
1582         $.each(channels, function(i, channel) {
1583             if (value === undefined) {
1584                 if (manifold.pubsub_debug) messages.debug("triggering [no value] on channel="+channel+" and event_type="+event_type);
1585                 $('.pubsub').trigger(channel, [event_type]);
1586             } else {
1587                 if (manifold.pubsub_debug) messages.debug("triggering [value="+value+"] on channel="+channel+" and event_type="+event_type);
1588                 $('.pubsub').trigger(channel, [event_type, value]);
1589             }
1590         });
1591     },
1592
1593     raise_query_event: function(query_uuid, event_type, value) {
1594         manifold.raise_event_handler('query', query_uuid, event_type, value);
1595     },
1596
1597     raise_record_event: function(query_uuid, event_type, value) {
1598         manifold.raise_event_handler('record', query_uuid, event_type, value);
1599     },
1600
1601     /**
1602      * Event handler helpers
1603      */
1604     _get_next_state_add: function(prev_state)
1605     {
1606         switch (prev_state) {
1607             case STATE_SET_OUT:
1608             case STATE_SET_OUT_SUCCESS:
1609             case STATE_SET_IN_FAILURE:
1610                 new_state = STATE_SET_IN_PENDING;
1611                 break;
1612
1613             case STATE_SET_OUT_PENDING:
1614                 new_state = STATE_SET_IN;
1615                 break;
1616
1617             case STATE_SET_IN:
1618             case STATE_SET_IN_PENDING:
1619             case STATE_SET_IN_SUCCESS:
1620             case STATE_SET_OUT_FAILURE:
1621                 console.log("Inconsistent state: already in");
1622                 return;
1623         }
1624         return new_state;
1625     },
1626
1627     _get_next_state_remove: function(prev_state)
1628     {
1629         switch (prev_state) {
1630             case STATE_SET_IN:
1631             case STATE_SET_IN_SUCCESS:
1632             case STATE_SET_OUT_FAILURE:
1633                 new_state = STATE_SET_OUT_PENDING;
1634                 break;
1635
1636             case STATE_SET_IN_PENDING:
1637                 new_state = STATE_SET_OUT;
1638                 break;  
1639
1640             case STATE_SET_OUT:
1641             case STATE_SET_OUT_PENDING:
1642             case STATE_SET_OUT_SUCCESS:
1643             case STATE_SET_IN_FAILURE:
1644                 console.log("Inconsistent state: already out");
1645                 return;
1646         }
1647         return new_state;
1648     },
1649
1650     _grep_active_lease_callback: function(lease_query, resource_key) {
1651         return function(lease_key_lease) {
1652             var state, lease_key, lease;
1653
1654             lease_key = lease_key_lease[0];
1655             lease = lease_key_lease[1];
1656
1657             if (lease['resource'] != resource_key)
1658                 return false;
1659
1660             state = manifold.query_store.get_record_state(lease_query.query_uuid, lease_key, STATE_SET);;
1661             switch(state) {
1662                 case STATE_SET_IN:
1663                 case STATE_SET_IN_PENDING:
1664                 case STATE_SET_IN_SUCCESS:
1665                 case STATE_SET_OUT_FAILURE:
1666                     return true;
1667                 case STATE_SET_OUT:
1668                 case STATE_SET_OUT_PENDING:
1669                 case STATE_SET_OUT_SUCCESS:
1670                 case STATE_SET_IN_FAILURE:
1671                     return false;
1672             }
1673         }
1674     },
1675
1676     _enforce_constraints: function(query_ext, record, record_key, event_type)
1677     {
1678         var query, data;
1679
1680         query = query_ext.query;
1681
1682         switch(query.object) {
1683
1684             case 'resource':
1685                 // CONSTRAINT_RESERVABLE_LEASE
1686                 // 
1687                 // +) If a reservable node is added to the slice, then it should have a corresponding lease
1688                 // XXX Not always a resource
1689                 var is_reservable = (record.exclusive == true);
1690                 if (is_reservable) {
1691                     var warnings = manifold.query_store.get_record_state(query.query_uuid, record_key, STATE_WARNINGS);
1692
1693                     if (event_type == STATE_SET_ADD) {
1694                         // We should have a lease_query associated
1695                         var lease_query = query_ext.parent_query_ext.query.subqueries['lease']; // in  options
1696                         var lease_query_ext = manifold.query_store.find_analyzed_query_ext(lease_query.query_uuid);
1697                         // Do we have lease records (in) with this resource
1698                         var lease_records = $.grep(lease_query_ext.records.entries(), this._grep_active_lease_callback(lease_query, record_key));
1699                         if (lease_records.length == 0) {
1700                             // Sets a warning
1701                             // XXX Need for a better function to manage warnings
1702                             var warn = CONSTRAINT_RESERVABLE_LEASE_MSG;
1703                             warnings[CONSTRAINT_RESERVABLE_LEASE] = warn;
1704                         } else {
1705                             // Lease are defined, delete the warning in case it was set previously
1706                             delete warnings[CONSTRAINT_RESERVABLE_LEASE];
1707                         }
1708                     } else {
1709                         // Remove warnings attached to this resource
1710                         delete warnings[CONSTRAINT_RESERVABLE_LEASE];
1711                     }
1712
1713                     manifold.query_store.set_record_state(query.query_uuid, record_key, STATE_WARNINGS, warnings);
1714                 }
1715
1716                 /* This was redundant */
1717                 // manifold.query_store.recount(query.query_uuid); 
1718
1719                 // Signal the change to plugins (even if the constraint does not apply, so that the plugin can display a checkmark)
1720                 data = {
1721                     state:  STATE_WARNINGS,
1722                     key   : record_key,
1723                     op    : null,
1724                     value : warnings
1725                 }
1726                 manifold.raise_record_event(query.query_uuid, FIELD_STATE_CHANGED, data);
1727                 break;
1728
1729             case 'lease':
1730                 var resource_key = record_key.resource;
1731                 var resource_query = query_ext.parent_query_ext.query.subqueries['resource'];
1732                 var warnings = manifold.query_store.get_record_state(resource_query.query_uuid, resource_key, STATE_WARNINGS);
1733
1734                 if (event_type == STATE_SET_ADD) {
1735                      // A lease is added, it removes the constraint
1736                     delete warnings[CONSTRAINT_RESERVABLE_LEASE];
1737                 } else {
1738                     // A lease is removed, it might trigger the warning
1739                     var lease_records = $.grep(query_ext.records.entries(), this._grep_active_lease_callback(query, resource_key));
1740                     if (lease_records.length == 0) { // XXX redundant cases
1741                         // Sets a warning
1742                         // XXX Need for a better function to manage warnings
1743                         var warn = CONSTRAINT_RESERVABLE_LEASE_MSG;
1744                         warnings[CONSTRAINT_RESERVABLE_LEASE] = warn;
1745                     } else {
1746                         // Lease are defined, delete the warning in case it was set previously
1747                         delete warnings[CONSTRAINT_RESERVABLE_LEASE];
1748                     }
1749                     
1750                 }
1751
1752                 manifold.query_store.recount(resource_query.query_uuid); 
1753
1754                 // Signal the change to plugins (even if the constraint does not apply, so that the plugin can display a checkmark)
1755                 data = {
1756                     state:  STATE_WARNINGS,
1757                     key   : resource_key,
1758                     op    : null,
1759                     value : warnings
1760                 }
1761                 manifold.raise_record_event(resource_query.query_uuid, FIELD_STATE_CHANGED, data);
1762                 break;
1763         }
1764
1765         // -) When a lease is added, it might remove the warning associated to a reservable node
1766
1767         // If a NITOS node is reserved, then at least a NITOS channel should be reserved
1768         // - When a NITOS channel is added, it might remove a warning associated to all NITOS nodes
1769
1770         // If a NITOS channel is reserved, then at least a NITOS node should be reserved
1771         // - When a NITOS node is added, it might remove a warning associated to all NITOS channels
1772
1773         // A lease is present while the resource has been removed => Require warnings on nodes not in set !
1774
1775     },
1776
1777     _get_query_path: function(query_ext) {
1778         var path = "";
1779         var sq = query_ext;
1780         while (sq.parent_query_ext) {
1781             if (path != "")
1782                 path = '.' + path;
1783             path = sq.query.object + path;
1784             sq = sq.parent_query_ext;
1785         }
1786         return path;
1787     },
1788
1789
1790     /**
1791      * Handling events raised by plugins
1792      */
1793     raise_event: function(query_uuid, event_type, data) 
1794     {
1795         var query, query_ext;
1796
1797         // Query uuid has been updated with the key of a new element
1798         query_ext    = manifold.query_store.find_analyzed_query_ext(query_uuid);
1799         query = query_ext.query;
1800
1801         switch(event_type) {
1802
1803             // XXX At some point, should be renamed to RECORD_STATE_CHANGED
1804             case FIELD_STATE_CHANGED:
1805
1806                 // value is an object (request, key, value, status)
1807                 // update is only possible is the query is not pending, etc
1808                 // SET_ADD is on a subquery, FIELD_STATE_CHANGED on the query itself
1809                 // we should map SET_ADD on this...
1810
1811                 // 1. Update internal query store about the change in status
1812
1813                 // 2. Update the update query
1814                 update_query      = query_ext.main_query_ext.update_query_ext.query;
1815                 update_query_orig = query_ext.main_query_ext.update_query_orig_ext.query;
1816
1817                 switch(data.state) {
1818             
1819                     case STATE_VALUE:
1820                         switch(data.op) {
1821                             case STATE_CHANGE:
1822                                 /* Set parameter data.key in the update_query to VALUE */
1823                                 if (update_query.params[data.key] === undefined)
1824                                     update_query.params[data.key] = Array();
1825                                 update_query.params[data.key] = value.value;
1826                                 break;
1827
1828                         }
1829                         break;
1830
1831                     case STATE_SET:
1832                         var prev_state, new_state;
1833                         var main_query, record, new_data, path;
1834         
1835                         // We only track state in the analyzed query
1836                         prev_state = manifold.query_store.get_record_state(query_uuid, data.value, STATE_SET);
1837                         if (prev_state === null)
1838                             prev_state = STATE_SET_OUT;
1839
1840                         switch(data.op) {
1841                             case STATE_SET_ADD:
1842                                 new_state = this._get_next_state_add(prev_state);
1843
1844                                 /* data.value containts the resource key */
1845                                 manifold.query_store.add_record(query_uuid, data.value, new_state);
1846                                 record = manifold.query_store.get_record(query_uuid, data.value);
1847                                 this._enforce_constraints(query_ext, record, data.value, STATE_SET_ADD);
1848                 
1849                                 /* Process update query in parent */
1850                                 path =  this._get_query_path(query_ext);
1851                                 if (update_query.params[path] === undefined)
1852                                     update_query.params[path] = Array();
1853                                 update_query.params[path].push(data.value);
1854
1855                                 break;
1856
1857                             case STATE_SET_REMOVE:
1858                                 new_state = this._get_next_state_remove(prev_state);
1859
1860                                 /* data.value contains the resource key */
1861                                 manifold.query_store.remove_record(query_uuid, data.value, new_state);
1862                                 record = manifold.query_store.get_record(query_uuid, data.value);
1863                                 this._enforce_constraints(query_ext, record, data.value, STATE_SET_REMOVE);
1864                     
1865                                 /* Process update query in parent */
1866                                 path =  this._get_query_path(query_ext);
1867                                 arr = update_query.params[path];
1868                                 
1869                                 var key = manifold.metadata.get_key(query.object);
1870                                 arr = $.grep(arr, function(x) { return (!(manifold._record_equals(x, data.value, key))); });
1871                                 if (update_query.params[path] === undefined)
1872                                     update_query.params[path] = Array();
1873                                 update_query.params[path] = arr;
1874                                 break;
1875                         }
1876
1877                         /* Inform the parent query: important for update */
1878                         new_data = {
1879                             state : STATE_SET,
1880                             key   : path,
1881                             op    : new_state,
1882                             value : data.value,
1883                         };
1884                         main_query = query_ext.main_query_ext.query;
1885                         manifold.raise_record_event(main_query.query_uuid, event_type, new_data);
1886                         /* Propagate the event to other plugins subscribed to the query */
1887                         manifold.query_store.recount(query_uuid);
1888                         new_data.key = ''
1889                         manifold.raise_record_event(query_uuid, event_type, new_data);
1890
1891                         break;
1892                 }
1893 /*
1894                 // 3. Inform others about the change
1895                 // a) the main query...
1896                 manifold.raise_record_event(query_uuid, event_type, data);
1897
1898                 // b) subqueries eventually (dot in the key)
1899                 // Let's unfold 
1900
1901                 var cur_query = query;
1902                 if (cur_query.analyzed_query)
1903                     cur_query = cur_query.analyzed_query;
1904
1905                 if (data.key) {
1906                     var path_array = data.key.split('.');
1907                     var value_key = data.key.split('.');
1908                     $.each(path_array, function(i, method) {
1909                         cur_query = cur_query.subqueries[method];
1910                         value_key.shift(); // XXX check that method is indeed shifted
1911                     });
1912                     data.key = value_key;
1913                 }
1914                 manifold.raise_record_event(cur_query.query_uuid, event_type, data);
1915 */
1916
1917                 break;
1918
1919             case RUN_UPDATE:
1920                 query_ext.main_query_ext.update_query_ext.query.fields = [];
1921                 manifold.run_query(query_ext.main_query_ext.update_query_ext.query);
1922                 break;
1923
1924             /* QUERY STATE CHANGED */
1925             
1926             // FILTERS
1927
1928             case FILTER_ADDED: 
1929                 console.log("FILTER ADDED", data);
1930                 /* Update internal record state */
1931                 manifold.query_store.add_filter(query_uuid, data);
1932
1933                 /* Propagate the message to plugins */
1934                 manifold.raise_query_event(query_uuid, event_type, data);
1935
1936                 break;
1937
1938             case FILTER_REMOVED:
1939                 console.log("FILTER REMOVED", data);
1940                 /* Update internal record state */
1941                 manifold.query_store.remove_filter(query_uuid, data);
1942
1943                 /* Propagate the message to plugins */
1944                 manifold.raise_query_event(query_uuid, event_type, data);
1945
1946                 break;
1947
1948             case FIELD_ADDED:
1949                 main_query = query_ext.main_query_ext.query;
1950                 main_update_query = query_ext.main_query_ext.update_query;
1951                 query.select(data);
1952
1953                 // Here we need the full path through all subqueries
1954                 path = ""
1955                 // XXX We might need the query name in the QueryExt structure
1956                 main_query.select(data);
1957
1958                 // XXX When is an update query associated ?
1959                 // XXX main_update_query.select(value);
1960
1961                 manifold.raise_query_event(query_uuid, event_type, data);
1962                 break;
1963
1964             case FIELD_REMOVED:
1965                 query = query_ext.query;
1966                 main_query = query_ext.main_query_ext.query;
1967                 main_update_query = query_ext.main_query_ext.update_query;
1968                 query.unselect(data);
1969                 main_query.unselect(data);
1970
1971                 // We need to inform about changes in these queries to the respective plugins
1972                 // Note: query & main_query have the same UUID
1973                 manifold.raise_query_event(query_uuid, event_type, data);
1974                 break;
1975         }
1976         // We need to inform about changes in these queries to the respective plugins
1977         // Note: query, main_query & update_query have the same UUID
1978
1979         // http://trac.myslice.info/ticket/32
1980         // Avoid multiple calls to the same event
1981         //manifold.raise_query_event(query_uuid, event_type, value);
1982
1983         // We are targeting the same object with get and update
1984         // The notion of query is bad, we should have a notion of destination, and issue queries on the destination
1985         // NOTE: Editing a subquery == editing a local view on the destination
1986
1987         // XXX We might need to run the new query again and manage the plugins in the meantime with spinners...
1988         // For the time being, we will collect all columns during the first query
1989     },
1990
1991     /* Publish/subscribe channels for internal use */
1992     get_channel: function(type, query_uuid) {
1993         if ((type !== 'query') && (type != 'record'))
1994             return null;
1995         return '/' + type + '/' + query_uuid;
1996     },
1997
1998 }; // manifold object
1999 /* ------------------------------------------------------------ */
2000
2001 (function($) {
2002
2003     // OLD PLUGIN API: extend jQuery/$ with pubsub capabilities
2004     // https://gist.github.com/661855
2005     var o = $({});
2006     $.subscribe = function( channel, selector, data, fn) {
2007       /* borrowed from jQuery */
2008       if ( data == null && fn == null ) {
2009           // ( channel, fn )
2010           fn = selector;
2011           data = selector = undefined;
2012       } else if ( fn == null ) {
2013           if ( typeof selector === "string" ) {
2014               // ( channel, selector, fn )
2015               fn = data;
2016               data = undefined;
2017           } else {
2018               // ( channel, data, fn )
2019               fn = data;
2020               data = selector;
2021               selector = undefined;
2022           }
2023       }
2024       /* </ugly> */
2025   
2026       /* We use an indirection function that will clone the object passed in
2027        * parameter to the subscribe callback 
2028        * 
2029        * FIXME currently we only clone query objects which are the only ones
2030        * supported and editable, we might have the same issue with results but
2031        * the page load time will be severely affected...
2032        */
2033       o.on.apply(o, [channel, selector, data, function() { 
2034           for(i = 1; i < arguments.length; i++) {
2035               if ( arguments[i].constructor.name == 'ManifoldQuery' )
2036                   arguments[i] = arguments[i].clone();
2037           }
2038           fn.apply(o, arguments);
2039       }]);
2040     };
2041   
2042     $.unsubscribe = function() {
2043       o.off.apply(o, arguments);
2044     };
2045   
2046     $.publish = function() {
2047       o.trigger.apply(o, arguments);
2048     };
2049   
2050 }(jQuery));
2051
2052 /* ------------------------------------------------------------ */
2053
2054 //http://stackoverflow.com/questions/5100539/django-csrf-check-failing-with-an-ajax-post-request
2055 //make sure to expose csrf in our outcoming ajax/post requests
2056 $.ajaxSetup({ 
2057      beforeSend: function(xhr, settings) {
2058          function getCookie(name) {
2059              var cookieValue = null;
2060              if (document.cookie && document.cookie != '') {
2061                  var cookies = document.cookie.split(';');
2062                  for (var i = 0; i < cookies.length; i++) {
2063                      var cookie = jQuery.trim(cookies[i]);
2064                      // Does this cookie string begin with the name we want?
2065                  if (cookie.substring(0, name.length + 1) == (name + '=')) {
2066                      cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
2067                      break;
2068                  }
2069              }
2070          }
2071          return cookieValue;
2072          }
2073          if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
2074              // Only send the token to relative URLs i.e. locally.
2075              xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
2076          }
2077      } 
2078 });