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.id() > b.id()) return 1;
80 if(a.id() < b.id()) 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.id() > b.id()) return 1;
105 if(a.id() < b.id()) 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 dojo.style(dojo.byId('vl-input-td'),"display","inline");
149 dojo.style(dojo.byId('vl-upload-progress-span'),"display","none");
150 dojo.style(dojo.byId('vl-file-label'), 'display', 'inline');
151 dojo.style(dojo.byId('vl-file-uploading'), 'display', 'none');
158 * Creates a new vandelay queue
160 function createQueue(queueName, type, onload) {
161 fieldmapper.standardRequest(
162 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.create'],
164 params: [authtoken, queueName, null, type],
165 oncomplete : function(r) {
166 var queue = r.recv().content();
167 if(e = openils.Event.parse(queue))
176 * Tells vendelay to pull a batch of records from the cache and explode them
177 * out into the vandelay tables
179 function processSpool(key, queueId, type, onload) {
180 fieldmapper.standardRequest(
181 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
183 params: [authtoken, key, queueId],
184 oncomplete : function(r) {
185 var resp = r.recv().content();
186 if(e = openils.Event.parse(resp))
194 function retrieveQueuedRecords(type, queueId, onload) {
196 queuedRecordsMap = {};
197 resetVlQueueGridLayout();
198 fieldmapper.standardRequest(
199 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.records.retrieve.atomic'],
201 params: [authtoken, queueId, {clear_marc:1}],
202 /* intermittent bug in streaming, multipart requests prevents use of onreponse for now...
203 onresponse: function(r) {
204 var rec = r.recv().content();
205 if(e = openils.Event.parse(rec))
207 queuedRecords.push(rec);
208 queuedRecordsMap[rec.id()] = rec;
211 oncomplete: function(r){
212 var recs = r.recv().content();
213 if(e = openils.Event.parse(recs[0]))
215 for(var i = 0; i < recs.length; i++) {
217 queuedRecords.push(rec);
218 queuedRecordsMap[rec.id()] = rec;
226 function vlLoadMatchUI(recId, attrCode) {
227 displayGlobalDiv('vl-generic-progress');
228 var matches = getRecMatchesFromAttrCode(queuedRecordsMap[recId], attrCode);
230 for(var i = 0; i < matches.length; i++)
231 records.push(matches[i].eg_record());
232 fieldmapper.standardRequest(
233 ['open-ils.search', 'open-ils.search.biblio.record_entry.slim.retrieve'],
236 oncomplete: function(r) {
237 var recs = r.recv().content();
238 if(e = openils.Event.parse(recs))
240 displayGlobalDiv('vl-match-div');
241 resetVlMatchGridLayout();
242 vlMatchGrid.setStructure(vlMatchGridLayout);
243 var store = new dojo.data.ItemFileReadStore({data:bre.toStoreData(recs)});
244 var model = new dojox.grid.data.DojoData(
245 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
246 vlMatchGrid.setModel(model);
247 vlMatchGrid.update();
253 function vlLoadMARCHtml(recId) {
254 displayGlobalDiv('vl-generic-progress');
255 fieldmapper.standardRequest(
256 ['open-ils.search', 'open-ils.search.biblio.record.html'],
259 oncomplete: function(r) {
260 displayGlobalDiv('vl-match-html-div');
261 var html = r.recv().content();
262 dojo.byId('vl-match-record-html').innerHTML = html;
270 * Given a record, an attribute definition code, and a matching record attribute,
271 * this will determine if there are any import matches and build the UI to
272 * represent those matches. If no matches exist, simply returns the attribute value
274 function buildAttrColumnUI(rec, attrCode, attr) {
275 var matches = getRecMatchesFromAttrCode(rec, attrCode);
276 if(matches.length > 0) { // found some matches
277 return '<div class="match_div">' +
278 '<a href="javascript:void(0);" onclick="vlLoadMatchUI('+
279 rec.id()+',\''+matches[0].field_type()+'\');">'+
280 attr.attr_value() + ' ('+matches.length+')</a></div>';
283 return attr.attr_value();
286 function getRecMatchesFromAttrCode(rec, attrCode) {
288 for(var j = 0; j < rec.matches().length; j++) {
289 var match = rec.matches()[j];
290 if(match.field_type() == attrCode)
296 function getRecAttrFromCode(rec, attrCode) {
297 var defId = attrMap[attrCode];
298 var attrs = rec.attributes();
299 for(var i = 0; i < attrs.length; i++) {
301 if(attr.field() == defId)
307 function getAttrValue(rowIdx) {
308 var data = this.grid.model.getRow(rowIdx);
310 var attrCode = this.field.split('.')[1];
311 var rec = queuedRecordsMap[data.id];
312 var attr = getRecAttrFromCode(rec, attrCode);
313 console.log('attr = ' + attr);
315 return buildAttrColumnUI(rec, attrCode, attr);
319 function vlGetDateTimeField(rowIdx) {
320 data = this.grid.model.getRow(rowIdx);
322 if(!data[this.field]) return '';
323 var date = dojo.date.stamp.fromISOString(data[this.field]);
324 return dojo.date.locale.format(date, {selector:'date'});
327 function vlGetCreator(rowIdx) {
328 data = this.grid.model.getRow(rowIdx);
330 var id = data.creator;
332 return userCache[id].usrname();
333 var user = fieldmapper.standardRequest(['open-ils.actor', 'open-ils.actor.user.retrieve'], [authtoken, id]);
334 if(e = openils.Event.parse(user))
336 userCache[id] = user;
337 return user.usrname();
340 function vlGetViewMARC(rowIdx) {
341 data = this.grid.model.getRow(rowIdx);
343 return this.value.replace('RECID', data.id);
346 function buildRecordGrid(type) {
347 displayGlobalDiv('vl-queue-div');
349 var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
350 for(var i = 0; i < defs.length; i++) {
352 attrMap[attr.code()] = attr.id();
354 name:attr.description(),
355 field:'attr.' + attr.code(),
358 //if(attr.code().match(/title/i)) col.width = 'auto'; // this is hack.
359 vlQueueGridLayout[0].cells[0].push(col);
362 vlQueueGrid.setStructure(vlQueueGridLayout);
366 storeData = vqbr.toStoreData(queuedRecords);
368 storeData = vqar.toStoreData(queuedRecords);
370 var store = new dojo.data.ItemFileReadStore({data:storeData});
371 var model = new dojox.grid.data.DojoData(
372 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
374 vlQueueGrid.setModel(model);
375 vlQueueGrid.update();
380 alert(vlQueueGridLayout.picker);
381 vlQueueGridLayout.oils = {};
382 vlQueueGridLayout[0].cells[0].pop();
383 vlQueueGridLayout[0].cells[0].pop();
384 vlQueueGridLayout[0].cells[0].pop();
385 vlQueueGridLayout[0].cells[0].pop();
386 vlQueueGridLayout[0].cells[0].pop();
387 vlQueueGrid.setStructure(vlQueueGridLayout);
388 vlQueueGrid.update();
392 var selectableGridRecords = {};
393 function vlQueueGridDrawSelectBox(rowIdx) {
394 var data = this.grid.model.getRow(rowIdx);
396 var domId = 'vl-record-list-selected-' +data.id;
397 selectableGridRecords[domId] = data.id;
398 return "<input type='checkbox' id='"+domId+"'/>";
401 function vlSelectAllGridRecords() {
402 for(var id in selectableGridRecords)
403 dojo.byId(id).checked = true;
405 function vlSelectNoGridRecords() {
406 for(var id in selectableGridRecords)
407 dojo.byId(id).checked = false;
410 var handleRetrieveRecords = function() {
411 buildRecordGrid(currentType);
414 function vlImportSelectedRecords() {
415 displayGlobalDiv('vl-generic-progress-with-total');
417 for(var id in selectableGridRecords) {
418 if(dojo.byId(id).checked) {
419 var recId = selectableGridRecords[id];
420 var rec = queuedRecordsMap[recId];
421 if(!rec.import_time())
425 fieldmapper.standardRequest(
426 ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
428 params: [authtoken, records],
429 onresponse: function(r) {
430 var resp = r.recv().content();
431 if(e = openils.Event.parse(resp))
433 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
435 oncomplete: function() {
436 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
444 * Create queue, upload MARC, process spool, load the newly created queue
446 function batchUpload() {
447 var queueName = dijit.byId('vl-queue-name').getValue();
448 currentType = dijit.byId('vl-record-type').getValue();
450 var handleProcessSpool = function() {
451 console.log('records uploaded and spooled');
452 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
455 var handleUploadMARC = function(key) {
456 console.log('marc uploaded');
457 processSpool(key, currentQueueId, currentType, handleProcessSpool);
460 var handleCreateQueue = function(queue) {
461 console.log('queue created ' + queue.name());
462 currentQueueId = queue.id();
463 uploadMARC(handleUploadMARC);
466 createQueue(queueName, currentType, handleCreateQueue);
469 dojo.addOnLoad(vlInit);