added paging to queued recs grid
[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("dijit.layout.ContentPane");
21 dojo.require("dijit.layout.TabContainer");
22 dojo.require("dojo.cookie");
23 dojo.require("dojox.grid.Grid");
24 dojo.require("dojo.data.ItemFileReadStore");
25 dojo.require('dojo.date.locale');
26 dojo.require('dojo.date.stamp');
27 dojo.require("fieldmapper.Fieldmapper");
28 dojo.require("fieldmapper.dojoData");
29 dojo.require('openils.CGI');
30 dojo.require('openils.User');
31 dojo.require('openils.Event');
32 dojo.require('openils.MarcXPathParser');
33
34 var globalDivs = [
35     'vl-generic-progress',
36     'vl-generic-progress-with-total',
37     'vl-marc-upload-div',
38     'vl-queue-div',
39     'vl-match-div',
40     'vl-match-html-div',
41     'vl-queue-select-div',
42     'vl-marc-upload-status-div'
43 ];
44
45 var authtoken;
46 var VANDELAY_URL = '/vandelay-upload';
47 var bibAttrDefs = [];
48 var authAttrDefs = [];
49 var queuedRecords = [];
50 var queuedRecordsMap = {};
51 var bibAttrsFetched = false;
52 var authAttrsFetched = false;
53 var attrDefMap = {}; // maps attr def code names to attr def ids
54 var currentType;
55 var currentQueueId = null;
56 var userCache = {};
57 var currentMatchedRecords; // set of loaded matched bib records
58 var currentOverlayRecordsMap; // map of import record to overlay record
59 var currentImportRecId; // when analyzing matches, this is the current import record
60 var userBibQueues;
61 var userAuthQueues;
62 var selectableGridRecords;
63 var cgi = new openils.CGI();
64
65 /**
66   * Grab initial data
67   */
68 function vlInit() {
69     authtoken = dojo.cookie('ses') || cgi.param('ses');
70     var initNeeded = 4; // how many async responses do we need before we're init'd 
71     var initCount = 0; // how many async reponses we've received
72
73     function checkInitDone() {
74         initCount++;
75         if(initCount == initNeeded)
76             runStartupCommands();
77     }
78
79     // Fetch the bib and authority attribute definitions
80     fieldmapper.standardRequest(
81         ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
82         {   async: true,
83             params: [authtoken, {id:{'!=':null}}],
84             onresponse: function(r) {
85                 var def = r.recv().content(); 
86                 if(e = openils.Event.parse(def[0])) 
87                     return alert(e);
88                 bibAttrDefs.push(def);
89             },
90             oncomplete: function() {
91                 bibAttrDefs = bibAttrDefs.sort(
92                     function(a, b) {
93                         if(a.id() > b.id()) return 1;
94                         if(a.id() < b.id()) return -1;
95                         return 0;
96                     }
97                 );
98                 checkInitDone();
99             }
100         }
101     );
102
103     fieldmapper.standardRequest(
104         ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
105         {   async: true,
106             params: [authtoken, {id:{'!=':null}}],
107             onresponse: function(r) {
108                 var def = r.recv().content(); 
109                 if(e = openils.Event.parse(def[0])) 
110                     return alert(e);
111                 authAttrDefs.push(def);
112             },
113             oncomplete: function() {
114                 authAttrDefs = authAttrDefs.sort(
115                     function(a, b) {
116                         if(a.id() > b.id()) return 1;
117                         if(a.id() < b.id()) return -1;
118                         return 0;
119                     }
120                 );
121                 checkInitDone();
122             }
123         }
124     );
125
126     fieldmapper.standardRequest(
127         ['open-ils.vandelay', 'open-ils.vandelay.bib_queue.owner.retrieve.atomic'],
128         {   async: true,
129             params: [authtoken],
130             oncomplete: function(r) {
131                 var list = r.recv().content();
132                 if(e = openils.Event.parse(list[0]))
133                     return alert(e);
134                 userBibQueues = list;
135                 checkInitDone();
136             }
137         }
138     );
139
140     fieldmapper.standardRequest(
141         ['open-ils.vandelay', 'open-ils.vandelay.authority_queue.owner.retrieve.atomic'],
142         {   async: true,
143             params: [authtoken],
144             oncomplete: function(r) {
145                 var list = r.recv().content();
146                 if(e = openils.Event.parse(list[0]))
147                     return alert(e);
148                 userAuthQueues = list;
149                 checkInitDone();
150             }
151         }
152     );
153 }
154
155 function displayGlobalDiv(id) {
156     for(var i = 0; i < globalDivs.length; i++) {
157         try {
158             dojo.style(dojo.byId(globalDivs[i]), 'display', 'none');
159         } catch(e) {
160             alert('please define div ' + globalDivs[i]);
161         }
162     }
163     dojo.style(dojo.byId(id),'display','block');
164 }
165
166 function runStartupCommands() {
167     currentQueueId = cgi.param('qid');
168     currentType = cgi.param('qtype');
169     if(currentQueueId)
170         return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
171     vlShowUploadForm();
172 }
173
174 /**
175   * asynchronously upload a file of MARC records
176   */
177 function uploadMARC(onload){
178     dojo.byId('vl-ses-input').value = authtoken;
179     displayGlobalDiv('vl-marc-upload-status-div');
180     dojo.io.iframe.send({
181         url: VANDELAY_URL,
182         method: "post",
183         handleAs: "html",
184         form: dojo.byId('vl-marc-upload-form'),
185         handle: function(data,ioArgs){
186             var content = data.documentElement.textContent;
187             onload(content);
188         }
189     });
190 }       
191
192 /**
193   * Creates a new vandelay queue
194   */
195 function createQueue(queueName, type, onload) {
196     fieldmapper.standardRequest(
197         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.create'],
198         {   async: true,
199             params: [authtoken, queueName, null, type],
200             oncomplete : function(r) {
201                 var queue = r.recv().content();
202                 if(e = openils.Event.parse(queue)) 
203                     return alert(e);
204                 onload(queue);
205             }
206         }
207     );
208 }
209
210 /**
211   * Tells vendelay to pull a batch of records from the cache and explode them
212   * out into the vandelay tables
213   */
214 function processSpool(key, queueId, type, onload) {
215     fieldmapper.standardRequest(
216         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
217         {   async: true,
218             params: [authtoken, key, queueId],
219             oncomplete : function(r) {
220                 var resp = r.recv().content();
221                 if(e = openils.Event.parse(resp)) 
222                     return alert(e);
223                 onload();
224             }
225         }
226     );
227 }
228
229 function retrieveQueuedRecords(type, queueId, onload) {
230     displayGlobalDiv('vl-generic-progress');
231     queuedRecords = [];
232     queuedRecordsMap = {};
233     currentOverlayRecordsMap = {};
234     selectableGridRecords = {};
235     resetVlQueueGridLayout();
236
237     var method = 'open-ils.vandelay.'+type+'_queue.records.retrieve.atomic';
238     if(vlQueueGridShowMatches.checked)
239         method = method.replace('records', 'records.matches');
240
241     var limit = parseInt(vlQueueDisplayLimit.getValue());
242     var offset = limit * parseInt(vlQueueDisplayPage.getValue()-1);
243
244     fieldmapper.standardRequest(
245         ['open-ils.vandelay', method],
246         {   async: true,
247             params: [authtoken, queueId, 
248                 {   clear_marc: 1, 
249                     offset: offset,
250                     limit: limit
251                 }
252             ],
253             /* intermittent bug in streaming, multipart requests prevents use of onreponse for now...
254             onresponse: function(r) {
255                 var rec = r.recv().content();
256                 if(e = openils.Event.parse(rec))
257                     return alert(e);
258                 queuedRecords.push(rec);
259                 queuedRecordsMap[rec.id()] = rec;
260             },
261             */
262             oncomplete: function(r){
263                 var recs = r.recv().content();
264                 if(e = openils.Event.parse(recs[0]))
265                     return alert(e);
266                 for(var i = 0; i < recs.length; i++) {
267                     var rec = recs[i];
268                     queuedRecords.push(rec);
269                     queuedRecordsMap[rec.id()] = rec;
270                 }
271                 onload();
272             }
273         }
274     );
275 }
276
277 function vlLoadMatchUI(recId, attrCode) {
278     displayGlobalDiv('vl-generic-progress');
279     var matches = getRecMatchesFromAttrCode(queuedRecordsMap[recId], attrCode);
280     var records = [];
281     currentImportRecId = recId;
282     for(var i = 0; i < matches.length; i++)
283         records.push(matches[i].eg_record());
284
285     var retrieve = ['open-ils.search', 'open-ils.search.biblio.record_entry.slim.retrieve'];
286     var params = [records];
287     if(currentType == 'auth') {
288         retrieve = ['open-ils.cat', 'open-ils.cat.authority.record.retrieve'];
289         parmas = [authtoken, records, {clear_marc:1}];
290     }
291
292     fieldmapper.standardRequest(
293         retrieve,
294         {   async: true,
295             params:params,
296             oncomplete: function(r) {
297                 var recs = r.recv().content();
298                 if(e = openils.Event.parse(recs))
299                     return alert(e);
300
301                 /* ui mangling */
302                 displayGlobalDiv('vl-match-div');
303                 resetVlMatchGridLayout();
304                 currentMatchedRecords = recs;
305                 vlMatchGrid.setStructure(vlMatchGridLayout);
306
307                 // build the data store or records with match information
308                 var dataStore = bre.toStoreData(recs, null, {virtualFields:['field_type']});
309                 for(var i = 0; i < dataStore.items.length; i++) {
310                     var item = dataStore.items[i];
311                     for(var j = 0; j < matches.length; j++) {
312                         var match = matches[j];
313                         if(match.eg_record() == item.id)
314                             item.field_type = match.field_type();
315                     }
316                 }
317                 // now populate the grid
318                 vlPopulateGrid(vlMatchGrid, dataStore);
319             }
320         }
321     );
322 }
323
324 function vlPopulateGrid(grid, data) {
325     var store = new dojo.data.ItemFileReadStore({data:data});
326     var model = new dojox.grid.data.DojoData(
327         null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
328     grid.setModel(model);
329     grid.update();
330 }
331
332
333 function vlLoadMARCHtml(recId) {
334     displayGlobalDiv('vl-generic-progress');
335     var api = ['open-ils.search', 'open-ils.search.biblio.record.html'];
336     if(currentType == 'auth')
337         api = ['open-ils.search', 'open-ils.search.authority.to_html'];
338     fieldmapper.standardRequest(
339         api, 
340         {   async: true,
341             params: [recId, 1],
342             oncomplete: function(r) {
343             displayGlobalDiv('vl-match-html-div');
344                 var html = r.recv().content();
345                 dojo.byId('vl-match-record-html').innerHTML = html;
346             }
347         }
348     );
349 }
350
351
352 /**
353   * Given a record, an attribute definition code, and a matching record attribute,
354   * this will determine if there are any import matches and build the UI to
355   * represent those matches.  If no matches exist, simply returns the attribute value
356   */
357 function buildAttrColumnUI(rec, attrCode, attr) {
358     var matches = getRecMatchesFromAttrCode(rec, attrCode);
359     if(matches.length > 0) { // found some matches
360         return '<div class="match_div">' +
361             '<a href="javascript:void(0);" onclick="vlLoadMatchUI('+
362             rec.id()+',\''+attrCode+'\');">'+ 
363             attr.attr_value() + '&nbsp;('+matches.length+')</a></div>';
364     }
365
366     return attr.attr_value();
367 }
368
369 function getRecMatchesFromAttrCode(rec, attrCode) {
370     var matches = [];
371     var attr = getRecAttrFromCode(rec, attrCode);
372     for(var j = 0; j < rec.matches().length; j++) {
373         var match = rec.matches()[j];
374         if(match.matched_attr() == attr.id()) 
375             matches.push(match);
376     }
377     return matches;
378 }
379
380 function getRecAttrFromCode(rec, attrCode) {
381     var defId = attrDefMap[attrCode];
382     var attrs = rec.attributes();
383     for(var i = 0; i < attrs.length; i++) {
384         var attr = attrs[i];
385         if(attr.field() == defId) 
386             return attr;
387     }
388     return null;
389 }
390
391 function getAttrValue(rowIdx) {
392     var data = this.grid.model.getRow(rowIdx);
393     if(!data) return '';
394     var attrCode = this.field.split('.')[1];
395     var rec = queuedRecordsMap[data.id];
396     var attr = getRecAttrFromCode(rec, attrCode);
397     if(attr)
398         return buildAttrColumnUI(rec, attrCode, attr);
399     return '';
400 }
401
402 function vlGetDateTimeField(rowIdx) {
403     data = this.grid.model.getRow(rowIdx);
404     if(!data) return '';
405     if(!data[this.field]) return '';
406     var date = dojo.date.stamp.fromISOString(data[this.field]);
407     return dojo.date.locale.format(date, {selector:'date'});
408 }
409
410 function vlGetCreator(rowIdx) {
411     data = this.grid.model.getRow(rowIdx);
412     if(!data) return '';
413     var id = data.creator;
414     if(userCache[id])
415         return userCache[id].usrname();
416     var user = fieldmapper.standardRequest(
417         ['open-ils.actor', 'open-ils.actor.user.retrieve'], [authtoken, id]);
418     if(e = openils.Event.parse(user))
419         return alert(e);
420     userCache[id] = user;
421     return user.usrname();
422 }
423
424 function vlGetViewMARC(rowIdx) {
425     data = this.grid.model.getRow(rowIdx);
426     if(data) 
427         return this.value.replace('RECID', data.id);
428 }
429
430 function vlGetOverlayTargetSelector(rowIdx) {
431     data = this.grid.model.getRow(rowIdx);
432     if(data) {
433         var value = this.value.replace('ID', data.id);
434         var overlay = currentOverlayRecordsMap[currentImportRecId];
435         if(overlay && overlay == data.id) 
436             value = value.replace('/>', 'checked="checked"/>');
437         return value;
438     }
439 }
440
441 /**
442   * see if the user has enabled overlays for the current match set and, 
443   * if so, map the current import record to the overlay target.
444   */
445 function vlHandleOverlayTargetSelected() {
446     if(vlOverlayTargetEnable.checked) {
447         for(var i = 0; i < currentMatchedRecords.length; i++) {
448             var matchRecId = currentMatchedRecords[i].id();
449             if(dojo.byId('vl-overlay-target-'+matchRecId).checked) {
450                 console.log("found overlay target " + matchRecId);
451                 currentOverlayRecordsMap[currentImportRecId] = matchRecId;
452                 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = true;
453                 dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = 'overlay_selected';
454                 return;
455             }
456         }
457     } else {
458         delete currentOverlayRecordsMap[currentImportRecId];
459         dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = false;
460     }
461 }
462
463 function buildRecordGrid(type) {
464     displayGlobalDiv('vl-queue-div');
465
466     currentOverlayRecordsMap = {};
467
468     if(queuedRecords.length == 0 && vlQueueDisplayPage.getValue() == 1) {
469         dojo.style(dojo.byId('vl-queue-no-records'), 'display', 'block');
470         dojo.style(dojo.byId('vl-queue-div-grid'), 'display', 'none');
471         return;
472     } else {
473         dojo.style(dojo.byId('vl-queue-no-records'), 'display', 'none');
474         dojo.style(dojo.byId('vl-queue-div-grid'), 'display', 'block');
475     }
476
477     var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
478     for(var i = 0; i < defs.length; i++) {
479         var def = defs[i]
480         attrDefMap[def.code()] = def.id();
481         var col = {
482             name:def.description(), 
483             field:'attr.' + def.code(),
484             get: getAttrValue
485         };
486         //if(def.code().match(/title/i)) col.width = 'auto'; // this is hack.
487         vlQueueGridLayout[0].cells[0].push(col);
488     }
489
490     var storeData;
491     if(type == 'bib')
492         storeData = vqbr.toStoreData(queuedRecords);
493     else
494         storeData = vqar.toStoreData(queuedRecords);
495
496     var store = new dojo.data.ItemFileReadStore({data:storeData});
497     var model = new dojox.grid.data.DojoData(
498         null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
499
500     vlQueueGrid.setModel(model);
501     vlQueueGrid.setStructure(vlQueueGridLayout);
502     vlQueueGrid.update();
503 }
504
505 /*
506 function test() {
507     alert(vlQueueGridLayout.picker);
508     vlQueueGridLayout.oils = {};
509     vlQueueGridLayout[0].cells[0].pop();
510     vlQueueGridLayout[0].cells[0].pop();
511     vlQueueGridLayout[0].cells[0].pop();
512     vlQueueGridLayout[0].cells[0].pop();
513     vlQueueGridLayout[0].cells[0].pop();
514     vlQueueGrid.setStructure(vlQueueGridLayout);
515     vlQueueGrid.update();
516 }
517 */
518
519 function vlQueueGridDrawSelectBox(rowIdx) {
520     var data = this.grid.model.getRow(rowIdx);
521     if(!data) return '';
522     var domId = 'vl-record-list-selected-' +data.id;
523     selectableGridRecords[domId] = data.id;
524     return "<div><input type='checkbox' id='"+domId+"'/></div>";
525 }
526
527 function vlSelectAllGridRecords() {
528     for(var id in selectableGridRecords) 
529         dojo.byId(id).checked = true;
530 }
531 function vlSelectNoGridRecords() {
532     for(var id in selectableGridRecords) 
533         dojo.byId(id).checked = false;
534 }
535
536 var handleRetrieveRecords = function() {
537     buildRecordGrid(currentType);
538 }
539
540 function vlImportSelectedRecords() {
541     displayGlobalDiv('vl-generic-progress-with-total');
542     var records = [];
543
544     for(var id in selectableGridRecords) {
545         if(dojo.byId(id).checked) {
546             var recId = selectableGridRecords[id];
547             var rec = queuedRecordsMap[recId];
548             if(!rec.import_time()) 
549                 records.push(recId);
550         }
551     }
552
553     fieldmapper.standardRequest(
554         ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
555         {   async: true,
556             params: [authtoken, records, {overlay_map:currentOverlayRecordsMap}],
557             onresponse: function(r) {
558                 var resp = r.recv().content();
559                 if(e = openils.Event.parse(resp))
560                     return alert(e);
561                 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
562             },
563             oncomplete: function() {
564                 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
565             }
566         }
567     );
568 }
569
570
571 /**
572   * Create queue, upload MARC, process spool, load the newly created queue 
573   */
574 function batchUpload() {
575     var queueName = dijit.byId('vl-queue-name').getValue();
576     currentType = dijit.byId('vl-record-type').getValue();
577
578     var handleProcessSpool = function() {
579         console.log('records uploaded and spooled');
580         retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
581     }
582
583     var handleUploadMARC = function(key) {
584         console.log('marc uploaded');
585         dojo.style(dojo.byId('vl-upload-status-processing'), 'display', 'block');
586         processSpool(key, currentQueueId, currentType, handleProcessSpool);
587     };
588
589     var handleCreateQueue = function(queue) {
590         console.log('queue created ' + queue.name());
591         currentQueueId = queue.id();
592         uploadMARC(handleUploadMARC);
593     };
594     
595     if(vlUploadQueueSelector.getValue() && !queueName) {
596         currentQueueId = vlUploadQueueSelector.getValue();
597         console.log('adding records to existing queue ' + currentQueueId);
598         uploadMARC(handleUploadMARC);
599     } else {
600         createQueue(queueName, currentType, handleCreateQueue);
601     }
602 }
603
604
605 function vlFleshQueueSelect(selector, type) {
606     var data = (type == 'bib') ? vbq.toStoreData(userBibQueues) : vaq.toStoreData(userAuthQueues);
607     selector.store = new dojo.data.ItemFileReadStore({data:data});
608     selector.setValue(null);
609     selector.setDisplayedValue('');
610     if(data[0])
611         selector.setValue(data[0].id());
612 }
613
614 function vlShowUploadForm() {
615     displayGlobalDiv('vl-marc-upload-div');
616     vlFleshQueueSelect(vlUploadQueueSelector, vlUploadRecordType.getValue());
617 }
618
619 function vlShowQueueSelect() {
620     displayGlobalDiv('vl-queue-select-div');
621     vlFleshQueueSelect(vlQueueSelectQueueList, vlQueueSelectType.getValue());
622 }
623
624 function vlFetchQueueFromForm() {
625     currentType = vlQueueSelectType.getValue();
626     currentQueueId = vlQueueSelectQueueList.getValue();
627     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
628 }
629
630 dojo.addOnLoad(vlInit);