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