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.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');
35 'vl-generic-progress',
36 'vl-generic-progress-with-total',
41 'vl-queue-select-div',
42 'vl-marc-upload-status-div'
46 var VANDELAY_URL = '/vandelay-upload';
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
55 var currentQueueId = null;
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
62 var selectableGridRecords;
63 var cgi = new openils.CGI();
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
73 function checkInitDone() {
75 if(initCount == initNeeded)
79 // Fetch the bib and authority attribute definitions
80 fieldmapper.standardRequest(
81 ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
83 params: [authtoken, {id:{'!=':null}}],
84 onresponse: function(r) {
85 var def = r.recv().content();
86 if(e = openils.Event.parse(def[0]))
88 bibAttrDefs.push(def);
90 oncomplete: function() {
91 bibAttrDefs = bibAttrDefs.sort(
93 if(a.id() > b.id()) return 1;
94 if(a.id() < b.id()) return -1;
103 fieldmapper.standardRequest(
104 ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
106 params: [authtoken, {id:{'!=':null}}],
107 onresponse: function(r) {
108 var def = r.recv().content();
109 if(e = openils.Event.parse(def[0]))
111 authAttrDefs.push(def);
113 oncomplete: function() {
114 authAttrDefs = authAttrDefs.sort(
116 if(a.id() > b.id()) return 1;
117 if(a.id() < b.id()) return -1;
126 fieldmapper.standardRequest(
127 ['open-ils.vandelay', 'open-ils.vandelay.bib_queue.owner.retrieve.atomic'],
130 oncomplete: function(r) {
131 var list = r.recv().content();
132 if(e = openils.Event.parse(list[0]))
134 userBibQueues = list;
140 fieldmapper.standardRequest(
141 ['open-ils.vandelay', 'open-ils.vandelay.authority_queue.owner.retrieve.atomic'],
144 oncomplete: function(r) {
145 var list = r.recv().content();
146 if(e = openils.Event.parse(list[0]))
148 userAuthQueues = list;
155 function displayGlobalDiv(id) {
156 for(var i = 0; i < globalDivs.length; i++) {
158 dojo.style(dojo.byId(globalDivs[i]), 'display', 'none');
160 alert('please define div ' + globalDivs[i]);
163 dojo.style(dojo.byId(id),'display','block');
166 function runStartupCommands() {
167 currentQueueId = cgi.param('qid');
168 currentType = cgi.param('qtype');
170 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
175 * asynchronously upload a file of MARC records
177 function uploadMARC(onload){
178 dojo.byId('vl-ses-input').value = authtoken;
179 displayGlobalDiv('vl-marc-upload-status-div');
180 dojo.io.iframe.send({
184 form: dojo.byId('vl-marc-upload-form'),
185 handle: function(data,ioArgs){
186 var content = data.documentElement.textContent;
193 * Creates a new vandelay queue
195 function createQueue(queueName, type, onload) {
196 fieldmapper.standardRequest(
197 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.create'],
199 params: [authtoken, queueName, null, type],
200 oncomplete : function(r) {
201 var queue = r.recv().content();
202 if(e = openils.Event.parse(queue))
211 * Tells vendelay to pull a batch of records from the cache and explode them
212 * out into the vandelay tables
214 function processSpool(key, queueId, type, onload) {
215 fieldmapper.standardRequest(
216 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
218 params: [authtoken, key, queueId],
219 oncomplete : function(r) {
220 var resp = r.recv().content();
221 if(e = openils.Event.parse(resp))
229 function retrieveQueuedRecords(type, queueId, onload) {
230 displayGlobalDiv('vl-generic-progress');
232 queuedRecordsMap = {};
233 currentOverlayRecordsMap = {};
234 selectableGridRecords = {};
235 resetVlQueueGridLayout();
237 var method = 'open-ils.vandelay.'+type+'_queue.records.retrieve.atomic';
238 if(vlQueueGridShowMatches.checked)
239 method = method.replace('records', 'records.matches');
241 fieldmapper.standardRequest(
242 ['open-ils.vandelay', method],
244 params: [authtoken, queueId, {clear_marc:1}],
245 /* intermittent bug in streaming, multipart requests prevents use of onreponse for now...
246 onresponse: function(r) {
247 var rec = r.recv().content();
248 if(e = openils.Event.parse(rec))
250 queuedRecords.push(rec);
251 queuedRecordsMap[rec.id()] = rec;
254 oncomplete: function(r){
255 var recs = r.recv().content();
256 if(e = openils.Event.parse(recs[0]))
258 for(var i = 0; i < recs.length; i++) {
260 queuedRecords.push(rec);
261 queuedRecordsMap[rec.id()] = rec;
269 function vlLoadMatchUI(recId, attrCode) {
270 displayGlobalDiv('vl-generic-progress');
271 var matches = getRecMatchesFromAttrCode(queuedRecordsMap[recId], attrCode);
273 currentImportRecId = recId;
274 for(var i = 0; i < matches.length; i++)
275 records.push(matches[i].eg_record());
277 var retrieve = ['open-ils.search', 'open-ils.search.biblio.record_entry.slim.retrieve'];
278 var params = [records];
279 if(currentType == 'auth') {
280 retrieve = ['open-ils.cat', 'open-ils.cat.authority.record.retrieve'];
281 parmas = [authtoken, records, {clear_marc:1}];
284 fieldmapper.standardRequest(
288 oncomplete: function(r) {
289 var recs = r.recv().content();
290 if(e = openils.Event.parse(recs))
294 displayGlobalDiv('vl-match-div');
295 resetVlMatchGridLayout();
296 currentMatchedRecords = recs;
297 vlMatchGrid.setStructure(vlMatchGridLayout);
299 // build the data store or records with match information
300 var dataStore = bre.toStoreData(recs, null, {virtualFields:['field_type']});
301 for(var i = 0; i < dataStore.items.length; i++) {
302 var item = dataStore.items[i];
303 for(var j = 0; j < matches.length; j++) {
304 var match = matches[j];
305 if(match.eg_record() == item.id)
306 item.field_type = match.field_type();
309 // now populate the grid
310 vlPopulateGrid(vlMatchGrid, dataStore);
316 function vlPopulateGrid(grid, data) {
317 var store = new dojo.data.ItemFileReadStore({data:data});
318 var model = new dojox.grid.data.DojoData(
319 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
320 grid.setModel(model);
325 function vlLoadMARCHtml(recId) {
326 displayGlobalDiv('vl-generic-progress');
327 var api = ['open-ils.search', 'open-ils.search.biblio.record.html'];
328 if(currentType == 'auth')
329 api = ['open-ils.search', 'open-ils.search.authority.to_html'];
330 fieldmapper.standardRequest(
334 oncomplete: function(r) {
335 displayGlobalDiv('vl-match-html-div');
336 var html = r.recv().content();
337 dojo.byId('vl-match-record-html').innerHTML = html;
345 * Given a record, an attribute definition code, and a matching record attribute,
346 * this will determine if there are any import matches and build the UI to
347 * represent those matches. If no matches exist, simply returns the attribute value
349 function buildAttrColumnUI(rec, attrCode, attr) {
350 var matches = getRecMatchesFromAttrCode(rec, attrCode);
351 if(matches.length > 0) { // found some matches
352 return '<div class="match_div">' +
353 '<a href="javascript:void(0);" onclick="vlLoadMatchUI('+
354 rec.id()+',\''+attrCode+'\');">'+
355 attr.attr_value() + ' ('+matches.length+')</a></div>';
358 return attr.attr_value();
361 function getRecMatchesFromAttrCode(rec, attrCode) {
363 var attr = getRecAttrFromCode(rec, attrCode);
364 for(var j = 0; j < rec.matches().length; j++) {
365 var match = rec.matches()[j];
366 if(match.matched_attr() == attr.id())
372 function getRecAttrFromCode(rec, attrCode) {
373 var defId = attrDefMap[attrCode];
374 var attrs = rec.attributes();
375 for(var i = 0; i < attrs.length; i++) {
377 if(attr.field() == defId)
383 function getAttrValue(rowIdx) {
384 var data = this.grid.model.getRow(rowIdx);
386 var attrCode = this.field.split('.')[1];
387 var rec = queuedRecordsMap[data.id];
388 var attr = getRecAttrFromCode(rec, attrCode);
390 return buildAttrColumnUI(rec, attrCode, attr);
394 function vlGetDateTimeField(rowIdx) {
395 data = this.grid.model.getRow(rowIdx);
397 if(!data[this.field]) return '';
398 var date = dojo.date.stamp.fromISOString(data[this.field]);
399 return dojo.date.locale.format(date, {selector:'date'});
402 function vlGetCreator(rowIdx) {
403 data = this.grid.model.getRow(rowIdx);
405 var id = data.creator;
407 return userCache[id].usrname();
408 var user = fieldmapper.standardRequest(
409 ['open-ils.actor', 'open-ils.actor.user.retrieve'], [authtoken, id]);
410 if(e = openils.Event.parse(user))
412 userCache[id] = user;
413 return user.usrname();
416 function vlGetViewMARC(rowIdx) {
417 data = this.grid.model.getRow(rowIdx);
419 return this.value.replace('RECID', data.id);
422 function vlGetOverlayTargetSelector(rowIdx) {
423 data = this.grid.model.getRow(rowIdx);
425 var value = this.value.replace('ID', data.id);
426 var overlay = currentOverlayRecordsMap[currentImportRecId];
427 if(overlay && overlay == data.id)
428 value = value.replace('/>', 'checked="checked"/>');
434 * see if the user has enabled overlays for the current match set and,
435 * if so, map the current import record to the overlay target.
437 function vlHandleOverlayTargetSelected() {
438 if(vlOverlayTargetEnable.checked) {
439 for(var i = 0; i < currentMatchedRecords.length; i++) {
440 var matchRecId = currentMatchedRecords[i].id();
441 if(dojo.byId('vl-overlay-target-'+matchRecId).checked) {
442 console.log("found overlay target " + matchRecId);
443 currentOverlayRecordsMap[currentImportRecId] = matchRecId;
444 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = true;
445 dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = 'overlay_selected';
450 delete currentOverlayRecordsMap[currentImportRecId];
451 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = false;
455 function buildRecordGrid(type) {
456 displayGlobalDiv('vl-queue-div');
458 currentOverlayRecordsMap = {};
460 if(queuedRecords.length == 0) {
461 dojo.style(dojo.byId('vl-queue-no-records'), 'display', 'block');
462 dojo.style(dojo.byId('vl-queue-div-grid'), 'display', 'none');
465 dojo.style(dojo.byId('vl-queue-no-records'), 'display', 'none');
466 dojo.style(dojo.byId('vl-queue-div-grid'), 'display', 'block');
469 var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
470 for(var i = 0; i < defs.length; i++) {
472 attrDefMap[def.code()] = def.id();
474 name:def.description(),
475 field:'attr.' + def.code(),
478 //if(def.code().match(/title/i)) col.width = 'auto'; // this is hack.
479 vlQueueGridLayout[0].cells[0].push(col);
484 storeData = vqbr.toStoreData(queuedRecords);
486 storeData = vqar.toStoreData(queuedRecords);
488 var store = new dojo.data.ItemFileReadStore({data:storeData});
489 var model = new dojox.grid.data.DojoData(
490 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
492 vlQueueGrid.setModel(model);
493 vlQueueGrid.setStructure(vlQueueGridLayout);
494 vlQueueGrid.update();
499 alert(vlQueueGridLayout.picker);
500 vlQueueGridLayout.oils = {};
501 vlQueueGridLayout[0].cells[0].pop();
502 vlQueueGridLayout[0].cells[0].pop();
503 vlQueueGridLayout[0].cells[0].pop();
504 vlQueueGridLayout[0].cells[0].pop();
505 vlQueueGridLayout[0].cells[0].pop();
506 vlQueueGrid.setStructure(vlQueueGridLayout);
507 vlQueueGrid.update();
511 function vlQueueGridDrawSelectBox(rowIdx) {
512 var data = this.grid.model.getRow(rowIdx);
514 var domId = 'vl-record-list-selected-' +data.id;
515 selectableGridRecords[domId] = data.id;
516 return "<div><input type='checkbox' id='"+domId+"'/></div>";
519 function vlSelectAllGridRecords() {
520 for(var id in selectableGridRecords)
521 dojo.byId(id).checked = true;
523 function vlSelectNoGridRecords() {
524 for(var id in selectableGridRecords)
525 dojo.byId(id).checked = false;
528 var handleRetrieveRecords = function() {
529 buildRecordGrid(currentType);
532 function vlImportSelectedRecords() {
533 displayGlobalDiv('vl-generic-progress-with-total');
536 for(var id in selectableGridRecords) {
537 if(dojo.byId(id).checked) {
538 var recId = selectableGridRecords[id];
539 var rec = queuedRecordsMap[recId];
540 if(!rec.import_time())
545 fieldmapper.standardRequest(
546 ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
548 params: [authtoken, records, {overlay_map:currentOverlayRecordsMap}],
549 onresponse: function(r) {
550 var resp = r.recv().content();
551 if(e = openils.Event.parse(resp))
553 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
555 oncomplete: function() {
556 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
564 * Create queue, upload MARC, process spool, load the newly created queue
566 function batchUpload() {
567 var queueName = dijit.byId('vl-queue-name').getValue();
568 currentType = dijit.byId('vl-record-type').getValue();
570 var handleProcessSpool = function() {
571 console.log('records uploaded and spooled');
572 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
575 var handleUploadMARC = function(key) {
576 console.log('marc uploaded');
577 dojo.style(dojo.byId('vl-upload-status-processing'), 'display', 'block');
578 processSpool(key, currentQueueId, currentType, handleProcessSpool);
581 var handleCreateQueue = function(queue) {
582 console.log('queue created ' + queue.name());
583 currentQueueId = queue.id();
584 uploadMARC(handleUploadMARC);
587 if(vlUploadQueueSelector.getValue() && !queueName) {
588 currentQueueId = vlUploadQueueSelector.getValue();
589 console.log('adding records to existing queue ' + currentQueueId);
590 uploadMARC(handleUploadMARC);
592 createQueue(queueName, currentType, handleCreateQueue);
597 function vlFleshQueueSelect(selector, type) {
598 var data = (type == 'bib') ? vbq.toStoreData(userBibQueues) : vaq.toStoreData(userAuthQueues);
599 selector.store = new dojo.data.ItemFileReadStore({data:data});
600 selector.setValue(null);
601 selector.setDisplayedValue('');
603 selector.setValue(data[0].id());
606 function vlShowUploadForm() {
607 displayGlobalDiv('vl-marc-upload-div');
608 vlFleshQueueSelect(vlUploadQueueSelector, vlUploadRecordType.getValue());
611 function vlShowQueueSelect() {
612 displayGlobalDiv('vl-queue-select-div');
613 vlFleshQueueSelect(vlQueueSelectQueueList, vlQueueSelectType.getValue());
616 function vlFetchQueueFromForm() {
617 currentType = vlQueueSelectType.getValue();
618 currentQueueId = vlQueueSelectQueueList.getValue();
619 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
622 dojo.addOnLoad(vlInit);