after queueing, retrieve the recs and shove them into a grid. need to add attr display
[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("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');
28
29 var authtoken = dojo.cookie('ses') || new openils.CGI().param('ses');
30 var VANDELAY_URL = '/vandelay';
31 var bibAttrDefs = [];
32 var authAttrDefs = [];
33 var queuedRecords = [];
34
35 /**
36   * Grab initial data
37   */
38 function vlInit() {
39
40     // Fetch the bib and authority attribute definitions
41     fieldmapper.standardRequest(
42         ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
43         {   async: true,
44             params: [authtoken, {id:{'!=':null}}],
45             onresponse: function(r) {
46                 var def = r.recv().content(); 
47                 if(openils.Event.parse(def)) 
48                     return alert(def);
49                 bibAttrDefs.push(def);
50             },
51         }
52     );
53
54     fieldmapper.standardRequest(
55         ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
56         {   async: true,
57             params: [authtoken, {id:{'!=':null}}],
58             onresponse: function(r) {
59                 var def = r.recv().content(); 
60                 if(openils.Event.parse(def)) 
61                     return alert(def);
62                 authAttrDefs.push(def);
63             }
64         }
65     );
66 }
67
68 /**
69   * asynchronously upload a file of MARC records
70   */
71 function uploadMARC(onload){
72     dojo.byId('vl-ses-input').value = authtoken;
73     dojo.style(dojo.byId('vl-input-td'),"display","none");
74     dojo.style(dojo.byId('vl-upload-progress-span'),"display","inline"); 
75
76     dojo.style(dojo.byId('vl-file-label'), 'display', 'none');
77     dojo.style(dojo.byId('vl-file-uploading'), 'display', 'inline');
78
79     dojo.io.iframe.send({
80         url: VANDELAY_URL,
81         method: "post",
82         handleAs: "html",
83         form: dojo.byId('vl-marc-upload-form'),
84         handle: function(data,ioArgs){
85             var content = data.documentElement.textContent;
86             var key = content.split(/\n/)[2]; /* XXX have to strip the headers.. (why?) */
87             dojo.style(dojo.byId('vl-input-td'),"display","inline");
88             dojo.style(dojo.byId('vl-upload-progress-span'),"display","none");
89             dojo.style(dojo.byId('vl-file-label'), 'display', 'inline');
90             dojo.style(dojo.byId('vl-file-uploading'), 'display', 'none');
91             onload(key);
92         }
93     });
94 }       
95
96 /**
97   * Creates a new vandelay queue
98   */
99 function createQueue(queueName, type, onload) {
100     fieldmapper.standardRequest(
101         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.create'],
102         {   async: true,
103             params: [authtoken, queueName, null, type],
104             oncomplete : function(r) {
105                 var queue = r.recv().content();
106                 if(e = openils.Event.parse(queue)) 
107                     return alert(e);
108                 onload(queue);
109             }
110         }
111     );
112 }
113
114 /**
115   * Tells vendelay to pull a batch of records from the cache and explode them
116   * out into the vandelay tables
117   */
118 function processSpool(key, queue, type, onload) {
119     fieldmapper.standardRequest(
120         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
121         {   async: true,
122             params: [authtoken, key, queue.id()],
123             oncomplete : function(r) {
124                 var queue = r.recv().content();
125                 if(e = openils.Event.parse(queue)) 
126                     return alert(e);
127                 onload();
128             }
129         }
130     );
131 }
132
133 function retrieveQueuedRecords(type, queueId, onload) {
134     fieldmapper.standardRequest(
135         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.records.retrieve'],
136         {   async: true,
137             params: [authtoken, queueId, {clear_marc:1}],
138             onresponse: function(r) {
139                 var rec = r.recv().content();
140                 if(e = openils.Event.parse(rec))
141                     return alert(e);
142                 queuedRecords.push(rec);
143             },
144             oncomplete: function(){onload();}
145         }
146     );
147 }
148
149 function buildRecordGrid(type) {
150     dojo.style(dojo.byId('vl-marc-upload-div'),"display","none");
151     dojo.style(dojo.byId('vl-queue-div'),"display","block");
152
153     /* test structure... */
154     var structure = [{
155         cells : [[
156             {name: 'ID', field: 'id'},
157             {name: 'Create Time', field: 'create_time'},
158             {name: 'Import Time', field: 'import_time'},
159             {name: 'Purpose', field: 'purpose'},
160         ]]
161     }];
162
163     vlQueueGrid.setStructure(structure);
164
165     var storeData;
166     if(type == 'bib')
167         storeData = vqbr.toStoreData(queuedRecords);
168     else
169         storeData = vqar.toStoreData(queuedRecords);
170
171     var store = new dojo.data.ItemFileReadStore({data:storeData});
172     var model = new dojox.grid.data.DojoData(
173         null, store, {rowsPerPage: 20, clientSort: true, query:{id:'*'}});
174     vlQueueGrid.setModel(model);
175     vlQueueGrid.update();
176 }
177
178 /**
179   * Create queue, upload MARC, process spool, load the newly created queue 
180   */
181 function batchUpload() {
182     var queueName = dijit.byId('vl-queue-name').getValue();
183     var recordType = dijit.byId('vl-record-type').getValue();
184
185     var currentQueue = null;
186
187     var handleRetrieveRecords = function() {
188         alert("building record grid");
189         buildRecordGrid(recordType);
190     }
191
192     var handleProcessSpool = function() {
193         alert('records uploaded and spooled');
194         retrieveQueuedRecords(recordType, currentQueue.id(), handleRetrieveRecords);
195     }
196
197     var handleUploadMARC = function(key) {
198         alert('marc uploaded');
199         processSpool(key, currentQueue, recordType, handleProcessSpool);
200     };
201
202     var handleCreateQueue = function(queue) {
203         alert('queue created ' + queue.name());
204         currentQueue = queue;
205         uploadMARC(handleUploadMARC);
206     };
207
208     createQueue(queueName, recordType, handleCreateQueue);
209 }