calling internal bib instead of auth rec creator on bib queue
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Vandelay.pm
1 package OpenILS::Application::Vandelay;
2 use OpenILS::Application;
3 use base qw/OpenILS::Application/;
4
5 use Unicode::Normalize;
6 use OpenSRF::EX qw/:try/;
7
8 use OpenSRF::AppSession;
9 use OpenSRF::Utils::SettingsClient;
10 use OpenSRF::Utils::Cache;
11
12 use OpenILS::Utils::Fieldmapper;
13 use OpenILS::Utils::CStoreEditor qw/:funcs/;
14
15 use MARC::Batch;
16 use MARC::Record;
17 use MARC::File::XML;
18
19 use OpenILS::Utils::Fieldmapper;
20
21 use Time::HiRes qw(time);
22
23 use OpenSRF::Utils::Logger qw/:level/;
24 my $log = 'OpenSRF::Utils::Logger';
25
26 sub initialize {}
27 sub child_init {}
28
29 sub entityize {
30         my $self = shift;
31         my $stuff = shift;
32         my $form = shift;
33
34         if ($form eq 'D') {
35                 $stuff = NFD($stuff);
36         } else {
37                 $stuff = NFC($stuff);
38         }
39
40         $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
41         return $stuff;
42 }
43
44 # --------------------------------------------------------------------------------
45 # Biblio ingest
46
47 sub create_bib_queue {
48         my $self = shift;
49         my $client = shift;
50         my $auth = shift;
51         my $name = shift;
52         my $owner = shift;
53         my $type = shift;
54
55         my $e = new_editor(authtoken => $auth, xact => 1);
56
57         return $e->die_event unless $e->checkauth;
58         return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE', $owner);
59
60         my $queue = new Fieldmapper::vandelay::bib_queue();
61         $queue->name( $name );
62         $queue->owner( $owner );
63         $queue->queue_type( $type ) if ($type);
64         $queue->queue_purpose( $purpose ) if ($purpose);
65
66         my $new_id = $e->create_vandelay_bib_queue( $queue );
67         $e->die_event unless ($new_id);
68         $e->commit;
69
70         $queue->id($new_id);
71         return $queue;
72 }
73 __PACKAGE__->register_method(  
74         api_name        => "open-ils.vandelay.bib_queue.create",
75         method          => "create_bib_queue",
76         api_level       => 1,
77         argc            => 3,
78 );                      
79
80
81 sub create_auth_queue {
82         my $self = shift;
83         my $client = shift;
84         my $auth = shift;
85         my $name = shift;
86         my $owner = shift;
87         my $type = shift;
88
89         my $e = new_editor(authtoken => $auth, xact => 1);
90
91         return $e->die_event unless $e->checkauth;
92         return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', $owner);
93
94         my $queue = new Fieldmapper::vandelay::authority_queue();
95         $queue->name( $name );
96         $queue->owner( $owner );
97         $queue->queue_type( $type ) if ($type);
98         $queue->queue_purpose( $purpose ) if ($purpose);
99
100         my $new_id = $e->create_vandelay_authority_queue( $queue );
101         $e->die_event unless ($new_id);
102         $e->commit;
103
104         $queue->id($new_id);
105         return $queue;
106 }
107 __PACKAGE__->register_method(  
108         api_name        => "open-ils.vandelay.authority_queue.create",
109         method          => "create_auth_queue",
110         api_level       => 1,
111         argc            => 3,
112 );                      
113
114 sub add_record_to_bib_queue {
115         my $self = shift;
116         my $client = shift;
117         my $auth = shift;
118         my $queue = shift;
119         my $marc = shift;
120         my $purpose = shift;
121
122         my $e = new_editor(authtoken => $auth, xact => 1);
123
124         $queue = $e->retrieve_vandelay_bib_queue($queue);
125
126         return $e->die_event unless $e->checkauth;
127         return $e->die_event unless
128                 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
129                  $e->allowed('CREATE_BIB_IMPORT_QUEUE', $queue->owner));
130
131         my $new_id = _add_bib_rec($e, $marc, $queue->id, $purpose);
132
133         $e->die_event unless ($new_id);
134         $e->commit;
135
136         $rec->id($new_id);
137         return $rec;
138 }
139 __PACKAGE__->register_method(  
140         api_name        => "open-ils.vandelay.queued_bib_record.create",
141         method          => "add_record_to_bib_queue",
142         api_level       => 1,
143         argc            => 3,
144 );                      
145
146 sub _add_bib_rec {
147         my $e = shift;
148         my $marc = shift;
149         my $queue = shift;
150         my $purpose = shift;
151
152         my $rec = new Fieldmapper::vandelay::queued_bib_record();
153         $rec->marc( $marc );
154         $rec->queue( $queue );
155         $rec->purpose( $purpose ) if ($purpose);
156
157         return $e->create_vandelay_queued_bib_record( $rec );
158 }
159
160 sub add_record_to_authority_queue {
161         my $self = shift;
162         my $client = shift;
163         my $auth = shift;
164         my $queue = shift;
165         my $marc = shift;
166         my $purpose = shift;
167
168         my $e = new_editor(authtoken => $auth, xact => 1);
169
170         $queue = $e->retrieve_vandelay_authority_queue($queue);
171
172         return $e->die_event unless $e->checkauth;
173         return $e->die_event unless
174                 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
175                  $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', $queue->owner));
176
177         my $new_id = _add_auth_rec($e, $marc, $queue->id, $purpose);
178
179         $e->die_event unless ($new_id);
180         $e->commit;
181
182         $rec->id($new_id);
183         return $rec;
184 }
185 __PACKAGE__->register_method(
186         api_name        => "open-ils.vandelay.queued_authority_record.create",
187         method          => "add_record_to_authority_queue",
188         api_level       => 1,
189         argc            => 3,
190 );
191
192 sub _add_auth_rec {
193         my $e = shift;
194         my $marc = shift;
195         my $queue = shift;
196
197         my $rec = new Fieldmapper::vandelay::queued_authority_record();
198         $rec->marc( $marc );
199         $rec->queue( $queue );
200         $rec->purpose( $purpose ) if ($purpose);
201
202         return $e->create_vandelay_queued_authority_record( $rec );
203 }
204
205 sub process_spool {
206         my $self = shift;
207         my $client = shift;
208         my $auth = shift;
209         my $fingerprint = shift;
210         my $queue = shift;
211
212         my $e = new_editor(authtoken => $auth, xact => 1);
213
214         if ($self->{record_type} eq 'bib') {
215                 return $e->die_event unless $e->checkauth;
216                 return $e->die_event unless
217                         ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
218                          $e->allowed('CREATE_BIB_IMPORT_QUEUE', $queue->owner));
219         } else {
220                 return $e->die_event unless $e->checkauth;
221                 return $e->die_event unless
222                         ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
223                          $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', $queue->owner));
224         }
225
226         my $method = 'open-ils.vandelay.queued_'.$self->{record_type}.'_record.create';
227         $method = $self->method_lookup( $method );
228
229     my $cache = new OpenSRF::Utils::Cache();
230
231     my $data = $cache->get_cache('vandelay_import_spool_' . $fingerprint);
232         my $purpose = $data->{purpose};
233     $data = decode_base64($data->{marc});
234
235         my $fh = new IO::Scalar \$data;
236
237         my $batch = new MARC::Batch ( $type, $fh );
238         $batch->strict_off;
239
240         my $count = 0;
241         while (my $r = $batch->next) {
242                 try {
243                         (my $xml = $rec->as_xml_record()) =~ s/\n//sog;
244                         $xml =~ s/^<\?xml.+\?\s*>//go;
245                         $xml =~ s/>\s+</></go;
246                         $xml =~ s/\p{Cc}//go;
247                         $xml = $self->entityize($xml);
248                         $xml =~ s/[\x00-\x1f]//go;
249
250                         if ($self->{record_type} eq 'bib') {
251                                 _add_bib_rec( $e, $xml, $queue, $purpose );
252                         } else {
253                                 _add_auth_rec( $e, $xml, $queue, $purpose );
254                         }
255                         $count++;
256                         
257                         $client->respond( $count );
258                 } catch Error with {
259                         my $error = shift;
260                         $log->warn("Encountered a bad record at Vandelay ingest: ".$error);
261                 }
262         }
263
264         $e->commit;
265         return undef;
266 }
267 __PACKAGE__->register_method(  
268         api_name        => "open-ils.vandelay.bib.process_spool",
269         method          => "process_spool",
270         api_level       => 1,
271         argc            => 3,
272         record_type     => 'bib'
273 );                      
274 __PACKAGE__->register_method(  
275         api_name        => "open-ils.vandelay.auth.process_spool",
276         method          => "process_spool",
277         api_level       => 1,
278         argc            => 3,
279         record_type     => 'auth'
280 );                      
281
282
283
284 1;
285