]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/z3950.js
consider .auth for services
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / cat / z3950.js
1 dump('entering cat.z3950.js\n');
2
3 if (typeof cat == 'undefined') cat = {};
4 cat.z3950 = function (params) {
5         try {
6                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
7                 JSAN.use('util.error'); this.error = new util.error();
8                 JSAN.use('util.network'); this.network = new util.network();
9         } catch(E) {
10                 dump('cat.z3950: ' + E + '\n');
11         }
12 }
13
14 cat.z3950.prototype = {
15
16         'creds_version' : 2,
17
18     'number_of_result_sets' : 0,
19
20     'result_set' : [],
21
22     'limit' : 10,
23
24         'init' : function( params ) {
25
26                 try {
27                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
28                         JSAN.use('util.widgets');
29
30                         var obj = this;
31
32             JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
33
34                         obj.load_creds();
35
36                         JSAN.use('circ.util');
37                         var columns = circ.util.columns(
38                                 {
39                                         'tcn' : { 'hidden' : false },
40                                         'isbn' : { 'hidden' : false },
41                                         'title' : { 'hidden' : false, 'flex' : '1' },
42                                         'author' : { 'hidden' : false },
43                                         'edition' : { 'hidden' : false },
44                                         'pubdate' : { 'hidden' : false },
45                                         'publisher' : { 'hidden' : false },
46                                         'service' : { 'hidden' : false }
47                                 }
48             );
49
50                         JSAN.use('util.list'); obj.list = new util.list('results');
51                         obj.list.init(
52                                 {
53                                         'columns' : columns,
54                                         'map_row_to_columns' : circ.util.std_map_row_to_columns(),
55                                         'on_select' : function(ev) {
56                                                 try {
57                                                         JSAN.use('util.functional');
58                                                         var sel = obj.list.retrieve_selection();
59                                                         document.getElementById('clip_button').disabled = sel.length < 1;
60                                                         var list = util.functional.map_list(
61                                                                 sel,
62                                                                 function(o) { return o.getAttribute('retrieve_id'); }
63                                                         );
64                                                         obj.error.sdump('D_TRACE','cat/z3950: selection list = ' + js2JSON(list) );
65                                                         obj.controller.view.marc_import.disabled = false;
66                                                         obj.controller.view.marc_import.setAttribute('retrieve_id',list[0]);
67                                                         obj.controller.view.marc_import_overlay.disabled = false;
68                                                         obj.controller.view.marc_import_overlay.setAttribute('retrieve_id',list[0]);
69                                                         obj.controller.view.marc_view.disabled = false;
70                                                         obj.controller.view.marc_view.setAttribute('retrieve_id',list[0]);
71                                                 } catch(E) {
72                                                         obj.error.standard_unexpected_error_alert('Failure during list construction.',E);
73                                                 }
74                                         },
75                                 }
76                         );
77
78                         JSAN.use('util.controller'); obj.controller = new util.controller();
79                         obj.controller.init(
80                                 {
81                                         control_map : {
82                                                 'save_columns' : [ [ 'command' ], function() { obj.list.save_columns(); } ],
83                                                 'sel_clip' : [
84                                                         ['command'],
85                                                         function() { obj.list.clipboard(); }
86                                                 ],
87                                                 'cmd_export' : [
88                                                         ['command'],
89                                                         function() {
90                                                                 obj.list.dump_csv_to_clipboard();
91                                                         }
92                                                 ],
93                                                 'cmd_broken' : [
94                                                         ['command'],
95                                                         function() { alert('Not Yet Implemented'); }
96                                                 ],
97                                                 'result_message' : [['render'],function(e){return function(){};}],
98                                                 'clear' : [
99                                                         ['command'],
100                                                         function() {
101                                                                 obj.clear();
102                                                         }
103                                                 ],
104                                                 'save_creds' : [
105                                                         ['command'],
106                                                         function() {
107                                                                 obj.save_creds();
108                                                         }
109                                                 ],
110                                                 'marc_view' : [
111                                                         ['command'],
112                                                         function(ev) {
113                                                                 try {
114                                                                         var n = obj.controller.view.marc_view;
115                                                                         if (n.getAttribute('toggle') == '1') {
116                                                                                 document.getElementById('deck').selectedIndex = 0;
117                                                                                 n.setAttribute('toggle','0');
118                                                                                 n.setAttribute('label','MARC View');
119                                                                                 document.getElementById('results').focus();
120                                                                         } else {
121                                                                                 document.getElementById('deck').selectedIndex = 1;
122                                                                                 n.setAttribute('toggle','1');
123                                                                                 n.setAttribute('label','Results View');
124                                                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
125                                                                                 var f = get_contentWindow(document.getElementById('marc_frame'));
126                                         var retrieve_id = n.getAttribute('retrieve_id');
127                                         var result_idx = retrieve_id.split('-')[0];
128                                         var record_idx = retrieve_id.split('-')[1];
129                                                                                 f.xulG = { 'marcxml' : obj.result_set[result_idx].records[ record_idx ].marcxml };
130                                                                                 f.my_init();
131                                                                                 f.document.body.firstChild.focus();
132                                                                         }
133                                                                 } catch(E) {
134                                                 obj.error.standard_unexpected_error_alert('Failure during marc view.',E);
135                                                                 }
136                                                         },
137                                                 ],
138                                                 'marc_import' : [
139                                                         ['command'],
140                                                         function() {
141                                 try {
142                                     var retrieve_id = obj.controller.view.marc_import.getAttribute('retrieve_id');
143                                     var result_idx = retrieve_id.split('-')[0];
144                                     var record_idx = retrieve_id.split('-')[1];
145                                     obj.spawn_marc_editor( 
146                                         obj.result_set[ result_idx ].records[ record_idx ].marcxml,
147                                         obj.result_set[ result_idx ].records[ record_idx ].service /* FIXME: we want biblio_source here */
148                                     );
149                                 } catch(E) {
150                                                 obj.error.standard_unexpected_error_alert('Failure during marc import.',E);
151                                 }
152                                                         },
153                                                 ],
154                                                 'marc_import_overlay' : [ 
155                                                         ['command'],
156                                                         function() {
157                                                                 try {
158                                     var retrieve_id = obj.controller.view.marc_import_overlay.getAttribute('retrieve_id');
159                                     var result_idx = retrieve_id.split('-')[0];
160                                     var record_idx = retrieve_id.split('-')[1];
161                                     obj.spawn_marc_editor_for_overlay( 
162                                         obj.result_set[ result_idx ].records[ record_idx ].marcxml,
163                                         obj.result_set[ result_idx ].records[ record_idx ].service /* FIXME: we want biblio_source here */
164                                     );
165                                                                 } catch(E) {
166                                                 obj.error.standard_unexpected_error_alert('Failure during marc import overlay.',E);
167                                                                 }
168                                                         },
169                                                 ],
170                                                 'search' : [
171                                                         ['command'],
172                                                         function() {
173                                                                 obj.initial_search();
174                                                         },
175                                                 ],
176                                                 'page_next' : [
177                                                         ['command'],
178                                                         function() {
179                                                                 obj.page_next();
180                                                         },
181                                                 ],
182                                                 'service_rows' : [
183                                                         ['render'],
184                                                         function(e) {
185                                                                 return function() {
186                                                                         try {
187
188                                                                                 function handle_switch(node) {
189                                             try {
190                                                 obj.active_services = [];
191                                                 var snl = document.getElementsByAttribute('mytype','service_class');
192                                                 for (var i = 0; i < snl.length; i++) {
193                                                     var n = snl[i];
194                                                     if (n.nodeName == 'checkbox') {
195                                                         if (n.checked) obj.active_services.push( n.getAttribute('service') );
196                                                     }
197                                                 }
198                                                 var nl = document.getElementsByAttribute('mytype','search_class');
199                                                 for (var i = 0; i < nl.length; i++) { nl[i].disabled = true; }
200                                                 var attrs = {};
201                                                 for (var j = 0; j < obj.active_services.length; j++) {
202                                                     for (var i in obj.services[obj.active_services[j]].attrs) {
203                                                         var attr = obj.services[obj.active_services[j]].attrs[i];
204                                                         if (! attrs[i]) {
205                                                             attrs[i] = { 'labels' : {} };
206                                                         }
207                                                         if (attr.label) {
208                                                             attrs[i].labels[ attr.label ] = true;
209                                                         } else if (entities['staff.z39_50.search_class.'+i]) {
210                                                             attrs[i].labels[ entities['staff.z39_50.search_class.'+i] ] = true;
211                                                         } else if (attr.name) {
212                                                             attrs[i].labels[ attr.name ] = true;
213                                                         } else {
214                                                             attrs[i].labels[ i ] = true;
215                                                         }
216
217                                                     }
218                                                     
219                                                 }
220
221                                                 function set_label(x,attr) {
222                                                     var labels = [];
223                                                     for (var j in attrs[attr].labels) {
224                                                         labels.push(j);
225                                                     }
226                                                     if (labels.length > 0) {
227                                                         x.setAttribute('value',labels[0]);
228                                                         x.setAttribute('tooltiptext',labels.join(','));
229                                                         if (labels.length > 1) x.setAttribute('class','multiple_labels');
230                                                     }
231                                                 }
232
233                                                 for (var i in attrs) {
234                                                     var x = document.getElementById(i + '_input');
235                                                     if (x) {
236                                                         x.disabled = false;
237                                                         var y = document.getElementById(i + '_label',i);
238                                                         if (y) set_label(y,i);
239                                                     } else {
240                                                         var rows = document.getElementById('query_inputs');
241                                                         var row = document.createElement('row'); rows.appendChild(row);
242                                                         var label = document.createElement('label');
243                                                         label.setAttribute('id',i+'_label');
244                                                         label.setAttribute('control',i+'_input');
245                                                         label.setAttribute('search_class',i);
246                                                         label.setAttribute('style','-moz-user-focus: ignore');
247                                                         row.appendChild(label);
248                                                         set_label(label,i);
249                                                         label.addEventListener('click',function(ev){
250                                                                 var a = ev.target.getAttribute('search_class');
251                                                                 if (a) obj.default_attr = a;
252                                                             },false
253                                                         );
254                                                         var tb = document.createElement('textbox');
255                                                         tb.setAttribute('id',i+'_input');
256                                                         tb.setAttribute('mytype','search_class');
257                                                         tb.setAttribute('search_class',i);
258                                                         row.appendChild(tb);
259                                                         tb.addEventListener('keypress',function(ev) { dump('foo\n'); return obj.handle_enter(ev); },false);
260                                                     }
261                                                 }
262                                             } catch(E) {
263                                                                                         obj.error.standard_unexpected_error_alert('Error setting up search fields.',E);
264                                             }
265                                                                                 }
266
267                                                                                 var robj = obj.network.simple_request(
268                                                                                         'RETRIEVE_Z3950_SERVICES',
269                                                                                         [ ses() ]
270                                                                                 );
271                                                                                 if (typeof robj.ilsevent != 'undefined') throw(robj);
272                                                                                 obj.services = robj;
273                                         var x = document.getElementById('service_rows');
274                                                                                 for (var i in robj) {
275                                             var r = document.createElement('row'); x.appendChild(r);
276                                             var cb = document.createElement('checkbox'); 
277                                                 if (robj[i].label) {
278                                                     cb.setAttribute('label',robj[i].label);
279                                                 } else if (robj[i].name) {
280                                                     cb.setAttribute('label',robj[i].name);
281                                                 } else {
282                                                     cb.setAttribute('label',i);
283                                                 }
284                                                 cb.setAttribute('tooltiptext',i + ' : ' + robj[i].db + '@' + robj[i].host + ':' + robj[i].port); 
285                                                 cb.setAttribute('mytype','service_class'); cb.setAttribute('service',i);
286                                                 cb.setAttribute('id',i+'_service'); r.appendChild(cb);
287                                                 cb.addEventListener('command',handle_switch,false);
288                                             var username = document.createElement('textbox'); username.setAttribute('id',i+'_username'); 
289                                             if (obj.creds.hosts[ obj.data.server_unadorned ] && obj.creds.hosts[ obj.data.server_unadorned ].services[i]) username.setAttribute('value',obj.creds.hosts[ obj.data.server_unadorned ].services[i].username);
290                                             r.appendChild(username);
291                                             if (typeof robj[i].auth != 'undefined') username.hidden = ! get_bool( robj[i].auth );
292                                             var password = document.createElement('textbox'); password.setAttribute('id',i+'_password'); 
293                                             if (obj.creds.hosts[ obj.data.server_unadorned ] && obj.creds.hosts[ obj.data.server_unadorned ].services[i]) password.setAttribute('value',obj.creds.hosts[ obj.data.server_unadorned ].services[i].password);
294                                             password.setAttribute('type','password'); r.appendChild(password);
295                                             if (typeof robj[i].auth != 'undefined') password.hidden = ! get_bool( robj[i].auth );
296                                         }
297                                         setTimeout(
298                                                                                         function() { 
299                                                 if (obj.creds.hosts[ obj.data.server_unadorned ]) {
300                                                     for (var i = 0; i < obj.creds.hosts[ obj.data.server_unadorned ].default_services.length; i++) {
301                                                         var x = document.getElementById(obj.creds.hosts[ obj.data.server_unadorned ].default_services[i]+'_service');
302                                                         if (x) x.checked = true;
303                                                     }
304                                                 } else if (obj.creds.default_service) {
305                                                     var x = document.getElementById(obj.creds.default_service+'_service');
306                                                     if (x) x.checked = true;
307                                                 }
308                                                 handle_switch();
309                                                                                         },0
310                                                                                 );
311                                                                         } catch(E) {
312                                                                                 obj.error.standard_unexpected_error_alert('Z39.50 services not likely retrieved.',E);
313                                                                         }
314                                                                 }
315                                                         }
316                                                 ],
317                                         }
318                                 }
319                         );
320
321                         obj.controller.render();
322
323             setTimeout( function() { obj.focus(); }, 0 );
324
325                 } catch(E) {
326                         this.error.sdump('D_ERROR','cat.z3950.init: ' + E + '\n');
327                 }
328         },
329
330         'focus' : function() {
331                 var obj = this;
332         var focus_me; var or_focus_me;
333         for (var i = 0; i < obj.active_services.length; i++) {
334             if (obj.creds.hosts[ obj.data.server_unadorned ] && obj.creds.hosts[ obj.data.server_unadorned ].services[ obj.active_services[i] ]) {
335                         var x = obj.creds.hosts[ obj.data.server_unadorned ].services[ obj.active_services[i] ].default_attr;
336                 if (x) { focus_me = x; break; }
337             }
338                         for (var i in obj.services[ obj.active_services[i] ].attr) { or_focus_me = i; }
339         }
340         if (! focus_me) focus_me = or_focus_me;
341                 var xx = document.getElementById(focus_me+'_input'); if (xx) xx.focus();
342         },
343
344         'clear' : function() {
345                 var obj = this;
346                 var nl = document.getElementsByAttribute('mytype','search_class');
347                 for (var i = 0; i < nl.length; i++) { nl[i].value = ''; nl[i].setAttribute('value',''); }
348                 //obj.focus(obj.controller.view.service_menu.value);
349         },
350
351         'search_params' : {},
352
353         'initial_search' : function() {
354                 try {
355                         var obj = this;
356             obj.result_set = []; obj.number_of_result_sets = 0;
357                         JSAN.use('util.widgets');
358                         util.widgets.remove_children( obj.controller.view.result_message );
359                         var x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
360             if (obj.active_services.length < 1) {
361                             x.appendChild( document.createTextNode( 'No services selected to search.' ));
362                 return;
363             }
364                         x.appendChild( document.createTextNode( 'Searching...' ));
365                         obj.search_params = {}; obj.list.clear();
366                         obj.controller.view.page_next.disabled = true;
367
368                         obj.search_params.service = []; 
369                         obj.search_params.username = [];
370                         obj.search_params.password = [];
371             for (var i = 0; i < obj.active_services.length; i++) {
372                 obj.search_params.service.push( obj.active_services[i] );
373                 obj.search_params.username.push( document.getElementById( obj.active_services[i]+'_username' ).value );
374                 obj.search_params.password.push( document.getElementById( obj.active_services[i]+'_password' ).value );
375             }
376                         obj.search_params.limit = Math.ceil( obj.limit / obj.active_services.length );
377                         obj.search_params.offset = 0;
378
379                         obj.search_params.search = {};
380                         var nl = document.getElementsByAttribute('mytype','search_class');
381                         var count = 0;
382                         for (var i = 0; i < nl.length; i++) {
383                                 if (nl[i].disabled) continue;
384                                 if (nl[i].value == '') continue;
385                                 count++;
386                                 obj.search_params.search[ nl[i].getAttribute('search_class') ] = nl[i].value;
387                         }
388                         if (count>0) {
389                                 obj.search();
390                         } else {
391                                 util.widgets.remove_children( obj.controller.view.result_message );
392                         }
393                 } catch(E) {
394                         this.error.standard_unexpected_error_alert('Failure during initial search.',E);
395                 }
396         },
397
398         'page_next' : function() {
399                 try {
400                         var obj = this;
401                         JSAN.use('util.widgets');
402                         util.widgets.remove_children( obj.controller.view.result_message );
403                         var x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
404                         x.appendChild( document.createTextNode( 'Retrieving more results...' ));
405                         obj.search_params.offset += obj.search_params.limit;
406                         obj.search();
407                 } catch(E) {
408                         this.error.standard_unexpected_error_alert('Failure during subsequent search.',E);
409                 }
410         },
411
412         'search' : function() {
413                 try {
414                         var obj = this;
415                         var method;
416                         if (typeof obj.search_params.query == 'undefined') {
417                                 method = 'FM_BLOB_RETRIEVE_VIA_Z3950_SEARCH';
418                         } else {
419                                 method = 'FM_BLOB_RETRIEVE_VIA_Z3950_RAW_SEARCH';
420                         }
421                         obj.network.simple_request(
422                                 method,
423                                 [ ses(), obj.search_params ],
424                                 function(req) {
425                                         obj.handle_results(req.getResultObject())
426                                 }
427                         );
428                         document.getElementById('deck').selectedIndex = 0;
429                 } catch(E) {
430                         this.error.standard_unexpected_error_alert('Failure during actual search.',E);
431                 }
432         },
433
434         'handle_results' : function(results) {
435                 var obj = this;
436                 try {
437                         JSAN.use('util.widgets');
438                         util.widgets.remove_children( obj.controller.view.result_message ); var x;
439                         if (results == null) {
440                                 x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
441                                 x.appendChild( document.createTextNode( 'Server Error: request returned null' ));
442                                 return;
443                         }
444                         if (typeof results.ilsevent != 'undefined') {
445                                 x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
446                                 x.appendChild( document.createTextNode( 'Server Error: ' + results.textcode + ' : ' + results.desc ));
447                                 return;
448                         }
449             if (typeof results.length == 'undefined') results = [ results ];
450             for (var i = 0; i < results.length; i++) {
451                 if (results[i].query) {
452                     x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
453                     x.appendChild( document.createTextNode( 'Raw query: ' + results[i].query ));
454                 }
455                 if (results[i].count) {
456                     if (results[i].records) {
457                         x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
458                         var showing = obj.search_params.offset + results[i].records.length; 
459                         x.appendChild(
460                             document.createTextNode( 'Showing ' + (showing > results[i].count ? results[i].count : showing) + ' of ' + results[i].count + ' for ' + results[i].service )
461                         );
462                     }
463                     if (obj.search_params.offset + obj.search_params.limit <= results[i].count) {
464                         obj.controller.view.page_next.disabled = false;
465                     }
466                 } else {
467                         x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
468                         x.appendChild(
469                             document.createTextNode( results[i].count + ' records found')
470                         );
471                 }
472                 if (results[i].records) {
473                     obj.result_set[ ++obj.number_of_result_sets ] = results[i];
474                     obj.controller.view.marc_import.disabled = true;
475                     obj.controller.view.marc_import_overlay.disabled = true;
476                     var x = obj.controller.view.marc_view;
477                     if (x.getAttribute('toggle') == '0') x.disabled = true;
478                     for (var j = 0; j < obj.result_set[ obj.number_of_result_sets ].records.length; j++) {
479                         obj.list.append(
480                             {
481                                 'retrieve_id' : String( obj.number_of_result_sets ) + '-' + String( j ),
482                                 'row' : {
483                                     'my' : {
484                                         'mvr' : function(a){return a;}(obj.result_set[ obj.number_of_result_sets ].records[j].mvr),
485                                         'service' : results[i].service
486                                     }
487                                 }
488                             }
489                         );
490                     }
491                 } else {
492                     x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
493                     x.appendChild(
494                         document.createTextNode( 'Error retrieving results.')
495                     );
496                 }
497             }
498                 } catch(E) {
499                         this.error.standard_unexpected_error_alert('Failure during search result handling.',E);
500                 }
501         },
502
503         'replace_tab_with_opac' : function(doc_id) {
504                 var opac_url = xulG.url_prefix( urls.opac_rdetail ) + '?r=' + doc_id;
505                 var content_params = { 
506                         'session' : ses(),
507                         'authtime' : ses('authtime'),
508                         'opac_url' : opac_url,
509                 };
510                 xulG.set_tab(
511                         xulG.url_prefix(urls.XUL_OPAC_WRAPPER), 
512                         {'tab_name':'Retrieving title...'}, 
513                         content_params
514                 );
515         },
516
517         'spawn_marc_editor' : function(my_marcxml,biblio_source) {
518                 var obj = this;
519                 xulG.new_tab(
520                         xulG.url_prefix(urls.XUL_MARC_EDIT), 
521                         { 'tab_name' : 'MARC Editor' }, 
522                         { 
523                                 'record' : { 'marc' : my_marcxml },
524                                 'save' : {
525                                         'label' : 'Import Record',
526                                         'func' : function (new_marcxml) {
527                                                 try {
528                                                         var r = obj.network.simple_request('MARC_XML_RECORD_IMPORT', [ ses(), new_marcxml, biblio_source ]);
529                                                         if (typeof r.ilsevent != 'undefined') {
530                                                                 switch(r.ilsevent) {
531                                                                         case 1704 /* TCN_EXISTS */ :
532                                                                                 var msg = 'A record with TCN ' + r.payload.tcn + ' already exists.\nFIXME: add record summary here';
533                                                                                 var title = 'Import Collision';
534                                                                                 var btn1 = 'Overlay';
535                                                                                 var btn2 = typeof r.payload.new_tcn == 'undefined' ? null : 'Import with alternate TCN ' + r.payload.new_tcn;
536                                                                                 if (btn2) {
537                                                                                         obj.data.init({'via':'stash'});
538                                                                                         var robj = obj.network.simple_request(
539                                                                                                 'PERM_CHECK',[
540                                                                                                         ses(),
541                                                                                                         obj.data.list.au[0].id(),
542                                                                                                         obj.data.list.au[0].ws_ou(),
543                                                                                                         [ 'ALLOW_ALT_TCN' ]
544                                                                                                 ]
545                                                                                         );
546                                                                                         if (typeof robj.ilsevent != 'undefined') {
547                                                                                                 obj.error.standard_unexpected_error_alert('check permission',E);
548                                                                                         }
549                                                                                         if (robj.length != 0) btn2 = null;
550                                                                                 }
551                                                                                 var btn3 = 'Cancel Import';
552                                                                                 var p = obj.error.yns_alert(msg,title,btn1,btn2,btn3,'Check here to confirm this action');
553                                                                                 obj.error.sdump('D_ERROR','option ' + p + 'chosen');
554                                                                                 switch(p) {
555                                                                                         case 0:
556                                                                                                 var r3 = obj.network.simple_request('MARC_XML_RECORD_UPDATE', [ ses(), r.payload.dup_record, new_marcxml, biblio_source ]);
557                                                                                                 if (typeof r3.ilsevent != 'undefined') {
558                                                                                                         throw(r3);
559                                                                                                 } else {
560                                                                                                         alert('Record successfully overlayed.');
561                                                                                                         obj.replace_tab_with_opac(r3.id());
562                                                                                                 }
563                                                                                         break;
564                                                                                         case 1:
565                                                                                                 var r2 = obj.network.request(
566                                                                                                         api.MARC_XML_RECORD_IMPORT.app,
567                                                                                                         api.MARC_XML_RECORD_IMPORT.method + '.override',
568                                                                                                         [ ses(), new_marcxml, biblio_source ]
569                                                                                                 );
570                                                                                                 if (typeof r2.ilsevent != 'undefined') {
571                                                                                                         throw(r2);
572                                                                                                 } else {
573                                                                                                         alert('Record successfully imported with alternate TCN.');
574                                                                                                         obj.replace_tab_with_opac(r2.id());
575                                                                                                 }
576                                                                                         break;
577                                                                                         case 2:
578                                                                                         default:
579                                                                                                 alert('Record import cancelled');
580                                                                                         break;
581                                                                                 }
582                                                                         break;
583                                                                         default:
584                                                                                 throw(r);
585                                                                         break;
586                                                                 }
587                                                         } else {
588                                                                 alert('Record successfully imported.');
589                                                                 obj.replace_tab_with_opac(r.id());
590                                                         }
591                                                 } catch(E) {
592                                                         obj.error.standard_unexpected_error_alert('Record not likely imported.',E);
593                                                 }
594                                         }
595                                 }
596                         } 
597                 );
598         },
599
600         'confirm_overlay' : function(record_ids) {
601                 var obj = this; // JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
602                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
603                 var top_xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" >';
604                 top_xml += '<description>Overlay this record?</description>';
605                 top_xml += '<hbox><button id="lead" disabled="false" label="Overlay" name="fancy_submit" accesskey="O"/><button label="Cancel" accesskey="C" name="fancy_cancel"/></hbox></vbox>';
606
607                 var xml = '<form xmlns="http://www.w3.org/1999/xhtml">';
608                 xml += '<table width="100%"><tr valign="top">';
609                 for (var i = 0; i < record_ids.length; i++) {
610                         xml += '<td nowrap="nowrap"><iframe src="' + urls.XUL_BIB_BRIEF; 
611                         xml += '?docid=' + record_ids[i] + '"/></td>';
612                 }
613                 xml += '</tr><tr valign="top">';
614                 for (var i = 0; i < record_ids.length; i++) {
615                         html = obj.network.simple_request('MARC_HTML_RETRIEVE',[ record_ids[i] ]);
616                         xml += '<td nowrap="nowrap"><iframe style="min-height: 1000px; min-width: 300px;" flex="1" src="data:text/html,' + window.escape(html) + '"/></td>';
617                 }
618                 xml += '</tr></table></form>';
619                 // data.temp_merge_top = top_xml; data.stash('temp_merge_top');
620                 // data.temp_merge_mid = xml; data.stash('temp_merge_mid');
621                 JSAN.use('util.window'); var win = new util.window();
622                 var fancy_prompt_data = win.open(
623                         urls.XUL_FANCY_PROMPT,
624                         // + '?xml_in_stash=temp_merge_mid'
625                         // + '&top_xml_in_stash=temp_merge_top'
626                         // + '&title=' + window.escape('Record Overlay'),
627                         'fancy_prompt', 'chrome,resizable,modal,width=700,height=500',
628                         { 'top_xml' : top_xml, 'xml' : xml, 'title' : 'Record Overlay' }
629                 );
630                 //data.stash_retrieve();
631                 if (fancy_prompt_data.fancy_status == 'incomplete') { alert('Overlay Aborted'); return false; }
632                 return true;
633         },
634
635         'spawn_marc_editor_for_overlay' : function(my_marcxml,biblio_source) {
636                 var obj = this;
637                 obj.data.init({'via':'stash'});
638                 if (!obj.data.marked_record) {
639                         alert('Please mark a record for overlay from within the catalog and try this again.');
640                         return;
641                 }
642
643                 xulG.new_tab(
644                         xulG.url_prefix(urls.XUL_MARC_EDIT), 
645                         { 'tab_name' : 'MARC Editor' }, 
646                         { 
647                                 'record' : { 'marc' : my_marcxml },
648                                 'save' : {
649                                         'label' : 'Overlay Record',
650                                         'func' : function (new_marcxml) {
651                                                 try {
652                                                         if (! obj.confirm_overlay( [ obj.data.marked_record ] ) ) { return; }
653                                                         var r = obj.network.simple_request('MARC_XML_RECORD_REPLACE', [ ses(), obj.data.marked_record, new_marcxml, biblio_source ]);
654                                                         if (typeof r.ilsevent != 'undefined') {
655                                                                 switch(r.ilsevent) {
656                                                                         case 1704 /* TCN_EXISTS */ :
657                                                                                 var msg = 'A record with TCN ' + r.payload.tcn + ' already exists.\nFIXME: add record summary here';
658                                                                                 var title = 'Import Collision';
659                                                                                 var btn1 = typeof r.payload.new_tcn == 'undefined' ? null : 'Overlay with alternate TCN ' + r.payload.new_tcn;
660                                                                                 if (btn1) {
661                                                                                         var robj = obj.network.simple_request(
662                                                                                                 'PERM_CHECK',[
663                                                                                                         ses(),
664                                                                                                         obj.data.list.au[0].id(),
665                                                                                                         obj.data.list.au[0].ws_ou(),
666                                                                                                         [ 'ALLOW_ALT_TCN' ]
667                                                                                                 ]
668                                                                                         );
669                                                                                         if (typeof robj.ilsevent != 'undefined') {
670                                                                                                 obj.error.standard_unexpected_error_alert('check permission',E);
671                                                                                         }
672                                                                                         if (robj.length != 0) btn1 = null;
673                                                                                 }
674                                                                                 var btn2 = 'Cancel Import';
675                                                                                 var p = obj.error.yns_alert(msg,title,btn1,btn2,null,'Check here to confirm this action');
676                                                                                 obj.error.sdump('D_ERROR','option ' + p + 'chosen');
677                                                                                 switch(p) {
678                                                                                         case 0:
679                                                                                                 var r2 = obj.network.request(
680                                                                                                         api.MARC_XML_RECORD_REPLACE.app,
681                                                                                                         api.MARC_XML_RECORD_REPLACE.method + '.override',
682                                                                                                         [ ses(), obj.data.marked_record, new_marcxml, biblio_source ]
683                                                                                                 );
684                                                                                                 if (typeof r2.ilsevent != 'undefined') {
685                                                                                                         throw(r2);
686                                                                                                 } else {
687                                                                                                         alert('Record successfully overlayed with alternate TCN.');
688                                                                                                         obj.replace_tab_with_opac(r2.id());
689                                                                                                 }
690                                                                                         break;
691                                                                                         case 1:
692                                                                                         default:
693                                                                                                 alert('Record overlay cancelled');
694                                                                                         break;
695                                                                                 }
696                                                                         break;
697                                                                         default:
698                                                                                 throw(r);
699                                                                         break;
700                                                                 }
701                                                         } else {
702                                                                 alert('Record successfully overlayed.');
703                                                                 obj.replace_tab_with_opac(r.id());
704                                                         }
705                                                 } catch(E) {
706                                                         obj.error.standard_unexpected_error_alert('Record not likely overlayed.',E);
707                                                 }
708                                         }
709                                 }
710                         } 
711                 );
712         },
713
714
715         'load_creds' : function() {
716                 var obj = this;
717                 try {
718                         obj.creds = { 'version' : g.save_version, 'services' : {}, 'hosts' : {} };
719                         /*
720                                 {
721                                         'version' : xx,
722                                         'default_service' : xx,
723                                         'services' : {
724
725                                                 'xx' : {
726                                                         'username' : xx,
727                                                         'password' : xx,
728                                                         'default_attr' : xx,
729                                                 },
730
731                                                 'xx' : {
732                                                         'username' : xx,
733                                                         'password' : xx,
734                                                         'default_attr' : xx,
735                                                 },
736                                         },
737                     // new in version 2
738                     'hosts' : {
739                         'xxxx' : {
740                             'default_services' : [ xx, ... ],
741                             'services' : {
742
743                                 'xx' : {
744                                     'username' : xx,
745                                     'password' : xx,
746                                     'default_attr' : xx,
747                                 },
748
749                                 'xx' : {
750                                     'username' : xx,
751                                     'password' : xx,
752                                     'default_attr' : xx,
753                                 },
754                             },
755                         }
756                     }
757                                 }
758                         */
759                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
760                         JSAN.use('util.file'); var file = new util.file('z3950_store');
761                         if (file._file.exists()) {
762                                 var creds = file.get_object(); file.close();
763                                 if (typeof creds.version != 'undefined') {
764                                         if (creds.version >= obj.creds_version) {  /* so apparently, this guy is assuming that future versions will be backwards compatible */
765                         if (typeof creds.hosts == 'undefined') creds.hosts = {};
766                                                 obj.creds = creds;
767                                         }
768                                 }
769                         }
770                 } catch(E) {
771                         obj.error.standard_unexpected_error_alert('Error retrieving stored z39.50 credentials',E);
772                 }
773         },
774
775         'save_creds' : function () {
776                 try {
777                         var obj = this;
778             if (typeof obj.creds.hosts == 'undefined') obj.creds.hosts = {};
779             if (typeof obj.creds.hosts[ obj.data.server_unadorned ] == 'undefined') obj.creds.hosts[ obj.data.server_unadorned ] = { 'services' : {} };
780             obj.creds.hosts[ obj.data.server_unadorned ].default_services = obj.active_services;
781             for (var i = 0; i < obj.creds.hosts[ obj.data.server_unadorned ].default_services.length; i++) {
782                             var service = obj.creds.hosts[ obj.data.server_unadorned ].default_services[i];
783                         if (typeof obj.creds.hosts[ obj.data.server_unadorned ].services[ service ] == 'undefined') {
784                     obj.creds.hosts[ obj.data.server_unadorned ].services[ service ] = {}
785                         }
786                         obj.creds.hosts[ obj.data.server_unadorned ].services[service].username = document.getElementById(service + '_username').value;
787                         obj.creds.hosts[ obj.data.server_unadorned ].services[service].password = document.getElementById(service + '_password').value;
788                         if (obj.default_attr) {
789                                 obj.creds.hosts[ obj.data.server_unadorned ].services[service].default_attr = obj.default_attr;
790                         }
791             }
792                         obj.creds.version = obj.creds_version;
793                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
794                         JSAN.use('util.file'); var file = new util.file('z3950_store');
795                         file.set_object(obj.creds);
796                         file.close();
797                 } catch(E) {
798                         obj.error.standard_unexpected_error_alert('Problem storing z39.50 credentials.',E);
799                 }
800         },
801
802         'handle_enter' : function(ev) {
803                 var obj = this;
804                 if (ev.target.tagName != 'textbox') return;
805                 if (ev.keyCode == 13 /* enter */ || ev.keyCode == 77 /* enter on a mac */) setTimeout( function() { obj.initial_search(); }, 0);
806         },
807 }
808
809 dump('exiting cat.z3950.js\n');