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