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('dojo.date.locale');
24 dojo.require('dojo.date.stamp');
25 dojo.require("fieldmapper.Fieldmapper");
26 dojo.require("fieldmapper.dojoData");
27 dojo.require('openils.CGI');
28 dojo.require('openils.User');
29 dojo.require('openils.Event');
32 'vl-generic-progress',
33 'vl-generic-progress-with-total',
39 var VANDELAY_URL = '/vandelay';
41 var authAttrDefs = [];
42 var queuedRecords = [];
43 var queuedRecordsMap = {};
44 var bibAttrsFetched = false;
45 var authAttrsFetched = false;
48 var cgi = new openils.CGI();
49 var currentQueueId = null;
55 authtoken = dojo.cookie('ses') || cgi.param('ses');
56 bibAttrsFetched = false;
57 authAttrsFetched = false;
59 // Fetch the bib and authority attribute definitions
60 fieldmapper.standardRequest(
61 ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
63 params: [authtoken, {id:{'!=':null}}],
64 onresponse: function(r) {
65 var def = r.recv().content();
66 if(e = openils.Event.parse(def[0]))
68 bibAttrDefs.push(def);
70 oncomplete: function() {
71 bibAttrsFetched = true;
72 bibAttrDefs = bibAttrDefs.sort(
74 if(a.description() > b.description()) return 1;
75 if(a.description() < b.description()) return -1;
85 fieldmapper.standardRequest(
86 ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
88 params: [authtoken, {id:{'!=':null}}],
89 onresponse: function(r) {
90 var def = r.recv().content();
91 if(e = openils.Event.parse(def[0]))
93 authAttrDefs.push(def);
95 oncomplete: function() {
96 authAttrsFetched = true;
97 authAttrDefs = authAttrDefs.sort(
99 if(a.description() > b.description()) return 1;
100 if(a.description() < b.description()) return -1;
105 runStartupCommands();
111 function displayGlobalDiv(id) {
112 for(var i = 0; i < globalDivs.length; i++)
113 dojo.style(dojo.byId(globalDivs[i]), 'display', 'none');
114 dojo.style(dojo.byId(id),'display','block');
117 function runStartupCommands() {
118 currentQueueId = cgi.param('qid');
119 currentType = cgi.param('qtype');
121 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
122 displayGlobalDiv('vl-marc-upload-div');
126 * asynchronously upload a file of MARC records
128 function uploadMARC(onload){
129 dojo.byId('vl-ses-input').value = authtoken;
130 dojo.style(dojo.byId('vl-input-td'),"display","none");
131 dojo.style(dojo.byId('vl-upload-progress-span'),"display","inline");
133 dojo.style(dojo.byId('vl-file-label'), 'display', 'none');
134 dojo.style(dojo.byId('vl-file-uploading'), 'display', 'inline');
136 dojo.io.iframe.send({
140 form: dojo.byId('vl-marc-upload-form'),
141 handle: function(data,ioArgs){
142 var content = data.documentElement.textContent;
143 var key = content.split(/\n/)[2]; /* XXX have to strip the headers.. (why?) */
144 dojo.style(dojo.byId('vl-input-td'),"display","inline");
145 dojo.style(dojo.byId('vl-upload-progress-span'),"display","none");
146 dojo.style(dojo.byId('vl-file-label'), 'display', 'inline');
147 dojo.style(dojo.byId('vl-file-uploading'), 'display', 'none');
154 * Creates a new vandelay queue
156 function createQueue(queueName, type, onload) {
157 fieldmapper.standardRequest(
158 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.create'],
160 params: [authtoken, queueName, null, type],
161 oncomplete : function(r) {
162 var queue = r.recv().content();
163 if(e = openils.Event.parse(queue))
172 * Tells vendelay to pull a batch of records from the cache and explode them
173 * out into the vandelay tables
175 function processSpool(key, queueId, type, onload) {
176 fieldmapper.standardRequest(
177 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
179 params: [authtoken, key, queueId],
180 oncomplete : function(r) {
181 var resp = r.recv().content();
182 if(e = openils.Event.parse(resp))
190 function retrieveQueuedRecords(type, queueId, onload) {
192 queuedRecordsMap = {};
193 resetVlQueueGridLayout();
194 fieldmapper.standardRequest(
195 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.records.retrieve.atomic'],
197 params: [authtoken, queueId, {clear_marc:1}],
198 /* intermittent bug in streaming, multipart requests prevents use of onreponse for now...
199 onresponse: function(r) {
200 var rec = r.recv().content();
201 if(e = openils.Event.parse(rec))
203 queuedRecords.push(rec);
204 queuedRecordsMap[rec.id()] = rec;
207 oncomplete: function(r){
208 var recs = r.recv().content();
209 if(e = openils.Event.parse(recs[0]))
211 for(var i = 0; i < recs.length; i++) {
213 queuedRecords.push(rec);
214 queuedRecordsMap[rec.id()] = rec;
222 function getAttrValue(rowIdx) {
223 var data = this.grid.model.getRow(rowIdx);
225 var attrName = this.field.split('.')[1];
226 var defId = attrMap[attrName];
227 var rec = queuedRecordsMap[data.id];
228 var attrs = rec.attributes();
229 for(var i = 0; i < attrs.length; i++) {
231 if(attr.field() == defId)
232 return attr.attr_value();
237 function getDateTimeField(rowIdx) {
238 data = this.grid.model.getRow(rowIdx);
240 var date = dojo.date.stamp.fromISOString(data[this.field]);
241 return dojo.date.locale.format(date, {formatLength:'medium'});
244 function buildRecordGrid(type) {
245 displayGlobalDiv('vl-queue-div');
247 var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
248 for(var i = 0; i < defs.length; i++) {
250 attrMap[attr.code()] = attr.id();
252 name:attr.description(),
253 field:'attr.' + attr.code(),
256 //if(attr.code().match(/title/i)) col.width = 'auto'; // this is hack.
257 vlQueueGridLayout[0].cells[0].push(col);
260 vlQueueGrid.setStructure(vlQueueGridLayout);
264 storeData = vqbr.toStoreData(queuedRecords);
266 storeData = vqar.toStoreData(queuedRecords);
268 var store = new dojo.data.ItemFileReadStore({data:storeData});
269 var model = new dojox.grid.data.DojoData(
270 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
272 vlQueueGrid.setModel(model);
273 vlQueueGrid.update();
276 var selectableGridRecords = {};
277 function vlQueueGridDrawSelectBox(rowIdx) {
278 var data = this.grid.model.getRow(rowIdx);
280 var domId = 'vl-record-list-selected-' +data.id;
281 selectableGridRecords[domId] = data.id;
282 return "<input type='checkbox' id='"+domId+"'/>";
285 function vlSelectAllGridRecords() {
286 for(var id in selectableGridRecords)
287 dojo.byId(id).checked = true;
289 function vlSelectNoGridRecords() {
290 for(var id in selectableGridRecords)
291 dojo.byId(id).checked = false;
294 var handleRetrieveRecords = function() {
295 buildRecordGrid(currentType);
298 function vlImportSelectedRecords() {
299 displayGlobalDiv('vl-generic-progress-with-total');
301 for(var id in selectableGridRecords) {
302 if(dojo.byId(id).checked)
303 records.push(selectableGridRecords[id]);
305 fieldmapper.standardRequest(
306 ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
308 params: [authtoken, records],
309 onresponse: function(r) {
310 var resp = r.recv().content();
311 if(e = openils.Event.parse(resp))
313 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
315 oncomplete: function() {
316 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
324 * Create queue, upload MARC, process spool, load the newly created queue
326 function batchUpload() {
327 var queueName = dijit.byId('vl-queue-name').getValue();
328 currentType = dijit.byId('vl-record-type').getValue();
330 var handleProcessSpool = function() {
331 console.log('records uploaded and spooled');
332 retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
335 var handleUploadMARC = function(key) {
336 console.log('marc uploaded');
337 processSpool(key, currentQueueId, currentType, handleProcessSpool);
340 var handleCreateQueue = function(queue) {
341 console.log('queue created ' + queue.name());
342 currentQueueId = queue.id();
343 uploadMARC(handleUploadMARC);
346 createQueue(queueName, currentType, handleCreateQueue);
349 dojo.addOnLoad(vlInit);