]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/vandelay/vandelay.js
2431e298863812f5e494867d38cb9702b9807c88
[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-upload-status-count').innerHTML = '0';
182     dojo.byId('vl-ses-input').value = authtoken;
183     displayGlobalDiv('vl-marc-upload-status-div');
184     dojo.io.iframe.send({
185         url: VANDELAY_URL,
186         method: "post",
187         handleAs: "html",
188         form: dojo.byId('vl-marc-upload-form'),
189         handle: function(data,ioArgs){
190             var content = data.documentElement.textContent;
191             onload(content);
192         }
193     });
194 }       
195
196 /**
197   * Creates a new vandelay queue
198   */
199 function createQueue(queueName, type, onload) {
200     fieldmapper.standardRequest(
201         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.create'],
202         {   async: true,
203             params: [authtoken, queueName, null, type],
204             oncomplete : function(r) {
205                 var queue = r.recv().content();
206                 if(e = openils.Event.parse(queue)) 
207                     return alert(e);
208                 onload(queue);
209             }
210         }
211     );
212 }
213
214 /**
215   * Tells vendelay to pull a batch of records from the cache and explode them
216   * out into the vandelay tables
217   */
218 function processSpool(key, queueId, type, onload) {
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 var vlQueueGridBuilt = false;
469 function buildRecordGrid(type) {
470     displayGlobalDiv('vl-queue-div');
471
472     currentOverlayRecordsMap = {};
473
474     if(!vlQueueGridBuilt) {
475         var defs = (type == 'bib') ? bibAttrDefs : authAttrDefs;
476         for(var i = 0; i < defs.length; i++) {
477             var def = defs[i]
478             attrDefMap[def.code()] = def.id();
479             var col = {
480                 name:def.description(), 
481                 field:'attr.' + def.code(),
482                 get: getAttrValue,
483                 selectableColumn:true
484             };
485             vlQueueGridLayout[0].cells[0].push(col);
486         }
487         vlQueueGridBuilt = true;
488     }
489
490     var storeData;
491     if(type == 'bib')
492         storeData = vqbr.toStoreData(queuedRecords);
493     else
494         storeData = vqar.toStoreData(queuedRecords);
495
496     var store = new dojo.data.ItemFileReadStore({data:storeData});
497     var model = new dojox.grid.data.DojoData(
498         null, store, {rowsPerPage: 100, clientSort: true, query:{id:'*'}});
499
500     vlQueueGrid.setModel(model);
501     if(vlQueueGridColumePicker) 
502         vlQueueGrid.setStructure(vlQueueGridColumePicker.structure);
503     else
504         vlQueueGrid.setStructure(vlQueueGridLayout);
505     vlQueueGrid.update();
506
507     if(!vlQueueGridColumePicker) {
508         vlQueueGridColumePicker = 
509             new openils.GridColumnPicker(vlQueueGridColumePickerDialog, vlQueueGrid);
510     }
511 }
512
513 function vlDeleteQueue(type, queueId, onload) {
514     fieldmapper.standardRequest(
515         ['open-ils.vandelay', 'open-ils.vandelay.'+type+'_queue.delete'],
516         {   async: true,
517             params: [authtoken, queueId],
518             oncomplete: function(r) {
519                 var resp = r.recv().content();
520                 if(e = openils.Event.parse(resp))
521                     return alert(e);
522                 onload();
523             }
524         }
525     );
526 }
527
528
529 function vlQueueGridDrawSelectBox(rowIdx) {
530     var data = this.grid.model.getRow(rowIdx);
531     if(!data) return '';
532     var domId = 'vl-record-list-selected-' +data.id;
533     selectableGridRecords[domId] = data.id;
534     return "<div><input type='checkbox' id='"+domId+"'/></div>";
535 }
536
537 function vlSelectAllQueueGridRecords() {
538     for(var id in selectableGridRecords) 
539         dojo.byId(id).checked = true;
540 }
541 function vlSelectNoQueueGridRecords() {
542     for(var id in selectableGridRecords) 
543         dojo.byId(id).checked = false;
544 }
545 function vlToggleQueueGridSelect() {
546     if(dojo.byId('vl-queue-grid-row-selector').checked)
547         vlSelectAllQueueGridRecords();
548     else
549         vlSelectNoQueueGridRecords();
550 }
551
552 var handleRetrieveRecords = function() {
553     buildRecordGrid(currentType);
554 }
555
556 function vlImportSelectedRecords() {
557     displayGlobalDiv('vl-generic-progress-with-total');
558     var records = [];
559
560     for(var id in selectableGridRecords) {
561         if(dojo.byId(id).checked) {
562             var recId = selectableGridRecords[id];
563             var rec = queuedRecordsMap[recId];
564             if(!rec.import_time()) 
565                 records.push(recId);
566         }
567     }
568
569     fieldmapper.standardRequest(
570         ['open-ils.vandelay', 'open-ils.vandelay.'+currentType+'_record.list.import'],
571         {   async: true,
572             params: [authtoken, records, {overlay_map:currentOverlayRecordsMap}],
573             onresponse: function(r) {
574                 var resp = r.recv().content();
575                 if(e = openils.Event.parse(resp))
576                     return alert(e);
577                 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
578             },
579             oncomplete: function() {
580                 return retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
581             }
582         }
583     );
584 }
585
586 function vlImportRecordQueue(type, queueId, noMatchOnly, onload) {
587     displayGlobalDiv('vl-generic-progress-with-total');
588     var method = 'open-ils.vandelay.bib_queue.import';
589     if(noMatchOnly)
590         method = method.replace('import', 'nomatch.import');
591     if(type == 'auth')
592         method = method.replace('bib', 'auth');
593
594     fieldmapper.standardRequest(
595         ['open-ils.vandelay', method],
596         {   async: true,
597             params: [authtoken, queueId],
598             onresponse: function(r) {
599                 var resp = r.recv().content();
600                 if(e = openils.Event.parse(resp))
601                     return alert(e);
602                 vlControlledProgressBar.update({maximum:resp.total, progress:resp.progress});
603             },
604             oncomplete: function() {onload();}
605         }
606     );
607 }
608
609
610 /**
611   * Create queue, upload MARC, process spool, load the newly created queue 
612   */
613 function batchUpload() {
614     var queueName = dijit.byId('vl-queue-name').getValue();
615     currentType = dijit.byId('vl-record-type').getValue();
616
617     var handleProcessSpool = function() {
618         console.log('records uploaded and spooled');
619         if(vlUploadQueueAutoImport.checked) {
620             vlImportRecordQueue(currentType, currentQueueId, true,  
621                 function() {
622                     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
623                 }
624             );
625         } else {
626             retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
627         }
628     }
629
630     var handleUploadMARC = function(key) {
631         console.log('marc uploaded');
632         dojo.style(dojo.byId('vl-upload-status-processing'), 'display', 'block');
633         processSpool(key, currentQueueId, currentType, handleProcessSpool);
634     };
635
636     var handleCreateQueue = function(queue) {
637         console.log('queue created ' + queue.name());
638         currentQueueId = queue.id();
639         uploadMARC(handleUploadMARC);
640     };
641     
642     if(vlUploadQueueSelector.getValue() && !queueName) {
643         currentQueueId = vlUploadQueueSelector.getValue();
644         console.log('adding records to existing queue ' + currentQueueId);
645         uploadMARC(handleUploadMARC);
646     } else {
647         createQueue(queueName, currentType, handleCreateQueue);
648     }
649 }
650
651
652 function vlFleshQueueSelect(selector, type) {
653     var data = (type == 'bib') ? vbq.toStoreData(userBibQueues) : vaq.toStoreData(userAuthQueues);
654     selector.store = new dojo.data.ItemFileReadStore({data:data});
655     selector.setValue(null);
656     selector.setDisplayedValue('');
657     if(data[0])
658         selector.setValue(data[0].id());
659 }
660
661 function vlShowUploadForm() {
662     displayGlobalDiv('vl-marc-upload-div');
663     vlFleshQueueSelect(vlUploadQueueSelector, vlUploadRecordType.getValue());
664 }
665
666 function vlShowQueueSelect() {
667     displayGlobalDiv('vl-queue-select-div');
668     vlFleshQueueSelect(vlQueueSelectQueueList, vlQueueSelectType.getValue());
669 }
670
671 function vlFetchQueueFromForm() {
672     currentType = vlQueueSelectType.getValue();
673     currentQueueId = vlQueueSelectQueueList.getValue();
674     retrieveQueuedRecords(currentType, currentQueueId, handleRetrieveRecords);
675 }
676
677 dojo.addOnLoad(vlInit);