1 package OpenILS::Application::Actor;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
5 use OpenSRF::EX qw(:try);
6 use OpenILS::Application::AppUtils;
7 use OpenILS::Utils::Fieldmapper;
8 use OpenILS::Application::Search::Actor;
10 my $apputils = "OpenILS::Application::AppUtils";
11 sub _d { warn "Patron:\n" . Dumper(shift()); }
12 my $cache_client = OpenSRF::Utils::Cache->new( "global", 0 );
15 __PACKAGE__->register_method(
16 method => "update_patron",
17 api_name => "open-ils.actor.patron.create",
20 __PACKAGE__->register_method(
21 method => "update_patron",
22 api_name => "open-ils.actor.patron.update",
27 my( $self, $client, $patron ) = @_;
29 my $session = $apputils->start_db_session();
33 # $new_patron is the patron in progress. $patron is the original patron
34 # passed in with the method. new_patron will change as the components
35 # of patron are added/updated.
40 # create/update the patron first so we can use his id
41 if( $self->api_name =~ /create/ ) {
42 $new_patron = _add_patron(
43 $session, _clone_patron($patron));
45 $new_patron = $patron;
48 $new_patron = _add_update_addresses($session, $patron, $new_patron);
49 $new_patron = _add_update_cards($session, $patron, $new_patron);
51 # re-update the patron if anything has happened to him during this process
52 if($new_patron->ischanged()) {
53 $new_patron = _update_patron($session, $new_patron);
55 $apputils->commit_db_session($session);
59 $err = "-*- Failure adding user: $e";
60 $apputils->rollback_db_session($session);
64 if($err) { throw OpenSRF::EX::ERROR ($err); }
65 warn "Patron Update/Create complete\n";
66 return flesh_user($new_patron->id());
76 $session = OpenSRF::AppSession->create("open-ils.storage");
80 # grab the user with the given card
81 my $ureq = $session->request(
82 "open-ils.storage.direct.actor.user.retrieve",
84 my $user = $ureq->gather(1);
87 my $cards_req = $session->request(
88 "open-ils.storage.direct.actor.card.search.usr",
90 $user->cards( $cards_req->gather(1) );
92 my $add_req = $session->request(
93 "open-ils.storage.direct.actor.user_address.search.usr",
95 $user->addresses( $add_req->gather(1) );
97 if($kill) { $session->disconnect(); }
104 # clone and clear stuff that would break the database
108 my $new_patron = Fieldmapper::actor::user->new();
110 my $fmap = $Fieldmapper::fieldmap;
111 no strict; # shallow clone, may be useful in the fieldmapper
113 (keys %{$fmap->{"Fieldmapper::actor::user"}->{'fields'}}) {
114 $new_patron->$field( $patron->$field() );
119 $new_patron->clear_billing_address();
120 $new_patron->clear_mailing_address();
121 $new_patron->clear_addresses();
122 $new_patron->clear_card();
123 $new_patron->clear_cards();
124 $new_patron->clear_id();
125 $new_patron->clear_isnew();
126 $new_patron->clear_changed();
127 $new_patron->clear_deleted();
137 my $req = $session->request(
138 "open-ils.storage.direct.actor.user.create",$patron);
139 my $id = $req->gather(1);
140 if(!$id) { throw OpenSRF::EX::ERROR ("Unable to create new user"); }
141 warn "Created new patron with id $id\n";
149 my( $session, $patron) = @_;
151 warn "updating patron " . $patron->usrname() . "\n";
152 my $req = $session->request(
153 "open-ils.storage.direct.actor.user.update",$patron );
154 my $status = $req->gather(1);
155 if(!defined($status)) {
156 throw OpenSRF::EX::ERROR
157 ("Unknown error updating patron");
163 sub _add_update_addresses {
166 my $new_patron = shift;
168 my $current_id; # id of the address before creation
170 for my $address (@{$patron->addresses()}) {
172 $address->usr($new_patron->id());
174 if(ref($address) and $address->isnew()) {
175 warn "Adding new address at street " . $address->street1() . "\n";
177 $current_id = $address->id();
178 $address = _add_address($session,$address);
180 if( $patron->billing_address() == $current_id ) {
181 $new_patron->billing_address($address->id());
182 $new_patron->ischanged(1);
185 if( $patron->mailing_address() == $current_id ) {
186 $new_patron->mailing_address($address->id());
187 $new_patron->ischanged(1);
190 } elsif( ref($address) and $address->ischanged() ) {
191 warn "Updating address at street " . $address->street1();
192 _update_address($session,$address);
194 } elsif( ref($address) and $address->isdeleted() ) {
195 warn "Deleting address at street " . $address->street1();
196 _delete_address($session,$address);
204 # adds an address to the db and returns the address with new id
206 my($session, $address) = @_;
207 $address->clear_id();
209 # put the address into the database
210 my $req = $session->request(
211 "open-ils.storage.direct.actor.user_address.create",
215 my $id = $req->gather(1);
217 throw OpenSRF::EX::ERROR
218 ("Unable to create new user address");
221 warn "Created address with id $id\n";
223 # update all the necessary id's
229 sub _update_address {
230 my( $session, $address ) = @_;
231 my $req = $session->request(
232 "open-ils.storage.direct.actor.user_address.update",
234 my $status = $req->gather(1);
235 if(!defined($status)) {
236 throw OpenSRF::EX::ERROR
237 ("Unknown error updating address");
244 sub _add_update_cards {
248 my $new_patron = shift;
250 my $virtual_id; #id of the card before creation
251 for my $card (@{$patron->cards()}) {
253 $card->usr($new_patron->id());
255 if(ref($card) and $card->isnew()) {
257 $virtual_id = $card->id();
258 $card = _add_card($session,$card);
260 if($patron->card() == $virtual_id) {
261 $new_patron->card($card->id());
262 $new_patron->ischanged(1);
265 } elsif( ref($card) and $card->ischanged() ) {
266 _update_card($session, $card);
273 # adds an card to the db and returns the card with new id
275 my( $session, $card ) = @_;
278 warn "Adding card with barcode " . $card->barcode() . "\n";
279 my $req = $session->request(
280 "open-ils.storage.direct.actor.card.create",
283 my $id = $req->gather(1);
285 throw OpenSRF::EX::ERROR
286 ("Unknown error creating card");
290 warn "Created patron card with id $id\n";
296 my( $session, $card ) = @_;
299 my $req = $session->request(
300 "open-ils.storage.direct.actor.card.update",
302 my $status = $req->gather(1);
303 if(!defined($status)) {
304 throw OpenSRF::EX::ERROR
305 ("Unknown error updating card");
313 sub _delete_address {
314 my( $session, $address ) = @_;
316 warn "Deleting address " . $address->street1() . "\n";
318 my $req = $session->request(
319 "open-ils.storage.direct.actor.user_address.delete",
321 my $status = $req->gather(1);
322 if(!defined($status)) {
323 throw OpenSRF::EX::ERROR
324 ("Unknown error updating address");
331 __PACKAGE__->register_method(
332 method => "search_username",
333 api_name => "open-ils.actor.user.search.username",
336 sub search_username {
337 my($self, $client, $username) = @_;
338 my $users = OpenILS::Application::AppUtils->simple_scalar_request(
340 "open-ils.storage.direct.actor.user.search.usrname",
346 __PACKAGE__->register_method(
347 method => "user_retrieve_by_barcode",
348 api_name => "open-ils.actor.user.fleshed.retrieve_by_barcode",
351 sub user_retrieve_by_barcode {
352 my($self, $client, $barcode) = @_;
353 warn "Searching for user with barcode $barcode\n";
355 my $session = OpenSRF::AppSession->create("open-ils.storage");
357 # find the card with the given barcode
358 my $creq = $session->request(
359 "open-ils.storage.direct.actor.card.search.barcode",
361 my $card = $creq->gather(1);
363 my $user = flesh_user($card->usr(), $session);
364 $session->disconnect();
372 __PACKAGE__->register_method(
373 method => "get_org_types",
374 api_name => "open-ils.actor.org_types.retrieve",
377 my($self, $client) = @_;
379 my $org_typelist = OpenILS::Application::AppUtils->simple_scalar_request(
381 "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
383 return $org_typelist;
387 __PACKAGE__->register_method(
388 method => "get_user_profiles",
389 api_name => "open-ils.actor.user.profiles.retrieve",
391 sub get_user_profiles {
392 return OpenILS::Application::AppUtils->simple_scalar_request(
394 "open-ils.storage.direct.actor.profile.retrieve.all.atomic",
400 __PACKAGE__->register_method(
401 method => "get_user_ident_types",
402 api_name => "open-ils.actor.user.ident_types.retrieve",
404 sub get_user_ident_types {
405 return OpenILS::Application::AppUtils->simple_scalar_request(
407 "open-ils.storage.direct.config.identification_type.retrieve.all.atomic" );
413 __PACKAGE__->register_method(
414 method => "get_org_unit",
415 api_name => "open-ils.actor.org_unit.retrieve",
417 note => "Returns the entire org tree structure",
422 my( $self, $client, $user_session ) = @_;
425 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
427 my $home_ou = OpenILS::Application::AppUtils->simple_scalar_request(
429 "open-ils.storage.direct.actor.org_unit.retrieve",
430 $user_obj->home_ou );
438 __PACKAGE__->register_method(
439 method => "get_org_tree",
440 api_name => "open-ils.actor.org_tree.retrieve",
442 note => "Returns the entire org tree structure",
446 my( $self, $client) = @_;
448 # see if it's in the cache
449 warn "Getting ORG Tree\n";
450 my $tree = $cache_client->get_cache('orgtree');
452 warn "Found orgtree in cache. returning...\n";
456 my $orglist = $apputils->simple_scalar_request(
458 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
460 $tree = $self->build_org_tree($orglist);
461 $cache_client->put_cache('orgtree', $tree);
469 my( $self, $orglist) = @_;
471 return $orglist unless (
472 ref($orglist) and @$orglist > 1 );
475 $a->ou_type <=> $b->ou_type ||
476 $a->name cmp $b->name } @$orglist;
478 for my $org (@list) {
480 next unless ($org and defined($org->parent_ou));
481 my ($parent) = grep { $_->id == $org->parent_ou } @list;
484 $parent->children([]) unless defined($parent->children);
485 push( @{$parent->children}, $org );
507 my( $session, $card ) = @_;
509 warn "Deleting card with barcode " . $card->barcode() . "\n";
510 my $req = $session->request(
511 "open-ils.storage.direct.actor.card.delete",
513 my $status = $req->gather(1);
514 if(!defined($status)) {
515 throw OpenSRF::EX::ERROR
516 ("Unknown error updating card");
522 # deletes the patron and any attached addresses and cards
523 __PACKAGE__->register_method(
524 method => "delete_patron",
525 api_name => "open-ils.actor.patron.delete",
530 my( $self, $client, $patron ) = @_;
531 my $session = $apputils->start_db_session();
536 $patron->clear_mailing_address();
537 $patron->clear_billing_address();
538 $patron->ischanged(1);
540 _update_patron($session, $patron);
541 _delete_address($session,$_) for (@{$patron->addresses()});
542 _delete_card($session,$_) for (@{$patron->cards()});
543 _delete_patron($session,$patron);
544 $apputils->commit_db_session($session);
548 $err = "-*- Failure deleting user: $e";
549 $apputils->rollback_db_session($session);
553 if($err) { throw OpenSRF::EX::ERROR ($err); }
554 warn "Patron Delete complete\n";
561 my( $session, $patron ) = @_;
563 warn "Deleting patron " . $patron->usrname() . "\n";
565 my $req = $session->request(
566 "open-ils.storage.direct.actor.user.delete",
568 my $status = $req->gather(1);
569 if(!defined($status)) {
570 throw OpenSRF::EX::ERROR
571 ("Unknown error updating patron");