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 my $filename = $data->{path};
222 unless(-r $filename) {
223 $logger->error("unable to read MARC file");
224 return -1; # make this an event XXX
227 $logger->info("vandelay spooling $fingerprint purpose=$purpose file=$filename");
229 my $marctype = 'USMARC'; # ?
230 my $batch = new MARC::Batch ($marctype, $filename);
234 while (my $r = $batch->next) {
235 $logger->info("processing record $count");
237 (my $xml = $r->as_xml_record()) =~ s/\n//sog;
238 $xml =~ s/^<\?xml.+\?\s*>//go;
239 $xml =~ s/>\s+</></go;
240 $xml =~ s/\p{Cc}//go;
241 $xml = $U->entityize($xml);
242 $xml =~ s/[\x00-\x1f]//go;
244 if ($type eq 'bib') {
245 _add_bib_rec( $e, $xml, $queue_id, $purpose ) or return $e->die_event;
247 _add_auth_rec( $e, $xml, $queue_id, $purpose ) or return $e->die_event;
251 $client->respond( $count );
254 $logger->warn("Encountered a bad record at Vandelay ingest: ".$error);
260 $cache->delete_cache('vandelay_import_spool_' . $fingerprint);
264 __PACKAGE__->register_method(
265 api_name => "open-ils.vandelay.bib.process_spool",
266 method => "process_spool",
271 __PACKAGE__->register_method(
272 api_name => "open-ils.vandelay.auth.process_spool",
273 method => "process_spool",
276 record_type => 'auth'
280 __PACKAGE__->register_method(
281 api_name => "open-ils.vandelay.bib_queue.records.retrieve",
282 method => 'retrieve_queue',
288 __PACKAGE__->register_method(
289 api_name => "open-ils.vandelay.auth_queue.records.retrieve",
290 method => 'retrieve_queue',
294 record_type => 'auth'
297 __PACKAGE__->register_method(
298 api_name => "open-ils.vandelay.bib_queue.records.matches.retrieve",
299 method => 'retrieve_queue',
303 record_type => 'bib',
305 desc => q/Only retrieve queued bib records that have matches against existing records/
308 __PACKAGE__->register_method(
309 api_name => "open-ils.vandelay.auth_queue.records.matches.retrieve",
310 method => 'retrieve_queue',
314 record_type => 'auth',
316 desc => q/Only retrieve queued authority records that have matches against existing records/
322 my($self, $conn, $auth, $queue_id, $options) = @_;
323 my $e = new_editor(authtoken => $auth);
324 return $e->event unless $e->checkauth;
326 my $limit = $$options{limit} || 20;
327 my $offset = $$options{offset} || 0;
329 my $type = $self->{record_type};
332 $queue = $e->retrieve_vandelay_bib_queue($queue_id) or return $e->die_event;
334 $queue = $e->retrieve_vandelay_authority_queue($queue_id) or return $e->die_event;
336 my $evt = check_queue_perms($e, $type, $queue);
339 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
340 my $search = ($type eq 'bib') ?
341 'search_vandelay_queued_bib_record' : 'search_vandelay_queued_authority_record';
342 my $retrieve = ($type eq 'bib') ?
343 'retrieve_vandelay_queued_bib_record' : 'retrieve_vandelay_queued_authority_record';
346 if($self->api_name =~ /matches/) {
347 # fetch only matched records
348 $record_ids = queued_records_with_matches($e, $type, $queue_id, $limit, $offset);
350 # fetch all queue records
351 $record_ids = $e->$search([
352 {queue => $queue_id},
353 {order_by => {$class => 'id'}, limit => $limit, offset => $offset}
360 for my $rec_id (@$record_ids) {
363 flesh_fields => {$class => ['attributes', 'matches']},
365 my $rec = $e->$retrieve([$rec_id, $params]);
366 $rec->clear_marc if $$options{clear_marc};
367 $conn->respond($rec);
372 sub check_queue_perms {
373 my($e, $type, $queue) = @_;
374 if ($type eq 'bib') {
375 return $e->die_event unless
376 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
377 $e->allowed('CREATE_BIB_IMPORT_QUEUE'));
379 return $e->die_event unless
380 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
381 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE'));
387 __PACKAGE__->register_method(
388 api_name => "open-ils.vandelay.bib_record.list.import",
389 method => 'import_record_list',
396 __PACKAGE__->register_method(
397 api_name => "open-ils.vandelay.auth_record.list.import",
398 method => 'import_record_list',
402 record_type => 'auth'
405 sub import_record_list {
406 my($self, $conn, $auth, $rec_ids, $args) = @_;
407 my $e = new_editor(xact => 1, authtoken => $auth);
408 return $e->die_event unless $e->checkauth;
410 my $err = import_record_list_impl($self, $conn, $auth, $e, $rec_ids, $args);
413 return {complete => 1};
417 __PACKAGE__->register_method(
418 api_name => "open-ils.vandelay.bib_queue.import",
419 method => 'import_queue',
426 __PACKAGE__->register_method(
427 api_name => "open-ils.vandelay.auth_queue.import",
428 method => 'import_queue',
432 record_type => 'auth'
434 __PACKAGE__->register_method(
435 api_name => "open-ils.vandelay.bib_queue.nomatch.import",
436 method => 'import_queue',
441 desc => q/Only import records that have no collisions/
446 __PACKAGE__->register_method(
447 api_name => "open-ils.vandelay.auth_queue.nomatch.import",
448 method => 'import_queue',
453 desc => q/Only import records that have no collisions/
455 record_type => 'auth'
458 my($self, $conn, $auth, $q_id, $options) = @_;
459 my $e = new_editor(xact => 1, authtoken => $auth);
460 return $e->die_event unless $e->checkauth;
462 my $type = $self->{record_type};
463 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
465 my $query = {queue => $q_id, import_time => undef};
467 if($self->api_name =~ /nomatch/) {
468 my $matched_recs = queued_records_with_matches($e, $type, $q_id, undef, undef, {import_time => undef});
469 $query->{id} = {'not in' => $matched_recs} if @$matched_recs;
472 my $search = ($type eq 'bib') ?
473 'search_vandelay_queued_bib_record' : 'search_vandelay_queued_authority_record';
474 my $rec_ids = $e->$search($query, {idlist => 1});
475 my $err = import_record_list_impl($self, $conn, $auth, $e, $rec_ids, $options);
478 return {complete => 1};
482 # returns a list of queued record IDs for a given queue that
483 # have at least one entry in the match table
484 sub queued_records_with_matches {
485 my($e, $type, $q_id, $limit, $offset, $filter) = @_;
487 my $match_class = 'vbm';
488 my $rec_class = 'vqbr';
489 if($type eq 'auth') {
490 $match_class = 'vam';
495 $filter->{queue} = $q_id;
499 select => {$match_class => ['queued_record']},
504 fkey => 'queued_record',
511 if($limit or defined $offset) {
514 $query->{limit} = $limit;
515 $query->{offset} = $offset;
518 my $data = $e->json_query($query);
519 return [ map {$_->{queued_record}} @$data ];
522 sub import_record_list_impl {
523 my($self, $conn, $auth, $e, $rec_ids, $args) = @_;
525 my $overlay_map = $args->{overlay_map} || {};
526 my $type = $self->{record_type};
527 my $total = @$rec_ids;
531 for my $rec_id (@$rec_ids) {
533 my $overlay_target = $overlay_map->{$rec_id};
537 my $rec = $e->retrieve_vandelay_queued_bib_record($rec_id)
538 or return $e->die_event;
540 next if $rec->import_time;
541 $queues{$rec->queue} = 1;
544 if(defined $overlay_target) {
545 $logger->info("vl: overlaying record $overlay_target");
546 $record = OpenILS::Application::Cat::BibCommon->biblio_record_replace_marc(
547 $e, $overlay_target, $rec->marc); #$rec->bib_source
549 $logger->info("vl: importing new record");
550 $record = OpenILS::Application::Cat::BibCommon->biblio_record_xml_import(
551 $e, $rec->marc); #$rec->bib_source
554 return $record if $U->event_code($record);
555 $rec->imported_as($record->id);
556 $rec->import_time('now');
557 $e->update_vandelay_queued_bib_record($rec) or return $e->die_event;
561 my $rec = $e->retrieve_vandelay_queued_authority_record($rec_id)
562 or return $e->die_event;
564 next if $rec->import_time;
565 $queues{$rec->queue} = 1;
568 if(defined $overlay_target) {
569 $logger->info("vl: overlaying record $overlay_target");
570 $record = OpenILS::Utils::Cat::AuthCommon->overlay_authority_record(
571 $overlay_target, $rec->marc); #$source);
573 $logger->info("vl: importing new record");
574 $record = OpenILS::Utils::Cat::AuthCommon->import_authority_record(
575 $rec->marc) #$source);
578 return $record if $U->event_code($record);
579 $rec->imported_as($record->id);
580 $rec->import_time('now');
581 $e->update_vandelay_queued_authority_record($rec) or return $e->die_event;
584 $conn->respond({total => $total, progress => ++$count, imported => $rec_id});
587 # see if we need to mark any queues as complete
588 for my $q_id (keys %queues) {
590 my $remaining = $e->search_vandelay_queued_bib_record(
591 {queue => $q_id, import_time => undef}, {idlist => 1});
592 unless(@$remaining) {
593 my $queue = $e->retrieve_vandelay_bib_queue($q_id);
594 unless($U->is_true($queue->complete)) {
595 $queue->complete('t');
596 $e->update_vandelay_bib_queue($queue) or return $e->die_event;
600 my $remaining = $e->search_vandelay_queued_authority_record(
601 {queue => $q_id, import_time => undef}, {idlist => 1});
602 unless(@$remaining) {
603 my $queue = $e->retrieve_vandelay_authority_queue($q_id);
604 unless($U->is_true($queue->complete)) {
605 $queue->complete('t');
606 $e->update_vandelay_authority_queue($queue) or return $e->die_event;
616 __PACKAGE__->register_method(
617 api_name => "open-ils.vandelay.bib_queue.owner.retrieve",
618 method => 'owner_queue_retrieve',
624 __PACKAGE__->register_method(
625 api_name => "open-ils.vandelay.authority_queue.owner.retrieve",
626 method => 'owner_queue_retrieve',
630 record_type => 'auth'
633 sub owner_queue_retrieve {
634 my($self, $conn, $auth, $owner_id, $filters) = @_;
635 my $e = new_editor(authtoken => $auth);
636 return $e->die_event unless $e->checkauth;
637 $owner_id = $e->requestor->id; # XXX add support for viewing other's queues?
640 my $search = {owner => $owner_id};
641 $search->{$_} = $filters->{$_} for keys %$filters;
643 if($self->{record_type} eq 'bib') {
644 $queues = $e->search_vandelay_bib_queue(
645 [$search, {order_by => {vbq => 'lower(name)'}}]);
647 $queues = $e->search_vandelay_authority_queue(
648 [$search, {order_by => {vaq => 'lower(name)'}}]);
650 $conn->respond($_) for @$queues;
654 __PACKAGE__->register_method(
655 api_name => "open-ils.vandelay.bib_queue.delete",
656 method => "delete_queue",
661 __PACKAGE__->register_method(
662 api_name => "open-ils.vandelay.auth_queue.delete",
663 method => "delete_queue",
666 record_type => 'auth'
670 my($self, $conn, $auth, $q_id) = @_;
671 my $e = new_editor(xact => 1, authtoken => $auth);
672 return $e->die_event unless $e->checkauth;
673 if($self->{record_type} eq 'bib') {
674 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE');
675 my $queue = $e->retrieve_vandelay_bib_queue($q_id)
676 or return $e->die_event;
677 $e->delete_vandelay_bib_queue($queue)
678 or return $e->die_event;
680 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE');
681 my $queue = $e->retrieve_vandelay_authority_queue($q_id)
682 or return $e->die_event;
683 $e->delete_vandelay_authority_queue($queue)
684 or return $e->die_event;
691 __PACKAGE__->register_method(
692 api_name => "open-ils.vandelay.queued_bib_record.html",
693 method => 'queued_record_html',
699 __PACKAGE__->register_method(
700 api_name => "open-ils.vandelay.queued_authority_record.html",
701 method => 'queued_record_html',
705 record_type => 'auth'
708 sub queued_record_html {
709 my($self, $conn, $auth, $rec_id) = @_;
710 my $e = new_editor(authtoken => $auth);
711 return $e->event unless $e->checkauth;
713 if($self->{record_type} eq 'bib') {
714 $rec = $e->retrieve_vandelay_queued_bib_record($rec_id)
717 $rec = $e->retrieve_vandelay_queued_authority_record($rec_id)
721 return $U->simplereq(
723 'open-ils.search.biblio.record.html', undef, 1, $rec->marc);