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("dojo.cookie");
21 dojo.require("dojox.grid.Grid");
22 dojo.require("dojo.data.ItemFileReadStore");
23 dojo.require("fieldmapper.Fieldmapper");
24 dojo.require("fieldmapper.dojoData");
25 dojo.require('openils.CGI');
26 dojo.require('openils.User');
27 dojo.require('openils.Event');
30 'vl-generic-progress',
31 'vl-generic-progress-with-total',
37 var VANDELAY_URL = '/vandelay';
39 var authAttrDefs = [];
40 var queuedRecords = [];
41 var queuedRecordsMap = {};
42 var bibAttrsFetched = false;
43 var authAttrsFetched = false;
46 var cgi = new openils.CGI();
47 var currentQueueId = null;
53 authtoken = dojo.cookie('ses') || cgi.param('ses');
54 bibAttrsFetched = false;
55 authAttrsFetched = false;
57 // Fetch the bib and authority attribute definitions
58 fieldmapper.standardRequest(
59 ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
61 params: [authtoken, {id:{'!=':null}}],
62 onresponse: function(r) {
63 var def = r.recv().content();
64 if(e = openils.Event.parse(def[0]))
66 bibAttrDefs.push(def);
68 oncomplete: function() {
69 bibAttrsFetched = true;
70 bibAttrDefs = bibAttrDefs.sort(
72 if(a.description() > b.description()) return 1;
73 if(a.description() < b.description()) return -1;
83 fieldmapper.standardRequest(
84 ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
86 params: [authtoken, {id:{'!=':null}}],
87 onresponse: function(r) {
88 var def = r.recv().content();
89 if(e = openils.Event.parse(def[0]))
91 authAttrDefs.push(def);
93 oncomplete: function() {
94 authAttrsFetched = true;
95 authAttrDefs = authAttrDefs.sort(
97 if(a.description() > b.description()) return 1;
98 if(a.description() < b.description()) return -1;
103 runStartupCommands();
109 function displayGlobalDiv(id) {
110 for(var i = 0; i < globalDivs.length; i++)
111 dojo.style(dojo.byId(globalDivs[i]), 'display', 'none');
112 dojo.style(dojo.byId(id),'display','block');
115 function runStartupCommands() {
116 currentQueueId = cgi.param('qid');
117 currentType = cgi.param('qtype');
119 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
120 displayGlobalDiv('vl-marc-upload-div');
124 * asynchronously upload a file of MARC records
126 function uploadMARC(onload){
127 dojo.byId('vl-ses-input').value = authtoken;
128 dojo.style(dojo.byId('vl-input-td'),"display","none");
129 dojo.style(dojo.byId('vl-upload-progress-span'),"display","inline");
131 dojo.style(dojo.byId('vl-file-label'), 'display', 'none');
132 dojo.style(dojo.byId('vl-file-uploading'), 'display', 'inline');
134 dojo.io.iframe.send({
138 form: dojo.byId('vl-marc-upload-form'),
139 handle: function(data,ioArgs){
140 var content = data.documentElement.textContent;
141 var key = content.split(/\n/)[2]; /* XXX have to strip the headers.. (why?) */
142 dojo.style(dojo.byId('vl-input-td'),"display","inline");
143 dojo.style(dojo.byId('vl-upload-progress-span'),"display","none");
144 dojo.style(dojo.byId('vl-file-label'), 'display', 'inline');
145 dojo.style(dojo.byId('vl-file-uploading'), 'display', 'none');
152 * Creates a new vandelay queue
154 function createQueue(queueName, type, onload) {
155 fieldmapper.standardRequest(
156 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.create'],
158 params: [authtoken, queueName, null, type],
159 oncomplete : function(r) {
160 var queue = r.recv().content();
161 if(e = openils.Event.parse(queue))
170 * Tells vendelay to pull a batch of records from the cache and explode them
171 * out into the vandelay tables
173 function processSpool(key, queueId, type, onload) {
174 fieldmapper.standardRequest(
175 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
177 params: [authtoken, key, queueId],
178 oncomplete : function(r) {
179 var resp = r.recv().content();
180 if(e = openils.Event.parse(resp))
188 function retrieveQueuedRecords(type, queueId, onload) {
190 queuedRecordsMap = {};
191 resetVlQueueGridLayout();
192 fieldmapper.standardRequest(
193 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.records.retrieve.atomic'],
195 params: [authtoken, queueId, {clear_marc:1}],
196 /* intermittent bug in streaming, multipart requests prevents use of onreponse for now...
197 onresponse: function(r) {
198 var rec = r.recv().content();
199 if(e = openils.Event.parse(rec))
201 queuedRecords.push(rec);
202 queuedRecordsMap[rec.id()] = rec;
205 oncomplete: function(r){
206 var recs = r.recv().content();
207 if(e = openils.Event.parse(recs[0]))
209 for(var i = 0; i < recs.length; i++) {
211 queuedRecords.push(rec);
212 queuedRecordsMap[rec.id()] = rec;
220 function getAttrValue(rowIdx) {
221 var data = this.grid.model.getRow(rowIdx);
223 var attrName = this.field.split('.')[1];
224 var defId = attrMap[attrName];
225 var rec = queuedRecordsMap[data.id];
226 var attrs = rec.attributes();
227 for(var i = 0; i < attrs.length; i++) {
229 if(attr.field() == defId)
230 return attr.attr_value();
235 function buildRecordGrid(type) {
236 displayGlobalDiv('vl-queue-div');
238 var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
239 for(var i = 0; i < defs.length; i++) {
241 attrMap[attr.code()] = attr.id();
243 name:attr.description(),
244 field:'attr.' + attr.code(),
247 //if(attr.code().match(/title/i)) col.width = 'auto'; // this is hack.
248 vlQueueGridLayout[0].cells[0].push(col);
251 vlQueueGrid.setStructure(vlQueueGridLayout);
255 storeData = vqbr.toStoreData(queuedRecords);
257 storeData = vqar.toStoreData(queuedRecords);
259 var store = new dojo.data.ItemFileReadStore({data:storeData});
260 var model = new dojox.grid.data.DojoData(
261 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
263 vlQueueGrid.setModel(model);
264 vlQueueGrid.update();
267 var selectableGridRecords = {};
268 function vlQueueGridDrawSelectBox(rowIdx) {
269 var data = this.grid.model.getRow(rowIdx);
271 var domId = 'vl-record-list-selected-' +data.id;
272 selectableGridRecords[domId] = data.id;
273 return "<input type='checkbox' id='"+domId+"'/>";
276 function vlSelectAllGridRecords() {
277 for(var id in selectableGridRecords)
278 dojo.byId(id).checked = true;
280 function vlSelectNoGridRecords() {
281 for(var id in selectableGridRecords)
282 dojo.byId(id).checked = false;
285 var handleRetrieveRecords = function() {
286 buildRecordGrid(currentType);
289 function vlImportSelectedRecords() {
290 displayGlobalDiv('vl-generic-progress-with-total');
292 for(var id in selectableGridRecords) {
293 if(dojo.byId(id).checked)
294 records.push(selectableGridRecords[id]);
296 fieldmapper.standardRequest(
297 ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
299 params: [authtoken, records],
300 onresponse: function(r) {
301 var resp = r.recv().content();
302 if(e = openils.Event.parse(resp))
304 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
306 oncomplete: function() {
307 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
315 * Create queue, upload MARC, process spool, load the newly created queue
317 function batchUpload() {
318 var queueName = dijit.byId('vl-queue-name').getValue();
319 currentType = dijit.byId('vl-record-type').getValue();
321 var handleProcessSpool = function() {
322 console.log('records uploaded and spooled');
323 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
326 var handleUploadMARC = function(key) {
327 console.log('marc uploaded');
328 processSpool(key, currentQueueId, currentType, handleProcessSpool);
331 var handleCreateQueue = function(queue) {
332 console.log('queue created ' + queue.name());
333 currentQueueId = queue.id();
334 uploadMARC(handleUploadMARC);
337 createQueue(queueName, currentType, handleCreateQueue);
340 dojo.addOnLoad(vlInit);