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/;
41 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
45 # --------------------------------------------------------------------------------
48 sub create_bib_queue {
56 my $e = new_editor(authtoken => $auth, xact => 1);
58 return $e->die_event unless $e->checkauth;
59 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE', $owner);
60 $owner ||= $e->requestor->id;
62 my $queue = new Fieldmapper::vandelay::bib_queue();
63 $queue->name( $name );
64 $queue->owner( $owner );
65 $queue->queue_type( $type ) if ($type);
67 my $new_q = $e->create_vandelay_bib_queue( $queue );
68 return $e->die_event unless ($new_q);
73 __PACKAGE__->register_method(
74 api_name => "open-ils.vandelay.bib_queue.create",
75 method => "create_bib_queue",
81 sub create_auth_queue {
89 my $e = new_editor(authtoken => $auth, xact => 1);
91 return $e->die_event unless $e->checkauth;
92 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', $owner);
93 $owner ||= $e->requestor->id;
95 my $queue = new Fieldmapper::vandelay::authority_queue();
96 $queue->name( $name );
97 $queue->owner( $owner );
98 $queue->queue_type( $type ) if ($type);
100 my $new_q = $e->create_vandelay_authority_queue( $queue );
101 $e->die_event unless ($new_q);
106 __PACKAGE__->register_method(
107 api_name => "open-ils.vandelay.authority_queue.create",
108 method => "create_auth_queue",
113 sub add_record_to_bib_queue {
121 my $e = new_editor(authtoken => $auth, xact => 1);
123 $queue = $e->retrieve_vandelay_bib_queue($queue);
125 return $e->die_event unless $e->checkauth;
126 return $e->die_event unless
127 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
128 $e->allowed('CREATE_BIB_IMPORT_QUEUE', $queue->owner));
130 my $new_rec = _add_bib_rec($e, $marc, $queue->id, $purpose);
132 return $e->die_event unless ($new_rec);
136 __PACKAGE__->register_method(
137 api_name => "open-ils.vandelay.queued_bib_record.create",
138 method => "add_record_to_bib_queue",
149 my $rec = new Fieldmapper::vandelay::queued_bib_record();
151 $rec->queue( $queue );
152 $rec->purpose( $purpose ) if ($purpose);
154 return $e->create_vandelay_queued_bib_record( $rec );
157 sub add_record_to_authority_queue {
165 my $e = new_editor(authtoken => $auth, xact => 1);
167 $queue = $e->retrieve_vandelay_authority_queue($queue);
169 return $e->die_event unless $e->checkauth;
170 return $e->die_event unless
171 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
172 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', $queue->owner));
174 my $new_rec = _add_auth_rec($e, $marc, $queue->id, $purpose);
176 return $e->die_event unless ($new_rec);
180 __PACKAGE__->register_method(
181 api_name => "open-ils.vandelay.queued_authority_record.create",
182 method => "add_record_to_authority_queue",
193 my $rec = new Fieldmapper::vandelay::queued_authority_record();
195 $rec->queue( $queue );
196 $rec->purpose( $purpose ) if ($purpose);
198 return $e->create_vandelay_queued_authority_record( $rec );
205 my $fingerprint = shift;
208 my $e = new_editor(authtoken => $auth, xact => 1);
210 my $type = ($self->api_name =~ /auth/) ? 'auth' : 'bib';
212 if ($type eq 'bib') {
213 return $e->die_event unless $e->checkauth;
214 return $e->die_event unless
215 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
216 $e->allowed('CREATE_BIB_IMPORT_QUEUE', $queue->owner));
218 return $e->die_event unless $e->checkauth;
219 return $e->die_event unless
220 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
221 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', $queue->owner));
224 my $method = "open-ils.vandelay.queued_${type}_record.create";
225 $method = $self->method_lookup( $method );
227 my $cache = new OpenSRF::Utils::Cache();
229 my $data = $cache->get_cache('vandelay_import_spool_' . $fingerprint);
230 my $purpose = $data->{purpose};
231 $data = decode_base64($data->{marc});
233 $logger->info("vandelay loaded $fingerprint purpose=$purpose and ".length($data)." bytes of data");
236 open $fh, '<', \$data;
238 my $marctype = 'USMARC'; # ?
239 my $batch = new MARC::Batch ( $marctype, $fh );
243 while (my $r = $batch->next) {
244 $logger->info("processing record $count");
246 (my $xml = $r->as_xml_record()) =~ s/\n//sog;
247 $xml =~ s/^<\?xml.+\?\s*>//go;
248 $xml =~ s/>\s+</></go;
249 $xml =~ s/\p{Cc}//go;
250 $xml = $self->entityize($xml);
251 $xml =~ s/[\x00-\x1f]//go;
253 if ($self->{record_type} eq 'bib') {
254 _add_bib_rec( $e, $xml, $queue, $purpose ) or return $e->die_event;
256 _add_auth_rec( $e, $xml, $queue, $purpose ) or return $e->die_event;
260 $client->respond( $count );
263 $logger->warn("Encountered a bad record at Vandelay ingest: ".$error);
270 __PACKAGE__->register_method(
271 api_name => "open-ils.vandelay.bib.process_spool",
272 method => "process_spool",
277 __PACKAGE__->register_method(
278 api_name => "open-ils.vandelay.auth.process_spool",
279 method => "process_spool",
282 record_type => 'auth'