1 package OpenILS::Application::Storage::Publisher;
2 use base qw/OpenILS::Application::Storage/;
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;
10 my $log = 'OpenSRF::Utils::Logger';
18 $class = ref($class) || $class;
20 $args{package} ||= $class;
21 __PACKAGE__->SUPER::register_method( %args );
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 );
35 sub cachable_wrapper {
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' ) {
56 $cache_args{$args[$key_ind]} = $args[$value_ind];
57 $log->debug("Cache limiter value for $args[$key_ind] is $args[$value_ind]", INTERNAL);
60 $key_string .= $args[$ind];
61 $log->debug("Partial cache key value is $args[$ind]", INTERNAL);
62 push @real_args, $args[$ind];
65 my $cache_key = md5_hex($key_string);
66 $log->debug("Key string for cache lookup is $key_string -> $cache_key", DEBUG);
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)] );
76 my $method = $self->method_lookup($self->{real_api_name});
77 my @res = $method->run(@real_args);
80 $client->respond( $_ ) for ( grep { defined } @res[$cache_args{offset} .. int($cache_args{offset} + $cache_args{limit} - 1)] );
82 OpenSRF::Utils::Cache->new->put_cache( $cache_key => \@res => $cache_args{timeout});
92 my $cdbi = $self->{cdbi};
97 my ($rec) = $cdbi->fast_fieldmapper($id);
98 if ($self->api_name !~ /batch/o) {
99 return $rec if ($rec);
101 $client->respond($rec);
109 my $searches = shift;
111 my $cdbi = $self->{cdbi};
113 $log->debug("Searching $cdbi for { ".join(',', map { "$_ => $$searches{$_}" } keys %$searches).' }',DEBUG);
115 for my $obj ($cdbi->search($searches)) {
116 $client->respond( $obj->to_fieldmapper );
121 sub search_one_field {
126 (my $search_type = $self->api_name) =~ s/.*\.(search[^.]*).*/$1/o;
127 (my $col = $self->api_name) =~ s/.*\.$search_type\.([^.]+).*/$1/;
128 my $cdbi = $self->{cdbi};
131 $like = 1 if ($search_type =~ /like$/o);
133 for my $term (@terms) {
134 $log->debug("Searching $cdbi for $col using type $search_type, value '$term'",DEBUG);
136 return [ $cdbi->fast_fieldmapper($term,$col,$like) ];
138 $client->respond( [ $cdbi->fast_fieldmapper($term,$col,$like) ] );
149 my $cdbi = $self->{cdbi};
153 my $rec = $cdbi->create($node);
154 $success = $rec->id if ($rec);
167 my $cdbi = $self->{cdbi};
169 return $cdbi->update($node);
177 my $where = 'WHERE ';
179 my $cdbi = $self->{cdbi};
180 my $table = $cdbi->table;
182 my @keys = sort keys %$search;
186 for my $col ( @keys ) {
187 if (ref($$search{$col}) and ref($$search{$col}) =~ /ARRAY/o) {
188 push @wheres, "$col IN (" . join(',', map { '?' } @{ $$search{$col} }) . ')';
189 push @binds, map { "$_" } @{ $$search{$col} };
191 push @wheres, "$col = ?";
192 push @binds, $$search{$col};
195 $where .= join ' AND ', @wheres;
197 my $delete = "DELETE FROM $table $where";
199 $log->debug("Performing MASS deletion : $delete",DEBUG);
201 my $dbh = $cdbi->db_Main;
204 my $sth = $dbh->prepare($delete);
205 $sth->execute( @binds );
207 $log->debug("MASS Delete succeeded",DEBUG);
209 $log->debug("MASS Delete FAILED : ".shift(),DEBUG);
220 my $cdbi = $self->{cdbi};
224 $success = $cdbi->delete($node);
236 my $cdbi = $self->{cdbi};
237 my $api_name = $self->api_name;
238 (my $single_call_api_name = $api_name) =~ s/batch\.//o;
240 $log->debug("Default $api_name looking up $single_call_api_name...",INTERNAL);
241 my $method = $self->method_lookup($single_call_api_name);
244 while ( my $node = shift(@nodes) ) {
245 my ($res) = $method->run( $node );
246 push(@success, 1) if ($res >= 0);
249 my $insert_total = 0;
250 $insert_total += $_ for (@success);
252 return $insert_total;
256 use OpenILS::Application::Storage::Publisher::actor;
257 use OpenILS::Application::Storage::Publisher::action;
258 use OpenILS::Application::Storage::Publisher::asset;
259 use OpenILS::Application::Storage::Publisher::biblio;
260 use OpenILS::Application::Storage::Publisher::config;
261 use OpenILS::Application::Storage::Publisher::metabib;
264 for my $fmclass ( (Fieldmapper->classes) ) {
265 $log->debug("Generating methods for Fieldmapper class $fmclass", DEBUG);
267 (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o;
268 (my $class = $cdbi) =~ s/::.*//o;
269 (my $api_class = $cdbi) =~ s/::/./go;
270 my $registration_class = __PACKAGE__ . "::$class";
271 my $api_prefix = 'open-ils.storage.direct.'.$api_class;
273 # Create the search method
274 unless ( __PACKAGE__->is_registered( $api_prefix.'.search' ) ) {
275 __PACKAGE__->register_method(
276 api_name => $api_prefix.'.search',
285 # Create the retrieve method
286 unless ( __PACKAGE__->is_registered( $api_prefix.'.retrieve' ) ) {
287 __PACKAGE__->register_method(
288 api_name => $api_prefix.'.retrieve',
289 method => 'retrieve_node',
296 # Create the batch retrieve method
297 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.retrieve' ) ) {
298 __PACKAGE__->register_method(
299 api_name => $api_prefix.'.batch.retrieve',
300 method => 'retrieve_node',
308 unless ($fmclass->is_virtual) {
309 for my $field ($fmclass->real_fields) {
310 unless ( __PACKAGE__->is_registered( $api_prefix.'.search.'.$field ) ) {
311 __PACKAGE__->register_method(
312 api_name => $api_prefix.'.search.'.$field,
313 method => 'search_one_field',
319 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like.'.$field ) ) {
320 __PACKAGE__->register_method(
321 api_name => $api_prefix.'.search_like.'.$field,
322 method => 'search_one_field',
332 # Create the create method
333 unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
334 __PACKAGE__->register_method(
335 api_name => $api_prefix.'.create',
336 method => 'create_node',
342 # Create the batch create method
343 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
344 __PACKAGE__->register_method(
345 api_name => $api_prefix.'.batch.create',
346 method => 'batch_call',
352 # Create the update method
353 unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
354 __PACKAGE__->register_method(
355 api_name => $api_prefix.'.update',
356 method => 'update_node',
362 # Create the batch update method
363 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
364 __PACKAGE__->register_method(
365 api_name => $api_prefix.'.batch.update',
366 method => 'batch_call',
372 # Create the delete method
373 unless ( __PACKAGE__->is_registered( $api_prefix.'.delete' ) ) {
374 __PACKAGE__->register_method(
375 api_name => $api_prefix.'.delete',
376 method => 'delete_node',
382 # Create the batch delete method
383 unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.delete' ) ) {
384 __PACKAGE__->register_method(
385 api_name => $api_prefix.'.batch.delete',
386 method => 'batch_call',
392 # Create the search-based mass delete method
393 unless ( __PACKAGE__->is_registered( $api_prefix.'.mass_delete' ) ) {
394 __PACKAGE__->register_method(
395 api_name => $api_prefix.'.mass_delete',
396 method => 'mass_delete',