]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Actor.pm
moving, faster than the speed of light
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Actor.pm
1 package OpenILS::Application::Actor;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
4 use Data::Dumper;
5
6 use OpenSRF::EX qw(:try);
7 use OpenILS::EX;
8
9 use OpenILS::Application::AppUtils;
10 use OpenILS::Utils::Fieldmapper;
11 use OpenILS::Application::Search::Actor;
12
13 my $apputils = "OpenILS::Application::AppUtils";
14 sub _d { warn "Patron:\n" . Dumper(shift()); }
15 my $cache_client = OpenSRF::Utils::Cache->new("global", 0);
16
17
18 __PACKAGE__->register_method(
19         method  => "update_patron",
20         api_name        => "open-ils.actor.patron.update",
21 );
22
23
24 sub update_patron {
25         my( $self, $client, $user_session, $patron ) = @_;
26
27         my $session = $apputils->start_db_session();
28         my $err = undef;
29
30         warn $user_session . " " . $patron . "\n";
31         _d($patron);
32
33         my $user_obj = 
34                 OpenILS::Application::AppUtils->check_user_session( 
35                                 $user_session ); #throws EX on error
36
37         # XXX does this user have permission to add/create users.  Granularity?
38
39         # $new_patron is the patron in progress.  $patron is the original patron
40         # passed in with the method.  new_patron will change as the components
41         # of patron are added/updated.
42
43         my $new_patron;
44
45         #try {
46                 # create/update the patron first so we can use his id
47                 if($patron->isnew()) {
48                         $new_patron = _add_patron(
49                                         $session, _clone_patron($patron));
50                         if(UNIVERSAL::isa($new_patron, "OpenILS::EX")) {
51                                 $client->respond_complete($new_patron->ex);
52                                 return undef;
53                         }
54
55                 } else { 
56                         $new_patron = $patron; 
57                 }
58
59                 $new_patron = _add_update_addresses($session, $patron, $new_patron);
60                 $new_patron = _add_update_cards($session, $patron, $new_patron);
61
62                 if(UNIVERSAL::isa($new_patron,"OpenILS::EX")) {
63                         $client->respond_complete($new_patron->ex);
64                         return undef;
65                 }
66
67                 $new_patron = _add_survey_responses($session, $patron, $new_patron);
68                 $new_patron     = _create_stat_maps($session, $user_session, $patron, $new_patron);
69
70                 # re-update the patron if anything has happened to him during this process
71                 if($new_patron->ischanged()) {
72                         $new_patron = _update_patron($session, $new_patron);
73                 }
74                 $apputils->commit_db_session($session);
75
76 =head
77         } catch Error with { 
78                 my $e = shift;
79                 $err =  "-*- Failure adding user: $e";
80                 $apputils->rollback_db_session($session);
81                 warn $err;
82         };
83
84         if($err) { throw OpenSRF::EX::ERROR ($err); }
85 =cut
86
87         warn "Patron Update/Create complete\n";
88         return flesh_user($new_patron->id());
89 }
90
91
92 __PACKAGE__->register_method(
93         method  => "user_retrieve_fleshed_by_id",
94         api_name        => "open-ils.actor.user.fleshed.retrieve",);
95
96 sub user_retrieve_fleshed_by_id {
97         my( $self, $client, $user_session, $user_id ) = @_;
98         my $user_obj = $apputils->check_user_session( $user_session ); 
99         return flesh_user($user_id);
100 }
101
102
103 sub flesh_user {
104         my $id = shift;
105         my $session = shift;
106
107         my $kill = 0;
108
109         if(!$session) {
110                 $session = OpenSRF::AppSession->create("open-ils.storage");
111                 $kill = 1;
112         }
113
114         # grab the user with the given card
115         my $ureq = $session->request(
116                         "open-ils.storage.direct.actor.user.retrieve",
117                         $id);
118         my $user = $ureq->gather(1);
119
120         # grab the cards
121         my $cards_req = $session->request(
122                         "open-ils.storage.direct.actor.card.search.usr",
123                         $user->id() );
124         $user->cards( $cards_req->gather(1) );
125
126         my $add_req = $session->request(
127                         "open-ils.storage.direct.actor.user_address.search.usr",
128                         $user->id() );
129         $user->addresses( $add_req->gather(1) );
130
131         my $stat_req = $session->request(
132                 "open-ils.storage.direct.actor.stat_cat_entry_user_map.search.target_usr",
133                 $user->id() );
134         $user->stat_cat_entries($stat_req->gather(1));
135
136         if($kill) { $session->disconnect(); }
137         $user->clear_passwd();
138         warn Dumper $user;
139
140         return $user;
141
142 }
143
144
145 # clone and clear stuff that would break the database
146 sub _clone_patron {
147         my $patron = shift;
148
149         my $new_patron = Fieldmapper::actor::user->new();
150
151         my $fmap = $Fieldmapper::fieldmap;
152         no strict; # shallow clone, may be useful in the fieldmapper
153         for my $field 
154                 (keys %{$fmap->{"Fieldmapper::actor::user"}->{'fields'}}) {
155                         $new_patron->$field( $patron->$field() );
156         }
157         use strict;
158
159         # clear these
160         $new_patron->clear_billing_address();
161         $new_patron->clear_mailing_address();
162         $new_patron->clear_addresses();
163         $new_patron->clear_card();
164         $new_patron->clear_cards();
165         $new_patron->clear_id();
166         $new_patron->clear_isnew();
167         $new_patron->clear_changed();
168         $new_patron->clear_deleted();
169         $new_patron->clear_stat_cat_entries();
170
171         return $new_patron;
172 }
173
174
175 sub _add_patron {
176         my $session             = shift;
177         my $patron              = shift;
178
179         warn "Creating new patron\n";
180         _d($patron);
181
182         my $req = $session->request(
183                 "open-ils.storage.direct.actor.user.create",$patron);
184         my $id = $req->gather(1);
185         if(!$id) { 
186                 return OpenILS::EX->new("DUPLICATE_USER_USERNAME");
187         }
188
189         # retrieve the patron from the db to collect defaults
190         my $ureq = $session->request(
191                         "open-ils.storage.direct.actor.user.retrieve",
192                         $id);
193
194         warn "Created new patron with id $id\n";
195
196         return $ureq->gather(1);
197 }
198
199
200 sub _update_patron {
201         my( $session, $patron) = @_;
202
203         warn "updating patron " . Dumper($patron) . "\n";
204
205         my $req = $session->request(
206                 "open-ils.storage.direct.actor.user.update",$patron );
207         my $status = $req->gather(1);
208         if(!defined($status)) { 
209                 throw OpenSRF::EX::ERROR 
210                         ("Unknown error updating patron"); 
211         }
212         return $patron;
213 }
214
215
216 sub _add_update_addresses {
217         my $session = shift;
218         my $patron = shift;
219         my $new_patron = shift;
220
221         my $current_id; # id of the address before creation
222
223         for my $address (@{$patron->addresses()}) {
224
225                 $address->usr($new_patron->id());
226
227                 if(ref($address) and $address->isnew()) {
228                         warn "Adding new address at street " . $address->street1() . "\n";
229
230                         $current_id = $address->id();
231                         $address = _add_address($session,$address);
232
233                         if( $patron->billing_address() and 
234                                         $patron->billing_address() == $current_id ) {
235                                 $new_patron->billing_address($address->id());
236                                 $new_patron->ischanged(1);
237                         }
238
239                         if( $patron->mailing_address() and
240                                         $patron->mailing_address() == $current_id ) {
241                                 $new_patron->mailing_address($address->id());
242                                 $new_patron->ischanged(1);
243                         }
244
245                 } elsif( ref($address) and $address->ischanged() ) {
246                         warn "Updating address at street " . $address->street1();
247                         $address->usr($new_patron->id());
248                         _update_address($session,$address);
249
250                 } elsif( ref($address) and $address->isdeleted() ) {
251                         warn "Deleting address at street " . $address->street1();
252
253                         if( $address->id() == $new_patron->mailing_address() ) {
254                                 $new_patron->clear_mailing_address();
255                                 _update_patron($session, $new_patron);
256                         }
257
258                         if( $address->id() == $new_patron->billing_address() ) {
259                                 $new_patron->clear_billing_address();
260                                 _update_patron($session, $new_patron);
261                         }
262
263                         _delete_address($session,$address);
264                 }
265         }
266
267         return $new_patron;
268 }
269
270
271 # adds an address to the db and returns the address with new id
272 sub _add_address {
273         my($session, $address) = @_;
274         $address->clear_id();
275
276         # put the address into the database
277         my $req = $session->request(
278                 "open-ils.storage.direct.actor.user_address.create",
279                 $address );
280
281         #update the id
282         my $id = $req->gather(1);
283         if(!$id) { 
284                 throw OpenSRF::EX::ERROR 
285                         ("Unable to create new user address"); 
286         }
287
288         warn "Created address with id $id\n";
289
290         # update all the necessary id's
291         $address->id( $id );
292         return $address;
293 }
294
295
296 sub _update_address {
297         my( $session, $address ) = @_;
298         my $req = $session->request(
299                 "open-ils.storage.direct.actor.user_address.update",
300                 $address );
301         my $status = $req->gather(1);
302         if(!defined($status)) { 
303                 throw OpenSRF::EX::ERROR 
304                         ("Unknown error updating address"); 
305         }
306         return $address;
307 }
308
309
310
311 sub _add_update_cards {
312
313         my $session = shift;
314         my $patron = shift;
315         my $new_patron = shift;
316
317         my $virtual_id; #id of the card before creation
318         for my $card (@{$patron->cards()}) {
319
320                 $card->usr($new_patron->id());
321
322                 if(ref($card) and $card->isnew()) {
323
324                         $virtual_id = $card->id();
325                         $card = _add_card($session,$card);
326                         if(UNIVERSAL::isa($card,"OpenILS::EX")) {
327                                 return $card;
328                         }
329
330                         if($patron->card() == $virtual_id) {
331                                 $new_patron->card($card->id());
332                                 $new_patron->ischanged(1);
333                         }
334
335                 } elsif( ref($card) and $card->ischanged() ) {
336                         $card->usr($new_patron->id());
337                         _update_card($session, $card);
338                 }
339         }
340         return $new_patron;
341 }
342
343
344 # adds an card to the db and returns the card with new id
345 sub _add_card {
346         my( $session, $card ) = @_;
347         $card->clear_id();
348
349         warn "Adding card with barcode " . $card->barcode() . "\n";
350         my $req = $session->request(
351                 "open-ils.storage.direct.actor.card.create",
352                 $card );
353
354         my $id = $req->gather(1);
355         if(!$id) { 
356                 return OpenILS::EX->new("DUPLICATE_INVALID_USER_BARCODE");
357         }
358
359         $card->id($id);
360         warn "Created patron card with id $id\n";
361         return $card;
362 }
363
364
365 sub _update_card {
366         my( $session, $card ) = @_;
367         warn Dumper $card;
368
369         my $req = $session->request(
370                 "open-ils.storage.direct.actor.card.update",
371                 $card );
372         my $status = $req->gather(1);
373         if(!defined($status)) { 
374                 throw OpenSRF::EX::ERROR 
375                         ("Unknown error updating card"); 
376         }
377         return $card;
378 }
379
380
381
382
383 sub _delete_address {
384         my( $session, $address ) = @_;
385
386         warn "Deleting address " . $address->street1() . "\n";
387
388         my $req = $session->request(
389                 "open-ils.storage.direct.actor.user_address.delete",
390                 $address );
391         my $status = $req->gather(1);
392         if(!defined($status)) { 
393                 throw OpenSRF::EX::ERROR 
394                         ("Unknown error updating address"); 
395         }
396         warn "Delete address status is $status\n";
397 }
398
399
400
401 sub _add_survey_responses {
402         my ($session, $patron, $new_patron) = @_;
403
404         warn "updating responses for user " . $new_patron->id . "\n";
405
406         my $responses = $patron->survey_responses;
407         for my $resp( @$responses ) {
408                 $resp->usr($new_patron->id);
409         }
410
411         my $status = $apputils->simple_scalar_request(
412                 "open-ils.circ", 
413                 "open-ils.circ.survey.submit.user_id",
414                 $responses );
415
416         return $new_patron;
417 }
418
419
420 sub _create_stat_maps {
421
422         my($session, $user_session, $patron, $new_patron) = @_;
423
424         my $maps = $patron->stat_cat_entries();
425
426         for my $map (@$maps) {
427
428                 next unless($map->isnew() || $map->ischanged());
429
430                 my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.update";
431                 if($map->isnew()) {
432                         $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.create";
433                 }
434
435                 $map->target_usr($new_patron->id);
436
437                 warn "Updating stat entry with method $method and session $user_session and map $map\n";
438
439                 my $req = $session->request($method, $map);
440                 my $status = $req->gather(1);
441
442                 warn "Updated\n";
443
444                 if(!$status) {
445                         throw OpenSRF::EX::ERROR 
446                                 ("Error updating stat map with method $method");        
447                 }
448         }
449
450         return $new_patron;
451 }
452
453
454
455 __PACKAGE__->register_method(
456         method  => "search_username",
457         api_name        => "open-ils.actor.user.search.username",
458 );
459
460 sub search_username {
461         my($self, $client, $username) = @_;
462         my $users = OpenILS::Application::AppUtils->simple_scalar_request(
463                         "open-ils.storage", 
464                         "open-ils.storage.direct.actor.user.search.usrname",
465                         $username );
466         return $users;
467 }
468
469
470
471
472 __PACKAGE__->register_method(
473         method  => "user_retrieve_by_barcode",
474         api_name        => "open-ils.actor.user.fleshed.retrieve_by_barcode",);
475
476 sub user_retrieve_by_barcode {
477         my($self, $client, $user_session, $barcode) = @_;
478         warn "Searching for user with barcode $barcode\n";
479         my $user_obj = $apputils->check_user_session( $user_session ); 
480
481         my $session = OpenSRF::AppSession->create("open-ils.storage");
482
483         # find the card with the given barcode
484         my $creq        = $session->request(
485                         "open-ils.storage.direct.actor.card.search.barcode",
486                         $barcode );
487         my $card = $creq->gather(1);
488         $card = $card->[0];
489         my $user = flesh_user($card->usr(), $session);
490         $session->disconnect();
491         return $user;
492
493 }
494
495
496
497 __PACKAGE__->register_method(
498         method  => "get_user_by_id",
499         api_name        => "open-ils.actor.user.retrieve",);
500
501 sub get_user_by_id {
502         my ($self, $client, $user_session, $id) = @_;
503
504         my $user_obj = $apputils->check_user_session( $user_session ); 
505
506         return $apputils->simple_scalar_request(
507                 "open-ils.storage",
508                 "open-ils.storage.direct.actor.user.retrieve",
509                 $id );
510 }
511
512
513
514 __PACKAGE__->register_method(
515         method  => "get_org_types",
516         api_name        => "open-ils.actor.org_types.retrieve",);
517
518 my $org_types;
519 sub get_org_types {
520         my($self, $client) = @_;
521
522         return $org_types if $org_types;
523          return $org_types = 
524                  $apputils->simple_scalar_request(
525                         "open-ils.storage",
526                         "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
527 }
528
529
530
531 __PACKAGE__->register_method(
532         method  => "get_user_profiles",
533         api_name        => "open-ils.actor.user.profiles.retrieve",
534 );
535
536 my $user_profiles;
537 sub get_user_profiles {
538         return $user_profiles if $user_profiles;
539
540         return $user_profiles = 
541                 $apputils->simple_scalar_request(
542                         "open-ils.storage",
543                         "open-ils.storage.direct.actor.profile.retrieve.all.atomic");
544 }
545
546
547
548 __PACKAGE__->register_method(
549         method  => "get_user_ident_types",
550         api_name        => "open-ils.actor.user.ident_types.retrieve",
551 );
552 my $ident_types;
553 sub get_user_ident_types {
554         return $ident_types if $ident_types;
555         return $ident_types = 
556                 $apputils->simple_scalar_request(
557                 "open-ils.storage",
558                 "open-ils.storage.direct.config.identification_type.retrieve.all.atomic" );
559 }
560
561
562
563
564 __PACKAGE__->register_method(
565         method  => "get_org_unit",
566         api_name        => "open-ils.actor.org_unit.retrieve",
567 );
568
569 sub get_org_unit {
570
571         my( $self, $client, $user_session ) = @_;
572
573         my $user_obj = 
574                 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
575
576         my $home_ou = OpenILS::Application::AppUtils->simple_scalar_request(
577                 "open-ils.storage",
578                 "open-ils.storage.direct.actor.org_unit.retrieve", 
579                 $user_obj->home_ou );
580
581         return $home_ou;
582 }
583
584
585 # build the org tree
586
587 __PACKAGE__->register_method(
588         method  => "get_org_tree",
589         api_name        => "open-ils.actor.org_tree.retrieve",
590         argc            => 1, 
591         note            => "Returns the entire org tree structure",
592 );
593
594 sub get_org_tree {
595         my( $self, $client) = @_;
596
597         # see if it's in the cache
598         warn "Getting ORG Tree\n";
599         my $tree = $cache_client->get_cache('orgtree');
600         if($tree) { 
601                 warn "Found orgtree in cache. returning...\n";
602                 return $tree; 
603         }
604
605         my $orglist = $apputils->simple_scalar_request( 
606                 "open-ils.storage", 
607                 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
608
609         $tree = $self->build_org_tree($orglist);
610         $cache_client->put_cache('orgtree', $tree);
611
612         return $tree;
613
614 }
615
616 # turns an org list into an org tree
617 sub build_org_tree {
618
619         my( $self, $orglist) = @_;
620
621         return $orglist unless ( 
622                         ref($orglist) and @$orglist > 1 );
623
624         my @list = sort { 
625                 $a->ou_type <=> $b->ou_type ||
626                 $a->name cmp $b->name } @$orglist;
627
628         for my $org (@list) {
629
630                 next unless ($org and defined($org->parent_ou));
631                 my ($parent) = grep { $_->id == $org->parent_ou } @list;
632                 next unless $parent;
633
634                 $parent->children([]) unless defined($parent->children); 
635                 push( @{$parent->children}, $org );
636         }
637
638         return $list[0];
639
640 }
641
642
643 __PACKAGE__->register_method(
644         method  => "get_org_descendants",
645         api_name        => "open-ils.actor.org_tree.descendants.retrieve"
646 );
647
648 # depth is optional.  org_unit is the id
649 sub get_org_descendants {
650         my( $self, $client, $org_unit, $depth ) = @_;
651         my $orglist = $apputils->simple_scalar_request(
652                         "open-ils.storage", 
653                         "open-ils.storage.actor.org_unit.descendants.atomic",
654                         $org_unit, $depth );
655         return $self->build_org_tree($orglist);
656 }
657
658
659 __PACKAGE__->register_method(
660         method  => "get_org_ancestors",
661         api_name        => "open-ils.actor.org_tree.ancestors.retrieve"
662 );
663
664 # depth is optional.  org_unit is the id
665 sub get_org_ancestors {
666         my( $self, $client, $org_unit, $depth ) = @_;
667         my $orglist = $apputils->simple_scalar_request(
668                         "open-ils.storage", 
669                         "open-ils.storage.actor.org_unit.ancestors.atomic",
670                         $org_unit, $depth );
671         return $self->build_org_tree($orglist);
672 }
673
674
675 __PACKAGE__->register_method(
676         method  => "get_standings",
677         api_name        => "open-ils.actor.standings.retrieve"
678 );
679
680 my $user_standings;
681 sub get_standings {
682         return $user_standings if $user_standings;
683         return $user_standings = 
684                 $apputils->simple_scalar_request(
685                         "open-ils.storage",
686                         "open-ils.storage.direct.config.standing.retrieve.all.atomic" );
687 }
688
689
690
691 __PACKAGE__->register_method(
692         method  => "get_my_org_path",
693         api_name        => "open-ils.actor.org_unit.full_path.retrieve"
694 );
695
696 sub get_my_org_path {
697         my( $self, $client, $user_session, $org_id ) = @_;
698         my $user_obj = $apputils->check_user_session($user_session); 
699         if(!defined($org_id)) { $org_id = $user_obj->home_ou; }
700
701         return $apputils->simple_scalar_request(
702                 "open-ils.storage",
703                 "open-ils.storage.actor.org_unit.full_path.atomic",
704                 $org_id );
705 }
706
707
708 __PACKAGE__->register_method(
709         method  => "patron_adv_search",
710         api_name        => "open-ils.actor.patron.search.advanced" );
711
712 sub patron_adv_search {
713         my( $self, $client, $staff_login, $search_hash ) = @_;
714
715         use Data::Dumper;
716         warn "patron adv with $staff_login and search " . 
717                 Dumper($search_hash) . "\n";
718
719         my $session = OpenSRF::AppSession->create("open-ils.storage");
720         my $req = $session->request(
721                 "open-ils.storage.actor.user.crazy_search", $search_hash);
722
723         my $ans = $req->gather(1);
724         $session->disconnect();
725         return $ans;
726
727 }
728
729
730
731
732
733
734
735
736 1;
737
738
739
740
741 __END__
742
743
744 some old methods that may be good to keep around for now
745
746 sub _delete_card {
747         my( $session, $card ) = @_;
748
749         warn "Deleting card with barcode " . $card->barcode() . "\n";
750         my $req = $session->request(
751                 "open-ils.storage.direct.actor.card.delete",
752                 $card );
753         my $status = $req->gather(1);
754         if(!defined($status)) { 
755                 throw OpenSRF::EX::ERROR 
756                         ("Unknown error updating card"); 
757         }
758 }
759
760
761
762 # deletes the patron and any attached addresses and cards
763 __PACKAGE__->register_method(
764         method  => "delete_patron",
765         api_name        => "open-ils.actor.patron.delete",
766 );
767
768 sub delete_patron {
769
770         my( $self, $client, $patron ) = @_;
771         my $session = $apputils->start_db_session();
772         my $err = undef;
773
774         try {
775
776                 $patron->clear_mailing_address();
777                 $patron->clear_billing_address();
778                 $patron->ischanged(1);
779
780                 _update_patron($session, $patron);
781                 _delete_address($session,$_) for (@{$patron->addresses()});
782                 _delete_card($session,$_) for (@{$patron->cards()});
783                 _delete_patron($session,$patron);
784                 $apputils->commit_db_session($session);
785
786         } catch Error with {
787                 my $e = shift;
788                 $err =  "-*- Failure deleting user: $e";
789                 $apputils->rollback_db_session($session);
790                 warn $err;
791         };
792
793         if($err) { throw OpenSRF::EX::ERROR ($err); }
794         warn "Patron Delete complete\n";
795         return 1;
796 }
797
798 sub _delete_patron {
799         my( $session, $patron ) = @_;
800
801         warn "Deleting patron " . $patron->usrname() . "\n";
802
803         my $req = $session->request(
804                 "open-ils.storage.direct.actor.user.delete",
805                 $patron );
806         my $status = $req->gather(1);
807         if(!defined($status)) { 
808                 throw OpenSRF::EX::ERROR 
809                         ("Unknown error updating patron"); 
810         }
811 }
812