]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/biblio.pm
faster version of nodeset.retrieve
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Publisher / biblio.pm
1 package OpenILS::Application::Storage::Publisher::biblio;
2 use base qw/OpenILS::Application::Storage/;
3 use OpenSRF::EX qw/:try/;
4 use OpenILS::Application::Storage::CDBI::biblio;
5
6 sub create_record_node {
7         my $self = shift;
8         my $client = shift;
9         my $node = shift;;
10
11         try {
12                 my $n = biblio::record_node->create($node);
13                 $client->respond( $n->id );
14         } catch Error with {
15                 $client->respond( 0 );
16         };
17
18         return undef;
19 }
20 __PACKAGE__->register_method(
21         method          => 'create_record_node',
22         api_name        => 'open-ils.storage.biblio.record_node.create',
23         api_level       => 1,
24         argc            => 1,
25 );
26
27 sub update_record_node {
28         my $self = shift;
29         my $client = shift;
30         my $node = shift;;
31
32         my $n = biblio::record_node->retrieve("$$node{id}");
33         return undef unless ($n);
34
35         for my $field ( keys %$node ) {
36                 $n->$field( $$node{$field} );
37         }
38
39         try {
40                 $n->update;
41                 $client->respond( $n->id );
42         } catch Error with {
43                 $client->respond( 0 );
44         };
45
46         return undef;
47 }
48 __PACKAGE__->register_method(
49         method          => 'update_record_node',
50         api_name        => 'open-ils.storage.biblio.record_node.update',
51         api_level       => 1,
52         argc            => 1,
53 );
54
55
56 sub create_record_nodeset {
57         my $self = shift;
58         my $client = shift;
59
60         # COPY version... not working yet
61         if (0) {
62         
63                 my $dbh = biblio::record_node->db_Main;
64                 my @cols = grep { $_ ne biblio::record_node->primary } biblio::record_node->columns('All');
65
66                 $dbh->do('COPY '. biblio::record_node->table .' ('.join(',',@cols).')'.' FROM STDIN');
67
68                 while ( my $node = shift(@_) ) {
69                         my @parts;
70                         for my $col (@cols) {
71                                 my $part;
72                                 if ($part = $node->{$col}) {
73                                         push @parts, $dbh->quote($part);
74                                 } else {
75                                         push @parts, '\N';
76                                 }
77                         }
78                         return 0 unless ($dbh->func(join("\t", map {s/^'(.*)'$/$1/o} @parts)."\n", 'putline'));
79                 }
80                 $dbh->func('\.', 'putline');
81                 return 1;
82         } else {
83                 # INSERT version, works but slow
84
85                 my $method = $self->method_lookup('open-ils.storage.biblio.record_node.create');
86
87                 my @ids;
88                 my $total = scalar(@ids);
89                 my @success;
90
91                 while ( my $node = shift(@_) ) {
92                         my ($res) = $method->run( $node );
93                         push @success, $res if ($res);
94                 }
95         
96                 if ($total == scalar(@success)) {
97                         return 1;
98                 } else {
99                         return 0;
100                 }
101         }
102 }
103 __PACKAGE__->register_method(
104         method          => 'create_record_nodeset',
105         api_name        => 'open-ils.storage.biblio.record_node.batch.create',
106         api_level       => 1,
107         argc            => 1,
108 );
109
110 sub create_record_entry {
111         my $self = shift;
112         my $client = shift;
113         my $metadata = shift;
114
115         try {
116                 my $rec = biblio::record_entry->create($metadata);
117                 $client->respond( $rec->id );
118         } catch Error with {
119                 $client->respond( 0 );
120         };
121
122         return undef;
123 }
124 __PACKAGE__->register_method(
125         method          => 'create_record_entry',
126         api_name        => 'open-ils.storage.biblio.record_entry.create',
127         api_level       => 1,
128         argc            => 2,
129         note            => <<TEXT,
130
131 Params should be passed as a hash ref! 
132 Required fields are:
133
134         creator
135         editor
136
137 Please at least try to fill in:
138
139         tcn_source
140         tcn_value
141         metarecord
142         source
143         active
144
145 TEXT
146
147 );
148
149 sub update_record_entry {
150         my $self = shift;
151         my $client = shift;
152         my $entry = shift;
153
154         my $rec = biblio::record_entry->retrieve("$$entry{id}");
155         return undef unless ($rec);
156
157         for my $field ( keys %$node ) {
158                 $rec->$field( $$node{$field} );
159         }
160
161         try {
162                 $rec->update;
163                 $client->respond( $rec->id );
164         } catech Error with {
165                 $client->respond( 0 );
166         };
167
168         return undef;
169 }
170 __PACKAGE__->register_method(
171         method          => 'update_record_node',
172         api_name        => 'open-ils.storage.biblio.record_node.update',
173         api_level       => 1,
174         argc            => 1,
175 );
176
177
178
179 sub get_record_entry {
180         my $self = shift;
181         my $client = shift;
182         my @ids = @_;
183
184         for my $id ( @ids ) {
185                 next unless ($id);
186                 
187                 my $rec = biblio::record_entry->retrieve("$id");
188                 $client->respond( $self->_cdbi2Hash( $rec ) ) if ($rec);
189
190                 last if ($self->api_name !~ /list/o);
191         }
192         return undef;
193 }
194 __PACKAGE__->register_method(
195         method          => 'get_record_entry',
196         api_name        => 'open-ils.storage.biblio.record_entry.retrieve',
197         api_level       => 1,
198         argc            => 1,
199 );
200 __PACKAGE__->register_method(
201         method          => 'get_record_entry',
202         api_name        => 'open-ils.storage.biblio.record_entry.retrieve.list',
203         api_level       => 1,
204         argc            => 1,
205         stream          => 1,
206 );
207
208 sub get_record_node {
209         my $self = shift;
210         my $client = shift;
211         my @ids = @_;
212
213         for my $id ( @ids ) {
214                 next unless ($id);
215                 
216                 my $rec = biblio::record_node->retrieve("$id");
217                 $client->respond( $self->_cdbi2Hash( $rec ) ) if ($rec);
218
219                 last if ($self->api_name !~ /list/o);
220         }
221         return undef;
222 }
223 __PACKAGE__->register_method(
224         method          => 'get_record_node',
225         api_name        => 'open-ils.storage.biblio.record_node.retrieve',
226         api_level       => 1,
227         argc            => 1,
228 );
229 __PACKAGE__->register_method(
230         method          => 'get_record_node',
231         api_name        => 'open-ils.storage.biblio.record_node.retrieve.list',
232         api_level       => 1,
233         argc            => 1,
234         stream          => 1,
235 );
236
237 sub get_record_nodeset {
238         my $self = shift;
239         my $client = shift;
240         my @ids = @_;
241
242         my $table = biblio::record_node->table;
243
244         my $sth = biblio::record_node->db_Main->prepare_cached(<<"      SQL");
245                 SELECT * FROM $table WHERE owner_doc = ? ORDER BY intra_doc_id;
246         SQL
247
248
249         for my $id ( @ids ) {
250                 next unless ($id);
251                 
252                 $sth->execute($id);
253
254                 my @rows;
255
256                 while (my $hr = $sth->fetchrow_hashref) {
257                         push @rows, $hr;
258                 }
259                 $client->respond( \@rows );
260
261                 $sth->finish;
262                 
263 #               $client->respond(
264 #                       $self->_cdbi_list2AoH(
265 #                               biblio::record_node->search(
266 #                                       owner_doc => "$id", { order_by => 'intra_doc_id' }
267 #                               )
268 #                       )
269 #               );
270
271                 last if ($self->api_name !~ /list/o);
272         }
273         return undef;
274 }
275 __PACKAGE__->register_method(
276         method          => 'get_record_nodeset',
277         api_name        => 'open-ils.storage.biblio.record_entry.nodeset.retrieve',
278         api_level       => 1,
279         argc            => 1,
280 );
281 __PACKAGE__->register_method(
282         method          => 'get_record_nodeset',
283         api_name        => 'open-ils.storage.biblio.record_entry.nodeset.retrieve.list',
284         api_level       => 1,
285         argc            => 1,
286         stream          => 1,
287 );
288
289
290 1;