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}) {
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 sub check_queue_perms {
474 my($e, $type, $queue) = @_;
475 if ($type eq 'bib') {
476 return $e->die_event unless
477 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
478 $e->allowed('CREATE_BIB_IMPORT_QUEUE'));
480 return $e->die_event unless
481 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
482 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE'));
488 __PACKAGE__->register_method(
489 api_name => "open-ils.vandelay.bib_record.list.import",
490 method => 'import_record_list',
497 __PACKAGE__->register_method(
498 api_name => "open-ils.vandelay.auth_record.list.import",
499 method => 'import_record_list',
503 record_type => 'auth'
506 sub import_record_list {
507 my($self, $conn, $auth, $rec_ids, $args) = @_;
508 my $e = new_editor(authtoken => $auth, xact => 1);
509 return $e->die_event unless $e->checkauth;
511 my $err = import_record_list_impl($self, $conn, $rec_ids, $e->requestor, $args);
514 return {complete => 1};
518 __PACKAGE__->register_method(
519 api_name => "open-ils.vandelay.bib_queue.import",
520 method => 'import_queue',
528 __PACKAGE__->register_method(
529 api_name => "open-ils.vandelay.auth_queue.import",
530 method => 'import_queue',
535 record_type => 'auth'
537 __PACKAGE__->register_method(
538 api_name => "open-ils.vandelay.bib_queue.nomatch.import",
539 method => 'import_queue',
544 desc => q/Only import records that have no collisions/
550 __PACKAGE__->register_method(
551 api_name => "open-ils.vandelay.auth_queue.nomatch.import",
552 method => 'import_queue',
557 desc => q/Only import records that have no collisions/
560 record_type => 'auth'
563 my($self, $conn, $auth, $q_id, $options) = @_;
564 my $e = new_editor(authtoken => $auth, xact => 1);
565 return $e->die_event unless $e->checkauth;
567 my $type = $self->{record_type};
568 my $class = ($type eq 'bib') ? 'vqbr' : 'vqar';
570 my $query = {queue => $q_id, import_time => undef};
572 if($self->api_name =~ /nomatch/) {
573 my $matched_recs = queued_records_with_matches($e, $type, $q_id, undef, undef, {import_time => undef});
574 $query->{id} = {'not in' => $matched_recs} if @$matched_recs;
577 my $search = ($type eq 'bib') ?
578 'search_vandelay_queued_bib_record' : 'search_vandelay_queued_authority_record';
579 my $rec_ids = $e->$search($query, {idlist => 1});
580 my $err = import_record_list_impl($self, $conn, $rec_ids, $e->requestor, $options);
581 try {$e->rollback} otherwise {}; # only using this to make the read authoritative -- don't die from it
583 return {complete => 1};
586 # returns a list of queued record IDs for a given queue that
587 # have at least one entry in the match table
588 sub queued_records_with_matches {
589 my($e, $type, $q_id, $limit, $offset, $filter) = @_;
591 my $match_class = 'vbm';
592 my $rec_class = 'vqbr';
593 if($type eq 'auth') {
594 $match_class = 'vam';
599 $filter->{queue} = $q_id;
603 select => {$match_class => ['queued_record']},
608 fkey => 'queued_record',
615 if($limit or defined $offset) {
618 $query->{limit} = $limit;
619 $query->{offset} = $offset;
622 my $data = $e->json_query($query);
623 return [ map {$_->{queued_record}} @$data ];
627 sub import_record_list_impl {
628 my($self, $conn, $rec_ids, $requestor, $args) = @_;
630 my $overlay_map = $args->{overlay_map} || {};
631 my $type = $self->{record_type};
638 total => scalar(@$rec_ids),
639 report_all => $$args{report_all}
642 my $auto_overlay_exact = $$args{auto_overlay_exact};
643 my $auto_overlay_1match = $$args{auto_overlay_1match};
644 my $merge_profile = $$args{merge_profile};
645 my $bib_source = $$args{bib_source};
647 my $overlay_func = 'vandelay.overlay_bib_record';
648 my $auto_overlay_func = 'vandelay.auto_overlay_bib_record';
649 my $retrieve_func = 'retrieve_vandelay_queued_bib_record';
650 my $update_func = 'update_vandelay_queued_bib_record';
651 my $search_func = 'search_vandelay_queued_bib_record';
652 my $retrieve_queue_func = 'retrieve_vandelay_bib_queue';
653 my $update_queue_func = 'update_vandelay_bib_queue';
654 my $rec_class = 'vqbr';
656 my $editor = new_editor();
659 my $sources = $editor->search_config_bib_source({id => {'!=' => undef}});
660 $bib_sources{$_->id} = $_->source for @$sources;
662 if($type eq 'auth') {
663 $overlay_func =~ s/bib/auth/o;
664 $auto_overlay_func = s/bib/auth/o;
665 $retrieve_func =~ s/bib/authority/o;
666 $retrieve_queue_func =~ s/bib/authority/o;
667 $update_queue_func =~ s/bib/authority/o;
668 $update_func =~ s/bib/authority/o;
669 $search_func =~ s/bib/authority/o;
674 for my $rec_id (@$rec_ids) {
677 my $overlay_target = $overlay_map->{$rec_id};
679 my $e = new_editor(xact => 1);
680 $e->requestor($requestor);
682 $report_args{progress}++;
683 $report_args{e} = $e;
685 my $rec = $e->$retrieve_func([
688 flesh_fields => { $rec_class => ['matches']},
693 finish_rec_import_attempt(%report_args, evt => $e->event);
697 $report_args{rec} = $rec;
699 if($rec->import_time) {
704 $queues{$rec->queue} = 1;
709 if(defined $overlay_target) {
710 # Caller chose an explicit overlay target
712 my $res = $e->json_query(
723 if($res and ($res = $res->[0])) {
725 if($res->{$overlay_func} eq 't') {
726 $logger->info("vl: $type direct overlay succeeded for queued rec " .
727 $rec->id . " and overlay target $overlay_target");
733 $logger->error("vl: Error attempting overlay with func=$overlay_func, profile=$merge_profile, record=$rec_id");
738 if($auto_overlay_1match) {
739 # caller says to overlay if there is exactly 1 match
741 my %match_recs = map { $_->eg_record => 1 } @{$rec->matches};
743 if( scalar(keys %match_recs) == 1) { # all matches point to the same record
745 my $res = $e->json_query(
750 $rec->matches->[0]->eg_record,
756 if($res and ($res = $res->[0])) {
758 if($res->{$overlay_func} eq 't') {
759 $logger->info("vl: $type overlay-1match succeeded for queued rec " . $rec->id);
765 $logger->error("vl: Error attempting overlay with func=$overlay_func, profile=$merge_profile, record=$rec_id");
770 if(!$imported and !$error and $auto_overlay_exact and scalar(@{$rec->matches}) == 1 ) {
772 # caller says to overlay if there is an /exact/ match
774 my $res = $e->json_query(
784 if($res and ($res = $res->[0])) {
786 if($res->{$auto_overlay_func} eq 't') {
787 $logger->info("vl: $type auto-overlay succeeded for queued rec " . $rec->id);
790 $logger->info("vl: $type auto-overlay failed for queued rec " . $rec->id);
795 $logger->error("vl: Error attempting overlay with func=$auto_overlay_func, profile=$merge_profile, record=$rec_id");
799 if(!$imported and !$error) {
801 # No overlay / merge occurred. Do a traditional record import by creating a new record
803 $logger->info("vl: creating new $type record for queued record $rec_id");
805 $record = OpenILS::Application::Cat::BibCommon->biblio_record_xml_import($e, $rec->marc, $bib_sources{$rec->bib_source});
808 $record = OpenILS::Application::Cat::AuthCommon->import_authority_record($e, $rec->marc); #$source);
811 if($U->event_code($record)) {
812 my $import_error = 'import.duplicate.tcn' if $record->{textcode} eq 'TCN_EXISTS';
813 finish_rec_import_attempt(%report_args, import_error => $import_error, evt => $record);
817 $logger->info("vl: successfully imported new $type record");
818 $rec->imported_as($record->id);
819 $rec->import_time('now');
821 $imported = 1 if $e->$update_func($rec);
827 push @success_rec_ids, $rec_id;
828 finish_rec_import_attempt(%report_args);
831 # Send an update whenever there's an error
832 finish_rec_import_attempt(%report_args, evt => $e->event);
836 # see if we need to mark any queues as complete
837 for my $q_id (keys %queues) {
839 my $e = new_editor(xact => 1);
840 my $remaining = $e->$search_func(
841 [{queue => $q_id, import_time => undef}, {limit =>1}], {idlist => 1});
843 unless(@$remaining) {
844 my $queue = $e->$retrieve_queue_func($q_id);
846 unless($U->is_true($queue->complete)) {
847 $queue->complete('t');
848 $e->$update_queue_func($queue) or return $e->die_event;
857 import_record_asset_list_impl($conn, \@success_rec_ids, $requestor);
859 $conn->respond({total => $report_args{total}, progress => $report_args{progress}});
863 # tracks any import errors, commits the current xact, responds to the client
864 sub finish_rec_import_attempt {
866 my $evt = $args{evt};
867 my $rec = $args{rec};
870 my $error = $args{import_error};
871 $error = 'general.unknown' if $evt and not $error;
878 # since an error occurred, there's no guarantee the transaction wasn't
879 # rolled back. force a rollback and create a new editor.
881 $e = new_editor(xact => 1);
882 $rec->import_error($error);
885 my $detail = sprintf("%s : %s", $evt->{textcode}, substr($evt->{desc}, 0, 140));
886 $rec->error_detail($detail);
889 my $method = 'update_vandelay_queued_bib_record';
890 $method =~ s/bib/authority/ if $args{type} eq 'auth';
891 $e->$method($rec) and $e->commit or $e->rollback;
895 $rec->clear_import_error;
896 $rec->clear_error_detail;
901 # requested queued record was not found
906 if($args{report_all} or ($args{progress} % $args{step}) == 0) {
908 $args{conn}->respond({
909 total => $args{total},
910 progress => $args{progress},
911 imported => ($rec) ? $rec->id : undef,
915 # report often at first, climb quickly, then hold steady
916 $args{step} *= 2 unless $args{step} == 256;
924 __PACKAGE__->register_method(
925 api_name => "open-ils.vandelay.bib_queue.owner.retrieve",
926 method => 'owner_queue_retrieve',
932 __PACKAGE__->register_method(
933 api_name => "open-ils.vandelay.authority_queue.owner.retrieve",
934 method => 'owner_queue_retrieve',
938 record_type => 'auth'
941 sub owner_queue_retrieve {
942 my($self, $conn, $auth, $owner_id, $filters) = @_;
943 my $e = new_editor(authtoken => $auth, xact => 1);
944 return $e->die_event unless $e->checkauth;
945 $owner_id = $e->requestor->id; # XXX add support for viewing other's queues?
948 my $search = {owner => $owner_id};
949 $search->{$_} = $filters->{$_} for keys %$filters;
951 if($self->{record_type} eq 'bib') {
952 $queues = $e->search_vandelay_bib_queue(
953 [$search, {order_by => {vbq => 'evergreen.lowercase(name)'}}]);
955 $queues = $e->search_vandelay_authority_queue(
956 [$search, {order_by => {vaq => 'evergreen.lowercase(name)'}}]);
958 $conn->respond($_) for @$queues;
963 __PACKAGE__->register_method(
964 api_name => "open-ils.vandelay.bib_queue.delete",
965 method => "delete_queue",
970 __PACKAGE__->register_method(
971 api_name => "open-ils.vandelay.auth_queue.delete",
972 method => "delete_queue",
975 record_type => 'auth'
979 my($self, $conn, $auth, $q_id) = @_;
980 my $e = new_editor(xact => 1, authtoken => $auth);
981 return $e->die_event unless $e->checkauth;
982 if($self->{record_type} eq 'bib') {
983 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE');
984 my $queue = $e->retrieve_vandelay_bib_queue($q_id)
985 or return $e->die_event;
986 $e->delete_vandelay_bib_queue($queue)
987 or return $e->die_event;
989 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE');
990 my $queue = $e->retrieve_vandelay_authority_queue($q_id)
991 or return $e->die_event;
992 $e->delete_vandelay_authority_queue($queue)
993 or return $e->die_event;
1000 __PACKAGE__->register_method(
1001 api_name => "open-ils.vandelay.queued_bib_record.html",
1002 method => 'queued_record_html',
1006 record_type => 'bib'
1008 __PACKAGE__->register_method(
1009 api_name => "open-ils.vandelay.queued_authority_record.html",
1010 method => 'queued_record_html',
1014 record_type => 'auth'
1017 sub queued_record_html {
1018 my($self, $conn, $auth, $rec_id) = @_;
1019 my $e = new_editor(xact=>1,authtoken => $auth);
1020 return $e->die_event unless $e->checkauth;
1022 if($self->{record_type} eq 'bib') {
1023 $rec = $e->retrieve_vandelay_queued_bib_record($rec_id)
1024 or return $e->die_event;
1026 $rec = $e->retrieve_vandelay_queued_authority_record($rec_id)
1027 or return $e->die_event;
1031 return $U->simplereq(
1033 'open-ils.search.biblio.record.html', undef, 1, $rec->marc);
1037 __PACKAGE__->register_method(
1038 api_name => "open-ils.vandelay.bib_queue.summary.retrieve",
1039 method => 'retrieve_queue_summary',
1043 record_type => 'bib'
1045 __PACKAGE__->register_method(
1046 api_name => "open-ils.vandelay.auth_queue.summary.retrieve",
1047 method => 'retrieve_queue_summary',
1051 record_type => 'auth'
1054 sub retrieve_queue_summary {
1055 my($self, $conn, $auth, $queue_id) = @_;
1056 my $e = new_editor(xact=>1, authtoken => $auth);
1057 return $e->die_event unless $e->checkauth;
1060 my $type = $self->{record_type};
1061 if($type eq 'bib') {
1062 $queue = $e->retrieve_vandelay_bib_queue($queue_id)
1063 or return $e->die_event;
1065 $queue = $e->retrieve_vandelay_authority_queue($queue_id)
1066 or return $e->die_event;
1069 my $evt = check_queue_perms($e, $type, $queue);
1070 return $evt if $evt;
1072 my $search = 'search_vandelay_queued_bib_record';
1073 $search =~ s/bib/authority/ if $type ne 'bib';
1077 total => scalar(@{$e->$search({queue => $queue_id}, {idlist=>1})}),
1078 imported => scalar(@{$e->$search({queue => $queue_id, import_time => {'!=' => undef}}, {idlist=>1})}),
1082 # --------------------------------------------------------------------------------
1083 # Given a list of queued record IDs, imports all items attached to those records
1084 # --------------------------------------------------------------------------------
1085 sub import_record_asset_list_impl {
1086 my($conn, $rec_ids, $requestor) = @_;
1088 my $roe = new_editor(xact=> 1, requestor => $requestor);
1092 total => scalar(@$rec_ids),
1093 step => 1, # how often to respond
1098 for my $rec_id (@$rec_ids) {
1099 my $rec = $roe->retrieve_vandelay_queued_bib_record($rec_id);
1100 next unless $rec and $rec->import_time;
1101 my $item_ids = $roe->search_vandelay_import_item({record => $rec->id}, {idlist=>1});
1103 for my $item_id (@$item_ids) {
1104 my $e = new_editor(requestor => $requestor, xact => 1);
1105 my $item = $e->retrieve_vandelay_import_item($item_id);
1106 $report_args{progress}++;
1107 $report_args{import_item} = $item;
1109 # --------------------------------------------------------------------------------
1110 # Find or create the volume
1111 # --------------------------------------------------------------------------------
1113 OpenILS::Application::Cat::AssetCommon->find_or_create_volume(
1114 $e, $item->call_number, $rec->imported_as, $item->owning_lib);
1117 respond_with_status(%report_args, evt => $evt);
1121 # --------------------------------------------------------------------------------
1122 # Create the new copy
1123 # --------------------------------------------------------------------------------
1124 my $copy = Fieldmapper::asset::copy->new;
1125 $copy->loan_duration(2);
1126 $copy->fine_level(2);
1127 $copy->barcode($item->barcode);
1128 $copy->location($item->location);
1129 $copy->circ_lib($item->circ_lib || $item->owning_lib);
1130 $copy->status( defined($item->status) ? $item->status : OILS_COPY_STATUS_IN_PROCESS );
1131 $copy->circulate($item->circulate);
1132 $copy->deposit($item->deposit);
1133 $copy->deposit_amount($item->deposit_amount);
1134 $copy->ref($item->ref);
1135 $copy->holdable($item->holdable);
1136 $copy->price($item->price);
1137 $copy->circ_as_type($item->circ_as_type);
1138 $copy->alert_message($item->alert_message);
1139 $copy->opac_visible($item->opac_visible);
1140 $copy->circ_modifier($item->circ_modifier);
1142 # --------------------------------------------------------------------------------
1143 # see if a valid circ_modifier was provided
1144 # --------------------------------------------------------------------------------
1145 if($copy->circ_modifier and not $e->search_config_circ_modifier({code=>$item->circ_modifier})->[0]) {
1146 $report_args{import_error} = 'import.item.invalid.circ_modifier';
1147 respond_with_status(%report_args, evt => $e->die_event);
1151 if($copy->location and not $e->retrieve_asset_copy_location($copy->location)) {
1152 $report_args{import_error} = 'import.item.invalid.location';
1153 respond_with_status(%report_args, evt => $e->die_event);
1157 if($evt = OpenILS::Application::Cat::AssetCommon->create_copy($e, $vol, $copy)) {
1158 try { $e->rollback } otherwise {}; # sometimes calls die_event, sometimes not
1159 $report_args{evt} = $evt;
1160 $report_args{import_error} = 'import.item.duplicate.barcode'
1161 if $evt->{textcode} eq 'ITEM_BARCODE_EXISTS';
1162 respond_with_status(%report_args);
1166 # --------------------------------------------------------------------------------
1168 # --------------------------------------------------------------------------------
1169 $evt = OpenILS::Application::Cat::AssetCommon->create_copy_note(
1170 $e, $copy, '', $item->pub_note, 1) if $item->pub_note;
1173 respond_with_status(%report_args, evt => $evt);
1177 $evt = OpenILS::Application::Cat::AssetCommon->create_copy_note(
1178 $e, $copy, '', $item->priv_note, 1) if $item->priv_note;
1181 respond_with_status(%report_args, evt => $evt);
1185 # --------------------------------------------------------------------------------
1186 # Item import succeeded
1187 # --------------------------------------------------------------------------------
1189 $report_args{in_count}++;
1190 respond_with_status(%report_args, imported_as => $copy->id)
1198 sub respond_with_status {
1201 # If the import failed, track the failure reason
1203 my $error = $args{import_error};
1204 my $evt = $args{evt};
1206 if($error or $evt) {
1208 my $item = $args{import_item};
1210 my $error ||= 'general.unknown';
1211 $item->import_error($error);
1214 my $detail = sprintf("%s : %s", $evt->{textcode}, substr($evt->{desc}, 0, 140));
1215 $item->error_detail($detail);
1218 my $e = new_editor(xact => 1);
1219 $e->update_vandelay_import_item($item);
1223 return unless $args{report_all} or ($args{progress} % $args{step}) == 0;
1224 $args{step} *= 2 unless $args{step} == 256;
1226 $args{conn}->respond({
1227 map { $_ => $args{$_} } qw/total progress success_count/,
1232 __PACKAGE__->register_method(
1233 api_name => "open-ils.vandelay.match_set.get_tree",
1234 method => "match_set_get_tree",
1238 desc => q/For a given vms object, return a tree of match set points
1239 represented by a vmsp object with recursively fleshed
1244 sub match_set_get_tree {
1245 my ($self, $conn, $authtoken, $match_set_id) = @_;
1247 $match_set_id = int($match_set_id) or return;
1249 my $e = new_editor("authtoken" => $authtoken);
1250 $e->checkauth or return $e->die_event;
1252 my $set = $e->retrieve_vandelay_match_set($match_set_id) or
1253 return $e->die_event;
1255 $e->allowed("ADMIN_IMPORT_MATCH_SET", $set->owner) or
1256 return $e->die_event;
1258 my $tree = $e->search_vandelay_match_set_point([
1259 {"match_set" => $match_set_id, "parent" => undef},
1260 {"flesh" => -1, "flesh_fields" => {"vmsp" => ["children"]}}
1261 ]) or return $e->die_event;
1267 __PACKAGE__->register_method(
1268 api_name => "open-ils.vandelay.match_set.update",
1269 method => "match_set_update_tree",
1273 desc => q/Replace any vmsp objects associated with a given (by ID) vms
1274 with the given objects (recursively fleshed vmsp tree)./
1278 sub _walk_new_vmsp {
1279 my ($e, $match_set_id, $node, $parent_id) = @_;
1281 my $point = new Fieldmapper::vandelay::match_set_point;
1282 $point->parent($parent_id);
1283 $point->match_set($match_set_id);
1284 $point->$_($node->$_) for (qw/bool_op svf tag subfield negate quality/);
1286 $e->create_vandelay_match_set_point($point) or return $e->die_event;
1288 $parent_id = $e->data->id;
1289 if ($node->children && @{$node->children}) {
1290 for (@{$node->children}) {
1291 return $e->die_event if
1292 _walk_new_vmsp($e, $match_set_id, $_, $parent_id);
1299 sub match_set_update_tree {
1300 my ($self, $conn, $authtoken, $match_set_id, $tree) = @_;
1302 my $e = new_editor("xact" => 1, "authtoken" => $authtoken);
1303 $e->checkauth or return $e->die_event;
1305 my $set = $e->retrieve_vandelay_match_set($match_set_id) or
1306 return $e->die_event;
1308 $e->allowed("ADMIN_IMPORT_MATCH_SET", $set->owner) or
1309 return $e->die_event;
1311 my $existing = $e->search_vandelay_match_set_point([
1312 {"match_set" => $match_set_id},
1313 {"order_by" => {"vmsp" => "id DESC"}}
1314 ]) or return $e->die_event;
1316 foreach (@$existing) {
1317 $e->delete_vandelay_match_set_point($_) or return $e->die_event;
1320 _walk_new_vmsp($e, $match_set_id, $tree);
1322 $e->commit or return $e->die_event;