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/;
12 use OpenILS::Utils::Normalize qw/clean_marc/;
15 use MARC::File::XML ( BinaryEncoding => 'UTF-8' );
16 use Time::HiRes qw(time);
17 use OpenSRF::Utils::Logger qw/$logger/;
20 use OpenILS::Const qw/:const/;
21 use OpenILS::Application::AppUtils;
22 use OpenILS::Application::Cat::BibCommon;
23 use OpenILS::Application::Cat::AuthCommon;
24 use OpenILS::Application::Cat::AssetCommon;
25 use OpenILS::Application::Cat::Merge;
26 my $U = 'OpenILS::Application::AppUtils';
28 # A list of LDR/06 values from http://loc.gov/marc
55 # --------------------------------------------------------------------------------
58 sub create_bib_queue {
65 my $match_set = shift;
66 my $import_def = shift;
67 my $match_bucket = shift;
69 my $e = new_editor(authtoken => $auth, xact => 1);
71 return $e->die_event unless $e->checkauth;
72 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE');
73 $owner ||= $e->requestor->id;
75 if ($e->search_vandelay_bib_queue( {name => $name, owner => $owner, queue_type => $type})->[0]) {
77 return OpenILS::Event->new('BIB_QUEUE_EXISTS')
80 my $queue = new Fieldmapper::vandelay::bib_queue();
81 $queue->name( $name );
82 $queue->owner( $owner );
83 $queue->queue_type( $type ) if ($type);
84 $queue->item_attr_def( $import_def ) if ($import_def);
85 $queue->match_set($match_set) if $match_set;
86 $queue->match_bucket($match_bucket) if $match_bucket;
88 my $new_q = $e->create_vandelay_bib_queue( $queue );
89 return $e->die_event unless ($new_q);
94 __PACKAGE__->register_method(
95 api_name => "open-ils.vandelay.bib_queue.create",
96 method => "create_bib_queue",
102 sub create_auth_queue {
109 my $match_set = shift;
111 my $e = new_editor(authtoken => $auth, xact => 1);
113 return $e->die_event unless $e->checkauth;
114 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE');
115 $owner ||= $e->requestor->id;
117 if ($e->search_vandelay_authority_queue({name => $name, owner => $owner, queue_type => $type})->[0]) {
119 return OpenILS::Event->new('AUTH_QUEUE_EXISTS')
122 my $queue = new Fieldmapper::vandelay::authority_queue();
123 $queue->name( $name );
124 $queue->owner( $owner );
125 $queue->queue_type( $type ) if ($type);
127 my $new_q = $e->create_vandelay_authority_queue( $queue );
128 $e->die_event unless ($new_q);
133 __PACKAGE__->register_method(
134 api_name => "open-ils.vandelay.authority_queue.create",
135 method => "create_auth_queue",
140 sub add_record_to_bib_queue {
147 my $bib_source = shift;
149 my $e = new_editor(authtoken => $auth, xact => 1);
151 $queue = $e->retrieve_vandelay_bib_queue($queue);
153 return $e->die_event unless $e->checkauth;
154 return $e->die_event unless
155 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
156 $e->allowed('CREATE_BIB_IMPORT_QUEUE'));
158 my $new_rec = _add_bib_rec($e, $marc, $queue->id, $purpose, $bib_source);
160 return $e->die_event unless ($new_rec);
164 __PACKAGE__->register_method(
165 api_name => "open-ils.vandelay.queued_bib_record.create",
166 method => "add_record_to_bib_queue",
176 my $bib_source = shift;
178 my $rec = new Fieldmapper::vandelay::queued_bib_record();
180 $rec->queue( $queue );
181 $rec->purpose( $purpose ) if ($purpose);
182 $rec->bib_source($bib_source);
184 return $e->create_vandelay_queued_bib_record( $rec );
187 sub add_record_to_authority_queue {
195 my $e = new_editor(authtoken => $auth, xact => 1);
197 $queue = $e->retrieve_vandelay_authority_queue($queue);
199 return $e->die_event unless $e->checkauth;
200 return $e->die_event unless
201 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
202 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE'));
204 my $new_rec = _add_auth_rec($e, $marc, $queue->id, $purpose);
206 return $e->die_event unless ($new_rec);
210 __PACKAGE__->register_method(
211 api_name => "open-ils.vandelay.queued_authority_record.create",
212 method => "add_record_to_authority_queue",
223 my $rec = new Fieldmapper::vandelay::queued_authority_record();
225 $rec->queue( $queue );
226 $rec->purpose( $purpose ) if ($purpose);
228 return $e->create_vandelay_queued_authority_record( $rec );
235 my $fingerprint = shift || '';
236 my $queue_id = shift;
238 my $filename = shift;
239 my $bib_source = shift;
241 my $e = new_editor(authtoken => $auth, xact => 1);
242 return $e->die_event unless $e->checkauth;
245 my $type = $self->{record_type};
248 $queue = $e->retrieve_vandelay_bib_queue($queue_id) or return $e->die_event;
250 $queue = $e->retrieve_vandelay_authority_queue($queue_id) or return $e->die_event;
253 my $evt = check_queue_perms($e, $type, $queue);
254 return $evt if ($evt);
256 my $cache = new OpenSRF::Utils::Cache();
259 my $data = $cache->get_cache('vandelay_import_spool_' . $fingerprint);
260 $purpose = $data->{purpose};
261 $filename = $data->{path};
262 $bib_source = $data->{bib_source};
265 unless(-r $filename) {
266 $logger->error("unable to read MARC file $filename");
267 return -1; # make this an event XXX
270 $logger->info("vandelay spooling $fingerprint purpose=$purpose file=$filename");
272 my $marctype = 'USMARC';
275 $marctype = 'XML' if (getc(F) =~ /^\D/o);
278 my $batch = new MARC::Batch ($marctype, $filename);
281 my $response_scale = 10;
284 while (try { $r = $batch->next } otherwise { $r = -1 }) {
286 $logger->warn("Processing of record $count in set $filename failed. Skipping this record");
290 $logger->info("processing record $count");
293 my $xml = clean_marc($r);
296 # Check the leader to ensure we've got something resembling the expected
297 # Allow spaces to give records the benefit of the doubt
298 my $ldr_type = substr($r->leader(), 6, 1);
299 if ($type eq 'bib' && ($record_types{$ldr_type}) eq 'bib' || $ldr_type eq ' ') {
300 $qrec = _add_bib_rec( $e, $xml, $queue_id, $purpose, $bib_source ) or return $e->die_event;
301 } elsif ($type eq 'auth' && ($record_types{$ldr_type}) eq 'auth' || $ldr_type eq ' ') {
302 $qrec = _add_auth_rec( $e, $xml, $queue_id, $purpose ) or return $e->die_event;
304 # I don't know how to handle this type; rock on
305 $logger->error("In process_spool(), type was $type and leader type was $ldr_type ; not currently supported");
309 if($self->api_name =~ /stream_results/ and $qrec) {
310 $client->respond($qrec->id)
312 $client->respond($count) if (++$count % $response_scale) == 0;
313 $response_scale *= 10 if ($count == ($response_scale * 10));
317 $logger->warn("Encountered a bad record at Vandelay ingest: ".$error);
323 $cache->delete_cache('vandelay_import_spool_' . $fingerprint) if $fingerprint;
327 __PACKAGE__->register_method(
328 api_name => "open-ils.vandelay.bib.process_spool",
329 method => "process_spool",
335 __PACKAGE__->register_method(
336 api_name => "open-ils.vandelay.auth.process_spool",
337 method => "process_spool",
341 record_type => 'auth'
344 __PACKAGE__->register_method(
345 api_name => "open-ils.vandelay.bib.process_spool.stream_results",
346 method => "process_spool",
353 __PACKAGE__->register_method(
354 api_name => "open-ils.vandelay.auth.process_spool.stream_results",
355 method => "process_spool",
360 record_type => 'auth'
363 __PACKAGE__->register_method(
364 api_name => "open-ils.vandelay.bib_queue.records.retrieve",
365 method => 'retrieve_queued_records',
371 __PACKAGE__->register_method(
372 api_name => "open-ils.vandelay.bib_queue.records.retrieve.export.print",
373 method => 'retrieve_queued_records',
379 __PACKAGE__->register_method(
380 api_name => "open-ils.vandelay.bib_queue.records.retrieve.export.csv",
381 method => 'retrieve_queued_records',
387 __PACKAGE__->register_method(
388 api_name => "open-ils.vandelay.bib_queue.records.retrieve.export.email",
389 method => 'retrieve_queued_records',
396 __PACKAGE__->register_method(
397 api_name => "open-ils.vandelay.auth_queue.records.retrieve",
398 method => 'retrieve_queued_records',
402 record_type => 'auth'
404 __PACKAGE__->register_method(
405 api_name => "open-ils.vandelay.auth_queue.records.retrieve.export.print",
406 method => 'retrieve_queued_records',
410 record_type => 'auth'
412 __PACKAGE__->register_method(
413 api_name => "open-ils.vandelay.auth_queue.records.retrieve.export.csv",
414 method => 'retrieve_queued_records',
418 record_type => 'auth'
420 __PACKAGE__->register_method(
421 api_name => "open-ils.vandelay.auth_queue.records.retrieve.export.email",
422 method => 'retrieve_queued_records',
426 record_type => 'auth'
429 __PACKAGE__->register_method(
430 api_name => "open-ils.vandelay.bib_queue.records.matches.retrieve",
431 method => 'retrieve_queued_records',
435 record_type => 'bib',
437 desc => q/Only retrieve queued bib records that have matches against existing records/
440 __PACKAGE__->register_method(
441 api_name => "open-ils.vandelay.auth_queue.records.matches.retrieve",
442 method => 'retrieve_queued_records',
446 record_type => 'auth',
448 desc => q/Only retrieve queued authority records that have matches against existing records/
452 sub retrieve_queued_records {
453 my($self, $conn, $auth, $queue_id, $options) = @_;
456 my $limit = $$options{limit} || 20;
457 my $offset = $$options{offset} || 0;
458 my $type = $self->{record_type};
460 my $e = new_editor(authtoken => $auth, xact => 1);
461 return $e->die_event unless $e->checkauth;
465 $queue = $e->retrieve_vandelay_bib_queue($queue_id) or return $e->die_event;
467 $queue = $e->retrieve_vandelay_authority_queue($queue_id) or return $e->die_event;
469 my $evt = check_queue_perms($e, $type, $queue);
470 return $evt if ($evt);
472 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
473 my $mclass = $type eq 'bib' ? 'vbm' : 'vam';
478 column => 'eg_record',
484 where => {queue => $queue_id},
489 if($self->api_name =~ /export/) {
490 delete $query->{limit};
491 delete $query->{offset};
494 $query->{where}->{import_time} = undef if $$options{non_imported};
496 if($$options{with_import_error}) {
498 $query->{from} = {$class => {vii => {type => 'left'}}};
499 $query->{where}->{'-or'} = [
500 {'+vqbr' => {import_error => {'!=' => undef}}},
501 {'+vii' => {import_error => {'!=' => undef}}}
506 if($$options{with_rec_import_error}) {
507 $query->{where}->{import_error} = {'!=' => undef};
509 } elsif( $$options{with_item_import_error} and $type eq 'bib') {
511 $query->{from} = {$class => 'vii'};
512 $query->{where}->{'+vii'} = {import_error => {'!=' => undef}};
516 if($self->api_name =~ /matches/) {
517 # find only records that have matches
518 $query->{from} = {$class => {$mclass => {type => 'right'}}};
520 # join to mclass for sorting (see below)
521 $query->{from} = {$class => {$mclass => {type => 'left'}}};
524 # order by the matched bib records to group like queued records
525 $query->{order_by} = [
526 {class => $mclass, field => 'eg_record', transform => 'min'},
527 {class => $class, field => 'id'}
530 my $record_ids = $e->json_query($query);
532 my $retrieve = ($type eq 'bib') ?
533 'retrieve_vandelay_queued_bib_record' : 'retrieve_vandelay_queued_authority_record';
534 my $search = ($type eq 'bib') ?
535 'search_vandelay_queued_bib_record' : 'search_vandelay_queued_authority_record';
537 if ($self->api_name =~ /export/) {
538 my $rec_list = $e->$search({id => [map { $_->{id} } @$record_ids]}, {substream => 1});
539 if ($self->api_name =~ /print/) {
542 return $U->fire_object_event(
544 'vandelay.queued_'.$type.'_record.print',
549 } elsif ($self->api_name =~ /csv/) {
552 return $U->fire_object_event(
554 'vandelay.queued_'.$type.'_record.csv',
559 } elsif ($self->api_name =~ /email/) {
561 $conn->respond_complete(1);
563 for my $rec (@$rec_list) {
564 $U->create_events_for_hook(
565 'vandelay.queued_'.$type.'_record.email',
567 $e->requestor->home_ou,
576 for my $rec_id (@$record_ids) {
577 my $flesh = ['attributes', 'matches'];
578 push(@$flesh, 'import_items') if $$options{flesh_import_items};
579 my $params = {flesh => 1, flesh_fields => {$class => $flesh}};
580 my $rec = $e->$retrieve([$rec_id->{id}, $params]);
581 $rec->clear_marc if $$options{clear_marc};
582 $conn->respond($rec);
590 __PACKAGE__->register_method(
591 api_name => 'open-ils.vandelay.import_item.queue.retrieve',
592 method => 'retrieve_queue_import_items',
598 Returns Import Item (vii) objects for the selected queue.
600 with_import_error : only return items that failed to import
603 __PACKAGE__->register_method(
604 api_name => 'open-ils.vandelay.import_item.queue.export.print',
605 method => 'retrieve_queue_import_items',
611 Returns template-generated printable output of Import Item (vii) objects for the selected queue.
613 with_import_error : only return items that failed to import
616 __PACKAGE__->register_method(
617 api_name => 'open-ils.vandelay.import_item.queue.export.csv',
618 method => 'retrieve_queue_import_items',
624 Returns template-generated CSV output of Import Item (vii) objects for the selected queue.
626 with_import_error : only return items that failed to import
629 __PACKAGE__->register_method(
630 api_name => 'open-ils.vandelay.import_item.queue.export.email',
631 method => 'retrieve_queue_import_items',
637 Emails template-generated output of Import Item (vii) objects for the selected queue.
639 with_import_error : only return items that failed to import
643 sub retrieve_queue_import_items {
644 my($self, $conn, $auth, $q_id, $options) = @_;
647 my $limit = $$options{limit} || 20;
648 my $offset = $$options{offset} || 0;
650 my $e = new_editor(authtoken => $auth);
651 return $e->event unless $e->checkauth;
653 my $queue = $e->retrieve_vandelay_bib_queue($q_id) or return $e->event;
654 my $evt = check_queue_perms($e, 'bib', $queue);
658 select => {vii => ['id']},
666 filter => {id => $q_id}
672 order_by => {'vii' => ['record','id']},
676 if($self->api_name =~ /export/) {
677 delete $query->{limit};
678 delete $query->{offset};
681 $query->{where} = {'+vii' => {import_error => {'!=' => undef}}}
682 if $$options{with_import_error};
684 my $items = $e->json_query($query);
685 my $item_list = $e->search_vandelay_import_item({id => [map { $_->{id} } @$items]});
686 if ($self->api_name =~ /export/) {
687 if ($self->api_name =~ /print/) {
689 return $U->fire_object_event(
691 'vandelay.import_items.print',
696 } elsif ($self->api_name =~ /csv/) {
698 return $U->fire_object_event(
700 'vandelay.import_items.csv',
705 } elsif ($self->api_name =~ /email/) {
707 $conn->respond_complete(1);
709 for my $item (@$item_list) {
710 $U->create_events_for_hook(
711 'vandelay.import_items.email',
713 $e->requestor->home_ou,
722 for my $item (@$item_list) {
723 $conn->respond($item);
730 sub check_queue_perms {
731 my($e, $type, $queue) = @_;
732 if ($type eq 'bib') {
733 return $e->die_event unless
734 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
735 $e->allowed('CREATE_BIB_IMPORT_QUEUE'));
737 return $e->die_event unless
738 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
739 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE'));
745 __PACKAGE__->register_method(
746 api_name => "open-ils.vandelay.bib_record.list.import",
747 method => 'import_record_list',
754 __PACKAGE__->register_method(
755 api_name => "open-ils.vandelay.auth_record.list.import",
756 method => 'import_record_list',
760 record_type => 'auth'
763 sub import_record_list {
764 my($self, $conn, $auth, $rec_ids, $args) = @_;
765 my $e = new_editor(authtoken => $auth, xact => 1);
766 return $e->die_event unless $e->checkauth;
768 my $err = import_record_list_impl($self, $conn, $rec_ids, $e->requestor, $args);
769 try {$e->rollback} otherwise {};
771 return {complete => 1};
775 __PACKAGE__->register_method(
776 api_name => "open-ils.vandelay.bib_queue.import",
777 method => 'import_queue',
782 record_type => 'bib',
785 Attempts to import all non-imported records for the selected queue.
786 Will also attempt import of all non-imported items.
791 __PACKAGE__->register_method(
792 api_name => "open-ils.vandelay.auth_queue.import",
793 method => 'import_queue',
798 record_type => 'auth'
802 my($self, $conn, $auth, $q_id, $options) = @_;
803 my $e = new_editor(authtoken => $auth, xact => 1);
804 return $e->die_event unless $e->checkauth;
806 my $type = $self->{record_type};
807 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
809 # First, collect the not-yet-imported records
810 my $query = {queue => $q_id, import_time => undef};
811 my $search = ($type eq 'bib') ?
812 'search_vandelay_queued_bib_record' :
813 'search_vandelay_queued_authority_record';
814 my $rec_ids = $e->$search($query, {idlist => 1});
816 # Now add any imported records that have un-imported items
819 my $item_recs = $e->json_query({
820 select => {vqbr => ['id']},
821 from => {vqbr => 'vii'},
825 import_time => {'!=' => undef}
827 '+vii' => {import_time => undef}
831 push(@$rec_ids, map {$_->{id}} @$item_recs);
834 my $err = import_record_list_impl($self, $conn, $rec_ids, $e->requestor, $options);
835 try {$e->rollback} otherwise {}; # only using this to make the read authoritative -- don't die from it
837 return {complete => 1};
840 # returns a list of queued record IDs for a given queue that
841 # have at least one entry in the match table
843 sub queued_records_with_matches {
844 my($e, $type, $q_id, $limit, $offset, $filter) = @_;
846 my $match_class = 'vbm';
847 my $rec_class = 'vqbr';
848 if($type eq 'auth') {
849 $match_class = 'vam';
854 $filter->{queue} = $q_id;
858 select => {$match_class => ['queued_record']},
863 fkey => 'queued_record',
870 if($limit or defined $offset) {
873 $query->{limit} = $limit;
874 $query->{offset} = $offset;
877 my $data = $e->json_query($query);
878 return [ map {$_->{queued_record}} @$data ];
882 # cache of import item org unit settings.
883 # used in apply_import_item_defaults() below,
884 # but reset on each call to import_record_list_impl()
885 my %item_defaults_cache;
887 sub import_record_list_impl {
888 my($self, $conn, $rec_ids, $requestor, $args) = @_;
890 my $overlay_map = $args->{overlay_map} || {};
891 my $type = $self->{record_type};
893 %item_defaults_cache = ();
899 total => scalar(@$rec_ids),
900 report_all => $$args{report_all}
903 $conn->max_chunk_count(1) if $$args{report_all};
905 my $auto_overlay_exact = $$args{auto_overlay_exact};
906 my $auto_overlay_1match = $$args{auto_overlay_1match};
907 my $auto_overlay_best = $$args{auto_overlay_best_match};
908 my $match_quality_ratio = $$args{match_quality_ratio};
909 my $merge_profile = $$args{merge_profile};
910 my $ft_merge_profile = $$args{fall_through_merge_profile};
911 my $bib_source = $$args{bib_source};
912 my $import_no_match = $$args{import_no_match};
913 my $strip_grps = $$args{strip_field_groups}; # bib-only
915 my $overlay_func = 'vandelay.overlay_bib_record';
916 my $auto_overlay_func = 'vandelay.auto_overlay_bib_record';
917 my $auto_overlay_best_func = 'vandelay.auto_overlay_bib_record_with_best'; # XXX bib-only
918 my $retrieve_func = 'retrieve_vandelay_queued_bib_record';
919 my $update_func = 'update_vandelay_queued_bib_record';
920 my $search_func = 'search_vandelay_queued_bib_record';
921 my $retrieve_queue_func = 'retrieve_vandelay_bib_queue';
922 my $update_queue_func = 'update_vandelay_bib_queue';
923 my $delete_queue_func = 'delete_vandelay_bib_queue';
924 my $rec_class = 'vqbr';
926 my $editor = new_editor();
929 my $sources = $editor->search_config_bib_source({id => {'!=' => undef}});
930 $bib_sources{$_->id} = $_->source for @$sources;
932 if($type eq 'auth') {
933 $overlay_func =~ s/bib/auth/o;
934 $auto_overlay_func = s/bib/auth/o;
935 $retrieve_func =~ s/bib/authority/o;
936 $retrieve_queue_func =~ s/bib/authority/o;
937 $update_queue_func =~ s/bib/authority/o;
938 $update_func =~ s/bib/authority/o;
939 $search_func =~ s/bib/authority/o;
940 $delete_queue_func =~ s/bib/authority/o;
944 my $new_rec_perm_cache;
946 for my $rec_id (@$rec_ids) {
949 my $overlay_target = $overlay_map->{$rec_id};
951 my $e = new_editor(xact => 1);
952 $e->requestor($requestor);
954 $$report_args{e} = $e;
955 $$report_args{evt} = undef;
956 $$report_args{import_error} = undef;
957 $$report_args{no_import} = 0;
959 my $rec = $e->$retrieve_func([
962 flesh_fields => { $rec_class => ['matches']},
967 $$report_args{evt} = $e->event;
968 finish_rec_import_attempt($report_args);
972 if($rec->import_time) {
973 # if the record is already imported, that means it may have
974 # un-imported copies. Add to success list for later processing.
975 push(@success_rec_ids, $rec_id);
980 $$report_args{rec} = $rec;
981 $queues{$rec->queue} = 1;
986 if ($type eq 'bib') {
987 # strip configured / selected MARC tags from inbound records
989 my $marcdoc = XML::LibXML->new->parse_string($rec->marc);
990 $rec->marc($U->strip_marc_fields($e, $marcdoc, $strip_grps));
992 unless ($e->$update_func($rec)) {
993 $$report_args{evt} = $e->die_event;
994 finish_rec_import_attempt($report_args);
999 if(defined $overlay_target) {
1000 # Caller chose an explicit overlay target
1002 my $res = $e->json_query(
1013 if($res and ($res = $res->[0])) {
1015 if($res->{$overlay_func} eq 't') {
1016 $logger->info("vl: $type direct overlay succeeded for queued rec ".
1017 "$rec_id and overlay target $overlay_target");
1019 $rec->imported_as($overlay_target);
1024 $logger->error("vl: Error attempting overlay with func=$overlay_func, profile=$merge_profile, record=$rec_id");
1029 if($auto_overlay_1match) { # overlay if there is exactly 1 match
1031 my %match_recs = map { $_->eg_record => 1 } @{$rec->matches};
1033 if( scalar(keys %match_recs) == 1) { # all matches point to the same record
1035 ($imported, $error, $rec) = try_auto_overlay(
1038 $auto_overlay_best_func,
1042 $match_quality_ratio,
1049 if(!$imported and !$error and $auto_overlay_exact and scalar(@{$rec->matches}) == 1 ) {
1051 # caller says to overlay if there is an /exact/ match
1052 # $auto_overlay_func only proceeds and returns true on exact matches
1054 my $res = $e->json_query(
1064 if($res and ($res = $res->[0])) {
1066 if($res->{$auto_overlay_func} eq 't') {
1067 $logger->info("vl: $type auto-overlay succeeded for queued rec $rec_id");
1070 # re-fetch the record to pick up the imported_as value from the DB
1071 $$report_args{rec} = $rec = $e->$retrieve_func([
1072 $rec_id, {flesh => 1, flesh_fields => {$rec_class => ['matches']}}]);
1075 $logger->info("vl: $type auto-overlay failed for queued rec $rec_id");
1080 $logger->error("vl: Error attempting overlay with func=$auto_overlay_func, profile=$merge_profile, record=$rec_id");
1084 if(!$imported and !$error and $auto_overlay_best and scalar(@{$rec->matches}) > 0 ) {
1085 # caller says to overlay the best match
1087 ($imported, $error, $rec) = try_auto_overlay(
1090 $auto_overlay_best_func,
1094 $match_quality_ratio,
1100 if(!$imported and !$error and $import_no_match and scalar(@{$rec->matches}) == 0) {
1102 # No overlay / merge occurred. Do a traditional record import by creating a new record
1104 if (!$new_rec_perm_cache) {
1105 $new_rec_perm_cache = {};
1107 # all users creating new records are required to have the basic permission.
1108 # if the client requests, we can enforce extra permissions for creating new records.
1109 # for speed, check the permissions the first time then cache the result.
1111 my $perm = ($type eq 'bib') ? 'IMPORT_MARC' : 'IMPORT_AUTHORITY_MARC';
1112 my $xperm = $$args{new_rec_perm};
1113 my $rec_ou = $e->requestor->ws_ou;
1115 $new_rec_perm_cache->{evt} = $e->die_event
1116 if !$e->allowed($perm, $rec_ou) || ($xperm and !$e->allowed($xperm, $rec_ou));
1119 if ($new_rec_perm_cache->{evt}) {
1121 # a cached event won't roll back the transaction (a la die_event), but
1122 # the transaction will get rolled back in finish_rec_import_attempt() below
1123 $$report_args{evt} = $new_rec_perm_cache->{evt};
1124 $$report_args{import_error} = 'import.record.perm_failure';
1126 } else { # perm checks succeeded
1128 $logger->info("vl: creating new $type record for queued record $rec_id");
1130 if ($type eq 'bib') {
1132 $record = OpenILS::Application::Cat::BibCommon->biblio_record_xml_import(
1133 $e, $rec->marc, $bib_sources{$rec->bib_source}, undef, 1);
1135 } else { # authority record
1137 $record = OpenILS::Application::Cat::AuthCommon->import_authority_record($e, $rec->marc); #$source);
1140 if($U->event_code($record)) {
1141 $$report_args{import_error} = 'import.duplicate.tcn'
1142 if $record->{textcode} eq 'OPEN_TCN_NOT_FOUND';
1143 $$report_args{evt} = $record;
1147 $logger->info("vl: successfully imported new $type record");
1148 $rec->imported_as($record->id);
1157 $rec->import_time('now');
1158 $rec->clear_import_error;
1159 $rec->clear_error_detail;
1161 if($e->$update_func($rec)) {
1163 if($type eq 'bib') {
1165 # see if this record is linked from an acq record.
1166 my $li = $e->search_acq_lineitem(
1167 {queued_record => $rec->id, state => {'!=' => 'canceled'}})->[0];
1170 # if so, update the acq lineitem to point to the imported record
1171 $li->eg_bib_id($rec->imported_as);
1172 $$report_args{evt} = $e->die_event unless $e->update_acq_lineitem($li);
1176 push @success_rec_ids, $rec_id;
1177 finish_rec_import_attempt($report_args);
1185 $logger->info("vl: record $rec_id was not imported");
1186 $$report_args{evt} = $e->event unless $$report_args{evt};
1187 $$report_args{no_import} = 1;
1188 finish_rec_import_attempt($report_args);
1192 # see if we need to mark any queues as complete
1193 for my $q_id (keys %queues) {
1195 my $e = new_editor(xact => 1);
1196 my $remaining = $e->$search_func(
1197 [{queue => $q_id, import_time => undef}, {limit =>1}], {idlist => 1});
1199 unless(@$remaining) {
1200 my $queue = $e->$retrieve_queue_func($q_id);
1201 unless($U->is_true($queue->complete)) {
1202 $queue->complete('t');
1203 $e->$update_queue_func($queue) or return $e->die_event;
1212 import_record_asset_list_impl($conn, \@success_rec_ids, $requestor, $args) if @success_rec_ids;
1214 $conn->respond({total => $$report_args{total}, progress => $$report_args{progress}});
1219 sub try_auto_overlay {
1222 my $report_args = shift;
1223 my $overlay_func = shift;
1224 my $retrieve_func = shift;
1225 my $rec_class = shift;
1227 my $match_quality_ratio = shift;
1228 my $merge_profile = shift;
1229 my $ft_merge_profile = shift;
1234 # Find the best match and overlay if the quality ratio allows it.
1235 my $res = $e->json_query(
1241 $match_quality_ratio
1246 if($res and ($res = $res->[0])) {
1248 if($res->{$overlay_func} eq 't') {
1250 # first attempt succeeded
1255 # quality-limited merge failed with insufficient quality. If there is a
1256 # fall-through merge profile, re-do the merge with the alternate profile
1257 # and no quality restriction.
1259 if($ft_merge_profile and $match_quality_ratio > 0) {
1261 $logger->info("vl: $type auto-merge failed with profile $merge_profile; ".
1262 "re-merging with fall-through profile $ft_merge_profile");
1264 my $res = $e->json_query(
1270 0 # minimum quality not required
1275 if($res and ($res = $res->[0])) {
1277 if($res->{$overlay_func} eq 't') {
1279 # second attempt succeeded
1284 # failed to merge on second attempt
1285 $logger->info("vl: $type auto-merge with fall-through failed for queued rec $rec_id");
1289 # second attempt died
1291 $logger->error("vl: Error attempting overlay with func=$overlay_func, profile=$merge_profile, record=$rec_id");
1296 # failed to merge on first attempt, no fall-through was provided
1297 $$report_args{import_error} = 'overlay.record.quality' if $match_quality_ratio > 0;
1298 $logger->info("vl: $type auto-merge failed for queued rec $rec_id");
1304 # first attempt died
1306 $logger->error("vl: Error attempting overlay with func=$overlay_func, profile=$merge_profile, record=$rec_id");
1311 # at least 1 of the attempts succeeded
1312 $logger->info("vl: $type auto-merge succeeded for queued rec $rec_id");
1314 # re-fetch the record to pick up the imported_as value from the DB
1315 $$report_args{rec} = $e->$retrieve_func([
1316 $rec_id, {flesh => 1, flesh_fields => {$rec_class => ['matches']}}]);
1319 return ($imported, $error, $$report_args{rec});
1323 # tracks any import errors, commits the current xact, responds to the client
1324 sub finish_rec_import_attempt {
1326 my $evt = $$args{evt};
1327 my $rec = $$args{rec};
1330 my $error = $$args{import_error};
1331 $error = 'general.unknown' if $evt and not $error;
1336 if($error or $evt) {
1338 # since an error occurred, there's no guarantee the transaction wasn't
1339 # rolled back. force a rollback and create a new editor.
1341 $e = new_editor(xact => 1);
1342 $rec->import_error($error);
1345 my $detail = sprintf("%s : %s", $evt->{textcode}, substr($evt->{desc}, 0, 140));
1346 $rec->error_detail($detail);
1349 my $method = 'update_vandelay_queued_bib_record';
1350 $method =~ s/bib/authority/ if $$args{type} eq 'auth';
1351 $e->$method($rec) and $e->commit or $e->rollback;
1354 # commit the successful import
1359 # requested queued record was not found
1364 if($$args{report_all} or ($$args{progress} % $$args{step}) == 0) {
1365 $$args{conn}->respond({
1366 total => $$args{total},
1367 progress => $$args{progress},
1368 imported => ($rec) ? $rec->id : undef,
1369 import_error => $error,
1370 no_import => $$args{no_import},
1373 $$args{step} *= 2 unless $$args{step} == 256;
1383 __PACKAGE__->register_method(
1384 api_name => "open-ils.vandelay.bib_queue.owner.retrieve",
1385 method => 'owner_queue_retrieve',
1389 record_type => 'bib'
1391 __PACKAGE__->register_method(
1392 api_name => "open-ils.vandelay.authority_queue.owner.retrieve",
1393 method => 'owner_queue_retrieve',
1397 record_type => 'auth'
1400 sub owner_queue_retrieve {
1401 my($self, $conn, $auth, $owner_id, $filters) = @_;
1402 my $e = new_editor(authtoken => $auth, xact => 1);
1403 return $e->die_event unless $e->checkauth;
1404 $owner_id = $e->requestor->id; # XXX add support for viewing other's queues?
1407 my $search = {owner => $owner_id};
1408 $search->{$_} = $filters->{$_} for keys %$filters;
1410 if($self->{record_type} eq 'bib') {
1411 $queues = $e->search_vandelay_bib_queue(
1412 [$search, {order_by => {vbq => 'evergreen.lowercase(name)'}}]);
1414 $queues = $e->search_vandelay_authority_queue(
1415 [$search, {order_by => {vaq => 'evergreen.lowercase(name)'}}]);
1417 $conn->respond($_) for @$queues;
1422 __PACKAGE__->register_method(
1423 api_name => "open-ils.vandelay.bib_queue.delete",
1424 method => "delete_queue",
1427 record_type => 'bib'
1429 __PACKAGE__->register_method(
1430 api_name => "open-ils.vandelay.auth_queue.delete",
1431 method => "delete_queue",
1434 record_type => 'auth'
1438 my($self, $conn, $auth, $q_id) = @_;
1439 my $e = new_editor(xact => 1, authtoken => $auth);
1440 return $e->die_event unless $e->checkauth;
1441 if($self->{record_type} eq 'bib') {
1442 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE');
1443 my $queue = $e->retrieve_vandelay_bib_queue($q_id)
1444 or return $e->die_event;
1445 $e->delete_vandelay_bib_queue($queue)
1446 or return $e->die_event;
1448 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE');
1449 my $queue = $e->retrieve_vandelay_authority_queue($q_id)
1450 or return $e->die_event;
1451 $e->delete_vandelay_authority_queue($queue)
1452 or return $e->die_event;
1459 __PACKAGE__->register_method(
1460 api_name => "open-ils.vandelay.queued_bib_record.html",
1461 method => 'queued_record_html',
1465 record_type => 'bib'
1467 __PACKAGE__->register_method(
1468 api_name => "open-ils.vandelay.queued_authority_record.html",
1469 method => 'queued_record_html',
1473 record_type => 'auth'
1476 sub queued_record_html {
1477 my($self, $conn, $auth, $rec_id) = @_;
1478 my $e = new_editor(xact=>1,authtoken => $auth);
1479 return $e->die_event unless $e->checkauth;
1481 if($self->{record_type} eq 'bib') {
1482 $rec = $e->retrieve_vandelay_queued_bib_record($rec_id)
1483 or return $e->die_event;
1485 $rec = $e->retrieve_vandelay_queued_authority_record($rec_id)
1486 or return $e->die_event;
1490 return $U->simplereq(
1492 'open-ils.search.biblio.record.html', undef, 1, $rec->marc);
1496 __PACKAGE__->register_method(
1497 api_name => "open-ils.vandelay.bib_queue.summary.retrieve",
1498 method => 'retrieve_queue_summary',
1502 record_type => 'bib'
1504 __PACKAGE__->register_method(
1505 api_name => "open-ils.vandelay.auth_queue.summary.retrieve",
1506 method => 'retrieve_queue_summary',
1510 record_type => 'auth'
1513 sub retrieve_queue_summary {
1514 my($self, $conn, $auth, $queue_id) = @_;
1515 my $e = new_editor(xact=>1, authtoken => $auth);
1516 return $e->die_event unless $e->checkauth;
1519 my $type = $self->{record_type};
1520 if($type eq 'bib') {
1521 $queue = $e->retrieve_vandelay_bib_queue($queue_id)
1522 or return $e->die_event;
1524 $queue = $e->retrieve_vandelay_authority_queue($queue_id)
1525 or return $e->die_event;
1528 my $evt = check_queue_perms($e, $type, $queue);
1529 return $evt if $evt;
1531 my $search = 'search_vandelay_queued_bib_record';
1532 $search =~ s/bib/authority/ if $type ne 'bib';
1536 total => scalar(@{$e->$search({queue => $queue_id}, {idlist=>1})}),
1537 imported => scalar(@{$e->$search({queue => $queue_id, import_time => {'!=' => undef}}, {idlist=>1})}),
1540 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
1541 $summary->{rec_import_errors} = $e->json_query({
1542 select => {$class => [{alias => 'count', column => 'id', transform => 'count', aggregate => 1}]},
1544 where => {queue => $queue_id, import_error => {'!=' => undef}}
1547 if($type eq 'bib') {
1549 # count of all items attached to records in the queue in question
1551 select => {vii => [{alias => 'count', column => 'id', transform => 'count', aggregate => 1}]},
1556 select => {vqbr => ['id']},
1558 where => {queue => $queue_id}
1563 $summary->{total_items} = $e->json_query($query)->[0]->{count};
1565 # count of items we attempted to import, but errored, attached to records in the queue in question
1566 $query->{where}->{import_error} = {'!=' => undef};
1567 $summary->{item_import_errors} = $e->json_query($query)->[0]->{count};
1569 # count of items we successfully imported attached to records in the queue in question
1570 delete $query->{where}->{import_error};
1571 $query->{where}->{import_time} = {'!=' => undef};
1572 $summary->{total_items_imported} = $e->json_query($query)->[0]->{count};
1578 # --------------------------------------------------------------------------------
1579 # Given a list of queued record IDs, imports all items attached to those records
1580 # --------------------------------------------------------------------------------
1581 sub import_record_asset_list_impl {
1582 my($conn, $rec_ids, $requestor, $args) = @_;
1584 my $roe = new_editor(xact=> 1, requestor => $requestor);
1586 # for speed, filter out any records have not been
1587 # imported or have no import items to load
1588 $rec_ids = $roe->json_query({
1589 select => {vqbr => ['id']},
1590 from => {vqbr => 'vii'},
1591 where => {'+vqbr' => {
1593 import_time => {'!=' => undef}
1597 $rec_ids = [map {$_->{id}} @$rec_ids];
1601 total => scalar(@$rec_ids),
1602 step => 1, # how often to respond
1607 for my $rec_id (@$rec_ids) {
1608 my $rec = $roe->retrieve_vandelay_queued_bib_record($rec_id);
1609 my $item_ids = $roe->search_vandelay_import_item(
1610 {record => $rec->id, import_error => undef},
1614 # if any items have no call_number label and a value should be
1615 # applied automatically (via org settings), we want to use the same
1616 # call number label for every copy per org per record.
1617 my $auto_callnumber = {};
1619 my $opp_acq_copy_overlay = $args->{opp_acq_copy_overlay};
1620 my @overlaid_copy_ids;
1621 for my $item_id (@$item_ids) {
1622 my $e = new_editor(requestor => $requestor, xact => 1);
1623 my $item = $e->retrieve_vandelay_import_item($item_id);
1624 my ($copy, $vol, $evt);
1626 $$report_args{e} = $e;
1627 $$report_args{evt} = undef;
1628 $$report_args{import_item} = $item;
1629 $$report_args{import_error} = undef;
1631 if (my $copy_id = $item->internal_id) { # assignment
1632 # copy matches an existing copy. Overlay instead of create.
1634 $logger->info("vl: performing copy overlay for internal_id=$copy_id");
1636 my $qt = $e->json_query({
1637 select => {vbq => ['queue_type']},
1638 from => {vqbr => 'vbq'},
1639 where => {'+vqbr' => {id => $rec_id}}
1640 })->[0]->{queue_type};
1643 # internal_id for ACQ queues refers to acq.lineitem_detail.id
1644 # pull the real copy id from the acq LID
1646 my $lid = $e->retrieve_acq_lineitem_detail($copy_id);
1648 $$report_args{evt} = $e->die_event;
1649 respond_with_status($report_args);
1652 $copy_id = $lid->eg_copy_id;
1653 $logger->info("vl: performing ACQ copy overlay for copy $copy_id");
1656 $copy = $e->search_asset_copy([
1657 {id => $copy_id, deleted => 'f'},
1658 {flesh => 1, flesh_fields => {acp => ['call_number']}}
1662 $$report_args{evt} = $e->die_event;
1663 respond_with_status($report_args);
1667 # prevent update of unrelated copies
1668 if ($copy->call_number->record != $rec->imported_as) {
1669 $logger->info("vl: attempt to overlay unrelated copy=$copy_id; rec=".$rec->imported_as);
1671 $evt = OpenILS::Event->new('INVALID_IMPORT_COPY_ID',
1672 note => 'Cannot overlay copies for unlinked bib',
1673 bre => $rec->imported_as,
1676 $$report_args{evt} = $evt;
1677 respond_with_status($report_args);
1680 } elsif ($opp_acq_copy_overlay) { # we are going to "opportunistically" overlay received, in-process acq copies
1681 # recv_time should never be null if the copy status is
1682 # "In Process", so that is just a double-check
1685 "recv_time" => {"!=" => undef},
1686 "owning_lib" => $item->owning_lib,
1687 "+acn" => {"record" => $rec->imported_as},
1688 "+acp" => {"status" => OILS_COPY_STATUS_IN_PROCESS}
1698 "acqlid" => ["eg_copy_id"],
1699 "acp" => ["call_number"]
1703 # don't overlay the same copy twice
1704 $query->[0]{"+acp"}{"id"} = {"not in" => \@overlaid_copy_ids} if @overlaid_copy_ids;
1705 if (my $acqlid = $e->search_acq_lineitem_detail($query)->[0]) {
1706 $copy = $acqlid->eg_copy_id;
1707 push(@overlaid_copy_ids, $copy->id);
1711 if ($copy) { # we found a copy to overlay
1713 # overlaying copies requires an extra permission
1714 if (!$e->allowed("IMPORT_OVERLAY_COPY", $copy->call_number->owning_lib)) {
1715 $$report_args{evt} = $e->die_event;
1716 respond_with_status($report_args);
1720 # are we updating the call-number?
1721 if ($item->call_number and $item->call_number ne $copy->call_number->label) {
1723 my $count = $e->json_query({
1724 select => {acp => [{
1727 transform => 'count',
1733 call_number => $copy->call_number->id
1739 my $evol = $e->search_asset_call_number({
1740 id => {'<>' => $copy->call_number->id},
1741 label => $item->call_number,
1742 owning_lib => $copy->call_number->owning_lib,
1743 record => $copy->call_number->record,
1744 prefix => $copy->call_number->prefix,
1745 suffix => $copy->call_number->suffix,
1750 # call number for overlayed copy changed to a
1751 # label already in use by another call number.
1752 # merge the old CN into the new CN
1755 "vl: moving copy to new call number ".
1756 $item->call_number);
1759 OpenILS::Application::Cat::Merge::merge_volumes(
1760 $e, [$copy->call_number], $evol);
1763 $$report_args{evt} = $err;
1764 respond_with_status($report_args);
1768 # update our copy *cough* of the copy to pick up
1769 # any changes made my merge_volumes
1770 $copy = $e->retrieve_asset_copy([
1772 {flesh => 1, flesh_fields => {acp => ['call_number']}}
1777 "vl: updating copy call number label".
1778 $item->call_number);
1780 $copy->call_number->label($item->call_number);
1781 if (!$e->update_asset_call_number($copy->call_number)) {
1782 $$report_args{evt} = $e->die_event;
1783 respond_with_status($report_args);
1790 # otherwise, move the copy to a new/existing
1791 # call-number with the given label/owner
1792 # note that overlay does not allow the owning_lib
1793 # to be changed. Should it?
1795 $logger->info("vl: moving copy to new callnumber in copy overlay");
1798 OpenILS::Application::Cat::AssetCommon->find_or_create_volume(
1799 $e, $item->call_number,
1800 $copy->call_number->record,
1801 $copy->call_number->owning_lib
1805 $$report_args{evt} = $evt;
1806 respond_with_status($report_args);
1810 $copy->call_number($vol);
1814 # for every field that has a non-'' value, overlay the copy value
1815 foreach (qw/ barcode location circ_lib status
1816 circulate deposit deposit_amount ref holdable
1817 price circ_as_type alert_message opac_visible circ_modifier/) {
1819 my $val = $item->$_();
1820 $copy->$_($val) if defined $val and $val ne '';
1823 # de-flesh for update
1824 $copy->call_number($copy->call_number->id);
1825 $copy->ischanged(1);
1827 $evt = OpenILS::Application::Cat::AssetCommon->
1828 update_fleshed_copies($e, {all => 1}, undef, [$copy]);
1831 $$report_args{evt} = $evt;
1832 respond_with_status($report_args);
1838 # Creating a new copy
1839 $logger->info("vl: creating new copy in import");
1841 # appply defaults values from org settings as needed
1842 # if $auto_callnumber is unset, it will be set within
1843 apply_import_item_defaults($e, $item, $auto_callnumber);
1845 # --------------------------------------------------------------------------------
1846 # Find or create the volume
1847 # --------------------------------------------------------------------------------
1849 OpenILS::Application::Cat::AssetCommon->find_or_create_volume(
1850 $e, $item->call_number, $rec->imported_as, $item->owning_lib);
1853 $$report_args{evt} = $evt;
1854 respond_with_status($report_args);
1858 # --------------------------------------------------------------------------------
1859 # Create the new copy
1860 # --------------------------------------------------------------------------------
1861 $copy = Fieldmapper::asset::copy->new;
1862 $copy->loan_duration(2);
1863 $copy->fine_level(2);
1864 $copy->barcode($item->barcode);
1865 $copy->location($item->location);
1866 $copy->circ_lib($item->circ_lib || $item->owning_lib);
1867 $copy->status( defined($item->status) ? $item->status : OILS_COPY_STATUS_IN_PROCESS );
1868 $copy->circulate($item->circulate);
1869 $copy->deposit($item->deposit);
1870 $copy->deposit_amount($item->deposit_amount);
1871 $copy->ref($item->ref);
1872 $copy->holdable($item->holdable);
1873 $copy->price($item->price);
1874 $copy->circ_as_type($item->circ_as_type);
1875 $copy->alert_message($item->alert_message);
1876 $copy->opac_visible($item->opac_visible);
1877 $copy->circ_modifier($item->circ_modifier);
1879 # --------------------------------------------------------------------------------
1880 # Check for dupe barcode
1881 # --------------------------------------------------------------------------------
1882 if($evt = OpenILS::Application::Cat::AssetCommon->create_copy($e, $vol, $copy)) {
1883 $$report_args{evt} = $evt;
1884 $$report_args{import_error} = 'import.item.duplicate.barcode'
1885 if $evt->{textcode} eq 'ITEM_BARCODE_EXISTS';
1886 respond_with_status($report_args);
1890 # --------------------------------------------------------------------------------
1892 # --------------------------------------------------------------------------------
1893 $evt = OpenILS::Application::Cat::AssetCommon->create_copy_note(
1894 $e, $copy, '', $item->pub_note, 1) if $item->pub_note;
1897 $$report_args{evt} = $evt;
1898 respond_with_status($report_args);
1902 $evt = OpenILS::Application::Cat::AssetCommon->create_copy_note(
1903 $e, $copy, '', $item->priv_note) if $item->priv_note;
1906 $$report_args{evt} = $evt;
1907 respond_with_status($report_args);
1912 # set the import data on the import item
1913 $item->imported_as($copy->id); # $copy->id is set by create_copy() ^--
1914 $item->import_time('now');
1916 unless($e->update_vandelay_import_item($item)) {
1917 $$report_args{evt} = $e->die_event;
1918 respond_with_status($report_args);
1922 # --------------------------------------------------------------------------------
1923 # Item import succeeded
1924 # --------------------------------------------------------------------------------
1926 $$report_args{in_count}++;
1927 respond_with_status($report_args);
1928 $logger->info("vl: successfully imported item " . $item->barcode);
1936 sub apply_import_item_defaults {
1937 my ($e, $item, $auto_cn) = @_;
1938 my $org = $item->owning_lib || $item->circ_lib;
1939 my %c = %item_defaults_cache;
1941 # fetch and cache the org unit setting value (unless
1942 # it's already cached) and return the value to the caller
1945 return $c{$org}{$name} if defined $c{$org}{$name};
1946 my $sname = "vandelay.item.$name";
1947 $c{$org}{$name} = $U->ou_ancestor_setting_value($org, $sname, $e);
1948 $c{$org}{$name} = '' unless defined $c{$org}{$name};
1949 return $c{$org}{$name};
1952 if (!$item->barcode) {
1954 if ($set->('barcode.auto')) {
1956 my $pfx = $set->('barcode.prefix') || 'VAN';
1957 my $barcode = $pfx . $item->record . $item->id;
1959 $logger->info("vl: using auto barcode $barcode for ".$item->id);
1960 $item->barcode($barcode);
1963 $logger->error("vl: no barcode (or defualt) for item ".$item->id);
1967 if (!$item->call_number) {
1969 if ($set->('call_number.auto')) {
1971 if (!$auto_cn->{$org}) {
1972 my $pfx = $set->('call_number.prefix') || 'VAN';
1974 # use the ID of the first item to differentiate this
1975 # call number from others linked to the same record
1976 $auto_cn->{$org} = $pfx . $item->record . $item->id;
1979 $logger->info("vl: using auto call number ".$auto_cn->{$org});
1980 $item->call_number($auto_cn->{$org});
1983 $logger->error("vl: no call number or default for item ".$item->id);
1989 sub respond_with_status {
1993 # If the import failed, track the failure reason
1995 my $error = $$args{import_error};
1996 my $evt = $$args{evt};
1998 if($error or $evt) {
2000 my $item = $$args{import_item};
2001 $logger->info("vl: unable to import item " . $item->barcode);
2003 $error ||= 'general.unknown';
2004 $item->import_error($error);
2007 my $detail = sprintf("%s : %s", $evt->{textcode}, substr($evt->{desc}, 0, 140));
2008 $item->error_detail($detail);
2011 # state of the editor is unknown at this point. Force a rollback and start over.
2013 $e = new_editor(xact => 1);
2014 $e->update_vandelay_import_item($item);
2018 if($$args{report_all} or ($$args{progress} % $$args{step}) == 0) {
2019 $$args{conn}->respond({
2020 total => $$args{total},
2021 progress => $$args{progress},
2022 success_count => $$args{success_count},
2025 $$args{step} *= 2 unless $$args{step} == 256;
2031 __PACKAGE__->register_method(
2032 api_name => "open-ils.vandelay.match_set.get_tree",
2033 method => "match_set_get_tree",
2037 desc => q/For a given vms object, return a tree of match set points
2038 represented by a vmsp object with recursively fleshed
2043 sub match_set_get_tree {
2044 my ($self, $conn, $authtoken, $match_set_id) = @_;
2046 $match_set_id = int($match_set_id) or return;
2048 my $e = new_editor("authtoken" => $authtoken);
2049 $e->checkauth or return $e->die_event;
2051 my $set = $e->retrieve_vandelay_match_set($match_set_id) or
2052 return $e->die_event;
2054 $e->allowed("ADMIN_IMPORT_MATCH_SET", $set->owner) or
2055 return $e->die_event;
2057 my $tree = $e->search_vandelay_match_set_point([
2058 {"match_set" => $match_set_id, "parent" => undef},
2059 {"flesh" => -1, "flesh_fields" => {"vmsp" => ["children"]}}
2060 ]) or return $e->die_event;
2066 __PACKAGE__->register_method(
2067 api_name => "open-ils.vandelay.match_set.update",
2068 method => "match_set_update_tree",
2072 desc => q/Replace any vmsp objects associated with a given (by ID) vms
2073 with the given objects (recursively fleshed vmsp tree)./
2077 sub _walk_new_vmsp {
2078 my ($e, $match_set_id, $node, $parent_id) = @_;
2080 my $point = new Fieldmapper::vandelay::match_set_point;
2081 $point->parent($parent_id);
2082 $point->match_set($match_set_id);
2083 $point->$_($node->$_) for (qw/bool_op svf tag subfield negate quality/);
2085 $e->create_vandelay_match_set_point($point) or return $e->die_event;
2087 $parent_id = $e->data->id;
2088 if ($node->children && @{$node->children}) {
2089 for (@{$node->children}) {
2090 return $e->die_event if
2091 _walk_new_vmsp($e, $match_set_id, $_, $parent_id);
2098 sub match_set_update_tree {
2099 my ($self, $conn, $authtoken, $match_set_id, $tree) = @_;
2101 my $e = new_editor("xact" => 1, "authtoken" => $authtoken);
2102 $e->checkauth or return $e->die_event;
2104 my $set = $e->retrieve_vandelay_match_set($match_set_id) or
2105 return $e->die_event;
2107 $e->allowed("ADMIN_IMPORT_MATCH_SET", $set->owner) or
2108 return $e->die_event;
2110 my $existing = $e->search_vandelay_match_set_point([
2111 {"match_set" => $match_set_id},
2112 {"order_by" => {"vmsp" => "id DESC"}}
2113 ]) or return $e->die_event;
2115 # delete points, working up from leaf points to the root
2117 for my $point (shift @$existing) {
2118 if( grep {$_->parent eq $point->id} @$existing) {
2119 push(@$existing, $point);
2121 $e->delete_vandelay_match_set_point($point) or return $e->die_event;
2126 _walk_new_vmsp($e, $match_set_id, $tree);
2128 $e->commit or return $e->die_event;
2131 __PACKAGE__->register_method(
2132 api_name => 'open-ils.vandelay.bib_queue.to_bucket',
2133 method => 'bib_queue_to_bucket',
2137 desc => q/Add to or create a new bib container (bucket) with the successfully
2138 imported contents of a vandelay queue. Any user that has Vandelay
2139 queue create permissions can append or create buckets from his-her own queues./,
2141 {desc => 'Authtoken', type => 'string'},
2142 {desc => 'Queue ID', type => 'number'},
2143 {desc => 'Bucket Name', type => 'string'}
2145 return => {desc => q/
2146 {bucket => $bucket, addcount => number-of-items-added-to-bucket, item_count => total-bucket-item-count} on success,
2147 {add_count => 0} if there is nothing to do, and Event on error/}
2151 sub bib_queue_to_bucket {
2152 my ($self, $conn, $auth, $q_id, $bucket_name) = @_;
2154 my $e = new_editor(xact => 1, authtoken => $auth);
2155 return $e->die_event unless $e->checkauth;
2157 my $queue = $e->retrieve_vandelay_bib_queue($q_id)
2158 or return $e->die_event;
2160 return OpenILS::Event->new('BAD_PARAMS',
2161 note => q/Bucket creator must be queue owner/)
2162 unless $queue->owner == $e->requestor->id;
2164 # find the bib IDs that will go into the bucket
2165 my $bib_ids = $e->json_query({
2166 select => {vqbr => ['imported_as']},
2168 where => {queue => $q_id, imported_as => {'!=' => undef}}
2171 if (!@$bib_ids) { # no records to add
2173 return {add_count => 0};
2176 # allow user to add to an existing bucket by name
2177 my $bucket = $e->search_container_biblio_record_entry_bucket({
2178 owner => $e->requestor->id,
2179 name => $bucket_name,
2180 btype => 'vandelay_queue'
2183 # if the bucket does not exist, create a new one
2185 $bucket = Fieldmapper::container::biblio_record_entry_bucket->new;
2186 $bucket->name($bucket_name);
2187 $bucket->owner($e->requestor->id);
2188 $bucket->btype('vandelay_queue');
2190 $e->create_container_biblio_record_entry_bucket($bucket)
2191 or return $e->die_event;
2194 # create the new bucket items
2195 for my $bib_id ( map {$_->{imported_as}} @$bib_ids ) {
2196 my $item = Fieldmapper::container::biblio_record_entry_bucket_item->new;
2197 $item->target_biblio_record_entry($bib_id);
2198 $item->bucket($bucket->id);
2199 $e->create_container_biblio_record_entry_bucket_item($item)
2200 or return $e->die_event;
2203 # re-fetch the bucket to pick up the correct create_time
2204 $bucket = $e->retrieve_container_biblio_record_entry_bucket($bucket->id)
2205 or return $e->die_event;
2207 # get the total count of items in this bucket
2208 my $count = $e->json_query({
2209 select => {cbrebi => [{
2211 transform => 'count',
2216 where => {bucket => $bucket->id}
2223 add_count => scalar(@$bib_ids), # items added to the bucket
2224 item_count => $count->{count} # total items in buckets