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');
34 'vl-generic-progress',
35 'vl-generic-progress-with-total',
44 var VANDELAY_URL = '/vandelay';
46 var authAttrDefs = [];
47 var queuedRecords = [];
48 var queuedRecordsMap = {};
49 var bibAttrsFetched = false;
50 var authAttrsFetched = false;
51 var attrDefMap = {}; // maps attr def code names to attr def ids
53 var currentQueueId = null;
55 var currentMatchedRecords; // set of loaded matched bib records
56 var currentOverlayRecordsMap; // map of import record to overlay record
57 var currentImportRecId; // when analyzing matches, this is the current import record
60 var selectableGridRecords;
61 var cgi = new openils.CGI();
67 authtoken = dojo.cookie('ses') || cgi.param('ses');
68 var initNeeded = 4; // how many async responses do we need before we're init'd
69 var initCount = 0; // how many async reponses we've received
71 function checkInitDone() {
73 if(initCount == initNeeded)
77 // Fetch the bib and authority attribute definitions
78 fieldmapper.standardRequest(
79 ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
81 params: [authtoken, {id:{'!=':null}}],
82 onresponse: function(r) {
83 var def = r.recv().content();
84 if(e = openils.Event.parse(def[0]))
86 bibAttrDefs.push(def);
88 oncomplete: function() {
89 bibAttrDefs = bibAttrDefs.sort(
91 if(a.id() > b.id()) return 1;
92 if(a.id() < b.id()) return -1;
101 fieldmapper.standardRequest(
102 ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
104 params: [authtoken, {id:{'!=':null}}],
105 onresponse: function(r) {
106 var def = r.recv().content();
107 if(e = openils.Event.parse(def[0]))
109 authAttrDefs.push(def);
111 oncomplete: function() {
112 authAttrDefs = authAttrDefs.sort(
114 if(a.id() > b.id()) return 1;
115 if(a.id() < b.id()) return -1;
124 fieldmapper.standardRequest(
125 ['open-ils.vandelay', 'open-ils.vandelay.bib_queue.owner.retrieve.atomic'],
128 oncomplete: function(r) {
129 var list = r.recv().content();
130 if(e = openils.Event.parse(list[0]))
132 userBibQueues = list;
138 fieldmapper.standardRequest(
139 ['open-ils.vandelay', 'open-ils.vandelay.authority_queue.owner.retrieve.atomic'],
142 oncomplete: function(r) {
143 var list = r.recv().content();
144 if(e = openils.Event.parse(list[0]))
146 userAuthQueues = list;
153 function displayGlobalDiv(id) {
154 for(var i = 0; i < globalDivs.length; i++) {
156 dojo.style(dojo.byId(globalDivs[i]), 'display', 'none');
158 alert('please define ' + globalDivs[i]);
161 dojo.style(dojo.byId(id),'display','block');
164 function runStartupCommands() {
165 currentQueueId = cgi.param('qid');
166 currentType = cgi.param('qtype');
168 return retrievenueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
173 * asynchronously upload a file of MARC records
175 function uploadMARC(onload){
176 dojo.byId('vl-ses-input').value = authtoken;
177 dojo.style(dojo.byId('vl-input-td'),"display","none");
178 dojo.style(dojo.byId('vl-upload-progress-span'),"display","inline");
180 dojo.style(dojo.byId('vl-file-label'), 'display', 'none');
181 dojo.style(dojo.byId('vl-file-uploading'), 'display', 'inline');
183 dojo.io.iframe.send({
187 form: dojo.byId('vl-marc-upload-form'),
188 handle: function(data,ioArgs){
189 var content = data.documentElement.textContent;
190 dojo.style(dojo.byId('vl-input-td'),"display","inline");
191 dojo.style(dojo.byId('vl-upload-progress-span'),"display","none");
192 dojo.style(dojo.byId('vl-file-label'), 'display', 'inline');
193 dojo.style(dojo.byId('vl-file-uploading'), 'display', 'none');
200 * Creates a new vandelay queue
202 function createQueue(queueName, type, onload) {
203 fieldmapper.standardRequest(
204 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.create'],
206 params: [authtoken, queueName, null, type],
207 oncomplete : function(r) {
208 var queue = r.recv().content();
209 if(e = openils.Event.parse(queue))
218 * Tells vendelay to pull a batch of records from the cache and explode them
219 * out into the vandelay tables
221 function processSpool(key, queueId, type, onload) {
222 fieldmapper.standardRequest(
223 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
225 params: [authtoken, key, queueId],
226 oncomplete : function(r) {
227 var resp = r.recv().content();
228 if(e = openils.Event.parse(resp))
236 function retrieveQueuedRecords(type, queueId, onload) {
237 displayGlobalDiv('vl-generic-progress');
239 queuedRecordsMap = {};
240 currentOverlayRecordsMap = {};
241 selectableGridRecords = {};
242 resetVlQueueGridLayout();
244 fieldmapper.standardRequest(
245 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.records.retrieve.atomic'],
247 params: [authtoken, queueId, {clear_marc:1}],
248 /* intermittent bug in streaming, multipart requests prevents use of onreponse for now...
249 onresponse: function(r) {
250 var rec = r.recv().content();
251 if(e = openils.Event.parse(rec))
253 queuedRecords.push(rec);
254 queuedRecordsMap[rec.id()] = rec;
257 oncomplete: function(r){
258 var recs = r.recv().content();
259 if(e = openils.Event.parse(recs[0]))
261 for(var i = 0; i < recs.length; i++) {
263 queuedRecords.push(rec);
264 queuedRecordsMap[rec.id()] = rec;
272 function vlLoadMatchUI(recId, attrCode) {
273 displayGlobalDiv('vl-generic-progress');
274 var matches = getRecMatchesFromAttrCode(queuedRecordsMap[recId], attrCode);
276 currentImportRecId = recId;
277 for(var i = 0; i < matches.length; i++)
278 records.push(matches[i].eg_record());
279 fieldmapper.standardRequest(
280 ['open-ils.search', 'open-ils.search.biblio.record_entry.slim.retrieve'],
283 oncomplete: function(r) {
284 var recs = r.recv().content();
285 if(e = openils.Event.parse(recs))
287 displayGlobalDiv('vl-match-div');
288 resetVlMatchGridLayout();
289 currentMatchedRecords = recs;
290 vlMatchGrid.setStructure(vlMatchGridLayout);
291 var dataStore = bre.toStoreData(recs, null, {virtualFields:['field_type']});
292 for(var i = 0; i < dataStore.items.length; i++) {
293 var item = dataStore.items[i];
294 for(var j = 0; j < matches.length; j++) {
295 var match = matches[j];
296 if(match.eg_record() == item.id)
297 item.field_type = match.field_type();
300 var store = new dojo.data.ItemFileReadStore({data:dataStore});
301 var model = new dojox.grid.data.DojoData(
302 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
303 vlMatchGrid.setModel(model);
304 vlMatchGrid.update();
311 function vlLoadMARCHtml(recId) {
312 displayGlobalDiv('vl-generic-progress');
313 fieldmapper.standardRequest(
314 ['open-ils.search', 'open-ils.search.biblio.record.html'],
317 oncomplete: function(r) {
318 displayGlobalDiv('vl-match-html-div');
319 var html = r.recv().content();
320 dojo.byId('vl-match-record-html').innerHTML = html;
328 * Given a record, an attribute definition code, and a matching record attribute,
329 * this will determine if there are any import matches and build the UI to
330 * represent those matches. If no matches exist, simply returns the attribute value
332 function buildAttrColumnUI(rec, attrCode, attr) {
333 var matches = getRecMatchesFromAttrCode(rec, attrCode);
334 if(matches.length > 0) { // found some matches
335 return '<div class="match_div">' +
336 '<a href="javascript:void(0);" onclick="vlLoadMatchUI('+
337 rec.id()+',\''+attrCode+'\');">'+
338 attr.attr_value() + ' ('+matches.length+')</a></div>';
341 return attr.attr_value();
344 function getRecMatchesFromAttrCode(rec, attrCode) {
346 var attr = getRecAttrFromCode(rec, attrCode);
347 for(var j = 0; j < rec.matches().length; j++) {
348 var match = rec.matches()[j];
349 if(match.matched_attr() == attr.id())
355 function getRecAttrFromCode(rec, attrCode) {
356 var defId = attrDefMap[attrCode];
357 var attrs = rec.attributes();
358 for(var i = 0; i < attrs.length; i++) {
360 if(attr.field() == defId)
366 function getAttrValue(rowIdx) {
367 var data = this.grid.model.getRow(rowIdx);
369 var attrCode = this.field.split('.')[1];
370 console.log(attrCode + " : " + data.id + ' : ' + queuedRecordsMap[data.id] + " : count = " + queuedRecords.length);
371 var rec = queuedRecordsMap[data.id];
372 var attr = getRecAttrFromCode(rec, attrCode);
374 return buildAttrColumnUI(rec, attrCode, attr);
378 function vlGetDateTimeField(rowIdx) {
379 data = this.grid.model.getRow(rowIdx);
381 if(!data[this.field]) return '';
382 var date = dojo.date.stamp.fromISOString(data[this.field]);
383 return dojo.date.locale.format(date, {selector:'date'});
386 function vlGetCreator(rowIdx) {
387 data = this.grid.model.getRow(rowIdx);
389 var id = data.creator;
391 return userCache[id].usrname();
392 var user = fieldmapper.standardRequest(['open-ils.actor', 'open-ils.actor.user.retrieve'], [authtoken, id]);
393 if(e = openils.Event.parse(user))
395 userCache[id] = user;
396 return user.usrname();
399 function vlGetViewMARC(rowIdx) {
400 data = this.grid.model.getRow(rowIdx);
402 return this.value.replace('RECID', data.id);
405 function vlGetOverlayTargetSelector(rowIdx) {
406 data = this.grid.model.getRow(rowIdx);
408 var value = this.value.replace('ID', data.id);
409 var overlay = currentOverlayRecordsMap[currentImportRecId];
410 if(overlay && overlay == data.id)
411 value = value.replace('/>', 'checked="checked"/>');
417 * see if the user has enabled overlays for the current match set and,
418 * if so, map the current import record to the overlay target.
420 function vlHandleOverlayTargetSelected() {
421 if(vlOverlayTargetEnable.checked) {
422 for(var i = 0; i < currentMatchedRecords.length; i++) {
423 var matchRecId = currentMatchedRecords[i].id();
424 if(dojo.byId('vl-overlay-target-'+matchRecId).checked) {
425 console.log("found overlay target " + matchRecId);
426 currentOverlayRecordsMap[currentImportRecId] = matchRecId;
427 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = true;
428 dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = 'overlay_selected';
433 delete currentOverlayRecordsMap[currentImportRecId];
434 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = false;
438 function buildRecordGrid(type) {
439 displayGlobalDiv('vl-queue-div');
441 currentOverlayRecordsMap = {};
443 if(queuedRecords.length == 0) {
444 dojo.style(dojo.byId('vl-queue-no-records'), 'display', 'block');
445 dojo.style(dojo.byId('vl-queue-div-grid'), 'display', 'none');
448 dojo.style(dojo.byId('vl-queue-no-records'), 'display', 'none');
449 dojo.style(dojo.byId('vl-queue-div-grid'), 'display', 'block');
452 var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
453 for(var i = 0; i < defs.length; i++) {
455 attrDefMap[def.code()] = def.id();
457 name:def.description(),
458 field:'attr.' + def.code(),
461 //if(def.code().match(/title/i)) col.width = 'auto'; // this is hack.
462 vlQueueGridLayout[0].cells[0].push(col);
467 storeData = vqbr.toStoreData(queuedRecords);
469 storeData = vqar.toStoreData(queuedRecords);
471 var store = new dojo.data.ItemFileReadStore({data:storeData});
472 var model = new dojox.grid.data.DojoData(
473 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
475 vlQueueGrid.setModel(model);
476 vlQueueGrid.setStructure(vlQueueGridLayout);
477 vlQueueGrid.update();
482 alert(vlQueueGridLayout.picker);
483 vlQueueGridLayout.oils = {};
484 vlQueueGridLayout[0].cells[0].pop();
485 vlQueueGridLayout[0].cells[0].pop();
486 vlQueueGridLayout[0].cells[0].pop();
487 vlQueueGridLayout[0].cells[0].pop();
488 vlQueueGridLayout[0].cells[0].pop();
489 vlQueueGrid.setStructure(vlQueueGridLayout);
490 vlQueueGrid.update();
494 function vlQueueGridDrawSelectBox(rowIdx) {
495 var data = this.grid.model.getRow(rowIdx);
497 var domId = 'vl-record-list-selected-' +data.id;
498 selectableGridRecords[domId] = data.id;
499 return "<div><input type='checkbox' id='"+domId+"'/></div>";
502 function vlSelectAllGridRecords() {
503 for(var id in selectableGridRecords)
504 dojo.byId(id).checked = true;
506 function vlSelectNoGridRecords() {
507 for(var id in selectableGridRecords)
508 dojo.byId(id).checked = false;
511 var handleRetrieveRecords = function() {
512 buildRecordGrid(currentType);
515 function vlImportSelectedRecords() {
516 displayGlobalDiv('vl-generic-progress-with-total');
519 for(var id in selectableGridRecords) {
520 if(dojo.byId(id).checked) {
521 var recId = selectableGridRecords[id];
522 var rec = queuedRecordsMap[recId];
523 if(!rec.import_time())
528 fieldmapper.standardRequest(
529 ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
531 params: [authtoken, records, {overlay_map:currentOverlayRecordsMap}],
532 onresponse: function(r) {
533 var resp = r.recv().content();
534 if(e = openils.Event.parse(resp))
536 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
538 oncomplete: function() {
539 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
547 * Create queue, upload MARC, process spool, load the newly created queue
549 function batchUpload() {
550 var queueName = dijit.byId('vl-queue-name').getValue();
551 currentType = dijit.byId('vl-record-type').getValue();
553 var handleProcessSpool = function() {
554 console.log('records uploaded and spooled');
555 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
558 var handleUploadMARC = function(key) {
559 console.log('marc uploaded');
560 processSpool(key, currentQueueId, currentType, handleProcessSpool);
563 var handleCreateQueue = function(queue) {
564 console.log('queue created ' + queue.name());
565 currentQueueId = queue.id();
566 uploadMARC(handleUploadMARC);
569 if(vlUploadQueueSelector.getValue() && !queueName) {
570 currentQueueId = vlUploadQueueSelector.getValue();
571 console.log('adding records to existing queue ' + currentQueueId);
572 uploadMARC(handleUploadMARC);
574 createQueue(queueName, currentType, handleCreateQueue);
579 function vlFleshQueueSelect(selector, type) {
580 var data = (type == 'bib') ? vbq.toStoreData(userBibQueues) : vaq.toStoreData(userAuthQueues);
581 selector.store = new dojo.data.ItemFileReadStore({data:data});
582 selector.setValue(null);
583 selector.setDisplayedValue('');
585 selector.setValue(data[0].id());
588 function vlShowUploadForm() {
589 displayGlobalDiv('vl-marc-upload-div');
590 vlFleshQueueSelect(vlUploadQueueSelector, vlUploadRecordType.getValue());
593 function vlShowQueueSelect() {
594 displayGlobalDiv('vl-queue-select-div');
595 vlFleshQueueSelect(vlQueueSelectQueueList, vlQueueSelectType.getValue());
598 function vlFetchQueueFromForm() {
599 currentType = vlQueueSelectType.getValue();
600 currentQueueId = vlQueueSelectQueueList.getValue();
601 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
604 dojo.addOnLoad(vlInit);