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 my $U = 'OpenILS::Application::AppUtils';
27 # A list of LDR/06 values from http://loc.gov/marc
54 # --------------------------------------------------------------------------------
57 sub create_bib_queue {
64 my $match_set = shift;
65 my $import_def = shift;
66 my $match_bucket = shift;
68 my $e = new_editor(authtoken => $auth, xact => 1);
70 return $e->die_event unless $e->checkauth;
71 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE');
72 $owner ||= $e->requestor->id;
74 if ($e->search_vandelay_bib_queue( {name => $name, owner => $owner, queue_type => $type})->[0]) {
76 return OpenILS::Event->new('BIB_QUEUE_EXISTS')
79 my $queue = new Fieldmapper::vandelay::bib_queue();
80 $queue->name( $name );
81 $queue->owner( $owner );
82 $queue->queue_type( $type ) if ($type);
83 $queue->item_attr_def( $import_def ) if ($import_def);
84 $queue->match_set($match_set) if $match_set;
85 $queue->match_bucket($match_bucket) if $match_bucket;
87 my $new_q = $e->create_vandelay_bib_queue( $queue );
88 return $e->die_event unless ($new_q);
93 __PACKAGE__->register_method(
94 api_name => "open-ils.vandelay.bib_queue.create",
95 method => "create_bib_queue",
101 sub create_auth_queue {
108 my $match_set = shift;
110 my $e = new_editor(authtoken => $auth, xact => 1);
112 return $e->die_event unless $e->checkauth;
113 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE');
114 $owner ||= $e->requestor->id;
116 if ($e->search_vandelay_authority_queue({name => $name, owner => $owner, queue_type => $type})->[0]) {
118 return OpenILS::Event->new('AUTH_QUEUE_EXISTS')
121 my $queue = new Fieldmapper::vandelay::authority_queue();
122 $queue->name( $name );
123 $queue->owner( $owner );
124 $queue->queue_type( $type ) if ($type);
126 my $new_q = $e->create_vandelay_authority_queue( $queue );
127 $e->die_event unless ($new_q);
132 __PACKAGE__->register_method(
133 api_name => "open-ils.vandelay.authority_queue.create",
134 method => "create_auth_queue",
139 sub add_record_to_bib_queue {
146 my $bib_source = shift;
148 my $e = new_editor(authtoken => $auth, xact => 1);
150 $queue = $e->retrieve_vandelay_bib_queue($queue);
152 return $e->die_event unless $e->checkauth;
153 return $e->die_event unless
154 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
155 $e->allowed('CREATE_BIB_IMPORT_QUEUE'));
157 my $new_rec = _add_bib_rec($e, $marc, $queue->id, $purpose, $bib_source);
159 return $e->die_event unless ($new_rec);
163 __PACKAGE__->register_method(
164 api_name => "open-ils.vandelay.queued_bib_record.create",
165 method => "add_record_to_bib_queue",
175 my $bib_source = shift;
177 my $rec = new Fieldmapper::vandelay::queued_bib_record();
179 $rec->queue( $queue );
180 $rec->purpose( $purpose ) if ($purpose);
181 $rec->bib_source($bib_source);
183 return $e->create_vandelay_queued_bib_record( $rec );
186 sub add_record_to_authority_queue {
194 my $e = new_editor(authtoken => $auth, xact => 1);
196 $queue = $e->retrieve_vandelay_authority_queue($queue);
198 return $e->die_event unless $e->checkauth;
199 return $e->die_event unless
200 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
201 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE'));
203 my $new_rec = _add_auth_rec($e, $marc, $queue->id, $purpose);
205 return $e->die_event unless ($new_rec);
209 __PACKAGE__->register_method(
210 api_name => "open-ils.vandelay.queued_authority_record.create",
211 method => "add_record_to_authority_queue",
222 my $rec = new Fieldmapper::vandelay::queued_authority_record();
224 $rec->queue( $queue );
225 $rec->purpose( $purpose ) if ($purpose);
227 return $e->create_vandelay_queued_authority_record( $rec );
234 my $fingerprint = shift || '';
235 my $queue_id = shift;
237 my $filename = shift;
238 my $bib_source = shift;
240 my $e = new_editor(authtoken => $auth, xact => 1);
241 return $e->die_event unless $e->checkauth;
244 my $type = $self->{record_type};
247 $queue = $e->retrieve_vandelay_bib_queue($queue_id) or return $e->die_event;
249 $queue = $e->retrieve_vandelay_authority_queue($queue_id) or return $e->die_event;
252 my $evt = check_queue_perms($e, $type, $queue);
253 return $evt if ($evt);
255 my $cache = new OpenSRF::Utils::Cache();
258 my $data = $cache->get_cache('vandelay_import_spool_' . $fingerprint);
259 $purpose = $data->{purpose};
260 $filename = $data->{path};
261 $bib_source = $data->{bib_source};
264 unless(-r $filename) {
265 $logger->error("unable to read MARC file $filename");
266 return -1; # make this an event XXX
269 $logger->info("vandelay spooling $fingerprint purpose=$purpose file=$filename");
271 my $marctype = 'USMARC';
274 $marctype = 'XML' if (getc(F) =~ /^\D/o);
277 my $batch = new MARC::Batch ($marctype, $filename);
280 my $response_scale = 10;
283 while (try { $r = $batch->next } otherwise { $r = -1 }) {
285 $logger->warn("Processing of record $count in set $filename failed. Skipping this record");
289 $logger->info("processing record $count");
292 my $xml = clean_marc($r);
295 # Check the leader to ensure we've got something resembling the expected
296 # Allow spaces to give records the benefit of the doubt
297 my $ldr_type = substr($r->leader(), 6, 1);
298 if ($type eq 'bib' && ($record_types{$ldr_type}) eq 'bib' || $ldr_type eq ' ') {
299 $qrec = _add_bib_rec( $e, $xml, $queue_id, $purpose, $bib_source ) or return $e->die_event;
300 } elsif ($type eq 'auth' && ($record_types{$ldr_type}) eq 'auth' || $ldr_type eq ' ') {
301 $qrec = _add_auth_rec( $e, $xml, $queue_id, $purpose ) or return $e->die_event;
303 # I don't know how to handle this type; rock on
304 $logger->error("In process_spool(), type was $type and leader type was $ldr_type ; not currently supported");
308 if($self->api_name =~ /stream_results/ and $qrec) {
309 $client->respond($qrec->id)
311 $client->respond($count) if (++$count % $response_scale) == 0;
312 $response_scale *= 10 if ($count == ($response_scale * 10));
316 $logger->warn("Encountered a bad record at Vandelay ingest: ".$error);
322 $cache->delete_cache('vandelay_import_spool_' . $fingerprint) if $fingerprint;
326 __PACKAGE__->register_method(
327 api_name => "open-ils.vandelay.bib.process_spool",
328 method => "process_spool",
334 __PACKAGE__->register_method(
335 api_name => "open-ils.vandelay.auth.process_spool",
336 method => "process_spool",
340 record_type => 'auth'
343 __PACKAGE__->register_method(
344 api_name => "open-ils.vandelay.bib.process_spool.stream_results",
345 method => "process_spool",
352 __PACKAGE__->register_method(
353 api_name => "open-ils.vandelay.auth.process_spool.stream_results",
354 method => "process_spool",
359 record_type => 'auth'
362 __PACKAGE__->register_method(
363 api_name => "open-ils.vandelay.bib_queue.records.retrieve",
364 method => 'retrieve_queued_records',
370 __PACKAGE__->register_method(
371 api_name => "open-ils.vandelay.bib_queue.records.retrieve.export.print",
372 method => 'retrieve_queued_records',
378 __PACKAGE__->register_method(
379 api_name => "open-ils.vandelay.bib_queue.records.retrieve.export.csv",
380 method => 'retrieve_queued_records',
386 __PACKAGE__->register_method(
387 api_name => "open-ils.vandelay.bib_queue.records.retrieve.export.email",
388 method => 'retrieve_queued_records',
395 __PACKAGE__->register_method(
396 api_name => "open-ils.vandelay.auth_queue.records.retrieve",
397 method => 'retrieve_queued_records',
401 record_type => 'auth'
403 __PACKAGE__->register_method(
404 api_name => "open-ils.vandelay.auth_queue.records.retrieve.export.print",
405 method => 'retrieve_queued_records',
409 record_type => 'auth'
411 __PACKAGE__->register_method(
412 api_name => "open-ils.vandelay.auth_queue.records.retrieve.export.csv",
413 method => 'retrieve_queued_records',
417 record_type => 'auth'
419 __PACKAGE__->register_method(
420 api_name => "open-ils.vandelay.auth_queue.records.retrieve.export.email",
421 method => 'retrieve_queued_records',
425 record_type => 'auth'
428 __PACKAGE__->register_method(
429 api_name => "open-ils.vandelay.bib_queue.records.matches.retrieve",
430 method => 'retrieve_queued_records',
434 record_type => 'bib',
436 desc => q/Only retrieve queued bib records that have matches against existing records/
439 __PACKAGE__->register_method(
440 api_name => "open-ils.vandelay.auth_queue.records.matches.retrieve",
441 method => 'retrieve_queued_records',
445 record_type => 'auth',
447 desc => q/Only retrieve queued authority records that have matches against existing records/
451 sub retrieve_queued_records {
452 my($self, $conn, $auth, $queue_id, $options) = @_;
455 my $limit = $$options{limit} || 20;
456 my $offset = $$options{offset} || 0;
457 my $type = $self->{record_type};
459 my $e = new_editor(authtoken => $auth, xact => 1);
460 return $e->die_event unless $e->checkauth;
464 $queue = $e->retrieve_vandelay_bib_queue($queue_id) or return $e->die_event;
466 $queue = $e->retrieve_vandelay_authority_queue($queue_id) or return $e->die_event;
468 my $evt = check_queue_perms($e, $type, $queue);
469 return $evt if ($evt);
471 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
472 my $mclass = $type eq 'bib' ? 'vbm' : 'vam';
477 column => 'eg_record',
483 where => {queue => $queue_id},
488 if($self->api_name =~ /export/) {
489 delete $query->{limit};
490 delete $query->{offset};
493 $query->{where}->{import_time} = undef if $$options{non_imported};
495 if($$options{with_import_error}) {
497 $query->{from} = {$class => {vii => {type => 'left'}}};
498 $query->{where}->{'-or'} = [
499 {'+vqbr' => {import_error => {'!=' => undef}}},
500 {'+vii' => {import_error => {'!=' => undef}}}
505 if($$options{with_rec_import_error}) {
506 $query->{where}->{import_error} = {'!=' => undef};
508 } elsif( $$options{with_item_import_error} and $type eq 'bib') {
510 $query->{from} = {$class => 'vii'};
511 $query->{where}->{'+vii'} = {import_error => {'!=' => undef}};
515 if($self->api_name =~ /matches/) {
516 # find only records that have matches
517 $query->{from} = {$class => {$mclass => {type => 'right'}}};
519 # join to mclass for sorting (see below)
520 $query->{from} = {$class => {$mclass => {type => 'left'}}};
523 # order by the matched bib records to group like queued records
524 $query->{order_by} = [
525 {class => $mclass, field => 'eg_record', transform => 'min'},
526 {class => $class, field => 'id'}
529 my $record_ids = $e->json_query($query);
531 my $retrieve = ($type eq 'bib') ?
532 'retrieve_vandelay_queued_bib_record' : 'retrieve_vandelay_queued_authority_record';
533 my $search = ($type eq 'bib') ?
534 'search_vandelay_queued_bib_record' : 'search_vandelay_queued_authority_record';
536 if ($self->api_name =~ /export/) {
537 my $rec_list = $e->$search({id => [map { $_->{id} } @$record_ids]}, {substream => 1});
538 if ($self->api_name =~ /print/) {
541 return $U->fire_object_event(
543 'vandelay.queued_'.$type.'_record.print',
548 } elsif ($self->api_name =~ /csv/) {
551 return $U->fire_object_event(
553 'vandelay.queued_'.$type.'_record.csv',
558 } elsif ($self->api_name =~ /email/) {
560 $conn->respond_complete(1);
562 for my $rec (@$rec_list) {
563 $U->create_events_for_hook(
564 'vandelay.queued_'.$type.'_record.email',
566 $e->requestor->home_ou,
575 for my $rec_id (@$record_ids) {
576 my $flesh = ['attributes', 'matches'];
577 push(@$flesh, 'import_items') if $$options{flesh_import_items};
578 my $params = {flesh => 1, flesh_fields => {$class => $flesh}};
579 my $rec = $e->$retrieve([$rec_id->{id}, $params]);
580 $rec->clear_marc if $$options{clear_marc};
581 $conn->respond($rec);
589 __PACKAGE__->register_method(
590 api_name => 'open-ils.vandelay.import_item.queue.retrieve',
591 method => 'retrieve_queue_import_items',
597 Returns Import Item (vii) objects for the selected queue.
599 with_import_error : only return items that failed to import
602 __PACKAGE__->register_method(
603 api_name => 'open-ils.vandelay.import_item.queue.export.print',
604 method => 'retrieve_queue_import_items',
610 Returns template-generated printable output of Import Item (vii) objects for the selected queue.
612 with_import_error : only return items that failed to import
615 __PACKAGE__->register_method(
616 api_name => 'open-ils.vandelay.import_item.queue.export.csv',
617 method => 'retrieve_queue_import_items',
623 Returns template-generated CSV output of Import Item (vii) objects for the selected queue.
625 with_import_error : only return items that failed to import
628 __PACKAGE__->register_method(
629 api_name => 'open-ils.vandelay.import_item.queue.export.email',
630 method => 'retrieve_queue_import_items',
636 Emails template-generated output of Import Item (vii) objects for the selected queue.
638 with_import_error : only return items that failed to import
642 sub retrieve_queue_import_items {
643 my($self, $conn, $auth, $q_id, $options) = @_;
646 my $limit = $$options{limit} || 20;
647 my $offset = $$options{offset} || 0;
649 my $e = new_editor(authtoken => $auth);
650 return $e->event unless $e->checkauth;
652 my $queue = $e->retrieve_vandelay_bib_queue($q_id) or return $e->event;
653 my $evt = check_queue_perms($e, 'bib', $queue);
657 select => {vii => ['id']},
665 filter => {id => $q_id}
671 order_by => {'vii' => ['record','id']},
675 if($self->api_name =~ /export/) {
676 delete $query->{limit};
677 delete $query->{offset};
680 $query->{where} = {'+vii' => {import_error => {'!=' => undef}}}
681 if $$options{with_import_error};
683 my $items = $e->json_query($query);
684 my $item_list = $e->search_vandelay_import_item({id => [map { $_->{id} } @$items]});
685 if ($self->api_name =~ /export/) {
686 if ($self->api_name =~ /print/) {
688 return $U->fire_object_event(
690 'vandelay.import_items.print',
695 } elsif ($self->api_name =~ /csv/) {
697 return $U->fire_object_event(
699 'vandelay.import_items.csv',
704 } elsif ($self->api_name =~ /email/) {
706 $conn->respond_complete(1);
708 for my $item (@$item_list) {
709 $U->create_events_for_hook(
710 'vandelay.import_items.email',
712 $e->requestor->home_ou,
721 for my $item (@$item_list) {
722 $conn->respond($item);
729 sub check_queue_perms {
730 my($e, $type, $queue) = @_;
731 if ($type eq 'bib') {
732 return $e->die_event unless
733 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
734 $e->allowed('CREATE_BIB_IMPORT_QUEUE'));
736 return $e->die_event unless
737 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
738 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE'));
744 __PACKAGE__->register_method(
745 api_name => "open-ils.vandelay.bib_record.list.import",
746 method => 'import_record_list',
753 __PACKAGE__->register_method(
754 api_name => "open-ils.vandelay.auth_record.list.import",
755 method => 'import_record_list',
759 record_type => 'auth'
762 sub import_record_list {
763 my($self, $conn, $auth, $rec_ids, $args) = @_;
764 my $e = new_editor(authtoken => $auth, xact => 1);
765 return $e->die_event unless $e->checkauth;
767 my $err = import_record_list_impl($self, $conn, $rec_ids, $e->requestor, $args);
768 try {$e->rollback} otherwise {};
770 return {complete => 1};
774 __PACKAGE__->register_method(
775 api_name => "open-ils.vandelay.bib_queue.import",
776 method => 'import_queue',
781 record_type => 'bib',
784 Attempts to import all non-imported records for the selected queue.
785 Will also attempt import of all non-imported items.
790 __PACKAGE__->register_method(
791 api_name => "open-ils.vandelay.auth_queue.import",
792 method => 'import_queue',
797 record_type => 'auth'
801 my($self, $conn, $auth, $q_id, $options) = @_;
802 my $e = new_editor(authtoken => $auth, xact => 1);
803 return $e->die_event unless $e->checkauth;
805 my $type = $self->{record_type};
806 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
808 # First, collect the not-yet-imported records
809 my $query = {queue => $q_id, import_time => undef};
810 my $search = ($type eq 'bib') ?
811 'search_vandelay_queued_bib_record' :
812 'search_vandelay_queued_authority_record';
813 my $rec_ids = $e->$search($query, {idlist => 1});
815 # Now add any imported records that have un-imported items
818 my $item_recs = $e->json_query({
819 select => {vqbr => ['id']},
820 from => {vqbr => 'vii'},
824 import_time => {'!=' => undef}
826 '+vii' => {import_time => undef}
830 push(@$rec_ids, map {$_->{id}} @$item_recs);
833 my $err = import_record_list_impl($self, $conn, $rec_ids, $e->requestor, $options);
834 try {$e->rollback} otherwise {}; # only using this to make the read authoritative -- don't die from it
836 return {complete => 1};
839 # returns a list of queued record IDs for a given queue that
840 # have at least one entry in the match table
842 sub queued_records_with_matches {
843 my($e, $type, $q_id, $limit, $offset, $filter) = @_;
845 my $match_class = 'vbm';
846 my $rec_class = 'vqbr';
847 if($type eq 'auth') {
848 $match_class = 'vam';
853 $filter->{queue} = $q_id;
857 select => {$match_class => ['queued_record']},
862 fkey => 'queued_record',
869 if($limit or defined $offset) {
872 $query->{limit} = $limit;
873 $query->{offset} = $offset;
876 my $data = $e->json_query($query);
877 return [ map {$_->{queued_record}} @$data ];
881 # cache of import item org unit settings.
882 # used in apply_import_item_defaults() below,
883 # but reset on each call to import_record_list_impl()
884 my %item_defaults_cache;
886 sub import_record_list_impl {
887 my($self, $conn, $rec_ids, $requestor, $args) = @_;
889 my $overlay_map = $args->{overlay_map} || {};
890 my $type = $self->{record_type};
892 %item_defaults_cache = ();
898 total => scalar(@$rec_ids),
899 report_all => $$args{report_all}
902 $conn->max_chunk_count(1) if $$args{report_all};
904 my $auto_overlay_exact = $$args{auto_overlay_exact};
905 my $auto_overlay_1match = $$args{auto_overlay_1match};
906 my $auto_overlay_best = $$args{auto_overlay_best_match};
907 my $match_quality_ratio = $$args{match_quality_ratio};
908 my $merge_profile = $$args{merge_profile};
909 my $ft_merge_profile = $$args{fall_through_merge_profile};
910 my $bib_source = $$args{bib_source};
911 my $import_no_match = $$args{import_no_match};
912 my $strip_grps = $$args{strip_field_groups}; # bib-only
914 my $overlay_func = 'vandelay.overlay_bib_record';
915 my $auto_overlay_func = 'vandelay.auto_overlay_bib_record';
916 my $auto_overlay_best_func = 'vandelay.auto_overlay_bib_record_with_best'; # XXX bib-only
917 my $retrieve_func = 'retrieve_vandelay_queued_bib_record';
918 my $update_func = 'update_vandelay_queued_bib_record';
919 my $search_func = 'search_vandelay_queued_bib_record';
920 my $retrieve_queue_func = 'retrieve_vandelay_bib_queue';
921 my $update_queue_func = 'update_vandelay_bib_queue';
922 my $delete_queue_func = 'delete_vandelay_bib_queue';
923 my $rec_class = 'vqbr';
925 my $editor = new_editor();
928 my $sources = $editor->search_config_bib_source({id => {'!=' => undef}});
929 $bib_sources{$_->id} = $_->source for @$sources;
931 if($type eq 'auth') {
932 $overlay_func =~ s/bib/auth/o;
933 $auto_overlay_func = s/bib/auth/o;
934 $retrieve_func =~ s/bib/authority/o;
935 $retrieve_queue_func =~ s/bib/authority/o;
936 $update_queue_func =~ s/bib/authority/o;
937 $update_func =~ s/bib/authority/o;
938 $search_func =~ s/bib/authority/o;
939 $delete_queue_func =~ s/bib/authority/o;
943 my $new_rec_perm_cache;
945 for my $rec_id (@$rec_ids) {
948 my $overlay_target = $overlay_map->{$rec_id};
950 my $e = new_editor(xact => 1);
951 $e->requestor($requestor);
953 $$report_args{e} = $e;
954 $$report_args{evt} = undef;
955 $$report_args{import_error} = undef;
956 $$report_args{no_import} = 0;
958 my $rec = $e->$retrieve_func([
961 flesh_fields => { $rec_class => ['matches']},
966 $$report_args{evt} = $e->event;
967 finish_rec_import_attempt($report_args);
971 if($rec->import_time) {
972 # if the record is already imported, that means it may have
973 # un-imported copies. Add to success list for later processing.
974 push(@success_rec_ids, $rec_id);
979 $$report_args{rec} = $rec;
980 $queues{$rec->queue} = 1;
985 if ($type eq 'bib') {
986 # strip configured / selected MARC tags from inbound records
988 my $marcdoc = XML::LibXML->new->parse_string($rec->marc);
989 $rec->marc($U->strip_marc_fields($e, $marcdoc, $strip_grps));
991 unless ($e->$update_func($rec)) {
992 $$report_args{evt} = $e->die_event;
993 finish_rec_import_attempt($report_args);
998 if(defined $overlay_target) {
999 # Caller chose an explicit overlay target
1001 my $res = $e->json_query(
1012 if($res and ($res = $res->[0])) {
1014 if($res->{$overlay_func} eq 't') {
1015 $logger->info("vl: $type direct overlay succeeded for queued rec ".
1016 "$rec_id and overlay target $overlay_target");
1022 $logger->error("vl: Error attempting overlay with func=$overlay_func, profile=$merge_profile, record=$rec_id");
1027 if($auto_overlay_1match) { # overlay if there is exactly 1 match
1029 my %match_recs = map { $_->eg_record => 1 } @{$rec->matches};
1031 if( scalar(keys %match_recs) == 1) { # all matches point to the same record
1033 ($imported, $error, $rec) = try_auto_overlay(
1036 $auto_overlay_best_func,
1040 $match_quality_ratio,
1047 if(!$imported and !$error and $auto_overlay_exact and scalar(@{$rec->matches}) == 1 ) {
1049 # caller says to overlay if there is an /exact/ match
1050 # $auto_overlay_func only proceeds and returns true on exact matches
1052 my $res = $e->json_query(
1062 if($res and ($res = $res->[0])) {
1064 if($res->{$auto_overlay_func} eq 't') {
1065 $logger->info("vl: $type auto-overlay succeeded for queued rec $rec_id");
1068 # re-fetch the record to pick up the imported_as value from the DB
1069 $$report_args{rec} = $rec = $e->$retrieve_func([
1070 $rec_id, {flesh => 1, flesh_fields => {$rec_class => ['matches']}}]);
1073 $logger->info("vl: $type auto-overlay failed for queued rec $rec_id");
1078 $logger->error("vl: Error attempting overlay with func=$auto_overlay_func, profile=$merge_profile, record=$rec_id");
1082 if(!$imported and !$error and $auto_overlay_best and scalar(@{$rec->matches}) > 0 ) {
1083 # caller says to overlay the best match
1085 ($imported, $error, $rec) = try_auto_overlay(
1088 $auto_overlay_best_func,
1092 $match_quality_ratio,
1098 if(!$imported and !$error and $import_no_match and scalar(@{$rec->matches}) == 0) {
1100 # No overlay / merge occurred. Do a traditional record import by creating a new record
1102 if (!$new_rec_perm_cache) {
1103 $new_rec_perm_cache = {};
1105 # all users creating new records are required to have the basic permission.
1106 # if the client requests, we can enforce extra permissions for creating new records.
1107 # for speed, check the permissions the first time then cache the result.
1109 my $perm = ($type eq 'bib') ? 'IMPORT_MARC' : 'IMPORT_AUTHORITY_MARC';
1110 my $xperm = $$args{new_rec_perm};
1111 my $rec_ou = $e->requestor->ws_ou;
1113 $new_rec_perm_cache->{evt} = $e->die_event
1114 if !$e->allowed($perm, $rec_ou) || ($xperm and !$e->allowed($xperm, $rec_ou));
1117 if ($new_rec_perm_cache->{evt}) {
1119 # a cached event won't roll back the transaction (a la die_event), but
1120 # the transaction will get rolled back in finish_rec_import_attempt() below
1121 $$report_args{evt} = $new_rec_perm_cache->{evt};
1122 $$report_args{import_error} = 'import.record.perm_failure';
1124 } else { # perm checks succeeded
1126 $logger->info("vl: creating new $type record for queued record $rec_id");
1128 if ($type eq 'bib') {
1130 $record = OpenILS::Application::Cat::BibCommon->biblio_record_xml_import(
1131 $e, $rec->marc, $bib_sources{$rec->bib_source}, undef, 1);
1133 } else { # authority record
1135 $record = OpenILS::Application::Cat::AuthCommon->import_authority_record($e, $rec->marc); #$source);
1138 if($U->event_code($record)) {
1139 $$report_args{import_error} = 'import.duplicate.tcn'
1140 if $record->{textcode} eq 'OPEN_TCN_NOT_FOUND';
1141 $$report_args{evt} = $record;
1145 $logger->info("vl: successfully imported new $type record");
1146 $rec->imported_as($record->id);
1155 $rec->import_time('now');
1156 $rec->clear_import_error;
1157 $rec->clear_error_detail;
1159 if($e->$update_func($rec)) {
1161 if($type eq 'bib') {
1163 # see if this record is linked from an acq record.
1164 my $li = $e->search_acq_lineitem(
1165 {queued_record => $rec->id, state => {'!=' => 'canceled'}})->[0];
1168 # if so, update the acq lineitem to point to the imported record
1169 $li->eg_bib_id($rec->imported_as);
1170 $$report_args{evt} = $e->die_event unless $e->update_acq_lineitem($li);
1174 push @success_rec_ids, $rec_id;
1175 finish_rec_import_attempt($report_args);
1183 $logger->info("vl: record $rec_id was not imported");
1184 $$report_args{evt} = $e->event unless $$report_args{evt};
1185 $$report_args{no_import} = 1;
1186 finish_rec_import_attempt($report_args);
1190 # see if we need to mark any queues as complete
1191 for my $q_id (keys %queues) {
1193 my $e = new_editor(xact => 1);
1194 my $remaining = $e->$search_func(
1195 [{queue => $q_id, import_time => undef}, {limit =>1}], {idlist => 1});
1197 unless(@$remaining) {
1198 my $queue = $e->$retrieve_queue_func($q_id);
1199 unless($U->is_true($queue->complete)) {
1200 $queue->complete('t');
1201 $e->$update_queue_func($queue) or return $e->die_event;
1210 import_record_asset_list_impl($conn, \@success_rec_ids, $requestor) if @success_rec_ids;
1212 $conn->respond({total => $$report_args{total}, progress => $$report_args{progress}});
1217 sub try_auto_overlay {
1220 my $report_args = shift;
1221 my $overlay_func = shift;
1222 my $retrieve_func = shift;
1223 my $rec_class = shift;
1225 my $match_quality_ratio = shift;
1226 my $merge_profile = shift;
1227 my $ft_merge_profile = shift;
1232 # Find the best match and overlay if the quality ratio allows it.
1233 my $res = $e->json_query(
1239 $match_quality_ratio
1244 if($res and ($res = $res->[0])) {
1246 if($res->{$overlay_func} eq 't') {
1248 # first attempt succeeded
1253 # quality-limited merge failed with insufficient quality. If there is a
1254 # fall-through merge profile, re-do the merge with the alternate profile
1255 # and no quality restriction.
1257 if($ft_merge_profile and $match_quality_ratio > 0) {
1259 $logger->info("vl: $type auto-merge failed with profile $merge_profile; ".
1260 "re-merging with fall-through profile $ft_merge_profile");
1262 my $res = $e->json_query(
1268 0 # minimum quality not required
1273 if($res and ($res = $res->[0])) {
1275 if($res->{$overlay_func} eq 't') {
1277 # second attempt succeeded
1282 # failed to merge on second attempt
1283 $logger->info("vl: $type auto-merge with fall-through failed for queued rec $rec_id");
1287 # second attempt died
1289 $logger->error("vl: Error attempting overlay with func=$overlay_func, profile=$merge_profile, record=$rec_id");
1294 # failed to merge on first attempt, no fall-through was provided
1295 $$report_args{import_error} = 'overlay.record.quality' if $match_quality_ratio > 0;
1296 $logger->info("vl: $type auto-merge failed for queued rec $rec_id");
1302 # first attempt died
1304 $logger->error("vl: Error attempting overlay with func=$overlay_func, profile=$merge_profile, record=$rec_id");
1309 # at least 1 of the attempts succeeded
1310 $logger->info("vl: $type auto-merge succeeded for queued rec $rec_id");
1312 # re-fetch the record to pick up the imported_as value from the DB
1313 $$report_args{rec} = $e->$retrieve_func([
1314 $rec_id, {flesh => 1, flesh_fields => {$rec_class => ['matches']}}]);
1317 return ($imported, $error, $$report_args{rec});
1321 # tracks any import errors, commits the current xact, responds to the client
1322 sub finish_rec_import_attempt {
1324 my $evt = $$args{evt};
1325 my $rec = $$args{rec};
1328 my $error = $$args{import_error};
1329 $error = 'general.unknown' if $evt and not $error;
1334 if($error or $evt) {
1336 # since an error occurred, there's no guarantee the transaction wasn't
1337 # rolled back. force a rollback and create a new editor.
1339 $e = new_editor(xact => 1);
1340 $rec->import_error($error);
1343 my $detail = sprintf("%s : %s", $evt->{textcode}, substr($evt->{desc}, 0, 140));
1344 $rec->error_detail($detail);
1347 my $method = 'update_vandelay_queued_bib_record';
1348 $method =~ s/bib/authority/ if $$args{type} eq 'auth';
1349 $e->$method($rec) and $e->commit or $e->rollback;
1352 # commit the successful import
1357 # requested queued record was not found
1362 if($$args{report_all} or ($$args{progress} % $$args{step}) == 0) {
1363 $$args{conn}->respond({
1364 total => $$args{total},
1365 progress => $$args{progress},
1366 imported => ($rec) ? $rec->id : undef,
1367 import_error => $error,
1368 no_import => $$args{no_import},
1371 $$args{step} *= 2 unless $$args{step} == 256;
1381 __PACKAGE__->register_method(
1382 api_name => "open-ils.vandelay.bib_queue.owner.retrieve",
1383 method => 'owner_queue_retrieve',
1387 record_type => 'bib'
1389 __PACKAGE__->register_method(
1390 api_name => "open-ils.vandelay.authority_queue.owner.retrieve",
1391 method => 'owner_queue_retrieve',
1395 record_type => 'auth'
1398 sub owner_queue_retrieve {
1399 my($self, $conn, $auth, $owner_id, $filters) = @_;
1400 my $e = new_editor(authtoken => $auth, xact => 1);
1401 return $e->die_event unless $e->checkauth;
1402 $owner_id = $e->requestor->id; # XXX add support for viewing other's queues?
1405 my $search = {owner => $owner_id};
1406 $search->{$_} = $filters->{$_} for keys %$filters;
1408 if($self->{record_type} eq 'bib') {
1409 $queues = $e->search_vandelay_bib_queue(
1410 [$search, {order_by => {vbq => 'evergreen.lowercase(name)'}}]);
1412 $queues = $e->search_vandelay_authority_queue(
1413 [$search, {order_by => {vaq => 'evergreen.lowercase(name)'}}]);
1415 $conn->respond($_) for @$queues;
1420 __PACKAGE__->register_method(
1421 api_name => "open-ils.vandelay.bib_queue.delete",
1422 method => "delete_queue",
1425 record_type => 'bib'
1427 __PACKAGE__->register_method(
1428 api_name => "open-ils.vandelay.auth_queue.delete",
1429 method => "delete_queue",
1432 record_type => 'auth'
1436 my($self, $conn, $auth, $q_id) = @_;
1437 my $e = new_editor(xact => 1, authtoken => $auth);
1438 return $e->die_event unless $e->checkauth;
1439 if($self->{record_type} eq 'bib') {
1440 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE');
1441 my $queue = $e->retrieve_vandelay_bib_queue($q_id)
1442 or return $e->die_event;
1443 $e->delete_vandelay_bib_queue($queue)
1444 or return $e->die_event;
1446 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE');
1447 my $queue = $e->retrieve_vandelay_authority_queue($q_id)
1448 or return $e->die_event;
1449 $e->delete_vandelay_authority_queue($queue)
1450 or return $e->die_event;
1457 __PACKAGE__->register_method(
1458 api_name => "open-ils.vandelay.queued_bib_record.html",
1459 method => 'queued_record_html',
1463 record_type => 'bib'
1465 __PACKAGE__->register_method(
1466 api_name => "open-ils.vandelay.queued_authority_record.html",
1467 method => 'queued_record_html',
1471 record_type => 'auth'
1474 sub queued_record_html {
1475 my($self, $conn, $auth, $rec_id) = @_;
1476 my $e = new_editor(xact=>1,authtoken => $auth);
1477 return $e->die_event unless $e->checkauth;
1479 if($self->{record_type} eq 'bib') {
1480 $rec = $e->retrieve_vandelay_queued_bib_record($rec_id)
1481 or return $e->die_event;
1483 $rec = $e->retrieve_vandelay_queued_authority_record($rec_id)
1484 or return $e->die_event;
1488 return $U->simplereq(
1490 'open-ils.search.biblio.record.html', undef, 1, $rec->marc);
1494 __PACKAGE__->register_method(
1495 api_name => "open-ils.vandelay.bib_queue.summary.retrieve",
1496 method => 'retrieve_queue_summary',
1500 record_type => 'bib'
1502 __PACKAGE__->register_method(
1503 api_name => "open-ils.vandelay.auth_queue.summary.retrieve",
1504 method => 'retrieve_queue_summary',
1508 record_type => 'auth'
1511 sub retrieve_queue_summary {
1512 my($self, $conn, $auth, $queue_id) = @_;
1513 my $e = new_editor(xact=>1, authtoken => $auth);
1514 return $e->die_event unless $e->checkauth;
1517 my $type = $self->{record_type};
1518 if($type eq 'bib') {
1519 $queue = $e->retrieve_vandelay_bib_queue($queue_id)
1520 or return $e->die_event;
1522 $queue = $e->retrieve_vandelay_authority_queue($queue_id)
1523 or return $e->die_event;
1526 my $evt = check_queue_perms($e, $type, $queue);
1527 return $evt if $evt;
1529 my $search = 'search_vandelay_queued_bib_record';
1530 $search =~ s/bib/authority/ if $type ne 'bib';
1534 total => scalar(@{$e->$search({queue => $queue_id}, {idlist=>1})}),
1535 imported => scalar(@{$e->$search({queue => $queue_id, import_time => {'!=' => undef}}, {idlist=>1})}),
1538 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
1539 $summary->{rec_import_errors} = $e->json_query({
1540 select => {$class => [{alias => 'count', column => 'id', transform => 'count', aggregate => 1}]},
1542 where => {queue => $queue_id, import_error => {'!=' => undef}}
1545 if($type eq 'bib') {
1547 # count of all items attached to records in the queue in question
1549 select => {vii => [{alias => 'count', column => 'id', transform => 'count', aggregate => 1}]},
1554 select => {vqbr => ['id']},
1556 where => {queue => $queue_id}
1561 $summary->{total_items} = $e->json_query($query)->[0]->{count};
1563 # count of items we attempted to import, but errored, attached to records in the queue in question
1564 $query->{where}->{import_error} = {'!=' => undef};
1565 $summary->{item_import_errors} = $e->json_query($query)->[0]->{count};
1567 # count of items we successfully imported attached to records in the queue in question
1568 delete $query->{where}->{import_error};
1569 $query->{where}->{import_time} = {'!=' => undef};
1570 $summary->{total_items_imported} = $e->json_query($query)->[0]->{count};
1576 # --------------------------------------------------------------------------------
1577 # Given a list of queued record IDs, imports all items attached to those records
1578 # --------------------------------------------------------------------------------
1579 sub import_record_asset_list_impl {
1580 my($conn, $rec_ids, $requestor) = @_;
1582 my $roe = new_editor(xact=> 1, requestor => $requestor);
1584 # for speed, filter out any records have not been
1585 # imported or have no import items to load
1586 $rec_ids = $roe->json_query({
1587 select => {vqbr => ['id']},
1588 from => {vqbr => 'vii'},
1589 where => {'+vqbr' => {
1591 import_time => {'!=' => undef}
1595 $rec_ids = [map {$_->{id}} @$rec_ids];
1599 total => scalar(@$rec_ids),
1600 step => 1, # how often to respond
1605 for my $rec_id (@$rec_ids) {
1606 my $rec = $roe->retrieve_vandelay_queued_bib_record($rec_id);
1607 my $item_ids = $roe->search_vandelay_import_item(
1608 {record => $rec->id, import_error => undef},
1612 # if any items have no call_number label and a value should be
1613 # applied automatically (via org settings), we want to use the same
1614 # call number label for every copy per org per record.
1615 my $auto_callnumber = {};
1617 for my $item_id (@$item_ids) {
1618 my $e = new_editor(requestor => $requestor, xact => 1);
1619 my $item = $e->retrieve_vandelay_import_item($item_id);
1620 my ($copy, $vol, $evt);
1622 $$report_args{e} = $e;
1623 $$report_args{evt} = undef;
1624 $$report_args{import_item} = $item;
1625 $$report_args{import_error} = undef;
1627 if (my $copy_id = $item->internal_id) { # assignment
1628 # copy matches an existing copy. Overlay instead of create.
1630 $logger->info("vl: performing copy overlay for internal_id=$copy_id");
1632 my $qt = $e->json_query({
1633 select => {vbq => ['queue_type']},
1634 from => {vqbr => 'vbq'},
1635 where => {'+vqbr' => {id => $rec_id}}
1636 })->[0]->{queue_type};
1639 # internal_id for ACQ queues refers to acq.lineitem_detail.id
1640 # pull the real copy id from the acq LID
1642 my $lid = $e->retrieve_acq_lineitem_detail($copy_id);
1644 $$report_args{evt} = $e->die_event;
1645 respond_with_status($report_args);
1648 $copy_id = $lid->eg_copy_id;
1649 $logger->info("vl: performing ACQ copy overlay for copy $copy_id");
1652 $copy = $e->search_asset_copy([
1653 {id => $copy_id, deleted => 'f'},
1654 {flesh => 1, flesh_fields => {acp => ['call_number']}}
1658 $$report_args{evt} = $e->die_event;
1659 respond_with_status($report_args);
1663 # prevent update of unrelated copies
1664 if ($copy->call_number->record != $rec->imported_as) {
1665 $logger->info("vl: attempt to overlay unrelated copy=$copy_id; rec=".$rec->imported_as);
1667 $evt = OpenILS::Event->new('INVALID_IMPORT_COPY_ID',
1668 note => 'Cannot overlay copies for unlinked bib',
1669 bre => $rec->imported_as,
1672 $$report_args{evt} = $evt;
1673 respond_with_status($report_args);
1677 # overlaying copies requires an extra permission
1678 if (!$e->allowed("IMPORT_OVERLAY_COPY", $copy->call_number->owning_lib)) {
1679 $$report_args{evt} = $e->die_event;
1680 respond_with_status($report_args);
1684 # are we updating the call-number?
1685 if ($item->call_number and $item->call_number ne $copy->call_number->label) {
1687 my $count = $e->json_query({
1688 select => {acp => [{
1691 transform => 'count',
1697 call_number => $copy->call_number->id
1702 # if this is the only copy attached to this
1703 # callnumber, just update the callnumber
1705 $logger->info("vl: updating callnumber label in copy overlay");
1707 $copy->call_number->label($item->call_number);
1708 if (!$e->update_asset_call_number($copy->call_number)) {
1709 $$report_args{evt} = $e->die_event;
1710 respond_with_status($report_args);
1716 # otherwise, move the copy to a new/existing
1717 # call-number with the given label/owner
1718 # note that overlay does not allow the owning_lib
1719 # to be changed. Should it?
1721 $logger->info("vl: moving copy to new callnumber in copy overlay");
1724 OpenILS::Application::Cat::AssetCommon->find_or_create_volume(
1725 $e, $item->call_number,
1726 $copy->call_number->record,
1727 $copy->call_number->owning_lib
1731 $$report_args{evt} = $evt;
1732 respond_with_status($report_args);
1736 $copy->call_number($vol);
1740 # for every field that has a non-'' value, overlay the copy value
1741 foreach (qw/ barcode location circ_lib status
1742 circulate deposit deposit_amount ref holdable
1743 price circ_as_type alert_message opac_visible circ_modifier/) {
1745 my $val = $item->$_();
1746 $copy->$_($val) if $val and $val ne '';
1749 # de-flesh for update
1750 $copy->call_number($copy->call_number->id);
1751 $copy->ischanged(1);
1753 $evt = OpenILS::Application::Cat::AssetCommon->
1754 update_fleshed_copies($e, {all => 1}, undef, [$copy]);
1757 $$report_args{evt} = $evt;
1758 respond_with_status($report_args);
1764 # Creating a new copy
1765 $logger->info("vl: creating new copy in import");
1767 # appply defaults values from org settings as needed
1768 # if $auto_callnumber is unset, it will be set within
1769 apply_import_item_defaults($e, $item, $auto_callnumber);
1771 # --------------------------------------------------------------------------------
1772 # Find or create the volume
1773 # --------------------------------------------------------------------------------
1775 OpenILS::Application::Cat::AssetCommon->find_or_create_volume(
1776 $e, $item->call_number, $rec->imported_as, $item->owning_lib);
1779 $$report_args{evt} = $evt;
1780 respond_with_status($report_args);
1784 # --------------------------------------------------------------------------------
1785 # Create the new copy
1786 # --------------------------------------------------------------------------------
1787 $copy = Fieldmapper::asset::copy->new;
1788 $copy->loan_duration(2);
1789 $copy->fine_level(2);
1790 $copy->barcode($item->barcode);
1791 $copy->location($item->location);
1792 $copy->circ_lib($item->circ_lib || $item->owning_lib);
1793 $copy->status( defined($item->status) ? $item->status : OILS_COPY_STATUS_IN_PROCESS );
1794 $copy->circulate($item->circulate);
1795 $copy->deposit($item->deposit);
1796 $copy->deposit_amount($item->deposit_amount);
1797 $copy->ref($item->ref);
1798 $copy->holdable($item->holdable);
1799 $copy->price($item->price);
1800 $copy->circ_as_type($item->circ_as_type);
1801 $copy->alert_message($item->alert_message);
1802 $copy->opac_visible($item->opac_visible);
1803 $copy->circ_modifier($item->circ_modifier);
1805 # --------------------------------------------------------------------------------
1806 # Check for dupe barcode
1807 # --------------------------------------------------------------------------------
1808 if($evt = OpenILS::Application::Cat::AssetCommon->create_copy($e, $vol, $copy)) {
1809 $$report_args{evt} = $evt;
1810 $$report_args{import_error} = 'import.item.duplicate.barcode'
1811 if $evt->{textcode} eq 'ITEM_BARCODE_EXISTS';
1812 respond_with_status($report_args);
1816 # --------------------------------------------------------------------------------
1818 # --------------------------------------------------------------------------------
1819 $evt = OpenILS::Application::Cat::AssetCommon->create_copy_note(
1820 $e, $copy, '', $item->pub_note, 1) if $item->pub_note;
1823 $$report_args{evt} = $evt;
1824 respond_with_status($report_args);
1828 $evt = OpenILS::Application::Cat::AssetCommon->create_copy_note(
1829 $e, $copy, '', $item->priv_note) if $item->priv_note;
1832 $$report_args{evt} = $evt;
1833 respond_with_status($report_args);
1838 # set the import data on the import item
1839 $item->imported_as($copy->id); # $copy->id is set by create_copy() ^--
1840 $item->import_time('now');
1842 unless($e->update_vandelay_import_item($item)) {
1843 $$report_args{evt} = $e->die_event;
1844 respond_with_status($report_args);
1848 # --------------------------------------------------------------------------------
1849 # Item import succeeded
1850 # --------------------------------------------------------------------------------
1852 $$report_args{in_count}++;
1853 respond_with_status($report_args);
1854 $logger->info("vl: successfully imported item " . $item->barcode);
1862 sub apply_import_item_defaults {
1863 my ($e, $item, $auto_cn) = @_;
1864 my $org = $item->owning_lib || $item->circ_lib;
1865 my %c = %item_defaults_cache;
1867 # fetch and cache the org unit setting value (unless
1868 # it's already cached) and return the value to the caller
1871 return $c{$org}{$name} if defined $c{$org}{$name};
1872 my $sname = "vandelay.item.$name";
1873 $c{$org}{$name} = $U->ou_ancestor_setting_value($org, $sname, $e);
1874 $c{$org}{$name} = '' unless defined $c{$org}{$name};
1875 return $c{$org}{$name};
1878 if (!$item->barcode) {
1880 if ($set->('barcode.auto')) {
1882 my $pfx = $set->('barcode.prefix') || 'VAN';
1883 my $barcode = $pfx . $item->record . $item->id;
1885 $logger->info("vl: using auto barcode $barcode for ".$item->id);
1886 $item->barcode($barcode);
1889 $logger->error("vl: no barcode (or defualt) for item ".$item->id);
1893 if (!$item->call_number) {
1895 if ($set->('call_number.auto')) {
1897 if (!$auto_cn->{$org}) {
1898 my $pfx = $set->('call_number.prefix') || 'VAN';
1900 # use the ID of the first item to differentiate this
1901 # call number from others linked to the same record
1902 $auto_cn->{$org} = $pfx . $item->record . $item->id;
1905 $logger->info("vl: using auto call number ".$auto_cn->{$org});
1906 $item->call_number($auto_cn->{$org});
1909 $logger->error("vl: no call number or default for item ".$item->id);
1915 sub respond_with_status {
1919 # If the import failed, track the failure reason
1921 my $error = $$args{import_error};
1922 my $evt = $$args{evt};
1924 if($error or $evt) {
1926 my $item = $$args{import_item};
1927 $logger->info("vl: unable to import item " . $item->barcode);
1929 $error ||= 'general.unknown';
1930 $item->import_error($error);
1933 my $detail = sprintf("%s : %s", $evt->{textcode}, substr($evt->{desc}, 0, 140));
1934 $item->error_detail($detail);
1937 # state of the editor is unknown at this point. Force a rollback and start over.
1939 $e = new_editor(xact => 1);
1940 $e->update_vandelay_import_item($item);
1944 if($$args{report_all} or ($$args{progress} % $$args{step}) == 0) {
1945 $$args{conn}->respond({
1946 total => $$args{total},
1947 progress => $$args{progress},
1948 success_count => $$args{success_count},
1951 $$args{step} *= 2 unless $$args{step} == 256;
1957 __PACKAGE__->register_method(
1958 api_name => "open-ils.vandelay.match_set.get_tree",
1959 method => "match_set_get_tree",
1963 desc => q/For a given vms object, return a tree of match set points
1964 represented by a vmsp object with recursively fleshed
1969 sub match_set_get_tree {
1970 my ($self, $conn, $authtoken, $match_set_id) = @_;
1972 $match_set_id = int($match_set_id) or return;
1974 my $e = new_editor("authtoken" => $authtoken);
1975 $e->checkauth or return $e->die_event;
1977 my $set = $e->retrieve_vandelay_match_set($match_set_id) or
1978 return $e->die_event;
1980 $e->allowed("ADMIN_IMPORT_MATCH_SET", $set->owner) or
1981 return $e->die_event;
1983 my $tree = $e->search_vandelay_match_set_point([
1984 {"match_set" => $match_set_id, "parent" => undef},
1985 {"flesh" => -1, "flesh_fields" => {"vmsp" => ["children"]}}
1986 ]) or return $e->die_event;
1992 __PACKAGE__->register_method(
1993 api_name => "open-ils.vandelay.match_set.update",
1994 method => "match_set_update_tree",
1998 desc => q/Replace any vmsp objects associated with a given (by ID) vms
1999 with the given objects (recursively fleshed vmsp tree)./
2003 sub _walk_new_vmsp {
2004 my ($e, $match_set_id, $node, $parent_id) = @_;
2006 my $point = new Fieldmapper::vandelay::match_set_point;
2007 $point->parent($parent_id);
2008 $point->match_set($match_set_id);
2009 $point->$_($node->$_) for (qw/bool_op svf tag subfield negate quality/);
2011 $e->create_vandelay_match_set_point($point) or return $e->die_event;
2013 $parent_id = $e->data->id;
2014 if ($node->children && @{$node->children}) {
2015 for (@{$node->children}) {
2016 return $e->die_event if
2017 _walk_new_vmsp($e, $match_set_id, $_, $parent_id);
2024 sub match_set_update_tree {
2025 my ($self, $conn, $authtoken, $match_set_id, $tree) = @_;
2027 my $e = new_editor("xact" => 1, "authtoken" => $authtoken);
2028 $e->checkauth or return $e->die_event;
2030 my $set = $e->retrieve_vandelay_match_set($match_set_id) or
2031 return $e->die_event;
2033 $e->allowed("ADMIN_IMPORT_MATCH_SET", $set->owner) or
2034 return $e->die_event;
2036 my $existing = $e->search_vandelay_match_set_point([
2037 {"match_set" => $match_set_id},
2038 {"order_by" => {"vmsp" => "id DESC"}}
2039 ]) or return $e->die_event;
2041 # delete points, working up from leaf points to the root
2043 for my $point (shift @$existing) {
2044 if( grep {$_->parent eq $point->id} @$existing) {
2045 push(@$existing, $point);
2047 $e->delete_vandelay_match_set_point($point) or return $e->die_event;
2052 _walk_new_vmsp($e, $match_set_id, $tree);
2054 $e->commit or return $e->die_event;
2057 __PACKAGE__->register_method(
2058 api_name => 'open-ils.vandelay.bib_queue.to_bucket',
2059 method => 'bib_queue_to_bucket',
2063 desc => q/Add to or create a new bib container (bucket) with the successfully
2064 imported contents of a vandelay queue. Any user that has Vandelay
2065 queue create permissions can append or create buckets from his-her own queues./,
2067 {desc => 'Authtoken', type => 'string'},
2068 {desc => 'Queue ID', type => 'number'},
2069 {desc => 'Bucket Name', type => 'string'}
2071 return => {desc => q/
2072 {bucket => $bucket, addcount => number-of-items-added-to-bucket, item_count => total-bucket-item-count} on success,
2073 {add_count => 0} if there is nothing to do, and Event on error/}
2077 sub bib_queue_to_bucket {
2078 my ($self, $conn, $auth, $q_id, $bucket_name) = @_;
2080 my $e = new_editor(xact => 1, authtoken => $auth);
2081 return $e->die_event unless $e->checkauth;
2083 my $queue = $e->retrieve_vandelay_bib_queue($q_id)
2084 or return $e->die_event;
2086 return OpenILS::Event->new('BAD_PARAMS',
2087 note => q/Bucket creator must be queue owner/)
2088 unless $queue->owner == $e->requestor->id;
2090 # find the bib IDs that will go into the bucket
2091 my $bib_ids = $e->json_query({
2092 select => {vqbr => ['imported_as']},
2094 where => {queue => $q_id, imported_as => {'!=' => undef}}
2097 if (!@$bib_ids) { # no records to add
2099 return {add_count => 0};
2102 # allow user to add to an existing bucket by name
2103 my $bucket = $e->search_container_biblio_record_entry_bucket({
2104 owner => $e->requestor->id,
2105 name => $bucket_name,
2106 btype => 'vandelay_queue'
2109 # if the bucket does not exist, create a new one
2111 $bucket = Fieldmapper::container::biblio_record_entry_bucket->new;
2112 $bucket->name($bucket_name);
2113 $bucket->owner($e->requestor->id);
2114 $bucket->btype('vandelay_queue');
2116 $e->create_container_biblio_record_entry_bucket($bucket)
2117 or return $e->die_event;
2120 # create the new bucket items
2121 for my $bib_id ( map {$_->{imported_as}} @$bib_ids ) {
2122 my $item = Fieldmapper::container::biblio_record_entry_bucket_item->new;
2123 $item->target_biblio_record_entry($bib_id);
2124 $item->bucket($bucket->id);
2125 $e->create_container_biblio_record_entry_bucket_item($item)
2126 or return $e->die_event;
2129 # re-fetch the bucket to pick up the correct create_time
2130 $bucket = $e->retrieve_container_biblio_record_entry_bucket($bucket->id)
2131 or return $e->die_event;
2133 # get the total count of items in this bucket
2134 my $count = $e->json_query({
2135 select => {cbrebi => [{
2137 transform => 'count',
2142 where => {bucket => $bucket->id}
2149 add_count => scalar(@$bib_ids), # items added to the bucket
2150 item_count => $count->{count} # total items in buckets