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