1 package OpenILS::Application::Vandelay;
2 use strict; use warnings;
3 use OpenILS::Application;
4 use base qw/OpenILS::Application/;
5 use Unicode::Normalize;
6 use OpenSRF::EX qw/:try/;
7 use OpenSRF::AppSession;
8 use OpenSRF::Utils::SettingsClient;
9 use OpenSRF::Utils::Cache;
10 use OpenILS::Utils::Fieldmapper;
11 use OpenILS::Utils::CStoreEditor qw/:funcs/;
14 use MARC::File::XML ( BinaryEncoding => 'UTF-8' );
15 use OpenILS::Utils::Fieldmapper;
16 use Time::HiRes qw(time);
17 use OpenSRF::Utils::Logger qw/$logger/;
19 use OpenILS::Const qw/:const/;
20 use OpenILS::Application::AppUtils;
21 use OpenILS::Application::Cat::BibCommon;
22 use OpenILS::Application::Cat::AuthCommon;
23 use OpenILS::Application::Cat::AssetCommon;
24 my $U = 'OpenILS::Application::AppUtils';
26 # A list of LDR/06 values from http://loc.gov/marc
53 # --------------------------------------------------------------------------------
56 sub create_bib_queue {
63 my $import_def = shift;
65 my $e = new_editor(authtoken => $auth, xact => 1);
67 return $e->die_event unless $e->checkauth;
68 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE');
69 $owner ||= $e->requestor->id;
71 if ($e->search_vandelay_bib_queue( {name => $name, owner => $owner, queue_type => $type})->[0]) {
73 return OpenILS::Event->new('BIB_QUEUE_EXISTS')
76 my $queue = new Fieldmapper::vandelay::bib_queue();
77 $queue->name( $name );
78 $queue->owner( $owner );
79 $queue->queue_type( $type ) if ($type);
80 $queue->item_attr_def( $import_def ) if ($import_def);
82 my $new_q = $e->create_vandelay_bib_queue( $queue );
83 return $e->die_event unless ($new_q);
88 __PACKAGE__->register_method(
89 api_name => "open-ils.vandelay.bib_queue.create",
90 method => "create_bib_queue",
96 sub create_auth_queue {
104 my $e = new_editor(authtoken => $auth, xact => 1);
106 return $e->die_event unless $e->checkauth;
107 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE');
108 $owner ||= $e->requestor->id;
110 if ($e->search_vandelay_bib_queue({name => $name, owner => $owner, queue_type => $type})->[0]) {
112 return OpenILS::Event->new('AUTH_QUEUE_EXISTS')
115 my $queue = new Fieldmapper::vandelay::authority_queue();
116 $queue->name( $name );
117 $queue->owner( $owner );
118 $queue->queue_type( $type ) if ($type);
120 my $new_q = $e->create_vandelay_authority_queue( $queue );
121 $e->die_event unless ($new_q);
126 __PACKAGE__->register_method(
127 api_name => "open-ils.vandelay.authority_queue.create",
128 method => "create_auth_queue",
133 sub add_record_to_bib_queue {
140 my $bib_source = shift;
142 my $e = new_editor(authtoken => $auth, xact => 1);
144 $queue = $e->retrieve_vandelay_bib_queue($queue);
146 return $e->die_event unless $e->checkauth;
147 return $e->die_event unless
148 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
149 $e->allowed('CREATE_BIB_IMPORT_QUEUE'));
151 my $new_rec = _add_bib_rec($e, $marc, $queue->id, $purpose, $bib_source);
153 return $e->die_event unless ($new_rec);
157 __PACKAGE__->register_method(
158 api_name => "open-ils.vandelay.queued_bib_record.create",
159 method => "add_record_to_bib_queue",
169 my $bib_source = shift;
171 my $rec = new Fieldmapper::vandelay::queued_bib_record();
173 $rec->queue( $queue );
174 $rec->purpose( $purpose ) if ($purpose);
175 $rec->bib_source($bib_source);
177 return $e->create_vandelay_queued_bib_record( $rec );
180 sub add_record_to_authority_queue {
188 my $e = new_editor(authtoken => $auth, xact => 1);
190 $queue = $e->retrieve_vandelay_authority_queue($queue);
192 return $e->die_event unless $e->checkauth;
193 return $e->die_event unless
194 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
195 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE'));
197 my $new_rec = _add_auth_rec($e, $marc, $queue->id, $purpose);
199 return $e->die_event unless ($new_rec);
203 __PACKAGE__->register_method(
204 api_name => "open-ils.vandelay.queued_authority_record.create",
205 method => "add_record_to_authority_queue",
216 my $rec = new Fieldmapper::vandelay::queued_authority_record();
218 $rec->queue( $queue );
219 $rec->purpose( $purpose ) if ($purpose);
221 return $e->create_vandelay_queued_authority_record( $rec );
228 my $fingerprint = shift || '';
229 my $queue_id = shift;
231 my $filename = shift;
232 my $bib_source = shift;
234 my $e = new_editor(authtoken => $auth, xact => 1);
235 return $e->die_event unless $e->checkauth;
238 my $type = $self->{record_type};
241 $queue = $e->retrieve_vandelay_bib_queue($queue_id) or return $e->die_event;
243 $queue = $e->retrieve_vandelay_authority_queue($queue_id) or return $e->die_event;
246 my $evt = check_queue_perms($e, $type, $queue);
247 return $evt if ($evt);
249 my $cache = new OpenSRF::Utils::Cache();
252 my $data = $cache->get_cache('vandelay_import_spool_' . $fingerprint);
253 $purpose = $data->{purpose};
254 $filename = $data->{path};
255 $bib_source = $data->{bib_source};
258 unless(-r $filename) {
259 $logger->error("unable to read MARC file $filename");
260 return -1; # make this an event XXX
263 $logger->info("vandelay spooling $fingerprint purpose=$purpose file=$filename");
265 my $marctype = 'USMARC';
268 $marctype = 'XML' if (getc(F) =~ /^\D/o);
271 my $batch = new MARC::Batch ($marctype, $filename);
274 my $response_scale = 10;
277 while (try { $r = $batch->next } otherwise { $r = -1 }) {
279 $logger->warn("Processing of record $count in set $filename failed. Skipping this record");
283 $logger->info("processing record $count");
286 (my $xml = $r->as_xml_record()) =~ s/\n//sog;
287 $xml =~ s/^<\?xml.+\?\s*>//go;
288 $xml =~ s/>\s+</></go;
289 $xml =~ s/\p{Cc}//go;
290 $xml = $U->entityize($xml);
291 $xml =~ s/[\x00-\x1f]//go;
294 # Check the leader to ensure we've got something resembling the expected
295 # Allow spaces to give records the benefit of the doubt
296 my $ldr_type = substr($r->leader(), 6, 1);
297 if ($type eq 'bib' && ($record_types{$ldr_type}) eq 'bib' || $ldr_type eq ' ') {
298 $qrec = _add_bib_rec( $e, $xml, $queue_id, $purpose, $bib_source ) or return $e->die_event;
299 } elsif ($type eq 'auth' && ($record_types{$ldr_type}) eq 'auth' || $ldr_type eq ' ') {
300 $qrec = _add_auth_rec( $e, $xml, $queue_id, $purpose ) or return $e->die_event;
302 # I don't know how to handle this type; rock on
303 $logger->error("In process_spool(), type was $type and leader type was $ldr_type ; not currently supported");
307 if($self->api_name =~ /stream_results/ and $qrec) {
308 $client->respond($qrec->id)
310 $client->respond($count) if (++$count % $response_scale) == 0;
311 $response_scale *= 10 if ($count == ($response_scale * 10));
315 $logger->warn("Encountered a bad record at Vandelay ingest: ".$error);
321 $cache->delete_cache('vandelay_import_spool_' . $fingerprint) if $fingerprint;
325 __PACKAGE__->register_method(
326 api_name => "open-ils.vandelay.bib.process_spool",
327 method => "process_spool",
333 __PACKAGE__->register_method(
334 api_name => "open-ils.vandelay.auth.process_spool",
335 method => "process_spool",
339 record_type => 'auth'
342 __PACKAGE__->register_method(
343 api_name => "open-ils.vandelay.bib.process_spool.stream_results",
344 method => "process_spool",
351 __PACKAGE__->register_method(
352 api_name => "open-ils.vandelay.auth.process_spool.stream_results",
353 method => "process_spool",
358 record_type => 'auth'
361 __PACKAGE__->register_method(
362 api_name => "open-ils.vandelay.bib_queue.records.retrieve",
363 method => 'retrieve_queued_records',
369 __PACKAGE__->register_method(
370 api_name => "open-ils.vandelay.auth_queue.records.retrieve",
371 method => 'retrieve_queued_records',
375 record_type => 'auth'
378 __PACKAGE__->register_method(
379 api_name => "open-ils.vandelay.bib_queue.records.matches.retrieve",
380 method => 'retrieve_queued_records',
384 record_type => 'bib',
386 desc => q/Only retrieve queued bib records that have matches against existing records/
389 __PACKAGE__->register_method(
390 api_name => "open-ils.vandelay.auth_queue.records.matches.retrieve",
391 method => 'retrieve_queued_records',
395 record_type => 'auth',
397 desc => q/Only retrieve queued authority records that have matches against existing records/
401 sub retrieve_queued_records {
402 my($self, $conn, $auth, $queue_id, $options) = @_;
405 my $limit = $$options{limit} || 20;
406 my $offset = $$options{offset} || 0;
407 my $type = $self->{record_type};
409 my $e = new_editor(authtoken => $auth, xact => 1);
410 return $e->die_event unless $e->checkauth;
414 $queue = $e->retrieve_vandelay_bib_queue($queue_id) or return $e->die_event;
416 $queue = $e->retrieve_vandelay_authority_queue($queue_id) or return $e->die_event;
418 my $evt = check_queue_perms($e, $type, $queue);
419 return $evt if ($evt);
421 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
423 select => {$class => ['id']},
425 where => {queue => $queue_id},
427 order_by => {$class => ['id']},
432 $query->{where}->{import_time} = undef if $$options{non_imported};
433 $query->{where}->{import_error} = {'!=' => undef} if $$options{with_rec_import_error};
435 if($$options{with_item_import_error} and $type eq 'bib') {
436 # limit to recs that have at least 1 item import error
442 filter => {import_error => {'!=' => undef}}
448 if($self->api_name =~ /matches/) {
449 # find only records that have matches
450 my $mclass = $type eq 'bib' ? 'vbm' : 'vam';
451 $query->{from} = {$class => {$mclass => {type => 'right'}}};
454 my $record_ids = $e->json_query($query);
456 my $retrieve = ($type eq 'bib') ?
457 'retrieve_vandelay_queued_bib_record' : 'retrieve_vandelay_queued_authority_record';
459 for my $rec_id (@$record_ids) {
462 flesh_fields => {$class => ['attributes', 'matches']},
464 my $rec = $e->$retrieve([$rec_id->{id}, $params]);
465 $rec->clear_marc if $$options{clear_marc};
466 $conn->respond($rec);
473 __PACKAGE__->register_method(
474 api_name => 'open-ils.vandelay.import_item.queue.retrieve',
475 method => 'retrieve_queue_import_items',
481 Returns Import Item (vii) objects for the selected queue.
483 with_import_error : only return items that failed to import
487 sub retrieve_queue_import_items {
488 my($self, $conn, $auth, $q_id, $options) = @_;
491 my $limit = $$options{limit} || 20;
492 my $offset = $$options{offset} || 0;
494 my $e = new_editor(authtoken => $auth);
495 return $e->event unless $e->checkauth;
497 my $queue = $e->retrieve_vandelay_bib_queue($q_id) or return $e->event;
498 my $evt = check_queue_perms($e, 'bib', $queue);
502 select => {vii => ['id']},
510 filter => {id => $q_id}
516 order_by => {'vii' => ['record','id']}
519 $query->{where} = {'+vii' => {import_error => {'!=' => undef}}}
520 if $$options{with_import_error};
522 my $items = $e->json_query($query);
523 for my $item (@$items) {
524 $conn->respond($e->retrieve_vandelay_import_item($item->{id}));
530 sub check_queue_perms {
531 my($e, $type, $queue) = @_;
532 if ($type eq 'bib') {
533 return $e->die_event unless
534 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
535 $e->allowed('CREATE_BIB_IMPORT_QUEUE'));
537 return $e->die_event unless
538 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
539 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE'));
545 __PACKAGE__->register_method(
546 api_name => "open-ils.vandelay.bib_record.list.import",
547 method => 'import_record_list',
554 __PACKAGE__->register_method(
555 api_name => "open-ils.vandelay.auth_record.list.import",
556 method => 'import_record_list',
560 record_type => 'auth'
563 sub import_record_list {
564 my($self, $conn, $auth, $rec_ids, $args) = @_;
565 my $e = new_editor(authtoken => $auth, xact => 1);
566 return $e->die_event unless $e->checkauth;
568 my $err = import_record_list_impl($self, $conn, $rec_ids, $e->requestor, $args);
571 return {complete => 1};
575 __PACKAGE__->register_method(
576 api_name => "open-ils.vandelay.bib_queue.import",
577 method => 'import_queue',
585 __PACKAGE__->register_method(
586 api_name => "open-ils.vandelay.auth_queue.import",
587 method => 'import_queue',
592 record_type => 'auth'
594 __PACKAGE__->register_method(
595 api_name => "open-ils.vandelay.bib_queue.nomatch.import",
596 method => 'import_queue',
601 desc => q/Only import records that have no collisions/
607 __PACKAGE__->register_method(
608 api_name => "open-ils.vandelay.auth_queue.nomatch.import",
609 method => 'import_queue',
614 desc => q/Only import records that have no collisions/
617 record_type => 'auth'
620 my($self, $conn, $auth, $q_id, $options) = @_;
621 my $e = new_editor(authtoken => $auth, xact => 1);
622 return $e->die_event unless $e->checkauth;
624 my $type = $self->{record_type};
625 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
627 my $query = {queue => $q_id, import_time => undef};
629 if($self->api_name =~ /nomatch/) {
630 my $matched_recs = queued_records_with_matches($e, $type, $q_id, undef, undef, {import_time => undef});
631 $query->{id} = {'not in' => $matched_recs} if @$matched_recs;
634 my $search = ($type eq 'bib') ?
635 'search_vandelay_queued_bib_record' : 'search_vandelay_queued_authority_record';
636 my $rec_ids = $e->$search($query, {idlist => 1});
637 my $err = import_record_list_impl($self, $conn, $rec_ids, $e->requestor, $options);
638 try {$e->rollback} otherwise {}; # only using this to make the read authoritative -- don't die from it
640 return {complete => 1};
643 # returns a list of queued record IDs for a given queue that
644 # have at least one entry in the match table
645 sub queued_records_with_matches {
646 my($e, $type, $q_id, $limit, $offset, $filter) = @_;
648 my $match_class = 'vbm';
649 my $rec_class = 'vqbr';
650 if($type eq 'auth') {
651 $match_class = 'vam';
656 $filter->{queue} = $q_id;
660 select => {$match_class => ['queued_record']},
665 fkey => 'queued_record',
672 if($limit or defined $offset) {
675 $query->{limit} = $limit;
676 $query->{offset} = $offset;
679 my $data = $e->json_query($query);
680 return [ map {$_->{queued_record}} @$data ];
684 sub import_record_list_impl {
685 my($self, $conn, $rec_ids, $requestor, $args) = @_;
687 my $overlay_map = $args->{overlay_map} || {};
688 my $type = $self->{record_type};
695 total => scalar(@$rec_ids),
696 report_all => $$args{report_all}
699 my $auto_overlay_exact = $$args{auto_overlay_exact};
700 my $auto_overlay_1match = $$args{auto_overlay_1match};
701 my $merge_profile = $$args{merge_profile};
702 my $bib_source = $$args{bib_source};
704 my $overlay_func = 'vandelay.overlay_bib_record';
705 my $auto_overlay_func = 'vandelay.auto_overlay_bib_record';
706 my $retrieve_func = 'retrieve_vandelay_queued_bib_record';
707 my $update_func = 'update_vandelay_queued_bib_record';
708 my $search_func = 'search_vandelay_queued_bib_record';
709 my $retrieve_queue_func = 'retrieve_vandelay_bib_queue';
710 my $update_queue_func = 'update_vandelay_bib_queue';
711 my $rec_class = 'vqbr';
713 my $editor = new_editor();
716 my $sources = $editor->search_config_bib_source({id => {'!=' => undef}});
717 $bib_sources{$_->id} = $_->source for @$sources;
719 if($type eq 'auth') {
720 $overlay_func =~ s/bib/auth/o;
721 $auto_overlay_func = s/bib/auth/o;
722 $retrieve_func =~ s/bib/authority/o;
723 $retrieve_queue_func =~ s/bib/authority/o;
724 $update_queue_func =~ s/bib/authority/o;
725 $update_func =~ s/bib/authority/o;
726 $search_func =~ s/bib/authority/o;
731 for my $rec_id (@$rec_ids) {
734 my $overlay_target = $overlay_map->{$rec_id};
736 my $e = new_editor(xact => 1);
737 $e->requestor($requestor);
739 $report_args{progress}++;
740 $report_args{e} = $e;
742 my $rec = $e->$retrieve_func([
745 flesh_fields => { $rec_class => ['matches']},
750 finish_rec_import_attempt(%report_args, evt => $e->event);
754 $report_args{rec} = $rec;
756 if($rec->import_time) {
761 $queues{$rec->queue} = 1;
766 if(defined $overlay_target) {
767 # Caller chose an explicit overlay target
769 my $res = $e->json_query(
780 if($res and ($res = $res->[0])) {
782 if($res->{$overlay_func} eq 't') {
783 $logger->info("vl: $type direct overlay succeeded for queued rec " .
784 $rec->id . " and overlay target $overlay_target");
790 $logger->error("vl: Error attempting overlay with func=$overlay_func, profile=$merge_profile, record=$rec_id");
795 if($auto_overlay_1match) {
796 # caller says to overlay if there is exactly 1 match
798 my %match_recs = map { $_->eg_record => 1 } @{$rec->matches};
800 if( scalar(keys %match_recs) == 1) { # all matches point to the same record
802 my $res = $e->json_query(
807 $rec->matches->[0]->eg_record,
813 if($res and ($res = $res->[0])) {
815 if($res->{$overlay_func} eq 't') {
816 $logger->info("vl: $type overlay-1match succeeded for queued rec " . $rec->id);
822 $logger->error("vl: Error attempting overlay with func=$overlay_func, profile=$merge_profile, record=$rec_id");
827 if(!$imported and !$error and $auto_overlay_exact and scalar(@{$rec->matches}) == 1 ) {
829 # caller says to overlay if there is an /exact/ match
831 my $res = $e->json_query(
841 if($res and ($res = $res->[0])) {
843 if($res->{$auto_overlay_func} eq 't') {
844 $logger->info("vl: $type auto-overlay succeeded for queued rec " . $rec->id);
847 $logger->info("vl: $type auto-overlay failed for queued rec " . $rec->id);
852 $logger->error("vl: Error attempting overlay with func=$auto_overlay_func, profile=$merge_profile, record=$rec_id");
856 if(!$imported and !$error) {
858 # No overlay / merge occurred. Do a traditional record import by creating a new record
860 $logger->info("vl: creating new $type record for queued record $rec_id");
862 $record = OpenILS::Application::Cat::BibCommon->biblio_record_xml_import($e, $rec->marc, $bib_sources{$rec->bib_source});
865 $record = OpenILS::Application::Cat::AuthCommon->import_authority_record($e, $rec->marc); #$source);
868 if($U->event_code($record)) {
869 my $import_error = 'import.duplicate.tcn' if $record->{textcode} eq 'TCN_EXISTS';
870 finish_rec_import_attempt(%report_args, import_error => $import_error, evt => $record);
874 $logger->info("vl: successfully imported new $type record");
875 $rec->imported_as($record->id);
876 $rec->import_time('now');
878 $imported = 1 if $e->$update_func($rec);
884 push @success_rec_ids, $rec_id;
885 finish_rec_import_attempt(%report_args);
888 # Send an update whenever there's an error
889 finish_rec_import_attempt(%report_args, evt => $e->event);
893 # see if we need to mark any queues as complete
894 for my $q_id (keys %queues) {
896 my $e = new_editor(xact => 1);
897 my $remaining = $e->$search_func(
898 [{queue => $q_id, import_time => undef}, {limit =>1}], {idlist => 1});
900 unless(@$remaining) {
901 my $queue = $e->$retrieve_queue_func($q_id);
903 unless($U->is_true($queue->complete)) {
904 $queue->complete('t');
905 $e->$update_queue_func($queue) or return $e->die_event;
914 import_record_asset_list_impl($conn, \@success_rec_ids, $requestor);
916 $conn->respond({total => $report_args{total}, progress => $report_args{progress}});
920 # tracks any import errors, commits the current xact, responds to the client
921 sub finish_rec_import_attempt {
923 my $evt = $args{evt};
924 my $rec = $args{rec};
927 my $error = $args{import_error};
928 $error = 'general.unknown' if $evt and not $error;
935 # since an error occurred, there's no guarantee the transaction wasn't
936 # rolled back. force a rollback and create a new editor.
938 $e = new_editor(xact => 1);
939 $rec->import_error($error);
942 my $detail = sprintf("%s : %s", $evt->{textcode}, substr($evt->{desc}, 0, 140));
943 $rec->error_detail($detail);
946 my $method = 'update_vandelay_queued_bib_record';
947 $method =~ s/bib/authority/ if $args{type} eq 'auth';
948 $e->$method($rec) and $e->commit or $e->rollback;
952 $rec->clear_import_error;
953 $rec->clear_error_detail;
958 # requested queued record was not found
963 if($args{report_all} or ($args{progress} % $args{step}) == 0) {
965 $args{conn}->respond({
966 total => $args{total},
967 progress => $args{progress},
968 imported => ($rec) ? $rec->id : undef,
972 # report often at first, climb quickly, then hold steady
973 $args{step} *= 2 unless $args{step} == 256;
981 __PACKAGE__->register_method(
982 api_name => "open-ils.vandelay.bib_queue.owner.retrieve",
983 method => 'owner_queue_retrieve',
989 __PACKAGE__->register_method(
990 api_name => "open-ils.vandelay.authority_queue.owner.retrieve",
991 method => 'owner_queue_retrieve',
995 record_type => 'auth'
998 sub owner_queue_retrieve {
999 my($self, $conn, $auth, $owner_id, $filters) = @_;
1000 my $e = new_editor(authtoken => $auth, xact => 1);
1001 return $e->die_event unless $e->checkauth;
1002 $owner_id = $e->requestor->id; # XXX add support for viewing other's queues?
1005 my $search = {owner => $owner_id};
1006 $search->{$_} = $filters->{$_} for keys %$filters;
1008 if($self->{record_type} eq 'bib') {
1009 $queues = $e->search_vandelay_bib_queue(
1010 [$search, {order_by => {vbq => 'evergreen.lowercase(name)'}}]);
1012 $queues = $e->search_vandelay_authority_queue(
1013 [$search, {order_by => {vaq => 'evergreen.lowercase(name)'}}]);
1015 $conn->respond($_) for @$queues;
1020 __PACKAGE__->register_method(
1021 api_name => "open-ils.vandelay.bib_queue.delete",
1022 method => "delete_queue",
1025 record_type => 'bib'
1027 __PACKAGE__->register_method(
1028 api_name => "open-ils.vandelay.auth_queue.delete",
1029 method => "delete_queue",
1032 record_type => 'auth'
1036 my($self, $conn, $auth, $q_id) = @_;
1037 my $e = new_editor(xact => 1, authtoken => $auth);
1038 return $e->die_event unless $e->checkauth;
1039 if($self->{record_type} eq 'bib') {
1040 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE');
1041 my $queue = $e->retrieve_vandelay_bib_queue($q_id)
1042 or return $e->die_event;
1043 $e->delete_vandelay_bib_queue($queue)
1044 or return $e->die_event;
1046 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE');
1047 my $queue = $e->retrieve_vandelay_authority_queue($q_id)
1048 or return $e->die_event;
1049 $e->delete_vandelay_authority_queue($queue)
1050 or return $e->die_event;
1057 __PACKAGE__->register_method(
1058 api_name => "open-ils.vandelay.queued_bib_record.html",
1059 method => 'queued_record_html',
1063 record_type => 'bib'
1065 __PACKAGE__->register_method(
1066 api_name => "open-ils.vandelay.queued_authority_record.html",
1067 method => 'queued_record_html',
1071 record_type => 'auth'
1074 sub queued_record_html {
1075 my($self, $conn, $auth, $rec_id) = @_;
1076 my $e = new_editor(xact=>1,authtoken => $auth);
1077 return $e->die_event unless $e->checkauth;
1079 if($self->{record_type} eq 'bib') {
1080 $rec = $e->retrieve_vandelay_queued_bib_record($rec_id)
1081 or return $e->die_event;
1083 $rec = $e->retrieve_vandelay_queued_authority_record($rec_id)
1084 or return $e->die_event;
1088 return $U->simplereq(
1090 'open-ils.search.biblio.record.html', undef, 1, $rec->marc);
1094 __PACKAGE__->register_method(
1095 api_name => "open-ils.vandelay.bib_queue.summary.retrieve",
1096 method => 'retrieve_queue_summary',
1100 record_type => 'bib'
1102 __PACKAGE__->register_method(
1103 api_name => "open-ils.vandelay.auth_queue.summary.retrieve",
1104 method => 'retrieve_queue_summary',
1108 record_type => 'auth'
1111 sub retrieve_queue_summary {
1112 my($self, $conn, $auth, $queue_id) = @_;
1113 my $e = new_editor(xact=>1, authtoken => $auth);
1114 return $e->die_event unless $e->checkauth;
1117 my $type = $self->{record_type};
1118 if($type eq 'bib') {
1119 $queue = $e->retrieve_vandelay_bib_queue($queue_id)
1120 or return $e->die_event;
1122 $queue = $e->retrieve_vandelay_authority_queue($queue_id)
1123 or return $e->die_event;
1126 my $evt = check_queue_perms($e, $type, $queue);
1127 return $evt if $evt;
1129 my $search = 'search_vandelay_queued_bib_record';
1130 $search =~ s/bib/authority/ if $type ne 'bib';
1134 total => scalar(@{$e->$search({queue => $queue_id}, {idlist=>1})}),
1135 imported => scalar(@{$e->$search({queue => $queue_id, import_time => {'!=' => undef}}, {idlist=>1})}),
1139 # --------------------------------------------------------------------------------
1140 # Given a list of queued record IDs, imports all items attached to those records
1141 # --------------------------------------------------------------------------------
1142 sub import_record_asset_list_impl {
1143 my($conn, $rec_ids, $requestor) = @_;
1145 my $roe = new_editor(xact=> 1, requestor => $requestor);
1149 total => scalar(@$rec_ids),
1150 step => 1, # how often to respond
1155 for my $rec_id (@$rec_ids) {
1156 my $rec = $roe->retrieve_vandelay_queued_bib_record($rec_id);
1157 next unless $rec and $rec->import_time;
1158 my $item_ids = $roe->search_vandelay_import_item({record => $rec->id}, {idlist=>1});
1160 for my $item_id (@$item_ids) {
1161 my $e = new_editor(requestor => $requestor, xact => 1);
1162 my $item = $e->retrieve_vandelay_import_item($item_id);
1163 $report_args{progress}++;
1164 $report_args{import_item} = $item;
1166 # --------------------------------------------------------------------------------
1167 # Find or create the volume
1168 # --------------------------------------------------------------------------------
1170 OpenILS::Application::Cat::AssetCommon->find_or_create_volume(
1171 $e, $item->call_number, $rec->imported_as, $item->owning_lib);
1174 respond_with_status(%report_args, evt => $evt);
1178 # --------------------------------------------------------------------------------
1179 # Create the new copy
1180 # --------------------------------------------------------------------------------
1181 my $copy = Fieldmapper::asset::copy->new;
1182 $copy->loan_duration(2);
1183 $copy->fine_level(2);
1184 $copy->barcode($item->barcode);
1185 $copy->location($item->location);
1186 $copy->circ_lib($item->circ_lib || $item->owning_lib);
1187 $copy->status( defined($item->status) ? $item->status : OILS_COPY_STATUS_IN_PROCESS );
1188 $copy->circulate($item->circulate);
1189 $copy->deposit($item->deposit);
1190 $copy->deposit_amount($item->deposit_amount);
1191 $copy->ref($item->ref);
1192 $copy->holdable($item->holdable);
1193 $copy->price($item->price);
1194 $copy->circ_as_type($item->circ_as_type);
1195 $copy->alert_message($item->alert_message);
1196 $copy->opac_visible($item->opac_visible);
1197 $copy->circ_modifier($item->circ_modifier);
1199 # --------------------------------------------------------------------------------
1200 # see if a valid circ_modifier was provided
1201 # --------------------------------------------------------------------------------
1202 if($copy->circ_modifier and not $e->search_config_circ_modifier({code=>$item->circ_modifier})->[0]) {
1203 $report_args{import_error} = 'import.item.invalid.circ_modifier';
1204 respond_with_status(%report_args, evt => $e->die_event);
1208 if($copy->location and not $e->retrieve_asset_copy_location($copy->location)) {
1209 $report_args{import_error} = 'import.item.invalid.location';
1210 respond_with_status(%report_args, evt => $e->die_event);
1214 if($evt = OpenILS::Application::Cat::AssetCommon->create_copy($e, $vol, $copy)) {
1215 try { $e->rollback } otherwise {}; # sometimes calls die_event, sometimes not
1216 $report_args{evt} = $evt;
1217 $report_args{import_error} = 'import.item.duplicate.barcode'
1218 if $evt->{textcode} eq 'ITEM_BARCODE_EXISTS';
1219 respond_with_status(%report_args);
1223 # --------------------------------------------------------------------------------
1225 # --------------------------------------------------------------------------------
1226 $evt = OpenILS::Application::Cat::AssetCommon->create_copy_note(
1227 $e, $copy, '', $item->pub_note, 1) if $item->pub_note;
1230 respond_with_status(%report_args, evt => $evt);
1234 $evt = OpenILS::Application::Cat::AssetCommon->create_copy_note(
1235 $e, $copy, '', $item->priv_note, 1) if $item->priv_note;
1238 respond_with_status(%report_args, evt => $evt);
1242 # --------------------------------------------------------------------------------
1243 # Item import succeeded
1244 # --------------------------------------------------------------------------------
1246 $report_args{in_count}++;
1247 respond_with_status(%report_args, imported_as => $copy->id)
1255 sub respond_with_status {
1258 # If the import failed, track the failure reason
1260 my $error = $args{import_error};
1261 my $evt = $args{evt};
1263 if($error or $evt) {
1265 my $item = $args{import_item};
1267 my $error ||= 'general.unknown';
1268 $item->import_error($error);
1271 my $detail = sprintf("%s : %s", $evt->{textcode}, substr($evt->{desc}, 0, 140));
1272 $item->error_detail($detail);
1275 my $e = new_editor(xact => 1);
1276 $e->update_vandelay_import_item($item);
1280 return unless $args{report_all} or ($args{progress} % $args{step}) == 0;
1281 $args{step} *= 2 unless $args{step} == 256;
1283 $args{conn}->respond({
1284 map { $_ => $args{$_} } qw/total progress success_count/,
1289 __PACKAGE__->register_method(
1290 api_name => "open-ils.vandelay.match_set.get_tree",
1291 method => "match_set_get_tree",
1295 desc => q/For a given vms object, return a tree of match set points
1296 represented by a vmsp object with recursively fleshed
1301 sub match_set_get_tree {
1302 my ($self, $conn, $authtoken, $match_set_id) = @_;
1304 $match_set_id = int($match_set_id) or return;
1306 my $e = new_editor("authtoken" => $authtoken);
1307 $e->checkauth or return $e->die_event;
1309 my $set = $e->retrieve_vandelay_match_set($match_set_id) or
1310 return $e->die_event;
1312 $e->allowed("ADMIN_IMPORT_MATCH_SET", $set->owner) or
1313 return $e->die_event;
1315 my $tree = $e->search_vandelay_match_set_point([
1316 {"match_set" => $match_set_id, "parent" => undef},
1317 {"flesh" => -1, "flesh_fields" => {"vmsp" => ["children"]}}
1318 ]) or return $e->die_event;
1324 __PACKAGE__->register_method(
1325 api_name => "open-ils.vandelay.match_set.update",
1326 method => "match_set_update_tree",
1330 desc => q/Replace any vmsp objects associated with a given (by ID) vms
1331 with the given objects (recursively fleshed vmsp tree)./
1335 sub _walk_new_vmsp {
1336 my ($e, $match_set_id, $node, $parent_id) = @_;
1338 my $point = new Fieldmapper::vandelay::match_set_point;
1339 $point->parent($parent_id);
1340 $point->match_set($match_set_id);
1341 $point->$_($node->$_) for (qw/bool_op svf tag subfield negate quality/);
1343 $e->create_vandelay_match_set_point($point) or return $e->die_event;
1345 $parent_id = $e->data->id;
1346 if ($node->children && @{$node->children}) {
1347 for (@{$node->children}) {
1348 return $e->die_event if
1349 _walk_new_vmsp($e, $match_set_id, $_, $parent_id);
1356 sub match_set_update_tree {
1357 my ($self, $conn, $authtoken, $match_set_id, $tree) = @_;
1359 my $e = new_editor("xact" => 1, "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 $existing = $e->search_vandelay_match_set_point([
1369 {"match_set" => $match_set_id},
1370 {"order_by" => {"vmsp" => "id DESC"}}
1371 ]) or return $e->die_event;
1373 foreach (@$existing) {
1374 $e->delete_vandelay_match_set_point($_) or return $e->die_event;
1377 _walk_new_vmsp($e, $match_set_id, $tree);
1379 $e->commit or return $e->die_event;