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 $match_set = shift;
64 my $import_def = shift;
66 my $e = new_editor(authtoken => $auth, xact => 1);
68 return $e->die_event unless $e->checkauth;
69 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE');
70 $owner ||= $e->requestor->id;
72 if ($e->search_vandelay_bib_queue( {name => $name, owner => $owner, queue_type => $type})->[0]) {
74 return OpenILS::Event->new('BIB_QUEUE_EXISTS')
77 my $queue = new Fieldmapper::vandelay::bib_queue();
78 $queue->name( $name );
79 $queue->owner( $owner );
80 $queue->queue_type( $type ) if ($type);
81 $queue->item_attr_def( $import_def ) if ($import_def);
82 $queue->match_set($match_set) if $match_set;
84 my $new_q = $e->create_vandelay_bib_queue( $queue );
85 return $e->die_event unless ($new_q);
90 __PACKAGE__->register_method(
91 api_name => "open-ils.vandelay.bib_queue.create",
92 method => "create_bib_queue",
98 sub create_auth_queue {
105 my $match_set = shift;
107 my $e = new_editor(authtoken => $auth, xact => 1);
109 return $e->die_event unless $e->checkauth;
110 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE');
111 $owner ||= $e->requestor->id;
113 if ($e->search_vandelay_bib_queue({name => $name, owner => $owner, queue_type => $type})->[0]) {
115 return OpenILS::Event->new('AUTH_QUEUE_EXISTS')
118 my $queue = new Fieldmapper::vandelay::authority_queue();
119 $queue->name( $name );
120 $queue->owner( $owner );
121 $queue->queue_type( $type ) if ($type);
123 my $new_q = $e->create_vandelay_authority_queue( $queue );
124 $e->die_event unless ($new_q);
129 __PACKAGE__->register_method(
130 api_name => "open-ils.vandelay.authority_queue.create",
131 method => "create_auth_queue",
136 sub add_record_to_bib_queue {
143 my $bib_source = shift;
145 my $e = new_editor(authtoken => $auth, xact => 1);
147 $queue = $e->retrieve_vandelay_bib_queue($queue);
149 return $e->die_event unless $e->checkauth;
150 return $e->die_event unless
151 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
152 $e->allowed('CREATE_BIB_IMPORT_QUEUE'));
154 my $new_rec = _add_bib_rec($e, $marc, $queue->id, $purpose, $bib_source);
156 return $e->die_event unless ($new_rec);
160 __PACKAGE__->register_method(
161 api_name => "open-ils.vandelay.queued_bib_record.create",
162 method => "add_record_to_bib_queue",
172 my $bib_source = shift;
174 my $rec = new Fieldmapper::vandelay::queued_bib_record();
176 $rec->queue( $queue );
177 $rec->purpose( $purpose ) if ($purpose);
178 $rec->bib_source($bib_source);
180 return $e->create_vandelay_queued_bib_record( $rec );
183 sub add_record_to_authority_queue {
191 my $e = new_editor(authtoken => $auth, xact => 1);
193 $queue = $e->retrieve_vandelay_authority_queue($queue);
195 return $e->die_event unless $e->checkauth;
196 return $e->die_event unless
197 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
198 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE'));
200 my $new_rec = _add_auth_rec($e, $marc, $queue->id, $purpose);
202 return $e->die_event unless ($new_rec);
206 __PACKAGE__->register_method(
207 api_name => "open-ils.vandelay.queued_authority_record.create",
208 method => "add_record_to_authority_queue",
219 my $rec = new Fieldmapper::vandelay::queued_authority_record();
221 $rec->queue( $queue );
222 $rec->purpose( $purpose ) if ($purpose);
224 return $e->create_vandelay_queued_authority_record( $rec );
231 my $fingerprint = shift || '';
232 my $queue_id = shift;
234 my $filename = shift;
235 my $bib_source = shift;
237 my $e = new_editor(authtoken => $auth, xact => 1);
238 return $e->die_event unless $e->checkauth;
241 my $type = $self->{record_type};
244 $queue = $e->retrieve_vandelay_bib_queue($queue_id) or return $e->die_event;
246 $queue = $e->retrieve_vandelay_authority_queue($queue_id) or return $e->die_event;
249 my $evt = check_queue_perms($e, $type, $queue);
250 return $evt if ($evt);
252 my $cache = new OpenSRF::Utils::Cache();
255 my $data = $cache->get_cache('vandelay_import_spool_' . $fingerprint);
256 $purpose = $data->{purpose};
257 $filename = $data->{path};
258 $bib_source = $data->{bib_source};
261 unless(-r $filename) {
262 $logger->error("unable to read MARC file $filename");
263 return -1; # make this an event XXX
266 $logger->info("vandelay spooling $fingerprint purpose=$purpose file=$filename");
268 my $marctype = 'USMARC';
271 $marctype = 'XML' if (getc(F) =~ /^\D/o);
274 my $batch = new MARC::Batch ($marctype, $filename);
277 my $response_scale = 10;
280 while (try { $r = $batch->next } otherwise { $r = -1 }) {
282 $logger->warn("Processing of record $count in set $filename failed. Skipping this record");
286 $logger->info("processing record $count");
289 (my $xml = $r->as_xml_record()) =~ s/\n//sog;
290 $xml =~ s/^<\?xml.+\?\s*>//go;
291 $xml =~ s/>\s+</></go;
292 $xml =~ s/\p{Cc}//go;
293 $xml = $U->entityize($xml);
294 $xml =~ s/[\x00-\x1f]//go;
297 # Check the leader to ensure we've got something resembling the expected
298 # Allow spaces to give records the benefit of the doubt
299 my $ldr_type = substr($r->leader(), 6, 1);
300 if ($type eq 'bib' && ($record_types{$ldr_type}) eq 'bib' || $ldr_type eq ' ') {
301 $qrec = _add_bib_rec( $e, $xml, $queue_id, $purpose, $bib_source ) or return $e->die_event;
302 } elsif ($type eq 'auth' && ($record_types{$ldr_type}) eq 'auth' || $ldr_type eq ' ') {
303 $qrec = _add_auth_rec( $e, $xml, $queue_id, $purpose ) or return $e->die_event;
305 # I don't know how to handle this type; rock on
306 $logger->error("In process_spool(), type was $type and leader type was $ldr_type ; not currently supported");
310 if($self->api_name =~ /stream_results/ and $qrec) {
311 $client->respond($qrec->id)
313 $client->respond($count) if (++$count % $response_scale) == 0;
314 $response_scale *= 10 if ($count == ($response_scale * 10));
318 $logger->warn("Encountered a bad record at Vandelay ingest: ".$error);
324 $cache->delete_cache('vandelay_import_spool_' . $fingerprint) if $fingerprint;
328 __PACKAGE__->register_method(
329 api_name => "open-ils.vandelay.bib.process_spool",
330 method => "process_spool",
336 __PACKAGE__->register_method(
337 api_name => "open-ils.vandelay.auth.process_spool",
338 method => "process_spool",
342 record_type => 'auth'
345 __PACKAGE__->register_method(
346 api_name => "open-ils.vandelay.bib.process_spool.stream_results",
347 method => "process_spool",
354 __PACKAGE__->register_method(
355 api_name => "open-ils.vandelay.auth.process_spool.stream_results",
356 method => "process_spool",
361 record_type => 'auth'
364 __PACKAGE__->register_method(
365 api_name => "open-ils.vandelay.bib_queue.records.retrieve",
366 method => 'retrieve_queued_records',
372 __PACKAGE__->register_method(
373 api_name => "open-ils.vandelay.auth_queue.records.retrieve",
374 method => 'retrieve_queued_records',
378 record_type => 'auth'
381 __PACKAGE__->register_method(
382 api_name => "open-ils.vandelay.bib_queue.records.matches.retrieve",
383 method => 'retrieve_queued_records',
387 record_type => 'bib',
389 desc => q/Only retrieve queued bib records that have matches against existing records/
392 __PACKAGE__->register_method(
393 api_name => "open-ils.vandelay.auth_queue.records.matches.retrieve",
394 method => 'retrieve_queued_records',
398 record_type => 'auth',
400 desc => q/Only retrieve queued authority records that have matches against existing records/
404 sub retrieve_queued_records {
405 my($self, $conn, $auth, $queue_id, $options) = @_;
408 my $limit = $$options{limit} || 20;
409 my $offset = $$options{offset} || 0;
410 my $type = $self->{record_type};
412 my $e = new_editor(authtoken => $auth, xact => 1);
413 return $e->die_event unless $e->checkauth;
417 $queue = $e->retrieve_vandelay_bib_queue($queue_id) or return $e->die_event;
419 $queue = $e->retrieve_vandelay_authority_queue($queue_id) or return $e->die_event;
421 my $evt = check_queue_perms($e, $type, $queue);
422 return $evt if ($evt);
424 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
426 select => {$class => ['id']},
428 where => {queue => $queue_id},
430 order_by => {$class => ['id']},
435 $query->{where}->{import_time} = undef if $$options{non_imported};
437 if($$options{with_import_error}) {
439 $query->{from} = {$class => {vii => {type => 'right'}}};
440 $query->{where}->{'-or'} = [
441 {'+vqbr' => {import_error => {'!=' => undef}}},
442 {'+vii' => {import_error => {'!=' => undef}}}
447 if($$options{with_rec_import_error}) {
448 $query->{where}->{import_error} = {'!=' => undef};
450 } elsif( $$options{with_item_import_error} and $type eq 'bib') {
452 $query->{from} = {$class => 'vii'};
453 $query->{where}->{'+vii'} = {import_error => {'!=' => undef}};
457 if($self->api_name =~ /matches/) {
458 # find only records that have matches
459 my $mclass = $type eq 'bib' ? 'vbm' : 'vam';
460 $query->{from} = {$class => {$mclass => {type => 'right'}}};
463 my $record_ids = $e->json_query($query);
465 my $retrieve = ($type eq 'bib') ?
466 'retrieve_vandelay_queued_bib_record' : 'retrieve_vandelay_queued_authority_record';
468 for my $rec_id (@$record_ids) {
469 my $flesh = ['attributes', 'matches'];
470 push(@$flesh, 'import_items') if $$options{flesh_import_items};
471 my $params = {flesh => 1, flesh_fields => {$class => $flesh}};
472 my $rec = $e->$retrieve([$rec_id->{id}, $params]);
473 $rec->clear_marc if $$options{clear_marc};
474 $conn->respond($rec);
481 __PACKAGE__->register_method(
482 api_name => 'open-ils.vandelay.import_item.queue.retrieve',
483 method => 'retrieve_queue_import_items',
489 Returns Import Item (vii) objects for the selected queue.
491 with_import_error : only return items that failed to import
495 sub retrieve_queue_import_items {
496 my($self, $conn, $auth, $q_id, $options) = @_;
499 my $limit = $$options{limit} || 20;
500 my $offset = $$options{offset} || 0;
502 my $e = new_editor(authtoken => $auth);
503 return $e->event unless $e->checkauth;
505 my $queue = $e->retrieve_vandelay_bib_queue($q_id) or return $e->event;
506 my $evt = check_queue_perms($e, 'bib', $queue);
510 select => {vii => ['id']},
518 filter => {id => $q_id}
524 order_by => {'vii' => ['record','id']},
529 $query->{where} = {'+vii' => {import_error => {'!=' => undef}}}
530 if $$options{with_import_error};
532 my $items = $e->json_query($query);
533 for my $item (@$items) {
534 $conn->respond($e->retrieve_vandelay_import_item($item->{id}));
540 sub check_queue_perms {
541 my($e, $type, $queue) = @_;
542 if ($type eq 'bib') {
543 return $e->die_event unless
544 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
545 $e->allowed('CREATE_BIB_IMPORT_QUEUE'));
547 return $e->die_event unless
548 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
549 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE'));
555 __PACKAGE__->register_method(
556 api_name => "open-ils.vandelay.bib_record.list.import",
557 method => 'import_record_list',
564 __PACKAGE__->register_method(
565 api_name => "open-ils.vandelay.auth_record.list.import",
566 method => 'import_record_list',
570 record_type => 'auth'
573 sub import_record_list {
574 my($self, $conn, $auth, $rec_ids, $args) = @_;
575 my $e = new_editor(authtoken => $auth, xact => 1);
576 return $e->die_event unless $e->checkauth;
578 my $err = import_record_list_impl($self, $conn, $rec_ids, $e->requestor, $args);
581 return {complete => 1};
585 __PACKAGE__->register_method(
586 api_name => "open-ils.vandelay.bib_queue.import",
587 method => 'import_queue',
595 __PACKAGE__->register_method(
596 api_name => "open-ils.vandelay.auth_queue.import",
597 method => 'import_queue',
602 record_type => 'auth'
604 __PACKAGE__->register_method(
605 api_name => "open-ils.vandelay.bib_queue.nomatch.import",
606 method => 'import_queue',
611 desc => q/Only import records that have no collisions/
617 __PACKAGE__->register_method(
618 api_name => "open-ils.vandelay.auth_queue.nomatch.import",
619 method => 'import_queue',
624 desc => q/Only import records that have no collisions/
627 record_type => 'auth'
630 my($self, $conn, $auth, $q_id, $options) = @_;
631 my $e = new_editor(authtoken => $auth, xact => 1);
632 return $e->die_event unless $e->checkauth;
634 my $type = $self->{record_type};
635 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
637 my $query = {queue => $q_id, import_time => undef};
639 if($self->api_name =~ /nomatch/) {
640 my $matched_recs = queued_records_with_matches($e, $type, $q_id, undef, undef, {import_time => undef});
641 $query->{id} = {'not in' => $matched_recs} if @$matched_recs;
644 my $search = ($type eq 'bib') ?
645 'search_vandelay_queued_bib_record' : 'search_vandelay_queued_authority_record';
646 my $rec_ids = $e->$search($query, {idlist => 1});
647 my $err = import_record_list_impl($self, $conn, $rec_ids, $e->requestor, $options);
648 try {$e->rollback} otherwise {}; # only using this to make the read authoritative -- don't die from it
650 return {complete => 1};
653 # returns a list of queued record IDs for a given queue that
654 # have at least one entry in the match table
655 sub queued_records_with_matches {
656 my($e, $type, $q_id, $limit, $offset, $filter) = @_;
658 my $match_class = 'vbm';
659 my $rec_class = 'vqbr';
660 if($type eq 'auth') {
661 $match_class = 'vam';
666 $filter->{queue} = $q_id;
670 select => {$match_class => ['queued_record']},
675 fkey => 'queued_record',
682 if($limit or defined $offset) {
685 $query->{limit} = $limit;
686 $query->{offset} = $offset;
689 my $data = $e->json_query($query);
690 return [ map {$_->{queued_record}} @$data ];
694 sub import_record_list_impl {
695 my($self, $conn, $rec_ids, $requestor, $args) = @_;
697 my $overlay_map = $args->{overlay_map} || {};
698 my $type = $self->{record_type};
705 total => scalar(@$rec_ids),
706 report_all => $$args{report_all}
709 my $auto_overlay_exact = $$args{auto_overlay_exact};
710 my $auto_overlay_1match = $$args{auto_overlay_1match};
711 my $merge_profile = $$args{merge_profile};
712 my $bib_source = $$args{bib_source};
714 my $overlay_func = 'vandelay.overlay_bib_record';
715 my $auto_overlay_func = 'vandelay.auto_overlay_bib_record';
716 my $retrieve_func = 'retrieve_vandelay_queued_bib_record';
717 my $update_func = 'update_vandelay_queued_bib_record';
718 my $search_func = 'search_vandelay_queued_bib_record';
719 my $retrieve_queue_func = 'retrieve_vandelay_bib_queue';
720 my $update_queue_func = 'update_vandelay_bib_queue';
721 my $rec_class = 'vqbr';
723 my $editor = new_editor();
726 my $sources = $editor->search_config_bib_source({id => {'!=' => undef}});
727 $bib_sources{$_->id} = $_->source for @$sources;
729 if($type eq 'auth') {
730 $overlay_func =~ s/bib/auth/o;
731 $auto_overlay_func = s/bib/auth/o;
732 $retrieve_func =~ s/bib/authority/o;
733 $retrieve_queue_func =~ s/bib/authority/o;
734 $update_queue_func =~ s/bib/authority/o;
735 $update_func =~ s/bib/authority/o;
736 $search_func =~ s/bib/authority/o;
741 for my $rec_id (@$rec_ids) {
744 my $overlay_target = $overlay_map->{$rec_id};
746 my $e = new_editor(xact => 1);
747 $e->requestor($requestor);
749 $report_args{progress}++;
750 $report_args{e} = $e;
752 my $rec = $e->$retrieve_func([
755 flesh_fields => { $rec_class => ['matches']},
760 finish_rec_import_attempt(%report_args, evt => $e->event);
764 $report_args{rec} = $rec;
766 if($rec->import_time) {
771 $queues{$rec->queue} = 1;
776 if(defined $overlay_target) {
777 # Caller chose an explicit overlay target
779 my $res = $e->json_query(
790 if($res and ($res = $res->[0])) {
792 if($res->{$overlay_func} eq 't') {
793 $logger->info("vl: $type direct overlay succeeded for queued rec " .
794 $rec->id . " and overlay target $overlay_target");
800 $logger->error("vl: Error attempting overlay with func=$overlay_func, profile=$merge_profile, record=$rec_id");
805 if($auto_overlay_1match) {
806 # caller says to overlay if there is exactly 1 match
808 my %match_recs = map { $_->eg_record => 1 } @{$rec->matches};
810 if( scalar(keys %match_recs) == 1) { # all matches point to the same record
812 my $res = $e->json_query(
817 $rec->matches->[0]->eg_record,
823 if($res and ($res = $res->[0])) {
825 if($res->{$overlay_func} eq 't') {
826 $logger->info("vl: $type overlay-1match succeeded for queued rec " . $rec->id);
832 $logger->error("vl: Error attempting overlay with func=$overlay_func, profile=$merge_profile, record=$rec_id");
837 if(!$imported and !$error and $auto_overlay_exact and scalar(@{$rec->matches}) == 1 ) {
839 # caller says to overlay if there is an /exact/ match
841 my $res = $e->json_query(
851 if($res and ($res = $res->[0])) {
853 if($res->{$auto_overlay_func} eq 't') {
854 $logger->info("vl: $type auto-overlay succeeded for queued rec " . $rec->id);
857 $logger->info("vl: $type auto-overlay failed for queued rec " . $rec->id);
862 $logger->error("vl: Error attempting overlay with func=$auto_overlay_func, profile=$merge_profile, record=$rec_id");
866 if(!$imported and !$error) {
868 # No overlay / merge occurred. Do a traditional record import by creating a new record
870 $logger->info("vl: creating new $type record for queued record $rec_id");
872 $record = OpenILS::Application::Cat::BibCommon->biblio_record_xml_import($e, $rec->marc, $bib_sources{$rec->bib_source});
875 $record = OpenILS::Application::Cat::AuthCommon->import_authority_record($e, $rec->marc); #$source);
878 if($U->event_code($record)) {
879 my $import_error = 'import.duplicate.tcn' if $record->{textcode} eq 'TCN_EXISTS';
880 finish_rec_import_attempt(%report_args, import_error => $import_error, evt => $record);
884 $logger->info("vl: successfully imported new $type record");
885 $rec->imported_as($record->id);
886 $rec->import_time('now');
888 $imported = 1 if $e->$update_func($rec);
894 push @success_rec_ids, $rec_id;
895 finish_rec_import_attempt(%report_args);
898 # Send an update whenever there's an error
899 finish_rec_import_attempt(%report_args, evt => $e->event);
903 # see if we need to mark any queues as complete
904 for my $q_id (keys %queues) {
906 my $e = new_editor(xact => 1);
907 my $remaining = $e->$search_func(
908 [{queue => $q_id, import_time => undef}, {limit =>1}], {idlist => 1});
910 unless(@$remaining) {
911 my $queue = $e->$retrieve_queue_func($q_id);
913 unless($U->is_true($queue->complete)) {
914 $queue->complete('t');
915 $e->$update_queue_func($queue) or return $e->die_event;
924 import_record_asset_list_impl($conn, \@success_rec_ids, $requestor);
926 $conn->respond({total => $report_args{total}, progress => $report_args{progress}});
930 # tracks any import errors, commits the current xact, responds to the client
931 sub finish_rec_import_attempt {
933 my $evt = $args{evt};
934 my $rec = $args{rec};
937 my $error = $args{import_error};
938 $error = 'general.unknown' if $evt and not $error;
945 # since an error occurred, there's no guarantee the transaction wasn't
946 # rolled back. force a rollback and create a new editor.
948 $e = new_editor(xact => 1);
949 $rec->import_error($error);
952 my $detail = sprintf("%s : %s", $evt->{textcode}, substr($evt->{desc}, 0, 140));
953 $rec->error_detail($detail);
956 my $method = 'update_vandelay_queued_bib_record';
957 $method =~ s/bib/authority/ if $args{type} eq 'auth';
958 $e->$method($rec) and $e->commit or $e->rollback;
962 $rec->clear_import_error;
963 $rec->clear_error_detail;
968 # requested queued record was not found
973 if($args{report_all} or ($args{progress} % $args{step}) == 0) {
975 $args{conn}->respond({
976 total => $args{total},
977 progress => $args{progress},
978 imported => ($rec) ? $rec->id : undef,
982 # report often at first, climb quickly, then hold steady
983 $args{step} *= 2 unless $args{step} == 256;
991 __PACKAGE__->register_method(
992 api_name => "open-ils.vandelay.bib_queue.owner.retrieve",
993 method => 'owner_queue_retrieve',
999 __PACKAGE__->register_method(
1000 api_name => "open-ils.vandelay.authority_queue.owner.retrieve",
1001 method => 'owner_queue_retrieve',
1005 record_type => 'auth'
1008 sub owner_queue_retrieve {
1009 my($self, $conn, $auth, $owner_id, $filters) = @_;
1010 my $e = new_editor(authtoken => $auth, xact => 1);
1011 return $e->die_event unless $e->checkauth;
1012 $owner_id = $e->requestor->id; # XXX add support for viewing other's queues?
1015 my $search = {owner => $owner_id};
1016 $search->{$_} = $filters->{$_} for keys %$filters;
1018 if($self->{record_type} eq 'bib') {
1019 $queues = $e->search_vandelay_bib_queue(
1020 [$search, {order_by => {vbq => 'evergreen.lowercase(name)'}}]);
1022 $queues = $e->search_vandelay_authority_queue(
1023 [$search, {order_by => {vaq => 'evergreen.lowercase(name)'}}]);
1025 $conn->respond($_) for @$queues;
1030 __PACKAGE__->register_method(
1031 api_name => "open-ils.vandelay.bib_queue.delete",
1032 method => "delete_queue",
1035 record_type => 'bib'
1037 __PACKAGE__->register_method(
1038 api_name => "open-ils.vandelay.auth_queue.delete",
1039 method => "delete_queue",
1042 record_type => 'auth'
1046 my($self, $conn, $auth, $q_id) = @_;
1047 my $e = new_editor(xact => 1, authtoken => $auth);
1048 return $e->die_event unless $e->checkauth;
1049 if($self->{record_type} eq 'bib') {
1050 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE');
1051 my $queue = $e->retrieve_vandelay_bib_queue($q_id)
1052 or return $e->die_event;
1053 $e->delete_vandelay_bib_queue($queue)
1054 or return $e->die_event;
1056 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE');
1057 my $queue = $e->retrieve_vandelay_authority_queue($q_id)
1058 or return $e->die_event;
1059 $e->delete_vandelay_authority_queue($queue)
1060 or return $e->die_event;
1067 __PACKAGE__->register_method(
1068 api_name => "open-ils.vandelay.queued_bib_record.html",
1069 method => 'queued_record_html',
1073 record_type => 'bib'
1075 __PACKAGE__->register_method(
1076 api_name => "open-ils.vandelay.queued_authority_record.html",
1077 method => 'queued_record_html',
1081 record_type => 'auth'
1084 sub queued_record_html {
1085 my($self, $conn, $auth, $rec_id) = @_;
1086 my $e = new_editor(xact=>1,authtoken => $auth);
1087 return $e->die_event unless $e->checkauth;
1089 if($self->{record_type} eq 'bib') {
1090 $rec = $e->retrieve_vandelay_queued_bib_record($rec_id)
1091 or return $e->die_event;
1093 $rec = $e->retrieve_vandelay_queued_authority_record($rec_id)
1094 or return $e->die_event;
1098 return $U->simplereq(
1100 'open-ils.search.biblio.record.html', undef, 1, $rec->marc);
1104 __PACKAGE__->register_method(
1105 api_name => "open-ils.vandelay.bib_queue.summary.retrieve",
1106 method => 'retrieve_queue_summary',
1110 record_type => 'bib'
1112 __PACKAGE__->register_method(
1113 api_name => "open-ils.vandelay.auth_queue.summary.retrieve",
1114 method => 'retrieve_queue_summary',
1118 record_type => 'auth'
1121 sub retrieve_queue_summary {
1122 my($self, $conn, $auth, $queue_id) = @_;
1123 my $e = new_editor(xact=>1, authtoken => $auth);
1124 return $e->die_event unless $e->checkauth;
1127 my $type = $self->{record_type};
1128 if($type eq 'bib') {
1129 $queue = $e->retrieve_vandelay_bib_queue($queue_id)
1130 or return $e->die_event;
1132 $queue = $e->retrieve_vandelay_authority_queue($queue_id)
1133 or return $e->die_event;
1136 my $evt = check_queue_perms($e, $type, $queue);
1137 return $evt if $evt;
1139 my $search = 'search_vandelay_queued_bib_record';
1140 $search =~ s/bib/authority/ if $type ne 'bib';
1144 total => scalar(@{$e->$search({queue => $queue_id}, {idlist=>1})}),
1145 imported => scalar(@{$e->$search({queue => $queue_id, import_time => {'!=' => undef}}, {idlist=>1})}),
1148 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
1149 $summary->{rec_import_errors} = $e->json_query({
1150 select => {$class => [{alias => 'count', column => 'id', transform => 'count', aggregate => 1}]},
1152 where => {queue => $queue_id, import_error => {'!=' => undef}}
1155 if($type eq 'bib') {
1158 select => {vii => [{alias => 'count', column => 'id', transform => 'count', aggregate => 1}]},
1163 select => {vqbr => ['id']},
1165 where => {queue => $queue_id}
1171 $summary->{total_items} = $e->json_query($query)->[0]->{count};
1172 $query->{where}->{import_error} = {'!=' => undef};
1173 $summary->{item_import_errors} = $e->json_query($query)->[0]->{count};
1179 # --------------------------------------------------------------------------------
1180 # Given a list of queued record IDs, imports all items attached to those records
1181 # --------------------------------------------------------------------------------
1182 sub import_record_asset_list_impl {
1183 my($conn, $rec_ids, $requestor) = @_;
1185 my $roe = new_editor(xact=> 1, requestor => $requestor);
1189 total => scalar(@$rec_ids),
1190 step => 1, # how often to respond
1195 for my $rec_id (@$rec_ids) {
1196 my $rec = $roe->retrieve_vandelay_queued_bib_record($rec_id);
1197 next unless $rec and $rec->import_time;
1198 my $item_ids = $roe->search_vandelay_import_item({record => $rec->id}, {idlist=>1});
1200 for my $item_id (@$item_ids) {
1201 my $e = new_editor(requestor => $requestor, xact => 1);
1202 my $item = $e->retrieve_vandelay_import_item($item_id);
1203 $report_args{progress}++;
1204 $report_args{import_item} = $item;
1205 $report_args{e} = $e;
1207 # --------------------------------------------------------------------------------
1208 # Find or create the volume
1209 # --------------------------------------------------------------------------------
1211 OpenILS::Application::Cat::AssetCommon->find_or_create_volume(
1212 $e, $item->call_number, $rec->imported_as, $item->owning_lib);
1215 respond_with_status(%report_args, evt => $evt);
1219 # --------------------------------------------------------------------------------
1220 # Create the new copy
1221 # --------------------------------------------------------------------------------
1222 my $copy = Fieldmapper::asset::copy->new;
1223 $copy->loan_duration(2);
1224 $copy->fine_level(2);
1225 $copy->barcode($item->barcode);
1226 $copy->location($item->location);
1227 $copy->circ_lib($item->circ_lib || $item->owning_lib);
1228 $copy->status( defined($item->status) ? $item->status : OILS_COPY_STATUS_IN_PROCESS );
1229 $copy->circulate($item->circulate);
1230 $copy->deposit($item->deposit);
1231 $copy->deposit_amount($item->deposit_amount);
1232 $copy->ref($item->ref);
1233 $copy->holdable($item->holdable);
1234 $copy->price($item->price);
1235 $copy->circ_as_type($item->circ_as_type);
1236 $copy->alert_message($item->alert_message);
1237 $copy->opac_visible($item->opac_visible);
1238 $copy->circ_modifier($item->circ_modifier);
1240 # --------------------------------------------------------------------------------
1241 # see if a valid circ_modifier was provided
1242 # --------------------------------------------------------------------------------
1243 if($copy->circ_modifier and not $e->search_config_circ_modifier({code=>$item->circ_modifier})->[0]) {
1244 respond_with_status(
1246 evt => $e->die_event,
1247 import_error => 'import.item.invalid.circ_modifier'
1252 if($copy->location and not $e->retrieve_asset_copy_location($copy->location)) {
1253 respond_with_status(
1255 evt => $e->die_event,
1256 import_error => 'import.item.invalid.location'
1261 if($evt = OpenILS::Application::Cat::AssetCommon->create_copy($e, $vol, $copy)) {
1262 respond_with_status(
1265 import_error => ($evt->{textcode} eq 'ITEM_BARCODE_EXISTS') ?
1266 'import.item.duplicate.barcode' : undef
1271 # --------------------------------------------------------------------------------
1273 # --------------------------------------------------------------------------------
1274 $evt = OpenILS::Application::Cat::AssetCommon->create_copy_note(
1275 $e, $copy, '', $item->pub_note, 1) if $item->pub_note;
1278 respond_with_status(%report_args, evt => $evt);
1282 $evt = OpenILS::Application::Cat::AssetCommon->create_copy_note(
1283 $e, $copy, '', $item->priv_note, 1) if $item->priv_note;
1286 respond_with_status(%report_args, evt => $evt);
1290 # --------------------------------------------------------------------------------
1291 # Item import succeeded
1292 # --------------------------------------------------------------------------------
1294 $report_args{in_count}++;
1295 respond_with_status(%report_args, imported_as => $copy->id);
1296 $logger->info("vl: successfully imported item " . $item->barcode);
1304 sub respond_with_status {
1308 # If the import failed, track the failure reason
1310 my $error = $args{import_error};
1311 my $evt = $args{evt};
1313 if($error or $evt) {
1315 my $item = $args{import_item};
1316 $logger->info("vl: unable to import item " . $item->barcode);
1318 $error ||= 'general.unknown';
1319 $item->import_error($error);
1322 my $detail = sprintf("%s : %s", $evt->{textcode}, substr($evt->{desc}, 0, 140));
1323 $item->error_detail($detail);
1326 # state of the editor is unknown at this point. Force a rollback and start over.
1328 $e = new_editor(xact => 1);
1329 $e->update_vandelay_import_item($item);
1333 return unless $args{report_all} or ($args{progress} % $args{step}) == 0;
1334 $args{step} *= 2 unless $args{step} == 256;
1336 $args{conn}->respond({
1337 map { $_ => $args{$_} } qw/total progress success_count/,
1342 __PACKAGE__->register_method(
1343 api_name => "open-ils.vandelay.match_set.get_tree",
1344 method => "match_set_get_tree",
1348 desc => q/For a given vms object, return a tree of match set points
1349 represented by a vmsp object with recursively fleshed
1354 sub match_set_get_tree {
1355 my ($self, $conn, $authtoken, $match_set_id) = @_;
1357 $match_set_id = int($match_set_id) or return;
1359 my $e = new_editor("authtoken" => $authtoken);
1360 $e->checkauth or return $e->die_event;
1362 my $set = $e->retrieve_vandelay_match_set($match_set_id) or
1363 return $e->die_event;
1365 $e->allowed("ADMIN_IMPORT_MATCH_SET", $set->owner) or
1366 return $e->die_event;
1368 my $tree = $e->search_vandelay_match_set_point([
1369 {"match_set" => $match_set_id, "parent" => undef},
1370 {"flesh" => -1, "flesh_fields" => {"vmsp" => ["children"]}}
1371 ]) or return $e->die_event;
1377 __PACKAGE__->register_method(
1378 api_name => "open-ils.vandelay.match_set.update",
1379 method => "match_set_update_tree",
1383 desc => q/Replace any vmsp objects associated with a given (by ID) vms
1384 with the given objects (recursively fleshed vmsp tree)./
1388 sub _walk_new_vmsp {
1389 my ($e, $match_set_id, $node, $parent_id) = @_;
1391 my $point = new Fieldmapper::vandelay::match_set_point;
1392 $point->parent($parent_id);
1393 $point->match_set($match_set_id);
1394 $point->$_($node->$_) for (qw/bool_op svf tag subfield negate quality/);
1396 $e->create_vandelay_match_set_point($point) or return $e->die_event;
1398 $parent_id = $e->data->id;
1399 if ($node->children && @{$node->children}) {
1400 for (@{$node->children}) {
1401 return $e->die_event if
1402 _walk_new_vmsp($e, $match_set_id, $_, $parent_id);
1409 sub match_set_update_tree {
1410 my ($self, $conn, $authtoken, $match_set_id, $tree) = @_;
1412 my $e = new_editor("xact" => 1, "authtoken" => $authtoken);
1413 $e->checkauth or return $e->die_event;
1415 my $set = $e->retrieve_vandelay_match_set($match_set_id) or
1416 return $e->die_event;
1418 $e->allowed("ADMIN_IMPORT_MATCH_SET", $set->owner) or
1419 return $e->die_event;
1421 my $existing = $e->search_vandelay_match_set_point([
1422 {"match_set" => $match_set_id},
1423 {"order_by" => {"vmsp" => "id DESC"}}
1424 ]) or return $e->die_event;
1426 # delete points, working up from leaf points to the root
1428 for my $point (shift @$existing) {
1429 if( grep {$_->parent eq $point->id} @$existing) {
1430 push(@$existing, $point);
1432 $e->delete_vandelay_match_set_point($point) or return $e->die_event;
1437 _walk_new_vmsp($e, $match_set_id, $tree);
1439 $e->commit or return $e->die_event;