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;
50 var attrDefMap = {}; // maps attr def code names to attr def ids
52 var cgi = new openils.CGI();
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
63 authtoken = dojo.cookie('ses') || cgi.param('ses');
64 bibAttrsFetched = false;
65 authAttrsFetched = false;
67 // Fetch the bib and authority attribute definitions
68 fieldmapper.standardRequest(
69 ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
71 params: [authtoken, {id:{'!=':null}}],
72 onresponse: function(r) {
73 var def = r.recv().content();
74 if(e = openils.Event.parse(def[0]))
76 bibAttrDefs.push(def);
78 oncomplete: function() {
79 bibAttrsFetched = true;
80 bibAttrDefs = bibAttrDefs.sort(
82 if(a.id() > b.id()) return 1;
83 if(a.id() < b.id()) return -1;
93 fieldmapper.standardRequest(
94 ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
96 params: [authtoken, {id:{'!=':null}}],
97 onresponse: function(r) {
98 var def = r.recv().content();
99 if(e = openils.Event.parse(def[0]))
101 authAttrDefs.push(def);
103 oncomplete: function() {
104 authAttrsFetched = true;
105 authAttrDefs = authAttrDefs.sort(
107 if(a.id() > b.id()) return 1;
108 if(a.id() < b.id()) return -1;
113 runStartupCommands();
119 function displayGlobalDiv(id) {
120 for(var i = 0; i < globalDivs.length; i++)
121 dojo.style(dojo.byId(globalDivs[i]), 'display', 'none');
122 dojo.style(dojo.byId(id),'display','block');
125 function runStartupCommands() {
126 currentQueueId = cgi.param('qid');
127 currentType = cgi.param('qtype');
129 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
130 displayGlobalDiv('vl-marc-upload-div');
134 * asynchronously upload a file of MARC records
136 function uploadMARC(onload){
137 dojo.byId('vl-ses-input').value = authtoken;
138 dojo.style(dojo.byId('vl-input-td'),"display","none");
139 dojo.style(dojo.byId('vl-upload-progress-span'),"display","inline");
141 dojo.style(dojo.byId('vl-file-label'), 'display', 'none');
142 dojo.style(dojo.byId('vl-file-uploading'), 'display', 'inline');
144 dojo.io.iframe.send({
148 form: dojo.byId('vl-marc-upload-form'),
149 handle: function(data,ioArgs){
150 var content = data.documentElement.textContent;
151 dojo.style(dojo.byId('vl-input-td'),"display","inline");
152 dojo.style(dojo.byId('vl-upload-progress-span'),"display","none");
153 dojo.style(dojo.byId('vl-file-label'), 'display', 'inline');
154 dojo.style(dojo.byId('vl-file-uploading'), 'display', 'none');
161 * Creates a new vandelay queue
163 function createQueue(queueName, type, onload) {
164 fieldmapper.standardRequest(
165 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.create'],
167 params: [authtoken, queueName, null, type],
168 oncomplete : function(r) {
169 var queue = r.recv().content();
170 if(e = openils.Event.parse(queue))
179 * Tells vendelay to pull a batch of records from the cache and explode them
180 * out into the vandelay tables
182 function processSpool(key, queueId, type, onload) {
183 fieldmapper.standardRequest(
184 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
186 params: [authtoken, key, queueId],
187 oncomplete : function(r) {
188 var resp = r.recv().content();
189 if(e = openils.Event.parse(resp))
197 function retrieveQueuedRecords(type, queueId, onload) {
199 queuedRecordsMap = {};
200 resetVlQueueGridLayout();
201 fieldmapper.standardRequest(
202 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.records.retrieve.atomic'],
204 params: [authtoken, queueId, {clear_marc:1}],
205 /* intermittent bug in streaming, multipart requests prevents use of onreponse for now...
206 onresponse: function(r) {
207 var rec = r.recv().content();
208 if(e = openils.Event.parse(rec))
210 queuedRecords.push(rec);
211 queuedRecordsMap[rec.id()] = rec;
214 oncomplete: function(r){
215 var recs = r.recv().content();
216 if(e = openils.Event.parse(recs[0]))
218 for(var i = 0; i < recs.length; i++) {
220 queuedRecords.push(rec);
221 queuedRecordsMap[rec.id()] = rec;
229 function vlLoadMatchUI(recId, attrCode) {
230 displayGlobalDiv('vl-generic-progress');
231 var matches = getRecMatchesFromAttrCode(queuedRecordsMap[recId], attrCode);
233 currentImportRecId = recId;
234 for(var i = 0; i < matches.length; i++)
235 records.push(matches[i].eg_record());
236 fieldmapper.standardRequest(
237 ['open-ils.search', 'open-ils.search.biblio.record_entry.slim.retrieve'],
240 oncomplete: function(r) {
241 var recs = r.recv().content();
242 if(e = openils.Event.parse(recs))
244 displayGlobalDiv('vl-match-div');
245 resetVlMatchGridLayout();
246 currentMatchedRecords = recs;
247 vlMatchGrid.setStructure(vlMatchGridLayout);
248 var dataStore = bre.toStoreData(recs, null, {virtualFields:['field_type']});
249 for(var i = 0; i < dataStore.items.length; i++) {
250 var item = dataStore.items[i];
251 for(var j = 0; j < matches.length; j++) {
252 var match = matches[j];
253 if(match.eg_record() == item.id)
254 item.field_type = match.field_type();
257 var store = new dojo.data.ItemFileReadStore({data:dataStore});
258 var model = new dojox.grid.data.DojoData(
259 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
260 vlMatchGrid.setModel(model);
261 vlMatchGrid.update();
268 function vlLoadMARCHtml(recId) {
269 displayGlobalDiv('vl-generic-progress');
270 fieldmapper.standardRequest(
271 ['open-ils.search', 'open-ils.search.biblio.record.html'],
274 oncomplete: function(r) {
275 displayGlobalDiv('vl-match-html-div');
276 var html = r.recv().content();
277 dojo.byId('vl-match-record-html').innerHTML = html;
285 * Given a record, an attribute definition code, and a matching record attribute,
286 * this will determine if there are any import matches and build the UI to
287 * represent those matches. If no matches exist, simply returns the attribute value
289 function buildAttrColumnUI(rec, attrCode, attr) {
290 var matches = getRecMatchesFromAttrCode(rec, attrCode);
291 if(matches.length > 0) { // found some matches
292 return '<div class="match_div">' +
293 '<a href="javascript:void(0);" onclick="vlLoadMatchUI('+
294 rec.id()+',\''+attrCode+'\');">'+
295 attr.attr_value() + ' ('+matches.length+')</a></div>';
298 return attr.attr_value();
301 function getRecMatchesFromAttrCode(rec, attrCode) {
303 var attr = getRecAttrFromCode(rec, attrCode);
304 for(var j = 0; j < rec.matches().length; j++) {
305 var match = rec.matches()[j];
306 if(match.matched_attr() == attr.id())
312 function getRecAttrFromCode(rec, attrCode) {
313 var defId = attrDefMap[attrCode];
314 var attrs = rec.attributes();
315 for(var i = 0; i < attrs.length; i++) {
317 if(attr.field() == defId)
323 function getAttrValue(rowIdx) {
324 var data = this.grid.model.getRow(rowIdx);
326 var attrCode = this.field.split('.')[1];
327 var rec = queuedRecordsMap[data.id];
328 var attr = getRecAttrFromCode(rec, attrCode);
330 return buildAttrColumnUI(rec, attrCode, attr);
334 function vlGetDateTimeField(rowIdx) {
335 data = this.grid.model.getRow(rowIdx);
337 if(!data[this.field]) return '';
338 var date = dojo.date.stamp.fromISOString(data[this.field]);
339 return dojo.date.locale.format(date, {selector:'date'});
342 function vlGetCreator(rowIdx) {
343 data = this.grid.model.getRow(rowIdx);
345 var id = data.creator;
347 return userCache[id].usrname();
348 var user = fieldmapper.standardRequest(['open-ils.actor', 'open-ils.actor.user.retrieve'], [authtoken, id]);
349 if(e = openils.Event.parse(user))
351 userCache[id] = user;
352 return user.usrname();
355 function vlGetViewMARC(rowIdx) {
356 data = this.grid.model.getRow(rowIdx);
358 return this.value.replace('RECID', data.id);
361 function vlGetOverlayTargetSelector(rowIdx) {
362 data = this.grid.model.getRow(rowIdx);
364 return this.value.replace('ID', data.id);
367 function vlHandleOverlayTargetSelected() {
369 console.log("checking target select..");
370 if(!vlOverlayTargetEnable.checked) return;
371 console.log("overlay enabled for for record "+matchRecId);
372 for(var i = 0; i < currentMatchedRecords.length; i++) {
373 var matchRecId = currentMatchedRecords[i].id();
374 console.log("checking id vl-overlay-target-"+matchRecId);
375 if(dojo.byId('vl-overlay-target-'+matchRecId).selected) {
376 console.log("CHECKED");
377 currentOverlayRecordsMap[currentImportRecId] = matchRecId;
383 function buildRecordGrid(type) {
384 displayGlobalDiv('vl-queue-div');
386 currentOverlayRecordsMap = {};
388 var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
389 for(var i = 0; i < defs.length; i++) {
391 attrDefMap[def.code()] = def.id();
393 name:def.description(),
394 field:'attr.' + def.code(),
397 //if(def.code().match(/title/i)) col.width = 'auto'; // this is hack.
398 vlQueueGridLayout[0].cells[0].push(col);
401 vlQueueGrid.setStructure(vlQueueGridLayout);
405 storeData = vqbr.toStoreData(queuedRecords);
407 storeData = vqar.toStoreData(queuedRecords);
409 var store = new dojo.data.ItemFileReadStore({data:storeData});
410 var model = new dojox.grid.data.DojoData(
411 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
413 vlQueueGrid.setModel(model);
414 vlQueueGrid.update();
419 alert(vlQueueGridLayout.picker);
420 vlQueueGridLayout.oils = {};
421 vlQueueGridLayout[0].cells[0].pop();
422 vlQueueGridLayout[0].cells[0].pop();
423 vlQueueGridLayout[0].cells[0].pop();
424 vlQueueGridLayout[0].cells[0].pop();
425 vlQueueGridLayout[0].cells[0].pop();
426 vlQueueGrid.setStructure(vlQueueGridLayout);
427 vlQueueGrid.update();
431 var selectableGridRecords = {};
432 function vlQueueGridDrawSelectBox(rowIdx) {
433 var data = this.grid.model.getRow(rowIdx);
435 var domId = 'vl-record-list-selected-' +data.id;
436 selectableGridRecords[domId] = data.id;
437 return "<input type='checkbox' id='"+domId+"'/>";
440 function vlSelectAllGridRecords() {
441 for(var id in selectableGridRecords)
442 dojo.byId(id).checked = true;
444 function vlSelectNoGridRecords() {
445 for(var id in selectableGridRecords)
446 dojo.byId(id).checked = false;
449 var handleRetrieveRecords = function() {
450 buildRecordGrid(currentType);
453 function vlImportSelectedRecords() {
454 displayGlobalDiv('vl-generic-progress-with-total');
456 for(var id in selectableGridRecords) {
457 if(dojo.byId(id).checked) {
458 var recId = selectableGridRecords[id];
459 var rec = queuedRecordsMap[recId];
460 if(!rec.import_time())
464 fieldmapper.standardRequest(
465 ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
467 params: [authtoken, records],
468 onresponse: function(r) {
469 var resp = r.recv().content();
470 if(e = openils.Event.parse(resp))
472 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
474 oncomplete: function() {
475 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
483 * Create queue, upload MARC, process spool, load the newly created queue
485 function batchUpload() {
486 var queueName = dijit.byId('vl-queue-name').getValue();
487 currentType = dijit.byId('vl-record-type').getValue();
489 var handleProcessSpool = function() {
490 console.log('records uploaded and spooled');
491 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
494 var handleUploadMARC = function(key) {
495 console.log('marc uploaded');
496 processSpool(key, currentQueueId, currentType, handleProcessSpool);
499 var handleCreateQueue = function(queue) {
500 console.log('queue created ' + queue.name());
501 currentQueueId = queue.id();
502 uploadMARC(handleUploadMARC);
505 createQueue(queueName, currentType, handleCreateQueue);
508 dojo.addOnLoad(vlInit);