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 $client->respond( $rec ) if ($rec);
100 last if ($self->api_name !~ /batch/o);
108 my $searches = shift;
110 my $cdbi = $self->{cdbi};
112 $log->debug("Searching $cdbi for { ".join(',', map { "$_ => $$searches{$_}" } keys %$searches).' }',DEBUG);
114 for my $obj ($cdbi->search($searches)) {
115 $client->respond( $obj->to_fieldmapper );
120 sub search_one_field {
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};
130 $like = 1 if ($search_type =~ /like$/o);
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) ] );
145 my $cdbi = $self->{cdbi};
149 my $rec = $cdbi->create($node);
150 $success = $rec->id if ($rec);
163 my $cdbi = $self->{cdbi};
165 return $cdbi->update($node);
173 my $where = 'WHERE ';
175 my $cdbi = $self->{cdbi};
176 my $table = $cdbi->table;
178 my @keys = sort keys %$search;
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} };
187 push @wheres, "$col = ?";
188 push @binds, $$search{$col};
191 $where .= join ' AND ', @wheres;
193 my $delete = "DELETE FROM $table $where";
195 $log->debug("Performing MASS deletion : $delete",DEBUG);
197 my $dbh = $cdbi->db_Main;
200 my $sth = $dbh->prepare($delete);
201 $sth->execute( @binds );
203 $log->debug("MASS Delete succeeded",DEBUG);
205 $log->debug("MASS Delete FAILED : ".shift(),DEBUG);
216 my $cdbi = $self->{cdbi};
220 $success = $cdbi->delete($node);
232 my $cdbi = $self->{cdbi};
233 my $api_name = $self->api_name;
234 (my $single_call_api_name = $api_name) =~ s/batch\.//o;
236 $log->debug("Default $api_name looking up $single_call_api_name...",INTERNAL);
237 my $method = $self->method_lookup($single_call_api_name);
240 while ( my $node = shift(@nodes) ) {
241 my ($res) = $method->run( $node );
242 push(@success, 1) if ($res >= 0);
245 my $insert_total = 0;
246 $insert_total += $_ for (@success);
248 return $insert_total;
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;
260 for my $fmclass ( (Fieldmapper->classes) ) {
261 $log->debug("Generating methods for Fieldmapper class $fmclass", DEBUG);
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;
269 # Create the search method
270 unless ( __PACKAGE__->is_registered( $api_prefix.'.search' ) ) {
271 __PACKAGE__->register_method(
272 api_name => $api_prefix.'.search',
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',
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',
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',
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',
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',
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',
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',
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',
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',
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',
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',