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