1 package OpenILS::Application::Vandelay;
2 use strict; use warnings;
3 use OpenILS::Application;
4 use base qw/OpenILS::Application/;
5 use Unicode::Normalize;
6 use OpenSRF::EX qw/:try/;
7 use OpenSRF::AppSession;
8 use OpenSRF::Utils::SettingsClient;
9 use OpenSRF::Utils::Cache;
10 use OpenILS::Utils::Fieldmapper;
11 use OpenILS::Utils::CStoreEditor qw/:funcs/;
15 use OpenILS::Utils::Fieldmapper;
16 use Time::HiRes qw(time);
17 use OpenSRF::Utils::Logger qw/$logger/;
19 use OpenILS::Application::AppUtils;
20 use OpenILS::Application::Cat::BibCommon;
21 use OpenILS::Application::Cat::AuthCommon;
22 my $U = 'OpenILS::Application::AppUtils';
27 # --------------------------------------------------------------------------------
30 sub create_bib_queue {
38 my $e = new_editor(authtoken => $auth, xact => 1);
40 return $e->die_event unless $e->checkauth;
41 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE');
42 $owner ||= $e->requestor->id;
44 return OpenILS::Event->new('BIB_QUEUE_EXISTS')
45 if $e->search_vandelay_bib_queue(
46 {name => $name, owner => $owner, queue_type => $type})->[0];
48 my $queue = new Fieldmapper::vandelay::bib_queue();
49 $queue->name( $name );
50 $queue->owner( $owner );
51 $queue->queue_type( $type ) if ($type);
53 my $new_q = $e->create_vandelay_bib_queue( $queue );
54 return $e->die_event unless ($new_q);
59 __PACKAGE__->register_method(
60 api_name => "open-ils.vandelay.bib_queue.create",
61 method => "create_bib_queue",
67 sub create_auth_queue {
75 my $e = new_editor(authtoken => $auth, xact => 1);
77 return $e->die_event unless $e->checkauth;
78 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE');
79 $owner ||= $e->requestor->id;
81 return OpenILS::Event->new('AUTH_QUEUE_EXISTS')
82 if $e->search_vandelay_bib_queue(
83 {name => $name, owner => $owner, queue_type => $type})->[0];
85 my $queue = new Fieldmapper::vandelay::authority_queue();
86 $queue->name( $name );
87 $queue->owner( $owner );
88 $queue->queue_type( $type ) if ($type);
90 my $new_q = $e->create_vandelay_authority_queue( $queue );
91 $e->die_event unless ($new_q);
96 __PACKAGE__->register_method(
97 api_name => "open-ils.vandelay.authority_queue.create",
98 method => "create_auth_queue",
103 sub add_record_to_bib_queue {
111 my $e = new_editor(authtoken => $auth, xact => 1);
113 $queue = $e->retrieve_vandelay_bib_queue($queue);
115 return $e->die_event unless $e->checkauth;
116 return $e->die_event unless
117 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
118 $e->allowed('CREATE_BIB_IMPORT_QUEUE'));
120 my $new_rec = _add_bib_rec($e, $marc, $queue->id, $purpose);
122 return $e->die_event unless ($new_rec);
126 __PACKAGE__->register_method(
127 api_name => "open-ils.vandelay.queued_bib_record.create",
128 method => "add_record_to_bib_queue",
139 my $rec = new Fieldmapper::vandelay::queued_bib_record();
141 $rec->queue( $queue );
142 $rec->purpose( $purpose ) if ($purpose);
144 return $e->create_vandelay_queued_bib_record( $rec );
147 sub add_record_to_authority_queue {
155 my $e = new_editor(authtoken => $auth, xact => 1);
157 $queue = $e->retrieve_vandelay_authority_queue($queue);
159 return $e->die_event unless $e->checkauth;
160 return $e->die_event unless
161 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
162 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE'));
164 my $new_rec = _add_auth_rec($e, $marc, $queue->id, $purpose);
166 return $e->die_event unless ($new_rec);
170 __PACKAGE__->register_method(
171 api_name => "open-ils.vandelay.queued_authority_record.create",
172 method => "add_record_to_authority_queue",
183 my $rec = new Fieldmapper::vandelay::queued_authority_record();
185 $rec->queue( $queue );
186 $rec->purpose( $purpose ) if ($purpose);
188 return $e->create_vandelay_queued_authority_record( $rec );
195 my $fingerprint = shift;
196 my $queue_id = shift;
198 my $e = new_editor(authtoken => $auth, xact => 1);
199 return $e->die_event unless $e->checkauth;
202 my $type = $self->{record_type};
205 $queue = $e->retrieve_vandelay_bib_queue($queue_id) or return $e->die_event;
207 $queue = $e->retrieve_vandelay_authority_queue($queue_id) or return $e->die_event;
210 my $evt = check_queue_perms($e, $type, $queue);
213 my $method = "open-ils.vandelay.queued_${type}_record.create";
214 $method = $self->method_lookup( $method );
216 my $cache = new OpenSRF::Utils::Cache();
218 my $data = $cache->get_cache('vandelay_import_spool_' . $fingerprint);
219 my $purpose = $data->{purpose};
220 $data = decode_base64($data->{marc});
222 $logger->info("vandelay loaded $fingerprint purpose=$purpose and ".length($data)." bytes of data");
225 open $fh, '<', \$data;
227 my $marctype = 'USMARC'; # ?
228 my $batch = new MARC::Batch ( $marctype, $fh );
232 while (my $r = $batch->next) {
233 $logger->info("processing record $count");
235 (my $xml = $r->as_xml_record()) =~ s/\n//sog;
236 $xml =~ s/^<\?xml.+\?\s*>//go;
237 $xml =~ s/>\s+</></go;
238 $xml =~ s/\p{Cc}//go;
239 $xml = $U->entityize($xml);
240 $xml =~ s/[\x00-\x1f]//go;
242 if ($type eq 'bib') {
243 _add_bib_rec( $e, $xml, $queue_id, $purpose ) or return $e->die_event;
245 _add_auth_rec( $e, $xml, $queue_id, $purpose ) or return $e->die_event;
249 $client->respond( $count );
252 $logger->warn("Encountered a bad record at Vandelay ingest: ".$error);
259 __PACKAGE__->register_method(
260 api_name => "open-ils.vandelay.bib.process_spool",
261 method => "process_spool",
266 __PACKAGE__->register_method(
267 api_name => "open-ils.vandelay.auth.process_spool",
268 method => "process_spool",
271 record_type => 'auth'
275 __PACKAGE__->register_method(
276 api_name => "open-ils.vandelay.bib_queue.records.retrieve",
277 method => 'retrieve_queue',
283 __PACKAGE__->register_method(
284 api_name => "open-ils.vandelay.auth_queue.records.retrieve",
285 method => 'retrieve_queue',
289 record_type => 'auth'
292 __PACKAGE__->register_method(
293 api_name => "open-ils.vandelay.bib_queue.records.matches.retrieve",
294 method => 'retrieve_queue',
298 record_type => 'bib',
300 desc => q/Only retrieve queued bib records that have matches against existing records/
303 __PACKAGE__->register_method(
304 api_name => "open-ils.vandelay.auth_queue.records.matches.retrieve",
305 method => 'retrieve_queue',
309 record_type => 'auth',
311 desc => q/Only retrieve queued authority records that have matches against existing records/
317 my($self, $conn, $auth, $queue_id, $options) = @_;
318 my $e = new_editor(authtoken => $auth);
319 return $e->event unless $e->checkauth;
321 my $limit = $$options{limit} || 20;
322 my $offset = $$options{offset} || 0;
324 my $type = $self->{record_type};
327 $queue = $e->retrieve_vandelay_bib_queue($queue_id) or return $e->die_event;
329 $queue = $e->retrieve_vandelay_authority_queue($queue_id) or return $e->die_event;
331 my $evt = check_queue_perms($e, $type, $queue);
334 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
335 my $search = ($type eq 'bib') ?
336 'search_vandelay_queued_bib_record' : 'search_vandelay_queued_authority_record';
337 my $retrieve = ($type eq 'bib') ?
338 'retrieve_vandelay_queued_bib_record' : 'retrieve_vandelay_queued_authority_record';
341 if($self->api_name =~ /matches/) {
342 # fetch only matched records
343 $record_ids = queued_records_with_matches($e, $type, $queue_id, $limit, $offset);
345 # fetch all queue records
346 $record_ids = $e->$search([
347 {queue => $queue_id},
348 {order_by => {$class => 'id'}, limit => $limit, offset => $offset}
355 for my $rec_id (@$record_ids) {
358 flesh_fields => {$class => ['attributes', 'matches']},
360 my $rec = $e->$retrieve([$rec_id, $params]);
361 $rec->clear_marc if $$options{clear_marc};
362 $conn->respond($rec);
367 sub check_queue_perms {
368 my($e, $type, $queue) = @_;
369 if ($type eq 'bib') {
370 return $e->die_event unless
371 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
372 $e->allowed('CREATE_BIB_IMPORT_QUEUE'));
374 return $e->die_event unless
375 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
376 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE'));
382 __PACKAGE__->register_method(
383 api_name => "open-ils.vandelay.bib_record.list.import",
384 method => 'import_record_list',
391 __PACKAGE__->register_method(
392 api_name => "open-ils.vandelay.auth_record.list.import",
393 method => 'import_record_list',
397 record_type => 'auth'
400 sub import_record_list {
401 my($self, $conn, $auth, $rec_ids, $args) = @_;
402 my $e = new_editor(xact => 1, authtoken => $auth);
403 return $e->die_event unless $e->checkauth;
405 my $err = import_record_list_impl($self, $conn, $auth, $e, $rec_ids, $args);
408 return {complete => 1};
412 __PACKAGE__->register_method(
413 api_name => "open-ils.vandelay.bib_queue.import",
414 method => 'import_queue',
421 __PACKAGE__->register_method(
422 api_name => "open-ils.vandelay.auth_queue.import",
423 method => 'import_queue',
427 record_type => 'auth'
429 __PACKAGE__->register_method(
430 api_name => "open-ils.vandelay.bib_queue.nomatch.import",
431 method => 'import_queue',
436 desc => q/Only import records that have no collisions/
441 __PACKAGE__->register_method(
442 api_name => "open-ils.vandelay.auth_queue.nomatch.import",
443 method => 'import_queue',
448 desc => q/Only import records that have no collisions/
450 record_type => 'auth'
453 my($self, $conn, $auth, $q_id, $options) = @_;
454 my $e = new_editor(xact => 1, authtoken => $auth);
455 return $e->die_event unless $e->checkauth;
457 my $type = $self->{record_type};
458 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
460 my $query = {queue => $q_id, import_time => undef};
462 if($self->api_name =~ /nomatch/) {
463 my $matched_recs = queued_records_with_matches($e, $type, $q_id, undef, undef, {import_time => undef});
464 $query->{id} = {'not in' => $matched_recs} if @$matched_recs;
467 my $search = ($type eq 'bib') ?
468 'search_vandelay_queued_bib_record' : 'search_vandelay_queued_authority_record';
469 my $rec_ids = $e->$search($query, {idlist => 1});
470 my $err = import_record_list_impl($self, $conn, $auth, $e, $rec_ids, $options);
473 return {complete => 1};
477 # returns a list of queued record IDs for a given queue that
478 # have at least one entry in the match table
479 sub queued_records_with_matches {
480 my($e, $type, $q_id, $limit, $offset, $filter) = @_;
482 my $match_class = 'vbm';
483 my $rec_class = 'vqbr';
484 if($type eq 'auth') {
485 $match_class = 'vam';
490 $filter->{queue} = $q_id;
494 select => {$match_class => ['queued_record']},
499 fkey => 'queued_record',
506 if($limit or defined $offset) {
509 $query->{limit} = $limit;
510 $query->{offset} = $offset;
513 my $data = $e->json_query($query);
514 return [ map {$_->{queued_record}} @$data ];
517 sub import_record_list_impl {
518 my($self, $conn, $auth, $e, $rec_ids, $args) = @_;
520 my $overlay_map = $args->{overlay_map} || {};
521 my $type = $self->{record_type};
522 my $total = @$rec_ids;
526 for my $rec_id (@$rec_ids) {
528 my $overlay_target = $overlay_map->{$rec_id};
532 my $rec = $e->retrieve_vandelay_queued_bib_record($rec_id)
533 or return $e->die_event;
535 next if $rec->import_time;
536 $queues{$rec->queue} = 1;
539 if(defined $overlay_target) {
540 $logger->info("vl: overlaying record $overlay_target");
541 $record = OpenILS::Application::Cat::BibCommon->biblio_record_replace_marc(
542 $e, $overlay_target, $rec->marc); #$rec->bib_source
544 $logger->info("vl: importing new record");
545 $record = OpenILS::Application::Cat::BibCommon->biblio_record_xml_import(
546 $e, $rec->marc); #$rec->bib_source
549 return $record if $U->event_code($record);
550 $rec->imported_as($record->id);
551 $rec->import_time('now');
552 $e->update_vandelay_queued_bib_record($rec) or return $e->die_event;
556 my $rec = $e->retrieve_vandelay_queued_authority_record($rec_id)
557 or return $e->die_event;
559 next if $rec->import_time;
560 $queues{$rec->queue} = 1;
563 if(defined $overlay_target) {
564 $logger->info("vl: overlaying record $overlay_target");
565 $record = OpenILS::Utils::Cat::AuthCommon->overlay_authority_record(
566 $overlay_target, $rec->marc); #$source);
568 $logger->info("vl: importing new record");
569 $record = OpenILS::Utils::Cat::AuthCommon->import_authority_record(
570 $rec->marc) #$source);
573 return $record if $U->event_code($record);
574 $rec->imported_as($record->id);
575 $rec->import_time('now');
576 $e->update_vandelay_queued_authority_record($rec) or return $e->die_event;
579 $conn->respond({total => $total, progress => ++$count, imported => $rec_id});
582 # see if we need to mark any queues as complete
583 for my $q_id (keys %queues) {
585 my $remaining = $e->search_vandelay_queued_bib_record(
586 {queue => $q_id, import_time => undef}, {idlist => 1});
587 unless(@$remaining) {
588 my $queue = $e->retrieve_vandelay_bib_queue($q_id);
589 unless($U->is_true($queue->complete)) {
590 $queue->complete('t');
591 $e->update_vandelay_bib_queue($queue) or return $e->die_event;
595 my $remaining = $e->search_vandelay_queued_authority_record(
596 {queue => $q_id, import_time => undef}, {idlist => 1});
597 unless(@$remaining) {
598 my $queue = $e->retrieve_vandelay_authority_queue($q_id);
599 unless($U->is_true($queue->complete)) {
600 $queue->complete('t');
601 $e->update_vandelay_authority_queue($queue) or return $e->die_event;
611 __PACKAGE__->register_method(
612 api_name => "open-ils.vandelay.bib_queue.owner.retrieve",
613 method => 'owner_queue_retrieve',
619 __PACKAGE__->register_method(
620 api_name => "open-ils.vandelay.authority_queue.owner.retrieve",
621 method => 'owner_queue_retrieve',
625 record_type => 'auth'
628 sub owner_queue_retrieve {
629 my($self, $conn, $auth, $owner_id) = @_;
630 my $e = new_editor(authtoken => $auth);
631 return $e->die_event unless $e->checkauth;
632 $owner_id = $e->requestor->id; # XXX add support for viewing other's queues?
634 if($self->{record_type} eq 'bib') {
635 $queues = $e->search_vandelay_bib_queue(
636 {complete => 'f', owner => $owner_id});
638 $queues = $e->search_vandelay_authority_queue(
639 {complete => 'f', owner => $owner_id});
641 $conn->respond($_) for @$queues;
645 __PACKAGE__->register_method(
646 api_name => "open-ils.vandelay.bib_queue.delete",
647 method => "delete_queue",
652 __PACKAGE__->register_method(
653 api_name => "open-ils.vandelay.auth_queue.delete",
654 method => "delete_queue",
657 record_type => 'auth'
661 my($self, $conn, $auth, $q_id) = @_;
662 my $e = new_editor(xact => 1, authtoken => $auth);
663 return $e->die_event unless $e->checkauth;
664 if($self->{record_type} eq 'bib') {
665 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE');
666 my $queue = $e->retrieve_vandelay_bib_queue($q_id)
667 or return $e->die_event;
668 $e->delete_vandelay_bib_queue($queue)
669 or return $e->die_event;
671 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE');
672 my $queue = $e->retrieve_vandelay_authority_queue($q_id)
673 or return $e->die_event;
674 $e->delete_vandelay_authority_queue($queue)
675 or return $e->die_event;