]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/copy_buckets.js
some pauses
[Evergreen.git] / Open-ILS / xul / staff_client / server / cat / copy_buckets.js
1 dump('entering cat.copy_buckets.js\n');
2
3 if (typeof cat == 'undefined') cat = {};
4 cat.copy_buckets = function (params) {
5
6         JSAN.use('util.error'); this.error = new util.error();
7         JSAN.use('util.network'); this.network = new util.network();
8         JSAN.use('util.date');
9         JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.init({'via':'stash'});
10         this.first_pause = true;
11 }
12
13 cat.copy_buckets.prototype = {
14         'selection_list1' : [],
15         'selection_list2' : [],
16         'bucket_id_name_map' : {},
17
18         'render_pending_copies' : function() {
19                 if (this.first_pause) {
20                         this.first_pause = false;
21                 } else {
22                         alert("Pausing for replicated databases... press Enter or Spacebar when ready.");
23                 }
24                 var obj = this;
25                 obj.list1.clear();
26                 for (var i = 0; i < obj.copy_ids.length; i++) {
27                         var item = obj.flesh_item_for_list( obj.copy_ids[i] );
28                         if (item) obj.list1.append( item );
29                 }
30         },
31
32         'init' : function( params ) {
33
34                 var obj = this;
35
36                 obj.copy_ids = params['copy_ids'] || [];
37
38                 JSAN.use('circ.util');
39                 var columns = circ.util.columns( 
40                         { 
41                                 'barcode' : { 'hidden' : false },
42                                 'title' : { 'hidden' : false },
43                                 'location' : { 'hidden' : false },
44                                 'call_number' : { 'hidden' : false },
45                                 'status' : { 'hidden' : false },
46                                 'deleted' : { 'hidden' : false },
47                         } 
48                 );
49
50                 JSAN.use('util.list'); 
51
52                 obj.list1 = new util.list('pending_copies_list');
53                 obj.list1.init(
54                         {
55                                 'columns' : columns,
56                                 'map_row_to_columns' : circ.util.std_map_row_to_columns(),
57                                 'on_select' : function(ev) {
58                                         try {
59                                                 JSAN.use('util.functional');
60                                                 var sel = obj.list1.retrieve_selection();
61                                                 obj.selection_list1 = util.functional.map_list(
62                                                         sel,
63                                                         function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
64                                                 );
65                                                 obj.error.sdump('D_TRACE','circ/copy_buckets: selection list 1 = ' + js2JSON(obj.selection_list1) );
66                                                 if (obj.selection_list1.length == 0) {
67                                                         obj.controller.view.copy_buckets_sel_add.disabled = true;
68                                                 } else {
69                                                         obj.controller.view.copy_buckets_sel_add.disabled = false;
70                                                 }
71                                         } catch(E) {
72                                                 alert('FIXME: ' + E);
73                                         }
74                                 },
75
76                         }
77                 );
78
79                 obj.render_pending_copies();
80         
81                 obj.list2 = new util.list('copies_in_bucket_list');
82                 obj.list2.init(
83                         {
84                                 'columns' : columns,
85                                 'map_row_to_columns' : circ.util.std_map_row_to_columns(),
86                                 'on_select' : function(ev) {
87                                         try {
88                                                 JSAN.use('util.functional');
89                                                 var sel = obj.list2.retrieve_selection();
90                                                 document.getElementById('clip_button').disabled = sel.length < 1;
91                                                 obj.selection_list2 = util.functional.map_list(
92                                                         sel,
93                                                         function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
94                                                 );
95                                                 obj.error.sdump('D_TRACE','circ/copy_buckets: selection list 2 = ' + js2JSON(obj.selection_list2) );
96                                                 if (obj.selection_list2.length == 0) {
97                                                         obj.controller.view.copy_buckets_delete_item.disabled = true;
98                                                         obj.controller.view.copy_buckets_delete_item.setAttribute('disabled','true');
99                                                         obj.controller.view.copy_buckets_export.disabled = true;
100                                                         obj.controller.view.copy_buckets_export.setAttribute('disabled','true');
101                                                 } else {
102                                                         obj.controller.view.copy_buckets_delete_item.disabled = false;
103                                                         obj.controller.view.copy_buckets_delete_item.setAttribute('disabled','false');
104                                                         obj.controller.view.copy_buckets_export.disabled = false;
105                                                         obj.controller.view.copy_buckets_export.setAttribute('disabled','false');
106                                                 }
107                                         } catch(E) {
108                                                 alert('FIXME: ' + E);
109                                         }
110                                 },
111                         }
112                 );
113                 
114                 JSAN.use('util.controller'); obj.controller = new util.controller();
115                 obj.controller.init(
116                         {
117                                 'control_map' : {
118                                         'sel_clip' : [
119                                                 ['command'],
120                                                 function() { obj.list2.clipboard(); }
121                                         ],
122                                         'copy_buckets_menulist_placeholder' : [
123                                                 ['render'],
124                                                 function(e) {
125                                                         return function() {
126                                                                 JSAN.use('util.widgets'); JSAN.use('util.functional');
127                                                                 var items = [ ['Choose a bucket...',''] ].concat(
128                                                                         util.functional.map_list(
129                                                                                 obj.network.simple_request(
130                                                                                         'BUCKET_RETRIEVE_VIA_USER',
131                                                                                         [ ses(), obj.data.list.au[0].id() ]
132                                                                                 ).copy,
133                                                                                 function(o) {
134                                                                                         obj.bucket_id_name_map[ o.id() ] = o.name();
135                                                                                         return [ o.name(), o.id() ];
136                                                                                 }
137                                                                         )
138                                                                 );
139                                                                 g.error.sdump('D_TRACE','items = ' + js2JSON(items));
140                                                                 util.widgets.remove_children( e );
141                                                                 var ml = util.widgets.make_menulist(
142                                                                         items
143                                                                 );
144                                                                 e.appendChild( ml );
145                                                                 ml.setAttribute('id','bucket_menulist');
146                                                                 ml.setAttribute('accesskey','');
147
148                                                                 function change_bucket(ev) {
149                                                                         var bucket_id = ev.target.value;
150                                                                         if (!bucket_id) return;
151                                                                         var bucket = obj.network.simple_request(
152                                                                                 'BUCKET_FLESH',
153                                                                                 [ ses(), 'copy', bucket_id ]
154                                                                         );
155                                                                         var items = bucket.items() || [];
156                                                                         obj.list2.clear();
157                                                                         for (var i = 0; i < items.length; i++) {
158                                                                                 var item = obj.flesh_item_for_list( 
159                                                                                         items[i].target_copy(),
160                                                                                         items[i].id()
161                                                                                 );
162                                                                                 if (item) obj.list2.append( item );
163                                                                         }
164                                                                 }
165
166                                                                 ml.addEventListener( 'change_bucket', change_bucket , false);
167                                                                 ml.addEventListener( 'command', function() {
168                                                                         JSAN.use('util.widgets'); util.widgets.dispatch('change_bucket',ml);
169                                                                 }, false);
170                                                                 obj.controller.view.bucket_menulist = ml;
171                                                                 JSAN.use('util.widgets'); util.widgets.dispatch('change_bucket',ml);
172                                                         };
173                                                 },
174                                         ],
175
176                                         'copy_buckets_add' : [
177                                                 ['command'],
178                                                 function() {
179                                                         var bucket_id = obj.controller.view.bucket_menulist.value;
180                                                         if (!bucket_id) return;
181                                                         for (var i = 0; i < obj.copy_ids.length; i++) {
182                                                                 var bucket_item = new ccbi();
183                                                                 bucket_item.isnew('1');
184                                                                 bucket_item.bucket(bucket_id);
185                                                                 bucket_item.target_copy( obj.copy_ids[i] );
186                                                                 try {
187                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_CREATE',
188                                                                                 [ ses(), 'copy', bucket_item ]);
189
190                                                                         if (typeof robj == 'object') throw robj;
191
192                                                                         var item = obj.flesh_item_for_list( obj.copy_ids[i], robj );
193                                                                         if (!item) continue;
194
195                                                                         obj.list2.append( item );
196                                                                 } catch(E) {
197                                                                         obj.error.standard_unexpected_error_alert('Addition likely failed.',E);
198                                                                 }
199                                                         }
200                                                 }
201                                         ],
202                                         'copy_buckets_sel_add' : [
203                                                 ['command'],
204                                                 function() {                                                        
205                                                         var bucket_id = obj.controller.view.bucket_menulist.value;
206                                                         if (!bucket_id) return;
207                                                         for (var i = 0; i < obj.selection_list1.length; i++) {
208                                                                 var acp_id = obj.selection_list1[i][0];
209                                                                 //var barcode = obj.selection_list1[i][1];
210                                                                 var bucket_item = new ccbi();
211                                                                 bucket_item.isnew('1');
212                                                                 bucket_item.bucket(bucket_id);
213                                                                 bucket_item.target_copy( acp_id );
214                                                                 try {
215                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_CREATE',
216                                                                                 [ ses(), 'copy', bucket_item ]);
217
218                                                                         if (typeof robj == 'object') throw robj;
219
220                                                                         var item = obj.flesh_item_for_list( acp_id, robj );
221                                                                         if (!item) continue;
222
223                                                                         obj.list2.append( item );
224                                                                 } catch(E) {
225                                                                         obj.error.standard_unexpected_error_alert('Deletion likely failed.',E);
226                                                                 }
227                                                         }
228
229                                                 }
230                                         ],
231                                         'copy_buckets_export' : [
232                                                 ['command'],
233                                                 function() {                                                        
234                                                         for (var i = 0; i < obj.selection_list2.length; i++) {
235                                                                 var acp_id = obj.selection_list2[i][0];
236                                                                 //var barcode = obj.selection_list1[i][1];
237                                                                 //var bucket_item_id = obj.selection_list1[i][2];
238                                                                 var item = obj.flesh_item_for_list( acp_id );
239                                                                 if (item) {
240                                                                         obj.list1.append( item );
241                                                                         obj.copy_ids.push( acp_id );
242                                                                 }
243                                                         }
244                                                 }
245                                         ],
246
247                                         'copy_buckets_delete_item' : [
248                                                 ['command'],
249                                                 function() {
250                                                         for (var i = 0; i < obj.selection_list2.length; i++) {
251                                                                 try {
252                                                                         //var acp_id = obj.selection_list2[i][0];
253                                                                         //var barcode = obj.selection_list2[i][1];
254                                                                         var bucket_item_id = obj.selection_list2[i][2];
255                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_DELETE',
256                                                                                 [ ses(), 'copy', bucket_item_id ]);
257                                                                         if (typeof robj == 'object') throw robj;
258                                                                 } catch(E) {
259                                                                         obj.error.standard_unexpected_error_alert('Deletion likely failed.',E);
260                                                                 }
261                                                         }
262                                                         alert("Pausing for replicated databases... press Enter or Spacebar when ready.");
263                                                         setTimeout(
264                                                                 function() {
265                                                                         JSAN.use('util.widgets'); 
266                                                                         util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
267                                                                 }, 0
268                                                         );
269                                                 }
270                                         ],
271                                         'copy_buckets_delete_bucket' : [
272                                                 ['command'],
273                                                 function() {
274                                                         try {
275                                                                 var bucket = obj.controller.view.bucket_menulist.value;
276                                                                 var name = obj.bucket_id_name_map[ bucket ];
277                                                                 var conf = window.confirm('Delete the bucket named ' + name + '?');
278                                                                 if (!conf) return;
279                                                                 obj.list2.clear();
280                                                                 var robj = obj.network.simple_request('BUCKET_DELETE',[ses(),'copy',bucket]);
281                                                                 if (typeof robj == 'object') throw robj;
282                                                                 alert("Pausing for replicated databases... press Enter or Spacebar when ready.");
283                                                                 obj.controller.render('copy_buckets_menulist_placeholder');
284                                                         } catch(E) {
285                                                                 obj.error.standard_unexpected_error_alert('Bucket deletion likely failed.',E);
286                                                         }
287                                                 }
288                                         ],
289                                         'copy_buckets_new_bucket' : [
290                                                 ['command'],
291                                                 function() {
292                                                         try {
293                                                                 var name = prompt('What would you like to name the bucket?','','Bucket Creation');
294
295                                                                 if (name) {
296                                                                         var bucket = new ccb();
297                                                                         bucket.btype('staff_client');
298                                                                         bucket.owner( obj.data.list.au[0].id() );
299                                                                         bucket.name( name );
300
301                                                                         var robj = obj.network.simple_request('BUCKET_CREATE',[ses(),'copy',bucket]);
302
303                                                                         if (typeof robj == 'object') throw robj;
304
305                                                                         alert('Bucket "' + name + '" created.');
306
307                                                                         obj.controller.render('copy_buckets_menulist_placeholder');
308                                                                         obj.controller.view.bucket_menulist.value = robj;
309                                                                         setTimeout(
310                                                                                 function() {
311                                                                                         JSAN.use('util.widgets'); 
312                                                                                         util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
313                                                                                 }, 0
314                                                                         );
315                                                                 }
316                                                         } catch(E) {
317                                                                 obj.error.standard_unexpected_error_alert('Bucket creation failed.',E);
318                                                         }
319                                                 }
320                                         ],
321                                         'copy_buckets_batch_copy_edit' : [
322                                                 ['command'],
323                                                 function() {
324                                                         try {
325
326                                                                 obj.list2.select_all();
327                                                         
328                                                                 JSAN.use('util.widgets'); JSAN.use('util.functional');
329
330                                                                 var list = util.functional.map_list(
331                                                                         obj.list2.dump_retrieve_ids(),
332                                                                         function (o) {
333                                                                                 return JSON2js(o)[0]; // acp_id
334                                                                         }
335                                                                 );
336
337                                                                 var copies = util.functional.map_list(
338                                                                         list,
339                                                                         function (acp_id) {
340                                                                                 return obj.network.simple_request('FM_ACP_RETRIEVE',[acp_id]);
341                                                                         }
342                                                                 );
343
344                                                                 var edit = 0;
345                                                                 try {
346                                                                         edit = obj.network.request(
347                                                                                 api.PERM_MULTI_ORG_CHECK.app,
348                                                                                 api.PERM_MULTI_ORG_CHECK.method,
349                                                                                 [ 
350                                                                                         ses(), 
351                                                                                         obj.data.list.au[0].id(), 
352                                                                                         util.functional.map_list(
353                                                                                                 copies,
354                                                                                                 function (o) {
355                                                                                                         return o.call_number() == -1 ? o.circ_lib() : obj.network.simple_request('FM_ACN_RETRIEVE',[o.call_number()]).owning_lib();
356                                                                                                 }
357                                                                                         ),
358                                                                                         copies.length == 1 ? [ 'UPDATE_COPY' ] : [ 'UPDATE_COPY', 'UPDATE_BATCH_COPY' ]
359                                                                                 ]
360                                                                         ).length == 0 ? 1 : 0;
361                                                                 } catch(E) {
362                                                                         obj.error.sdump('D_ERROR','batch permission check: ' + E);
363                                                                 }
364
365                                                                 JSAN.use('cat.util'); cat.util.spawn_copy_editor(list,edit);
366
367                                                                 obj.render_pending_copies(); // FIXME -- need a generic refresh for lists
368                                                                 setTimeout(
369                                                                         function() {
370                                                                                 JSAN.use('util.widgets'); 
371                                                                                 util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
372                                                                         }, 0
373                                                                 );
374                                                         } catch(E) {
375                                                                 alert( js2JSON(E) );
376                                                         }
377                                                 }
378                                         ],
379                                         'copy_buckets_batch_copy_delete' : [
380                                                 ['command'],
381                                                 function() {
382                                                         try {
383                                                         
384                                                                 obj.list2.select_all();
385
386                                                                 JSAN.use('util.widgets'); JSAN.use('util.functional');
387
388                                                                 var list = util.functional.map_list(
389                                                                         obj.list2.dump_retrieve_ids(),
390                                                                         function (o) {
391                                                                                 return JSON2js(o)[0]; // acp_id
392                                                                         }
393                                                                 );
394
395                                                                 var copies = util.functional.map_list(
396                                                                         list,
397                                                                         function (acp_id) {
398                                                                                 return obj.network.simple_request('FM_ACP_RETRIEVE',[acp_id]);
399                                                                         }
400                                                                 );
401
402                                                                 for (var i = 0; i < copies.length; i++) {
403                                                                         copies[i].ischanged(1);
404                                                                         copies[i].isdeleted(1);
405                                                                 }
406
407                                                                 var robj = obj.network.simple_request('FM_ACP_FLESHED_BATCH_UPDATE',[ ses(), copies, true]);
408                                                                 if (typeof robj.ilsevent != 'undefined') obj.error.standard_unexpected_error_alert('Batch Item Deletion',robj);
409
410                                                                 obj.render_pending_copies(); // FIXME -- need a generic refresh for lists
411                                                                 setTimeout(
412                                                                         function() {
413                                                                                 JSAN.use('util.widgets'); 
414                                                                                 util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
415                                                                         }, 0
416                                                                 );
417                                                         } catch(E) {
418                                                                 alert( js2JSON(E) );
419                                                         }
420                                                 }
421                                         ],
422
423                                         'copy_buckets_transfer_to_volume' : [
424                                                 ['command'],
425                                                 function() {
426                                                         try {
427                                                                 obj.list2.select_all();
428
429                                                                 obj.data.stash_retrieve();
430                                                                 if (!obj.data.marked_volume) {
431                                                                         alert('Please mark a volume as the destination from within the copy browser and then try this again.');
432                                                                         return;
433                                                                 }
434
435                                                                 var copy_ids = util.functional.map_list(
436                                                                         obj.list2.dump_retrieve_ids(),
437                                                                         function (o) {
438                                                                                 return JSON2js(o)[0]; // acp_id
439                                                                         }
440                                                                 )
441
442                                                                 var volume = obj.network.simple_request('FM_ACN_RETRIEVE',[ obj.data.marked_volume ]);
443
444                                                                 var msg = 'Transfer the items in bucket "';
445                                                                 msg += obj.controller.view.bucket_menulist.getAttribute('label') + '" ';
446                                                                 msg += 'from their original volumes to ';
447                                                                 msg += obj.data.hash.aou[ volume.owning_lib() ].shortname() + "'s volume labelled ";
448                                                                 msg += '"' + volume.label() + '" on the following record?';
449
450                                                                 JSAN.use('cat.util'); cat.util.transfer_copies( { 
451                                                                         'copy_ids' : copy_ids, 
452                                                                         'message' : msg, 
453                                                                         'docid' : volume.record(),
454                                                                         'volume_label' : volume.label(),
455                                                                         'owning_lib' : volume.owning_lib(),
456                                                                 } );
457
458                                                                 obj.render_pending_copies(); // FIXME -- need a generic refresh for lists
459                                                                 setTimeout(
460                                                                         function() {
461                                                                                 JSAN.use('util.widgets'); 
462                                                                                 util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
463                                                                         }, 0
464                                                                 );
465                                                                 
466                                                         } catch(E) {
467                                                                 obj.error.standard_unexpected_error_alert('Items not likely transferred.',E);
468                                                         }
469                                                 }
470                                         ],
471                                         'cmd_broken' : [
472                                                 ['command'],
473                                                 function() { alert('Not Yet Implemented'); }
474                                         ],
475                                         'cmd_copy_buckets_print' : [
476                                                 ['command'],
477                                                 function() {
478                                                         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
479                                                         obj.list2.on_all_fleshed = function() {
480                                                                 try {
481                                                                         dump( js2JSON( obj.list2.dump_with_keys() ) + '\n' );
482                                                                         data.stash_retrieve();
483                                                                         var lib = data.hash.aou[ data.list.au[0].ws_ou() ];
484                                                                         lib.children(null);
485                                                                         var p = { 
486                                                                                 'lib' : lib,
487                                                                                 'staff' : data.list.au[0],
488                                                                                 'header' : data.print_list_templates.item_status.header,
489                                                                                 'line_item' : data.print_list_templates.item_status.line_item,
490                                                                                 'footer' : data.print_list_templates.item_status.footer,
491                                                                                 'type' : data.print_list_templates.item_status.type,
492                                                                                 'list' : obj.list2.dump_with_keys(),
493                                                                         };
494                                                                         JSAN.use('util.print'); var print = new util.print();
495                                                                         print.tree_list( p );
496                                                                         setTimeout(function(){obj.list2.on_all_fleshed = null;},0);
497                                                                 } catch(E) {
498                                                                         alert(E); 
499                                                                 }
500                                                         }
501                                                         obj.list2.full_retrieve();
502                                                 }
503                                         ],
504                                         'cmd_copy_buckets_reprint' : [
505                                                 ['command'],
506                                                 function() {
507                                                 }
508                                         ],
509                                         'cmd_copy_buckets_done' : [
510                                                 ['command'],
511                                                 function() {
512                                                         window.close();
513                                                 }
514                                         ],
515                                         'cmd_export_to_copy_status' : [
516                                                 ['command'],
517                                                 function() {
518                                                         try {
519                                                                 obj.list2.select_all();
520                                                                 JSAN.use('util.functional');
521                                                                 var barcodes = util.functional.map_list(
522                                                                         obj.list2.dump_retrieve_ids(),
523                                                                         function(o) { return JSON2js(o)[1]; }
524                                                                 );
525                                                                 var url = urls.XUL_COPY_STATUS + '?barcodes=' + window.escape( js2JSON(barcodes) );
526                                                                 xulG.new_tab( url, {}, {});
527                                                         } catch(E) {
528                                                                 obj.error.standard_unexpected_error_alert('Copy Status from Copy Buckets',E);
529                                                         }
530                                                 }
531                                         ],
532                                 }
533                         }
534                 );
535                 this.controller.render();
536
537                 if (typeof xulG == 'undefined') {
538                         obj.controller.view.cmd_export_to_copy_status.disabled = true;
539                         obj.controller.view.cmd_export_to_copy_status.setAttribute('disabled',true);
540                 } else {
541                         obj.controller.view.cmd_copy_buckets_done.disabled = true;
542                         obj.controller.view.cmd_copy_buckets_done.setAttribute('disabled',true);
543                 }
544         
545         },
546
547         'flesh_item_for_list' : function(acp_id,bucket_item_id) {
548                 var obj = this;
549                 try {
550                         var copy = obj.network.simple_request( 'FM_ACP_RETRIEVE', [ acp_id ]);
551                         if (copy == null || typeof copy.ilsevent != 'undefined') {
552                                 throw(copy);
553                         } else {
554                                 var item = {
555                                         'retrieve_id' : js2JSON( [ copy.id(), copy.barcode(), bucket_item_id ] ),
556                                         'row' : {
557                                                 'my' : {
558                                                         'mvr' : obj.network.simple_request('MODS_SLIM_RECORD_RETRIEVE_VIA_COPY', [ copy.id() ]),
559                                                         'acp' : copy,
560                                                 }
561                                         }
562                                 };
563                                 return item;
564                         }
565                 } catch(E) {
566                         obj.error.standard_unexpected_error_alert('List building failed.',E);
567                         return null;
568                 }
569
570         },
571         
572 }
573
574 dump('exiting cat.copy_buckets.js\n');