1 package OpenILS::Application::Vandelay;
2 use strict; use warnings;
3 use OpenILS::Application;
4 use base qw/OpenILS::Application/;
6 use Unicode::Normalize;
7 use OpenSRF::EX qw/:try/;
9 use OpenSRF::AppSession;
10 use OpenSRF::Utils::SettingsClient;
11 use OpenSRF::Utils::Cache;
13 use OpenILS::Utils::Fieldmapper;
14 use OpenILS::Utils::CStoreEditor qw/:funcs/;
20 use OpenILS::Utils::Fieldmapper;
22 use Time::HiRes qw(time);
24 use OpenSRF::Utils::Logger qw/$logger/;
40 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
44 # --------------------------------------------------------------------------------
47 sub create_bib_queue {
55 my $e = new_editor(authtoken => $auth, xact => 1);
57 return $e->die_event unless $e->checkauth;
58 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE', $owner);
59 $owner ||= $e->requestor->id;
61 my $queue = new Fieldmapper::vandelay::bib_queue();
62 $queue->name( $name );
63 $queue->owner( $owner );
64 $queue->queue_type( $type ) if ($type);
66 my $new_q = $e->create_vandelay_bib_queue( $queue );
67 return $e->die_event unless ($new_q);
72 __PACKAGE__->register_method(
73 api_name => "open-ils.vandelay.bib_queue.create",
74 method => "create_bib_queue",
80 sub create_auth_queue {
88 my $e = new_editor(authtoken => $auth, xact => 1);
90 return $e->die_event unless $e->checkauth;
91 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', $owner);
92 $owner ||= $e->requestor->id;
94 my $queue = new Fieldmapper::vandelay::authority_queue();
95 $queue->name( $name );
96 $queue->owner( $owner );
97 $queue->queue_type( $type ) if ($type);
99 my $new_q = $e->create_vandelay_authority_queue( $queue );
100 $e->die_event unless ($new_q);
105 __PACKAGE__->register_method(
106 api_name => "open-ils.vandelay.authority_queue.create",
107 method => "create_auth_queue",
112 sub add_record_to_bib_queue {
120 my $e = new_editor(authtoken => $auth, xact => 1);
122 $queue = $e->retrieve_vandelay_bib_queue($queue);
124 return $e->die_event unless $e->checkauth;
125 return $e->die_event unless
126 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
127 $e->allowed('CREATE_BIB_IMPORT_QUEUE', $queue->owner));
129 my $new_rec = _add_bib_rec($e, $marc, $queue->id, $purpose);
131 return $e->die_event unless ($new_rec);
135 __PACKAGE__->register_method(
136 api_name => "open-ils.vandelay.queued_bib_record.create",
137 method => "add_record_to_bib_queue",
148 my $rec = new Fieldmapper::vandelay::queued_bib_record();
150 $rec->queue( $queue );
151 $rec->purpose( $purpose ) if ($purpose);
153 return $e->create_vandelay_queued_bib_record( $rec );
156 sub add_record_to_authority_queue {
164 my $e = new_editor(authtoken => $auth, xact => 1);
166 $queue = $e->retrieve_vandelay_authority_queue($queue);
168 return $e->die_event unless $e->checkauth;
169 return $e->die_event unless
170 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
171 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', $queue->owner));
173 my $new_rec = _add_auth_rec($e, $marc, $queue->id, $purpose);
175 return $e->die_event unless ($new_rec);
179 __PACKAGE__->register_method(
180 api_name => "open-ils.vandelay.queued_authority_record.create",
181 method => "add_record_to_authority_queue",
192 my $rec = new Fieldmapper::vandelay::queued_authority_record();
194 $rec->queue( $queue );
195 $rec->purpose( $purpose ) if ($purpose);
197 return $e->create_vandelay_queued_authority_record( $rec );
204 my $fingerprint = shift;
207 my $e = new_editor(authtoken => $auth, xact => 1);
209 my $type = ($self->api_name =~ /auth/) ? 'auth' : 'bib';
211 if ($type eq 'bib') {
212 return $e->die_event unless $e->checkauth;
213 return $e->die_event unless
214 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
215 $e->allowed('CREATE_BIB_IMPORT_QUEUE', $queue->owner));
217 return $e->die_event unless $e->checkauth;
218 return $e->die_event unless
219 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
220 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', $queue->owner));
223 my $method = "open-ils.vandelay.queued_${type}_record.create";
224 $method = $self->method_lookup( $method );
226 my $cache = new OpenSRF::Utils::Cache();
228 my $data = $cache->get_cache('vandelay_import_spool_' . $fingerprint);
229 my $purpose = $data->{purpose};
230 $data = decode_base64($data->{marc});
232 $logger->info("vandelay loaded $fingerprint purpose=$purpose and ".length($data)." bytes of data");
235 open $fh, '<', \$data;
237 my $marctype = 'USMARC'; # ?
238 my $batch = new MARC::Batch ( $marctype, $fh );
242 while (my $r = $batch->next) {
243 $logger->info("processing record $count");
245 (my $xml = $r->as_xml_record()) =~ s/\n//sog;
246 $xml =~ s/^<\?xml.+\?\s*>//go;
247 $xml =~ s/>\s+</></go;
248 $xml =~ s/\p{Cc}//go;
249 $xml = entityize($xml);
250 $xml =~ s/[\x00-\x1f]//go;
252 if ($self->{record_type} eq 'bib') {
253 _add_bib_rec( $e, $xml, $queue, $purpose ) or return $e->die_event;
255 _add_auth_rec( $e, $xml, $queue, $purpose ) or return $e->die_event;
259 $client->respond( $count );
262 $logger->warn("Encountered a bad record at Vandelay ingest: ".$error);
269 __PACKAGE__->register_method(
270 api_name => "open-ils.vandelay.bib.process_spool",
271 method => "process_spool",
276 __PACKAGE__->register_method(
277 api_name => "open-ils.vandelay.auth.process_spool",
278 method => "process_spool",
281 record_type => 'auth'