1 /* ---------------------------------------------------------------------------
2 # Copyright (C) 2008 Georgia Public Library Service
3 # Bill Erickson <erickson@esilibrary.com>
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.
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.LayoutContainer");
21 dojo.require("dijit.layout.ContentPane");
22 dojo.require("dijit.layout.TabContainer");
23 dojo.require("dijit.Dialog");
24 dojo.require("dojo.cookie");
25 dojo.require("dojox.grid.Grid");
26 dojo.require("dojo.data.ItemFileReadStore");
27 dojo.require('dojo.date.locale');
28 dojo.require('dojo.date.stamp');
29 dojo.require("fieldmapper.Fieldmapper");
30 dojo.require("fieldmapper.dojoData");
31 dojo.require('openils.CGI');
32 dojo.require('openils.User');
33 dojo.require('openils.Event');
34 dojo.require('openils.MarcXPathParser');
35 dojo.require('openils.GridColumnPicker');
38 'vl-generic-progress',
39 'vl-generic-progress-with-total',
44 'vl-queue-select-div',
45 'vl-marc-upload-status-div'
49 var VANDELAY_URL = '/vandelay-upload';
51 var authAttrDefs = [];
52 var queuedRecords = [];
53 var queuedRecordsMap = {};
54 var bibAttrsFetched = false;
55 var authAttrsFetched = false;
56 var attrDefMap = {}; // maps attr def code names to attr def ids
58 var currentQueueId = null;
60 var currentMatchedRecords; // set of loaded matched bib records
61 var currentOverlayRecordsMap; // map of import record to overlay record
62 var currentImportRecId; // when analyzing matches, this is the current import record
63 var userBibQueues = []; // only non-complete queues
64 var userAuthQueues = []; // only non-complete queues
66 var allUserAuthQueues;
67 var selectableGridRecords;
68 var cgi = new openils.CGI();
69 var vlQueueGridColumePicker;
75 authtoken = dojo.cookie('ses') || cgi.param('ses');
76 var initNeeded = 4; // how many async responses do we need before we're init'd
77 var initCount = 0; // how many async reponses we've received
79 function checkInitDone() {
81 if(initCount == initNeeded)
85 // Fetch the bib and authority attribute definitions
86 fieldmapper.standardRequest(
87 ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
89 params: [authtoken, {id:{'!=':null}}],
90 onresponse: function(r) {
91 var def = r.recv().content();
92 if(e = openils.Event.parse(def[0]))
94 bibAttrDefs.push(def);
96 oncomplete: function() {
97 bibAttrDefs = bibAttrDefs.sort(
99 if(a.id() > b.id()) return 1;
100 if(a.id() < b.id()) return -1;
109 fieldmapper.standardRequest(
110 ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
112 params: [authtoken, {id:{'!=':null}}],
113 onresponse: function(r) {
114 var def = r.recv().content();
115 if(e = openils.Event.parse(def[0]))
117 authAttrDefs.push(def);
119 oncomplete: function() {
120 authAttrDefs = authAttrDefs.sort(
122 if(a.id() > b.id()) return 1;
123 if(a.id() < b.id()) return -1;
132 vlRetrieveQueueList('bib', null,
134 allUserBibQueues = list;
135 for(var i = 0; i < allUserBibQueues.length; i++) {
136 if(allUserBibQueues[i].complete() == 'f')
137 userBibQueues.push(allUserBibQueues[i]);
143 vlRetrieveQueueList('auth', null,
145 allUserAuthQueues = list;
146 for(var i = 0; i < allUserAuthQueues.length; i++) {
147 if(allUserAuthQueues[i].complete() == 'f')
148 userAuthQueues.push(allUserAuthQueues[i]);
155 function vlRetrieveQueueList(type, filter, onload) {
156 type = (type == 'bib') ? type : 'authority';
157 fieldmapper.standardRequest(
158 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.owner.retrieve.atomic'],
160 params: [authtoken, null, filter],
161 oncomplete: function(r) {
162 var list = r.recv().content();
163 if(e = openils.Event.parse(list[0]))
171 function displayGlobalDiv(id) {
172 for(var i = 0; i < globalDivs.length; i++) {
174 dojo.style(dojo.byId(globalDivs[i]), 'display', 'none');
176 alert('please define div ' + globalDivs[i]);
179 dojo.style(dojo.byId(id),'display','block');
182 function runStartupCommands() {
183 currentQueueId = cgi.param('qid');
184 currentType = cgi.param('qtype');
186 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
191 * asynchronously upload a file of MARC records
193 function uploadMARC(onload){
194 dojo.byId('vl-upload-status-count').innerHTML = '0';
195 dojo.byId('vl-ses-input').value = authtoken;
196 displayGlobalDiv('vl-marc-upload-status-div');
197 dojo.io.iframe.send({
201 form: dojo.byId('vl-marc-upload-form'),
202 handle: function(data,ioArgs){
203 var content = data.documentElement.textContent;
210 * Creates a new vandelay queue
212 function createQueue(queueName, type, onload) {
213 fieldmapper.standardRequest(
214 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.create'],
216 params: [authtoken, queueName, null, type],
217 oncomplete : function(r) {
218 var queue = r.recv().content();
219 if(e = openils.Event.parse(queue))
228 * Tells vendelay to pull a batch of records from the cache and explode them
229 * out into the vandelay tables
231 function processSpool(key, queueId, type, onload) {
232 fieldmapper.standardRequest(
233 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
235 params: [authtoken, key, queueId],
236 onresponse : function(r) {
237 var resp = r.recv().content();
238 if(e = openils.Event.parse(resp))
240 dojo.byId('vl-upload-status-count').innerHTML = resp;
242 oncomplete : function(r) {onload();}
247 function retrieveQueuedRecords(type, queueId, onload) {
248 displayGlobalDiv('vl-generic-progress');
250 queuedRecordsMap = {};
251 currentOverlayRecordsMap = {};
252 selectableGridRecords = {};
253 resetVlQueueGridLayout();
255 var method = 'open-ils.vandelay.'+type+'_queue.records.retrieve.atomic';
256 if(vlQueueGridShowMatches.checked)
257 method = method.replace('records', 'records.matches');
259 var limit = parseInt(vlQueueDisplayLimit.getValue());
260 var offset = limit * parseInt(vlQueueDisplayPage.getValue()-1);
262 fieldmapper.standardRequest(
263 ['open-ils.vandelay', method],
265 params: [authtoken, queueId,
271 /* intermittent bug in streaming, multipart requests prevents use of onreponse for now...
272 onresponse: function(r) {
273 var rec = r.recv().content();
274 if(e = openils.Event.parse(rec))
276 queuedRecords.push(rec);
277 queuedRecordsMap[rec.id()] = rec;
280 oncomplete: function(r){
281 var recs = r.recv().content();
282 if(e = openils.Event.parse(recs[0]))
284 for(var i = 0; i < recs.length; i++) {
286 queuedRecords.push(rec);
287 queuedRecordsMap[rec.id()] = rec;
295 function vlLoadMatchUI(recId, attrCode) {
296 displayGlobalDiv('vl-generic-progress');
297 var matches = getRecMatchesFromAttrCode(queuedRecordsMap[recId], attrCode);
299 currentImportRecId = recId;
300 for(var i = 0; i < matches.length; i++)
301 records.push(matches[i].eg_record());
303 var retrieve = ['open-ils.search', 'open-ils.search.biblio.record_entry.slim.retrieve'];
304 var params = [records];
305 if(currentType == 'auth') {
306 retrieve = ['open-ils.cat', 'open-ils.cat.authority.record.retrieve'];
307 parmas = [authtoken, records, {clear_marc:1}];
310 fieldmapper.standardRequest(
314 oncomplete: function(r) {
315 var recs = r.recv().content();
316 if(e = openils.Event.parse(recs))
320 displayGlobalDiv('vl-match-div');
321 resetVlMatchGridLayout();
322 currentMatchedRecords = recs;
323 vlMatchGrid.setStructure(vlMatchGridLayout);
325 // build the data store or records with match information
326 var dataStore = bre.toStoreData(recs, null, {virtualFields:['field_type']});
327 for(var i = 0; i < dataStore.items.length; i++) {
328 var item = dataStore.items[i];
329 for(var j = 0; j < matches.length; j++) {
330 var match = matches[j];
331 if(match.eg_record() == item.id)
332 item.field_type = match.field_type();
335 // now populate the grid
336 vlPopulateGrid(vlMatchGrid, dataStore);
342 function vlPopulateGrid(grid, data) {
343 var store = new dojo.data.ItemFileReadStore({data:data});
344 var model = new dojox.grid.data.DojoData(
345 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
346 grid.setModel(model);
351 function vlLoadMARCHtml(recId, inCat, oncomplete) {
352 dijit.byId('vl-marc-html-done-button').onClick = oncomplete;
353 displayGlobalDiv('vl-generic-progress');
355 var params = [recId, 1];
357 api = ['open-ils.search', 'open-ils.search.biblio.record.html'];
358 if(currentType == 'auth')
359 api = ['open-ils.search', 'open-ils.search.authority.to_html'];
361 params = [authtoken, recId];
362 api = ['open-ils.vandelay', 'open-ils.vandelay.queued_bib_record.html'];
363 if(currentType == 'auth')
364 api = ['open-ils.vandelay', 'open-ils.vandelay.queued_authority_record.html'];
366 fieldmapper.standardRequest(
370 oncomplete: function(r) {
371 displayGlobalDiv('vl-marc-html-div');
372 var html = r.recv().content();
373 dojo.byId('vl-marc-record-html').innerHTML = html;
381 * Given a record, an attribute definition code, and a matching record attribute,
382 * this will determine if there are any import matches and build the UI to
383 * represent those matches. If no matches exist, simply returns the attribute value
385 function buildAttrColumnUI(rec, attrCode, attr) {
386 var matches = getRecMatchesFromAttrCode(rec, attrCode);
387 if(matches.length > 0) { // found some matches
388 return '<div class="match_div">' +
389 '<a href="javascript:void(0);" onclick="vlLoadMatchUI('+
390 rec.id()+',\''+attrCode+'\');">'+
391 attr.attr_value() + ' ('+matches.length+')</a></div>';
394 return attr.attr_value();
397 function getRecMatchesFromAttrCode(rec, attrCode) {
399 var attr = getRecAttrFromCode(rec, attrCode);
400 for(var j = 0; j < rec.matches().length; j++) {
401 var match = rec.matches()[j];
402 if(match.matched_attr() == attr.id())
408 function getRecAttrFromCode(rec, attrCode) {
409 var defId = attrDefMap[attrCode];
410 var attrs = rec.attributes();
411 for(var i = 0; i < attrs.length; i++) {
413 if(attr.field() == defId)
419 function getAttrValue(rowIdx) {
420 var data = this.grid.model.getRow(rowIdx);
422 var attrCode = this.field.split('.')[1];
423 var rec = queuedRecordsMap[data.id];
424 var attr = getRecAttrFromCode(rec, attrCode);
426 return buildAttrColumnUI(rec, attrCode, attr);
430 function vlGetDateTimeField(rowIdx) {
431 data = this.grid.model.getRow(rowIdx);
433 if(!data[this.field]) return '';
434 var date = dojo.date.stamp.fromISOString(data[this.field]);
435 return dojo.date.locale.format(date, {selector:'date'});
438 function vlGetCreator(rowIdx) {
439 data = this.grid.model.getRow(rowIdx);
441 var id = data.creator;
443 return userCache[id].usrname();
444 var user = fieldmapper.standardRequest(
445 ['open-ils.actor', 'open-ils.actor.user.retrieve'], [authtoken, id]);
446 if(e = openils.Event.parse(user))
448 userCache[id] = user;
449 return user.usrname();
452 function vlGetViewMARC(rowIdx) {
453 data = this.grid.model.getRow(rowIdx);
455 return this.value.replace('RECID', data.id);
458 function vlGetOverlayTargetSelector(rowIdx) {
459 data = this.grid.model.getRow(rowIdx);
461 var value = this.value.replace('ID', data.id);
462 var overlay = currentOverlayRecordsMap[currentImportRecId];
463 if(overlay && overlay == data.id)
464 value = value.replace('/>', 'checked="checked"/>');
470 * see if the user has enabled overlays for the current match set and,
471 * if so, map the current import record to the overlay target.
473 function vlHandleOverlayTargetSelected() {
474 if(vlOverlayTargetEnable.checked) {
475 for(var i = 0; i < currentMatchedRecords.length; i++) {
476 var matchRecId = currentMatchedRecords[i].id();
477 if(dojo.byId('vl-overlay-target-'+matchRecId).checked) {
478 console.log("found overlay target " + matchRecId);
479 currentOverlayRecordsMap[currentImportRecId] = matchRecId;
480 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = true;
481 dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = 'overlay_selected';
486 delete currentOverlayRecordsMap[currentImportRecId];
487 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = false;
491 var vlQueueGridBuilt = false;
492 function buildRecordGrid(type) {
493 displayGlobalDiv('vl-queue-div');
495 currentOverlayRecordsMap = {};
497 if(!vlQueueGridBuilt) {
498 var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
499 for(var i = 0; i < defs.length; i++) {
501 attrDefMap[def.code()] = def.id();
503 name:def.description(),
504 field:'attr.' + def.code(),
506 selectableColumn:true
508 vlQueueGridLayout[0].cells[0].push(col);
510 vlQueueGridBuilt = true;
515 storeData = vqbr.toStoreData(queuedRecords);
517 storeData = vqar.toStoreData(queuedRecords);
519 var store = new dojo.data.ItemFileReadStore({data:storeData});
520 var model = new dojox.grid.data.DojoData(
521 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
523 vlQueueGrid.setModel(model);
524 if(vlQueueGridColumePicker)
525 vlQueueGrid.setStructure(vlQueueGridColumePicker.structure);
527 vlQueueGrid.setStructure(vlQueueGridLayout);
528 vlQueueGrid.update();
530 if(!vlQueueGridColumePicker) {
531 vlQueueGridColumePicker =
532 new openils.GridColumnPicker(vlQueueGridColumePickerDialog, vlQueueGrid);
536 function vlDeleteQueue(type, queueId, onload) {
537 fieldmapper.standardRequest(
538 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.delete'],
540 params: [authtoken, queueId],
541 oncomplete: function(r) {
542 var resp = r.recv().content();
543 if(e = openils.Event.parse(resp))
552 function vlQueueGridDrawSelectBox(rowIdx) {
553 var data = this.grid.model.getRow(rowIdx);
555 var domId = 'vl-record-list-selected-' +data.id;
556 selectableGridRecords[domId] = data.id;
557 return "<div><input type='checkbox' id='"+domId+"'/></div>";
560 function vlSelectAllQueueGridRecords() {
561 for(var id in selectableGridRecords)
562 dojo.byId(id).checked = true;
564 function vlSelectNoQueueGridRecords() {
565 for(var id in selectableGridRecords)
566 dojo.byId(id).checked = false;
568 function vlToggleQueueGridSelect() {
569 if(dojo.byId('vl-queue-grid-row-selector').checked)
570 vlSelectAllQueueGridRecords();
572 vlSelectNoQueueGridRecords();
575 var handleRetrieveRecords = function() {
576 buildRecordGrid(currentType);
579 function vlImportSelectedRecords() {
580 displayGlobalDiv('vl-generic-progress-with-total');
583 for(var id in selectableGridRecords) {
584 if(dojo.byId(id).checked) {
585 var recId = selectableGridRecords[id];
586 var rec = queuedRecordsMap[recId];
587 if(!rec.import_time())
592 fieldmapper.standardRequest(
593 ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
595 params: [authtoken, records, {overlay_map:currentOverlayRecordsMap}],
596 onresponse: function(r) {
597 var resp = r.recv().content();
598 if(e = openils.Event.parse(resp))
600 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
602 oncomplete: function() {
603 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
609 function vlImportRecordQueue(type, queueId, noMatchOnly, onload) {
610 displayGlobalDiv('vl-generic-progress-with-total');
611 var method = 'open-ils.vandelay.bib_queue.import';
613 method = method.replace('import', 'nomatch.import');
615 method = method.replace('bib', 'auth');
617 fieldmapper.standardRequest(
618 ['open-ils.vandelay', method],
620 params: [authtoken, queueId],
621 onresponse: function(r) {
622 var resp = r.recv().content();
623 if(e = openils.Event.parse(resp))
625 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
627 oncomplete: function() {onload();}
634 * Create queue, upload MARC, process spool, load the newly created queue
636 function batchUpload() {
637 var queueName = dijit.byId('vl-queue-name').getValue();
638 currentType = dijit.byId('vl-record-type').getValue();
640 var handleProcessSpool = function() {
641 console.log('records uploaded and spooled');
642 if(vlUploadQueueAutoImport.checked) {
643 vlImportRecordQueue(currentType, currentQueueId, true,
645 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
649 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
653 var handleUploadMARC = function(key) {
654 console.log('marc uploaded');
655 dojo.style(dojo.byId('vl-upload-status-processing'), 'display', 'block');
656 processSpool(key, currentQueueId, currentType, handleProcessSpool);
659 var handleCreateQueue = function(queue) {
660 console.log('queue created ' + queue.name());
661 currentQueueId = queue.id();
662 uploadMARC(handleUploadMARC);
665 if(vlUploadQueueSelector.getValue() && !queueName) {
666 currentQueueId = vlUploadQueueSelector.getValue();
667 console.log('adding records to existing queue ' + currentQueueId);
668 uploadMARC(handleUploadMARC);
670 createQueue(queueName, currentType, handleCreateQueue);
675 function vlFleshQueueSelect(selector, type) {
676 var data = (type == 'bib') ? vbq.toStoreData(allUserBibQueues) : vaq.toStoreData(allUserAuthQueues);
677 selector.store = new dojo.data.ItemFileReadStore({data:data});
678 selector.setValue(null);
679 selector.setDisplayedValue('');
681 selector.setValue(data[0].id());
684 function vlShowUploadForm() {
685 displayGlobalDiv('vl-marc-upload-div');
686 vlFleshQueueSelect(vlUploadQueueSelector, vlUploadRecordType.getValue());
689 function vlShowQueueSelect() {
690 displayGlobalDiv('vl-queue-select-div');
691 vlFleshQueueSelect(vlQueueSelectQueueList, vlQueueSelectType.getValue());
694 function vlFetchQueueFromForm() {
695 currentType = vlQueueSelectType.getValue();
696 currentQueueId = vlQueueSelectQueueList.getValue();
697 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
700 dojo.addOnLoad(vlInit);