1 package OpenILS::Application::Storage::Publisher::actor;
2 use base qw/OpenILS::Application::Storage/;
3 use OpenILS::Application::Storage::CDBI::actor;
4 use OpenSRF::Utils::Logger qw/:level/;
5 use OpenILS::Utils::Fieldmapper;
7 my $log = 'OpenSRF::Utils::Logger';
14 return undef unless @barcodes;
16 for my $card ( actor::card->search( { barcode => @barcodes } ) ) {
19 return $card->usr->to_fieldmapper;
21 $client->respond( $card->usr->to_fieldmapper);
25 __PACKAGE__->register_method(
26 api_name => 'open-ils.storage.direct.actor.user.search.barcode',
28 method => 'user_by_barcode',
38 my $limit = shift || 1000;
40 $sort = ['family_name','first_given_name'] unless ($$sort[0]);
44 # group 2 = phone, ident
46 my $usr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
47 my @usrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
49 my $addr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
50 my @addrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
52 my $pv = $$search{phone}{value};
53 my $iv = $$search{ident}{value};
54 my $nv = $$search{name}{value};
60 for my $p ( qw/day_phone evening_phone other_phone/ ) {
61 push @ps, "LOWER($p) ~ ?";
64 $phone = '(' . join(' OR ', @ps) . ')';
71 for my $i ( qw/ident_value ident_value2/ ) {
72 push @is, "LOWER($i) ~ ?";
75 $ident = '(' . join(' OR ', @is) . ')';
82 for my $n ( qw/first_given_name second_given_name family_name/ ) {
83 push @ns, "LOWER($i) ~ ?";
86 $name = '(' . join(' OR ', @ns) . ')';
89 my $usr_where = join ' AND ', grep { $_ } ($usr,$phone,$ident,$name);
90 my $addr_where = $addr;
93 my $u_table = actor::user->table;
94 my $a_table = actor::user_address->table;
96 my $u_select = "SELECT id as id FROM $u_table u WHERE $usr_where";
97 my $a_select = "SELECT usr as id FROM $a_table a WHERE $addr_where";
102 $select = "$u_select INTERSECT $a_select";
106 } elsif ($addr_where) {
112 my $order_by = join ', ', map { 'users.'. $_} @$sort;
114 $select = "SELECT users.id FROM $u_table AS users JOIN ($select) AS search USING (id) ORDER BY $order_by LIMIT $limit";
116 return actor::user->db_Main->selectcol_arrayref($select, {}, map {lc($_)} (@usrv,@phonev,@identv,@namev,@addrv));
118 __PACKAGE__->register_method(
119 api_name => 'open-ils.storage.actor.user.crazy_search',
121 method => 'patron_search',
124 =comment not gonna use it...
129 my $searches = shift;
131 return undef unless (defined $searches);
133 for my $usr ( actor::user->search( $searches ) ) {
135 $client->respond( flesh_user( $usr ) );
139 __PACKAGE__->register_method(
140 api_name => 'open-ils.storage.fleshed.actor.user.search',
142 method => 'fleshed_search',
147 sub fleshed_search_like {
150 my $searches = shift;
152 return undef unless (defined $searches);
154 for my $usr ( actor::user->search_like( $searches ) ) {
156 $client->respond( flesh_user( $usr ) );
160 __PACKAGE__->register_method(
161 api_name => 'open-ils.storage.fleshed.actor.user.search_like',
163 method => 'user_by_barcode',
168 sub retrieve_fleshed_user {
173 return undef unless @ids;
175 @ids = ($ids[0]) unless ($self->api_name =~ /batch/o);
177 $client->respond( flesh_user( actor::user->retrieve( $_ ) ) ) for ( @ids );
181 __PACKAGE__->register_method(
182 api_name => 'open-ils.storage.fleshed.actor.user.retrieve',
184 method => 'retrieve_fleshed_user',
187 __PACKAGE__->register_method(
188 api_name => 'open-ils.storage.fleshed.actor.user.batch.retrieve',
190 method => 'retrieve_fleshed_user',
199 my $standing = $usr->standing;
200 my $profile = $usr->profile;
201 my $ident_type = $usr->ident_type;
203 my $maddress = $usr->mailing_address;
204 my $baddress = $usr->billing_address;
205 my $card = $usr->card;
207 my @addresses = $usr->addresses;
208 my @cards = $usr->cards;
210 my $usr_fm = $usr->to_fieldmapper;
211 $usr_fm->standing( $standing->to_fieldmapper );
212 $usr_fm->profile( $profile->to_fieldmapper );
213 $usr_fm->ident_type( $ident_type->to_fieldmapper );
215 $usr_fm->card( $card->to_fieldmapper );
216 $usr_fm->mailing_address( $maddress->to_fieldmapper ) if ($maddress);
217 $usr_fm->billing_address( $baddress->to_fieldmapper ) if ($baddress);
219 $usr_fm->cards( [ map { $_->to_fieldmapper } @cards ] );
220 $usr_fm->addresses( [ map { $_->to_fieldmapper } @addresses ] );
231 my $select =<<" SQL";
234 ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
237 my $sth = actor::org_unit->db_Main->prepare_cached($select);
240 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
244 __PACKAGE__->register_method(
245 api_name => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
248 method => 'org_unit_list',
251 sub org_unit_type_list {
255 my $select =<<" SQL";
257 FROM actor.org_unit_type
258 ORDER BY depth, name;
261 my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
264 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
268 __PACKAGE__->register_method(
269 api_name => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
272 method => 'org_unit_type_list',
275 sub org_unit_full_path {
280 return undef unless (@binds);
282 my $func = 'actor.org_unit_full_path(?)';
283 $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
285 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
286 $sth->execute(@binds);
288 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
292 __PACKAGE__->register_method(
293 api_name => 'open-ils.storage.actor.org_unit.full_path',
296 method => 'org_unit_full_path',
299 sub org_unit_ancestors {
304 return undef unless ($id);
306 my $func = 'actor.org_unit_ancestors(?)';
308 my $sth = actor::org_unit->db_Main->prepare_cached(<<" SQL");
311 JOIN actor.org_unit_type t ON (f.ou_type = t.id)
312 ORDER BY t.depth, f.name;
314 $sth->execute(''.$id);
316 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
320 __PACKAGE__->register_method(
321 api_name => 'open-ils.storage.actor.org_unit.ancestors',
324 method => 'org_unit_ancestors',
327 sub org_unit_descendants {
333 return undef unless ($id);
335 my $func = 'actor.org_unit_descendants(?)';
336 if (defined $depth) {
337 $func = 'actor.org_unit_descendants(?,?)';
340 my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
341 $sth->execute(''.$id, ''.$depth) if (defined $depth);
342 $sth->execute(''.$id) unless (defined $depth);
344 $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
348 __PACKAGE__->register_method(
349 api_name => 'open-ils.storage.actor.org_unit.descendants',
352 method => 'org_unit_descendants',
359 for my $rec ( actor::profile->retrieve_all ) {
360 $client->respond( $rec->to_fieldmapper );
365 __PACKAGE__->register_method(
366 method => 'profile_all',
367 api_name => 'open-ils.storage.direct.actor.profile.retrieve.all',
372 sub fleshed_actor_stat_cat {
377 @list = ($list[0]) unless ($self->api_name =~ /batch/o);
380 my $cat = actor::stat_cat->retrieve($sc);
383 my $sc_fm = $cat->to_fieldmapper;
384 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
386 $client->respond( $sc_fm );
392 __PACKAGE__->register_method(
393 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
396 method => 'fleshed_actor_stat_cat',
399 __PACKAGE__->register_method(
400 api_name => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
404 method => 'fleshed_actor_stat_cat',
407 #XXX Fix stored proc calls
408 sub ranged_actor_stat_cat_all {
413 return undef unless ($ou);
414 my $s_table = actor::stat_cat->table;
416 my $select = <<" SQL";
419 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
424 $fleshed = 1 if ($self->api_name =~ /fleshed/o);
426 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
429 for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
430 my $sc_fm = $sc->to_fieldmapper;
432 [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
434 $client->respond( $sc_fm );
439 __PACKAGE__->register_method(
440 api_name => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
444 method => 'ranged_actor_stat_cat_all',
447 __PACKAGE__->register_method(
448 api_name => 'open-ils.storage.ranged.actor.stat_cat.all',
452 method => 'ranged_actor_stat_cat_all',
455 #XXX Fix stored proc calls
456 sub ranged_actor_stat_cat_entry {
462 return undef unless ($ou);
463 my $s_table = actor::stat_cat_entry->table;
465 my $select = <<" SQL";
468 JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
473 my $sth = actor::stat_cat->db_Main->prepare_cached($select);
474 $sth->execute($ou,$sc);
476 for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
477 $client->respond( $sce->to_fieldmapper );
482 __PACKAGE__->register_method(
483 api_name => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
486 method => 'ranged_actor_stat_cat_entry',