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