]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/common/vlagent.js
Vandelay-based record matching and import for Acquisitions
[working/Evergreen.git] / Open-ILS / web / js / ui / default / acq / common / vlagent.js
1 dojo.require('openils.widget.AutoFieldWidget');
2 dojo.require('openils.PermaCrud');
3
4 function VLAgent(args) {
5     args = args || {};
6     for (var key in args) { 
7         this[key] = args[key]; 
8     }
9
10     this.widgets = [  
11         {key : 'import_no_match'},
12         {key : 'auto_overlay_exact'},
13         {key : 'auto_overlay_1match'},
14         {key : 'auto_overlay_best_match'},
15         {key : 'match_quality_ratio'},
16         {key : 'queue_name'},
17         {key : 'match_set', cls : 'vms'},
18         {key : 'bib_source', cls : 'cbs'},
19         {key : 'merge_profile', cls : 'vmp'},
20         {key : 'fall_through_merge_profile', cls : 'vmp'},
21         {key : 'existing_queue', cls : 'vbq'}
22     ];
23     
24     this.loaded = false;
25
26     this.init = function() {
27         var self = this;
28
29         dojo.forEach(this.widgets,
30             function(widg) {
31                 if (widg.cls) { // selectors
32
33                     new openils.widget.AutoFieldWidget({
34                         fmClass : widg.cls,
35                         selfReference : true,
36                         orgLimitPerms : [self.limitPerm || 'CREATE_PURCHASE_ORDER'],
37                         parentNode : dojo.byId('acq_vl:' + widg.key),
38                         searchFilter : (widg.cls == 'vbq') ? {queue_type : 'acq'} : null,
39                         useWriteStore :  (widg.cls == 'vbq')
40                     }).build(function(dijit) { 
41                         widg.dijit = dijit; 
42                         self.attachOnChange(widg);
43                     }); 
44
45                 } else { // bools
46                     widg.dijit = dijit.byId('acq_vl:' + widg.key);
47                     self.attachOnChange(widg);
48                 }
49             }
50         );
51         
52         // loaded != all widgets are done rendering,
53         // only that init() has been called.
54         this.loaded = true;
55     }
56
57     this.attachOnChange = function(widg) {
58         var self = this;
59         var qInputChange;
60
61         var qSelChange = function(val) {
62             // user selected a queue from the selector;  clear the text input 
63             // and set the item import profile already defined for the queue
64
65             var qInput = self.getDijit('queue_name');
66             var matchSetSelector = self.getDijit('match_set');
67             var qSelector = self.getDijit('existing_queue');
68
69             if(val) {
70                 qSelector.store.fetch({
71                     query : {id : val+''},
72                     onComplete : function(items) {
73                         matchSetSelector.attr('value', items[0].match_set[0] || '');
74                         matchSetSelector.attr('disabled', true);
75                     }
76                 });
77             } else {
78                 matchSetSelector.attr('value', '');
79                 matchSetSelector.attr('disabled', false);
80             }
81
82             // detach and reattach to avoid onchange firing while when we clear
83             dojo.disconnect(qInput._onchange);
84             qInput.attr('value', '');
85             qInput._onchange = dojo.connect(qInput, 'onChange', qInputChange);
86         }
87
88         qInputChange = function(val) {
89
90             var qSelector = self.getDijit('existing_queue');
91             var matchSetSelector = self.getDijit('match_set');
92             var foundMatch = false;
93
94             if (val) {
95
96                 // if the user entered the name of an existing queue, update the 
97                 // queue selector to match the value (and clear the text input 
98                 // via qselector onchange)
99                 qSelector.store.fetch({
100                     query:{name:val},
101                     onComplete:function(items) {
102                         if(items.length == 0) return;
103                         var item = items[0];
104                         qSelector.attr('value', item.id);
105                         foundMatch = true;
106                     }
107                 });
108             }
109
110             if (!foundMatch) {
111                 self.getDijit('match_set').attr('disabled', false);
112                 dojo.disconnect(qSelector._onchange);
113                 qSelector.attr('value', '');
114                 qSelector._onchange = dojo.connect(qSelector, 'onChange', qSelChange);
115             }
116         }
117
118         if (widg.key == 'existing_queue') {
119             var qSelector = self.getDijit('existing_queue');
120             qSelector._onchange = dojo.connect(qSelector, 'onChange', qSelChange);
121         } else if(widg.key == 'queue_name') {
122             var qInput = self.getDijit('queue_name');
123             qInput._onchange = dojo.connect(qInput, 'onChange', qInputChange);
124         }
125     }
126
127     this.getDijit = function(key) {
128         return this.widgets.filter(function(w) {return (w.key == key)})[0].dijit;
129     }
130
131     this.values = function() {
132         var values = {};
133         dojo.forEach(this.widgets,
134             function(widg) {
135                 values[widg.key] = widg.dijit.attr('value');
136             }
137         );
138         return values;
139     }
140
141     this.handleResponse = function(resp, oncomplete) {
142         if(!resp) return;
143         var res = {}
144
145         console.log('vandelay import returned : ' + js2JSON(resp));
146
147         // update the display counts
148         dojo.byId('acq_vl:li-processed').innerHTML = resp.li;
149         dojo.byId('acq_vl:vqbr-processed').innerHTML = resp.vqbr;
150         dojo.byId('acq_vl:bibs-processed').innerHTML = resp.bibs;
151         dojo.byId('acq_vl:lid-processed').innerHTML = resp.lid;
152         dojo.byId('acq_vl:debits-processed').innerHTML = resp.debits_accrued;
153         dojo.byId('acq_vl:copies-processed').innerHTML = resp.copies;
154
155         if (resp.complete) {
156
157             if(resp.picklist) {
158                 res.picklist_url = oilsBasePath + '/acq/picklist/view/' + resp.picklist.id();
159             } 
160
161             if(resp.purchase_order) {
162                 res.po_url = oilsBasePath + '/acq/po/view/' + resp.purchase_order.id();
163             }
164
165             if (resp.queue) {
166                 var newQid = resp.queue.id();
167                 res.queue_url = oilsBasePath + '/vandelay/vandelay?qtype=bib&qid=' + newQid;
168
169                 var qInput = this.getDijit('queue_name');
170
171                 if (newQName = qInput.attr('value')) {
172                     // user created a new queue.  Fetch the new queue object,
173                     // replace the ReadStore with a WriteStore and insert.
174                     qInput.attr('value', '');
175                     var qSelector = this.getDijit('existing_queue');
176                     var newQ = new openils.PermaCrud().retrieve('vbq', newQid);
177                     qSelector.store.newItem(newQ.toStoreItem());
178                     qSelector.attr('value', newQid);
179                 }
180             }
181
182             if (oncomplete) 
183                 oncomplete(resp, res);
184
185             return res;
186         }
187
188         return false; // not yet complete
189     }
190 }