adding "merge" and "remote_update" methods. these are propogated to all writable...
authormiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 29 Jul 2005 19:01:20 +0000 (19:01 +0000)
committermiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 29 Jul 2005 19:01:20 +0000 (19:01 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@1578 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/perlmods/OpenILS/Application/Storage/CDBI.pm
Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm

index 7ed0dab..020bf87 100644 (file)
@@ -165,13 +165,46 @@ sub to_fieldmapper {
        return $fm;
 }
 
+sub merge {
+       my $self = shift;
+       my $search = shift;
+       my $arg = shift;
+
+       delete $$arg{$_} for (keys %$search);
+
+       my @objs = $self->search_where($search);
+       if (@objs == 1) {
+               return $objs[0]->update($arg)->id;
+       } elsif (@objs == 0) {
+               return $self->create($arg)->id;
+       } else {
+               throw OpenSRF::EX::WARN ("Non-unique search key for merge.  Perhaps you meant to use remote_update?");
+       }
+}
+
+sub remote_update {
+       my $self = shift;
+       my $search = shift;
+       my $arg = shift;
+
+       delete $$arg{$_} for (keys %$search);
+
+       my @objs = $self->search_where($search);
+       if (@objs == 0) {
+               throw OpenSRF::EX::WARN ("No objects found for remote_update.  Perhaps you meant to use merge?");
+       } else {
+               $_->update($arg) for (@objs);
+               return scalar(@objs);
+       }
+}
+
 sub create {
        my $self = shift;
        my $arg = shift;
 
        $log->debug("\$arg is $arg (".ref($arg).")",DEBUG);
 
-       if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
+       if (ref($arg)) {
                return $self->create_from_fieldmapper($arg,@_);
        }
 
@@ -188,16 +221,24 @@ sub create_from_fieldmapper {
        my $class = ref($obj) || $obj;
        my ($primary) = $class->columns('Primary');
 
-       if (ref $fm) {
-               my %hash = map { defined $fm->$_ ?
-                                       ($_ => $fm->$_) :
-                                       ()
-                               } grep { $_ ne $primary } $class->columns('All');
-
-               if ($class->find_column( 'last_xact_id' )) {
-                       my $xact_id = $class->current_xact_id;
-                       throw Error unless ($xact_id);
-                       $hash{last_xact_id} = $xact_id;
+       if (ref($fm)) {
+               my %hash;
+               if (UNIVERSAL::isa($fm => 'Fieldmapper')) {
+                       %hash = map { defined $fm->$_ ?
+                                               ($_ => $fm->$_) :
+                                               ()
+                                       } grep { $_ ne $primary } $class->columns('All');
+
+                       if ($class->find_column( 'last_xact_id' )) {
+                               my $xact_id = $class->current_xact_id;
+                               throw Error unless ($xact_id);
+                               $hash{last_xact_id} = $xact_id;
+                       }
+               } else {
+                       %hash = map { exists $fm->{$_} ?
+                                               ($_ => $fm->{$_}) :
+                                               ()
+                                       } grep { $_ ne $primary } $class->columns('All');
                }
 
                return $class->create( \%hash, @params );
@@ -237,7 +278,7 @@ sub update {
 
        $log->debug("Attempting to update using $arg", DEBUG) if ($arg);
 
-       if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
+       if (ref($arg)) {
                $self = $self->modify_from_fieldmapper($arg);
                $log->debug("Modification of $self seems to have failed....", DEBUG);
                return undef unless (defined $self);
@@ -264,10 +305,16 @@ sub modify_from_fieldmapper {
                }
        }
 
-       my %hash = map { defined $fm->$_ ?
+       my %hash;
+       
+       if (ref($fm) and UNIVERSAL::isa($fm => 'Fieldmapper')) {
+               %hash = map { defined $fm->$_ ?
                                ($_ => ''.$fm->$_) :
                                ()
                        } $fm->real_fields;
+       } else {
+               %hash = %{$fm};
+       }
 
        my $au = $obj->autoupdate;
        $obj->autoupdate(0);
index 50c10bc..b46e361 100644 (file)
@@ -262,6 +262,38 @@ sub mass_delete {
        return $success;
 }
 
+sub remote_update_node {
+       my $self = shift;
+       my $client = shift;
+       my $node = shift;
+
+       my $cdbi = $self->{cdbi};
+
+       my $success = 1;
+       try {
+               $success = $cdbi->remote_update($node);
+       } catch Error with {
+               $success = 0;
+       };
+       return $success;
+}
+
+sub merge_node {
+       my $self = shift;
+       my $client = shift;
+       my $node = shift;
+
+       my $cdbi = $self->{cdbi};
+
+       my $success = 1;
+       try {
+               $success = $cdbi->merge($node);
+       } catch Error with {
+               $success = 0;
+       };
+       return $success;
+}
+
 sub delete_node {
        my $self = shift;
        my $client = shift;
@@ -548,6 +580,46 @@ for my $fmclass ( (Fieldmapper->classes) ) {
                        );
                }
 
+               # Create the merge method
+               unless ( __PACKAGE__->is_registered( $api_prefix.'.merge' ) ) {
+                       __PACKAGE__->register_method(
+                               api_name        => $api_prefix.'.merge',
+                               method          => 'merge_node',
+                               api_level       => 1,
+                               cdbi            => $cdbi,
+                       );
+               }
+
+               # Create the batch merge method
+               unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.merge' ) ) {
+                       __PACKAGE__->register_method(
+                               api_name        => $api_prefix.'.batch.merge',
+                               method          => 'batch_call',
+                               api_level       => 1,
+                               cdbi            => $cdbi,
+                       );
+               }
+
+               # Create the remote_update method
+               unless ( __PACKAGE__->is_registered( $api_prefix.'.remote_update' ) ) {
+                       __PACKAGE__->register_method(
+                               api_name        => $api_prefix.'.remote_update',
+                               method          => 'remote_update_node',
+                               api_level       => 1,
+                               cdbi            => $cdbi,
+                       );
+               }
+
+               # Create the batch remote_update method
+               unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.remote_update' ) ) {
+                       __PACKAGE__->register_method(
+                               api_name        => $api_prefix.'.batch.remote_update',
+                               method          => 'batch_call',
+                               api_level       => 1,
+                               cdbi            => $cdbi,
+                       );
+               }
+
                # Create the search-based mass delete method
                unless ( __PACKAGE__->is_registered( $api_prefix.'.mass_delete' ) ) {
                        __PACKAGE__->register_method(