]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm
permission stuff
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Publisher.pm
1 package OpenILS::Application::Storage::Publisher;
2 use base qw/OpenILS::Application::Storage/;
3 our $VERSION = 1;
4
5 use Digest::MD5 qw/md5_hex/;
6 use OpenSRF::EX qw/:try/;;
7 use OpenSRF::Utils::Logger qw/:level/;
8 use OpenILS::Utils::Fieldmapper;
9
10 my $log = 'OpenSRF::Utils::Logger';
11
12
13 sub register_method {
14         my $class = shift;
15         my %args = @_;
16         my %dup_args = %args;
17
18         $class = ref($class) || $class;
19
20         $args{package} ||= $class;
21         __PACKAGE__->SUPER::register_method( %args );
22
23         if (exists($dup_args{cachable}) and $dup_args{cachable}) {
24                 (my $name = $dup_args{api_name}) =~ s/^open-ils\.storage/open-ils.storage.cachable/o;
25                 if ($name ne $dup_args{api_name}) {
26                         $dup_args{real_api_name} = $dup_args{api_name};
27                         $dup_args{method} = 'cachable_wrapper';
28                         $dup_args{api_name} = $name;
29                         $dup_args{package} = __PACKAGE__;
30                         __PACKAGE__->SUPER::register_method( %dup_args );
31                 }
32         }
33 }
34
35 sub cachable_wrapper {
36         my $self = shift;
37         my $client = shift;
38         my @args = @_;
39
40         my %cache_args = (
41                 limit           => 100,
42                 offset          => 0,
43                 timeout         => 7200,
44                 cache_page_size => 1000,
45         );
46
47         my @real_args;
48         my $key_string = $self->api_name;
49         for (my $ind = 0; $ind < scalar(@args); $ind++) {
50                 if (    $args[$ind] eq 'limit' ||
51                         $args[$ind] eq 'offset' ||
52                         $args[$ind] eq 'cache_page_size' ||
53                         $args[$ind] eq 'timeout' ) {
54
55                         my $key_ind = $ind;
56                         $ind++;
57                         my $value_ind = $ind;
58                         $cache_args{$args[$key_ind]} = $args[$value_ind];
59                         $log->debug("Cache limiter value for $args[$key_ind] is $args[$value_ind]", INTERNAL);
60                         next;
61                 }
62                 $key_string .= $args[$ind];
63                 $log->debug("Partial cache key value is $args[$ind]", INTERNAL);
64                 push @real_args, $args[$ind];
65         }
66
67         my $cache_page = int($cache_args{offset} / $cache_args{cache_page_size});
68         my $cache_key;
69         {       use bytes;
70                 $cache_key = md5_hex($key_string.$cache_page);
71         }
72
73         $log->debug("Key string for cache lookup is $key_string -> $cache_key", DEBUG);
74         $log->debug("Cache page is $cache_page", DEBUG);
75
76         my $cached_res = OpenSRF::Utils::Cache->new->get_cache( $cache_key );
77         if (defined $cached_res) {
78                 $log->debug("Found ".scalar(@$cached_res)." records in the cache", INFO);
79                 $log->debug("Values from cache: ".join(', ', @$cached_res), INTERNAL);
80                 my $start = int($cache_args{offset} - ($cache_page * $cache_args{cache_page_size}));
81                 my $end = int($start + $cache_args{limit} - 1);
82                 $log->debug("Responding with values from ".$start.' to '.$end,DEBUG);
83                 $client->respond( $_ ) for ( grep { defined } @$cached_res[ $start .. $end ]);
84                 return undef;
85         }
86
87         my $method = $self->method_lookup($self->{real_api_name});
88         my @res = $method->run(@real_args);
89
90
91         $client->respond( $_ ) for ( grep { defined } @res[$cache_args{offset} .. int($cache_args{offset} + $cache_args{limit} - 1)] );
92
93         $log->debug("Saving values from ".int($cache_page * $cache_args{cache_page_size})." to ".
94                 int(($cache_page + 1) * $cache_args{cache_page_size}). "to the cache", INTERNAL);
95         try {
96                 OpenSRF::Utils::Cache->new->put_cache(
97                         $cache_key =>
98                         [@res[int($cache_page * $cache_args{cache_page_size}) .. int(($cache_page + 1) * $cache_args{cache_page_size}) ]] =>
99                         $cache_args{timeout}
100                 );
101         } catch Error with {
102                 my $e = shift;
103                 $log->error("Cache seems to be down, $e");
104         };
105
106         return undef;
107 }
108
109 sub retrieve_node {
110         my $self = shift;
111         my $client = shift;
112         my @ids = @_;
113
114         my $cdbi = $self->{cdbi};
115
116         for my $id ( @ids ) {
117                 next unless ($id);
118
119                 my ($rec) = $cdbi->fast_fieldmapper($id);
120                 if ($self->api_name !~ /batch/o) {
121                         return $rec if ($rec);
122                 }
123                 $client->respond($rec);
124         }
125         return undef;
126 }
127
128 sub search {
129         my $self = shift;
130         my $client = shift;
131         my $searches = shift;
132
133         my $cdbi = $self->{cdbi};
134
135         $log->debug("Searching $cdbi for { ".join(',', map { "$_ => $$searches{$_}" } keys %$searches).' }',DEBUG);
136
137         for my $obj ($cdbi->search($searches)) {
138                 $client->respond( $obj->to_fieldmapper );
139         }
140         return undef;
141 }
142
143 sub search_one_field {
144         my $self = shift;
145         my $client = shift;
146         my @terms = @_;
147
148         (my $search_type = $self->api_name) =~ s/.*\.(search[^.]*).*/$1/o;
149         (my $col = $self->api_name) =~ s/.*\.$search_type\.([^.]+).*/$1/;
150         my $cdbi = $self->{cdbi};
151
152         my $like = 0;
153         $like = 1 if ($search_type =~ /like$/o);
154
155         for my $term (@terms) {
156                 $log->debug("Searching $cdbi for $col using type $search_type, value '$term'",DEBUG);
157                 if (@terms == 1) {
158                         return [ $cdbi->fast_fieldmapper($term,$col,$like) ];
159                 }
160                 $client->respond( [ $cdbi->fast_fieldmapper($term,$col,$like) ] );
161         }
162         return undef;
163 }
164
165
166 sub create_node {
167         my $self = shift;
168         my $client = shift;
169         my $node = shift;
170
171         my $cdbi = $self->{cdbi};
172
173         my $success;
174         try {
175                 my $rec = $cdbi->create($node);
176                 $success = $rec->id if ($rec);
177         } catch Error with {
178                 $success = 0;
179         };
180
181         return $success;
182 }
183
184 sub update_node {
185         my $self = shift;
186         my $client = shift;
187         my $node = shift;
188
189         my $cdbi = $self->{cdbi};
190
191         return $cdbi->update($node);
192 }
193
194 sub mass_delete {
195         my $self = shift;
196         my $client = shift;
197         my $search = shift;
198
199         my $where = 'WHERE ';
200
201         my $cdbi = $self->{cdbi};
202         my $table = $cdbi->table;
203
204         my @keys = sort keys %$search;
205         
206         my @binds;
207         my @wheres;
208         for my $col ( @keys ) {
209                 if (ref($$search{$col}) and ref($$search{$col}) =~ /ARRAY/o) {
210                         push @wheres, "$col IN (" . join(',', map { '?' } @{ $$search{$col} }) . ')';
211                         push @binds, map { "$_" } @{ $$search{$col} };
212                 } else {
213                         push @wheres, "$col = ?";
214                         push @binds, $$search{$col};
215                 }
216         }
217         $where .= join ' AND ', @wheres;
218
219         my $delete = "DELETE FROM $table $where";
220
221         $log->debug("Performing MASS deletion : $delete",DEBUG);
222
223         my $dbh = $cdbi->db_Main;
224         my $success = 1;
225         try {
226                 my $sth = $dbh->prepare($delete);
227                 $sth->execute( @binds );
228                 $sth->finish;
229                 $log->debug("MASS Delete succeeded",DEBUG);
230         } catch Error with {
231                 $log->debug("MASS Delete FAILED : ".shift(),DEBUG);
232                 $success = 0;
233         };
234         return $success;
235 }
236
237 sub delete_node {
238         my $self = shift;
239         my $client = shift;
240         my $node = shift;
241
242         my $cdbi = $self->{cdbi};
243
244         my $success = 1;
245         try {
246                 $success = $cdbi->delete($node);
247         } catch Error with {
248                 $success = 0;
249         };
250         return $success;
251 }
252
253 sub batch_call {
254         my $self = shift;
255         my $client = shift;
256         my @nodes = @_;
257
258         my $cdbi = $self->{cdbi};
259         my $api_name = $self->api_name;
260         (my $single_call_api_name = $api_name) =~ s/batch\.//o;
261
262         $log->debug("Default $api_name looking up $single_call_api_name...",INTERNAL);
263         my $method = $self->method_lookup($single_call_api_name);
264
265         my @success;
266         while ( my $node = shift(@nodes) ) {
267                 my ($res) = $method->run( $node ); 
268                 push(@success, 1) if ($res >= 0);
269         }
270
271         my $insert_total = 0;
272         $insert_total += $_ for (@success);
273
274         return $insert_total;
275 }
276
277 eval '
278 use OpenILS::Application::Storage::Publisher::actor;
279 use OpenILS::Application::Storage::Publisher::action;
280 use OpenILS::Application::Storage::Publisher::asset;
281 use OpenILS::Application::Storage::Publisher::biblio;
282 use OpenILS::Application::Storage::Publisher::config;
283 use OpenILS::Application::Storage::Publisher::metabib;
284 use OpenILS::Application::Storage::Publisher::permission;
285 ';
286
287 for my $fmclass ( (Fieldmapper->classes) ) {
288         $log->debug("Generating methods for Fieldmapper class $fmclass", DEBUG);
289
290         (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o;
291         (my $class = $cdbi) =~ s/::.*//o;
292         (my $api_class = $cdbi) =~ s/::/./go;
293         my $registration_class = __PACKAGE__ . "::$class";
294         my $api_prefix = 'open-ils.storage.direct.'.$api_class;
295
296         # Create the search method
297         unless ( __PACKAGE__->is_registered( $api_prefix.'.search' ) ) {
298                 __PACKAGE__->register_method(
299                         api_name        => $api_prefix.'.search',
300                         method          => 'search',
301                         api_level       => 1,
302                         stream          => 1,
303                         cdbi            => $cdbi,
304                         cachable        => 1,
305                 );
306         }
307
308         # Create the retrieve method
309         unless ( __PACKAGE__->is_registered( $api_prefix.'.retrieve' ) ) {
310                 __PACKAGE__->register_method(
311                         api_name        => $api_prefix.'.retrieve',
312                         method          => 'retrieve_node',
313                         api_level       => 1,
314                         cdbi            => $cdbi,
315                         cachable        => 1,
316                 );
317         }
318
319         # Create the batch retrieve method
320         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.retrieve' ) ) {
321                 __PACKAGE__->register_method(
322                         api_name        => $api_prefix.'.batch.retrieve',
323                         method          => 'retrieve_node',
324                         api_level       => 1,
325                         stream          => 1,
326                         cdbi            => $cdbi,
327                         cachable        => 1,
328                 );
329         }
330
331         unless ($fmclass->is_virtual) {
332                 for my $field ($fmclass->real_fields) {
333                         unless ( __PACKAGE__->is_registered( $api_prefix.'.search.'.$field ) ) {
334                                 __PACKAGE__->register_method(
335                                         api_name        => $api_prefix.'.search.'.$field,
336                                         method          => 'search_one_field',
337                                         api_level       => 1,
338                                         cdbi            => $cdbi,
339                                         cachable        => 1,
340                                 );
341                         }
342                         unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like.'.$field ) ) {
343                                 __PACKAGE__->register_method(
344                                         api_name        => $api_prefix.'.search_like.'.$field,
345                                         method          => 'search_one_field',
346                                         api_level       => 1,
347                                         cdbi            => $cdbi,
348                                         cachable        => 1,
349                                 );
350                         }
351                 }
352         }
353
354
355         # Create the create method
356         unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
357                 __PACKAGE__->register_method(
358                         api_name        => $api_prefix.'.create',
359                         method          => 'create_node',
360                         api_level       => 1,
361                         cdbi            => $cdbi,
362                 );
363         }
364
365         # Create the batch create method
366         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
367                 __PACKAGE__->register_method(
368                         api_name        => $api_prefix.'.batch.create',
369                         method          => 'batch_call',
370                         api_level       => 1,
371                         cdbi            => $cdbi,
372                 );
373         }
374
375         # Create the update method
376         unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
377                 __PACKAGE__->register_method(
378                         api_name        => $api_prefix.'.update',
379                         method          => 'update_node',
380                         api_level       => 1,
381                         cdbi            => $cdbi,
382                 );
383         }
384
385         # Create the batch update method
386         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
387                 __PACKAGE__->register_method(
388                         api_name        => $api_prefix.'.batch.update',
389                         method          => 'batch_call',
390                         api_level       => 1,
391                         cdbi            => $cdbi,
392                 );
393         }
394
395         # Create the delete method
396         unless ( __PACKAGE__->is_registered( $api_prefix.'.delete' ) ) {
397                 __PACKAGE__->register_method(
398                         api_name        => $api_prefix.'.delete',
399                         method          => 'delete_node',
400                         api_level       => 1,
401                         cdbi            => $cdbi,
402                 );
403         }
404
405         # Create the batch delete method
406         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.delete' ) ) {
407                 __PACKAGE__->register_method(
408                         api_name        => $api_prefix.'.batch.delete',
409                         method          => 'batch_call',
410                         api_level       => 1,
411                         cdbi            => $cdbi,
412                 );
413         }
414
415         # Create the search-based mass delete method
416         unless ( __PACKAGE__->is_registered( $api_prefix.'.mass_delete' ) ) {
417                 __PACKAGE__->register_method(
418                         api_name        => $api_prefix.'.mass_delete',
419                         method          => 'mass_delete',
420                         api_level       => 1,
421                         cdbi            => $cdbi,
422                 );
423         }
424
425 }
426
427 1;