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