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