]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/vandelay/vandelay.js
fcfec72323cf9ec587cca559030f86e1817406f4
[Evergreen.git] / Open-ILS / web / vandelay / vandelay.js
1 /* ---------------------------------------------------------------------------
2 # Copyright (C) 2008  Georgia Public Library Service
3 # Bill Erickson <erickson@esilibrary.com>
4
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.
9
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("fieldmapper.Fieldmapper");
22 dojo.require('openils.CGI');
23 dojo.require('openils.User');
24 dojo.require('openils.Event');
25
26 var authtoken = dojo.cookie('ses') || new openils.CGI().param('ses');
27 var VANDELAY_URL = '/vandelay';
28 var bibAttrDefs = [];
29 var authAttrDefs = [];
30
31 /**
32   * Grab initial data
33   */
34 function vlInit() {
35
36     // Fetch the bib and authority attribute definitions
37     fieldmapper.standardRequest(
38         ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
39         {   async: true,
40             params: [authtoken, {id:{'!=':null}}],
41             onresponse: function(r) {
42                 var def = r.recv().content(); 
43                 if(openils.Event.parse(def)) 
44                     return alert(def);
45                 bibAttrDefs.push(def);
46             },
47         }
48     );
49
50     fieldmapper.standardRequest(
51         ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
52         {   async: true,
53             params: [authtoken, {id:{'!=':null}}],
54             onresponse: function(r) {
55                 var def = r.recv().content(); 
56                 if(openils.Event.parse(def)) 
57                     return alert(def);
58                 authAttrDefs.push(def);
59             }
60         }
61     );
62 }
63
64 /**
65   * asynchronously upload a file of MARC records
66   */
67 function uploadMARC(onload){
68     dojo.byId('vl-ses-input').value = authtoken;
69     dojo.style(dojo.byId('vl-input-td'),"display","none");
70     dojo.style(dojo.byId('vl-upload-progress-span'),"display","inline"); 
71
72     dojo.style(dojo.byId('vl-file-label'), 'display', 'none');
73     dojo.style(dojo.byId('vl-file-uploading'), 'display', 'inline');
74
75     dojo.io.iframe.send({
76         url: VANDELAY_URL,
77         method: "post",
78         handleAs: "html",
79         form: dojo.byId('vl-marc-upload-form'),
80         handle: function(data,ioArgs){
81             var content = data.documentElement.textContent;
82             var key = content.split(/\n/)[2]; /* XXX have to strip the headers.. (why?) */
83             dojo.style(dojo.byId('vl-input-td'),"display","inline");
84             dojo.style(dojo.byId('vl-upload-progress-span'),"display","none");
85             dojo.style(dojo.byId('vl-file-label'), 'display', 'inline');
86             dojo.style(dojo.byId('vl-file-uploading'), 'display', 'none');
87             onload(key);
88         }
89     });
90 }       
91
92 /**
93   * Creates a new vandelay queue
94   */
95 function createQueue(queueName, type, onload) {
96     fieldmapper.standardRequest(
97         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.create'],
98         {   async: true,
99             params: [authtoken, queueName, null, type],
100             oncomplete : function(r) {
101                 var queue = r.recv().content();
102                 if(e = openils.Event.parse(queue)) 
103                     return alert(e);
104                 onload(queue);
105             }
106         }
107     );
108 }
109
110 /**
111   * Tells vendelay to pull a batch of records from the cache and explode them
112   * out into the vandelay tables
113   */
114 function processSpool(key, queue, type, onload) {
115     fieldmapper.standardRequest(
116         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
117         {   async: true,
118             params: [authtoken, key, queue.id()],
119             oncomplete : function(r) {
120                 var queue = r.recv().content();
121                 if(e = openils.Event.parse(queue)) 
122                     return alert(e);
123                 onload();
124             }
125         }
126     );
127 }
128
129 /**
130   * Create queue, upload MARC, process spool, load the newly created queue 
131   */
132 function batchUpload() {
133     var queueName = dijit.byId('vl-queue-name').getValue();
134     var recordType = dijit.byId('vl-record-type').getValue();
135
136     var currentQueue = null;
137
138     var handleProcessSpool = function() {
139         alert('records uploaded and spooled');
140     }
141
142     var handleUploadMARC = function(key) {
143         alert('marc uploaded');
144         processSpool(key, currentQueue, recordType, handleProcessSpool);
145     };
146
147     var handleCreateQueue = function(queue) {
148         alert('queue created ' + queue.name());
149         currentQueue = queue;
150         uploadMARC(handleUploadMARC);
151     };
152
153     createQueue(queueName, recordType, handleCreateQueue);
154 }