added upload-to-existing-queue. styling records to overlay slightly differently
[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("dijit.layout.ContentPane");
21 dojo.require("dijit.layout.TabContainer");
22 dojo.require("dojo.cookie");
23 dojo.require("dojox.grid.Grid");
24 dojo.require("dojo.data.ItemFileReadStore");
25 dojo.require('dojo.date.locale');
26 dojo.require('dojo.date.stamp');
27 dojo.require("fieldmapper.Fieldmapper");
28 dojo.require("fieldmapper.dojoData");
29 dojo.require('openils.CGI');
30 dojo.require('openils.User');
31 dojo.require('openils.Event');
32
33 var globalDivs = [
34     'vl-generic-progress',
35     'vl-generic-progress-with-total',
36     'vl-marc-upload-div',
37     'vl-queue-div',
38     'vl-match-div',
39     'vl-match-html-div',
40     'vl-queue-select-div'
41 ];
42
43 var authtoken;
44 var VANDELAY_URL = '/vandelay';
45 var bibAttrDefs = [];
46 var authAttrDefs = [];
47 var queuedRecords = [];
48 var queuedRecordsMap = {};
49 var bibAttrsFetched = false;
50 var authAttrsFetched = false;
51 var attrDefMap = {}; // maps attr def code names to attr def ids
52 var currentType;
53 var currentQueueId = null;
54 var userCache = {};
55 var currentMatchedRecords; // set of loaded matched bib records
56 var currentOverlayRecordsMap; // map of import record to overlay record
57 var currentImportRecId; // when analyzing matches, this is the current import record
58 var userBibQueues;
59 var userAuthQueues;
60 var selectableGridRecords;
61 var cgi = new openils.CGI();
62
63 /**
64   * Grab initial data
65   */
66 function vlInit() {
67     authtoken = dojo.cookie('ses') || cgi.param('ses');
68     var initNeeded = 4; // how many async responses do we need before we're init'd 
69     var initCount = 0; // how many async reponses we've received
70
71     function checkInitDone() {
72         initCount++;
73         if(initCount == initNeeded)
74             runStartupCommands();
75     }
76
77     // Fetch the bib and authority attribute definitions
78     fieldmapper.standardRequest(
79         ['open-ils.permacrud', 'open-ils.permacrud.search.vqbrad'],
80         {   async: true,
81             params: [authtoken, {id:{'!=':null}}],
82             onresponse: function(r) {
83                 var def = r.recv().content(); 
84                 if(e = openils.Event.parse(def[0])) 
85                     return alert(e);
86                 bibAttrDefs.push(def);
87             },
88             oncomplete: function() {
89                 bibAttrDefs = bibAttrDefs.sort(
90                     function(a, b) {
91                         if(a.id() > b.id()) return 1;
92                         if(a.id() < b.id()) return -1;
93                         return 0;
94                     }
95                 );
96                 checkInitDone();
97             }
98         }
99     );
100
101     fieldmapper.standardRequest(
102         ['open-ils.permacrud', 'open-ils.permacrud.search.vqarad'],
103         {   async: true,
104             params: [authtoken, {id:{'!=':null}}],
105             onresponse: function(r) {
106                 var def = r.recv().content(); 
107                 if(e = openils.Event.parse(def[0])) 
108                     return alert(e);
109                 authAttrDefs.push(def);
110             },
111             oncomplete: function() {
112                 authAttrDefs = authAttrDefs.sort(
113                     function(a, b) {
114                         if(a.id() > b.id()) return 1;
115                         if(a.id() < b.id()) return -1;
116                         return 0;
117                     }
118                 );
119                 checkInitDone();
120             }
121         }
122     );
123
124     fieldmapper.standardRequest(
125         ['open-ils.vandelay', 'open-ils.vandelay.bib_queue.owner.retrieve.atomic'],
126         {   async: true,
127             params: [authtoken],
128             oncomplete: function(r) {
129                 var list = r.recv().content();
130                 if(e = openils.Event.parse(list[0]))
131                     return alert(e);
132                 userBibQueues = list;
133                 checkInitDone();
134             }
135         }
136     );
137
138     fieldmapper.standardRequest(
139         ['open-ils.vandelay', 'open-ils.vandelay.authority_queue.owner.retrieve.atomic'],
140         {   async: true,
141             params: [authtoken],
142             oncomplete: function(r) {
143                 var list = r.recv().content();
144                 if(e = openils.Event.parse(list[0]))
145                     return alert(e);
146                 userAuthQueues = list;
147                 checkInitDone();
148             }
149         }
150     );
151 }
152
153 function displayGlobalDiv(id) {
154     for(var i = 0; i < globalDivs.length; i++) {
155         try {
156             dojo.style(dojo.byId(globalDivs[i]), 'display', 'none');
157         } catch(e) {
158             alert('please define ' + globalDivs[i]);
159         }
160     }
161     dojo.style(dojo.byId(id),'display','block');
162 }
163
164 function runStartupCommands() {
165     currentQueueId = cgi.param('qid');
166     currentType = cgi.param('qtype');
167     if(currentQueueId)
168         return retrievenueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
169     vlShowUploadForm();
170 }
171
172 /**
173   * asynchronously upload a file of MARC records
174   */
175 function uploadMARC(onload){
176     dojo.byId('vl-ses-input').value = authtoken;
177     dojo.style(dojo.byId('vl-input-td'),"display","none");
178     dojo.style(dojo.byId('vl-upload-progress-span'),"display","inline"); 
179
180     dojo.style(dojo.byId('vl-file-label'), 'display', 'none');
181     dojo.style(dojo.byId('vl-file-uploading'), 'display', 'inline');
182
183     dojo.io.iframe.send({
184         url: VANDELAY_URL,
185         method: "post",
186         handleAs: "html",
187         form: dojo.byId('vl-marc-upload-form'),
188         handle: function(data,ioArgs){
189             var content = data.documentElement.textContent;
190             dojo.style(dojo.byId('vl-input-td'),"display","inline");
191             dojo.style(dojo.byId('vl-upload-progress-span'),"display","none");
192             dojo.style(dojo.byId('vl-file-label'), 'display', 'inline');
193             dojo.style(dojo.byId('vl-file-uploading'), 'display', 'none');
194             onload(content);
195         }
196     });
197 }       
198
199 /**
200   * Creates a new vandelay queue
201   */
202 function createQueue(queueName, type, onload) {
203     fieldmapper.standardRequest(
204         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.create'],
205         {   async: true,
206             params: [authtoken, queueName, null, type],
207             oncomplete : function(r) {
208                 var queue = r.recv().content();
209                 if(e = openils.Event.parse(queue)) 
210                     return alert(e);
211                 onload(queue);
212             }
213         }
214     );
215 }
216
217 /**
218   * Tells vendelay to pull a batch of records from the cache and explode them
219   * out into the vandelay tables
220   */
221 function processSpool(key, queueId, type, onload) {
222     fieldmapper.standardRequest(
223         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
224         {   async: true,
225             params: [authtoken, key, queueId],
226             oncomplete : function(r) {
227                 var resp = r.recv().content();
228                 if(e = openils.Event.parse(resp)) 
229                     return alert(e);
230                 onload();
231             }
232         }
233     );
234 }
235
236 function retrieveQueuedRecords(type, queueId, onload) {
237     displayGlobalDiv('vl-generic-progress');
238     queuedRecords = [];
239     queuedRecordsMap = {};
240     currentOverlayRecordsMap = {};
241     selectableGridRecords = {};
242     resetVlQueueGridLayout();
243
244     fieldmapper.standardRequest(
245         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.records.retrieve.atomic'],
246         {   async: true,
247             params: [authtoken, queueId, {clear_marc:1}],
248             /* intermittent bug in streaming, multipart requests prevents use of onreponse for now...
249             onresponse: function(r) {
250                 var rec = r.recv().content();
251                 if(e = openils.Event.parse(rec))
252                     return alert(e);
253                 queuedRecords.push(rec);
254                 queuedRecordsMap[rec.id()] = rec;
255             },
256             */
257             oncomplete: function(r){
258                 var recs = r.recv().content();
259                 if(e = openils.Event.parse(recs[0]))
260                     return alert(e);
261                 for(var i = 0; i < recs.length; i++) {
262                     var rec = recs[i];
263                     queuedRecords.push(rec);
264                     queuedRecordsMap[rec.id()] = rec;
265                 }
266                 onload();
267             }
268         }
269     );
270 }
271
272 function vlLoadMatchUI(recId, attrCode) {
273     displayGlobalDiv('vl-generic-progress');
274     var matches = getRecMatchesFromAttrCode(queuedRecordsMap[recId], attrCode);
275     var records = [];
276     currentImportRecId = recId;
277     for(var i = 0; i < matches.length; i++)
278         records.push(matches[i].eg_record());
279     fieldmapper.standardRequest(
280         ['open-ils.search', 'open-ils.search.biblio.record_entry.slim.retrieve'],
281         {   async: true,
282             params:[records],
283             oncomplete: function(r) {
284                 var recs = r.recv().content();
285                 if(e = openils.Event.parse(recs))
286                     return alert(e);
287                 displayGlobalDiv('vl-match-div');
288                 resetVlMatchGridLayout();
289                 currentMatchedRecords = recs;
290                 vlMatchGrid.setStructure(vlMatchGridLayout);
291                 var dataStore = bre.toStoreData(recs, null, {virtualFields:['field_type']});
292                 for(var i = 0; i < dataStore.items.length; i++) {
293                     var item = dataStore.items[i];
294                     for(var j = 0; j < matches.length; j++) {
295                         var match = matches[j];
296                         if(match.eg_record() == item.id)
297                             item.field_type = match.field_type();
298                     }
299                 }
300                 var store = new dojo.data.ItemFileReadStore({data:dataStore});
301                 var model = new dojox.grid.data.DojoData(
302                     null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
303                 vlMatchGrid.setModel(model);
304                 vlMatchGrid.update();
305             }
306         }
307     );
308 }
309
310
311 function vlLoadMARCHtml(recId) {
312     displayGlobalDiv('vl-generic-progress');
313     fieldmapper.standardRequest(
314         ['open-ils.search', 'open-ils.search.biblio.record.html'],
315         {   async: true,
316             params: [recId, 1],
317             oncomplete: function(r) {
318             displayGlobalDiv('vl-match-html-div');
319                 var html = r.recv().content();
320                 dojo.byId('vl-match-record-html').innerHTML = html;
321             }
322         }
323     );
324 }
325
326
327 /**
328   * Given a record, an attribute definition code, and a matching record attribute,
329   * this will determine if there are any import matches and build the UI to
330   * represent those matches.  If no matches exist, simply returns the attribute value
331   */
332 function buildAttrColumnUI(rec, attrCode, attr) {
333     var matches = getRecMatchesFromAttrCode(rec, attrCode);
334     if(matches.length > 0) { // found some matches
335         return '<div class="match_div">' +
336             '<a href="javascript:void(0);" onclick="vlLoadMatchUI('+
337             rec.id()+',\''+attrCode+'\');">'+ 
338             attr.attr_value() + '&nbsp;('+matches.length+')</a></div>';
339     }
340
341     return attr.attr_value();
342 }
343
344 function getRecMatchesFromAttrCode(rec, attrCode) {
345     var matches = [];
346     var attr = getRecAttrFromCode(rec, attrCode);
347     for(var j = 0; j < rec.matches().length; j++) {
348         var match = rec.matches()[j];
349         if(match.matched_attr() == attr.id()) 
350             matches.push(match);
351     }
352     return matches;
353 }
354
355 function getRecAttrFromCode(rec, attrCode) {
356     var defId = attrDefMap[attrCode];
357     var attrs = rec.attributes();
358     for(var i = 0; i < attrs.length; i++) {
359         var attr = attrs[i];
360         if(attr.field() == defId) 
361             return attr;
362     }
363     return null;
364 }
365
366 function getAttrValue(rowIdx) {
367     var data = this.grid.model.getRow(rowIdx);
368     if(!data) return '';
369     var attrCode = this.field.split('.')[1];
370     console.log(attrCode + " : " + data.id + ' : ' + queuedRecordsMap[data.id] + " : count = " + queuedRecords.length);
371     var rec = queuedRecordsMap[data.id];
372     var attr = getRecAttrFromCode(rec, attrCode);
373     if(attr)
374         return buildAttrColumnUI(rec, attrCode, attr);
375     return '';
376 }
377
378 function vlGetDateTimeField(rowIdx) {
379     data = this.grid.model.getRow(rowIdx);
380     if(!data) return '';
381     if(!data[this.field]) return '';
382     var date = dojo.date.stamp.fromISOString(data[this.field]);
383     return dojo.date.locale.format(date, {selector:'date'});
384 }
385
386 function vlGetCreator(rowIdx) {
387     data = this.grid.model.getRow(rowIdx);
388     if(!data) return '';
389     var id = data.creator;
390     if(userCache[id])
391         return userCache[id].usrname();
392     var user = fieldmapper.standardRequest(['open-ils.actor', 'open-ils.actor.user.retrieve'], [authtoken, id]);
393     if(e = openils.Event.parse(user))
394         return alert(e);
395     userCache[id] = user;
396     return user.usrname();
397 }
398
399 function vlGetViewMARC(rowIdx) {
400     data = this.grid.model.getRow(rowIdx);
401     if(data) 
402         return this.value.replace('RECID', data.id);
403 }
404
405 function vlGetOverlayTargetSelector(rowIdx) {
406     data = this.grid.model.getRow(rowIdx);
407     if(data) {
408         var value = this.value.replace('ID', data.id);
409         var overlay = currentOverlayRecordsMap[currentImportRecId];
410         if(overlay && overlay == data.id) 
411             value = value.replace('/>', 'checked="checked"/>');
412         return value;
413     }
414 }
415
416 /**
417   * see if the user has enabled overlays for the current match set and, 
418   * if so, map the current import record to the overlay target.
419   */
420 function vlHandleOverlayTargetSelected() {
421     if(vlOverlayTargetEnable.checked) {
422         for(var i = 0; i < currentMatchedRecords.length; i++) {
423             var matchRecId = currentMatchedRecords[i].id();
424             if(dojo.byId('vl-overlay-target-'+matchRecId).checked) {
425                 console.log("found overlay target " + matchRecId);
426                 currentOverlayRecordsMap[currentImportRecId] = matchRecId;
427                 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = true;
428                 dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = 'overlay_selected';
429                 return;
430             }
431         }
432     } else {
433         delete currentOverlayRecordsMap[currentImportRecId];
434         dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = false;
435     }
436 }
437
438 function buildRecordGrid(type) {
439     displayGlobalDiv('vl-queue-div');
440
441     currentOverlayRecordsMap = {};
442
443     if(queuedRecords.length == 0) {
444         dojo.style(dojo.byId('vl-queue-no-records'), 'display', 'block');
445         dojo.style(dojo.byId('vl-queue-div-grid'), 'display', 'none');
446         return;
447     } else {
448         dojo.style(dojo.byId('vl-queue-no-records'), 'display', 'none');
449         dojo.style(dojo.byId('vl-queue-div-grid'), 'display', 'block');
450     }
451
452     var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
453     for(var i = 0; i < defs.length; i++) {
454         var def = defs[i]
455         attrDefMap[def.code()] = def.id();
456         var col = {
457             name:def.description(), 
458             field:'attr.' + def.code(),
459             get: getAttrValue
460         };
461         //if(def.code().match(/title/i)) col.width = 'auto'; // this is hack.
462         vlQueueGridLayout[0].cells[0].push(col);
463     }
464
465     var storeData;
466     if(type == 'bib')
467         storeData = vqbr.toStoreData(queuedRecords);
468     else
469         storeData = vqar.toStoreData(queuedRecords);
470
471     var store = new dojo.data.ItemFileReadStore({data:storeData});
472     var model = new dojox.grid.data.DojoData(
473         null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
474
475     vlQueueGrid.setModel(model);
476     vlQueueGrid.setStructure(vlQueueGridLayout);
477     vlQueueGrid.update();
478 }
479
480 /*
481 function test() {
482     alert(vlQueueGridLayout.picker);
483     vlQueueGridLayout.oils = {};
484     vlQueueGridLayout[0].cells[0].pop();
485     vlQueueGridLayout[0].cells[0].pop();
486     vlQueueGridLayout[0].cells[0].pop();
487     vlQueueGridLayout[0].cells[0].pop();
488     vlQueueGridLayout[0].cells[0].pop();
489     vlQueueGrid.setStructure(vlQueueGridLayout);
490     vlQueueGrid.update();
491 }
492 */
493
494 function vlQueueGridDrawSelectBox(rowIdx) {
495     var data = this.grid.model.getRow(rowIdx);
496     if(!data) return '';
497     var domId = 'vl-record-list-selected-' +data.id;
498     selectableGridRecords[domId] = data.id;
499     return "<div><input type='checkbox' id='"+domId+"'/></div>";
500 }
501
502 function vlSelectAllGridRecords() {
503     for(var id in selectableGridRecords) 
504         dojo.byId(id).checked = true;
505 }
506 function vlSelectNoGridRecords() {
507     for(var id in selectableGridRecords) 
508         dojo.byId(id).checked = false;
509 }
510
511 var handleRetrieveRecords = function() {
512     buildRecordGrid(currentType);
513 }
514
515 function vlImportSelectedRecords() {
516     displayGlobalDiv('vl-generic-progress-with-total');
517     var records = [];
518
519     for(var id in selectableGridRecords) {
520         if(dojo.byId(id).checked) {
521             var recId = selectableGridRecords[id];
522             var rec = queuedRecordsMap[recId];
523             if(!rec.import_time()) 
524                 records.push(recId);
525         }
526     }
527
528     fieldmapper.standardRequest(
529         ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
530         {   async: true,
531             params: [authtoken, records, {overlay_map:currentOverlayRecordsMap}],
532             onresponse: function(r) {
533                 var resp = r.recv().content();
534                 if(e = openils.Event.parse(resp))
535                     return alert(e);
536                 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
537             },
538             oncomplete: function() {
539                 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
540             }
541         }
542     );
543 }
544
545
546 /**
547   * Create queue, upload MARC, process spool, load the newly created queue 
548   */
549 function batchUpload() {
550     var queueName = dijit.byId('vl-queue-name').getValue();
551     currentType = dijit.byId('vl-record-type').getValue();
552
553     var handleProcessSpool = function() {
554         console.log('records uploaded and spooled');
555         retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
556     }
557
558     var handleUploadMARC = function(key) {
559         console.log('marc uploaded');
560         processSpool(key, currentQueueId, currentType, handleProcessSpool);
561     };
562
563     var handleCreateQueue = function(queue) {
564         console.log('queue created ' + queue.name());
565         currentQueueId = queue.id();
566         uploadMARC(handleUploadMARC);
567     };
568     
569     if(vlUploadQueueSelector.getValue() && !queueName) {
570         currentQueueId = vlUploadQueueSelector.getValue();
571         console.log('adding records to existing queue ' + currentQueueId);
572         uploadMARC(handleUploadMARC);
573     } else {
574         createQueue(queueName, currentType, handleCreateQueue);
575     }
576 }
577
578
579 function vlFleshQueueSelect(selector, type) {
580     var data = (type == 'bib') ? vbq.toStoreData(userBibQueues) : vaq.toStoreData(userAuthQueues);
581     selector.store = new dojo.data.ItemFileReadStore({data:data});
582     selector.setValue(null);
583     selector.setDisplayedValue('');
584     if(data[0])
585         selector.setValue(data[0].id());
586 }
587
588 function vlShowUploadForm() {
589     displayGlobalDiv('vl-marc-upload-div');
590     vlFleshQueueSelect(vlUploadQueueSelector, vlUploadRecordType.getValue());
591 }
592
593 function vlShowQueueSelect() {
594     displayGlobalDiv('vl-queue-select-div');
595     vlFleshQueueSelect(vlQueueSelectQueueList, vlQueueSelectType.getValue());
596 }
597
598 function vlFetchQueueFromForm() {
599     currentType = vlQueueSelectType.getValue();
600     currentQueueId = vlQueueSelectQueueList.getValue();
601     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
602 }
603
604 dojo.addOnLoad(vlInit);