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