updater's query now has its own uuid
[myslice.git] / plugins / updater / updater.js
1 /**
2  * Description: associate with a Get query, maintains the 'Update' query that records pending changes
3  * Copyright (c) 2012 UPMC Sorbonne Universite - INRIA
4  * License: GPLv3
5  */
6
7 // xxx this is ongoing work, very rough, and not working at all yet 
8
9 ( function ( $ ) {
10     
11     $.fn.Updater = function ( method ) {
12         /* Method calling logic */
13         if ( methods[method] ) {
14             return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
15         } else if ( typeof method === 'object' || ! method ) {
16             return methods.init.apply( this, arguments );
17         } else {
18             $.error( 'Method ' +  method + ' does not exist on $.Updater' );
19         }
20     };
21     
22     var methods = {
23         init : function( options ) {
24             return this.each(function(){
25                 var $this = $(this);
26                 var updater = new Updater (options);
27                 $this.data('Updater',updater);
28                 /* Subscribe to query updates */
29                 var results_channel = '/results/' + options.query_uuid + '/changed';
30                 $.subscribe(results_channel, function (e,rows) { updater.update_slice (e,rows); } );
31             });
32         },
33         destroy : function( ) {
34             return this.each(function(){
35                 var $this = $(this);
36                 $(window).unbind('Updater');
37                 data.Updater.remove();
38                 $this.removeData('Updater');
39             });
40         },
41
42         show : function( content ) { }
43     };
44     
45     function Updater (options) {
46         this.options=options;
47         // xxx should try to locate update_query first, in case we have several Updaters
48         // on the same query
49         // however the mental model behind the global manifold object for now is 
50         // to unambiguously find a query based on its query_uuid, which in the joomla
51         // implementation wouldn't fly
52         // we keep this for a later improvement
53         var query=manifold.find_query (options.query_uuid);
54         messages.info("retrieved query " + query.__repr());
55         // very rough way of filling this for now
56         this.update_query = 
57             new ManifoldQuery ("update", query.subject, null, query.filters, 
58                                {}, // params
59                                query.fields, 
60                                undefined, /* unique */ 
61                                Math.uuid(32,16), 
62                                undefined, undefined /* maybe some day I'll get that one */);
63         // arm button once document is loaded
64         (function(updater) {$(document).ready(function(){updater.arm_button()})})(this);
65
66         this.arm_button = function () {
67             $('#updater-' + this.options.plugin_uuid).click(this, this.submit_update_request);
68         },
69         this.submit_update_request = function (e) {
70             messages.debug("submit_update_request");
71             var query_uuid = e.data.options.query_uuid;
72             var update_query = e.data.update_query;
73             $.publish("/messages/debug","Updater.submit_update_request " + update_query.__repr());
74             // publish results as if coming from the original query
75             manifold.asynchroneous_exec ( [ {'query_uuid': query_uuid, 'publish_uuid' : update_query.query_uuid} ]);
76             // looks like a previous attempt to disable the button while the query is flying
77             //$('#updateslice-' + options.plugin_uuid).prop('disabled', true);
78         },
79
80         update_resources = function (e, resources, change) {
81             data = e.data.instance.data().Slices;
82
83             data.update_query.params['resource'] = resources
84             $.publish('/update/' + data.options.query_uuid, [data.update_query, true]);
85         },
86
87         update_leases = function (e, leases, change) {
88             data = e.data.instance.data().Slices;
89             
90             data.update_query.params['lease'] = leases
91             $.publish('/update/' + data.options.query_uuid, [data.update_query, true]);
92         },
93   
94         update_slice = function (e, rows, query) {
95
96             /* This function is called twice : get and update */
97
98       
99             var data = e.data.instance.data().Slices;
100       
101             /* Update placeholders and trigger subqueries updates */
102             if (rows.length == 0) {
103                 alert("no result");
104                 return;
105             }
106             var slice = rows[0];
107       
108             /* for get */
109             if (data.update_query == null) {
110                 data.update_query = new Query('update','slice', 'now', query.filter, {"resource": null, "lease": null}, query.fields, 0, data.options.query_uuid);
111             }
112             /* In case of update the list of resources and leases should be updated accordingly */
113       
114             /* only for get ? */
115             $.each(slice, function(key, value) {
116                 if (typeof value == 'string') {
117                     $('#myslice__' + key).html(value);
118                 }
119             });
120       
121             /* TODO avoid repetitions + made this code generic and plugin-independent */
122             
123             if (query.method == 'update') {
124                 // XXX NON, les uuid doivent etre les memes que dans la query Get, cet appel devrait etre fait avant.
125                 query.analyzed_subqueries();
126             }
127       
128             /* NOTE: Dans le cadre d'un update, on n'a pas besoin de refaire tout
129              * le query plan et obtenir toutes les infos, par contre on ne peut pas
130              * savoir d'avance quels parametres ont été accordés, changés, etc.
131              * Dans le cas général, ca pourrait affecter le query plan...
132              * Par contre on n'a pas d'information sur toutes les resources, mais
133              * uniquement celles dans la liste. Comment gérer ?
134              */
135             
136             /* Inform child plugins about their respective parts of the results */
137             /* Only for get */
138             var r_subq = query.analyzed_query.subqueries['resource'];
139             var l_subq = query.analyzed_query.subqueries['lease'];
140             $.publish('/results/' + r_subq.uuid + '/changed', [slice['resource'], r_subq]);
141             $.publish('/results/' + l_subq.uuid + '/changed', [slice['lease'],    l_subq]);
142             
143             /* Subscribe to get notifications from child plugins */
144             if (!data.child_subscribe) {
145                 $.subscribe('/update-set/' + r_subq.uuid, {instance: e.data.instance}, update_resources);
146                 $.subscribe('/update-set/' + l_subq.uuid, {instance: e.data.instance}, update_leases);
147                 data.child_subscribe = true
148             }
149             
150         }
151     }
152 })( jQuery );
153