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',
43 var VANDELAY_URL = '/vandelay';
45 var authAttrDefs = [];
46 var queuedRecords = [];
47 var queuedRecordsMap = {};
48 var bibAttrsFetched = false;
49 var authAttrsFetched = false;
52 var cgi = new openils.CGI();
53 var currentQueueId = null;
60 authtoken = dojo.cookie('ses') || cgi.param('ses');
61 bibAttrsFetched = false;
62 authAttrsFetched = false;
64 // Fetch the bib and authority attribute definitions
65 fieldmapper.standardRequest(
66 ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
68 params: [authtoken, {id:{'!=':null}}],
69 onresponse: function(r) {
70 var def = r.recv().content();
71 if(e = openils.Event.parse(def[0]))
73 bibAttrDefs.push(def);
75 oncomplete: function() {
76 bibAttrsFetched = true;
77 bibAttrDefs = bibAttrDefs.sort(
79 if(a.description() > b.description()) return 1;
80 if(a.description() < b.description()) return -1;
90 fieldmapper.standardRequest(
91 ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
93 params: [authtoken, {id:{'!=':null}}],
94 onresponse: function(r) {
95 var def = r.recv().content();
96 if(e = openils.Event.parse(def[0]))
98 authAttrDefs.push(def);
100 oncomplete: function() {
101 authAttrsFetched = true;
102 authAttrDefs = authAttrDefs.sort(
104 if(a.description() > b.description()) return 1;
105 if(a.description() < b.description()) return -1;
110 runStartupCommands();
116 function displayGlobalDiv(id) {
117 for(var i = 0; i < globalDivs.length; i++)
118 dojo.style(dojo.byId(globalDivs[i]), 'display', 'none');
119 dojo.style(dojo.byId(id),'display','block');
122 function runStartupCommands() {
123 currentQueueId = cgi.param('qid');
124 currentType = cgi.param('qtype');
126 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
127 displayGlobalDiv('vl-marc-upload-div');
131 * asynchronously upload a file of MARC records
133 function uploadMARC(onload){
134 dojo.byId('vl-ses-input').value = authtoken;
135 dojo.style(dojo.byId('vl-input-td'),"display","none");
136 dojo.style(dojo.byId('vl-upload-progress-span'),"display","inline");
138 dojo.style(dojo.byId('vl-file-label'), 'display', 'none');
139 dojo.style(dojo.byId('vl-file-uploading'), 'display', 'inline');
141 dojo.io.iframe.send({
145 form: dojo.byId('vl-marc-upload-form'),
146 handle: function(data,ioArgs){
147 var content = data.documentElement.textContent;
148 var key = content.split(/\n/)[2]; /* XXX have to strip the headers.. (why?) */
149 dojo.style(dojo.byId('vl-input-td'),"display","inline");
150 dojo.style(dojo.byId('vl-upload-progress-span'),"display","none");
151 dojo.style(dojo.byId('vl-file-label'), 'display', 'inline');
152 dojo.style(dojo.byId('vl-file-uploading'), 'display', 'none');
159 * Creates a new vandelay queue
161 function createQueue(queueName, type, onload) {
162 fieldmapper.standardRequest(
163 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.create'],
165 params: [authtoken, queueName, null, type],
166 oncomplete : function(r) {
167 var queue = r.recv().content();
168 if(e = openils.Event.parse(queue))
177 * Tells vendelay to pull a batch of records from the cache and explode them
178 * out into the vandelay tables
180 function processSpool(key, queueId, type, onload) {
181 fieldmapper.standardRequest(
182 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
184 params: [authtoken, key, queueId],
185 oncomplete : function(r) {
186 var resp = r.recv().content();
187 if(e = openils.Event.parse(resp))
195 function retrieveQueuedRecords(type, queueId, onload) {
197 queuedRecordsMap = {};
198 resetVlQueueGridLayout();
199 fieldmapper.standardRequest(
200 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.records.retrieve.atomic'],
202 params: [authtoken, queueId, {clear_marc:1}],
203 /* intermittent bug in streaming, multipart requests prevents use of onreponse for now...
204 onresponse: function(r) {
205 var rec = r.recv().content();
206 if(e = openils.Event.parse(rec))
208 queuedRecords.push(rec);
209 queuedRecordsMap[rec.id()] = rec;
212 oncomplete: function(r){
213 var recs = r.recv().content();
214 if(e = openils.Event.parse(recs[0]))
216 for(var i = 0; i < recs.length; i++) {
218 queuedRecords.push(rec);
219 queuedRecordsMap[rec.id()] = rec;
227 function vlLoadMatchUI(recId, attrCode) {
228 displayGlobalDiv('vl-generic-progress');
229 var matches = getRecMatchesFromAttrCode(queuedRecordsMap[recId], attrCode);
231 for(var i = 0; i < matches.length; i++)
232 records.push(matches[i].eg_record());
233 fieldmapper.standardRequest(
234 ['open-ils.search', 'open-ils.search.biblio.record_entry.slim.retrieve'],
237 oncomplete: function(r) {
238 var recs = r.recv().content();
239 if(e = openils.Event.parse(recs))
241 displayGlobalDiv('vl-match-div');
242 resetVlMatchGridLayout();
243 vlMatchGrid.setStructure(vlMatchGridLayout);
244 var store = new dojo.data.ItemFileReadStore({data:bre.toStoreData(recs)});
245 var model = new dojox.grid.data.DojoData(
246 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
247 vlMatchGrid.setModel(model);
248 vlMatchGrid.update();
254 function vlLoadMARCHtml(recId) {
255 displayGlobalDiv('vl-generic-progress');
256 fieldmapper.standardRequest(
257 ['open-ils.search', 'open-ils.search.biblio.record.html'],
260 oncomplete: function(r) {
261 displayGlobalDiv('vl-match-html-div');
262 var html = r.recv().content();
263 dojo.byId('vl-match-record-html').innerHTML = html;
271 * Given a record, an attribute definition code, and a matching record attribute,
272 * this will determine if there are any import matches and build the UI to
273 * represent those matches. If no matches exist, simply returns the attribute value
275 function buildAttrColumnUI(rec, attrCode, attr) {
276 var matches = getRecMatchesFromAttrCode(rec, attrCode);
277 if(matches.length > 0) { // found some matches
278 return '<div class="match_div">' +
279 '<a href="javascript:void(0);" onclick="vlLoadMatchUI('+
280 rec.id()+',\''+matches[0].field_type()+'\');">'+
281 attr.attr_value() + ' ('+matches.length+')</a></div>';
284 return attr.attr_value();
287 function getRecMatchesFromAttrCode(rec, attrCode) {
289 for(var j = 0; j < rec.matches().length; j++) {
290 var match = rec.matches()[j];
291 if(match.field_type() == attrCode)
297 function getRecAttrFromCode(rec, attrCode) {
298 var defId = attrMap[attrCode];
299 var attrs = rec.attributes();
300 for(var i = 0; i < attrs.length; i++) {
302 if(attr.field() == defId)
308 function getAttrValue(rowIdx) {
309 var data = this.grid.model.getRow(rowIdx);
311 var attrCode = this.field.split('.')[1];
312 var rec = queuedRecordsMap[data.id];
313 var attr = getRecAttrFromCode(rec, attrCode);
314 console.log('attr = ' + attr);
316 return buildAttrColumnUI(rec, attrCode, attr);
320 function vlGetDateTimeField(rowIdx) {
321 data = this.grid.model.getRow(rowIdx);
323 if(!data[this.field]) return '';
324 var date = dojo.date.stamp.fromISOString(data[this.field]);
325 return dojo.date.locale.format(date, {selector:'date'});
328 function vlGetCreator(rowIdx) {
329 data = this.grid.model.getRow(rowIdx);
331 var id = data.creator;
333 return userCache[id].usrname();
334 var user = fieldmapper.standardRequest(['open-ils.actor', 'open-ils.actor.user.retrieve'], [authtoken, id]);
335 if(e = openils.Event.parse(user))
337 userCache[id] = user;
338 return user.usrname();
341 function vlGetViewMARC(rowIdx) {
342 data = this.grid.model.getRow(rowIdx);
344 return this.value.replace('RECID', data.id);
347 function buildRecordGrid(type) {
348 displayGlobalDiv('vl-queue-div');
350 var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
351 for(var i = 0; i < defs.length; i++) {
353 attrMap[attr.code()] = attr.id();
355 name:attr.description(),
356 field:'attr.' + attr.code(),
359 //if(attr.code().match(/title/i)) col.width = 'auto'; // this is hack.
360 vlQueueGridLayout[0].cells[0].push(col);
363 vlQueueGrid.setStructure(vlQueueGridLayout);
367 storeData = vqbr.toStoreData(queuedRecords);
369 storeData = vqar.toStoreData(queuedRecords);
371 var store = new dojo.data.ItemFileReadStore({data:storeData});
372 var model = new dojox.grid.data.DojoData(
373 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
375 vlQueueGrid.setModel(model);
376 vlQueueGrid.update();
379 var selectableGridRecords = {};
380 function vlQueueGridDrawSelectBox(rowIdx) {
381 var data = this.grid.model.getRow(rowIdx);
383 var domId = 'vl-record-list-selected-' +data.id;
384 selectableGridRecords[domId] = data.id;
385 return "<input type='checkbox' id='"+domId+"'/>";
388 function vlSelectAllGridRecords() {
389 for(var id in selectableGridRecords)
390 dojo.byId(id).checked = true;
392 function vlSelectNoGridRecords() {
393 for(var id in selectableGridRecords)
394 dojo.byId(id).checked = false;
397 var handleRetrieveRecords = function() {
398 buildRecordGrid(currentType);
401 function vlImportSelectedRecords() {
402 displayGlobalDiv('vl-generic-progress-with-total');
404 for(var id in selectableGridRecords) {
405 if(dojo.byId(id).checked) {
406 var recId = selectableGridRecords[id];
407 var rec = queuedRecordsMap[recId];
408 if(!rec.import_time())
412 fieldmapper.standardRequest(
413 ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
415 params: [authtoken, records],
416 onresponse: function(r) {
417 var resp = r.recv().content();
418 if(e = openils.Event.parse(resp))
420 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
422 oncomplete: function() {
423 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
431 * Create queue, upload MARC, process spool, load the newly created queue
433 function batchUpload() {
434 var queueName = dijit.byId('vl-queue-name').getValue();
435 currentType = dijit.byId('vl-record-type').getValue();
437 var handleProcessSpool = function() {
438 console.log('records uploaded and spooled');
439 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
442 var handleUploadMARC = function(key) {
443 console.log('marc uploaded');
444 processSpool(key, currentQueueId, currentType, handleProcessSpool);
447 var handleCreateQueue = function(queue) {
448 console.log('queue created ' + queue.name());
449 currentQueueId = queue.id();
450 uploadMARC(handleUploadMARC);
453 createQueue(queueName, currentType, handleCreateQueue);
456 dojo.addOnLoad(vlInit);