]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/actor.pm
minor bugfix
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Publisher / actor.pm
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;
6
7 my $log = 'OpenSRF::Utils::Logger';
8
9 sub user_by_barcode {
10         my $self = shift;
11         my $client = shift;
12         my @barcodes = shift;
13
14         return undef unless @barcodes;
15
16         for my $card ( actor::card->search( { barcode => @barcodes } ) ) {
17                 next unless $card;
18                 if (@barcodes == 1) {
19                         return $card->usr->to_fieldmapper;
20                 }
21                 $client->respond( $card->usr->to_fieldmapper);
22         }
23         return undef;
24 }
25 __PACKAGE__->register_method(
26         api_name        => 'open-ils.storage.direct.actor.user.search.barcode',
27         api_level       => 1,
28         method          => 'user_by_barcode',
29         stream          => 1,
30         cachable        => 1,
31 );
32
33
34 sub patron_search {
35         my $self = shift;
36         my $client = shift;
37         my $search = shift;
38
39         # group 0 = user
40         # group 1 = address
41         # group 2 = phone, ident
42
43         my $usr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
44         my @usrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '0' } keys %$search;
45
46         my $addr = join ' AND ', map { "LOWER($_) ~ ?" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
47         my @addrv = map { "^$$search{$_}{value}" } grep { ''.$$search{$_}{group} eq '1' } keys %$search;
48
49         my $pv = $$search{phone}{value};
50         my $iv = $$search{ident}{value};
51
52         my $phone = '';
53         my @ps;
54         my @phonev;
55         if ($pv) {
56                 for my $p ( qw/day_phone evening_phone other_phone/ ) {
57                         push @ps, "LOWER($p) ~ ?";
58                         push @phonev, "^$pv";
59                 }
60                 $phone = '(' . join(' OR ', @ps) . ')';
61         }
62
63         my $ident = '';
64         my @is;
65         my @identv;
66         if ($pv) {
67                 for my $i ( qw/ident_value ident_value2/ ) {
68                         push @is, "LOWER($i) ~ ?";
69                         push @identv, "^$iv";
70                 }
71                 $ident = '(' . join(' OR ', @is) . ')';
72         }
73
74         my $usr_where = join ' AND ', grep { $_ } ($usr,$phone,$ident);
75         my $addr_where = $addr;
76
77
78         my $u_table = actor::user->table;
79         my $a_table = actor::user_address->table;
80
81         my $u_select = "SELECT id FROM $u_table a WHERE $usr_where";
82         my $a_select = "SELECT usr FROM $a_table a WHERE $addr_where";
83
84         my $select = '';
85         if ($usr_where) {
86                 if ($addr_where) {
87                         $select = "$u_select INTERSECT $a_select";
88                 } else {
89                         $select = $u_select;
90                 }
91         } elsif ($addr_where) {
92                 $select = $a_select;
93         } else {
94                 return undef;
95         }
96
97         return actor::user->db_Main->selectcol_arrayref($select." LIMIT 1000", {}, map {lc($_)} (@usrv,@phonev,@identv,@addrv));
98 }
99 __PACKAGE__->register_method(
100         api_name        => 'open-ils.storage.actor.user.crazy_search',
101         api_level       => 1,
102         method          => 'patron_search',
103 );
104
105 =comment not gonna use it...
106
107 sub fleshed_search {
108         my $self = shift;
109         my $client = shift;
110         my $searches = shift;
111
112         return undef unless (defined $searches);
113
114         for my $usr ( actor::user->search( $searches ) ) {
115                 next unless $usr;
116                 $client->respond( flesh_user( $usr ) );
117         }
118         return undef;
119 }
120 __PACKAGE__->register_method(
121         api_name        => 'open-ils.storage.fleshed.actor.user.search',
122         api_level       => 1,
123         method          => 'fleshed_search',
124         stream          => 1,
125         cachable        => 1,
126 );
127
128 sub fleshed_search_like {
129         my $self = shift;
130         my $client = shift;
131         my $searches = shift;
132
133         return undef unless (defined $searches);
134
135         for my $usr ( actor::user->search_like( $searches ) ) {
136                 next unless $usr;
137                 $client->respond( flesh_user( $usr ) );
138         }
139         return undef;
140 }
141 __PACKAGE__->register_method(
142         api_name        => 'open-ils.storage.fleshed.actor.user.search_like',
143         api_level       => 1,
144         method          => 'user_by_barcode',
145         stream          => 1,
146         cachable        => 1,
147 );
148
149 sub retrieve_fleshed_user {
150         my $self = shift;
151         my $client = shift;
152         my @ids = shift;
153
154         return undef unless @ids;
155
156         @ids = ($ids[0]) unless ($self->api_name =~ /batch/o); 
157
158         $client->respond( flesh_user( actor::user->retrieve( $_ ) ) ) for ( @ids );
159
160         return undef;
161 }
162 __PACKAGE__->register_method(
163         api_name        => 'open-ils.storage.fleshed.actor.user.retrieve',
164         api_level       => 1,
165         method          => 'retrieve_fleshed_user',
166         cachable        => 1,
167 );
168 __PACKAGE__->register_method(
169         api_name        => 'open-ils.storage.fleshed.actor.user.batch.retrieve',
170         api_level       => 1,
171         method          => 'retrieve_fleshed_user',
172         stream          => 1,
173         cachable        => 1,
174 );
175
176 sub flesh_user {
177         my $usr = shift;
178
179
180         my $standing = $usr->standing;
181         my $profile = $usr->profile;
182         my $ident_type = $usr->ident_type;
183                 
184         my $maddress = $usr->mailing_address;
185         my $baddress = $usr->billing_address;
186         my $card = $usr->card;
187
188         my @addresses = $usr->addresses;
189         my @cards = $usr->cards;
190
191         my $usr_fm = $usr->to_fieldmapper;
192         $usr_fm->standing( $standing->to_fieldmapper );
193         $usr_fm->profile( $profile->to_fieldmapper );
194         $usr_fm->ident_type( $ident_type->to_fieldmapper );
195
196         $usr_fm->card( $card->to_fieldmapper );
197         $usr_fm->mailing_address( $maddress->to_fieldmapper ) if ($maddress);
198         $usr_fm->billing_address( $baddress->to_fieldmapper ) if ($baddress);
199
200         $usr_fm->cards( [ map { $_->to_fieldmapper } @cards ] );
201         $usr_fm->addresses( [ map { $_->to_fieldmapper } @addresses ] );
202
203         return $usr_fm;
204 }
205
206 =cut
207
208 sub org_unit_list {
209         my $self = shift;
210         my $client = shift;
211
212         my $select =<<" SQL";
213         SELECT  *
214           FROM  actor.org_unit
215           ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
216         SQL
217
218         my $sth = actor::org_unit->db_Main->prepare_cached($select);
219         $sth->execute;
220
221         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
222
223         return undef;
224 }
225 __PACKAGE__->register_method(
226         api_name        => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
227         api_level       => 1,
228         stream          => 1,
229         method          => 'org_unit_list',
230 );
231
232 sub org_unit_type_list {
233         my $self = shift;
234         my $client = shift;
235
236         my $select =<<" SQL";
237         SELECT  *
238           FROM  actor.org_unit_type
239           ORDER BY depth, name;
240         SQL
241
242         my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
243         $sth->execute;
244
245         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
246
247         return undef;
248 }
249 __PACKAGE__->register_method(
250         api_name        => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
251         api_level       => 1,
252         stream          => 1,
253         method          => 'org_unit_type_list',
254 );
255
256 sub org_unit_full_path {
257         my $self = shift;
258         my $client = shift;
259         my @binds = @_;
260
261         return undef unless (@binds);
262
263         my $func = 'actor.org_unit_full_path(?)';
264         $func = 'actor.org_unit_full_path(?,?)' if (@binds > 1);
265
266         my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
267         $sth->execute(@binds);
268
269         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
270
271         return undef;
272 }
273 __PACKAGE__->register_method(
274         api_name        => 'open-ils.storage.actor.org_unit.full_path',
275         api_level       => 1,
276         stream          => 1,
277         method          => 'org_unit_full_path',
278 );
279
280 sub org_unit_ancestors {
281         my $self = shift;
282         my $client = shift;
283         my $id = shift;
284
285         return undef unless ($id);
286
287         my $func = 'actor.org_unit_ancestors(?)';
288
289         my $sth = actor::org_unit->db_Main->prepare_cached(<<"  SQL");
290                 SELECT  f.*
291                   FROM  $func f
292                         JOIN actor.org_unit_type t ON (f.ou_type = t.id)
293                   ORDER BY t.depth, f.name;
294         SQL
295         $sth->execute(''.$id);
296
297         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
298
299         return undef;
300 }
301 __PACKAGE__->register_method(
302         api_name        => 'open-ils.storage.actor.org_unit.ancestors',
303         api_level       => 1,
304         stream          => 1,
305         method          => 'org_unit_ancestors',
306 );
307
308 sub org_unit_descendants {
309         my $self = shift;
310         my $client = shift;
311         my $id = shift;
312         my $depth = shift;
313
314         return undef unless ($id);
315
316         my $func = 'actor.org_unit_descendants(?)';
317         if (defined $depth) {
318                 $func = 'actor.org_unit_descendants(?,?)';
319         }
320
321         my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
322         $sth->execute(''.$id, ''.$depth) if (defined $depth);
323         $sth->execute(''.$id) unless (defined $depth);
324
325         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
326
327         return undef;
328 }
329 __PACKAGE__->register_method(
330         api_name        => 'open-ils.storage.actor.org_unit.descendants',
331         api_level       => 1,
332         stream          => 1,
333         method          => 'org_unit_descendants',
334 );
335
336 sub profile_all {
337         my $self = shift;
338         my $client = shift;
339
340         for my $rec ( actor::profile->retrieve_all ) {
341                 $client->respond( $rec->to_fieldmapper );
342         }
343
344         return undef;
345 }
346 __PACKAGE__->register_method(
347         method          => 'profile_all',
348         api_name        => 'open-ils.storage.direct.actor.profile.retrieve.all',
349         argc            => 0,
350         stream          => 1,
351 );
352
353 sub fleshed_actor_stat_cat {
354         my $self = shift;
355         my $client = shift;
356         my @list = @_;
357         
358         @list = ($list[0]) unless ($self->api_name =~ /batch/o);
359
360         for my $sc (@list) {
361                 my $cat = actor::stat_cat->retrieve($sc);
362                 next unless ($cat);
363
364                 my $sc_fm = $cat->to_fieldmapper;
365                 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
366
367                 $client->respond( $sc_fm );
368
369         }
370
371         return undef;
372 }
373 __PACKAGE__->register_method(
374         api_name        => 'open-ils.storage.fleshed.actor.stat_cat.retrieve',
375         api_level       => 1,
376         argc            => 1,
377         method          => 'fleshed_actor_stat_cat',
378 );
379
380 __PACKAGE__->register_method(
381         api_name        => 'open-ils.storage.fleshed.actor.stat_cat.retrieve.batch',
382         api_level       => 1,
383         argc            => 1,
384         stream          => 1,
385         method          => 'fleshed_actor_stat_cat',
386 );
387
388 #XXX Fix stored proc calls
389 sub ranged_actor_stat_cat_all {
390         my $self = shift;
391         my $client = shift;
392         my $ou = ''.shift();
393         
394         return undef unless ($ou);
395         my $s_table = actor::stat_cat->table;
396
397         my $select = <<"        SQL";
398                 SELECT  s.*
399                   FROM  $s_table s
400                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
401                   ORDER BY name
402         SQL
403
404         $fleshed = 0;
405         $fleshed = 1 if ($self->api_name =~ /fleshed/o);
406
407         my $sth = actor::stat_cat->db_Main->prepare_cached($select);
408         $sth->execute($ou);
409
410         for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
411                 my $sc_fm = $sc->to_fieldmapper;
412                 $sc_fm->entries(
413                         [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
414                 ) if ($fleshed);
415                 $client->respond( $sc_fm );
416         }
417
418         return undef;
419 }
420 __PACKAGE__->register_method(
421         api_name        => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
422         api_level       => 1,
423         argc            => 1,
424         stream          => 1,
425         method          => 'ranged_actor_stat_cat_all',
426 );
427
428 __PACKAGE__->register_method(
429         api_name        => 'open-ils.storage.ranged.actor.stat_cat.all',
430         api_level       => 1,
431         argc            => 1,
432         stream          => 1,
433         method          => 'ranged_actor_stat_cat_all',
434 );
435
436 #XXX Fix stored proc calls
437 sub ranged_actor_stat_cat_entry {
438         my $self = shift;
439         my $client = shift;
440         my $ou = ''.shift();
441         my $sc = ''.shift();
442         
443         return undef unless ($ou);
444         my $s_table = actor::stat_cat_entry->table;
445
446         my $select = <<"        SQL";
447                 SELECT  s.*
448                   FROM  $s_table s
449                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
450                   WHERE stat_cat = ?
451                   ORDER BY name
452         SQL
453
454         my $sth = actor::stat_cat->db_Main->prepare_cached($select);
455         $sth->execute($ou,$sc);
456
457         for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
458                 $client->respond( $sce->to_fieldmapper );
459         }
460
461         return undef;
462 }
463 __PACKAGE__->register_method(
464         api_name        => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
465         api_level       => 1,
466         stream          => 1,
467         method          => 'ranged_actor_stat_cat_entry',
468 );
469
470
471 1;