]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm
storage server stuff
[working/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 !~ /batch/o);
38         }
39         return undef;
40 }
41
42 sub search {
43         my $self = shift;
44         my $client = shift;
45         my $term = shift;
46
47         (my $search_type = $self->api_name) =~ s/.*\.(search[^.]*).*/$1/o;
48         (my $col = $self->api_name) =~ s/.*\.$search_type\.([^.]+).*/$1/;
49         my $cdbi = $self->{cdbi};
50         $log->debug("Searching $cdbi for $col using type $search_type, value '$term'",DEBUG);
51
52         my $like = 0;
53         $like = 1 if ($search_type =~ /like$/o);
54
55         return [ $cdbi->fast_fieldmapper($term,$col,$like) ];
56 }
57
58
59 sub create_node {
60         my $self = shift;
61         my $client = shift;
62         my $node = shift;
63
64         my $cdbi = $self->{cdbi};
65
66         my $success;
67         try {
68                 my $rec = $cdbi->create($node);
69                 $success = $rec->id if ($rec);
70         } catch Error with {
71                 $success = 0;
72         };
73
74         return $success;
75 }
76
77 sub update_node {
78         my $self = shift;
79         my $client = shift;
80         my $node = shift;
81
82         my $cdbi = $self->{cdbi};
83
84         return $cdbi->update($node);
85 }
86
87 sub delete_node {
88         my $self = shift;
89         my $client = shift;
90         my $node = shift;
91
92         my $cdbi = $self->{cdbi};
93
94         my $success = 1;
95         try {
96                 $success = $cdbi->delete($node);
97         } catch Error with {
98                 $success = 0;
99         };
100         return $success;
101 }
102
103 sub batch_call {
104         my $self = shift;
105         my $client = shift;
106         my @nodes = @_;
107
108         my $cdbi = $self->{cdbi};
109         my $api_name = $self->api_name;
110         (my $single_call_api_name = $api_name) =~ s/batch\.//o;
111
112         $log->debug("Default $api_name looking up $single_call_api_name...",INTERNAL);
113         my $method = $self->method_lookup($single_call_api_name);
114
115         my @success;
116         while ( my $node = shift(@nodes) ) {
117                 my ($res) = $method->run( $node ); 
118                 push(@success, 1) if ($res >= 0);
119         }
120
121         my $insert_total = 0;
122         $insert_total += $_ for (@success);
123
124         return $insert_total;
125 }
126
127 for my $fmclass ( Fieldmapper->classes ) {
128         (my $cdbi = $fmclass) =~ s/^Fieldmapper:://o;
129         (my $class = $cdbi) =~ s/::.*//o;
130         (my $api_class = $cdbi) =~ s/::/./go;
131         my $registration_class = __PACKAGE__ . "::$class";
132         my $api_prefix = 'open-ils.storage.'.$api_class;
133
134         # Create the retrieve method
135         unless ( __PACKAGE__->is_registered( $api_prefix.'.retrieve' ) ) {
136                 __PACKAGE__->register_method(
137                         api_name        => $api_prefix.'.retrieve',
138                         method          => 'retrieve_node',
139                         api_level       => 1,
140                         cdbi            => $cdbi,
141                 );
142         }
143
144         # Create the batch retrieve method
145         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.retrieve' ) ) {
146                 __PACKAGE__->register_method(
147                         api_name        => $api_prefix.'.batch.retrieve',
148                         method          => 'retrieve_node',
149                         api_level       => 1,
150                         stream          => 1,
151                         cdbi            => $cdbi,
152                 );
153         }
154
155         for my $field ($fmclass->real_fields) {
156                 unless ( __PACKAGE__->is_registered( $api_prefix.'.search.'.$field ) ) {
157                         __PACKAGE__->register_method(
158                                 api_name        => $api_prefix.'.search.'.$field,
159                                 method          => 'search',
160                                 api_level       => 1,
161                                 cdbi            => $cdbi,
162                         );
163                 }
164                 unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like.'.$field ) ) {
165                         __PACKAGE__->register_method(
166                                 api_name        => $api_prefix.'.search_like.'.$field,
167                                 method          => 'search',
168                                 api_level       => 1,
169                                 cdbi            => $cdbi,
170                         );
171                 }
172         }
173
174
175         # Create the create method
176         unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
177                 __PACKAGE__->register_method(
178                         api_name        => $api_prefix.'.create',
179                         method          => 'create_node',
180                         api_level       => 1,
181                         cdbi            => $cdbi,
182                 );
183         }
184
185         # Create the batch create method
186         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.create' ) ) {
187                 __PACKAGE__->register_method(
188                         api_name        => $api_prefix.'.batch.create',
189                         method          => 'batch_call',
190                         api_level       => 1,
191                         cdbi            => $cdbi,
192                 );
193         }
194
195         # Create the update method
196         unless ( __PACKAGE__->is_registered( $api_prefix.'.update' ) ) {
197                 __PACKAGE__->register_method(
198                         api_name        => $api_prefix.'.update',
199                         method          => 'update_node',
200                         api_level       => 1,
201                         cdbi            => $cdbi,
202                 );
203         }
204
205         # Create the batch update method
206         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.update' ) ) {
207                 __PACKAGE__->register_method(
208                         api_name        => $api_prefix.'.batch.update',
209                         method          => 'batch_call',
210                         api_level       => 1,
211                         cdbi            => $cdbi,
212                 );
213         }
214
215         # Create the delete method
216         unless ( __PACKAGE__->is_registered( $api_prefix.'.delete' ) ) {
217                 __PACKAGE__->register_method(
218                         api_name        => $api_prefix.'.delete',
219                         method          => 'delete_node',
220                         api_level       => 1,
221                         cdbi            => $cdbi,
222                 );
223         }
224
225         # Create the batch delete method
226         unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.delete' ) ) {
227                 __PACKAGE__->register_method(
228                         api_name        => $api_prefix.'.batch.delete',
229                         method          => 'batch_call',
230                         api_level       => 1,
231                         cdbi            => $cdbi,
232                 );
233         }
234
235 }
236
237 1;