automatic generation of most "Publish" methods
authormiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 11 Mar 2005 13:58:17 +0000 (13:58 +0000)
committermiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 11 Mar 2005 13:58:17 +0000 (13:58 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@311 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/perlmods/OpenILS/Application/Storage.pm
Open-ILS/src/perlmods/OpenILS/Application/Storage/CDBI.pm
Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm
Open-ILS/src/perlmods/OpenILS/Utils/Fieldmapper.pm
OpenSRF/src/perlmods/OpenSRF/Application.pm
OpenSRF/src/perlmods/OpenSRF/DomainObject/oilsMessage.pm

index 73f3ce9..9446909 100644 (file)
@@ -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')
        );
index 626940e..e5b7914 100644 (file)
@@ -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;
 }
 
 
index 5737ddc..26f82d0 100644 (file)
@@ -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;
index 8b519a2..a67fd88 100644 (file)
@@ -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;
index b79c785..ca3ece8 100644 (file)
@@ -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;
 
index c6c7af5..b33d5a0 100644 (file)
@@ -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;