]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/z3950.js
hardcode native evergreen catalog as a virtual z-source, and show 0 records found...
[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                                                     if (obj.services[obj.active_services[j]]) 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                                         document.getElementById('native-evergreen-catalog_service').addEventListener('command',handle_switch,false);
268
269                                                                                 var robj = obj.network.simple_request(
270                                                                                         'RETRIEVE_Z3950_SERVICES',
271                                                                                         [ ses() ]
272                                                                                 );
273                                                                                 if (typeof robj.ilsevent != 'undefined') throw(robj);
274                                                                                 obj.services = robj;
275                                         var x = document.getElementById('service_rows');
276                                                                                 for (var i in robj) {
277                                             var r = document.createElement('row'); x.appendChild(r);
278                                             var cb = document.createElement('checkbox'); 
279                                                 if (robj[i].label) {
280                                                     cb.setAttribute('label',robj[i].label);
281                                                 } else if (robj[i].name) {
282                                                     cb.setAttribute('label',robj[i].name);
283                                                 } else {
284                                                     cb.setAttribute('label',i);
285                                                 }
286                                                 cb.setAttribute('tooltiptext',i + ' : ' + robj[i].db + '@' + robj[i].host + ':' + robj[i].port); 
287                                                 cb.setAttribute('mytype','service_class'); cb.setAttribute('service',i);
288                                                 cb.setAttribute('id',i+'_service'); r.appendChild(cb);
289                                                 cb.addEventListener('command',handle_switch,false);
290                                             var username = document.createElement('textbox'); username.setAttribute('id',i+'_username'); 
291                                             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);
292                                             r.appendChild(username);
293                                             if (typeof robj[i].auth != 'undefined') username.hidden = ! get_bool( robj[i].auth );
294                                             var password = document.createElement('textbox'); password.setAttribute('id',i+'_password'); 
295                                             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);
296                                             password.setAttribute('type','password'); r.appendChild(password);
297                                             if (typeof robj[i].auth != 'undefined') password.hidden = ! get_bool( robj[i].auth );
298                                         }
299                                         obj.services[ 'native-evergreen-catalog' ] = { 'attrs' : { 'author' : {}, 'title' : {} } };
300                                         setTimeout(
301                                                                                         function() { 
302                                                 if (obj.creds.hosts[ obj.data.server_unadorned ]) {
303                                                     for (var i = 0; i < obj.creds.hosts[ obj.data.server_unadorned ].default_services.length; i++) {
304                                                         var x = document.getElementById(obj.creds.hosts[ obj.data.server_unadorned ].default_services[i]+'_service');
305                                                         if (x) x.checked = true;
306                                                     }
307                                                 } else if (obj.creds.default_service) {
308                                                     var x = document.getElementById(obj.creds.default_service+'_service');
309                                                     if (x) x.checked = true;
310                                                 }
311                                                 handle_switch();
312                                                                                         },0
313                                                                                 );
314                                                                         } catch(E) {
315                                                                                 obj.error.standard_unexpected_error_alert('Z39.50 services not likely retrieved.',E);
316                                                                         }
317                                                                 }
318                                                         }
319                                                 ],
320                                         }
321                                 }
322                         );
323
324                         obj.controller.render();
325
326             setTimeout( function() { obj.focus(); }, 0 );
327
328                 } catch(E) {
329                         this.error.sdump('D_ERROR','cat.z3950.init: ' + E + '\n');
330                 }
331         },
332
333         'focus' : function() {
334                 var obj = this;
335         var focus_me; var or_focus_me;
336         for (var i = 0; i < obj.active_services.length; i++) {
337             if (obj.creds.hosts[ obj.data.server_unadorned ] && obj.creds.hosts[ obj.data.server_unadorned ].services[ obj.active_services[i] ]) {
338                         var x = obj.creds.hosts[ obj.data.server_unadorned ].services[ obj.active_services[i] ].default_attr;
339                 if (x) { focus_me = x; break; }
340             }
341             if (ob.services[ obj.active_services[i] ]) for (var i in obj.services[ obj.active_services[i] ].attr) { or_focus_me = i; }
342         }
343         if (! focus_me) focus_me = or_focus_me;
344                 var xx = document.getElementById(focus_me+'_input'); if (xx) xx.focus();
345         },
346
347         'clear' : function() {
348                 var obj = this;
349                 var nl = document.getElementsByAttribute('mytype','search_class');
350                 for (var i = 0; i < nl.length; i++) { nl[i].value = ''; nl[i].setAttribute('value',''); }
351                 //obj.focus(obj.controller.view.service_menu.value);
352         },
353
354         'search_params' : {},
355
356         'initial_search' : function() {
357                 try {
358                         var obj = this;
359             obj.result_set = []; obj.number_of_result_sets = 0;
360                         JSAN.use('util.widgets');
361                         util.widgets.remove_children( obj.controller.view.result_message );
362                         var x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
363             if (obj.active_services.length < 1) {
364                             x.appendChild( document.createTextNode( 'No services selected to search.' ));
365                 return;
366             }
367                         x.appendChild( document.createTextNode( 'Searching...' ));
368                         obj.search_params = {}; obj.list.clear();
369                         obj.controller.view.page_next.disabled = true;
370
371                         obj.search_params.service = []; 
372                         obj.search_params.username = [];
373                         obj.search_params.password = [];
374             for (var i = 0; i < obj.active_services.length; i++) {
375                 obj.search_params.service.push( obj.active_services[i] );
376                 obj.search_params.username.push( document.getElementById( obj.active_services[i]+'_username' ).value );
377                 obj.search_params.password.push( document.getElementById( obj.active_services[i]+'_password' ).value );
378             }
379                         obj.search_params.limit = Math.ceil( obj.limit / obj.active_services.length );
380                         obj.search_params.offset = 0;
381
382                         obj.search_params.search = {};
383                         var nl = document.getElementsByAttribute('mytype','search_class');
384                         var count = 0;
385                         for (var i = 0; i < nl.length; i++) {
386                                 if (nl[i].disabled) continue;
387                                 if (nl[i].value == '') continue;
388                                 count++;
389                                 obj.search_params.search[ nl[i].getAttribute('search_class') ] = nl[i].value;
390                         }
391                         if (count>0) {
392                                 obj.search();
393                         } else {
394                                 util.widgets.remove_children( obj.controller.view.result_message );
395                         }
396                 } catch(E) {
397                         this.error.standard_unexpected_error_alert('Failure during initial search.',E);
398                 }
399         },
400
401         'page_next' : function() {
402                 try {
403                         var obj = this;
404                         JSAN.use('util.widgets');
405                         util.widgets.remove_children( obj.controller.view.result_message );
406                         var x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
407                         x.appendChild( document.createTextNode( 'Retrieving more results...' ));
408                         obj.search_params.offset += obj.search_params.limit;
409                         obj.search();
410                 } catch(E) {
411                         this.error.standard_unexpected_error_alert('Failure during subsequent search.',E);
412                 }
413         },
414
415         'search' : function() {
416                 try {
417                         var obj = this;
418                         var method;
419                         if (typeof obj.search_params.query == 'undefined') {
420                                 method = 'FM_BLOB_RETRIEVE_VIA_Z3950_SEARCH';
421                         } else {
422                                 method = 'FM_BLOB_RETRIEVE_VIA_Z3950_RAW_SEARCH';
423                         }
424                         obj.network.simple_request(
425                                 method,
426                                 [ ses(), obj.search_params ],
427                                 function(req) {
428                                         obj.handle_results(req.getResultObject())
429                                 }
430                         );
431                         document.getElementById('deck').selectedIndex = 0;
432                 } catch(E) {
433                         this.error.standard_unexpected_error_alert('Failure during actual search.',E);
434                 }
435         },
436
437         'handle_results' : function(results) {
438                 var obj = this;
439                 try {
440                         JSAN.use('util.widgets');
441                         util.widgets.remove_children( obj.controller.view.result_message ); var x;
442                         if (results == null) {
443                                 x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
444                                 x.appendChild( document.createTextNode( 'Server Error: request returned null' ));
445                                 return;
446                         }
447                         if (typeof results.ilsevent != 'undefined') {
448                                 x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
449                                 x.appendChild( document.createTextNode( 'Server Error: ' + results.textcode + ' : ' + results.desc ));
450                                 return;
451                         }
452             if (typeof results.length == 'undefined') results = [ results ];
453             for (var i = 0; i < results.length; i++) {
454                 if (results[i].query) {
455                     x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
456                     x.appendChild( document.createTextNode( 'Raw query: ' + results[i].query ));
457                 }
458                 if (results[i].count) {
459                     if (results[i].records) {
460                         x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
461                         var showing = obj.search_params.offset + results[i].records.length; 
462                         x.appendChild(
463                             document.createTextNode( 'Showing ' + (showing > results[i].count ? results[i].count : showing) + ' of ' + results[i].count + ' for ' + results[i].service )
464                         );
465                     }
466                     if (obj.search_params.offset + obj.search_params.limit <= results[i].count) {
467                         obj.controller.view.page_next.disabled = false;
468                     }
469                 } else {
470                         x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
471                         x.appendChild(
472                             document.createTextNode( (results[i].count ? results[i].count : 0) + ' records found')
473                         );
474                 }
475                 if (results[i].records) {
476                     obj.result_set[ ++obj.number_of_result_sets ] = results[i];
477                     obj.controller.view.marc_import.disabled = true;
478                     obj.controller.view.marc_import_overlay.disabled = true;
479                     var x = obj.controller.view.marc_view;
480                     if (x.getAttribute('toggle') == '0') x.disabled = true;
481                     for (var j = 0; j < obj.result_set[ obj.number_of_result_sets ].records.length; j++) {
482                         obj.list.append(
483                             {
484                                 'retrieve_id' : String( obj.number_of_result_sets ) + '-' + String( j ),
485                                 'row' : {
486                                     'my' : {
487                                         'mvr' : function(a){return a;}(obj.result_set[ obj.number_of_result_sets ].records[j].mvr),
488                                         'service' : results[i].service
489                                     }
490                                 }
491                             }
492                         );
493                     }
494                 } else {
495                     x = document.createElement('description'); obj.controller.view.result_message.appendChild(x);
496                     x.appendChild(
497                         document.createTextNode( 'Error retrieving results.')
498                     );
499                 }
500             }
501                 } catch(E) {
502                         this.error.standard_unexpected_error_alert('Failure during search result handling.',E);
503                 }
504         },
505
506         'replace_tab_with_opac' : function(doc_id) {
507                 var opac_url = xulG.url_prefix( urls.opac_rdetail ) + '?r=' + doc_id;
508                 var content_params = { 
509                         'session' : ses(),
510                         'authtime' : ses('authtime'),
511                         'opac_url' : opac_url,
512                 };
513                 xulG.set_tab(
514                         xulG.url_prefix(urls.XUL_OPAC_WRAPPER), 
515                         {'tab_name':'Retrieving title...'}, 
516                         content_params
517                 );
518         },
519
520         'spawn_marc_editor' : function(my_marcxml,biblio_source) {
521                 var obj = this;
522                 xulG.new_tab(
523                         xulG.url_prefix(urls.XUL_MARC_EDIT), 
524                         { 'tab_name' : 'MARC Editor' }, 
525                         { 
526                                 'record' : { 'marc' : my_marcxml },
527                                 'save' : {
528                                         'label' : 'Import Record',
529                                         'func' : function (new_marcxml) {
530                                                 try {
531                                                         var r = obj.network.simple_request('MARC_XML_RECORD_IMPORT', [ ses(), new_marcxml, biblio_source ]);
532                                                         if (typeof r.ilsevent != 'undefined') {
533                                                                 switch(r.ilsevent) {
534                                                                         case 1704 /* TCN_EXISTS */ :
535                                                                                 var msg = 'A record with TCN ' + r.payload.tcn + ' already exists.\nFIXME: add record summary here';
536                                                                                 var title = 'Import Collision';
537                                                                                 var btn1 = 'Overlay';
538                                                                                 var btn2 = typeof r.payload.new_tcn == 'undefined' ? null : 'Import with alternate TCN ' + r.payload.new_tcn;
539                                                                                 if (btn2) {
540                                                                                         obj.data.init({'via':'stash'});
541                                                                                         var robj = obj.network.simple_request(
542                                                                                                 'PERM_CHECK',[
543                                                                                                         ses(),
544                                                                                                         obj.data.list.au[0].id(),
545                                                                                                         obj.data.list.au[0].ws_ou(),
546                                                                                                         [ 'ALLOW_ALT_TCN' ]
547                                                                                                 ]
548                                                                                         );
549                                                                                         if (typeof robj.ilsevent != 'undefined') {
550                                                                                                 obj.error.standard_unexpected_error_alert('check permission',E);
551                                                                                         }
552                                                                                         if (robj.length != 0) btn2 = null;
553                                                                                 }
554                                                                                 var btn3 = 'Cancel Import';
555                                                                                 var p = obj.error.yns_alert(msg,title,btn1,btn2,btn3,'Check here to confirm this action');
556                                                                                 obj.error.sdump('D_ERROR','option ' + p + 'chosen');
557                                                                                 switch(p) {
558                                                                                         case 0:
559                                                                                                 var r3 = obj.network.simple_request('MARC_XML_RECORD_UPDATE', [ ses(), r.payload.dup_record, new_marcxml, biblio_source ]);
560                                                                                                 if (typeof r3.ilsevent != 'undefined') {
561                                                                                                         throw(r3);
562                                                                                                 } else {
563                                                                                                         alert('Record successfully overlayed.');
564                                                                                                         obj.replace_tab_with_opac(r3.id());
565                                                                                                 }
566                                                                                         break;
567                                                                                         case 1:
568                                                                                                 var r2 = obj.network.request(
569                                                                                                         api.MARC_XML_RECORD_IMPORT.app,
570                                                                                                         api.MARC_XML_RECORD_IMPORT.method + '.override',
571                                                                                                         [ ses(), new_marcxml, biblio_source ]
572                                                                                                 );
573                                                                                                 if (typeof r2.ilsevent != 'undefined') {
574                                                                                                         throw(r2);
575                                                                                                 } else {
576                                                                                                         alert('Record successfully imported with alternate TCN.');
577                                                                                                         obj.replace_tab_with_opac(r2.id());
578                                                                                                 }
579                                                                                         break;
580                                                                                         case 2:
581                                                                                         default:
582                                                                                                 alert('Record import cancelled');
583                                                                                         break;
584                                                                                 }
585                                                                         break;
586                                                                         default:
587                                                                                 throw(r);
588                                                                         break;
589                                                                 }
590                                                         } else {
591                                                                 alert('Record successfully imported.');
592                                                                 obj.replace_tab_with_opac(r.id());
593                                                         }
594                                                 } catch(E) {
595                                                         obj.error.standard_unexpected_error_alert('Record not likely imported.',E);
596                                                 }
597                                         }
598                                 }
599                         } 
600                 );
601         },
602
603         'confirm_overlay' : function(record_ids) {
604                 var obj = this; // JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
605                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
606                 var top_xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" >';
607                 top_xml += '<description>Overlay this record?</description>';
608                 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>';
609
610                 var xml = '<form xmlns="http://www.w3.org/1999/xhtml">';
611                 xml += '<table width="100%"><tr valign="top">';
612                 for (var i = 0; i < record_ids.length; i++) {
613                         xml += '<td nowrap="nowrap"><iframe src="' + urls.XUL_BIB_BRIEF; 
614                         xml += '?docid=' + record_ids[i] + '"/></td>';
615                 }
616                 xml += '</tr><tr valign="top">';
617                 for (var i = 0; i < record_ids.length; i++) {
618                         html = obj.network.simple_request('MARC_HTML_RETRIEVE',[ record_ids[i] ]);
619                         xml += '<td nowrap="nowrap"><iframe style="min-height: 1000px; min-width: 300px;" flex="1" src="data:text/html,' + window.escape(html) + '"/></td>';
620                 }
621                 xml += '</tr></table></form>';
622                 // data.temp_merge_top = top_xml; data.stash('temp_merge_top');
623                 // data.temp_merge_mid = xml; data.stash('temp_merge_mid');
624                 JSAN.use('util.window'); var win = new util.window();
625                 var fancy_prompt_data = win.open(
626                         urls.XUL_FANCY_PROMPT,
627                         // + '?xml_in_stash=temp_merge_mid'
628                         // + '&top_xml_in_stash=temp_merge_top'
629                         // + '&title=' + window.escape('Record Overlay'),
630                         'fancy_prompt', 'chrome,resizable,modal,width=700,height=500',
631                         { 'top_xml' : top_xml, 'xml' : xml, 'title' : 'Record Overlay' }
632                 );
633                 //data.stash_retrieve();
634                 if (fancy_prompt_data.fancy_status == 'incomplete') { alert('Overlay Aborted'); return false; }
635                 return true;
636         },
637
638         'spawn_marc_editor_for_overlay' : function(my_marcxml,biblio_source) {
639                 var obj = this;
640                 obj.data.init({'via':'stash'});
641                 if (!obj.data.marked_record) {
642                         alert('Please mark a record for overlay from within the catalog and try this again.');
643                         return;
644                 }
645
646                 xulG.new_tab(
647                         xulG.url_prefix(urls.XUL_MARC_EDIT), 
648                         { 'tab_name' : 'MARC Editor' }, 
649                         { 
650                                 'record' : { 'marc' : my_marcxml },
651                                 'save' : {
652                                         'label' : 'Overlay Record',
653                                         'func' : function (new_marcxml) {
654                                                 try {
655                                                         if (! obj.confirm_overlay( [ obj.data.marked_record ] ) ) { return; }
656                                                         var r = obj.network.simple_request('MARC_XML_RECORD_REPLACE', [ ses(), obj.data.marked_record, new_marcxml, biblio_source ]);
657                                                         if (typeof r.ilsevent != 'undefined') {
658                                                                 switch(r.ilsevent) {
659                                                                         case 1704 /* TCN_EXISTS */ :
660                                                                                 var msg = 'A record with TCN ' + r.payload.tcn + ' already exists.\nFIXME: add record summary here';
661                                                                                 var title = 'Import Collision';
662                                                                                 var btn1 = typeof r.payload.new_tcn == 'undefined' ? null : 'Overlay with alternate TCN ' + r.payload.new_tcn;
663                                                                                 if (btn1) {
664                                                                                         var robj = obj.network.simple_request(
665                                                                                                 'PERM_CHECK',[
666                                                                                                         ses(),
667                                                                                                         obj.data.list.au[0].id(),
668                                                                                                         obj.data.list.au[0].ws_ou(),
669                                                                                                         [ 'ALLOW_ALT_TCN' ]
670                                                                                                 ]
671                                                                                         );
672                                                                                         if (typeof robj.ilsevent != 'undefined') {
673                                                                                                 obj.error.standard_unexpected_error_alert('check permission',E);
674                                                                                         }
675                                                                                         if (robj.length != 0) btn1 = null;
676                                                                                 }
677                                                                                 var btn2 = 'Cancel Import';
678                                                                                 var p = obj.error.yns_alert(msg,title,btn1,btn2,null,'Check here to confirm this action');
679                                                                                 obj.error.sdump('D_ERROR','option ' + p + 'chosen');
680                                                                                 switch(p) {
681                                                                                         case 0:
682                                                                                                 var r2 = obj.network.request(
683                                                                                                         api.MARC_XML_RECORD_REPLACE.app,
684                                                                                                         api.MARC_XML_RECORD_REPLACE.method + '.override',
685                                                                                                         [ ses(), obj.data.marked_record, new_marcxml, biblio_source ]
686                                                                                                 );
687                                                                                                 if (typeof r2.ilsevent != 'undefined') {
688                                                                                                         throw(r2);
689                                                                                                 } else {
690                                                                                                         alert('Record successfully overlayed with alternate TCN.');
691                                                                                                         obj.replace_tab_with_opac(r2.id());
692                                                                                                 }
693                                                                                         break;
694                                                                                         case 1:
695                                                                                         default:
696                                                                                                 alert('Record overlay cancelled');
697                                                                                         break;
698                                                                                 }
699                                                                         break;
700                                                                         default:
701                                                                                 throw(r);
702                                                                         break;
703                                                                 }
704                                                         } else {
705                                                                 alert('Record successfully overlayed.');
706                                                                 obj.replace_tab_with_opac(r.id());
707                                                         }
708                                                 } catch(E) {
709                                                         obj.error.standard_unexpected_error_alert('Record not likely overlayed.',E);
710                                                 }
711                                         }
712                                 }
713                         } 
714                 );
715         },
716
717
718         'load_creds' : function() {
719                 var obj = this;
720                 try {
721                         obj.creds = { 'version' : g.save_version, 'services' : {}, 'hosts' : {} };
722                         /*
723                                 {
724                                         'version' : xx,
725                                         'default_service' : xx,
726                                         'services' : {
727
728                                                 'xx' : {
729                                                         'username' : xx,
730                                                         'password' : xx,
731                                                         'default_attr' : xx,
732                                                 },
733
734                                                 'xx' : {
735                                                         'username' : xx,
736                                                         'password' : xx,
737                                                         'default_attr' : xx,
738                                                 },
739                                         },
740                     // new in version 2
741                     'hosts' : {
742                         'xxxx' : {
743                             'default_services' : [ xx, ... ],
744                             'services' : {
745
746                                 'xx' : {
747                                     'username' : xx,
748                                     'password' : xx,
749                                     'default_attr' : xx,
750                                 },
751
752                                 'xx' : {
753                                     'username' : xx,
754                                     'password' : xx,
755                                     'default_attr' : xx,
756                                 },
757                             },
758                         }
759                     }
760                                 }
761                         */
762                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
763                         JSAN.use('util.file'); var file = new util.file('z3950_store');
764                         if (file._file.exists()) {
765                                 var creds = file.get_object(); file.close();
766                                 if (typeof creds.version != 'undefined') {
767                                         if (creds.version >= obj.creds_version) {  /* so apparently, this guy is assuming that future versions will be backwards compatible */
768                         if (typeof creds.hosts == 'undefined') creds.hosts = {};
769                                                 obj.creds = creds;
770                                         }
771                                 }
772                         }
773                 } catch(E) {
774                         obj.error.standard_unexpected_error_alert('Error retrieving stored z39.50 credentials',E);
775                 }
776         },
777
778         'save_creds' : function () {
779                 try {
780                         var obj = this;
781             if (typeof obj.creds.hosts == 'undefined') obj.creds.hosts = {};
782             if (typeof obj.creds.hosts[ obj.data.server_unadorned ] == 'undefined') obj.creds.hosts[ obj.data.server_unadorned ] = { 'services' : {} };
783             obj.creds.hosts[ obj.data.server_unadorned ].default_services = obj.active_services;
784             for (var i = 0; i < obj.creds.hosts[ obj.data.server_unadorned ].default_services.length; i++) {
785                             var service = obj.creds.hosts[ obj.data.server_unadorned ].default_services[i];
786                         if (typeof obj.creds.hosts[ obj.data.server_unadorned ].services[ service ] == 'undefined') {
787                     obj.creds.hosts[ obj.data.server_unadorned ].services[ service ] = {}
788                         }
789                         obj.creds.hosts[ obj.data.server_unadorned ].services[service].username = document.getElementById(service + '_username').value;
790                         obj.creds.hosts[ obj.data.server_unadorned ].services[service].password = document.getElementById(service + '_password').value;
791                         if (obj.default_attr) {
792                                 obj.creds.hosts[ obj.data.server_unadorned ].services[service].default_attr = obj.default_attr;
793                         }
794             }
795                         obj.creds.version = obj.creds_version;
796                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
797                         JSAN.use('util.file'); var file = new util.file('z3950_store');
798                         file.set_object(obj.creds);
799                         file.close();
800                 } catch(E) {
801                         obj.error.standard_unexpected_error_alert('Problem storing z39.50 credentials.',E);
802                 }
803         },
804
805         'handle_enter' : function(ev) {
806                 var obj = this;
807                 if (ev.target.tagName != 'textbox') return;
808                 if (ev.keyCode == 13 /* enter */ || ev.keyCode == 77 /* enter on a mac */) setTimeout( function() { obj.initial_search(); }, 0);
809         },
810 }
811
812 dump('exiting cat.z3950.js\n');