]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm
eet leevs
[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::EX qw/:try/;;
6 use OpenSRF::Utils::Logger;
7 my $log = 'OpenSRF::Utils::Logger';
8
9 use OpenILS::Utils::Fieldmapper;
10 use OpenILS::Application::Storage::CDBI;
11
12 #use OpenILS::Application::Storage::CDBI::actor;
13 #use OpenILS::Application::Storage::CDBI::asset;
14 #use OpenILS::Application::Storage::CDBI::biblio;
15 #use OpenILS::Application::Storage::CDBI::config;
16 #use OpenILS::Application::Storage::CDBI::metabib;
17
18 use OpenILS::Application::Storage::Publisher::actor;
19 #use OpenILS::Application::Storage::Publisher::asset;
20 use OpenILS::Application::Storage::Publisher::biblio;
21 use OpenILS::Application::Storage::Publisher::config;
22 use OpenILS::Application::Storage::Publisher::metabib;
23
24 sub retrieve_node {
25         my $self = shift;
26         my $client = shift;
27         my @ids = @_;
28
29         my $cdbi = $self->{cdbi};
30
31         for my $id ( @ids ) {
32                 next unless ($id);
33
34                 my ($rec) = $cdbi->fast_fieldmapper($id);
35                 $client->respond( $rec ) if ($rec);
36
37                 last if ($self->api_name !~ /list/o);
38         }
39         return undef;
40 }
41
42
43 sub create_node {
44         my $self = shift;
45         my $client = shift;
46         my $node = shift;
47
48         my $cdbi = $self->{cdbi};
49
50         my $success;
51         try {
52                 my $rec = $cdbi->create($node);
53                 $success = $rec->id if ($rec);
54         } catch Error with {
55                 $success = 0;
56         };
57
58         return $success;
59 }
60
61 sub update_node {
62         my $self = shift;
63         my $client = shift;
64         my $node = shift;
65
66         my $cdbi = $self->{cdbi};
67
68         return $cdbi->update($node);
69 }
70
71 sub delete_node {
72         my $self = shift;
73         my $client = shift;
74         my $node = shift;
75
76         my $cdbi = $self->{cdbi};
77
78         my $success = 1;
79         try {
80                 $success = $cdbi->delete($node);
81         } catch Error with {
82                 $success = 0;
83         };
84         return $success;
85 }
86
87 sub batch_call {
88         my $self = shift;
89         my $client = shift;
90         my @nodes = @_;
91
92         my $cdbi = $self->{cdbi};
93         my $api_name = $self->api_name;
94         (my $single_call_api_name = $api_name) =~ s/batch\.//o;
95
96         $log->debug("Default $api_name looking up $single_call_api_name...",INTERNAL);
97         my $method = $self->method_lookup($single_call_api_name);
98
99         my @success;
100         while ( my $node = shift(@nodes) ) {
101                 my ($res) = $method->run( $node ); 
102                 push(@success, 1) if ($res >= 0);
103         }
104
105         my $insert_total = 0;
106         $insert_total += $_ for (@success);
107
108         return $insert_total;
109 }
110
111 for my $fmclass ( Fieldmapper->classes ) {
112         (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o;
113         (my $class = $cdbi) =~ s/::.*//o;
114         (my $api_class = $cdbi) =~ s/::/./go;
115         my $registration_class = __PACKAGE__ . "::$class";
116         my $api_prefix = 'open-ils.storage.'.$api_class;
117
118         # Create the retrieve method
119         unless ( __PACKAGE__->is_registered( $api_prefix.'.retrieve' ) ) {
120                 __PACKAGE__->register_method(
121                         api_name        => $api_prefix.'.retrieve',
122                         method          => 'retrieve_node',
123                         api_level       => 1,
124                         cdbi            => $cdbi,
125                 );
126         }
127
128         # Create the batch retrieve method
129         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.retrieve' ) ) {
130                 __PACKAGE__->register_method(
131                         api_name        => $api_prefix.'.batch.retrieve',
132                         method          => 'batch_call',
133                         api_level       => 1,
134                         cdbi            => $cdbi,
135                 );
136         }
137
138         # Create the create method
139         unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
140                 __PACKAGE__->register_method(
141                         api_name        => $api_prefix.'.create',
142                         method          => 'create_node',
143                         api_level       => 1,
144                         cdbi            => $cdbi,
145                 );
146         }
147
148         # Create the batch create method
149         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
150                 __PACKAGE__->register_method(
151                         api_name        => $api_prefix.'.batch.create',
152                         method          => 'batch_call',
153                         api_level       => 1,
154                         cdbi            => $cdbi,
155                 );
156         }
157
158         # Create the update method
159         unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
160                 __PACKAGE__->register_method(
161                         api_name        => $api_prefix.'.update',
162                         method          => 'update_node',
163                         api_level       => 1,
164                         cdbi            => $cdbi,
165                 );
166         }
167
168         # Create the batch update method
169         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
170                 __PACKAGE__->register_method(
171                         api_name        => $api_prefix.'.batch.update',
172                         method          => 'batch_call',
173                         api_level       => 1,
174                         cdbi            => $cdbi,
175                 );
176         }
177
178         # Create the delete method
179         unless ( __PACKAGE__->is_registered( $api_prefix.'.delete' ) ) {
180                 __PACKAGE__->register_method(
181                         api_name        => $api_prefix.'.delete',
182                         method          => 'delete_node',
183                         api_level       => 1,
184                         cdbi            => $cdbi,
185                 );
186         }
187
188         # Create the batch delete method
189         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.delete' ) ) {
190                 __PACKAGE__->register_method(
191                         api_name        => $api_prefix.'.batch.delete',
192                         method          => 'batch_call',
193                         api_level       => 1,
194                         cdbi            => $cdbi,
195                 );
196         }
197
198 }
199
200 1;