1 package OpenILS::Application::Vandelay;
2 use strict; use warnings;
3 use OpenILS::Application;
4 use base qw/OpenILS::Application/;
5 use Unicode::Normalize;
6 use OpenSRF::EX qw/:try/;
7 use OpenSRF::AppSession;
8 use OpenSRF::Utils::SettingsClient;
9 use OpenSRF::Utils::Cache;
10 use OpenILS::Utils::Fieldmapper;
11 use OpenILS::Utils::CStoreEditor qw/:funcs/;
14 use MARC::File::XML ( BinaryEncoding => 'UTF-8' );
15 use OpenILS::Utils::Fieldmapper;
16 use Time::HiRes qw(time);
17 use OpenSRF::Utils::Logger qw/$logger/;
19 use OpenILS::Const qw/:const/;
20 use OpenILS::Application::AppUtils;
21 use OpenILS::Application::Cat::BibCommon;
22 use OpenILS::Application::Cat::AuthCommon;
23 use OpenILS::Application::Cat::AssetCommon;
24 my $U = 'OpenILS::Application::AppUtils';
26 # A list of LDR/06 values from http://loc.gov/marc
53 # --------------------------------------------------------------------------------
56 sub create_bib_queue {
63 my $import_def = shift;
65 my $e = new_editor(authtoken => $auth, xact => 1);
67 return $e->die_event unless $e->checkauth;
68 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE');
69 $owner ||= $e->requestor->id;
71 if ($e->search_vandelay_bib_queue( {name => $name, owner => $owner, queue_type => $type})->[0]) {
73 return OpenILS::Event->new('BIB_QUEUE_EXISTS')
76 my $queue = new Fieldmapper::vandelay::bib_queue();
77 $queue->name( $name );
78 $queue->owner( $owner );
79 $queue->queue_type( $type ) if ($type);
80 $queue->item_attr_def( $import_def ) if ($import_def);
82 my $new_q = $e->create_vandelay_bib_queue( $queue );
83 return $e->die_event unless ($new_q);
88 __PACKAGE__->register_method(
89 api_name => "open-ils.vandelay.bib_queue.create",
90 method => "create_bib_queue",
96 sub create_auth_queue {
104 my $e = new_editor(authtoken => $auth, xact => 1);
106 return $e->die_event unless $e->checkauth;
107 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE');
108 $owner ||= $e->requestor->id;
110 if ($e->search_vandelay_bib_queue({name => $name, owner => $owner, queue_type => $type})->[0]) {
112 return OpenILS::Event->new('AUTH_QUEUE_EXISTS')
115 my $queue = new Fieldmapper::vandelay::authority_queue();
116 $queue->name( $name );
117 $queue->owner( $owner );
118 $queue->queue_type( $type ) if ($type);
120 my $new_q = $e->create_vandelay_authority_queue( $queue );
121 $e->die_event unless ($new_q);
126 __PACKAGE__->register_method(
127 api_name => "open-ils.vandelay.authority_queue.create",
128 method => "create_auth_queue",
133 sub add_record_to_bib_queue {
140 my $bib_source = shift;
142 my $e = new_editor(authtoken => $auth, xact => 1);
144 $queue = $e->retrieve_vandelay_bib_queue($queue);
146 return $e->die_event unless $e->checkauth;
147 return $e->die_event unless
148 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
149 $e->allowed('CREATE_BIB_IMPORT_QUEUE'));
151 my $new_rec = _add_bib_rec($e, $marc, $queue->id, $purpose, $bib_source);
153 return $e->die_event unless ($new_rec);
157 __PACKAGE__->register_method(
158 api_name => "open-ils.vandelay.queued_bib_record.create",
159 method => "add_record_to_bib_queue",
169 my $bib_source = shift;
171 my $rec = new Fieldmapper::vandelay::queued_bib_record();
173 $rec->queue( $queue );
174 $rec->purpose( $purpose ) if ($purpose);
175 $rec->bib_source($bib_source);
177 return $e->create_vandelay_queued_bib_record( $rec );
180 sub add_record_to_authority_queue {
188 my $e = new_editor(authtoken => $auth, xact => 1);
190 $queue = $e->retrieve_vandelay_authority_queue($queue);
192 return $e->die_event unless $e->checkauth;
193 return $e->die_event unless
194 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
195 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE'));
197 my $new_rec = _add_auth_rec($e, $marc, $queue->id, $purpose);
199 return $e->die_event unless ($new_rec);
203 __PACKAGE__->register_method(
204 api_name => "open-ils.vandelay.queued_authority_record.create",
205 method => "add_record_to_authority_queue",
216 my $rec = new Fieldmapper::vandelay::queued_authority_record();
218 $rec->queue( $queue );
219 $rec->purpose( $purpose ) if ($purpose);
221 return $e->create_vandelay_queued_authority_record( $rec );
228 my $fingerprint = shift || '';
229 my $queue_id = shift;
231 my $filename = shift;
232 my $bib_source = shift;
234 my $e = new_editor(authtoken => $auth, xact => 1);
235 return $e->die_event unless $e->checkauth;
238 my $type = $self->{record_type};
241 $queue = $e->retrieve_vandelay_bib_queue($queue_id) or return $e->die_event;
243 $queue = $e->retrieve_vandelay_authority_queue($queue_id) or return $e->die_event;
246 my $evt = check_queue_perms($e, $type, $queue);
247 return $evt if ($evt);
249 my $cache = new OpenSRF::Utils::Cache();
252 my $data = $cache->get_cache('vandelay_import_spool_' . $fingerprint);
253 $purpose = $data->{purpose};
254 $filename = $data->{path};
255 $bib_source = $data->{bib_source};
258 unless(-r $filename) {
259 $logger->error("unable to read MARC file $filename");
260 return -1; # make this an event XXX
263 $logger->info("vandelay spooling $fingerprint purpose=$purpose file=$filename");
265 my $marctype = 'USMARC';
268 $marctype = 'XML' if (getc(F) =~ /^\D/o);
271 my $batch = new MARC::Batch ($marctype, $filename);
274 my $response_scale = 10;
277 while (try { $r = $batch->next } otherwise { $r = -1 }) {
279 $logger->warn("Processing of record $count in set $filename failed. Skipping this record");
283 $logger->info("processing record $count");
286 (my $xml = $r->as_xml_record()) =~ s/\n//sog;
287 $xml =~ s/^<\?xml.+\?\s*>//go;
288 $xml =~ s/>\s+</></go;
289 $xml =~ s/\p{Cc}//go;
290 $xml = $U->entityize($xml);
291 $xml =~ s/[\x00-\x1f]//go;
294 # Check the leader to ensure we've got something resembling the expected
295 # Allow spaces to give records the benefit of the doubt
296 my $ldr_type = substr($r->leader(), 6, 1);
297 if ($type eq 'bib' && ($record_types{$ldr_type}) eq 'bib' || $ldr_type eq ' ') {
298 $qrec = _add_bib_rec( $e, $xml, $queue_id, $purpose, $bib_source ) or return $e->die_event;
299 } elsif ($type eq 'auth' && ($record_types{$ldr_type}) eq 'auth' || $ldr_type eq ' ') {
300 $qrec = _add_auth_rec( $e, $xml, $queue_id, $purpose ) or return $e->die_event;
302 # I don't know how to handle this type; rock on
303 $logger->error("In process_spool(), type was $type and leader type was $ldr_type ; not currently supported");
307 if($self->api_name =~ /stream_results/ and $qrec) {
308 $client->respond($qrec->id)
310 $client->respond($count) if (++$count % $response_scale) == 0;
311 $response_scale *= 10 if ($count == ($response_scale * 10));
315 $logger->warn("Encountered a bad record at Vandelay ingest: ".$error);
321 $cache->delete_cache('vandelay_import_spool_' . $fingerprint) if $fingerprint;
325 __PACKAGE__->register_method(
326 api_name => "open-ils.vandelay.bib.process_spool",
327 method => "process_spool",
333 __PACKAGE__->register_method(
334 api_name => "open-ils.vandelay.auth.process_spool",
335 method => "process_spool",
339 record_type => 'auth'
342 __PACKAGE__->register_method(
343 api_name => "open-ils.vandelay.bib.process_spool.stream_results",
344 method => "process_spool",
351 __PACKAGE__->register_method(
352 api_name => "open-ils.vandelay.auth.process_spool.stream_results",
353 method => "process_spool",
358 record_type => 'auth'
361 __PACKAGE__->register_method(
362 api_name => "open-ils.vandelay.bib_queue.records.retrieve",
363 method => 'retrieve_queued_records',
369 __PACKAGE__->register_method(
370 api_name => "open-ils.vandelay.auth_queue.records.retrieve",
371 method => 'retrieve_queued_records',
375 record_type => 'auth'
378 __PACKAGE__->register_method(
379 api_name => "open-ils.vandelay.bib_queue.records.matches.retrieve",
380 method => 'retrieve_queued_records',
384 record_type => 'bib',
386 desc => q/Only retrieve queued bib records that have matches against existing records/
389 __PACKAGE__->register_method(
390 api_name => "open-ils.vandelay.auth_queue.records.matches.retrieve",
391 method => 'retrieve_queued_records',
395 record_type => 'auth',
397 desc => q/Only retrieve queued authority records that have matches against existing records/
401 sub retrieve_queued_records {
402 my($self, $conn, $auth, $queue_id, $options) = @_;
405 my $limit = $$options{limit} || 20;
406 my $offset = $$options{offset} || 0;
407 my $type = $self->{record_type};
409 my $e = new_editor(authtoken => $auth, xact => 1);
410 return $e->die_event unless $e->checkauth;
414 $queue = $e->retrieve_vandelay_bib_queue($queue_id) or return $e->die_event;
416 $queue = $e->retrieve_vandelay_authority_queue($queue_id) or return $e->die_event;
418 my $evt = check_queue_perms($e, $type, $queue);
419 return $evt if ($evt);
421 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
423 select => {$class => ['id']},
425 where => {queue => $queue_id},
427 order_by => {$class => ['id']},
432 $query->{where}->{import_time} = undef if $$options{non_imported};
434 if($$options{with_import_error}) {
436 $query->{from} = {$class => {vii => {type => 'right'}}};
437 $query->{where}->{'-or'} = [
438 {'+vqbr' => {import_error => {'!=' => undef}}},
439 {'+vii' => {import_error => {'!=' => undef}}}
444 if($$options{with_rec_import_error}) {
445 $query->{where}->{import_error} = {'!=' => undef};
447 } elsif( $$options{with_item_import_error} and $type eq 'bib') {
449 $query->{from} = {$class => 'vii'};
450 $query->{where}->{'+vii'} = {import_error => {'!=' => undef}};
454 if($self->api_name =~ /matches/) {
455 # find only records that have matches
456 my $mclass = $type eq 'bib' ? 'vbm' : 'vam';
457 $query->{from} = {$class => {$mclass => {type => 'right'}}};
460 my $record_ids = $e->json_query($query);
462 my $retrieve = ($type eq 'bib') ?
463 'retrieve_vandelay_queued_bib_record' : 'retrieve_vandelay_queued_authority_record';
465 for my $rec_id (@$record_ids) {
466 my $flesh = ['attributes', 'matches'];
467 push(@$flesh, 'import_items') if $$options{flesh_import_items};
468 my $params = {flesh => 1, flesh_fields => {$class => $flesh}};
469 my $rec = $e->$retrieve([$rec_id->{id}, $params]);
470 $rec->clear_marc if $$options{clear_marc};
471 $conn->respond($rec);
478 __PACKAGE__->register_method(
479 api_name => 'open-ils.vandelay.import_item.queue.retrieve',
480 method => 'retrieve_queue_import_items',
486 Returns Import Item (vii) objects for the selected queue.
488 with_import_error : only return items that failed to import
492 sub retrieve_queue_import_items {
493 my($self, $conn, $auth, $q_id, $options) = @_;
496 my $limit = $$options{limit} || 20;
497 my $offset = $$options{offset} || 0;
499 my $e = new_editor(authtoken => $auth);
500 return $e->event unless $e->checkauth;
502 my $queue = $e->retrieve_vandelay_bib_queue($q_id) or return $e->event;
503 my $evt = check_queue_perms($e, 'bib', $queue);
507 select => {vii => ['id']},
515 filter => {id => $q_id}
521 order_by => {'vii' => ['record','id']},
526 $query->{where} = {'+vii' => {import_error => {'!=' => undef}}}
527 if $$options{with_import_error};
529 my $items = $e->json_query($query);
530 for my $item (@$items) {
531 $conn->respond($e->retrieve_vandelay_import_item($item->{id}));
537 sub check_queue_perms {
538 my($e, $type, $queue) = @_;
539 if ($type eq 'bib') {
540 return $e->die_event unless
541 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
542 $e->allowed('CREATE_BIB_IMPORT_QUEUE'));
544 return $e->die_event unless
545 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
546 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE'));
552 __PACKAGE__->register_method(
553 api_name => "open-ils.vandelay.bib_record.list.import",
554 method => 'import_record_list',
561 __PACKAGE__->register_method(
562 api_name => "open-ils.vandelay.auth_record.list.import",
563 method => 'import_record_list',
567 record_type => 'auth'
570 sub import_record_list {
571 my($self, $conn, $auth, $rec_ids, $args) = @_;
572 my $e = new_editor(authtoken => $auth, xact => 1);
573 return $e->die_event unless $e->checkauth;
575 my $err = import_record_list_impl($self, $conn, $rec_ids, $e->requestor, $args);
578 return {complete => 1};
582 __PACKAGE__->register_method(
583 api_name => "open-ils.vandelay.bib_queue.import",
584 method => 'import_queue',
592 __PACKAGE__->register_method(
593 api_name => "open-ils.vandelay.auth_queue.import",
594 method => 'import_queue',
599 record_type => 'auth'
601 __PACKAGE__->register_method(
602 api_name => "open-ils.vandelay.bib_queue.nomatch.import",
603 method => 'import_queue',
608 desc => q/Only import records that have no collisions/
614 __PACKAGE__->register_method(
615 api_name => "open-ils.vandelay.auth_queue.nomatch.import",
616 method => 'import_queue',
621 desc => q/Only import records that have no collisions/
624 record_type => 'auth'
627 my($self, $conn, $auth, $q_id, $options) = @_;
628 my $e = new_editor(authtoken => $auth, xact => 1);
629 return $e->die_event unless $e->checkauth;
631 my $type = $self->{record_type};
632 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
634 my $query = {queue => $q_id, import_time => undef};
636 if($self->api_name =~ /nomatch/) {
637 my $matched_recs = queued_records_with_matches($e, $type, $q_id, undef, undef, {import_time => undef});
638 $query->{id} = {'not in' => $matched_recs} if @$matched_recs;
641 my $search = ($type eq 'bib') ?
642 'search_vandelay_queued_bib_record' : 'search_vandelay_queued_authority_record';
643 my $rec_ids = $e->$search($query, {idlist => 1});
644 my $err = import_record_list_impl($self, $conn, $rec_ids, $e->requestor, $options);
645 try {$e->rollback} otherwise {}; # only using this to make the read authoritative -- don't die from it
647 return {complete => 1};
650 # returns a list of queued record IDs for a given queue that
651 # have at least one entry in the match table
652 sub queued_records_with_matches {
653 my($e, $type, $q_id, $limit, $offset, $filter) = @_;
655 my $match_class = 'vbm';
656 my $rec_class = 'vqbr';
657 if($type eq 'auth') {
658 $match_class = 'vam';
663 $filter->{queue} = $q_id;
667 select => {$match_class => ['queued_record']},
672 fkey => 'queued_record',
679 if($limit or defined $offset) {
682 $query->{limit} = $limit;
683 $query->{offset} = $offset;
686 my $data = $e->json_query($query);
687 return [ map {$_->{queued_record}} @$data ];
691 sub import_record_list_impl {
692 my($self, $conn, $rec_ids, $requestor, $args) = @_;
694 my $overlay_map = $args->{overlay_map} || {};
695 my $type = $self->{record_type};
702 total => scalar(@$rec_ids),
703 report_all => $$args{report_all}
706 my $auto_overlay_exact = $$args{auto_overlay_exact};
707 my $auto_overlay_1match = $$args{auto_overlay_1match};
708 my $merge_profile = $$args{merge_profile};
709 my $bib_source = $$args{bib_source};
711 my $overlay_func = 'vandelay.overlay_bib_record';
712 my $auto_overlay_func = 'vandelay.auto_overlay_bib_record';
713 my $retrieve_func = 'retrieve_vandelay_queued_bib_record';
714 my $update_func = 'update_vandelay_queued_bib_record';
715 my $search_func = 'search_vandelay_queued_bib_record';
716 my $retrieve_queue_func = 'retrieve_vandelay_bib_queue';
717 my $update_queue_func = 'update_vandelay_bib_queue';
718 my $rec_class = 'vqbr';
720 my $editor = new_editor();
723 my $sources = $editor->search_config_bib_source({id => {'!=' => undef}});
724 $bib_sources{$_->id} = $_->source for @$sources;
726 if($type eq 'auth') {
727 $overlay_func =~ s/bib/auth/o;
728 $auto_overlay_func = s/bib/auth/o;
729 $retrieve_func =~ s/bib/authority/o;
730 $retrieve_queue_func =~ s/bib/authority/o;
731 $update_queue_func =~ s/bib/authority/o;
732 $update_func =~ s/bib/authority/o;
733 $search_func =~ s/bib/authority/o;
738 for my $rec_id (@$rec_ids) {
741 my $overlay_target = $overlay_map->{$rec_id};
743 my $e = new_editor(xact => 1);
744 $e->requestor($requestor);
746 $report_args{progress}++;
747 $report_args{e} = $e;
749 my $rec = $e->$retrieve_func([
752 flesh_fields => { $rec_class => ['matches']},
757 finish_rec_import_attempt(%report_args, evt => $e->event);
761 $report_args{rec} = $rec;
763 if($rec->import_time) {
768 $queues{$rec->queue} = 1;
773 if(defined $overlay_target) {
774 # Caller chose an explicit overlay target
776 my $res = $e->json_query(
787 if($res and ($res = $res->[0])) {
789 if($res->{$overlay_func} eq 't') {
790 $logger->info("vl: $type direct overlay succeeded for queued rec " .
791 $rec->id . " and overlay target $overlay_target");
797 $logger->error("vl: Error attempting overlay with func=$overlay_func, profile=$merge_profile, record=$rec_id");
802 if($auto_overlay_1match) {
803 # caller says to overlay if there is exactly 1 match
805 my %match_recs = map { $_->eg_record => 1 } @{$rec->matches};
807 if( scalar(keys %match_recs) == 1) { # all matches point to the same record
809 my $res = $e->json_query(
814 $rec->matches->[0]->eg_record,
820 if($res and ($res = $res->[0])) {
822 if($res->{$overlay_func} eq 't') {
823 $logger->info("vl: $type overlay-1match succeeded for queued rec " . $rec->id);
829 $logger->error("vl: Error attempting overlay with func=$overlay_func, profile=$merge_profile, record=$rec_id");
834 if(!$imported and !$error and $auto_overlay_exact and scalar(@{$rec->matches}) == 1 ) {
836 # caller says to overlay if there is an /exact/ match
838 my $res = $e->json_query(
848 if($res and ($res = $res->[0])) {
850 if($res->{$auto_overlay_func} eq 't') {
851 $logger->info("vl: $type auto-overlay succeeded for queued rec " . $rec->id);
854 $logger->info("vl: $type auto-overlay failed for queued rec " . $rec->id);
859 $logger->error("vl: Error attempting overlay with func=$auto_overlay_func, profile=$merge_profile, record=$rec_id");
863 if(!$imported and !$error) {
865 # No overlay / merge occurred. Do a traditional record import by creating a new record
867 $logger->info("vl: creating new $type record for queued record $rec_id");
869 $record = OpenILS::Application::Cat::BibCommon->biblio_record_xml_import($e, $rec->marc, $bib_sources{$rec->bib_source});
872 $record = OpenILS::Application::Cat::AuthCommon->import_authority_record($e, $rec->marc); #$source);
875 if($U->event_code($record)) {
876 my $import_error = 'import.duplicate.tcn' if $record->{textcode} eq 'TCN_EXISTS';
877 finish_rec_import_attempt(%report_args, import_error => $import_error, evt => $record);
881 $logger->info("vl: successfully imported new $type record");
882 $rec->imported_as($record->id);
883 $rec->import_time('now');
885 $imported = 1 if $e->$update_func($rec);
891 push @success_rec_ids, $rec_id;
892 finish_rec_import_attempt(%report_args);
895 # Send an update whenever there's an error
896 finish_rec_import_attempt(%report_args, evt => $e->event);
900 # see if we need to mark any queues as complete
901 for my $q_id (keys %queues) {
903 my $e = new_editor(xact => 1);
904 my $remaining = $e->$search_func(
905 [{queue => $q_id, import_time => undef}, {limit =>1}], {idlist => 1});
907 unless(@$remaining) {
908 my $queue = $e->$retrieve_queue_func($q_id);
910 unless($U->is_true($queue->complete)) {
911 $queue->complete('t');
912 $e->$update_queue_func($queue) or return $e->die_event;
921 import_record_asset_list_impl($conn, \@success_rec_ids, $requestor);
923 $conn->respond({total => $report_args{total}, progress => $report_args{progress}});
927 # tracks any import errors, commits the current xact, responds to the client
928 sub finish_rec_import_attempt {
930 my $evt = $args{evt};
931 my $rec = $args{rec};
934 my $error = $args{import_error};
935 $error = 'general.unknown' if $evt and not $error;
942 # since an error occurred, there's no guarantee the transaction wasn't
943 # rolled back. force a rollback and create a new editor.
945 $e = new_editor(xact => 1);
946 $rec->import_error($error);
949 my $detail = sprintf("%s : %s", $evt->{textcode}, substr($evt->{desc}, 0, 140));
950 $rec->error_detail($detail);
953 my $method = 'update_vandelay_queued_bib_record';
954 $method =~ s/bib/authority/ if $args{type} eq 'auth';
955 $e->$method($rec) and $e->commit or $e->rollback;
959 $rec->clear_import_error;
960 $rec->clear_error_detail;
965 # requested queued record was not found
970 if($args{report_all} or ($args{progress} % $args{step}) == 0) {
972 $args{conn}->respond({
973 total => $args{total},
974 progress => $args{progress},
975 imported => ($rec) ? $rec->id : undef,
979 # report often at first, climb quickly, then hold steady
980 $args{step} *= 2 unless $args{step} == 256;
988 __PACKAGE__->register_method(
989 api_name => "open-ils.vandelay.bib_queue.owner.retrieve",
990 method => 'owner_queue_retrieve',
996 __PACKAGE__->register_method(
997 api_name => "open-ils.vandelay.authority_queue.owner.retrieve",
998 method => 'owner_queue_retrieve',
1002 record_type => 'auth'
1005 sub owner_queue_retrieve {
1006 my($self, $conn, $auth, $owner_id, $filters) = @_;
1007 my $e = new_editor(authtoken => $auth, xact => 1);
1008 return $e->die_event unless $e->checkauth;
1009 $owner_id = $e->requestor->id; # XXX add support for viewing other's queues?
1012 my $search = {owner => $owner_id};
1013 $search->{$_} = $filters->{$_} for keys %$filters;
1015 if($self->{record_type} eq 'bib') {
1016 $queues = $e->search_vandelay_bib_queue(
1017 [$search, {order_by => {vbq => 'evergreen.lowercase(name)'}}]);
1019 $queues = $e->search_vandelay_authority_queue(
1020 [$search, {order_by => {vaq => 'evergreen.lowercase(name)'}}]);
1022 $conn->respond($_) for @$queues;
1027 __PACKAGE__->register_method(
1028 api_name => "open-ils.vandelay.bib_queue.delete",
1029 method => "delete_queue",
1032 record_type => 'bib'
1034 __PACKAGE__->register_method(
1035 api_name => "open-ils.vandelay.auth_queue.delete",
1036 method => "delete_queue",
1039 record_type => 'auth'
1043 my($self, $conn, $auth, $q_id) = @_;
1044 my $e = new_editor(xact => 1, authtoken => $auth);
1045 return $e->die_event unless $e->checkauth;
1046 if($self->{record_type} eq 'bib') {
1047 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE');
1048 my $queue = $e->retrieve_vandelay_bib_queue($q_id)
1049 or return $e->die_event;
1050 $e->delete_vandelay_bib_queue($queue)
1051 or return $e->die_event;
1053 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE');
1054 my $queue = $e->retrieve_vandelay_authority_queue($q_id)
1055 or return $e->die_event;
1056 $e->delete_vandelay_authority_queue($queue)
1057 or return $e->die_event;
1064 __PACKAGE__->register_method(
1065 api_name => "open-ils.vandelay.queued_bib_record.html",
1066 method => 'queued_record_html',
1070 record_type => 'bib'
1072 __PACKAGE__->register_method(
1073 api_name => "open-ils.vandelay.queued_authority_record.html",
1074 method => 'queued_record_html',
1078 record_type => 'auth'
1081 sub queued_record_html {
1082 my($self, $conn, $auth, $rec_id) = @_;
1083 my $e = new_editor(xact=>1,authtoken => $auth);
1084 return $e->die_event unless $e->checkauth;
1086 if($self->{record_type} eq 'bib') {
1087 $rec = $e->retrieve_vandelay_queued_bib_record($rec_id)
1088 or return $e->die_event;
1090 $rec = $e->retrieve_vandelay_queued_authority_record($rec_id)
1091 or return $e->die_event;
1095 return $U->simplereq(
1097 'open-ils.search.biblio.record.html', undef, 1, $rec->marc);
1101 __PACKAGE__->register_method(
1102 api_name => "open-ils.vandelay.bib_queue.summary.retrieve",
1103 method => 'retrieve_queue_summary',
1107 record_type => 'bib'
1109 __PACKAGE__->register_method(
1110 api_name => "open-ils.vandelay.auth_queue.summary.retrieve",
1111 method => 'retrieve_queue_summary',
1115 record_type => 'auth'
1118 sub retrieve_queue_summary {
1119 my($self, $conn, $auth, $queue_id) = @_;
1120 my $e = new_editor(xact=>1, authtoken => $auth);
1121 return $e->die_event unless $e->checkauth;
1124 my $type = $self->{record_type};
1125 if($type eq 'bib') {
1126 $queue = $e->retrieve_vandelay_bib_queue($queue_id)
1127 or return $e->die_event;
1129 $queue = $e->retrieve_vandelay_authority_queue($queue_id)
1130 or return $e->die_event;
1133 my $evt = check_queue_perms($e, $type, $queue);
1134 return $evt if $evt;
1136 my $search = 'search_vandelay_queued_bib_record';
1137 $search =~ s/bib/authority/ if $type ne 'bib';
1141 total => scalar(@{$e->$search({queue => $queue_id}, {idlist=>1})}),
1142 imported => scalar(@{$e->$search({queue => $queue_id, import_time => {'!=' => undef}}, {idlist=>1})}),
1145 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
1146 $summary->{rec_import_errors} = $e->json_query({
1147 select => {$class => [{alias => 'count', column => 'id', transform => 'count', aggregate => 1}]},
1149 where => {queue => $queue_id, import_error => {'!=' => undef}}
1152 if($type eq 'bib') {
1155 select => {vii => [{alias => 'count', column => 'id', transform => 'count', aggregate => 1}]},
1160 select => {vqbr => ['id']},
1162 where => {queue => $queue_id}
1168 $summary->{total_items} = $e->json_query($query)->[0]->{count};
1169 $query->{where}->{import_error} = {'!=' => undef};
1170 $summary->{item_import_errors} = $e->json_query($query)->[0]->{count};
1176 # --------------------------------------------------------------------------------
1177 # Given a list of queued record IDs, imports all items attached to those records
1178 # --------------------------------------------------------------------------------
1179 sub import_record_asset_list_impl {
1180 my($conn, $rec_ids, $requestor) = @_;
1182 my $roe = new_editor(xact=> 1, requestor => $requestor);
1186 total => scalar(@$rec_ids),
1187 step => 1, # how often to respond
1192 for my $rec_id (@$rec_ids) {
1193 my $rec = $roe->retrieve_vandelay_queued_bib_record($rec_id);
1194 next unless $rec and $rec->import_time;
1195 my $item_ids = $roe->search_vandelay_import_item({record => $rec->id}, {idlist=>1});
1197 for my $item_id (@$item_ids) {
1198 my $e = new_editor(requestor => $requestor, xact => 1);
1199 my $item = $e->retrieve_vandelay_import_item($item_id);
1200 $report_args{progress}++;
1201 $report_args{import_item} = $item;
1202 $report_args{e} = $e;
1204 # --------------------------------------------------------------------------------
1205 # Find or create the volume
1206 # --------------------------------------------------------------------------------
1208 OpenILS::Application::Cat::AssetCommon->find_or_create_volume(
1209 $e, $item->call_number, $rec->imported_as, $item->owning_lib);
1212 respond_with_status(%report_args, evt => $evt);
1216 # --------------------------------------------------------------------------------
1217 # Create the new copy
1218 # --------------------------------------------------------------------------------
1219 my $copy = Fieldmapper::asset::copy->new;
1220 $copy->loan_duration(2);
1221 $copy->fine_level(2);
1222 $copy->barcode($item->barcode);
1223 $copy->location($item->location);
1224 $copy->circ_lib($item->circ_lib || $item->owning_lib);
1225 $copy->status( defined($item->status) ? $item->status : OILS_COPY_STATUS_IN_PROCESS );
1226 $copy->circulate($item->circulate);
1227 $copy->deposit($item->deposit);
1228 $copy->deposit_amount($item->deposit_amount);
1229 $copy->ref($item->ref);
1230 $copy->holdable($item->holdable);
1231 $copy->price($item->price);
1232 $copy->circ_as_type($item->circ_as_type);
1233 $copy->alert_message($item->alert_message);
1234 $copy->opac_visible($item->opac_visible);
1235 $copy->circ_modifier($item->circ_modifier);
1237 # --------------------------------------------------------------------------------
1238 # see if a valid circ_modifier was provided
1239 # --------------------------------------------------------------------------------
1240 if($copy->circ_modifier and not $e->search_config_circ_modifier({code=>$item->circ_modifier})->[0]) {
1241 respond_with_status(
1243 evt => $e->die_event,
1244 import_error => 'import.item.invalid.circ_modifier'
1249 if($copy->location and not $e->retrieve_asset_copy_location($copy->location)) {
1250 respond_with_status(
1252 evt => $e->die_event,
1253 import_error => 'import.item.invalid.location'
1258 if($evt = OpenILS::Application::Cat::AssetCommon->create_copy($e, $vol, $copy)) {
1259 respond_with_status(
1262 import_error => ($evt->{textcode} eq 'ITEM_BARCODE_EXISTS') ?
1263 'import.item.duplicate.barcode' : undef
1268 # --------------------------------------------------------------------------------
1270 # --------------------------------------------------------------------------------
1271 $evt = OpenILS::Application::Cat::AssetCommon->create_copy_note(
1272 $e, $copy, '', $item->pub_note, 1) if $item->pub_note;
1275 respond_with_status(%report_args, evt => $evt);
1279 $evt = OpenILS::Application::Cat::AssetCommon->create_copy_note(
1280 $e, $copy, '', $item->priv_note, 1) if $item->priv_note;
1283 respond_with_status(%report_args, evt => $evt);
1287 # --------------------------------------------------------------------------------
1288 # Item import succeeded
1289 # --------------------------------------------------------------------------------
1291 $report_args{in_count}++;
1292 respond_with_status(%report_args, imported_as => $copy->id);
1293 $logger->info("vl: successfully imported item " . $item->barcode);
1301 sub respond_with_status {
1305 # If the import failed, track the failure reason
1307 my $error = $args{import_error};
1308 my $evt = $args{evt};
1310 if($error or $evt) {
1312 my $item = $args{import_item};
1313 $logger->info("vl: unable to import item " . $item->barcode);
1315 $error ||= 'general.unknown';
1316 $item->import_error($error);
1319 my $detail = sprintf("%s : %s", $evt->{textcode}, substr($evt->{desc}, 0, 140));
1320 $item->error_detail($detail);
1323 # state of the editor is unknown at this point. Force a rollback and start over.
1325 $e = new_editor(xact => 1);
1326 $e->update_vandelay_import_item($item);
1330 return unless $args{report_all} or ($args{progress} % $args{step}) == 0;
1331 $args{step} *= 2 unless $args{step} == 256;
1333 $args{conn}->respond({
1334 map { $_ => $args{$_} } qw/total progress success_count/,
1339 __PACKAGE__->register_method(
1340 api_name => "open-ils.vandelay.match_set.get_tree",
1341 method => "match_set_get_tree",
1345 desc => q/For a given vms object, return a tree of match set points
1346 represented by a vmsp object with recursively fleshed
1351 sub match_set_get_tree {
1352 my ($self, $conn, $authtoken, $match_set_id) = @_;
1354 $match_set_id = int($match_set_id) or return;
1356 my $e = new_editor("authtoken" => $authtoken);
1357 $e->checkauth or return $e->die_event;
1359 my $set = $e->retrieve_vandelay_match_set($match_set_id) or
1360 return $e->die_event;
1362 $e->allowed("ADMIN_IMPORT_MATCH_SET", $set->owner) or
1363 return $e->die_event;
1365 my $tree = $e->search_vandelay_match_set_point([
1366 {"match_set" => $match_set_id, "parent" => undef},
1367 {"flesh" => -1, "flesh_fields" => {"vmsp" => ["children"]}}
1368 ]) or return $e->die_event;
1374 __PACKAGE__->register_method(
1375 api_name => "open-ils.vandelay.match_set.update",
1376 method => "match_set_update_tree",
1380 desc => q/Replace any vmsp objects associated with a given (by ID) vms
1381 with the given objects (recursively fleshed vmsp tree)./
1385 sub _walk_new_vmsp {
1386 my ($e, $match_set_id, $node, $parent_id) = @_;
1388 my $point = new Fieldmapper::vandelay::match_set_point;
1389 $point->parent($parent_id);
1390 $point->match_set($match_set_id);
1391 $point->$_($node->$_) for (qw/bool_op svf tag subfield negate quality/);
1393 $e->create_vandelay_match_set_point($point) or return $e->die_event;
1395 $parent_id = $e->data->id;
1396 if ($node->children && @{$node->children}) {
1397 for (@{$node->children}) {
1398 return $e->die_event if
1399 _walk_new_vmsp($e, $match_set_id, $_, $parent_id);
1406 sub match_set_update_tree {
1407 my ($self, $conn, $authtoken, $match_set_id, $tree) = @_;
1409 my $e = new_editor("xact" => 1, "authtoken" => $authtoken);
1410 $e->checkauth or return $e->die_event;
1412 my $set = $e->retrieve_vandelay_match_set($match_set_id) or
1413 return $e->die_event;
1415 $e->allowed("ADMIN_IMPORT_MATCH_SET", $set->owner) or
1416 return $e->die_event;
1418 my $existing = $e->search_vandelay_match_set_point([
1419 {"match_set" => $match_set_id},
1420 {"order_by" => {"vmsp" => "id DESC"}}
1421 ]) or return $e->die_event;
1423 foreach (@$existing) {
1424 $e->delete_vandelay_match_set_point($_) or return $e->die_event;
1427 _walk_new_vmsp($e, $match_set_id, $tree);
1429 $e->commit or return $e->die_event;