9a7c445259e1616806e2f0436eaea78bf8d1a561
[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     dojo.byId('vl-upload-status-count').innerHTML = '0';
219     fieldmapper.standardRequest(
220         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'.process_spool'],
221         {   async: true,
222             params: [authtoken, key, queueId],
223             onresponse : function(r) {
224                 var resp = r.recv().content();
225                 if(e = openils.Event.parse(resp)) 
226                     return alert(e);
227                 dojo.byId('vl-upload-status-count').innerHTML = resp;
228             },
229             oncomplete : function(r) {onload();}
230         }
231     );
232 }
233
234 function retrieveQueuedRecords(type, queueId, onload) {
235     displayGlobalDiv('vl-generic-progress');
236     queuedRecords = [];
237     queuedRecordsMap = {};
238     currentOverlayRecordsMap = {};
239     selectableGridRecords = {};
240     resetVlQueueGridLayout();
241
242     var method = 'open-ils.vandelay.'+type+'_queue.records.retrieve.atomic';
243     if(vlQueueGridShowMatches.checked)
244         method = method.replace('records', 'records.matches');
245
246     var limit = parseInt(vlQueueDisplayLimit.getValue());
247     var offset = limit * parseInt(vlQueueDisplayPage.getValue()-1);
248
249     fieldmapper.standardRequest(
250         ['open-ils.vandelay', method],
251         {   async: true,
252             params: [authtoken, queueId, 
253                 {   clear_marc: 1, 
254                     offset: offset,
255                     limit: limit
256                 }
257             ],
258             /* intermittent bug in streaming, multipart requests prevents use of onreponse for now...
259             onresponse: function(r) {
260                 var rec = r.recv().content();
261                 if(e = openils.Event.parse(rec))
262                     return alert(e);
263                 queuedRecords.push(rec);
264                 queuedRecordsMap[rec.id()] = rec;
265             },
266             */
267             oncomplete: function(r){
268                 var recs = r.recv().content();
269                 if(e = openils.Event.parse(recs[0]))
270                     return alert(e);
271                 for(var i = 0; i < recs.length; i++) {
272                     var rec = recs[i];
273                     queuedRecords.push(rec);
274                     queuedRecordsMap[rec.id()] = rec;
275                 }
276                 onload();
277             }
278         }
279     );
280 }
281
282 function vlLoadMatchUI(recId, attrCode) {
283     displayGlobalDiv('vl-generic-progress');
284     var matches = getRecMatchesFromAttrCode(queuedRecordsMap[recId], attrCode);
285     var records = [];
286     currentImportRecId = recId;
287     for(var i = 0; i < matches.length; i++)
288         records.push(matches[i].eg_record());
289
290     var retrieve = ['open-ils.search', 'open-ils.search.biblio.record_entry.slim.retrieve'];
291     var params = [records];
292     if(currentType == 'auth') {
293         retrieve = ['open-ils.cat', 'open-ils.cat.authority.record.retrieve'];
294         parmas = [authtoken, records, {clear_marc:1}];
295     }
296
297     fieldmapper.standardRequest(
298         retrieve,
299         {   async: true,
300             params:params,
301             oncomplete: function(r) {
302                 var recs = r.recv().content();
303                 if(e = openils.Event.parse(recs))
304                     return alert(e);
305
306                 /* ui mangling */
307                 displayGlobalDiv('vl-match-div');
308                 resetVlMatchGridLayout();
309                 currentMatchedRecords = recs;
310                 vlMatchGrid.setStructure(vlMatchGridLayout);
311
312                 // build the data store or records with match information
313                 var dataStore = bre.toStoreData(recs, null, {virtualFields:['field_type']});
314                 for(var i = 0; i < dataStore.items.length; i++) {
315                     var item = dataStore.items[i];
316                     for(var j = 0; j < matches.length; j++) {
317                         var match = matches[j];
318                         if(match.eg_record() == item.id)
319                             item.field_type = match.field_type();
320                     }
321                 }
322                 // now populate the grid
323                 vlPopulateGrid(vlMatchGrid, dataStore);
324             }
325         }
326     );
327 }
328
329 function vlPopulateGrid(grid, data) {
330     var store = new dojo.data.ItemFileReadStore({data:data});
331     var model = new dojox.grid.data.DojoData(
332         null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
333     grid.setModel(model);
334     grid.update();
335 }
336
337
338 function vlLoadMARCHtml(recId) {
339     displayGlobalDiv('vl-generic-progress');
340     var api = ['open-ils.search', 'open-ils.search.biblio.record.html'];
341     if(currentType == 'auth')
342         api = ['open-ils.search', 'open-ils.search.authority.to_html'];
343     fieldmapper.standardRequest(
344         api, 
345         {   async: true,
346             params: [recId, 1],
347             oncomplete: function(r) {
348             displayGlobalDiv('vl-match-html-div');
349                 var html = r.recv().content();
350                 dojo.byId('vl-match-record-html').innerHTML = html;
351             }
352         }
353     );
354 }
355
356
357 /**
358   * Given a record, an attribute definition code, and a matching record attribute,
359   * this will determine if there are any import matches and build the UI to
360   * represent those matches.  If no matches exist, simply returns the attribute value
361   */
362 function buildAttrColumnUI(rec, attrCode, attr) {
363     var matches = getRecMatchesFromAttrCode(rec, attrCode);
364     if(matches.length > 0) { // found some matches
365         return '<div class="match_div">' +
366             '<a href="javascript:void(0);" onclick="vlLoadMatchUI('+
367             rec.id()+',\''+attrCode+'\');">'+ 
368             attr.attr_value() + '&nbsp;('+matches.length+')</a></div>';
369     }
370
371     return attr.attr_value();
372 }
373
374 function getRecMatchesFromAttrCode(rec, attrCode) {
375     var matches = [];
376     var attr = getRecAttrFromCode(rec, attrCode);
377     for(var j = 0; j < rec.matches().length; j++) {
378         var match = rec.matches()[j];
379         if(match.matched_attr() == attr.id()) 
380             matches.push(match);
381     }
382     return matches;
383 }
384
385 function getRecAttrFromCode(rec, attrCode) {
386     var defId = attrDefMap[attrCode];
387     var attrs = rec.attributes();
388     for(var i = 0; i < attrs.length; i++) {
389         var attr = attrs[i];
390         if(attr.field() == defId) 
391             return attr;
392     }
393     return null;
394 }
395
396 function getAttrValue(rowIdx) {
397     var data = this.grid.model.getRow(rowIdx);
398     if(!data) return '';
399     var attrCode = this.field.split('.')[1];
400     var rec = queuedRecordsMap[data.id];
401     var attr = getRecAttrFromCode(rec, attrCode);
402     if(attr)
403         return buildAttrColumnUI(rec, attrCode, attr);
404     return '';
405 }
406
407 function vlGetDateTimeField(rowIdx) {
408     data = this.grid.model.getRow(rowIdx);
409     if(!data) return '';
410     if(!data[this.field]) return '';
411     var date = dojo.date.stamp.fromISOString(data[this.field]);
412     return dojo.date.locale.format(date, {selector:'date'});
413 }
414
415 function vlGetCreator(rowIdx) {
416     data = this.grid.model.getRow(rowIdx);
417     if(!data) return '';
418     var id = data.creator;
419     if(userCache[id])
420         return userCache[id].usrname();
421     var user = fieldmapper.standardRequest(
422         ['open-ils.actor', 'open-ils.actor.user.retrieve'], [authtoken, id]);
423     if(e = openils.Event.parse(user))
424         return alert(e);
425     userCache[id] = user;
426     return user.usrname();
427 }
428
429 function vlGetViewMARC(rowIdx) {
430     data = this.grid.model.getRow(rowIdx);
431     if(data) 
432         return this.value.replace('RECID', data.id);
433 }
434
435 function vlGetOverlayTargetSelector(rowIdx) {
436     data = this.grid.model.getRow(rowIdx);
437     if(data) {
438         var value = this.value.replace('ID', data.id);
439         var overlay = currentOverlayRecordsMap[currentImportRecId];
440         if(overlay && overlay == data.id) 
441             value = value.replace('/>', 'checked="checked"/>');
442         return value;
443     }
444 }
445
446 /**
447   * see if the user has enabled overlays for the current match set and, 
448   * if so, map the current import record to the overlay target.
449   */
450 function vlHandleOverlayTargetSelected() {
451     if(vlOverlayTargetEnable.checked) {
452         for(var i = 0; i < currentMatchedRecords.length; i++) {
453             var matchRecId = currentMatchedRecords[i].id();
454             if(dojo.byId('vl-overlay-target-'+matchRecId).checked) {
455                 console.log("found overlay target " + matchRecId);
456                 currentOverlayRecordsMap[currentImportRecId] = matchRecId;
457                 dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = true;
458                 dojo.byId('vl-record-list-selected-' + currentImportRecId).parentNode.className = 'overlay_selected';
459                 return;
460             }
461         }
462     } else {
463         delete currentOverlayRecordsMap[currentImportRecId];
464         dojo.byId('vl-record-list-selected-' + currentImportRecId).checked = false;
465     }
466 }
467
468 function buildRecordGrid(type) {
469     displayGlobalDiv('vl-queue-div');
470
471     currentOverlayRecordsMap = {};
472
473     if(!vlQueueGrid.structure) {
474         var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
475         for(var i = 0; i < defs.length; i++) {
476             var def = defs[i]
477             attrDefMap[def.code()] = def.id();
478             var col = {
479                 name:def.description(), 
480                 field:'attr.' + def.code(),
481                 get: getAttrValue,
482                 selectableColumn:true
483             };
484             vlQueueGridLayout[0].cells[0].push(col);
485         }
486     }
487
488     var storeData;
489     if(type == 'bib')
490         storeData = vqbr.toStoreData(queuedRecords);
491     else
492         storeData = vqar.toStoreData(queuedRecords);
493
494     var store = new dojo.data.ItemFileReadStore({data:storeData});
495     var model = new dojox.grid.data.DojoData(
496         null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
497
498     vlQueueGrid.setModel(model);
499     if(vlQueueGridColumePicker) 
500         vlQueueGrid.setStructure(vlQueueGridColumePicker.structure);
501     else
502         vlQueueGrid.setStructure(vlQueueGridLayout);
503     vlQueueGrid.update();
504
505     if(!vlQueueGridColumePicker) {
506         vlQueueGridColumePicker = 
507             new openils.GridColumnPicker(vlQueueGridColumePickerDialog, vlQueueGrid);
508     }
509 }
510
511 function vlDeleteQueue(type, queueId, onload) {
512     fieldmapper.standardRequest(
513         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.delete'],
514         {   async: true,
515             params: [authtoken, queueId],
516             oncomplete: function(r) {
517                 var resp = r.recv().content();
518                 if(e = openils.Event.parse(resp))
519                     return alert(e);
520                 onload();
521             }
522         }
523     );
524 }
525
526
527 function vlQueueGridDrawSelectBox(rowIdx) {
528     var data = this.grid.model.getRow(rowIdx);
529     if(!data) return '';
530     var domId = 'vl-record-list-selected-' +data.id;
531     selectableGridRecords[domId] = data.id;
532     return "<div><input type='checkbox' id='"+domId+"'/></div>";
533 }
534
535 function vlSelectAllQueueGridRecords() {
536     for(var id in selectableGridRecords) 
537         dojo.byId(id).checked = true;
538 }
539 function vlSelectNoQueueGridRecords() {
540     for(var id in selectableGridRecords) 
541         dojo.byId(id).checked = false;
542 }
543 function vlToggleQueueGridSelect() {
544     if(dojo.byId('vl-queue-grid-row-selector').checked)
545         vlSelectAllQueueGridRecords();
546     else
547         vlSelectNoQueueGridRecords();
548 }
549
550 var handleRetrieveRecords = function() {
551     buildRecordGrid(currentType);
552 }
553
554 function vlImportSelectedRecords() {
555     displayGlobalDiv('vl-generic-progress-with-total');
556     var records = [];
557
558     for(var id in selectableGridRecords) {
559         if(dojo.byId(id).checked) {
560             var recId = selectableGridRecords[id];
561             var rec = queuedRecordsMap[recId];
562             if(!rec.import_time()) 
563                 records.push(recId);
564         }
565     }
566
567     fieldmapper.standardRequest(
568         ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
569         {   async: true,
570             params: [authtoken, records, {overlay_map:currentOverlayRecordsMap}],
571             onresponse: function(r) {
572                 var resp = r.recv().content();
573                 if(e = openils.Event.parse(resp))
574                     return alert(e);
575                 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
576             },
577             oncomplete: function() {
578                 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
579             }
580         }
581     );
582 }
583
584 function vlImportRecordQueue(type, queueId, noMatchOnly, onload) {
585     displayGlobalDiv('vl-generic-progress-with-total');
586     var method = 'open-ils.vandelay.bib_queue.import';
587     if(noMatchOnly)
588         method = method.replace('import', 'nomatch.import');
589     if(type == 'auth')
590         method = method.replace('bib', 'auth');
591
592     fieldmapper.standardRequest(
593         ['open-ils.vandelay', method],
594         {   async: true,
595             params: [authtoken, queueId],
596             onresponse: function(r) {
597                 var resp = r.recv().content();
598                 if(e = openils.Event.parse(resp))
599                     return alert(e);
600                 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
601             },
602             oncomplete: function() {onload();}
603         }
604     );
605 }
606
607
608 /**
609   * Create queue, upload MARC, process spool, load the newly created queue 
610   */
611 function batchUpload() {
612     var queueName = dijit.byId('vl-queue-name').getValue();
613     currentType = dijit.byId('vl-record-type').getValue();
614
615     var handleProcessSpool = function() {
616         console.log('records uploaded and spooled');
617         if(vlUploadQueueAutoImport.checked) {
618             vlImportRecordQueue(currentType, currentQueueId, true,  
619                 function() {
620                     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
621                 }
622             );
623         } else {
624             retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
625         }
626     }
627
628     var handleUploadMARC = function(key) {
629         console.log('marc uploaded');
630         dojo.style(dojo.byId('vl-upload-status-processing'), 'display', 'block');
631         processSpool(key, currentQueueId, currentType, handleProcessSpool);
632     };
633
634     var handleCreateQueue = function(queue) {
635         console.log('queue created ' + queue.name());
636         currentQueueId = queue.id();
637         uploadMARC(handleUploadMARC);
638     };
639     
640     if(vlUploadQueueSelector.getValue() && !queueName) {
641         currentQueueId = vlUploadQueueSelector.getValue();
642         console.log('adding records to existing queue ' + currentQueueId);
643         uploadMARC(handleUploadMARC);
644     } else {
645         createQueue(queueName, currentType, handleCreateQueue);
646     }
647 }
648
649
650 function vlFleshQueueSelect(selector, type) {
651     var data = (type == 'bib') ? vbq.toStoreData(userBibQueues) : vaq.toStoreData(userAuthQueues);
652     selector.store = new dojo.data.ItemFileReadStore({data:data});
653     selector.setValue(null);
654     selector.setDisplayedValue('');
655     if(data[0])
656         selector.setValue(data[0].id());
657 }
658
659 function vlShowUploadForm() {
660     displayGlobalDiv('vl-marc-upload-div');
661     vlFleshQueueSelect(vlUploadQueueSelector, vlUploadRecordType.getValue());
662 }
663
664 function vlShowQueueSelect() {
665     displayGlobalDiv('vl-queue-select-div');
666     vlFleshQueueSelect(vlQueueSelectQueueList, vlQueueSelectType.getValue());
667 }
668
669 function vlFetchQueueFromForm() {
670     currentType = vlQueueSelectType.getValue();
671     currentQueueId = vlQueueSelectQueueList.getValue();
672     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
673 }
674
675 dojo.addOnLoad(vlInit);