]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm
fat fingers make for silly bugs...
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Publisher.pm
1 package OpenILS::Application::Storage::Publisher;
2 use base qw/OpenILS::Application::Storage/;
3 our $VERSION = 1;
4
5 use OpenSRF::Utils::Logger;
6 my $log = 'OpenSRF::Utils::Logger';
7
8 use OpenILS::Utils::Fieldmapper;
9 use OpenILS::Application::Storage::CDBI;
10
11 #use OpenILS::Application::Storage::CDBI::actor;
12 #use OpenILS::Application::Storage::CDBI::asset;
13 #use OpenILS::Application::Storage::CDBI::biblio;
14 #use OpenILS::Application::Storage::CDBI::config;
15 #use OpenILS::Application::Storage::CDBI::metabib;
16
17 use OpenILS::Application::Storage::Publisher::actor;
18 #use OpenILS::Application::Storage::Publisher::asset;
19 use OpenILS::Application::Storage::Publisher::biblio;
20 use OpenILS::Application::Storage::Publisher::config;
21 use OpenILS::Application::Storage::Publisher::metabib;
22
23 sub create_node {
24         my $self = shift;
25         my $client = shift;
26         my $node = shift;
27
28         my $cdbi = $self->{cdbi};
29
30         my $success;
31         try {
32                 my $rec = $cdbi->create($node);
33                 $success = $rec->id;
34         } catch Error with {
35                 $success = 0;
36         };
37
38         return $success;
39 }
40
41 sub update_node {
42         my $self = shift;
43         my $client = shift;
44         my $node = shift;
45
46         my $cdbi = $self->{cdbi};
47
48         return $cdbi->update($node);
49 }
50
51 sub delete_node {
52         my $self = shift;
53         my $client = shift;
54         my $node = shift;
55
56         my $cdbi = $self->{cdbi};
57
58         my $success = 1;
59         try {
60                 $cdbi->delete($node);
61         } catch Error with {
62                 $success = 0;
63         };
64         return $success;
65 }
66
67 sub batch_call {
68         my $self = shift;
69         my $client = shift;
70         my @nodes = @_;
71
72         my $cdbi = $self->{cdbi};
73         my $api_name = $self->api_name;
74         (my $single_call_api_name = $api_name) =~ s/batch\.//o;
75
76         $log->debug("Default $api_name looking up $single_call_api_name...",INTERNAL);
77         my $method = $self->method_lookup($single_call_api_name);
78
79         my @success;
80         while ( my $node = shift(@nodes) ) {
81                 my ($res) = $method->run( $node ); 
82                 push(@success, 1) if ($res >= 0);
83         }
84
85         my $insert_total = 0;
86         $insert_total += $_ for (@success);
87
88         return $insert_total;
89 }
90
91 for my $fmclass ( Fieldmapper->classes ) {
92         (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o;
93         (my $class = $cdbi) =~ s/::.*//o;
94         (my $api_class = $cdbi) =~ s/::/./go;
95         my $registration_class = __PACKAGE__ . "::$class";
96         my $api_prefix = 'open-ils.storage.'.$api_class;
97
98         # Create the create method
99         unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
100                 __PACKAGE__->register_method(
101                         api_name        => $api_prefix.'.create',
102                         method          => 'create_node',
103                         api_level       => 1,
104                         cdbi            => $cdbi,
105                 );
106         }
107
108         # Create the batch create method
109         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
110                 __PACKAGE__->register_method(
111                         api_name        => $api_prefix.'.batch.create',
112                         method          => 'batch_call',
113                         api_level       => 1,
114                         cdbi            => $cdbi,
115                 );
116         }
117
118         # Create the update method
119         unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
120                 __PACKAGE__->register_method(
121                         api_name        => $api_prefix.'.update',
122                         method          => 'update_node',
123                         api_level       => 1,
124                         cdbi            => $cdbi,
125                 );
126         }
127
128         # Create the batch update method
129         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
130                 __PACKAGE__->register_method(
131                         api_name        => $api_prefix.'.batch.update',
132                         method          => 'batch_call',
133                         api_level       => 1,
134                         cdbi            => $cdbi,
135                 );
136         }
137
138         # Create the delete method
139         unless ( __PACKAGE__->is_registered( $api_prefix.'.delete' ) ) {
140                 __PACKAGE__->register_method(
141                         api_name        => $api_prefix.'.delete',
142                         method          => 'delete_node',
143                         api_level       => 1,
144                         cdbi            => $cdbi,
145                 );
146         }
147
148         # Create the batch delete method
149         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.delete' ) ) {
150                 __PACKAGE__->register_method(
151                         api_name        => $api_prefix.'.batch.delete',
152                         method          => 'batch_call',
153                         api_level       => 1,
154                         cdbi            => $cdbi,
155                 );
156         }
157
158 }
159
160 1;