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 var VANDELAY_URL = '/vandelay';
32 var authAttrDefs = [];
33 var queuedRecords = [];
34 var queuedRecordsMap = {};
35 var bibAttrsFetched = false;
36 var authAttrsFetched = false;
39 var cgi = new openils.CGI();
45 authtoken = dojo.cookie('ses') || cgi.param('ses');
46 bibAttrsFetched = false;
47 authAttrsFetched = false;
49 // Fetch the bib and authority attribute definitions
50 fieldmapper.standardRequest(
51 ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
53 params: [authtoken, {id:{'!=':null}}],
54 onresponse: function(r) {
55 var def = r.recv().content();
56 if(openils.Event.parse(def))
58 bibAttrDefs.push(def);
60 oncomplete: function() {
61 bibAttrsFetched = true;
68 fieldmapper.standardRequest(
69 ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
71 params: [authtoken, {id:{'!=':null}}],
72 onresponse: function(r) {
73 var def = r.recv().content();
74 if(openils.Event.parse(def))
76 authAttrDefs.push(def);
78 oncomplete: function() {
79 authAttrsFetched = true;
87 function displayGlobalDiv(id) {
88 dojo.style(dojo.byId('vl-generic-progress'),"display","none");
89 dojo.style(dojo.byId('vl-marc-upload-div'),"display","none");
90 dojo.style(dojo.byId('vl-queue-div'),"display","none");
91 dojo.style(dojo.byId(id),"display","block");
94 function runStartupCommands() {
95 var queueParam = cgi.param('qid');
96 currentType = cgi.param('qtype');
98 return retrieveQueuedRecords(currentType, queueParam, handleRetrieveRecords);
99 displayGlobalDiv('vl-marc-upload-div');
103 * asynchronously upload a file of MARC records
105 function uploadMARC(onload){
106 dojo.byId('vl-ses-input').value = authtoken;
107 dojo.style(dojo.byId('vl-input-td'),"display","none");
108 dojo.style(dojo.byId('vl-upload-progress-span'),"display","inline");
110 dojo.style(dojo.byId('vl-file-label'), 'display', 'none');
111 dojo.style(dojo.byId('vl-file-uploading'), 'display', 'inline');
113 dojo.io.iframe.send({
117 form: dojo.byId('vl-marc-upload-form'),
118 handle: function(data,ioArgs){
119 var content = data.documentElement.textContent;
120 var key = content.split(/\n/)[2]; /* XXX have to strip the headers.. (why?) */
121 dojo.style(dojo.byId('vl-input-td'),"display","inline");
122 dojo.style(dojo.byId('vl-upload-progress-span'),"display","none");
123 dojo.style(dojo.byId('vl-file-label'), 'display', 'inline');
124 dojo.style(dojo.byId('vl-file-uploading'), 'display', 'none');
131 * Creates a new vandelay queue
133 function createQueue(queueName, type, onload) {
134 fieldmapper.standardRequest(
135 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.create'],
137 params: [authtoken, queueName, null, type],
138 oncomplete : function(r) {
139 var queue = r.recv().content();
140 if(e = openils.Event.parse(queue))
149 * Tells vendelay to pull a batch of records from the cache and explode them
150 * out into the vandelay tables
152 function processSpool(key, queue, type, onload) {
153 fieldmapper.standardRequest(
154 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
156 params: [authtoken, key, queue.id()],
157 oncomplete : function(r) {
158 var queue = r.recv().content();
159 if(e = openils.Event.parse(queue))
167 function retrieveQueuedRecords(type, queueId, onload) {
168 fieldmapper.standardRequest(
169 ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.records.retrieve'],
171 params: [authtoken, queueId, {clear_marc:1}],
172 onresponse: function(r) {
173 var rec = r.recv().content();
174 if(e = openils.Event.parse(rec))
176 queuedRecords.push(rec);
177 queuedRecordsMap[rec.id()] = rec;
179 oncomplete: function(){onload();}
184 function getAttrValue(rowIdx) {
185 var data = this.grid.model.getRow(rowIdx);
187 var attrName = this.field.split('.')[1];
188 var defId = attrMap[attrName];
189 var rec = queuedRecordsMap[data.id];
190 var attrs = rec.attributes();
191 for(var i = 0; i < attrs.length; i++) {
193 if(attr.field() == defId)
194 return attr.attr_value();
199 function buildRecordGrid(type) {
200 displayGlobalDiv('vl-queue-div');
202 /* test structure... */
206 {name: 'ID', field: 'id'},
210 var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
211 for(var i = 0; i < defs.length; i++) {
213 attrMap[attr.code()] = attr.id();
214 structure[0].cells[0].push({
215 name:attr.description(),
216 field:'attr.' + attr.code(),
221 vlQueueGrid.setStructure(structure);
225 storeData = vqbr.toStoreData(queuedRecords);
227 storeData = vqar.toStoreData(queuedRecords);
229 var store = new dojo.data.ItemFileReadStore({data:storeData});
230 var model = new dojox.grid.data.DojoData(
231 null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
232 vlQueueGrid.setModel(model);
233 vlQueueGrid.update();
236 var handleRetrieveRecords = function() {
237 buildRecordGrid(currentType);
241 * Create queue, upload MARC, process spool, load the newly created queue
243 function batchUpload() {
244 var queueName = dijit.byId('vl-queue-name').getValue();
245 currentType = dijit.byId('vl-record-type').getValue();
246 var currentQueue = null;
248 var handleProcessSpool = function() {
249 console.log('records uploaded and spooled');
250 retrieveQueuedRecords(currentType, currentQueue.id(), handleRetrieveRecords);
253 var handleUploadMARC = function(key) {
254 console.log('marc uploaded');
255 processSpool(key, currentQueue, currentType, handleProcessSpool);
258 var handleCreateQueue = function(queue) {
259 console.log('queue created ' + queue.name());
260 currentQueue = queue;
261 uploadMARC(handleUploadMARC);
264 createQueue(queueName, currentType, handleCreateQueue);