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/
402 sub retrieve_queued_records {
403 my($self, $conn, $auth, $queue_id, $options) = @_;
404 my $e = new_editor(authtoken => $auth, xact => 1);
405 return $e->die_event unless $e->checkauth;
407 my $limit = $$options{limit} || 20;
408 my $offset = $$options{offset} || 0;
410 my $type = $self->{record_type};
413 $queue = $e->retrieve_vandelay_bib_queue($queue_id) or return $e->die_event;
415 $queue = $e->retrieve_vandelay_authority_queue($queue_id) or return $e->die_event;
417 my $evt = check_queue_perms($e, $type, $queue);
418 return $evt if ($evt);
420 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
421 my $search = ($type eq 'bib') ?
422 'search_vandelay_queued_bib_record' : 'search_vandelay_queued_authority_record';
423 my $retrieve = ($type eq 'bib') ?
424 'retrieve_vandelay_queued_bib_record' : 'retrieve_vandelay_queued_authority_record';
426 my $filter = ($$options{non_imported}) ? {import_time => undef} : {};
429 if($self->api_name =~ /matches/) {
430 # fetch only matched records
431 $record_ids = queued_records_with_matches($e, $type, $queue_id, $limit, $offset, $filter);
433 # fetch all queue records
434 $record_ids = $e->$search([
435 {queue => $queue_id, %$filter},
436 {order_by => {$class => 'id'}, limit => $limit, offset => $offset}
443 for my $rec_id (@$record_ids) {
446 flesh_fields => {$class => ['attributes', 'matches']},
448 my $rec = $e->$retrieve([$rec_id, $params]);
449 $rec->clear_marc if $$options{clear_marc};
450 $conn->respond($rec);
456 sub check_queue_perms {
457 my($e, $type, $queue) = @_;
458 if ($type eq 'bib') {
459 return $e->die_event unless
460 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
461 $e->allowed('CREATE_BIB_IMPORT_QUEUE'));
463 return $e->die_event unless
464 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
465 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE'));
471 __PACKAGE__->register_method(
472 api_name => "open-ils.vandelay.bib_record.list.import",
473 method => 'import_record_list',
480 __PACKAGE__->register_method(
481 api_name => "open-ils.vandelay.auth_record.list.import",
482 method => 'import_record_list',
486 record_type => 'auth'
489 sub import_record_list {
490 my($self, $conn, $auth, $rec_ids, $args) = @_;
491 my $e = new_editor(authtoken => $auth, xact => 1);
492 return $e->die_event unless $e->checkauth;
494 my $err = import_record_list_impl($self, $conn, $rec_ids, $e->requestor, $args);
497 return {complete => 1};
501 __PACKAGE__->register_method(
502 api_name => "open-ils.vandelay.bib_queue.import",
503 method => 'import_queue',
511 __PACKAGE__->register_method(
512 api_name => "open-ils.vandelay.auth_queue.import",
513 method => 'import_queue',
518 record_type => 'auth'
520 __PACKAGE__->register_method(
521 api_name => "open-ils.vandelay.bib_queue.nomatch.import",
522 method => 'import_queue',
527 desc => q/Only import records that have no collisions/
533 __PACKAGE__->register_method(
534 api_name => "open-ils.vandelay.auth_queue.nomatch.import",
535 method => 'import_queue',
540 desc => q/Only import records that have no collisions/
543 record_type => 'auth'
546 my($self, $conn, $auth, $q_id, $options) = @_;
547 my $e = new_editor(authtoken => $auth, xact => 1);
548 return $e->die_event unless $e->checkauth;
550 my $type = $self->{record_type};
551 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
553 my $query = {queue => $q_id, import_time => undef};
555 if($self->api_name =~ /nomatch/) {
556 my $matched_recs = queued_records_with_matches($e, $type, $q_id, undef, undef, {import_time => undef});
557 $query->{id} = {'not in' => $matched_recs} if @$matched_recs;
560 my $search = ($type eq 'bib') ?
561 'search_vandelay_queued_bib_record' : 'search_vandelay_queued_authority_record';
562 my $rec_ids = $e->$search($query, {idlist => 1});
563 my $err = import_record_list_impl($self, $conn, $rec_ids, $e->requestor, $options);
564 try {$e->rollback} otherwise {}; # only using this to make the read authoritative -- don't die from it
566 return {complete => 1};
569 # returns a list of queued record IDs for a given queue that
570 # have at least one entry in the match table
571 sub queued_records_with_matches {
572 my($e, $type, $q_id, $limit, $offset, $filter) = @_;
574 my $match_class = 'vbm';
575 my $rec_class = 'vqbr';
576 if($type eq 'auth') {
577 $match_class = 'vam';
582 $filter->{queue} = $q_id;
586 select => {$match_class => ['queued_record']},
591 fkey => 'queued_record',
598 if($limit or defined $offset) {
601 $query->{limit} = $limit;
602 $query->{offset} = $offset;
605 my $data = $e->json_query($query);
606 return [ map {$_->{queued_record}} @$data ];
610 sub import_record_list_impl {
611 my($self, $conn, $rec_ids, $requestor, $args) = @_;
613 my $overlay_map = $args->{overlay_map} || {};
614 my $type = $self->{record_type};
621 total => scalar(@$rec_ids),
622 report_all => $$args{report_all}
625 my $auto_overlay_exact = $$args{auto_overlay_exact};
626 my $auto_overlay_1match = $$args{auto_overlay_1match};
627 my $merge_profile = $$args{merge_profile};
628 my $bib_source = $$args{bib_source};
630 my $overlay_func = 'vandelay.overlay_bib_record';
631 my $auto_overlay_func = 'vandelay.auto_overlay_bib_record';
632 my $retrieve_func = 'retrieve_vandelay_queued_bib_record';
633 my $update_func = 'update_vandelay_queued_bib_record';
634 my $search_func = 'search_vandelay_queued_bib_record';
635 my $retrieve_queue_func = 'retrieve_vandelay_bib_queue';
636 my $update_queue_func = 'update_vandelay_bib_queue';
637 my $rec_class = 'vqbr';
639 my $editor = new_editor();
642 my $sources = $editor->search_config_bib_source({id => {'!=' => undef}});
643 $bib_sources{$_->id} = $_->source for @$sources;
645 if($type eq 'auth') {
646 $overlay_func =~ s/bib/auth/o;
647 $auto_overlay_func = s/bib/auth/o;
648 $retrieve_func =~ s/bib/authority/o;
649 $retrieve_queue_func =~ s/bib/authority/o;
650 $update_queue_func =~ s/bib/authority/o;
651 $update_func =~ s/bib/authority/o;
652 $search_func =~ s/bib/authority/o;
657 for my $rec_id (@$rec_ids) {
660 my $overlay_target = $overlay_map->{$rec_id};
662 my $e = new_editor(xact => 1);
663 $e->requestor($requestor);
665 $report_args{progress}++;
666 $report_args{e} = $e;
668 my $rec = $e->$retrieve_func([
671 flesh_fields => { $rec_class => ['matches']},
676 finish_rec_import_attempt(%report_args, evt => $e->event);
680 $report_args{rec} = $rec;
682 if($rec->import_time) {
687 $queues{$rec->queue} = 1;
692 if(defined $overlay_target) {
693 # Caller chose an explicit overlay target
695 my $res = $e->json_query(
706 if($res and ($res = $res->[0])) {
708 if($res->{$overlay_func} eq 't') {
709 $logger->info("vl: $type direct overlay succeeded for queued rec " .
710 $rec->id . " and overlay target $overlay_target");
716 $logger->error("vl: Error attempting overlay with func=$overlay_func, profile=$merge_profile, record=$rec_id");
721 if($auto_overlay_1match) {
722 # caller says to overlay if there is exactly 1 match
724 my %match_recs = map { $_->eg_record => 1 } @{$rec->matches};
726 if( scalar(keys %match_recs) == 1) { # all matches point to the same record
728 my $res = $e->json_query(
733 $rec->matches->[0]->eg_record,
739 if($res and ($res = $res->[0])) {
741 if($res->{$overlay_func} eq 't') {
742 $logger->info("vl: $type overlay-1match succeeded for queued rec " . $rec->id);
748 $logger->error("vl: Error attempting overlay with func=$overlay_func, profile=$merge_profile, record=$rec_id");
753 if(!$imported and !$error and $auto_overlay_exact and scalar(@{$rec->matches}) == 1 ) {
755 # caller says to overlay if there is an /exact/ match
757 my $res = $e->json_query(
767 if($res and ($res = $res->[0])) {
769 if($res->{$auto_overlay_func} eq 't') {
770 $logger->info("vl: $type auto-overlay succeeded for queued rec " . $rec->id);
773 $logger->info("vl: $type auto-overlay failed for queued rec " . $rec->id);
778 $logger->error("vl: Error attempting overlay with func=$auto_overlay_func, profile=$merge_profile, record=$rec_id");
782 if(!$imported and !$error) {
784 # No overlay / merge occurred. Do a traditional record import by creating a new record
786 $logger->info("vl: creating new $type record for queued record $rec_id");
788 $record = OpenILS::Application::Cat::BibCommon->biblio_record_xml_import($e, $rec->marc, $bib_sources{$rec->bib_source});
791 $record = OpenILS::Application::Cat::AuthCommon->import_authority_record($e, $rec->marc); #$source);
794 if($U->event_code($record)) {
795 my $import_error = 'import.duplicate.tcn' if $record->{textcode} eq 'TCN_EXISTS';
796 finish_rec_import_attempt(%report_args, import_error => $import_error, evt => $record);
800 $logger->info("vl: successfully imported new $type record");
801 $rec->imported_as($record->id);
802 $rec->import_time('now');
804 $imported = 1 if $e->$update_func($rec);
810 push @success_rec_ids, $rec_id;
811 finish_rec_import_attempt(%report_args);
814 # Send an update whenever there's an error
815 finish_rec_import_attempt(%report_args, evt => $e->event);
819 # see if we need to mark any queues as complete
820 for my $q_id (keys %queues) {
822 my $e = new_editor(xact => 1);
823 my $remaining = $e->$search_func(
824 [{queue => $q_id, import_time => undef}, {limit =>1}], {idlist => 1});
826 unless(@$remaining) {
827 my $queue = $e->$retrieve_queue_func($q_id);
829 unless($U->is_true($queue->complete)) {
830 $queue->complete('t');
831 $e->$update_queue_func($queue) or return $e->die_event;
840 import_record_asset_list_impl($conn, \@success_rec_ids, $requestor);
842 $conn->respond({total => $report_args{total}, progress => $report_args{progress}});
846 # tracks any import errors, commits the current xact, responds to the client
847 sub finish_rec_import_attempt {
849 my $evt = $args{evt};
850 my $rec = $args{rec};
853 my $error = $args{import_error};
854 $error = 'general.unknown' if $evt and not $error;
861 # since an error occurred, there's no guarantee the transaction wasn't
862 # rolled back. force a rollback and create a new editor.
864 $e = new_editor(xact => 1);
865 $rec->import_error($error);
868 my $detail = sprintf("%s : %s", $evt->{textcode}, substr($evt->{desc}, 0, 140));
869 $rec->error_detail($detail);
872 my $method = 'update_vandelay_queued_bib_record';
873 $method =~ s/bib/authority/ if $args{type} eq 'auth';
874 $e->$method($rec) and $e->commit or $e->rollback;
878 $rec->clear_import_error;
879 $rec->clear_error_detail;
884 # requested queued record was not found
889 if($args{report_all} or ($args{progress} % $args{step}) == 0) {
891 $args{conn}->respond({
892 total => $args{total},
893 progress => $args{progress},
894 imported => ($rec) ? $rec->id : undef,
898 # report often at first, climb quickly, then hold steady
899 $args{step} *= 2 unless $args{step} == 256;
907 __PACKAGE__->register_method(
908 api_name => "open-ils.vandelay.bib_queue.owner.retrieve",
909 method => 'owner_queue_retrieve',
915 __PACKAGE__->register_method(
916 api_name => "open-ils.vandelay.authority_queue.owner.retrieve",
917 method => 'owner_queue_retrieve',
921 record_type => 'auth'
924 sub owner_queue_retrieve {
925 my($self, $conn, $auth, $owner_id, $filters) = @_;
926 my $e = new_editor(authtoken => $auth, xact => 1);
927 return $e->die_event unless $e->checkauth;
928 $owner_id = $e->requestor->id; # XXX add support for viewing other's queues?
931 my $search = {owner => $owner_id};
932 $search->{$_} = $filters->{$_} for keys %$filters;
934 if($self->{record_type} eq 'bib') {
935 $queues = $e->search_vandelay_bib_queue(
936 [$search, {order_by => {vbq => 'evergreen.lowercase(name)'}}]);
938 $queues = $e->search_vandelay_authority_queue(
939 [$search, {order_by => {vaq => 'evergreen.lowercase(name)'}}]);
941 $conn->respond($_) for @$queues;
946 __PACKAGE__->register_method(
947 api_name => "open-ils.vandelay.bib_queue.delete",
948 method => "delete_queue",
953 __PACKAGE__->register_method(
954 api_name => "open-ils.vandelay.auth_queue.delete",
955 method => "delete_queue",
958 record_type => 'auth'
962 my($self, $conn, $auth, $q_id) = @_;
963 my $e = new_editor(xact => 1, authtoken => $auth);
964 return $e->die_event unless $e->checkauth;
965 if($self->{record_type} eq 'bib') {
966 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE');
967 my $queue = $e->retrieve_vandelay_bib_queue($q_id)
968 or return $e->die_event;
969 $e->delete_vandelay_bib_queue($queue)
970 or return $e->die_event;
972 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE');
973 my $queue = $e->retrieve_vandelay_authority_queue($q_id)
974 or return $e->die_event;
975 $e->delete_vandelay_authority_queue($queue)
976 or return $e->die_event;
983 __PACKAGE__->register_method(
984 api_name => "open-ils.vandelay.queued_bib_record.html",
985 method => 'queued_record_html',
991 __PACKAGE__->register_method(
992 api_name => "open-ils.vandelay.queued_authority_record.html",
993 method => 'queued_record_html',
997 record_type => 'auth'
1000 sub queued_record_html {
1001 my($self, $conn, $auth, $rec_id) = @_;
1002 my $e = new_editor(xact=>1,authtoken => $auth);
1003 return $e->die_event unless $e->checkauth;
1005 if($self->{record_type} eq 'bib') {
1006 $rec = $e->retrieve_vandelay_queued_bib_record($rec_id)
1007 or return $e->die_event;
1009 $rec = $e->retrieve_vandelay_queued_authority_record($rec_id)
1010 or return $e->die_event;
1014 return $U->simplereq(
1016 'open-ils.search.biblio.record.html', undef, 1, $rec->marc);
1020 __PACKAGE__->register_method(
1021 api_name => "open-ils.vandelay.bib_queue.summary.retrieve",
1022 method => 'retrieve_queue_summary',
1026 record_type => 'bib'
1028 __PACKAGE__->register_method(
1029 api_name => "open-ils.vandelay.auth_queue.summary.retrieve",
1030 method => 'retrieve_queue_summary',
1034 record_type => 'auth'
1037 sub retrieve_queue_summary {
1038 my($self, $conn, $auth, $queue_id) = @_;
1039 my $e = new_editor(xact=>1, authtoken => $auth);
1040 return $e->die_event unless $e->checkauth;
1043 my $type = $self->{record_type};
1044 if($type eq 'bib') {
1045 $queue = $e->retrieve_vandelay_bib_queue($queue_id)
1046 or return $e->die_event;
1048 $queue = $e->retrieve_vandelay_authority_queue($queue_id)
1049 or return $e->die_event;
1052 my $evt = check_queue_perms($e, $type, $queue);
1053 return $evt if $evt;
1055 my $search = 'search_vandelay_queued_bib_record';
1056 $search =~ s/bib/authority/ if $type ne 'bib';
1060 total => scalar(@{$e->$search({queue => $queue_id}, {idlist=>1})}),
1061 imported => scalar(@{$e->$search({queue => $queue_id, import_time => {'!=' => undef}}, {idlist=>1})}),
1065 # --------------------------------------------------------------------------------
1066 # Given a list of queued record IDs, imports all items attached to those records
1067 # --------------------------------------------------------------------------------
1068 sub import_record_asset_list_impl {
1069 my($conn, $rec_ids, $requestor) = @_;
1071 my $roe = new_editor(xact=> 1, requestor => $requestor);
1075 total => scalar(@$rec_ids),
1076 step => 1, # how often to respond
1081 for my $rec_id (@$rec_ids) {
1082 my $rec = $roe->retrieve_vandelay_queued_bib_record($rec_id);
1083 next unless $rec and $rec->import_time;
1084 my $item_ids = $roe->search_vandelay_import_item({record => $rec->id}, {idlist=>1});
1086 for my $item_id (@$item_ids) {
1087 my $e = new_editor(requestor => $requestor, xact => 1);
1088 my $item = $e->retrieve_vandelay_import_item($item_id);
1089 $report_args{progress}++;
1090 $report_args{import_item} = $item;
1092 # --------------------------------------------------------------------------------
1093 # Find or create the volume
1094 # --------------------------------------------------------------------------------
1096 OpenILS::Application::Cat::AssetCommon->find_or_create_volume(
1097 $e, $item->call_number, $rec->imported_as, $item->owning_lib);
1100 respond_with_status(%report_args, evt => $evt);
1104 # --------------------------------------------------------------------------------
1105 # Create the new copy
1106 # --------------------------------------------------------------------------------
1107 my $copy = Fieldmapper::asset::copy->new;
1108 $copy->loan_duration(2);
1109 $copy->fine_level(2);
1110 $copy->barcode($item->barcode);
1111 $copy->location($item->location);
1112 $copy->circ_lib($item->circ_lib || $item->owning_lib);
1113 $copy->status( defined($item->status) ? $item->status : OILS_COPY_STATUS_IN_PROCESS );
1114 $copy->circulate($item->circulate);
1115 $copy->deposit($item->deposit);
1116 $copy->deposit_amount($item->deposit_amount);
1117 $copy->ref($item->ref);
1118 $copy->holdable($item->holdable);
1119 $copy->price($item->price);
1120 $copy->circ_as_type($item->circ_as_type);
1121 $copy->alert_message($item->alert_message);
1122 $copy->opac_visible($item->opac_visible);
1123 $copy->circ_modifier($item->circ_modifier);
1125 # --------------------------------------------------------------------------------
1126 # see if a valid circ_modifier was provided
1127 # --------------------------------------------------------------------------------
1128 if($copy->circ_modifier and not $e->search_config_circ_modifier({code=>$item->circ_modifier})->[0]) {
1129 $report_args{import_error} = 'import.item.invalid.circ_modifier';
1130 respond_with_status(%report_args, evt => $e->die_event);
1134 if($copy->location and not $e->retrieve_asset_copy_location($copy->location)) {
1135 $report_args{import_error} = 'import.item.invalid.location';
1136 respond_with_status(%report_args, evt => $e->die_event);
1140 if($evt = OpenILS::Application::Cat::AssetCommon->create_copy($e, $vol, $copy)) {
1141 try { $e->rollback } otherwise {}; # sometimes calls die_event, sometimes not
1142 $report_args{evt} = $evt;
1143 $report_args{import_error} = 'import.item.duplicate.barcode'
1144 if $evt->{textcode} eq 'ITEM_BARCODE_EXISTS';
1145 respond_with_status(%report_args);
1149 # --------------------------------------------------------------------------------
1151 # --------------------------------------------------------------------------------
1152 $evt = OpenILS::Application::Cat::AssetCommon->create_copy_note(
1153 $e, $copy, '', $item->pub_note, 1) if $item->pub_note;
1156 respond_with_status(%report_args, evt => $evt);
1160 $evt = OpenILS::Application::Cat::AssetCommon->create_copy_note(
1161 $e, $copy, '', $item->priv_note, 1) if $item->priv_note;
1164 respond_with_status(%report_args, evt => $evt);
1168 # --------------------------------------------------------------------------------
1169 # Item import succeeded
1170 # --------------------------------------------------------------------------------
1172 $report_args{in_count}++;
1173 respond_with_status(%report_args, imported_as => $copy->id)
1181 sub respond_with_status {
1184 # If the import failed, track the failure reason
1186 my $error = $args{import_error};
1187 my $evt = $args{evt};
1189 if($error or $evt) {
1191 my $item = $args{import_item};
1193 my $error ||= 'general.unknown';
1194 $item->import_error($error);
1197 my $detail = sprintf("%s : %s", $evt->{textcode}, substr($evt->{desc}, 0, 140));
1198 $item->error_detail($detail);
1201 my $e = new_editor(xact => 1);
1202 $e->update_vandelay_import_item($item);
1206 return unless $args{report_all} or ($args{progress} % $args{step}) == 0;
1207 $args{step} *= 2 unless $args{step} == 256;
1209 $args{conn}->respond({
1210 map { $_ => $args{$_} } qw/total progress success_count/,
1215 __PACKAGE__->register_method(
1216 api_name => "open-ils.vandelay.match_set.get_tree",
1217 method => "match_set_get_tree",
1221 desc => q/For a given vms object, return a tree of match set points
1222 represented by a vmsp object with recursively fleshed
1227 sub match_set_get_tree {
1228 my ($self, $conn, $authtoken, $match_set_id) = @_;
1230 $match_set_id = int($match_set_id) or return;
1232 my $e = new_editor("authtoken" => $authtoken);
1233 $e->checkauth or return $e->die_event;
1235 my $set = $e->retrieve_vandelay_match_set($match_set_id) or
1236 return $e->die_event;
1238 $e->allowed("ADMIN_IMPORT_MATCH_SET", $set->owner) or
1239 return $e->die_event;
1241 my $tree = $e->search_vandelay_match_set_point([
1242 {"match_set" => $match_set_id, "parent" => undef},
1243 {"flesh" => -1, "flesh_fields" => {"vmsp" => ["children"]}}
1244 ]) or return $e->die_event;
1250 __PACKAGE__->register_method(
1251 api_name => "open-ils.vandelay.match_set.update",
1252 method => "match_set_update_tree",
1256 desc => q/Replace any vmsp objects associated with a given (by ID) vms
1257 with the given objects (recursively fleshed vmsp tree)./
1261 sub _walk_new_vmsp {
1262 my ($e, $match_set_id, $node, $parent_id) = @_;
1264 my $point = new Fieldmapper::vandelay::match_set_point;
1265 $point->parent($parent_id);
1266 $point->match_set($match_set_id);
1267 $point->$_($node->$_) for (qw/bool_op svf tag subfield negate quality/);
1269 $e->create_vandelay_match_set_point($point) or return $e->die_event;
1271 $parent_id = $e->data->id;
1272 if ($node->children && @{$node->children}) {
1273 for (@{$node->children}) {
1274 return $e->die_event if
1275 _walk_new_vmsp($e, $match_set_id, $_, $parent_id);
1282 sub match_set_update_tree {
1283 my ($self, $conn, $authtoken, $match_set_id, $tree) = @_;
1285 my $e = new_editor("xact" => 1, "authtoken" => $authtoken);
1286 $e->checkauth or return $e->die_event;
1288 my $set = $e->retrieve_vandelay_match_set($match_set_id) or
1289 return $e->die_event;
1291 $e->allowed("ADMIN_IMPORT_MATCH_SET", $set->owner) or
1292 return $e->die_event;
1294 my $existing = $e->search_vandelay_match_set_point([
1295 {"match_set" => $match_set_id},
1296 {"order_by" => {"vmsp" => "id DESC"}}
1297 ]) or return $e->die_event;
1299 foreach (@$existing) {
1300 $e->delete_vandelay_match_set_point($_) or return $e->die_event;
1303 _walk_new_vmsp($e, $match_set_id, $tree);
1305 $e->commit or return $e->die_event;