]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/actor.pm
ranged stat_cat search methods... YAY, I got to write some code!
[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 =comment not gonna use it...
34
35 sub fleshed_search {
36         my $self = shift;
37         my $client = shift;
38         my $searches = shift;
39
40         return undef unless (defined $searches);
41
42         for my $usr ( actor::user->search( $searches ) ) {
43                 next unless $usr;
44                 $client->respond( flesh_user( $usr ) );
45         }
46         return undef;
47 }
48 __PACKAGE__->register_method(
49         api_name        => 'open-ils.storage.fleshed.actor.user.search',
50         api_level       => 1,
51         method          => 'fleshed_search',
52         stream          => 1,
53         cachable        => 1,
54 );
55
56 sub fleshed_search_like {
57         my $self = shift;
58         my $client = shift;
59         my $searches = shift;
60
61         return undef unless (defined $searches);
62
63         for my $usr ( actor::user->search_like( $searches ) ) {
64                 next unless $usr;
65                 $client->respond( flesh_user( $usr ) );
66         }
67         return undef;
68 }
69 __PACKAGE__->register_method(
70         api_name        => 'open-ils.storage.fleshed.actor.user.search_like',
71         api_level       => 1,
72         method          => 'user_by_barcode',
73         stream          => 1,
74         cachable        => 1,
75 );
76
77 sub retrieve_fleshed_user {
78         my $self = shift;
79         my $client = shift;
80         my @ids = shift;
81
82         return undef unless @ids;
83
84         @ids = ($ids[0]) unless ($self->api_name =~ /batch/o); 
85
86         $client->respond( flesh_user( actor::user->retrieve( $_ ) ) ) for ( @ids );
87
88         return undef;
89 }
90 __PACKAGE__->register_method(
91         api_name        => 'open-ils.storage.fleshed.actor.user.retrieve',
92         api_level       => 1,
93         method          => 'retrieve_fleshed_user',
94         cachable        => 1,
95 );
96 __PACKAGE__->register_method(
97         api_name        => 'open-ils.storage.fleshed.actor.user.batch.retrieve',
98         api_level       => 1,
99         method          => 'retrieve_fleshed_user',
100         stream          => 1,
101         cachable        => 1,
102 );
103
104 sub flesh_user {
105         my $usr = shift;
106
107
108         my $standing = $usr->standing;
109         my $profile = $usr->profile;
110         my $ident_type = $usr->ident_type;
111                 
112         my $maddress = $usr->mailing_address;
113         my $baddress = $usr->billing_address;
114         my $card = $usr->card;
115
116         my @addresses = $usr->addresses;
117         my @cards = $usr->cards;
118
119         my $usr_fm = $usr->to_fieldmapper;
120         $usr_fm->standing( $standing->to_fieldmapper );
121         $usr_fm->profile( $profile->to_fieldmapper );
122         $usr_fm->ident_type( $ident_type->to_fieldmapper );
123
124         $usr_fm->card( $card->to_fieldmapper );
125         $usr_fm->mailing_address( $maddress->to_fieldmapper ) if ($maddress);
126         $usr_fm->billing_address( $baddress->to_fieldmapper ) if ($baddress);
127
128         $usr_fm->cards( [ map { $_->to_fieldmapper } @cards ] );
129         $usr_fm->addresses( [ map { $_->to_fieldmapper } @addresses ] );
130
131         return $usr_fm;
132 }
133
134 =cut
135
136 sub org_unit_list {
137         my $self = shift;
138         my $client = shift;
139
140         my $select =<<" SQL";
141         SELECT  *
142           FROM  actor.org_unit
143           ORDER BY CASE WHEN parent_ou IS NULL THEN 0 ELSE 1 END, name;
144         SQL
145
146         my $sth = actor::org_unit->db_Main->prepare_cached($select);
147         $sth->execute;
148
149         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
150
151         return undef;
152 }
153 __PACKAGE__->register_method(
154         api_name        => 'open-ils.storage.direct.actor.org_unit.retrieve.all',
155         api_level       => 1,
156         stream          => 1,
157         method          => 'org_unit_list',
158 );
159
160 sub org_unit_type_list {
161         my $self = shift;
162         my $client = shift;
163
164         my $select =<<" SQL";
165         SELECT  *
166           FROM  actor.org_unit_type
167           ORDER BY depth, name;
168         SQL
169
170         my $sth = actor::org_unit_type->db_Main->prepare_cached($select);
171         $sth->execute;
172
173         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit_type->construct($_) } $sth->fetchall_hash );
174
175         return undef;
176 }
177 __PACKAGE__->register_method(
178         api_name        => 'open-ils.storage.direct.actor.org_unit_type.retrieve.all',
179         api_level       => 1,
180         stream          => 1,
181         method          => 'org_unit_type_list',
182 );
183
184 sub org_unit_full_path {
185         my $self = shift;
186         my $client = shift;
187         my $id = shift;
188
189         return undef unless ($id);
190
191         my $func = 'actor.org_unit_full_path(?)';
192
193         my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
194         $sth->execute(''.$id);
195
196         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
197
198         return undef;
199 }
200 __PACKAGE__->register_method(
201         api_name        => 'open-ils.storage.actor.org_unit.full_path',
202         api_level       => 1,
203         stream          => 1,
204         method          => 'org_unit_full_path',
205 );
206
207 sub org_unit_ancestors {
208         my $self = shift;
209         my $client = shift;
210         my $id = shift;
211
212         return undef unless ($id);
213
214         my $func = 'actor.org_unit_ancestors(?)';
215
216         my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
217         $sth->execute(''.$id);
218
219         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
220
221         return undef;
222 }
223 __PACKAGE__->register_method(
224         api_name        => 'open-ils.storage.actor.org_unit.ancestors',
225         api_level       => 1,
226         stream          => 1,
227         method          => 'org_unit_ancestors',
228 );
229
230 sub org_unit_descendants {
231         my $self = shift;
232         my $client = shift;
233         my $id = shift;
234         my $depth = shift;
235
236         return undef unless ($id);
237
238         my $func = 'actor.org_unit_descendants(?)';
239         if (defined $depth) {
240                 $func = 'actor.org_unit_descendants(?,?)';
241         }
242
243         my $sth = actor::org_unit->db_Main->prepare_cached("SELECT * FROM $func");
244         $sth->execute(''.$id, ''.$depth) if (defined $depth);
245         $sth->execute(''.$id) unless (defined $depth);
246
247         $client->respond( $_->to_fieldmapper ) for ( map { actor::org_unit->construct($_) } $sth->fetchall_hash );
248
249         return undef;
250 }
251 __PACKAGE__->register_method(
252         api_name        => 'open-ils.storage.actor.org_unit.descendants',
253         api_level       => 1,
254         stream          => 1,
255         method          => 'org_unit_descendants',
256 );
257
258 sub profile_all {
259         my $self = shift;
260         my $client = shift;
261
262         for my $rec ( actor::profile->retrieve_all ) {
263                 $client->respond( $rec->to_fieldmapper );
264         }
265
266         return undef;
267 }
268 __PACKAGE__->register_method(
269         method          => 'profile_all',
270         api_name        => 'open-ils.storage.direct.actor.profile.retrieve.all',
271         argc            => 0,
272         stream          => 1,
273 );
274
275 #XXX Fix stored proc calls
276 sub ranged_actor_stat_cat {
277         my $self = shift;
278         my $client = shift;
279         my $ou = ''.shift();
280         
281         return undef unless ($ou);
282         my $s_table = actor::stat_cat->table;
283
284         my $select = <<"        SQL";
285                 SELECT  s.*
286                   FROM  $s_table s
287                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
288                   ORDER BY name
289         SQL
290
291         $fleshed = 0;
292         $fleshed = 1 if ($self->api_name =~ /fleshed/o);
293
294         my $sth = actor::stat_cat->db_Main->prepare_cached($select);
295         $sth->execute($ou);
296
297         for my $sc ( map { actor::stat_cat->construct($_) } $sth->fetchall_hash ) {
298                 my $sc_fm = $sc->to_fieldmapper;
299                 $sc_fm->entries(
300                         [ $self->method_lookup( 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
301                 ) if ($fleshed);
302                 $client->respond( $sc_fm );
303         }
304
305         return undef;
306 }
307 __PACKAGE__->register_method(
308         api_name        => 'open-ils.storage.ranged.fleshed.actor.stat_cat.all',
309         api_level       => 1,
310         stream          => 1,
311         method          => 'ranged_actor_stat_cat',
312 );
313
314 __PACKAGE__->register_method(
315         api_name        => 'open-ils.storage.ranged.actor.stat_cat.all',
316         api_level       => 1,
317         stream          => 1,
318         method          => 'ranged_actor_stat_cat',
319 );
320
321 #XXX Fix stored proc calls
322 sub ranged_actor_stat_cat_entry {
323         my $self = shift;
324         my $client = shift;
325         my $ou = ''.shift();
326         my $sc = ''.shift();
327         
328         return undef unless ($ou);
329         my $s_table = actor::stat_cat_entry->table;
330
331         my $select = <<"        SQL";
332                 SELECT  s.*
333                   FROM  $s_table s
334                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
335                   WHERE stat_cat = ?
336                   ORDER BY name
337         SQL
338
339         my $sth = actor::stat_cat->db_Main->prepare_cached($select);
340         $sth->execute($ou,$sc);
341
342         for my $sce ( map { actor::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
343                 $client->respond( $sce->to_fieldmapper );
344         }
345
346         return undef;
347 }
348 __PACKAGE__->register_method(
349         api_name        => 'open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat',
350         api_level       => 1,
351         stream          => 1,
352         method          => 'ranged_actor_stat_cat_entry',
353 );
354
355
356 1;