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/;
14 use MARC::File::XML ( BinaryEncoding => 'UTF-8' );
15 use OpenILS::Utils::Fieldmapper;
16 use Time::HiRes qw(time);
17 use OpenSRF::Utils::Logger qw/$logger/;
19 use OpenILS::Const qw/:const/;
20 use OpenILS::Application::AppUtils;
21 use OpenILS::Application::Cat::BibCommon;
22 use OpenILS::Application::Cat::AuthCommon;
23 use OpenILS::Application::Cat::AssetCommon;
24 my $U = 'OpenILS::Application::AppUtils';
26 # A list of LDR/06 values from http://loc.gov/marc
53 # --------------------------------------------------------------------------------
56 sub create_bib_queue {
63 my $import_def = shift;
65 my $e = new_editor(authtoken => $auth, xact => 1);
67 return $e->die_event unless $e->checkauth;
68 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE');
69 $owner ||= $e->requestor->id;
71 if ($e->search_vandelay_bib_queue( {name => $name, owner => $owner, queue_type => $type})->[0]) {
73 return OpenILS::Event->new('BIB_QUEUE_EXISTS')
76 my $queue = new Fieldmapper::vandelay::bib_queue();
77 $queue->name( $name );
78 $queue->owner( $owner );
79 $queue->queue_type( $type ) if ($type);
80 $queue->item_attr_def( $import_def ) if ($import_def);
82 my $new_q = $e->create_vandelay_bib_queue( $queue );
83 return $e->die_event unless ($new_q);
88 __PACKAGE__->register_method(
89 api_name => "open-ils.vandelay.bib_queue.create",
90 method => "create_bib_queue",
96 sub create_auth_queue {
104 my $e = new_editor(authtoken => $auth, xact => 1);
106 return $e->die_event unless $e->checkauth;
107 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE');
108 $owner ||= $e->requestor->id;
110 if ($e->search_vandelay_bib_queue({name => $name, owner => $owner, queue_type => $type})->[0]) {
112 return OpenILS::Event->new('AUTH_QUEUE_EXISTS')
115 my $queue = new Fieldmapper::vandelay::authority_queue();
116 $queue->name( $name );
117 $queue->owner( $owner );
118 $queue->queue_type( $type ) if ($type);
120 my $new_q = $e->create_vandelay_authority_queue( $queue );
121 $e->die_event unless ($new_q);
126 __PACKAGE__->register_method(
127 api_name => "open-ils.vandelay.authority_queue.create",
128 method => "create_auth_queue",
133 sub add_record_to_bib_queue {
140 my $bib_source = shift;
142 my $e = new_editor(authtoken => $auth, xact => 1);
144 $queue = $e->retrieve_vandelay_bib_queue($queue);
146 return $e->die_event unless $e->checkauth;
147 return $e->die_event unless
148 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
149 $e->allowed('CREATE_BIB_IMPORT_QUEUE'));
151 my $new_rec = _add_bib_rec($e, $marc, $queue->id, $purpose, $bib_source);
153 return $e->die_event unless ($new_rec);
157 __PACKAGE__->register_method(
158 api_name => "open-ils.vandelay.queued_bib_record.create",
159 method => "add_record_to_bib_queue",
169 my $bib_source = shift;
171 my $rec = new Fieldmapper::vandelay::queued_bib_record();
173 $rec->queue( $queue );
174 $rec->purpose( $purpose ) if ($purpose);
175 $rec->bib_source($bib_source);
177 return $e->create_vandelay_queued_bib_record( $rec );
180 sub add_record_to_authority_queue {
188 my $e = new_editor(authtoken => $auth, xact => 1);
190 $queue = $e->retrieve_vandelay_authority_queue($queue);
192 return $e->die_event unless $e->checkauth;
193 return $e->die_event unless
194 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
195 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE'));
197 my $new_rec = _add_auth_rec($e, $marc, $queue->id, $purpose);
199 return $e->die_event unless ($new_rec);
203 __PACKAGE__->register_method(
204 api_name => "open-ils.vandelay.queued_authority_record.create",
205 method => "add_record_to_authority_queue",
216 my $rec = new Fieldmapper::vandelay::queued_authority_record();
218 $rec->queue( $queue );
219 $rec->purpose( $purpose ) if ($purpose);
221 return $e->create_vandelay_queued_authority_record( $rec );
228 my $fingerprint = shift || '';
229 my $queue_id = shift;
231 my $filename = shift;
232 my $bib_source = shift;
234 my $e = new_editor(authtoken => $auth, xact => 1);
235 return $e->die_event unless $e->checkauth;
238 my $type = $self->{record_type};
241 $queue = $e->retrieve_vandelay_bib_queue($queue_id) or return $e->die_event;
243 $queue = $e->retrieve_vandelay_authority_queue($queue_id) or return $e->die_event;
246 my $evt = check_queue_perms($e, $type, $queue);
247 return $evt if ($evt);
249 my $cache = new OpenSRF::Utils::Cache();
252 my $data = $cache->get_cache('vandelay_import_spool_' . $fingerprint);
253 $purpose = $data->{purpose};
254 $filename = $data->{path};
255 $bib_source = $data->{bib_source};
258 unless(-r $filename) {
259 $logger->error("unable to read MARC file $filename");
260 return -1; # make this an event XXX
263 $logger->info("vandelay spooling $fingerprint purpose=$purpose file=$filename");
265 my $marctype = 'USMARC';
268 $marctype = 'XML' if (getc(F) =~ /^\D/o);
271 my $batch = new MARC::Batch ($marctype, $filename);
274 my $response_scale = 10;
277 while (try { $r = $batch->next } otherwise { $r = -1 }) {
279 $logger->warn("Processing of record $count in set $filename failed. Skipping this record");
283 $logger->info("processing record $count");
286 (my $xml = $r->as_xml_record()) =~ s/\n//sog;
287 $xml =~ s/^<\?xml.+\?\s*>//go;
288 $xml =~ s/>\s+</></go;
289 $xml =~ s/\p{Cc}//go;
290 $xml = $U->entityize($xml);
291 $xml =~ s/[\x00-\x1f]//go;
294 # Check the leader to ensure we've got something resembling the expected
295 # Allow spaces to give records the benefit of the doubt
296 my $ldr_type = substr($r->leader(), 6, 1);
297 if ($type eq 'bib' && ($record_types{$ldr_type}) eq 'bib' || $ldr_type eq ' ') {
298 $qrec = _add_bib_rec( $e, $xml, $queue_id, $purpose, $bib_source ) or return $e->die_event;
299 } elsif ($type eq 'auth' && ($record_types{$ldr_type}) eq 'auth' || $ldr_type eq ' ') {
300 $qrec = _add_auth_rec( $e, $xml, $queue_id, $purpose ) or return $e->die_event;
302 # I don't know how to handle this type; rock on
303 $logger->error("In process_spool(), type was $type and leader type was $ldr_type ; not currently supported");
307 if($self->api_name =~ /stream_results/ and $qrec) {
308 $client->respond($qrec->id)
310 $client->respond($count) if (++$count % $response_scale) == 0;
311 $response_scale *= 10 if ($count == ($response_scale * 10));
315 $logger->warn("Encountered a bad record at Vandelay ingest: ".$error);
321 $cache->delete_cache('vandelay_import_spool_' . $fingerprint) if $fingerprint;
325 __PACKAGE__->register_method(
326 api_name => "open-ils.vandelay.bib.process_spool",
327 method => "process_spool",
333 __PACKAGE__->register_method(
334 api_name => "open-ils.vandelay.auth.process_spool",
335 method => "process_spool",
339 record_type => 'auth'
342 __PACKAGE__->register_method(
343 api_name => "open-ils.vandelay.bib.process_spool.stream_results",
344 method => "process_spool",
351 __PACKAGE__->register_method(
352 api_name => "open-ils.vandelay.auth.process_spool.stream_results",
353 method => "process_spool",
358 record_type => 'auth'
361 __PACKAGE__->register_method(
362 api_name => "open-ils.vandelay.bib_queue.records.retrieve",
363 method => 'retrieve_queued_records',
369 __PACKAGE__->register_method(
370 api_name => "open-ils.vandelay.auth_queue.records.retrieve",
371 method => 'retrieve_queued_records',
375 record_type => 'auth'
378 __PACKAGE__->register_method(
379 api_name => "open-ils.vandelay.bib_queue.records.matches.retrieve",
380 method => 'retrieve_queued_records',
384 record_type => 'bib',
386 desc => q/Only retrieve queued bib records that have matches against existing records/
389 __PACKAGE__->register_method(
390 api_name => "open-ils.vandelay.auth_queue.records.matches.retrieve",
391 method => 'retrieve_queued_records',
395 record_type => 'auth',
397 desc => q/Only retrieve queued authority records that have matches against existing records/
401 sub retrieve_queued_records {
402 my($self, $conn, $auth, $queue_id, $options) = @_;
405 my $limit = $$options{limit} || 20;
406 my $offset = $$options{offset} || 0;
407 my $type = $self->{record_type};
409 my $e = new_editor(authtoken => $auth, xact => 1);
410 return $e->die_event unless $e->checkauth;
414 $queue = $e->retrieve_vandelay_bib_queue($queue_id) or return $e->die_event;
416 $queue = $e->retrieve_vandelay_authority_queue($queue_id) or return $e->die_event;
418 my $evt = check_queue_perms($e, $type, $queue);
419 return $evt if ($evt);
421 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
423 select => {$class => ['id']},
425 where => {queue => $queue_id},
427 order_by => {$class => ['id']},
432 $query->{where}->{import_time} = undef if $$options{non_imported};
433 $query->{where}->{import_error} = {'!=' => undef} if $$options{with_rec_import_error};
435 if($$options{with_item_import_error} and $type eq 'bib') {
436 # limit to recs that have at least 1 item import error
442 filter => {import_error => {'!=' => undef}}
448 if($self->api_name =~ /matches/) {
449 # find only records that have matches
450 my $mclass = $type eq 'bib' ? 'vbm' : 'vam';
451 $query->{from} = {$class => {$mclass => {type => 'right'}}};
454 my $record_ids = $e->json_query($query);
456 my $retrieve = ($type eq 'bib') ?
457 'retrieve_vandelay_queued_bib_record' : 'retrieve_vandelay_queued_authority_record';
459 for my $rec_id (@$record_ids) {
460 my $flesh = ['attributes', 'matches'];
461 push(@$flesh, 'import_items') if $$options{flesh_import_items};
462 my $params = {flesh => 1, flesh_fields => {$class => $flesh}};
463 my $rec = $e->$retrieve([$rec_id->{id}, $params]);
464 $rec->clear_marc if $$options{clear_marc};
465 $conn->respond($rec);
472 __PACKAGE__->register_method(
473 api_name => 'open-ils.vandelay.import_item.queue.retrieve',
474 method => 'retrieve_queue_import_items',
480 Returns Import Item (vii) objects for the selected queue.
482 with_import_error : only return items that failed to import
486 sub retrieve_queue_import_items {
487 my($self, $conn, $auth, $q_id, $options) = @_;
490 my $limit = $$options{limit} || 20;
491 my $offset = $$options{offset} || 0;
493 my $e = new_editor(authtoken => $auth);
494 return $e->event unless $e->checkauth;
496 my $queue = $e->retrieve_vandelay_bib_queue($q_id) or return $e->event;
497 my $evt = check_queue_perms($e, 'bib', $queue);
501 select => {vii => ['id']},
509 filter => {id => $q_id}
515 order_by => {'vii' => ['record','id']}
518 $query->{where} = {'+vii' => {import_error => {'!=' => undef}}}
519 if $$options{with_import_error};
521 my $items = $e->json_query($query);
522 for my $item (@$items) {
523 $conn->respond($e->retrieve_vandelay_import_item($item->{id}));
529 sub check_queue_perms {
530 my($e, $type, $queue) = @_;
531 if ($type eq 'bib') {
532 return $e->die_event unless
533 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
534 $e->allowed('CREATE_BIB_IMPORT_QUEUE'));
536 return $e->die_event unless
537 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
538 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE'));
544 __PACKAGE__->register_method(
545 api_name => "open-ils.vandelay.bib_record.list.import",
546 method => 'import_record_list',
553 __PACKAGE__->register_method(
554 api_name => "open-ils.vandelay.auth_record.list.import",
555 method => 'import_record_list',
559 record_type => 'auth'
562 sub import_record_list {
563 my($self, $conn, $auth, $rec_ids, $args) = @_;
564 my $e = new_editor(authtoken => $auth, xact => 1);
565 return $e->die_event unless $e->checkauth;
567 my $err = import_record_list_impl($self, $conn, $rec_ids, $e->requestor, $args);
570 return {complete => 1};
574 __PACKAGE__->register_method(
575 api_name => "open-ils.vandelay.bib_queue.import",
576 method => 'import_queue',
584 __PACKAGE__->register_method(
585 api_name => "open-ils.vandelay.auth_queue.import",
586 method => 'import_queue',
591 record_type => 'auth'
593 __PACKAGE__->register_method(
594 api_name => "open-ils.vandelay.bib_queue.nomatch.import",
595 method => 'import_queue',
600 desc => q/Only import records that have no collisions/
606 __PACKAGE__->register_method(
607 api_name => "open-ils.vandelay.auth_queue.nomatch.import",
608 method => 'import_queue',
613 desc => q/Only import records that have no collisions/
616 record_type => 'auth'
619 my($self, $conn, $auth, $q_id, $options) = @_;
620 my $e = new_editor(authtoken => $auth, xact => 1);
621 return $e->die_event unless $e->checkauth;
623 my $type = $self->{record_type};
624 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
626 my $query = {queue => $q_id, import_time => undef};
628 if($self->api_name =~ /nomatch/) {
629 my $matched_recs = queued_records_with_matches($e, $type, $q_id, undef, undef, {import_time => undef});
630 $query->{id} = {'not in' => $matched_recs} if @$matched_recs;
633 my $search = ($type eq 'bib') ?
634 'search_vandelay_queued_bib_record' : 'search_vandelay_queued_authority_record';
635 my $rec_ids = $e->$search($query, {idlist => 1});
636 my $err = import_record_list_impl($self, $conn, $rec_ids, $e->requestor, $options);
637 try {$e->rollback} otherwise {}; # only using this to make the read authoritative -- don't die from it
639 return {complete => 1};
642 # returns a list of queued record IDs for a given queue that
643 # have at least one entry in the match table
644 sub queued_records_with_matches {
645 my($e, $type, $q_id, $limit, $offset, $filter) = @_;
647 my $match_class = 'vbm';
648 my $rec_class = 'vqbr';
649 if($type eq 'auth') {
650 $match_class = 'vam';
655 $filter->{queue} = $q_id;
659 select => {$match_class => ['queued_record']},
664 fkey => 'queued_record',
671 if($limit or defined $offset) {
674 $query->{limit} = $limit;
675 $query->{offset} = $offset;
678 my $data = $e->json_query($query);
679 return [ map {$_->{queued_record}} @$data ];
683 sub import_record_list_impl {
684 my($self, $conn, $rec_ids, $requestor, $args) = @_;
686 my $overlay_map = $args->{overlay_map} || {};
687 my $type = $self->{record_type};
694 total => scalar(@$rec_ids),
695 report_all => $$args{report_all}
698 my $auto_overlay_exact = $$args{auto_overlay_exact};
699 my $auto_overlay_1match = $$args{auto_overlay_1match};
700 my $merge_profile = $$args{merge_profile};
701 my $bib_source = $$args{bib_source};
703 my $overlay_func = 'vandelay.overlay_bib_record';
704 my $auto_overlay_func = 'vandelay.auto_overlay_bib_record';
705 my $retrieve_func = 'retrieve_vandelay_queued_bib_record';
706 my $update_func = 'update_vandelay_queued_bib_record';
707 my $search_func = 'search_vandelay_queued_bib_record';
708 my $retrieve_queue_func = 'retrieve_vandelay_bib_queue';
709 my $update_queue_func = 'update_vandelay_bib_queue';
710 my $rec_class = 'vqbr';
712 my $editor = new_editor();
715 my $sources = $editor->search_config_bib_source({id => {'!=' => undef}});
716 $bib_sources{$_->id} = $_->source for @$sources;
718 if($type eq 'auth') {
719 $overlay_func =~ s/bib/auth/o;
720 $auto_overlay_func = s/bib/auth/o;
721 $retrieve_func =~ s/bib/authority/o;
722 $retrieve_queue_func =~ s/bib/authority/o;
723 $update_queue_func =~ s/bib/authority/o;
724 $update_func =~ s/bib/authority/o;
725 $search_func =~ s/bib/authority/o;
730 for my $rec_id (@$rec_ids) {
733 my $overlay_target = $overlay_map->{$rec_id};
735 my $e = new_editor(xact => 1);
736 $e->requestor($requestor);
738 $report_args{progress}++;
739 $report_args{e} = $e;
741 my $rec = $e->$retrieve_func([
744 flesh_fields => { $rec_class => ['matches']},
749 finish_rec_import_attempt(%report_args, evt => $e->event);
753 $report_args{rec} = $rec;
755 if($rec->import_time) {
760 $queues{$rec->queue} = 1;
765 if(defined $overlay_target) {
766 # Caller chose an explicit overlay target
768 my $res = $e->json_query(
779 if($res and ($res = $res->[0])) {
781 if($res->{$overlay_func} eq 't') {
782 $logger->info("vl: $type direct overlay succeeded for queued rec " .
783 $rec->id . " and overlay target $overlay_target");
789 $logger->error("vl: Error attempting overlay with func=$overlay_func, profile=$merge_profile, record=$rec_id");
794 if($auto_overlay_1match) {
795 # caller says to overlay if there is exactly 1 match
797 my %match_recs = map { $_->eg_record => 1 } @{$rec->matches};
799 if( scalar(keys %match_recs) == 1) { # all matches point to the same record
801 my $res = $e->json_query(
806 $rec->matches->[0]->eg_record,
812 if($res and ($res = $res->[0])) {
814 if($res->{$overlay_func} eq 't') {
815 $logger->info("vl: $type overlay-1match succeeded for queued rec " . $rec->id);
821 $logger->error("vl: Error attempting overlay with func=$overlay_func, profile=$merge_profile, record=$rec_id");
826 if(!$imported and !$error and $auto_overlay_exact and scalar(@{$rec->matches}) == 1 ) {
828 # caller says to overlay if there is an /exact/ match
830 my $res = $e->json_query(
840 if($res and ($res = $res->[0])) {
842 if($res->{$auto_overlay_func} eq 't') {
843 $logger->info("vl: $type auto-overlay succeeded for queued rec " . $rec->id);
846 $logger->info("vl: $type auto-overlay failed for queued rec " . $rec->id);
851 $logger->error("vl: Error attempting overlay with func=$auto_overlay_func, profile=$merge_profile, record=$rec_id");
855 if(!$imported and !$error) {
857 # No overlay / merge occurred. Do a traditional record import by creating a new record
859 $logger->info("vl: creating new $type record for queued record $rec_id");
861 $record = OpenILS::Application::Cat::BibCommon->biblio_record_xml_import($e, $rec->marc, $bib_sources{$rec->bib_source});
864 $record = OpenILS::Application::Cat::AuthCommon->import_authority_record($e, $rec->marc); #$source);
867 if($U->event_code($record)) {
868 my $import_error = 'import.duplicate.tcn' if $record->{textcode} eq 'TCN_EXISTS';
869 finish_rec_import_attempt(%report_args, import_error => $import_error, evt => $record);
873 $logger->info("vl: successfully imported new $type record");
874 $rec->imported_as($record->id);
875 $rec->import_time('now');
877 $imported = 1 if $e->$update_func($rec);
883 push @success_rec_ids, $rec_id;
884 finish_rec_import_attempt(%report_args);
887 # Send an update whenever there's an error
888 finish_rec_import_attempt(%report_args, evt => $e->event);
892 # see if we need to mark any queues as complete
893 for my $q_id (keys %queues) {
895 my $e = new_editor(xact => 1);
896 my $remaining = $e->$search_func(
897 [{queue => $q_id, import_time => undef}, {limit =>1}], {idlist => 1});
899 unless(@$remaining) {
900 my $queue = $e->$retrieve_queue_func($q_id);
902 unless($U->is_true($queue->complete)) {
903 $queue->complete('t');
904 $e->$update_queue_func($queue) or return $e->die_event;
913 import_record_asset_list_impl($conn, \@success_rec_ids, $requestor);
915 $conn->respond({total => $report_args{total}, progress => $report_args{progress}});
919 # tracks any import errors, commits the current xact, responds to the client
920 sub finish_rec_import_attempt {
922 my $evt = $args{evt};
923 my $rec = $args{rec};
926 my $error = $args{import_error};
927 $error = 'general.unknown' if $evt and not $error;
934 # since an error occurred, there's no guarantee the transaction wasn't
935 # rolled back. force a rollback and create a new editor.
937 $e = new_editor(xact => 1);
938 $rec->import_error($error);
941 my $detail = sprintf("%s : %s", $evt->{textcode}, substr($evt->{desc}, 0, 140));
942 $rec->error_detail($detail);
945 my $method = 'update_vandelay_queued_bib_record';
946 $method =~ s/bib/authority/ if $args{type} eq 'auth';
947 $e->$method($rec) and $e->commit or $e->rollback;
951 $rec->clear_import_error;
952 $rec->clear_error_detail;
957 # requested queued record was not found
962 if($args{report_all} or ($args{progress} % $args{step}) == 0) {
964 $args{conn}->respond({
965 total => $args{total},
966 progress => $args{progress},
967 imported => ($rec) ? $rec->id : undef,
971 # report often at first, climb quickly, then hold steady
972 $args{step} *= 2 unless $args{step} == 256;
980 __PACKAGE__->register_method(
981 api_name => "open-ils.vandelay.bib_queue.owner.retrieve",
982 method => 'owner_queue_retrieve',
988 __PACKAGE__->register_method(
989 api_name => "open-ils.vandelay.authority_queue.owner.retrieve",
990 method => 'owner_queue_retrieve',
994 record_type => 'auth'
997 sub owner_queue_retrieve {
998 my($self, $conn, $auth, $owner_id, $filters) = @_;
999 my $e = new_editor(authtoken => $auth, xact => 1);
1000 return $e->die_event unless $e->checkauth;
1001 $owner_id = $e->requestor->id; # XXX add support for viewing other's queues?
1004 my $search = {owner => $owner_id};
1005 $search->{$_} = $filters->{$_} for keys %$filters;
1007 if($self->{record_type} eq 'bib') {
1008 $queues = $e->search_vandelay_bib_queue(
1009 [$search, {order_by => {vbq => 'evergreen.lowercase(name)'}}]);
1011 $queues = $e->search_vandelay_authority_queue(
1012 [$search, {order_by => {vaq => 'evergreen.lowercase(name)'}}]);
1014 $conn->respond($_) for @$queues;
1019 __PACKAGE__->register_method(
1020 api_name => "open-ils.vandelay.bib_queue.delete",
1021 method => "delete_queue",
1024 record_type => 'bib'
1026 __PACKAGE__->register_method(
1027 api_name => "open-ils.vandelay.auth_queue.delete",
1028 method => "delete_queue",
1031 record_type => 'auth'
1035 my($self, $conn, $auth, $q_id) = @_;
1036 my $e = new_editor(xact => 1, authtoken => $auth);
1037 return $e->die_event unless $e->checkauth;
1038 if($self->{record_type} eq 'bib') {
1039 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE');
1040 my $queue = $e->retrieve_vandelay_bib_queue($q_id)
1041 or return $e->die_event;
1042 $e->delete_vandelay_bib_queue($queue)
1043 or return $e->die_event;
1045 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE');
1046 my $queue = $e->retrieve_vandelay_authority_queue($q_id)
1047 or return $e->die_event;
1048 $e->delete_vandelay_authority_queue($queue)
1049 or return $e->die_event;
1056 __PACKAGE__->register_method(
1057 api_name => "open-ils.vandelay.queued_bib_record.html",
1058 method => 'queued_record_html',
1062 record_type => 'bib'
1064 __PACKAGE__->register_method(
1065 api_name => "open-ils.vandelay.queued_authority_record.html",
1066 method => 'queued_record_html',
1070 record_type => 'auth'
1073 sub queued_record_html {
1074 my($self, $conn, $auth, $rec_id) = @_;
1075 my $e = new_editor(xact=>1,authtoken => $auth);
1076 return $e->die_event unless $e->checkauth;
1078 if($self->{record_type} eq 'bib') {
1079 $rec = $e->retrieve_vandelay_queued_bib_record($rec_id)
1080 or return $e->die_event;
1082 $rec = $e->retrieve_vandelay_queued_authority_record($rec_id)
1083 or return $e->die_event;
1087 return $U->simplereq(
1089 'open-ils.search.biblio.record.html', undef, 1, $rec->marc);
1093 __PACKAGE__->register_method(
1094 api_name => "open-ils.vandelay.bib_queue.summary.retrieve",
1095 method => 'retrieve_queue_summary',
1099 record_type => 'bib'
1101 __PACKAGE__->register_method(
1102 api_name => "open-ils.vandelay.auth_queue.summary.retrieve",
1103 method => 'retrieve_queue_summary',
1107 record_type => 'auth'
1110 sub retrieve_queue_summary {
1111 my($self, $conn, $auth, $queue_id) = @_;
1112 my $e = new_editor(xact=>1, authtoken => $auth);
1113 return $e->die_event unless $e->checkauth;
1116 my $type = $self->{record_type};
1117 if($type eq 'bib') {
1118 $queue = $e->retrieve_vandelay_bib_queue($queue_id)
1119 or return $e->die_event;
1121 $queue = $e->retrieve_vandelay_authority_queue($queue_id)
1122 or return $e->die_event;
1125 my $evt = check_queue_perms($e, $type, $queue);
1126 return $evt if $evt;
1128 my $search = 'search_vandelay_queued_bib_record';
1129 $search =~ s/bib/authority/ if $type ne 'bib';
1133 total => scalar(@{$e->$search({queue => $queue_id}, {idlist=>1})}),
1134 imported => scalar(@{$e->$search({queue => $queue_id, import_time => {'!=' => undef}}, {idlist=>1})}),
1137 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
1138 $summary->{rec_import_errors} = $e->json_query({
1139 select => {$class => [{alias => 'count', column => 'id', transform => 'count', aggregate => 1}]},
1141 where => {queue => $queue_id, import_error => {'!=' => undef}}
1144 if($type eq 'bib') {
1147 select => {vii => [{alias => 'count', column => 'id', transform => 'count', aggregate => 1}]},
1152 select => {vqbr => ['id']},
1154 where => {queue => $queue_id}
1160 $summary->{total_items} = $e->json_query($query)->[0]->{count};
1161 $query->{where}->{import_error} = {'!=' => undef};
1162 $summary->{item_import_errors} = $e->json_query($query)->[0]->{count};
1168 # --------------------------------------------------------------------------------
1169 # Given a list of queued record IDs, imports all items attached to those records
1170 # --------------------------------------------------------------------------------
1171 sub import_record_asset_list_impl {
1172 my($conn, $rec_ids, $requestor) = @_;
1174 my $roe = new_editor(xact=> 1, requestor => $requestor);
1178 total => scalar(@$rec_ids),
1179 step => 1, # how often to respond
1184 for my $rec_id (@$rec_ids) {
1185 my $rec = $roe->retrieve_vandelay_queued_bib_record($rec_id);
1186 next unless $rec and $rec->import_time;
1187 my $item_ids = $roe->search_vandelay_import_item({record => $rec->id}, {idlist=>1});
1189 for my $item_id (@$item_ids) {
1190 my $e = new_editor(requestor => $requestor, xact => 1);
1191 my $item = $e->retrieve_vandelay_import_item($item_id);
1192 $report_args{progress}++;
1193 $report_args{import_item} = $item;
1194 $report_args{e} = $e;
1196 # --------------------------------------------------------------------------------
1197 # Find or create the volume
1198 # --------------------------------------------------------------------------------
1200 OpenILS::Application::Cat::AssetCommon->find_or_create_volume(
1201 $e, $item->call_number, $rec->imported_as, $item->owning_lib);
1204 respond_with_status(%report_args, evt => $evt);
1208 # --------------------------------------------------------------------------------
1209 # Create the new copy
1210 # --------------------------------------------------------------------------------
1211 my $copy = Fieldmapper::asset::copy->new;
1212 $copy->loan_duration(2);
1213 $copy->fine_level(2);
1214 $copy->barcode($item->barcode);
1215 $copy->location($item->location);
1216 $copy->circ_lib($item->circ_lib || $item->owning_lib);
1217 $copy->status( defined($item->status) ? $item->status : OILS_COPY_STATUS_IN_PROCESS );
1218 $copy->circulate($item->circulate);
1219 $copy->deposit($item->deposit);
1220 $copy->deposit_amount($item->deposit_amount);
1221 $copy->ref($item->ref);
1222 $copy->holdable($item->holdable);
1223 $copy->price($item->price);
1224 $copy->circ_as_type($item->circ_as_type);
1225 $copy->alert_message($item->alert_message);
1226 $copy->opac_visible($item->opac_visible);
1227 $copy->circ_modifier($item->circ_modifier);
1229 # --------------------------------------------------------------------------------
1230 # see if a valid circ_modifier was provided
1231 # --------------------------------------------------------------------------------
1232 if($copy->circ_modifier and not $e->search_config_circ_modifier({code=>$item->circ_modifier})->[0]) {
1233 respond_with_status(
1235 evt => $e->die_event,
1236 import_error => 'import.item.invalid.circ_modifier'
1241 if($copy->location and not $e->retrieve_asset_copy_location($copy->location)) {
1242 respond_with_status(
1244 evt => $e->die_event,
1245 import_error => 'import.item.invalid.location'
1250 if($evt = OpenILS::Application::Cat::AssetCommon->create_copy($e, $vol, $copy)) {
1251 respond_with_status(
1254 import_error => ($evt->{textcode} eq 'ITEM_BARCODE_EXISTS') ?
1255 'import.item.duplicate.barcode' : undef
1260 # --------------------------------------------------------------------------------
1262 # --------------------------------------------------------------------------------
1263 $evt = OpenILS::Application::Cat::AssetCommon->create_copy_note(
1264 $e, $copy, '', $item->pub_note, 1) if $item->pub_note;
1267 respond_with_status(%report_args, evt => $evt);
1271 $evt = OpenILS::Application::Cat::AssetCommon->create_copy_note(
1272 $e, $copy, '', $item->priv_note, 1) if $item->priv_note;
1275 respond_with_status(%report_args, evt => $evt);
1279 # --------------------------------------------------------------------------------
1280 # Item import succeeded
1281 # --------------------------------------------------------------------------------
1283 $report_args{in_count}++;
1284 respond_with_status(%report_args, imported_as => $copy->id);
1285 $logger->info("vl: successfully imported item " . $item->barcode);
1293 sub respond_with_status {
1297 # If the import failed, track the failure reason
1299 my $error = $args{import_error};
1300 my $evt = $args{evt};
1302 if($error or $evt) {
1304 my $item = $args{import_item};
1305 $logger->info("vl: unable to import item " . $item->barcode);
1307 $error ||= 'general.unknown';
1308 $item->import_error($error);
1311 my $detail = sprintf("%s : %s", $evt->{textcode}, substr($evt->{desc}, 0, 140));
1312 $item->error_detail($detail);
1315 # state of the editor is unknown at this point. Force a rollback and start over.
1317 $e = new_editor(xact => 1);
1318 $e->update_vandelay_import_item($item);
1322 return unless $args{report_all} or ($args{progress} % $args{step}) == 0;
1323 $args{step} *= 2 unless $args{step} == 256;
1325 $args{conn}->respond({
1326 map { $_ => $args{$_} } qw/total progress success_count/,
1331 __PACKAGE__->register_method(
1332 api_name => "open-ils.vandelay.match_set.get_tree",
1333 method => "match_set_get_tree",
1337 desc => q/For a given vms object, return a tree of match set points
1338 represented by a vmsp object with recursively fleshed
1343 sub match_set_get_tree {
1344 my ($self, $conn, $authtoken, $match_set_id) = @_;
1346 $match_set_id = int($match_set_id) or return;
1348 my $e = new_editor("authtoken" => $authtoken);
1349 $e->checkauth or return $e->die_event;
1351 my $set = $e->retrieve_vandelay_match_set($match_set_id) or
1352 return $e->die_event;
1354 $e->allowed("ADMIN_IMPORT_MATCH_SET", $set->owner) or
1355 return $e->die_event;
1357 my $tree = $e->search_vandelay_match_set_point([
1358 {"match_set" => $match_set_id, "parent" => undef},
1359 {"flesh" => -1, "flesh_fields" => {"vmsp" => ["children"]}}
1360 ]) or return $e->die_event;
1366 __PACKAGE__->register_method(
1367 api_name => "open-ils.vandelay.match_set.update",
1368 method => "match_set_update_tree",
1372 desc => q/Replace any vmsp objects associated with a given (by ID) vms
1373 with the given objects (recursively fleshed vmsp tree)./
1377 sub _walk_new_vmsp {
1378 my ($e, $match_set_id, $node, $parent_id) = @_;
1380 my $point = new Fieldmapper::vandelay::match_set_point;
1381 $point->parent($parent_id);
1382 $point->match_set($match_set_id);
1383 $point->$_($node->$_) for (qw/bool_op svf tag subfield negate quality/);
1385 $e->create_vandelay_match_set_point($point) or return $e->die_event;
1387 $parent_id = $e->data->id;
1388 if ($node->children && @{$node->children}) {
1389 for (@{$node->children}) {
1390 return $e->die_event if
1391 _walk_new_vmsp($e, $match_set_id, $_, $parent_id);
1398 sub match_set_update_tree {
1399 my ($self, $conn, $authtoken, $match_set_id, $tree) = @_;
1401 my $e = new_editor("xact" => 1, "authtoken" => $authtoken);
1402 $e->checkauth or return $e->die_event;
1404 my $set = $e->retrieve_vandelay_match_set($match_set_id) or
1405 return $e->die_event;
1407 $e->allowed("ADMIN_IMPORT_MATCH_SET", $set->owner) or
1408 return $e->die_event;
1410 my $existing = $e->search_vandelay_match_set_point([
1411 {"match_set" => $match_set_id},
1412 {"order_by" => {"vmsp" => "id DESC"}}
1413 ]) or return $e->die_event;
1415 foreach (@$existing) {
1416 $e->delete_vandelay_match_set_point($_) or return $e->die_event;
1419 _walk_new_vmsp($e, $match_set_id, $tree);
1421 $e->commit or return $e->die_event;