]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Cat.pm
Let the onslaught continue...
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Cat.pm
1 use strict; use warnings;
2 package OpenILS::Application::Cat;
3 use OpenILS::Application::AppUtils;
4 use OpenSRF::Application;
5 use OpenILS::Application::Cat::Utils;
6 use base qw/OpenSRF::Application/;
7 use Time::HiRes qw(time);
8 use OpenSRF::EX qw(:try);
9 use JSON;
10 use OpenILS::Utils::Fieldmapper;
11
12 my $apputils = "OpenILS::Application::AppUtils";
13
14 my $utils = "OpenILS::Application::Cat::Utils";
15
16
17
18
19 __PACKAGE__->register_method(
20         method  => "biblio_record_tree_retrieve",
21         api_name        => "open-ils.cat.biblio.record.tree.retrieve",
22         argc            => 1, 
23         note            => "Returns the tree associated with the nodeset of the given doc id"
24 );
25
26 sub biblio_record_tree_retrieve {
27
28         my( $self, $client, $recordid ) = @_;
29
30         my $name = "open-ils.storage.direct.biblio.record_entry.retrieve";
31         my $session = OpenSRF::AppSession->create( "open-ils.storage" );
32         my $request = $session->request( $name, $recordid );
33         my $marcxml = $request->gather(1);
34
35         if(!$marcxml) {
36                 throw OpenSRF::EX::ERROR 
37                         ("No record in database with id $recordid");
38         }
39
40         $session->disconnect();
41         $session->kill_me();
42
43         warn "turning into nodeset\n";
44         my $nodes = OpenILS::Utils::FlatXML->new()->xml_to_nodeset( $marcxml->marc ); 
45         warn "turning nodeset into tree\n";
46         my $tree = $utils->nodeset2tree( $nodes->nodeset );
47
48         $tree->owner_doc( $marcxml->id() );
49
50         warn "returning tree\n";
51
52         return $tree;
53 }
54
55 __PACKAGE__->register_method(
56         method  => "biblio_record_tree_commit",
57         api_name        => "open-ils.cat.biblio.record.tree.commit",
58         argc            => 3, #(session_id, biblio_tree ) 
59         note            => "Walks the tree and commits any changed nodes " .
60                                         "adds any new nodes, and deletes any deleted nodes",
61 );
62
63 sub biblio_record_tree_commit {
64
65         my( $self, $client, $user_session,  $tree ) = @_;
66         new Fieldmapper::biblio::record_node ($tree);
67
68         use Data::Dumper;
69
70         throw OpenSRF::EX::InvalidArg 
71                 ("Not enough args to to open-ils.cat.biblio.record.tree.commit")
72                 unless ( $user_session and $tree );
73
74         my $user_obj = 
75                 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
76
77         # capture the doc id
78         my $docid = $tree->owner_doc();
79         my $session = OpenILS::Application::AppUtils->start_db_session();
80
81         warn "Retrieving biblio record from storage for update\n";
82
83         my $req1 = $session->request(
84                         "open-ils.storage.direct.biblio.record_entry.batch.retrieve", 
85                         $docid );
86         my $biblio = $req1->gather(1);
87
88         warn "retrieved doc $docid\n";
89
90
91         # turn the tree into a nodeset
92         my $nodeset = $utils->tree2nodeset($tree);
93         $nodeset = $utils->clean_nodeset( $nodeset );
94
95         if(!defined($docid)) { # be sure
96                 for my $node (@$nodeset) {
97                         $docid = $node->owner_doc();
98                         last if defined($docid);
99                 }
100         }
101
102         # turn the nodeset into a doc
103         my $marcxml = OpenILS::Utils::FlatXML->new()->nodeset_to_xml( $nodeset );
104
105         $biblio->marc( $marcxml->toString() );
106
107         warn "Starting db session\n";
108
109         my $x = _update_record_metadata( $session, { user => $user_obj, docid => $docid } );
110         OpenILS::Application::AppUtils->rollback_db_session($session) unless $x;
111
112         warn "Sending updated doc $docid to db\n";
113         my $req = $session->request( "open-ils.storage.direct.biblio.record_entry.update", $biblio );
114
115         $req->wait_complete;
116         my $status = $req->recv();
117         if( !$status || $status->isa("Error") || ! $status->content) {
118                 OpenILS::Application::AppUtils->rollback_db_session($session);
119                 if($status->isa("Error")) { throw $status ($status); }
120                 throw OpenSRF::EX::ERROR ("Error updating biblio record");
121         }
122         $req->finish();
123
124         # Send the doc to the wormer for wormizing
125         warn "Starting worm session\n";
126
127         my $success = 0;
128         my $wresp;
129
130         my $wreq = $session->request( "open-ils.worm.wormize", $docid );
131
132         try {
133                 $wreq->gather(1);
134
135         } catch Error with {
136                 my $e = shift;
137                 warn "wormizing failed, rolling back\n";
138                 OpenILS::Application::AppUtils->rollback_db_session($session);
139
140                 if($e) { throw $e ($e); }
141                 throw OpenSRF::EX::ERROR ("Wormizing Failed for $docid" );
142         };
143
144         OpenILS::Application::AppUtils->commit_db_session( $session );
145
146         $nodeset = OpenILS::Utils::FlatXML->new()->xmldoc_to_nodeset($marcxml);
147         $tree = $utils->nodeset2tree($nodeset->nodeset);
148         $tree->owner_doc($docid);
149
150         $client->respond_complete($tree);
151
152         warn "Done wormizing\n";
153
154 }
155
156
157
158 __PACKAGE__->register_method(
159         method  => "biblio_record_record_metadata",
160         api_name        => "open-ils.cat.biblio.record.metadata.retrieve",
161         argc            => 1, #(session_id, biblio_tree ) 
162         note            => "Walks the tree and commits any changed nodes " .
163                                         "adds any new nodes, and deletes any deleted nodes",
164 );
165
166 sub biblio_record_record_metadata {
167         my( $self, $client, @ids ) = @_;
168
169         if(!@ids){return undef;}
170
171         my $session = OpenSRF::AppSession->create("open-ils.storage");
172         my $request = $session->request( 
173                         "open-ils.storage.direct.biblio.record_entry.batch.retrieve", @ids );
174
175         my $results = [];
176
177         while( my $response = $request->recv() ) {
178
179                 if(!$response) {
180                         throw OpenSRF::EX::ERROR ("No Response from Storage");
181                 }
182                 if($response->isa("Error")) {
183                         throw $response ($response->stringify);
184                 }
185
186                 my $record_entry = $response->content;
187
188                 my $creator = $record_entry->creator;
189                 my $editor      = $record_entry->editor;
190
191                 ($creator, $editor) = _get_userid_by_id($creator, $editor);
192
193                 $record_entry->creator( $creator );
194                 $record_entry->editor( $editor );
195
196                 push @$results, $record_entry;
197
198         }
199
200         $request->finish;
201         $session->disconnect();
202         $session->finish();
203
204         return $results;
205
206 }
207
208 # gets the username
209 sub _get_userid_by_id {
210
211         my @ids = @_;
212         my @users;
213
214         my $session = OpenSRF::AppSession->create( "open-ils.storage" );
215         my $request = $session->request( 
216                 "open-ils.storage.direct.actor.user.batch.retrieve.atomic", @ids );
217
218         $request->wait_complete;
219         my $response = $request->recv();
220         if(!$request->complete) { return undef; }
221
222         if($response->isa("Error")){
223                 throw $response ($response);
224         }
225
226         for my $u (@{$response->content}) {
227                 next unless ref($u);
228                 push @users, $u->usrname;
229         }
230
231         $request->finish;
232         $session->disconnect;
233         $session->kill_me();
234
235         return @users;
236 }
237
238 sub _get_id_by_userid {
239
240         my @users = @_;
241         my @ids;
242
243         my $session = OpenSRF::AppSession->create( "open-ils.storage" );
244         my $request = $session->request( 
245                 "open-ils.storage.direct.actor.user.search.usrname", @users );
246
247         $request->wait_complete;
248         my $response = $request->recv();
249         if(!$request->complete) { 
250                 throw OpenSRF::EX::ERROR ("no response from storage on user retrieve");
251         }
252
253         if(UNIVERSAL::isa( $response, "Error")){
254                 throw $response ($response);
255         }
256
257         for my $u (@{$response->content}) {
258                 next unless ref($u);
259                 push @ids, $u->id();
260         }
261
262         $request->finish;
263         $session->disconnect;
264         $session->kill_me();
265
266         return @ids;
267 }
268
269
270 # commits metadata objects to the db
271 sub _update_record_metadata {
272
273         my ($session, @docs ) = @_;
274
275         for my $doc (@docs) {
276
277                 my $user_obj = $doc->{user};
278                 my $docid = $doc->{docid};
279
280                 warn "Updating metata for doc $docid\n";
281
282                 my $request = $session->request( 
283                         "open-ils.storage.direct.biblio.record_entry.retrieve", $docid );
284                 my $record = $request->gather(1);
285
286                 warn "retrieved record\n";
287                 my ($id) = _get_id_by_userid($user_obj->usrname);
288
289                 warn "got $id from _get_id_by_userid\n";
290                 $record->editor($id);
291                 
292                 warn "Grabbed the record, updating and moving on\n";
293
294                 $request = $session->request( 
295                         "open-ils.storage.direct.biblio.record_entry.update", $record );
296                 $request->gather(1);
297         }
298
299         warn "committing metarecord update\n";
300
301         return 1;
302 }
303
304
305
306 __PACKAGE__->register_method(
307         method  => "orgs_for_title",
308         api_name        => "open-ils.cat.actor.org_unit.retrieve_by_title"
309 );
310
311 sub orgs_for_title {
312         my( $self, $client, $record_id ) = @_;
313
314         my $vols = $apputils->simple_scalar_request(
315                 "open-ils.storage",
316                 "open-ils.storage.direct.asset.call_number.search.record",
317                 $record_id );
318
319         my $orgs = { map {$_->owning_lib => 1 } @$vols };
320         return [ keys %$orgs ];
321 }
322
323
324
325 __PACKAGE__->register_method(
326         method  => "retrieve_copies",
327         api_name        => "open-ils.cat.asset.copy_tree.retrieve",
328 );
329
330 __PACKAGE__->register_method(
331         method  => "retrieve_copies",
332         api_name        => "open-ils.cat.asset.copy_tree.global.retrieve",
333 );
334
335 sub retrieve_copies {
336
337         my( $self, $client, $user_session, $docid, @org_ids ) = @_;
338
339         if(ref($org_ids[0])) { @org_ids = @{$org_ids[0]}; }
340
341         $docid = "$docid";
342
343         warn " $$ retrieving copy tree for doc $docid at " . time() . "\n";
344
345         if(!@org_ids) {
346                 my $user_obj = 
347                         OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
348                         @org_ids = ($user_obj->home_ou);
349         }
350
351         if( $self->api_name =~ /global/ ) {
352                 warn "performing global copy_tree search for $docid\n";
353                 return _build_volume_list( { record => $docid } );
354
355         } else {
356
357                 my @all_vols;
358                 for my $orgid (@org_ids) {
359                         my $vols = _build_volume_list( 
360                                         { record => $docid, owning_lib => $orgid } );
361                         warn "Volumes built for org $orgid\n";
362                         push( @all_vols, @$vols );
363                 }
364                 
365                 warn " $$ Finished copy_tree at " . time() . "\n";
366                 return \@all_vols;
367         }
368
369         return undef;
370 }
371
372
373 sub _build_volume_list {
374         my $search_hash = shift;
375
376         my      $session = OpenSRF::AppSession->create( "open-ils.storage" );
377         
378
379         my $request = $session->request( 
380                         "open-ils.storage.direct.asset.call_number.search.atomic", $search_hash );
381
382         my $vols = $request->gather(1);
383         my @volumes;
384
385         for my $volume (@$vols) {
386
387                 warn "Grabbing copies for volume: " . $volume->id . "\n";
388                 my $creq = $session->request(
389                         "open-ils.storage.direct.asset.copy.search.call_number", 
390                         $volume->id );
391                 my $copies = $creq->gather(1);
392
393                 $volume->copies($copies);
394
395                 push( @volumes, $volume );
396         }
397
398
399         $session->disconnect();
400         return \@volumes;
401
402 }
403
404
405
406
407 =head
408 __PACKAGE__->register_method(
409         method  => "generic_edit_copies_volumes",
410         api_name        => "open-ils.cat.asset.volume.batch.update",
411 );
412
413 __PACKAGE__->register_method(
414         method  => "generic_edit_copies_volumes",
415         api_name        => "open-ils.cat.asset.volume.batch.delete",
416 );
417
418 __PACKAGE__->register_method(
419         method  => "generic_edit_copies_volumes",
420         api_name        => "open-ils.cat.asset.copy.batch.update",
421 );
422
423 __PACKAGE__->register_method(
424         method  => "generic_edit_copies_volumes",
425         api_name        => "open-ils.cat.asset.copy.batch.delete",
426 );
427
428
429 sub generic_edit_copies_volumes {
430
431         my( $self, $client, $user_session, $items ) = @_;
432
433         my $method = $self->api_name;
434         $method =~ s/open-ils\.cat/open-ils\.storage\.direct/og;
435         warn "our method is $method\n";
436
437         my $user_obj = 
438                 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
439         
440         warn "updating editor info\n";
441
442         for my $item (@$items) {
443                 $item->editor( $user_obj->id );
444         }
445
446         my $session = OpenILS::Application::AppUtils->start_db_session;
447         my $request = $session->request( $method, @$items );
448         my $result = $request->gather(1);
449
450         OpenILS::Application::AppUtils->commit_db_session($session);
451
452         warn "looks like we succeeded\n";
453         return $result;
454 }
455
456 =cut
457
458
459
460 # -----------------------------------------------------------------
461 # Fleshed volume tree batch add/update.  This does everything a 
462 # volume tree could want, add, update, delete
463 # -----------------------------------------------------------------
464 __PACKAGE__->register_method(
465         method  => "volume_tree_fleshed_update",
466         api_name        => "open-ils.cat.asset.volume_tree.fleshed.batch.update",
467 );
468 sub volume_tree_fleshed_update {
469
470         my( $self, $client, $user_session, $volumes ) = @_;
471         return undef unless $volumes;
472         my $user_obj = $apputils->check_user_session($user_session);
473
474         my $session = $apputils->start_db_session();
475         warn "Looping on volumes in fleshed volume tree update\n";
476
477         # cycle through the volumes provided and update/create/delete where necessary
478         for my $volume (@$volumes) {
479
480                 warn "updating volume " . $volume->id . "\n";
481
482                 my $update_copy_list = $volume->copies;
483
484                 if( $volume->isnew ) {
485
486                         $volume->clear_id;
487                         $volume->editor($user_obj->id);
488                         $volume->creator($user_obj->id);
489                         $volume = _add_volume($session, $volume);
490
491                 } elsif( $volume->ischanged ) {
492                         $volume->editor($user_obj->id);
493                         _update_volume($session, $volume);
494
495                 } elsif( $volume->isdeleted) {
496                         return _delete_volume($session, $volume);
497                 }
498
499                 for my $copy (@{$update_copy_list}) {
500
501                         $copy->editor($user_obj->id);
502                         warn "updating copy for volume " . $volume->id . "\n";
503                         if( $copy->isnew ) {
504
505                                 $copy->clear_id;
506                                 $copy->call_number($volume->id);
507                                 $copy->creator($user_obj->id);
508                                 $copy = _fleshed_copy_update($session,$copy,$user_obj->id);
509
510                         } elsif( $copy->ischanged ) {
511                                 $copy->call_number($volume->id);
512                                 $copy = _fleshed_copy_update($session, $copy, $user_obj->id);
513
514                         } elsif( $copy->isdeleted ) {
515                                 _fleshed_copy_update($session, $copy, $user_obj->id);
516                         }
517                 }
518         }
519         $apputils->commit_db_session($session);
520         return scalar(@$volumes);
521 }
522
523
524 #XXX make me
525 sub _delete_volume {
526         my( $session, $volume ) = @_;
527
528         $volume = _find_volume($session, $volume);
529
530 #       my $copies = $session->request(
531 #               "open-ils.storage.direct.asset.copy.search.call_number",
532 #               $volume-id )->gather(1);
533 #       if(@$copies) {
534 #               throw OpenSRF::EX::ERROR 
535 #                       ("Cannot remove volume with copies attached");
536 #       }
537
538 }
539
540 =head
541 sub _find_volume {
542         my( $session, $volume ) = @_;
543         my $cn_req = $session->request( 
544                 'open-ils.storage.direct.asset.call_number.search' =>
545              {       owning_lib      => $volume->owning_lib,
546                      label           => $volume->label,
547                      record          => $volume->record,
548                 }); 
549
550         return $cn_req->gather(1);
551 }
552 =cut
553
554 sub _update_volume {
555         my($session, $volume) = @_;
556         my $req = $session->request(
557                 "open-ils.storage.direct.asset.call_number.update",
558                 $volume );
559         my $status = $req->gather(1);
560 }
561
562 sub _add_volume {
563
564         my($session, $volume) = @_;
565
566         my $request = $session->request( 
567                 "open-ils.storage.direct.asset.call_number.create", $volume );
568
569         my $id = $request->gather(1);
570
571         if( $id == 0 ) {
572                 OpenILS::Application::AppUtils->rollback_db_session($session);
573                 throw OpenSRF::EX::ERROR (" * -> Error creating new volume");
574         }
575
576         $volume->id($id);
577         warn "received new volume id: $id\n";
578         return $volume;
579
580 }
581
582
583
584
585 =head
586 __PACKAGE__->register_method(
587         method  => "volume_tree_add",
588         api_name        => "open-ils.cat.asset.volume.tree.batch.add",
589 );
590
591 sub volume_tree_add {
592
593         my( $self, $client, $user_session, $volumes ) = @_;
594         return undef unless $volumes;
595
596         use Data::Dumper;
597         warn "Volumes:\n";
598         warn Dumper $volumes;
599
600         my $user_obj = 
601                 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
602
603         warn "volume_tree_add creating new db session\n";
604
605         my $session = OpenILS::Application::AppUtils->start_db_session;
606
607         for my $volume (@$volumes) {
608
609                 warn "Looping on volumes\n";
610
611                 my $new_copy_list = $volume->copies;
612
613                 warn "Searching for volume with ".$volume->owning_lib . " " .
614                         $volume->label . " " . $volume->record . "\n";
615
616                 my $cn = _find_volume($session, $volume);
617
618                 
619                 if($cn) {
620
621                         $volume = $cn;
622                         $volume->editor( $user_obj->id );
623
624                 } else {
625
626                         $volume->creator( $user_obj->id );
627                         $volume->editor( $user_obj->id );
628                         $volume = _add_volume($volume);
629
630                 }
631
632                 for my $copy (@{$new_copy_list}) {
633
634                         warn "adding a copy for volume $id\n";
635
636                         $copy->call_number($id);
637                         $copy->creator($user_obj->id);
638                         $copy->editor($user_obj->id);
639
640                         warn Dumper $copy;
641
642                         if( $copy->isnew() ) {
643
644                                 my $req = $session->request(
645                                                 "open-ils.storage.direct.asset.copy.create", $copy );
646                                 my $cid = $req->gather(1);
647
648                                 if(!$cid) {
649                                         OpenILS::Application::AppUtils->rollback_db_session($session);
650                                         throw OpenSRF::EX::ERROR ("Error adding copy to volume $id" );
651                                 }
652         
653                                 warn "got new copy id $cid\n";
654
655                         } elsif( $copy->ischanged() ) {
656
657                         }
658
659                 }
660
661                 warn "completed adding copies for $id\n";
662
663
664         }
665
666         warn "committing volume tree add db session\n";
667         OpenILS::Application::AppUtils->commit_db_session($session);
668
669         return scalar(@$volumes);
670
671 }
672
673 __PACKAGE__->register_method(
674         method  => "copy_update",
675         api_name        => "open-ils.cat.asset.copy.batch.update",
676 );
677
678 sub copy_update {
679         my($self, $client, $user_session, $copies) = @_;
680
681         my $user_obj = $apputils->check_user_session( $user_session ); #throws EX on error
682
683         my $session = $apputils->start_db_session();
684         for my $copy (@$copies) {
685                 $copy->editor($user_obj->id);
686                 my $req = $session->request(
687                         "open-ils.storage.direct.asset.copy.update",
688                         $copy );
689                 my $status = $req->gather(1);
690         }
691
692         $apputils->commit_db_session($session);
693         return 1;
694 }
695
696 =cut
697
698
699 __PACKAGE__->register_method(
700         method  => "fleshed_copy_update",
701         api_name        => "open-ils.cat.asset.copy.fleshed.batch.update",
702 );
703
704 sub fleshed_copy_update {
705         my($self, $client, $user_session, $copies) = @_;
706
707         my $user_obj = $apputils->check_user_session($user_session); 
708         my $session = $apputils->start_db_session();
709
710         for my $copy (@$copies) {
711                 _fleshed_copy_update($session, $copies, $user_obj->id);
712         }
713
714         $apputils->commit_db_session($session);
715         return 1;
716 }
717
718
719 sub _delete_copy {
720         my($session, $copy) = @_;
721         warn "Deleting copy " . $copy->id . "\n";
722         my $request = $session->request(
723                 "open-ils.storage.direct.asset.copy.delete",
724                 $copy );
725         return $request->gather(1);
726 }
727
728 sub _create_copy {
729         my($session, $copy) = @_;
730
731         my $request = $session->request(
732                 "open-ils.storage.direct.asset.copy.create",
733                 $copy );
734         my $id = $request->gather(1);
735
736         if($id < 1) {
737                 throw OpenSRF::EX::ERROR
738                         ("Unable to create new copy " . Dumper($copy));
739         }
740         $copy->id($id);
741         warn "Created copy " . $copy->id . "\n";
742
743         return $copy;
744
745 }
746
747 sub _update_copy {
748         my($session, $copy) = @_;
749         my $request = $session->request(
750                 "open-ils.storage.asset.copy.update",
751                 $copy );
752         warn "Updated copy " . $copy->id . "\n";
753         return $request->gather(1);
754 }
755
756
757 # -----------------------------------------------------------------
758 # Creates/Updates/Deletes a fleshed asset.copy.  
759 # adds/deletes copy stat_cat maps where necessary
760 # -----------------------------------------------------------------
761 sub _fleshed_copy_update {
762         my($session, $copy, $editor) = @_;
763
764         my $stat_cat_entries = $copy->stat_cat_entries;
765         $copy->editor($editor);
766         
767         # in case we're fleshed
768         if(ref($copy->status))          {$copy->status( $copy->status->id ); }
769         if(ref($copy->location))        {$copy->location( $copy->location->id ); }
770         if(ref($copy->circ_lib))        {$copy->circ_lib( $copy->circ_lib->id ); }
771
772
773         if( $copy->isdeleted ) { 
774                 return _delete_copy($session, $copy);
775         } elsif( $copy->isnew ) {
776                 $copy = _create_copy($session, $copy);
777         } elsif( $copy->ischanged ) {
778                 _update_copy($session, $copy);
779         }
780
781         warn "Updating copy " . Dumper($copy) . "\n";
782         
783         if(!@$stat_cat_entries) { return 1; }
784
785         my $stat_maps = $session->request(
786                 "open-ils.storage.direct.asset.stat_cat_entry_copy_map.search.owning_copy",
787                 $copy->id )->gather(1);
788
789         if( ! $copy->isnew ) { _delete_stale_maps($session, $copy); }
790         
791         # go through the stat cat update/create process
792         for my $stat_entry (@{$stat_cat_entries}){ 
793                 _copy_update_stat_cats( $session, $copy, $stat_maps, $stat_entry );
794         }
795         
796         return 1;
797 }
798
799
800 # -----------------------------------------------------------------
801 # Deletes stat maps attached to this copy in the database that
802 # are no longer attached to the current copy
803 # -----------------------------------------------------------------
804 sub _delete_stale_maps {
805         my( $session, $stat_maps, $copy) = @_;
806
807         warn "Deleting stale stat maps for copy " . $copy->id . "\n";
808         for my $map (@$stat_maps) {
809         # if there is no stat cat entry on the copy who's id matches the
810         # current map's id, remove the map from the database
811         if(! grep { $_->id == $map->stat_cat_entry } @{$copy->stat_cat_entries} ) {
812                 my $req = $session->request(
813                         "open-ils.storage.direct.asset.stat_cat_entry_copy_map.delete", $map );
814                 $req->gather(1);
815                 }
816         }
817
818         return $stat_maps;
819 }
820
821
822 # -----------------------------------------------------------------
823 # Searches the stat maps to see if '$entry' already exists on
824 # the given copy.  If it does not, a new stat map is created
825 # for the given entry and copy
826 # -----------------------------------------------------------------
827 sub _copy_update_stat_cats {
828         my ( $session, $copy, $stat_maps, $entry ) = @_;
829
830         warn "Updating stat maps for copy " . $copy->id . "\n";
831
832         # see if this map already exists
833         for my $map (@$stat_maps) {
834                 if( $map->stat_cat_entry == $entry->id ) {return;}
835         }
836
837         warn "Creating new stat map for stat  " . 
838                 $entry->stat_cat . " and copy " . $copy->id . "\n";
839
840         # if not, create it
841         my $new_map = Fieldmapper::asset::stat_cat_entry_copy_map->new();
842
843         $new_map->stat_cat( $entry->stat_cat );
844         $new_map->stat_cat_entry( $entry->id );
845         $new_map->owning_copy( $copy->id );
846
847         warn "New map is " . Dumper($new_map) . "\n";
848
849         my $request = $session->request(
850                 "open-ils.storage.direct.asset.stat_cat_entry_copy_map.create",
851                 $new_map );
852         my $status = $request->gather(1);
853         warn "created new map with id $status\n";
854
855 }
856
857
858
859 =head
860 __PACKAGE__->register_method(
861         method  => "volume_tree_delete",
862         api_name        => "open-ils.cat.asset.volume.tree.batch.delete",
863 );
864
865 sub volume_tree_delete {
866
867         my( $self, $client, $user_session, $volumes ) = @_;
868         return undef unless $volumes;
869
870         my $user_obj = 
871                 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
872
873         my $session = OpenILS::Application::AppUtils->start_db_session;
874
875         for my $volume (@$volumes) {
876
877                 $volume->editor($user_obj->id);
878
879                 new Fieldmapper::asset::call_number($volume);
880
881                 for my $copy (@{$volume->copies}) {
882
883                         new Fieldmapper::asset::copy($copy);
884
885                         $copy->editor( $user_obj->id );
886
887                         warn "Deleting copy " . $copy->id . " from db\n";
888
889                         my $req = $session->request(
890                                         "open-ils.storage.direct.asset.copy.delete", $copy );
891
892                         my $resp = $req->recv();
893
894                         if( !$req->complete ) {
895                                 OpenILS::Application::AppUtils->rollback_db_session($session);
896                                 throw OpenSRF::EX::ERROR (
897                                                 "No response from storage on copy delete");
898                         }
899         
900                         if(UNIVERSAL::isa($resp, "Error")) {
901                                 OpenILS::Application::AppUtils->rollback_db_session($session);
902                                 throw $resp ($resp->stringify);
903                         }
904                         
905                         $req->finish();
906                 }
907
908                 warn "Deleting volume " . $volume->id . " from database\n";
909
910                 my $vol_req = $session->request(
911                                 "open-ils.storage.direct.asset.call_number.delete", $volume );
912                 my $vol_resp = $vol_req;
913
914                 if(!$vol_req->complete) {
915                         OpenILS::Application::AppUtils->rollback_db_session($session);
916                                 throw OpenSRF::EX::ERROR 
917                                         ("No response from storage on volume delete");
918                 }
919
920                 if( $vol_resp and UNIVERSAL::isa($vol_resp, "Error")) {
921                         OpenILS::Application::AppUtils->rollback_db_session($session);
922                         throw $vol_resp ($vol_resp->stringify);
923                 }
924
925                 $vol_req->finish();
926
927         }
928
929         warn "committing delete volume tree add db session\n";
930
931         OpenILS::Application::AppUtils->commit_db_session($session);
932
933         return scalar(@$volumes);
934
935 }
936
937
938 =cut
939
940
941
942 1;