fixed bugs in displaying multiple matches on the same destionation record. fixed...
[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("dijit.layout.LayoutContainer");
23 dojo.require('dijit.form.Button');
24 dojo.require('dijit.Toolbar');
25 dojo.require('dijit.Tooltip');
26 dojo.require('dijit.Menu');
27 dojo.require("dijit.Dialog");
28 dojo.require("dojo.cookie");
29 dojo.require("dojox.grid.Grid");
30 dojo.require("dojo.data.ItemFileReadStore");
31 dojo.require('dojo.date.locale');
32 dojo.require('dojo.date.stamp');
33 dojo.require("fieldmapper.Fieldmapper");
34 dojo.require("fieldmapper.dojoData");
35 dojo.require('openils.CGI');
36 dojo.require('openils.User');
37 dojo.require('openils.Event');
38 dojo.require('openils.MarcXPathParser');
39 dojo.require('openils.GridColumnPicker');
40
41
42 var globalDivs = [
43     'vl-generic-progress',
44     'vl-generic-progress-with-total',
45     'vl-marc-upload-div',
46     'vl-queue-div',
47     'vl-match-div',
48     'vl-marc-html-div',
49     'vl-queue-select-div',
50     'vl-marc-upload-status-div',
51     'vl-attr-editor-div',
52 ];
53
54 var authtoken;
55 var VANDELAY_URL = '/vandelay-upload';
56 var bibAttrDefs = [];
57 var authAttrDefs = [];
58 var queuedRecords = [];
59 var queuedRecordsMap = {};
60 var bibAttrsFetched = false;
61 var authAttrsFetched = false;
62 var attrDefMap = {}; // maps attr def code names to attr def ids
63 var currentType;
64 var currentQueueId = null;
65 var userCache = {};
66 var currentMatchedRecords; // set of loaded matched bib records
67 var currentOverlayRecordsMap; // map of import record to overlay record
68 var currentOverlayRecordsMapGid; // map of import record to overlay record grid id
69 var currentImportRecId; // when analyzing matches, this is the current import record
70 var userBibQueues = []; // only non-complete queues
71 var userAuthQueues = []; // only non-complete queues
72 var allUserBibQueues;
73 var allUserAuthQueues;
74 var selectableGridRecords;
75 var cgi = new openils.CGI();
76 var vlQueueGridColumePicker;
77
78 /**
79   * Grab initial data
80   */
81 function vlInit() {
82     authtoken = dojo.cookie('ses') || cgi.param('ses');
83     var initNeeded = 4; // how many async responses do we need before we're init'd 
84     var initCount = 0; // how many async reponses we've received
85
86     function checkInitDone() {
87         initCount++;
88         if(initCount == initNeeded)
89             runStartupCommands();
90     }
91
92     // Fetch the bib and authority attribute definitions 
93     vlFetchBibAttrDefs(function () { checkInitDone(); });
94     vlFetchAuthAttrDefs(function () { checkInitDone(); });
95
96     vlRetrieveQueueList('bib', null, 
97         function(list) {
98             allUserBibQueues = list;
99             for(var i = 0; i < allUserBibQueues.length; i++) {
100                 if(allUserBibQueues[i].complete() == 'f')
101                     userBibQueues.push(allUserBibQueues[i]);
102             }
103             checkInitDone();
104         }
105     );
106
107     vlRetrieveQueueList('auth', null, 
108         function(list) {
109             allUserAuthQueues = list;
110             for(var i = 0; i < allUserAuthQueues.length; i++) {
111                 if(allUserAuthQueues[i].complete() == 'f')
112                     userAuthQueues.push(allUserAuthQueues[i]);
113             }
114             checkInitDone();
115         }
116     );
117
118     vlAttrEditorInit();
119 }
120
121
122 dojo.addOnLoad(vlInit);
123
124
125 // fetch the bib and authority attribute definitions
126
127 function vlFetchBibAttrDefs(postcomplete) {
128     bibAttrDefs = [];
129     fieldmapper.standardRequest(
130         ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
131         {   async: true,
132             params: [authtoken, {id:{'!=':null}}],
133             onresponse: function(r) {
134                 var def = r.recv().content(); 
135                 if(e = openils.Event.parse(def[0])) 
136                     return alert(e);
137                 bibAttrDefs.push(def);
138             },
139             oncomplete: function() {
140                 bibAttrDefs = bibAttrDefs.sort(
141                     function(a, b) {
142                         if(a.id() > b.id()) return 1;
143                         if(a.id() < b.id()) return -1;
144                         return 0;
145                     }
146                 );
147                 postcomplete();
148             }
149         }
150     );
151 }
152
153 function vlFetchAuthAttrDefs(postcomplete) {
154     authAttrDefs = [];
155     fieldmapper.standardRequest(
156         ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
157         {   async: true,
158             params: [authtoken, {id:{'!=':null}}],
159             onresponse: function(r) {
160                 var def = r.recv().content(); 
161                 if(e = openils.Event.parse(def[0])) 
162                     return alert(e);
163                 authAttrDefs.push(def);
164             },
165             oncomplete: function() {
166                 authAttrDefs = authAttrDefs.sort(
167                     function(a, b) {
168                         if(a.id() > b.id()) return 1;
169                         if(a.id() < b.id()) return -1;
170                         return 0;
171                     }
172                 );
173                 postcomplete();
174             }
175         }
176     );
177 }
178
179 function vlRetrieveQueueList(type, filter, onload) {
180     type = (type == 'bib') ? type : 'authority';
181     fieldmapper.standardRequest(
182         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.owner.retrieve.atomic'],
183         {   async: true,
184             params: [authtoken, null, filter],
185             oncomplete: function(r) {
186                 var list = r.recv().content();
187                 if(e = openils.Event.parse(list[0]))
188                     return alert(e);
189                 onload(list);
190             }
191         }
192     );
193
194 }
195
196 function displayGlobalDiv(id) {
197     for(var i = 0; i < globalDivs.length; i++) {
198         try {
199             dojo.style(dojo.byId(globalDivs[i]), 'display', 'none');
200         } catch(e) {
201             alert('please define div ' + globalDivs[i]);
202         }
203     }
204     dojo.style(dojo.byId(id),'display','block');
205 }
206
207 function runStartupCommands() {
208     currentQueueId = cgi.param('qid');
209     currentType = cgi.param('qtype');
210     dojo.style('vl-nav-bar', 'visibility', 'visible');
211     if(currentQueueId)
212         return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
213     vlShowUploadForm();
214 }
215
216 /**
217   * asynchronously upload a file of MARC records
218   */
219 function uploadMARC(onload){
220     dojo.byId('vl-upload-status-count').innerHTML = '0';
221     dojo.byId('vl-ses-input').value = authtoken;
222     displayGlobalDiv('vl-marc-upload-status-div');
223     dojo.io.iframe.send({
224         url: VANDELAY_URL,
225         method: "post",
226         handleAs: "html",
227         form: dojo.byId('vl-marc-upload-form'),
228         handle: function(data,ioArgs){
229             var content = data.documentElement.textContent;
230             onload(content);
231         }
232     });
233 }       
234
235 /**
236   * Creates a new vandelay queue
237   */
238 function createQueue(queueName, type, onload) {
239     fieldmapper.standardRequest(
240         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.create'],
241         {   async: true,
242             params: [authtoken, queueName, null, type],
243             oncomplete : function(r) {
244                 var queue = r.recv().content();
245                 if(e = openils.Event.parse(queue)) 
246                     return alert(e);
247                 onload(queue);
248             }
249         }
250     );
251 }
252
253 /**
254   * Tells vandelay to pull a batch of records from the cache and explode them
255   * out into the vandelay tables
256   */
257 function processSpool(key, queueId, type, onload) {
258     fieldmapper.standardRequest(
259         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
260         {   async: true,
261             params: [authtoken, key, queueId],
262             onresponse : function(r) {
263                 var resp = r.recv().content();
264                 if(e = openils.Event.parse(resp)) 
265                     return alert(e);
266                 dojo.byId('vl-upload-status-count').innerHTML = resp;
267             },
268             oncomplete : function(r) {onload();}
269         }
270     );
271 }
272
273 function retrieveQueuedRecords(type, queueId, onload) {
274     displayGlobalDiv('vl-generic-progress');
275     queuedRecords = [];
276     queuedRecordsMap = {};
277     currentOverlayRecordsMap = {};
278     currentOverlayRecordsMapGid = {};
279     selectableGridRecords = {};
280     resetVlQueueGridLayout();
281
282     var method = 'open-ils.vandelay.'+type+'_queue.records.retrieve.atomic';
283     if(vlQueueGridShowMatches.checked)
284         method = method.replace('records', 'records.matches');
285
286     var limit = parseInt(vlQueueDisplayLimit.getValue());
287     var offset = limit * parseInt(vlQueueDisplayPage.getValue()-1);
288
289     fieldmapper.standardRequest(
290         ['open-ils.vandelay', method],
291         {   async: true,
292             params: [authtoken, queueId, 
293                 {   clear_marc: 1, 
294                     offset: offset,
295                     limit: limit
296                 }
297             ],
298             /* intermittent bug in streaming, multipart requests prevents use of onreponse for now...
299             onresponse: function(r) {
300                 var rec = r.recv().content();
301                 if(e = openils.Event.parse(rec))
302                     return alert(e);
303                 queuedRecords.push(rec);
304                 queuedRecordsMap[rec.id()] = rec;
305             },
306             */
307             oncomplete: function(r){
308                 var recs = r.recv().content();
309                 if(e = openils.Event.parse(recs[0]))
310                     return alert(e);
311                 for(var i = 0; i < recs.length; i++) {
312                     var rec = recs[i];
313                     queuedRecords.push(rec);
314                     queuedRecordsMap[rec.id()] = rec;
315                 }
316                 onload();
317             }
318         }
319     );
320 }
321
322 function vlLoadMatchUI(recId) {
323     displayGlobalDiv('vl-generic-progress');
324     var matches = queuedRecordsMap[recId].matches();
325     var records = [];
326     currentImportRecId = recId;
327     for(var i = 0; i < matches.length; i++)
328         records.push(matches[i].eg_record());
329
330     var retrieve = ['open-ils.search', 'open-ils.search.biblio.record_entry.slim.retrieve'];
331     var params = [records];
332     if(currentType == 'auth') {
333         retrieve = ['open-ils.cat', 'open-ils.cat.authority.record.retrieve'];
334         parmas = [authtoken, records, {clear_marc:1}];
335     }
336
337     fieldmapper.standardRequest(
338         retrieve,
339         {   async: true,
340             params:params,
341             oncomplete: function(r) {
342                 var recs = r.recv().content();
343                 if(e = openils.Event.parse(recs))
344                     return alert(e);
345
346                 /* ui mangling */
347                 displayGlobalDiv('vl-match-div');
348                 resetVlMatchGridLayout();
349                 currentMatchedRecords = recs;
350                 if(!vlMatchGrid.structure)
351                     vlMatchGrid.setStructure(vlMatchGridLayout);
352
353                 // build the data store of records with match information
354                 var dataStore = bre.toStoreData(recs, null, 
355                     {virtualFields:['dest_matchpoint', 'src_matchpoint', '_id']});
356                 dataStore.identifier = '_id';
357
358                 var matchSeenMap = {};
359
360                 for(var i = 0; i < dataStore.items.length; i++) {
361                     var item = dataStore.items[i];
362                     item._id = i; // just need something unique
363                     for(var j = 0; j < matches.length; j++) {
364                         var match = matches[j];
365                         if(match.eg_record() == item.id && !matchSeenMap[match.id()]) {
366                             item.dest_matchpoint = match.field_type();
367                             var attr = getRecAttrFromMatch(queuedRecordsMap[recId], match);
368                             item.src_matchpoint = getRecAttrDefFromAttr(attr, currentType).code();
369                             matchSeenMap[match.id()] = 1;
370                             break;
371                         }
372                     }
373                 }
374
375                 // now populate the grid
376                 vlPopulateMatchGrid(vlMatchGrid, dataStore);
377             }
378         }
379     );
380 }
381
382 function vlPopulateMatchGrid(grid, data) {
383     var store = new dojo.data.ItemFileReadStore({data:data});
384     var model = new dojox.grid.data.DojoData(
385         null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
386     grid.setModel(model);
387     grid.update();
388 }
389
390 function showMe(id) {
391     dojo.style(dojo.byId(id), 'display', 'block');
392 }
393 function hideMe(id) {
394     dojo.style(dojo.byId(id), 'display', 'none');
395 }
396
397
398 function vlLoadMARCHtml(recId, inCat, oncomplete) {
399     dijit.byId('vl-marc-html-done-button').onClick = oncomplete;
400     displayGlobalDiv('vl-generic-progress');
401     var api;
402     var params = [recId, 1];
403
404     if(inCat) {
405         hideMe('vl-marc-html-edit-button'); // don't show marc editor button
406         dijit.byId('vl-marc-html-edit-button').onClick = function(){}
407         api = ['open-ils.search', 'open-ils.search.biblio.record.html'];
408         if(currentType == 'auth')
409             api = ['open-ils.search', 'open-ils.search.authority.to_html'];
410     } else {
411         showMe('vl-marc-html-edit-button'); // plug in the marc editor button
412         dijit.byId('vl-marc-html-edit-button').onClick = function() {vlLoadMarcEditor(currentType, recId);};
413         params = [authtoken, recId];
414         api = ['open-ils.vandelay', 'open-ils.vandelay.queued_bib_record.html'];
415         if(currentType == 'auth')
416             api = ['open-ils.vandelay', 'open-ils.vandelay.queued_authority_record.html'];
417     }
418
419     fieldmapper.standardRequest(
420         api, 
421         {   async: true,
422             params: params,
423             oncomplete: function(r) {
424             displayGlobalDiv('vl-marc-html-div');
425                 var html = r.recv().content();
426                 dojo.byId('vl-marc-record-html').innerHTML = html;
427             }
428         }
429     );
430 }
431
432
433 /*
434 function getRecMatchesFromAttrCode(rec, attrCode) {
435     var matches = [];
436     var attr = getRecAttrFromCode(rec, attrCode);
437     for(var j = 0; j < rec.matches().length; j++) {
438         var match = rec.matches()[j];
439         if(match.matched_attr() == attr.id()) 
440             matches.push(match);
441     }
442     return matches;
443 }
444 */
445
446 function getRecAttrFromMatch(rec, match) {
447     for(var i = 0; i < rec.attributes().length; i++) {
448         var attr = rec.attributes()[i];
449         if(attr.id() == match.matched_attr())
450             return attr;
451     }
452 }
453
454 function getRecAttrDefFromAttr(attr, type) {
455     var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
456     for(var i = 0; i < defs.length; i++) {
457         var def = defs[i];
458         if(def.id() == attr.field())
459             return def;
460     }
461 }
462
463 function getRecAttrFromCode(rec, attrCode) {
464     var defId = attrDefMap[attrCode];
465     var attrs = rec.attributes();
466     for(var i = 0; i < attrs.length; i++) {
467         var attr = attrs[i];
468         if(attr.field() == defId) 
469             return attr;
470     }
471     return null;
472 }
473
474 function vlGetViewMatches(rowIdx) {
475     var data = this.grid.model.getRow(rowIdx);
476     if(!data) return '';
477     var rec = queuedRecordsMap[data.id];
478     if(rec.matches().length > 0)
479         return this.value.replace('RECID', data.id);
480     return '';
481 }
482
483 function getAttrValue(rowIdx) {
484     var data = this.grid.model.getRow(rowIdx);
485     if(!data) return '';
486     var attrCode = this.field.split('.')[1];
487     var rec = queuedRecordsMap[data.id];
488     var attr = getRecAttrFromCode(rec, attrCode);
489     return (attr) ? attr.attr_value() : '';
490 }
491
492 function vlGetDateTimeField(rowIdx) {
493     data = this.grid.model.getRow(rowIdx);
494     if(!data) return '';
495     if(!data[this.field]) return '';
496     var date = dojo.date.stamp.fromISOString(data[this.field]);
497     return dojo.date.locale.format(date, {selector:'date'});
498 }
499
500 function vlGetCreator(rowIdx) {
501     data = this.grid.model.getRow(rowIdx);
502     if(!data) return '';
503     var id = data.creator;
504     if(userCache[id])
505         return userCache[id].usrname();
506     var user = fieldmapper.standardRequest(
507         ['open-ils.actor', 'open-ils.actor.user.retrieve'], [authtoken, id]);
508     if(e = openils.Event.parse(user))
509         return alert(e);
510     userCache[id] = user;
511     return user.usrname();
512 }
513
514 function vlGetViewMARC(rowIdx) {
515     data = this.grid.model.getRow(rowIdx);
516     if(data) 
517         return this.value.replace('RECID', data.id);
518 }
519
520 function vlGetOverlayTargetSelector(rowIdx) {
521     data = this.grid.model.getRow(rowIdx);
522     if(data) {
523         var value = this.value.replace(/GRIDID/g, data._id);
524         value = value.replace(/RECID/g, currentImportRecId);
525         value = value.replace(/ID/g, data.id);
526         if(data._id == currentOverlayRecordsMapGid[currentImportRecId])
527             return value.replace('/>', 'checked="checked"/>');
528         return value;
529     }
530 }
531
532 /**
533   * see if the user has enabled overlays for the current match set and, 
534   * if so, map the current import record to the overlay target.
535   */
536 function vlHandleOverlayTargetSelected(recId, gridId) {
537     var noneSelected = true;
538     var checkboxes = dojo.query('[name=vl-overlay-target-'+currentImportRecId+']');
539     for(var i = 0; i < checkboxes.length; i++) {
540         var checkbox = checkboxes[i];
541         var matchRecId = checkbox.getAttribute('match');
542         var gid = checkbox.getAttribute('gridid');
543         if(checkbox.checked) {
544             if(matchRecId == recId && gid == gridId) {
545                 noneSelected = false;
546                 currentOverlayRecordsMap[currentImportRecId] = matchRecId;
547                 currentOverlayRecordsMapGid[currentImportRecId] = gid;
548                 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = true;
549                 dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = 'overlay_selected';
550             } else {
551                 checkbox.checked = false;
552             }
553         }
554     }
555
556     if(noneSelected) {
557         delete currentOverlayRecordsMap[currentImportRecId];
558         delete currentOverlayRecordsMapGid[currentImportRecId];
559         dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = false;
560         dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = '';
561     }
562 }
563
564 var vlQueueGridBuilt = false;
565 function buildRecordGrid(type) {
566     displayGlobalDiv('vl-queue-div');
567
568     currentOverlayRecordsMap = {};
569
570     if(!vlQueueGridBuilt) {
571         var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
572         for(var i = 0; i < defs.length; i++) {
573             var def = defs[i]
574             attrDefMap[def.code()] = def.id();
575             var col = {
576                 name:def.description(), 
577                 field:'attr.' + def.code(),
578                 get: getAttrValue,
579                 selectableColumn:true
580             };
581             vlQueueGridLayout[0].cells[0].push(col);
582         }
583         vlQueueGridBuilt = true;
584     }
585
586     var storeData;
587     if(type == 'bib')
588         storeData = vqbr.toStoreData(queuedRecords);
589     else
590         storeData = vqar.toStoreData(queuedRecords);
591
592     var store = new dojo.data.ItemFileReadStore({data:storeData});
593     var model = new dojox.grid.data.DojoData(
594         null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
595     vlQueueGrid.setModel(model);
596
597     if(vlQueueGridColumePicker) {
598         vlQueueGrid.update();
599     } else {
600         vlQueueGridColumePicker = 
601             new openils.GridColumnPicker(vlQueueGridColumePickerDialog, 
602                 vlQueueGrid, vlQueueGridLayout, authtoken, 'vandelay.queue');
603         vlQueueGridColumePicker.load();
604     }
605 }
606
607 function vlQueueGridPrevPage() {
608     var page = parseInt(vlQueueDisplayPage.getValue());
609     if(page < 2) return;
610     vlQueueDisplayPage.setValue(page - 1);
611     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
612 }
613
614 function vlQueueGridNextPage() {
615     vlQueueDisplayPage.setValue(parseInt(vlQueueDisplayPage.getValue())+1);
616     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
617 }
618
619 function vlDeleteQueue(type, queueId, onload) {
620     fieldmapper.standardRequest(
621         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.delete'],
622         {   async: true,
623             params: [authtoken, queueId],
624             oncomplete: function(r) {
625                 var resp = r.recv().content();
626                 if(e = openils.Event.parse(resp))
627                     return alert(e);
628                 onload();
629             }
630         }
631     );
632 }
633
634
635 function vlQueueGridDrawSelectBox(rowIdx) {
636     var data = this.grid.model.getRow(rowIdx);
637     if(!data) return '';
638     var domId = 'vl-record-list-selected-' +data.id;
639     selectableGridRecords[domId] = data.id;
640     return "<div><input type='checkbox' id='"+domId+"'/></div>";
641 }
642
643 function vlSelectAllQueueGridRecords() {
644     for(var id in selectableGridRecords) 
645         dojo.byId(id).checked = true;
646 }
647 function vlSelectNoQueueGridRecords() {
648     for(var id in selectableGridRecords) 
649         dojo.byId(id).checked = false;
650 }
651 function vlToggleQueueGridSelect() {
652     if(dojo.byId('vl-queue-grid-row-selector').checked)
653         vlSelectAllQueueGridRecords();
654     else
655         vlSelectNoQueueGridRecords();
656 }
657
658 var handleRetrieveRecords = function() {
659     buildRecordGrid(currentType);
660 }
661
662 function vlImportSelectedRecords() {
663     displayGlobalDiv('vl-generic-progress-with-total');
664     var records = [];
665
666     for(var id in selectableGridRecords) {
667         if(dojo.byId(id).checked) {
668             var recId = selectableGridRecords[id];
669             var rec = queuedRecordsMap[recId];
670             if(!rec.import_time()) 
671                 records.push(recId);
672         }
673     }
674
675     fieldmapper.standardRequest(
676         ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
677         {   async: true,
678             params: [authtoken, records, {overlay_map:currentOverlayRecordsMap}],
679             onresponse: function(r) {
680                 var resp = r.recv().content();
681                 if(e = openils.Event.parse(resp))
682                     return alert(e);
683                 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
684             },
685             oncomplete: function() {
686                 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
687             }
688         }
689     );
690 }
691
692 function vlImportRecordQueue(type, queueId, noMatchOnly, onload) {
693     displayGlobalDiv('vl-generic-progress-with-total');
694     var method = 'open-ils.vandelay.bib_queue.import';
695     if(noMatchOnly)
696         method = method.replace('import', 'nomatch.import');
697     if(type == 'auth')
698         method = method.replace('bib', 'auth');
699
700     fieldmapper.standardRequest(
701         ['open-ils.vandelay', method],
702         {   async: true,
703             params: [authtoken, queueId],
704             onresponse: function(r) {
705                 var resp = r.recv().content();
706                 if(e = openils.Event.parse(resp))
707                     return alert(e);
708                 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
709             },
710             oncomplete: function() {onload();}
711         }
712     );
713 }
714
715
716 /**
717   * Create queue, upload MARC, process spool, load the newly created queue 
718   */
719 function batchUpload() {
720     var queueName = dijit.byId('vl-queue-name').getValue();
721     currentType = dijit.byId('vl-record-type').getValue();
722
723     var handleProcessSpool = function() {
724         console.log('records uploaded and spooled');
725         if(vlUploadQueueAutoImport.checked) {
726             vlImportRecordQueue(currentType, currentQueueId, true,  
727                 function() {
728                     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
729                 }
730             );
731         } else {
732             retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
733         }
734     }
735
736     var handleUploadMARC = function(key) {
737         console.log('marc uploaded');
738         dojo.style(dojo.byId('vl-upload-status-processing'), 'display', 'block');
739         processSpool(key, currentQueueId, currentType, handleProcessSpool);
740     };
741
742     var handleCreateQueue = function(queue) {
743         console.log('queue created ' + queue.name());
744         currentQueueId = queue.id();
745         uploadMARC(handleUploadMARC);
746     };
747     
748     if(vlUploadQueueSelector.getValue() && !queueName) {
749         currentQueueId = vlUploadQueueSelector.getValue();
750         console.log('adding records to existing queue ' + currentQueueId);
751         uploadMARC(handleUploadMARC);
752     } else {
753         createQueue(queueName, currentType, handleCreateQueue);
754     }
755 }
756
757
758 function vlFleshQueueSelect(selector, type) {
759     var data = (type == 'bib') ? vbq.toStoreData(allUserBibQueues) : vaq.toStoreData(allUserAuthQueues);
760     selector.store = new dojo.data.ItemFileReadStore({data:data});
761     selector.setValue(null);
762     selector.setDisplayedValue('');
763     if(data[0])
764         selector.setValue(data[0].id());
765 }
766
767 function vlShowUploadForm() {
768     displayGlobalDiv('vl-marc-upload-div');
769     vlFleshQueueSelect(vlUploadQueueSelector, vlUploadRecordType.getValue());
770 }
771
772 function vlShowQueueSelect() {
773     displayGlobalDiv('vl-queue-select-div');
774     vlFleshQueueSelect(vlQueueSelectQueueList, vlQueueSelectType.getValue());
775 }
776
777 function vlFetchQueueFromForm() {
778     currentType = vlQueueSelectType.getValue();
779     currentQueueId = vlQueueSelectQueueList.getValue();
780     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
781 }
782
783 function vlOpenMarcEditWindow(rec) {
784     /*
785         To run in Firefox directly, must set signed.applets.codebase_principal_support
786         to true in about:config
787     */
788     netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
789     win = window.open('/xul/server/cat/marcedit.xul'); // XXX version?
790     win.xulG = {
791         record : {marc : rec.marc()},
792         save : {
793             label: 'Save', // XXX
794             func: function(xmlString) {
795                 var method = 'open-ils.permacrud.update.' + rec.classname;
796                 rec.marc(xmlString);
797                 fieldmapper.standardRequest(
798                     ['open-ils.permacrud', method],
799                     {   async: true,
800                         params: [authtoken, rec],
801                         oncomplete: function(r) {
802                             if(e = openils.Event.parse(rec))
803                                 return alert(e);
804                             alert('Record Updated'); // XXX
805                             win.close();
806                             // XXX reload marc html view with updates
807                         }
808                     }
809                 );
810             },
811         }
812     };
813 }
814
815 function vlLoadMarcEditor(type, recId) {
816     var method = 'open-ils.permacrud.search.vqbr';
817     if(currentType != 'bib')
818         method = method.replace(/vqbr/,'vqar');
819
820     fieldmapper.standardRequest(
821         ['open-ils.permacrud', method],
822         {   async: true, 
823             params: [authtoken, {id : recId}],
824             oncomplete: function(r) {
825                 var rec = r.recv().content();
826                 if(e = openils.Event.parse(rec))
827                     return alert(e);
828                 vlOpenMarcEditWindow(rec);
829             }
830         }
831     );
832 }
833
834
835
836 //------------------------------------------------------------
837 // attribute editors
838
839 // attribute-editor global variables
840
841 var ATTR_EDITOR_IN_UPDATE_MODE = false; // true on 'edit', false on 'create'
842 var ATTR_EDIT_ID = null;                // id of current 'edit' attribute
843 var ATTR_EDIT_GROUP = 'bib';            // bib-attrs or auth-attrs
844
845 function vlAttrEditorInit() {
846     // set up tooltips on the edit form
847     connectTooltip('attr-editor-tags'); 
848     connectTooltip('attr-editor-subfields'); 
849 }
850
851 function vlShowAttrEditor() {
852     displayGlobalDiv('vl-attr-editor-div');
853     loadAttrEditorGrid();
854     idHide('vl-generic-progress');
855 }
856
857 function setAttrEditorGroup(groupName) {
858     // put us into 'bib'-attr or 'auth'-attr mode.
859     if (ATTR_EDIT_GROUP != groupName) {
860         ATTR_EDIT_GROUP = groupName;
861         loadAttrEditorGrid();
862     }
863 }
864
865 function onAttrEditorOpen() {
866     // the "bars" have the create/update/cancel/etc. buttons.
867     var create_bar = document.getElementById('attr-editor-create-bar');
868     var update_bar = document.getElementById('attr-editor-update-bar');
869     if (ATTR_EDITOR_IN_UPDATE_MODE) {
870         update_bar.style.display='table-row';
871         create_bar.style.display='none';
872         // hide the dropdown-button
873         idStyle('vl-create-attr-editor-button', 'visibility', 'hidden');
874     } else {
875         dijit.byId('attr-editor-dialog').reset();
876         create_bar.style.display='table-row';
877         update_bar.style.display='none';
878     }
879 }
880
881 function onAttrEditorClose() {
882     // reset the form to a "create" form. (We may have borrowed it for editing.)
883     ATTR_EDITOR_IN_UPDATE_MODE = false;
884     // show the dropdown-button
885     idStyle('vl-create-attr-editor-button', 'visibility', 'visible');
886 }
887
888 function loadAttrEditorGrid() {
889     var _data = (ATTR_EDIT_GROUP == 'auth') ? 
890         vqarad.toStoreData(authAttrDefs) : vqbrad.toStoreData(bibAttrDefs) ;
891                  
892     var store = new dojo.data.ItemFileReadStore({data:_data});
893     var model = new dojox.grid.data.DojoData(
894         null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
895     attrEditorGrid.setModel(model);
896     attrEditorGrid.setStructure(vlAttrGridLayout);
897     attrEditorGrid.onRowClick = onAttrEditorClick;
898     attrEditorGrid.update();
899 }
900
901 function attrGridGetTag(n) {
902     // grid helper: return the tags from the row's xpath column.
903     var xp = this.grid.model.getRow(n);
904     return xp && xpathParser.parse(xp.xpath).tags;
905 }
906
907 function attrGridGetSubfield(n) {
908     // grid helper: return the subfields from the row's xpath column.
909     var xp = this.grid.model.getRow(n);
910     return xp && xpathParser.parse(xp.xpath).subfields;
911 }
912
913 function onAttrEditorClick(evt) {
914     var row = attrEditorGrid.model.getRow(evt.rowIndex);
915     ATTR_EDIT_ID = row.id;
916     ATTR_EDITOR_IN_UPDATE_MODE = true;
917
918     // populate the popup editor.
919     dojo.byId('attr-editor-code').value = row.code;
920     dojo.byId('attr-editor-description').value = row.description;
921     var parsed_xpath = xpathParser.parse(row.xpath);
922     dojo.byId('attr-editor-tags').value = parsed_xpath.tags;
923     dojo.byId('attr-editor-subfields').value = parsed_xpath.subfields;
924     dojo.byId('attr-editor-identifier').value = (row.ident ? 'True':'False');
925     dojo.byId('attr-editor-xpath').value = row.xpath;
926     dojo.byId('attr-editor-remove').value = row.remove;
927
928     // set up UI for editing
929     dojo.byId('vl-create-attr-editor-button').click();
930 }
931
932 function vlSaveAttrDefinition(data) {
933     idHide('vl-attr-editor-div');
934     idShow('vl-generic-progress');
935
936     data.id = ATTR_EDIT_ID;
937
938     // this ought to honour custom xpaths, but overwrite xpaths
939     // derived from tags/subfields.
940     if (data.xpath == '' || looksLikeDerivedXpath(data.xpath)) {
941         var _xpath = tagAndSubFieldsToXpath(data.tag, data.subfield);
942         data.xpath = _xpath;
943     }
944
945     // build up our permacrud params. Key variables here are
946     // "create or update" and "bib or auth".
947
948     var isAuth   = (ATTR_EDIT_GROUP == 'auth');
949     var isCreate = (ATTR_EDIT_ID == null);
950     var rad      = isAuth ? new vqarad() : new vqbrad() ;
951     var method   = 'open-ils.permacrud' + (isCreate ? '.create.' : '.update.') 
952         + (isAuth ? 'vqarad' : 'vqbrad');
953     var _data    = rad.fromStoreItem(data);
954
955     _data.ischanged(1);
956
957     fieldmapper.standardRequest(
958         ['open-ils.permacrud', method],
959         {   async: true,
960             params: [authtoken, _data ],
961             onresponse: function(r) { },
962             oncomplete: function(r) {
963                 attrEditorFetchAttrDefs(vlShowAttrEditor);
964                 ATTR_EDIT_ID = null;
965             },
966             onerror: function(r) {
967                 alert('vlSaveAttrDefinition comms error: ' + r);
968             }
969         }
970     );
971 }
972
973 function attrEditorFetchAttrDefs(callback) {
974     var fn = (ATTR_EDIT_GROUP == 'auth') ? vlFetchAuthAttrDefs : vlFetchBibAttrDefs;
975     return fn(callback);
976 }
977
978 function vlAttrDelete() {
979     idHide('vl-attr-editor-div');
980     idShow('vl-generic-progress');
981
982     var isAuth = (ATTR_EDIT_GROUP == 'auth');
983     var method = 'open-ils.permacrud.delete.' + (isAuth ? 'vqarad' : 'vqbrad');
984     var rad    = isAuth ? new vqarad() : new vqbrad() ;
985     fieldmapper.standardRequest(
986         ['open-ils.permacrud', method],
987         {   async: true,
988             params: [authtoken, rad.fromHash({ id : ATTR_EDIT_ID }), ],
989             oncomplete: function() {
990                 dijit.byId('attr-editor-dialog').onCancel(); // close the dialog
991                 attrEditorFetchAttrDefs(vlShowAttrEditor);
992                 ATTR_EDIT_ID = null;
993             },
994             onerror: function(r) {
995                 alert('vlAttrDelete comms error: ' + r);
996             }
997         }
998     );
999 }
1000
1001 // ------------------------------------------------------------
1002 // utilities for attribute editors
1003
1004 // dom utilities (maybe dojo does these, and these should be replaced)
1005
1006 function idStyle(obId, k, v)    { document.getElementById(obId).style[k] = v;   }
1007 function idShow(obId)           { idStyle(obId, 'display', 'block');            }
1008 function idHide(obId)           { idStyle(obId, 'display' , 'none');            }
1009
1010 function connectTooltip(fieldId) {
1011     // Given an element id, look up a tooltip element in the doc (same
1012     // id with a '-tip' suffix) and associate the two. Maybe dojo has
1013     // a better way to do this?
1014     var fld = dojo.byId(fieldId);
1015     var tip = dojo.byId(fieldId + '-tip');
1016     dojo.connect(fld, 'onfocus', function(evt) {
1017                      dijit.showTooltip(tip.innerHTML, fld, ['below', 'after']); });
1018     dojo.connect(fld, 'onblur', function(evt) { dijit.hideTooltip(fld); });
1019 }
1020
1021 // xpath utilities
1022
1023 var xpathParser = new openils.MarcXPathParser();
1024
1025 function tagAndSubFieldsToXpath(tags, subfields) {
1026     // given tags, and subfields, build up an XPath.
1027     try {
1028         var parts = {
1029             'tags':tags.match(/[\d]+/g), 
1030             'subfields':subfields.match(/[a-zA-z]/g) };
1031         return xpathParser.compile(parts);
1032     } catch (err) {
1033         return {'parts':null, 'tags':null, 'error':err};
1034     }
1035 }
1036
1037 function looksLikeDerivedXpath(path) {
1038     // Does this path look like it was derived from tags and subfields?
1039     var parsed = xpathParser.parse(path);
1040     if (parsed.tags == null) 
1041         return false;
1042     var compiled = xpathParser.compile(parsed);
1043     return (path == compiled);
1044 }
1045
1046 // amazing xpath-util unit-tests
1047 if (!looksLikeDerivedXpath('//*[@tag="901"]/*[@code="c"]'))     alert('vandelay xpath-utility error');
1048 if ( looksLikeDerivedXpath('ba-boo-ba-boo!'))                   alert('vandelay xpath-utility error');