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';
345 my $filter = ($$options{non_imported}) ? {import_time => undef} : {};
348 if($self->api_name =~ /matches/) {
349 # fetch only matched records
350 $record_ids = queued_records_with_matches($e, $type, $queue_id, $limit, $offset, $filter);
352 # fetch all queue records
353 $record_ids = $e->$search([
354 {queue => $queue_id, %$filter},
355 {order_by => {$class => 'id'}, limit => $limit, offset => $offset}
362 for my $rec_id (@$record_ids) {
365 flesh_fields => {$class => ['attributes', 'matches']},
367 my $rec = $e->$retrieve([$rec_id, $params]);
368 $rec->clear_marc if $$options{clear_marc};
369 $conn->respond($rec);
374 sub check_queue_perms {
375 my($e, $type, $queue) = @_;
376 if ($type eq 'bib') {
377 return $e->die_event unless
378 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
379 $e->allowed('CREATE_BIB_IMPORT_QUEUE'));
381 return $e->die_event unless
382 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
383 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE'));
389 __PACKAGE__->register_method(
390 api_name => "open-ils.vandelay.bib_record.list.import",
391 method => 'import_record_list',
398 __PACKAGE__->register_method(
399 api_name => "open-ils.vandelay.auth_record.list.import",
400 method => 'import_record_list',
404 record_type => 'auth'
407 sub import_record_list {
408 my($self, $conn, $auth, $rec_ids, $args) = @_;
409 my $e = new_editor(xact => 1, authtoken => $auth);
410 return $e->die_event unless $e->checkauth;
412 my $err = import_record_list_impl($self, $conn, $auth, $e, $rec_ids, $args);
415 return {complete => 1};
419 __PACKAGE__->register_method(
420 api_name => "open-ils.vandelay.bib_queue.import",
421 method => 'import_queue',
428 __PACKAGE__->register_method(
429 api_name => "open-ils.vandelay.auth_queue.import",
430 method => 'import_queue',
434 record_type => 'auth'
436 __PACKAGE__->register_method(
437 api_name => "open-ils.vandelay.bib_queue.nomatch.import",
438 method => 'import_queue',
443 desc => q/Only import records that have no collisions/
448 __PACKAGE__->register_method(
449 api_name => "open-ils.vandelay.auth_queue.nomatch.import",
450 method => 'import_queue',
455 desc => q/Only import records that have no collisions/
457 record_type => 'auth'
460 my($self, $conn, $auth, $q_id, $options) = @_;
461 my $e = new_editor(xact => 1, authtoken => $auth);
462 return $e->die_event unless $e->checkauth;
464 my $type = $self->{record_type};
465 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
467 my $query = {queue => $q_id, import_time => undef};
469 if($self->api_name =~ /nomatch/) {
470 my $matched_recs = queued_records_with_matches($e, $type, $q_id, undef, undef, {import_time => undef});
471 $query->{id} = {'not in' => $matched_recs} if @$matched_recs;
474 my $search = ($type eq 'bib') ?
475 'search_vandelay_queued_bib_record' : 'search_vandelay_queued_authority_record';
476 my $rec_ids = $e->$search($query, {idlist => 1});
477 my $err = import_record_list_impl($self, $conn, $auth, $e, $rec_ids, $options);
480 return {complete => 1};
484 # returns a list of queued record IDs for a given queue that
485 # have at least one entry in the match table
486 sub queued_records_with_matches {
487 my($e, $type, $q_id, $limit, $offset, $filter) = @_;
489 my $match_class = 'vbm';
490 my $rec_class = 'vqbr';
491 if($type eq 'auth') {
492 $match_class = 'vam';
497 $filter->{queue} = $q_id;
501 select => {$match_class => ['queued_record']},
506 fkey => 'queued_record',
513 if($limit or defined $offset) {
516 $query->{limit} = $limit;
517 $query->{offset} = $offset;
520 my $data = $e->json_query($query);
521 return [ map {$_->{queued_record}} @$data ];
524 sub import_record_list_impl {
525 my($self, $conn, $auth, $e, $rec_ids, $args) = @_;
527 my $overlay_map = $args->{overlay_map} || {};
528 my $type = $self->{record_type};
529 my $total = @$rec_ids;
533 for my $rec_id (@$rec_ids) {
535 my $overlay_target = $overlay_map->{$rec_id};
539 my $rec = $e->retrieve_vandelay_queued_bib_record($rec_id)
540 or return $e->die_event;
542 next if $rec->import_time;
543 $queues{$rec->queue} = 1;
546 if(defined $overlay_target) {
547 $logger->info("vl: overlaying record $overlay_target");
548 $record = OpenILS::Application::Cat::BibCommon->biblio_record_replace_marc(
549 $e, $overlay_target, $rec->marc); #$rec->bib_source
551 $logger->info("vl: importing new record");
552 $record = OpenILS::Application::Cat::BibCommon->biblio_record_xml_import(
553 $e, $rec->marc); #$rec->bib_source
556 return $record if $U->event_code($record);
557 $rec->imported_as($record->id);
558 $rec->import_time('now');
559 $e->update_vandelay_queued_bib_record($rec) or return $e->die_event;
563 my $rec = $e->retrieve_vandelay_queued_authority_record($rec_id)
564 or return $e->die_event;
566 next if $rec->import_time;
567 $queues{$rec->queue} = 1;
570 if(defined $overlay_target) {
571 $logger->info("vl: overlaying record $overlay_target");
572 $record = OpenILS::Utils::Cat::AuthCommon->overlay_authority_record(
573 $overlay_target, $rec->marc); #$source);
575 $logger->info("vl: importing new record");
576 $record = OpenILS::Utils::Cat::AuthCommon->import_authority_record(
577 $rec->marc) #$source);
580 return $record if $U->event_code($record);
581 $rec->imported_as($record->id);
582 $rec->import_time('now');
583 $e->update_vandelay_queued_authority_record($rec) or return $e->die_event;
586 $conn->respond({total => $total, progress => ++$count, imported => $rec_id});
589 # see if we need to mark any queues as complete
590 for my $q_id (keys %queues) {
592 my $remaining = $e->search_vandelay_queued_bib_record(
593 {queue => $q_id, import_time => undef}, {idlist => 1});
594 unless(@$remaining) {
595 my $queue = $e->retrieve_vandelay_bib_queue($q_id);
596 unless($U->is_true($queue->complete)) {
597 $queue->complete('t');
598 $e->update_vandelay_bib_queue($queue) or return $e->die_event;
602 my $remaining = $e->search_vandelay_queued_authority_record(
603 {queue => $q_id, import_time => undef}, {idlist => 1});
604 unless(@$remaining) {
605 my $queue = $e->retrieve_vandelay_authority_queue($q_id);
606 unless($U->is_true($queue->complete)) {
607 $queue->complete('t');
608 $e->update_vandelay_authority_queue($queue) or return $e->die_event;
618 __PACKAGE__->register_method(
619 api_name => "open-ils.vandelay.bib_queue.owner.retrieve",
620 method => 'owner_queue_retrieve',
626 __PACKAGE__->register_method(
627 api_name => "open-ils.vandelay.authority_queue.owner.retrieve",
628 method => 'owner_queue_retrieve',
632 record_type => 'auth'
635 sub owner_queue_retrieve {
636 my($self, $conn, $auth, $owner_id, $filters) = @_;
637 my $e = new_editor(authtoken => $auth);
638 return $e->die_event unless $e->checkauth;
639 $owner_id = $e->requestor->id; # XXX add support for viewing other's queues?
642 my $search = {owner => $owner_id};
643 $search->{$_} = $filters->{$_} for keys %$filters;
645 if($self->{record_type} eq 'bib') {
646 $queues = $e->search_vandelay_bib_queue(
647 [$search, {order_by => {vbq => 'lower(name)'}}]);
649 $queues = $e->search_vandelay_authority_queue(
650 [$search, {order_by => {vaq => 'lower(name)'}}]);
652 $conn->respond($_) for @$queues;
656 __PACKAGE__->register_method(
657 api_name => "open-ils.vandelay.bib_queue.delete",
658 method => "delete_queue",
663 __PACKAGE__->register_method(
664 api_name => "open-ils.vandelay.auth_queue.delete",
665 method => "delete_queue",
668 record_type => 'auth'
672 my($self, $conn, $auth, $q_id) = @_;
673 my $e = new_editor(xact => 1, authtoken => $auth);
674 return $e->die_event unless $e->checkauth;
675 if($self->{record_type} eq 'bib') {
676 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE');
677 my $queue = $e->retrieve_vandelay_bib_queue($q_id)
678 or return $e->die_event;
679 $e->delete_vandelay_bib_queue($queue)
680 or return $e->die_event;
682 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE');
683 my $queue = $e->retrieve_vandelay_authority_queue($q_id)
684 or return $e->die_event;
685 $e->delete_vandelay_authority_queue($queue)
686 or return $e->die_event;
693 __PACKAGE__->register_method(
694 api_name => "open-ils.vandelay.queued_bib_record.html",
695 method => 'queued_record_html',
701 __PACKAGE__->register_method(
702 api_name => "open-ils.vandelay.queued_authority_record.html",
703 method => 'queued_record_html',
707 record_type => 'auth'
710 sub queued_record_html {
711 my($self, $conn, $auth, $rec_id) = @_;
712 my $e = new_editor(authtoken => $auth);
713 return $e->event unless $e->checkauth;
715 if($self->{record_type} eq 'bib') {
716 $rec = $e->retrieve_vandelay_queued_bib_record($rec_id)
719 $rec = $e->retrieve_vandelay_queued_authority_record($rec_id)
723 return $U->simplereq(
725 'open-ils.search.biblio.record.html', undef, 1, $rec->marc);