From 6d118ca5aa2d2f8a2fbbfe09b6d0c21ea58a136c Mon Sep 17 00:00:00 2001 From: miker Date: Fri, 11 Mar 2005 13:58:17 +0000 Subject: [PATCH] automatic generation of most "Publish" methods git-svn-id: svn://svn.open-ils.org/ILS/trunk@311 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- .../perlmods/OpenILS/Application/Storage.pm | 9 +- .../OpenILS/Application/Storage/CDBI.pm | 14 +- .../OpenILS/Application/Storage/Publisher.pm | 122 +++++++++++++++++- .../src/perlmods/OpenILS/Utils/Fieldmapper.pm | 9 +- OpenSRF/src/perlmods/OpenSRF/Application.pm | 19 ++- .../OpenSRF/DomainObject/oilsMessage.pm | 1 - 6 files changed, 159 insertions(+), 15 deletions(-) diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Storage.pm b/Open-ILS/src/perlmods/OpenILS/Application/Storage.pm index 73f3ce97d3..9446909a13 100644 --- a/Open-ILS/src/perlmods/OpenILS/Application/Storage.pm +++ b/Open-ILS/src/perlmods/OpenILS/Application/Storage.pm @@ -40,12 +40,15 @@ sub initialize { sub child_init { - my $conf = OpenSRF::Utils::SettingsClient->new; + $log->debug('Running child_init for ' . __PACKAGE__ . '...', DEBUG); - OpenSRF::Application->method_lookup('crappola'); + my $conf = OpenSRF::Utils::SettingsClient->new; - $log->debug('Running child_init for ' . __PACKAGE__ . '...', DEBUG); + # XXX -- this died... router down? + #$log->debug('Prepopulating method_lookup cache', DEBUG); + #OpenSRF::Application->method_lookup('crappola'); + $log->debug('Calling the Driver child_init', DEBUG); OpenILS::Application::Storage::CDBI->child_init( $conf->config_value( apps => 'open-ils.storage' => app_settings => databases => 'database') ); diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Storage/CDBI.pm b/Open-ILS/src/perlmods/OpenILS/Application/Storage/CDBI.pm index 626940eccb..e5b791444d 100644 --- a/Open-ILS/src/perlmods/OpenILS/Application/Storage/CDBI.pm +++ b/Open-ILS/src/perlmods/OpenILS/Application/Storage/CDBI.pm @@ -2,10 +2,6 @@ package OpenILS::Application::Storage::CDBI; use base qw/Class::DBI/; use Class::DBI; - -our $VERSION = 1; - - use OpenILS::Application::Storage::CDBI::config; use OpenILS::Application::Storage::CDBI::actor; use OpenILS::Application::Storage::CDBI::asset; @@ -15,11 +11,13 @@ use OpenILS::Application::Storage::CDBI::metabib; use OpenILS::Utils::Fieldmapper; use OpenSRF::Utils::Logger; +our $VERSION; my $log = 'OpenSRF::Utils::Logger'; sub child_init { my $self = shift; + $log->debug("Creating ImaDBI Querys", DEBUG); __PACKAGE__->set_sql( 'OILSFastSearch', <<" SQL", 'Main'); SELECT %s FROM %s @@ -33,6 +31,7 @@ sub child_init { ORDER BY %s SQL + $log->debug("Calling Driver child_init", DEBUG); $self->SUPER::child_init(@_); } @@ -187,7 +186,8 @@ sub update { } $log->debug("Calling Class::DBI->update on modified object $self", DEBUG); - return $self->SUPER::update; + return $self->SUPER::update if ($self->is_changed); + return 0; } sub modify_from_fieldmapper { @@ -223,7 +223,7 @@ sub modify_from_fieldmapper { if ($class->find_column( 'last_xact_id' ) and $obj->is_changed) { my $xact_id = $obj->current_xact_id; throw Error unless ($xact_id); - $hash{last_xact_id} = $xact_id; + $obj->last_xact_id( $xact_id ); } else { $obj->autoupdate($au) } @@ -234,6 +234,7 @@ sub modify_from_fieldmapper { sub import { + return if ($VERSION); #------------------------------------------------------------------------------- actor::user->has_a( home_ou => 'actor::org_unit' ); #actor::org_unit->has_a( address => 'actor::address' ); @@ -314,6 +315,7 @@ sub import { metabib::keyword_field_entry_source_map->has_a( field_entry => 'metabib::keyword_field_entry' ); metabib::keyword_field_entry_source_map->has_a( source_record => 'biblio::record_entry' ); #------------------------------------------------------------------------------- + $VERSION = 1; } diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm b/Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm index 5737ddc6a9..26f82d07d5 100644 --- a/Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm +++ b/Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm @@ -1,6 +1,126 @@ package OpenILS::Application::Storage::Publisher; +use base qw/OpenILS::Application::Storage/; our $VERSION = 1; -use OpenILS::Application::Storage::Publisher::config; + +use OpenILS::Utils::Fieldmapper; +use OpenILS::Application::Storage::CDBI; + +#use OpenILS::Application::Storage::CDBI::actor; +#use OpenILS::Application::Storage::CDBI::asset; +#use OpenILS::Application::Storage::CDBI::biblio; +#use OpenILS::Application::Storage::CDBI::config; +#use OpenILS::Application::Storage::CDBI::metabib; + use OpenILS::Application::Storage::Publisher::actor; +#use OpenILS::Application::Storage::Publisher::asset; use OpenILS::Application::Storage::Publisher::biblio; +use OpenILS::Application::Storage::Publisher::config; +use OpenILS::Application::Storage::Publisher::metabib; + + +for my $fmclass ( Fieldmapper->classes ) { + (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o; + (my $class = $cdbi) =~ s/::.*//o; + (my $api_class = $cdbi) =~ s/::/./go; + my $registration_class = __PACKAGE__ . "::$class"; + my $api_prefix = 'open-ils.storage.'.$api_class; + + warn "\tfmclass => $fmclass\n\tclass => $class\n\tregclass => $registration_class\n\tprefix => $api_prefix\n\tcdbi => $cdbi\n\n"; + + # Create the create method + unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) { + *{ $registration_class . '::create_node' } = sub { + my $self = shift; + my $client = shift; + my $node = shift; + + my $success; + try { + my $rec = $cdbi->create($node); + $success = $rec->id; + } catch Error with { + $success = 0; + }; + + return $success; + }; + $registration_class->register_method( + api_name => $api_prefix.'.create', + method => 'create_node', + api_level => 1, + ); + } + + # Create the batch create method + unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) { + *{ $registration_class . '::create_node_batch' } = sub { + my $self = shift; + my $client = shift; + my @nodes = @_; + + my $method = $self->method_lookup($api_prefix.'.create'); + + my @success; + while ( my $node = shift(@nodes) ) { + my ($res) = $method->run( $node ); + push(@success, 1) if ($res >= 0); + } + + my $insert_total = 0; + $insert_total += $_ for (@success); + + return $insert_total; + }; + $registration_class->register_method( + api_name => $api_prefix.'.batch.create', + method => 'create_node_batch', + api_level => 1, + ); + } + + # Create the update method + unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) { + *{ $registration_class . '::update_node' } = sub { + my $self = shift; + my $client = shift; + my $node = shift; + + return $cdbi->update($node); + }; + $registration_class->register_method( + api_name => $api_prefix.'.update', + method => 'update_node', + api_level => 1, + ); + } + + # Create the batch update method + unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) { + *{ $registration_class . '::update_node_batch' } = sub { + my $self = shift; + my $client = shift; + my @nodes = @_; + + my $method = $self->method_lookup($api_prefix.'.update'); + + my @success; + while ( my $node = shift(@nodes) ) { + my ($res) = $method->run( $node ); + push(@success, $res) if ($res >= 0); + } + + my $insert_total = 0; + $insert_total += $_ for (@success); + + return $insert_total; + }; + $registration_class->register_method( + api_name => $api_prefix.'.batch.update', + method => 'update_node_batch', + api_level => 1, + ); + } + +} +1; diff --git a/Open-ILS/src/perlmods/OpenILS/Utils/Fieldmapper.pm b/Open-ILS/src/perlmods/OpenILS/Utils/Fieldmapper.pm index 8b519a2329..a67fd8815f 100644 --- a/Open-ILS/src/perlmods/OpenILS/Utils/Fieldmapper.pm +++ b/Open-ILS/src/perlmods/OpenILS/Utils/Fieldmapper.pm @@ -7,7 +7,7 @@ use OpenILS::Application::Storage::CDBI::biblio; use OpenILS::Application::Storage::CDBI::config; use OpenILS::Application::Storage::CDBI::metabib; -use vars qw/$fieldmap @class_name_list $VERSION/; +use vars qw/$fieldmap $VERSION/; _init(); @@ -19,6 +19,11 @@ _init(); # ... adjusted for your CVS sandbox, of course. # +sub classes { + return () unless (defined $fieldmap); + return keys %$fieldmap; +} + sub _init { return if (defined $fieldmap); @@ -51,8 +56,6 @@ sub _init { use base 'Fieldmapper'; PERL - push @class_name_list, $pkg; - $$fieldmapp{$pkg}{cdbi} = $cdbi; my $pos = 0; diff --git a/OpenSRF/src/perlmods/OpenSRF/Application.pm b/OpenSRF/src/perlmods/OpenSRF/Application.pm index b79c785948..ca3ece84be 100644 --- a/OpenSRF/src/perlmods/OpenSRF/Application.pm +++ b/OpenSRF/src/perlmods/OpenSRF/Application.pm @@ -22,11 +22,21 @@ $log = 'OpenSRF::Utils::Logger'; our $in_request = 0; our @pending_requests; +sub package { + my $self = shift; + return $self->{api_name}; +} + sub api_name { my $self = shift; return $self->{api_name}; } +sub api_level { + my $self = shift; + return $self->{api_name}; +} + sub server_class { my $class = shift; if($class) { @@ -207,6 +217,13 @@ sub handler { return 1; } +sub is_registered { + my $self = shift; + my $api_name = shift; + my $api_level = shift || 1; + return exists($_METHODS[$api_level]{$api_name}); +} + sub register_method { my $self = shift; my $app = ref($self) || $self; @@ -309,7 +326,7 @@ sub method_lookup { my $no_remote = shift || 0; # this instead of " || 1;" above to allow api_level 0 - $proto = 1 unless (defined $proto); + $proto = $self->api_level unless (defined $proto); my $class = ref($self) || $self; diff --git a/OpenSRF/src/perlmods/OpenSRF/DomainObject/oilsMessage.pm b/OpenSRF/src/perlmods/OpenSRF/DomainObject/oilsMessage.pm index c6c7af5989..b33d5a04e8 100644 --- a/OpenSRF/src/perlmods/OpenSRF/DomainObject/oilsMessage.pm +++ b/OpenSRF/src/perlmods/OpenSRF/DomainObject/oilsMessage.pm @@ -175,7 +175,6 @@ sub handler { } else { $log->debug("Request was handled internally", DEBUG); } - $log->debug("Returning to ".join('::',(caller)[0,3]), DEBUG); return 1; -- 2.43.2