]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/vandelay/vandelay.js
ccf72c6c4e96ec6b76f8e2288271f7cbd3694457
[Evergreen.git] / Open-ILS / web / vandelay / vandelay.js
1 /* ---------------------------------------------------------------------------
2 # Copyright (C) 2008  Georgia Public Library Service
3 # Bill Erickson <erickson@esilibrary.com>
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 # --------------------------------------------------------------------------- */
15 dojo.require("dojo.parser");
16 dojo.require("dojo.io.iframe"); 
17 dojo.require("dijit.ProgressBar"); 
18 dojo.require("dijit.form.Button"); 
19 dojo.require("dijit.form.FilteringSelect"); 
20 dojo.require("dojo.cookie");
21 dojo.require("dojox.grid.Grid");
22 dojo.require("dojo.data.ItemFileReadStore");
23 dojo.require("fieldmapper.Fieldmapper");
24 dojo.require("fieldmapper.dojoData");
25 dojo.require('openils.CGI');
26 dojo.require('openils.User');
27 dojo.require('openils.Event');
28
29 var globalDivs = [
30     'vl-generic-progress',
31     'vl-generic-progress-with-total',
32     'vl-marc-upload-div',
33     'vl-queue-div'
34 ];
35
36 var authtoken;
37 var VANDELAY_URL = '/vandelay';
38 var bibAttrDefs = [];
39 var authAttrDefs = [];
40 var queuedRecords = [];
41 var queuedRecordsMap = {};
42 var bibAttrsFetched = false;
43 var authAttrsFetched = false;
44 var attrMap = {};
45 var currentType;
46 var cgi = new openils.CGI();
47 var currentQueueId = null;
48
49 /**
50   * Grab initial data
51   */
52 function vlInit() {
53     authtoken = dojo.cookie('ses') || cgi.param('ses');
54     bibAttrsFetched = false;
55     authAttrsFetched = false;
56
57     // Fetch the bib and authority attribute definitions
58     fieldmapper.standardRequest(
59         ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
60         {   async: true,
61             params: [authtoken, {id:{'!=':null}}],
62             onresponse: function(r) {
63                 var def = r.recv().content(); 
64                 if(e = openils.Event.parse(def[0])) 
65                     return alert(e);
66                 bibAttrDefs.push(def);
67             },
68             oncomplete: function() {
69                 bibAttrsFetched = true;
70                 bibAttrDefs = bibAttrDefs.sort(
71                     function(a, b) {
72                         if(a.description() > b.description()) return 1;
73                         if(a.description() < b.description()) return -1;
74                         return 0;
75                     }
76                 );
77                 if(authAttrsFetched) 
78                     runStartupCommands();
79             }
80         }
81     );
82
83     fieldmapper.standardRequest(
84         ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
85         {   async: true,
86             params: [authtoken, {id:{'!=':null}}],
87             onresponse: function(r) {
88                 var def = r.recv().content(); 
89                 if(e = openils.Event.parse(def[0])) 
90                     return alert(e);
91                 authAttrDefs.push(def);
92             },
93             oncomplete: function() {
94                 authAttrsFetched = true;
95                 authAttrDefs = authAttrDefs.sort(
96                     function(a, b) {
97                         if(a.description() > b.description()) return 1;
98                         if(a.description() < b.description()) return -1;
99                         return 0;
100                     }
101                 );
102                 if(bibAttrsFetched) 
103                     runStartupCommands();
104             }
105         }
106     );
107 }
108
109 function displayGlobalDiv(id) {
110     for(var i = 0; i < globalDivs.length; i++) 
111         dojo.style(dojo.byId(globalDivs[i]), 'display', 'none');
112     dojo.style(dojo.byId(id),'display','block');
113 }
114
115 function runStartupCommands() {
116     currentQueueId = cgi.param('qid');
117     currentType = cgi.param('qtype');
118     if(currentQueueId)
119         return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
120     displayGlobalDiv('vl-marc-upload-div');
121 }
122
123 /**
124   * asynchronously upload a file of MARC records
125   */
126 function uploadMARC(onload){
127     dojo.byId('vl-ses-input').value = authtoken;
128     dojo.style(dojo.byId('vl-input-td'),"display","none");
129     dojo.style(dojo.byId('vl-upload-progress-span'),"display","inline"); 
130
131     dojo.style(dojo.byId('vl-file-label'), 'display', 'none');
132     dojo.style(dojo.byId('vl-file-uploading'), 'display', 'inline');
133
134     dojo.io.iframe.send({
135         url: VANDELAY_URL,
136         method: "post",
137         handleAs: "html",
138         form: dojo.byId('vl-marc-upload-form'),
139         handle: function(data,ioArgs){
140             var content = data.documentElement.textContent;
141             var key = content.split(/\n/)[2]; /* XXX have to strip the headers.. (why?) */
142             dojo.style(dojo.byId('vl-input-td'),"display","inline");
143             dojo.style(dojo.byId('vl-upload-progress-span'),"display","none");
144             dojo.style(dojo.byId('vl-file-label'), 'display', 'inline');
145             dojo.style(dojo.byId('vl-file-uploading'), 'display', 'none');
146             onload(key);
147         }
148     });
149 }       
150
151 /**
152   * Creates a new vandelay queue
153   */
154 function createQueue(queueName, type, onload) {
155     fieldmapper.standardRequest(
156         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.create'],
157         {   async: true,
158             params: [authtoken, queueName, null, type],
159             oncomplete : function(r) {
160                 var queue = r.recv().content();
161                 if(e = openils.Event.parse(queue)) 
162                     return alert(e);
163                 onload(queue);
164             }
165         }
166     );
167 }
168
169 /**
170   * Tells vendelay to pull a batch of records from the cache and explode them
171   * out into the vandelay tables
172   */
173 function processSpool(key, queueId, type, onload) {
174     fieldmapper.standardRequest(
175         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
176         {   async: true,
177             params: [authtoken, key, queueId],
178             oncomplete : function(r) {
179                 var resp = r.recv().content();
180                 if(e = openils.Event.parse(resp)) 
181                     return alert(e);
182                 onload();
183             }
184         }
185     );
186 }
187
188 function retrieveQueuedRecords(type, queueId, onload) {
189     queuedRecords = [];
190     queuedRecordsMap = {};
191     resetVlQueueGridLayout();
192     fieldmapper.standardRequest(
193         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.records.retrieve.atomic'],
194         {   async: true,
195             params: [authtoken, queueId, {clear_marc:1}],
196             /* intermittent bug in streaming, multipart requests prevents use of onreponse for now...
197             onresponse: function(r) {
198                 var rec = r.recv().content();
199                 if(e = openils.Event.parse(rec))
200                     return alert(e);
201                 queuedRecords.push(rec);
202                 queuedRecordsMap[rec.id()] = rec;
203             },
204             */
205             oncomplete: function(r){
206                 var recs = r.recv().content();
207                 if(e = openils.Event.parse(recs[0]))
208                     return alert(e);
209                 for(var i = 0; i < recs.length; i++) {
210                     var rec = recs[i];
211                     queuedRecords.push(rec);
212                     queuedRecordsMap[rec.id()] = rec;
213                 }
214                 onload();
215             }
216         }
217     );
218 }
219
220 function getAttrValue(rowIdx) {
221     var data = this.grid.model.getRow(rowIdx);
222     if(!data) return '';
223     var attrName = this.field.split('.')[1];
224     var defId = attrMap[attrName];
225     var rec = queuedRecordsMap[data.id];
226     var attrs = rec.attributes();
227     for(var i = 0; i < attrs.length; i++) {
228         var attr = attrs[i];
229         if(attr.field() == defId) 
230             return attr.attr_value();
231     }
232     return '';
233 }
234
235 function buildRecordGrid(type) {
236     displayGlobalDiv('vl-queue-div');
237
238     var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
239     for(var i = 0; i < defs.length; i++) {
240         var attr = defs[i]
241         attrMap[attr.code()] = attr.id();
242         var col = {
243             name:attr.description(), 
244             field:'attr.' + attr.code(),
245             get: getAttrValue
246         };
247         //if(attr.code().match(/title/i)) col.width = 'auto'; // this is hack.
248         vlQueueGridLayout[0].cells[0].push(col);
249     }
250
251     vlQueueGrid.setStructure(vlQueueGridLayout);
252
253     var storeData;
254     if(type == 'bib')
255         storeData = vqbr.toStoreData(queuedRecords);
256     else
257         storeData = vqar.toStoreData(queuedRecords);
258
259     var store = new dojo.data.ItemFileReadStore({data:storeData});
260     var model = new dojox.grid.data.DojoData(
261         null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
262
263     vlQueueGrid.setModel(model);
264     vlQueueGrid.update();
265 }
266
267 var selectableGridRecords = {};
268 function vlQueueGridDrawSelectBox(rowIdx) {
269     var data = this.grid.model.getRow(rowIdx);
270     if(!data) return '';
271     var domId = 'vl-record-list-selected-' +data.id;
272     selectableGridRecords[domId] = data.id;
273     return "<input type='checkbox' id='"+domId+"'/>";
274 }
275
276 function vlSelectAllGridRecords() {
277     for(var id in selectableGridRecords) 
278         dojo.byId(id).checked = true;
279 }
280 function vlSelectNoGridRecords() {
281     for(var id in selectableGridRecords) 
282         dojo.byId(id).checked = false;
283 }
284
285 var handleRetrieveRecords = function() {
286     buildRecordGrid(currentType);
287 }
288
289 function vlImportSelectedRecords() {
290     displayGlobalDiv('vl-generic-progress-with-total');
291     var records = [];
292     for(var id in selectableGridRecords) {
293         if(dojo.byId(id).checked) 
294             records.push(selectableGridRecords[id]);
295     }
296     fieldmapper.standardRequest(
297         ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
298         {   async: true,
299             params: [authtoken, records],
300             onresponse: function(r) {
301                 var resp = r.recv().content();
302                 if(e = openils.Event.parse(resp))
303                     return alert(e);
304                 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
305             },
306             oncomplete: function() {
307                 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
308             }
309         }
310     );
311 }
312
313
314 /**
315   * Create queue, upload MARC, process spool, load the newly created queue 
316   */
317 function batchUpload() {
318     var queueName = dijit.byId('vl-queue-name').getValue();
319     currentType = dijit.byId('vl-record-type').getValue();
320
321     var handleProcessSpool = function() {
322         console.log('records uploaded and spooled');
323         retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
324     }
325
326     var handleUploadMARC = function(key) {
327         console.log('marc uploaded');
328         processSpool(key, currentQueueId, currentType, handleProcessSpool);
329     };
330
331     var handleCreateQueue = function(queue) {
332         console.log('queue created ' + queue.name());
333         currentQueueId = queue.id();
334         uploadMARC(handleUploadMARC);
335     };
336
337     createQueue(queueName, currentType, handleCreateQueue);
338 }
339
340 dojo.addOnLoad(vlInit);