1 use strict; use warnings;
2 package OpenILS::Application::Cat;
3 use OpenILS::Application::AppUtils;
4 use OpenILS::Application;
5 use OpenILS::Application::Cat::Merge;
6 use OpenILS::Application::Cat::Authority;
7 use OpenILS::Application::Cat::BibCommon;
8 use OpenILS::Application::Cat::AssetCommon;
9 use base qw/OpenILS::Application/;
10 use Time::HiRes qw(time);
11 use OpenSRF::EX qw(:try);
12 use OpenSRF::Utils::JSON;
13 use OpenILS::Utils::Fieldmapper;
15 use OpenILS::Const qw/:const/;
18 use Unicode::Normalize;
20 use OpenILS::Utils::FlatXML;
21 use OpenILS::Utils::CStoreEditor q/:funcs/;
23 use OpenSRF::Utils::SettingsClient;
24 use OpenSRF::Utils::Logger qw($logger);
25 use OpenSRF::AppSession;
27 my $U = "OpenILS::Application::AppUtils";
31 __PACKAGE__->register_method(
32 method => "retrieve_marc_template",
33 api_name => "open-ils.cat.biblio.marc_template.retrieve",
35 Returns a MARC 'record tree' based on a set of pre-defined templates.
36 Templates include : book
39 sub retrieve_marc_template {
40 my( $self, $client, $type ) = @_;
41 return $marctemplates{$type} if defined($marctemplates{$type});
42 $marctemplates{$type} = _load_marc_template($type);
43 return $marctemplates{$type};
46 __PACKAGE__->register_method(
47 method => 'fetch_marc_template_types',
48 api_name => 'open-ils.cat.marc_template.types.retrieve'
51 my $marc_template_files;
53 sub fetch_marc_template_types {
54 my( $self, $conn ) = @_;
55 __load_marc_templates();
56 return [ keys %$marc_template_files ];
59 sub __load_marc_templates {
60 return if $marc_template_files;
61 if(!$conf) { $conf = OpenSRF::Utils::SettingsClient->new; }
63 $marc_template_files = $conf->config_value(
64 "apps", "open-ils.cat","app_settings", "marctemplates" );
66 $logger->info("Loaded marc templates: " . Dumper($marc_template_files));
69 sub _load_marc_template {
72 __load_marc_templates();
74 my $template = $$marc_template_files{$type};
75 open( F, $template ) or
76 throw OpenSRF::EX::ERROR ("Unable to open MARC template file: $template : $@");
80 my $xml = join('', @xml);
82 return XML::LibXML->new->parse_string($xml)->documentElement->toString;
87 __PACKAGE__->register_method(
88 method => 'fetch_bib_sources',
89 api_name => 'open-ils.cat.bib_sources.retrieve.all');
91 sub fetch_bib_sources {
92 return OpenILS::Application::Cat::BibCommon->fetch_bib_sources();
95 __PACKAGE__->register_method(
96 method => "create_record_xml",
97 api_name => "open-ils.cat.biblio.record.xml.create.override",
98 signature => q/@see open-ils.cat.biblio.record.xml.create/);
100 __PACKAGE__->register_method(
101 method => "create_record_xml",
102 api_name => "open-ils.cat.biblio.record.xml.create",
104 Inserts a new biblio with the given XML
108 sub create_record_xml {
109 my( $self, $client, $login, $xml, $source ) = @_;
111 my $override = 1 if $self->api_name =~ /override/;
113 my( $user_obj, $evt ) = $U->checksesperm($login, 'CREATE_MARC');
116 $logger->activity("user ".$user_obj->id." creating new MARC record");
118 my $meth = $self->method_lookup("open-ils.cat.biblio.record.xml.import");
120 $meth = $self->method_lookup(
121 "open-ils.cat.biblio.record.xml.import.override") if $override;
123 my ($s) = $meth->run($login, $xml, $source);
129 __PACKAGE__->register_method(
130 method => "biblio_record_replace_marc",
131 api_name => "open-ils.cat.biblio.record.xml.update",
134 Updates the XML for a given biblio record.
135 This does not change any other aspect of the record entry
136 exception the XML, the editor, and the edit date.
137 @return The update record object
141 __PACKAGE__->register_method(
142 method => 'biblio_record_replace_marc',
143 api_name => 'open-ils.cat.biblio.record.marc.replace',
145 @param auth The authtoken
146 @param recid The record whose MARC we're replacing
147 @param newxml The new xml to use
151 __PACKAGE__->register_method(
152 method => 'biblio_record_replace_marc',
153 api_name => 'open-ils.cat.biblio.record.marc.replace.override',
154 signature => q/@see open-ils.cat.biblio.record.marc.replace/
157 sub biblio_record_replace_marc {
158 my( $self, $conn, $auth, $recid, $newxml, $source ) = @_;
159 my $e = new_editor(authtoken=>$auth, xact=>1);
160 return $e->die_event unless $e->checkauth;
161 return $e->die_event unless $e->allowed('CREATE_MARC', $e->requestor->ws_ou);
163 my $fix_tcn = $self->api_name =~ /replace/o;
164 my $override = $self->api_name =~ /override/o;
166 my $res = OpenILS::Application::Cat::BibCommon->biblio_record_replace_marc(
167 $e, $recid, $newxml, $source, $fix_tcn, $override);
169 $e->commit unless $U->event_code($res);
171 #my $ses = OpenSRF::AppSession->create('open-ils.ingest');
172 #$ses->request('open-ils.ingest.full.biblio.record', $recid);
177 __PACKAGE__->register_method(
178 method => "update_biblio_record_entry",
179 api_name => "open-ils.cat.biblio.record_entry.update",
181 Updates a biblio.record_entry
182 @param auth The authtoken
183 @param record The record with updated values
184 @return 1 on success, Event on error.
188 sub update_biblio_record_entry {
189 my($self, $conn, $auth, $record) = @_;
190 my $e = new_editor(authtoken=>$auth, xact=>1);
191 return $e->die_event unless $e->checkauth;
192 return $e->die_event unless $e->allowed('UPDATE_RECORD');
193 $e->update_biblio_record_entry($record) or return $e->die_event;
198 __PACKAGE__->register_method(
199 method => "undelete_biblio_record_entry",
200 api_name => "open-ils.cat.biblio.record_entry.undelete",
202 Un-deletes a record and sets active=true
203 @param auth The authtoken
204 @param record The record_id to ressurect
205 @return 1 on success, Event on error.
208 sub undelete_biblio_record_entry {
209 my($self, $conn, $auth, $record_id) = @_;
210 my $e = new_editor(authtoken=>$auth, xact=>1);
211 return $e->die_event unless $e->checkauth;
212 return $e->die_event unless $e->allowed('UPDATE_RECORD');
214 my $record = $e->retrieve_biblio_record_entry($record_id)
215 or return $e->die_event;
216 $record->deleted('f');
217 $record->active('t');
219 # Set the leader/05 to indicate that the record has been corrected/revised
220 my $marc = $record->marc();
221 $marc =~ s{(<leader>.{5}).}{$1c};
222 $record->marc($marc);
224 # no 2 non-deleted records can have the same tcn_value
225 my $existing = $e->search_biblio_record_entry(
227 tcn_value => $record->tcn_value,
228 id => {'!=' => $record_id}
230 return OpenILS::Event->new('TCN_EXISTS') if @$existing;
232 $e->update_biblio_record_entry($record) or return $e->die_event;
238 __PACKAGE__->register_method(
239 method => "biblio_record_xml_import",
240 api_name => "open-ils.cat.biblio.record.xml.import.override",
241 signature => q/@see open-ils.cat.biblio.record.xml.import/);
243 __PACKAGE__->register_method(
244 method => "biblio_record_xml_import",
245 api_name => "open-ils.cat.biblio.record.xml.import",
246 notes => <<" NOTES");
247 Takes a marcxml record and imports the record into the database. In this
248 case, the marcxml record is assumed to be a complete record (i.e. valid
249 MARC). The title control number is taken from (whichever comes first)
250 tags 001, 039[ab], 020a, 022a, 010, 035a and whichever does not already exist
252 user_session must have IMPORT_MARC permissions
256 sub biblio_record_xml_import {
257 my( $self, $client, $authtoken, $xml, $source, $auto_tcn) = @_;
258 my $e = new_editor(xact=>1, authtoken=>$authtoken);
259 return $e->die_event unless $e->checkauth;
260 return $e->die_event unless $e->allowed('IMPORT_MARC', $e->requestor->ws_ou);
262 my $override = $self->api_name =~ /override/;
263 my $record = OpenILS::Application::Cat::BibCommon->biblio_record_xml_import(
264 $e, $xml, $source, $auto_tcn, $override);
266 return $record if $U->event_code($record);
270 #my $ses = OpenSRF::AppSession->create('open-ils.ingest');
271 #$ses->request('open-ils.ingest.full.biblio.record', $record->id);
276 __PACKAGE__->register_method(
277 method => "biblio_record_record_metadata",
278 api_name => "open-ils.cat.biblio.record.metadata.retrieve",
280 argc => 1, #(session_id, biblio_tree )
281 notes => "Walks the tree and commits any changed nodes " .
282 "adds any new nodes, and deletes any deleted nodes",
285 sub biblio_record_record_metadata {
286 my( $self, $client, $authtoken, $ids ) = @_;
288 return [] unless $ids and @$ids;
290 my $editor = new_editor(authtoken => $authtoken);
291 return $editor->event unless $editor->checkauth;
292 return $editor->event unless $editor->allowed('VIEW_USER');
297 return $editor->event unless
298 my $rec = $editor->retrieve_biblio_record_entry($_);
299 $rec->creator($editor->retrieve_actor_user($rec->creator));
300 $rec->editor($editor->retrieve_actor_user($rec->editor));
301 $rec->clear_marc; # slim the record down
302 push( @results, $rec );
310 __PACKAGE__->register_method(
311 method => "biblio_record_marc_cn",
312 api_name => "open-ils.cat.biblio.record.marc_cn.retrieve",
313 argc => 1, #(bib id )
316 sub biblio_record_marc_cn {
317 my( $self, $client, $id ) = @_;
319 my $session = OpenSRF::AppSession->create("open-ils.cstore");
321 ->request("open-ils.cstore.direct.biblio.record_entry.retrieve", $id )
325 my $doc = XML::LibXML->new->parse_string($marc);
326 $doc->documentElement->setNamespace( "http://www.loc.gov/MARC21/slim", "marc", 1 );
329 for my $tag ( qw/050 055 060 070 080 082 086 088 090 092 096 098 099/ ) {
330 my @node = $doc->findnodes("//marc:datafield[\@tag='$tag']");
332 my $cn = $x->findvalue("marc:subfield[\@code='a' or \@code='b']");
333 push @res, {$tag => $cn} if ($cn);
340 __PACKAGE__->register_method(
341 method => 'autogen_barcodes',
342 api_name => "open-ils.cat.item.barcode.autogen",
344 desc => 'Returns N generated barcodes following a specified barcode.',
346 {desc => 'Authentication token', type => 'string'},
347 {desc => 'Barcode which the sequence should follow from', type => 'string'},
348 {desc => 'Number of barcodes to generate', type => 'number'},
349 {desc => 'Options hash. Currently you can pass in checkdigit : false to disable the use of checkdigits.'}
351 return => {desc => 'Array of generated barcodes'}
355 sub autogen_barcodes {
356 my( $self, $client, $auth, $barcode, $num_of_barcodes, $options ) = @_;
357 my $e = new_editor(authtoken => $auth);
358 return $e->event unless $e->checkauth;
359 return $e->event unless $e->allowed('UPDATE_COPY', $e->requestor->ws_ou);
363 for (my $i = 1; $i <= $num_of_barcodes; $i++) {
364 # default is to use checkdigits, so looking for an explicit false here
365 if (defined $$options{'checkdigit'} && ! $$options{'checkdigit'}) {
366 push @res, $barcode + $i;
368 if ($barcode !~ /^\d{13,14}$/) {
369 push @res, $barcode + $i;
371 push @res, add_codabar_checkdigit($barcode + $i*10);
378 # Codabar doesn't define a checkdigit algorithm, but this one is typically used by libraries. gmcharlt++
379 sub add_codabar_checkdigit {
382 return $barcode if $barcode !~ /^\d{13,14}$/;
383 $barcode = substr($barcode, 0, 13); # ignore 14th digit
384 my @digits = split //, $barcode;
386 $total += $digits[$_] foreach (1, 3, 5, 7, 9, 11);
387 $total += (2 * $digits[$_] >= 10) ? (2 * $digits[$_] - 9) : (2 * $digits[$_]) foreach (0, 2, 4, 6, 8, 10, 12);
388 my $remainder = $total % 10;
389 my $checkdigit = ($remainder == 0) ? $remainder : 10 - $remainder;
390 return $barcode . $checkdigit;
393 __PACKAGE__->register_method(
394 method => "orgs_for_title",
396 api_name => "open-ils.cat.actor.org_unit.retrieve_by_title"
400 my( $self, $client, $record_id ) = @_;
402 my $vols = $U->simple_scalar_request(
404 "open-ils.cstore.direct.asset.call_number.search.atomic",
405 { record => $record_id, deleted => 'f' });
407 my $orgs = { map {$_->owning_lib => 1 } @$vols };
408 return [ keys %$orgs ];
412 __PACKAGE__->register_method(
413 method => "retrieve_copies",
415 api_name => "open-ils.cat.asset.copy_tree.retrieve");
417 __PACKAGE__->register_method(
418 method => "retrieve_copies",
419 api_name => "open-ils.cat.asset.copy_tree.global.retrieve");
421 # user_session may be null/undef
422 sub retrieve_copies {
424 my( $self, $client, $user_session, $docid, @org_ids ) = @_;
426 if(ref($org_ids[0])) { @org_ids = @{$org_ids[0]}; }
430 # grabbing copy trees should be available for everyone..
431 if(!@org_ids and $user_session) {
432 my($user_obj, $evt) = OpenILS::Application::AppUtils->checkses($user_session);
434 @org_ids = ($user_obj->home_ou);
437 if( $self->api_name =~ /global/ ) {
438 return _build_volume_list( { record => $docid, deleted => 'f', label => { '<>' => '##URI##' } } );
443 for my $orgid (@org_ids) {
444 my $vols = _build_volume_list(
445 { record => $docid, owning_lib => $orgid, deleted => 'f', label => { '<>' => '##URI##' } } );
446 push( @all_vols, @$vols );
456 sub _build_volume_list {
457 my $search_hash = shift;
459 $search_hash->{deleted} = 'f';
460 my $e = new_editor();
462 my $vols = $e->search_asset_call_number($search_hash);
466 for my $volume (@$vols) {
468 my $copies = $e->search_asset_copy(
469 { call_number => $volume->id , deleted => 'f' });
471 $copies = [ sort { $a->barcode cmp $b->barcode } @$copies ];
473 for my $c (@$copies) {
474 if( $c->status == OILS_COPY_STATUS_CHECKED_OUT ) {
476 $e->search_action_circulation(
478 { target_copy => $c->id },
480 order_by => { circ => 'xact_start desc' },
489 $volume->copies($copies);
490 push( @volumes, $volume );
493 #$session->disconnect();
499 __PACKAGE__->register_method(
500 method => "fleshed_copy_update",
501 api_name => "open-ils.cat.asset.copy.fleshed.batch.update",);
503 __PACKAGE__->register_method(
504 method => "fleshed_copy_update",
505 api_name => "open-ils.cat.asset.copy.fleshed.batch.update.override",);
508 sub fleshed_copy_update {
509 my( $self, $conn, $auth, $copies, $delete_stats ) = @_;
510 return 1 unless ref $copies;
511 my( $reqr, $evt ) = $U->checkses($auth);
513 my $editor = new_editor(requestor => $reqr, xact => 1);
514 my $override = $self->api_name =~ /override/;
515 my $retarget_holds = [];
516 $evt = OpenILS::Application::Cat::AssetCommon->update_fleshed_copies(
517 $editor, $override, undef, $copies, $delete_stats, $retarget_holds, undef);
520 $logger->info("fleshed copy update failed with event: ".OpenSRF::Utils::JSON->perl2JSON($evt));
526 $logger->info("fleshed copy update successfully updated ".scalar(@$copies)." copies");
527 reset_hold_list($auth, $retarget_holds);
532 sub reset_hold_list {
533 my($auth, $hold_ids) = @_;
534 return unless @$hold_ids;
535 $logger->info("reseting holds after copy status change: @$hold_ids");
536 my $ses = OpenSRF::AppSession->create('open-ils.circ');
537 $ses->request('open-ils.circ.hold.reset.batch', $auth, $hold_ids);
541 __PACKAGE__->register_method(
542 method => 'in_db_merge',
543 api_name => 'open-ils.cat.biblio.records.merge',
545 Merges a group of records
546 @param auth The login session key
547 @param master The id of the record all other records should be merged into
548 @param records Array of records to be merged into the master record
549 @return 1 on success, Event on error.
554 my( $self, $conn, $auth, $master, $records ) = @_;
556 my $editor = new_editor( authtoken => $auth, xact => 1 );
557 return $editor->die_event unless $editor->checkauth;
558 return $editor->die_event unless $editor->allowed('MERGE_BIB_RECORDS'); # TODO see below about record ownership
561 for my $source ( @$records ) {
562 #XXX we actually /will/ want to check perms for master and sources after record ownership exists
564 # This stored proc (asset.merge_record_assets(target,source)) has the side effects of
565 # moving call_number, title-type (and some volume-type) hold_request and uri-mapping
566 # objects from the source record to the target record, so must be called from within
569 $count += $editor->json_query({
573 transform => 'asset.merge_record_assets',
579 where => { id => $master }
580 })->[0]->{count}; # count of objects moved, of all types
589 __PACKAGE__->register_method(
590 method => "fleshed_volume_update",
591 api_name => "open-ils.cat.asset.volume.fleshed.batch.update",);
593 __PACKAGE__->register_method(
594 method => "fleshed_volume_update",
595 api_name => "open-ils.cat.asset.volume.fleshed.batch.update.override",);
597 sub fleshed_volume_update {
598 my( $self, $conn, $auth, $volumes, $delete_stats, $options ) = @_;
599 my( $reqr, $evt ) = $U->checkses($auth);
603 my $override = ($self->api_name =~ /override/);
604 my $editor = new_editor( requestor => $reqr, xact => 1 );
605 my $retarget_holds = [];
606 my $auto_merge_vols = $options->{auto_merge_vols};
608 for my $vol (@$volumes) {
609 $logger->info("vol-update: investigating volume ".$vol->id);
611 $vol->editor($reqr->id);
612 $vol->edit_date('now');
614 my $copies = $vol->copies;
617 $vol->editor($editor->requestor->id);
618 $vol->edit_date('now');
620 if( $vol->isdeleted ) {
622 $logger->info("vol-update: deleting volume");
623 my $cs = $editor->search_asset_copy(
624 { call_number => $vol->id, deleted => 'f' } );
625 return OpenILS::Event->new(
626 'VOLUME_NOT_EMPTY', payload => $vol->id ) if @$cs;
629 return $editor->event unless
630 $editor->update_asset_call_number($vol);
633 } elsif( $vol->isnew ) {
634 $logger->info("vol-update: creating volume");
635 $evt = OpenILS::Application::Cat::AssetCommon->create_volume( $override, $editor, $vol );
638 } elsif( $vol->ischanged ) {
639 $logger->info("vol-update: update volume");
640 my $resp = update_volume($vol, $editor, ($override or $auto_merge_vols));
641 return $resp->{evt} if $resp->{evt};
642 $vol = $resp->{merge_vol};
645 # now update any attached copies
646 if( $copies and @$copies and !$vol->isdeleted ) {
647 $_->call_number($vol->id) for @$copies;
648 $evt = OpenILS::Application::Cat::AssetCommon->update_fleshed_copies(
649 $editor, $override, $vol, $copies, $delete_stats, $retarget_holds, undef);
655 reset_hold_list($auth, $retarget_holds);
656 return scalar(@$volumes);
663 my $auto_merge = shift;
668 if ( $evt = OpenILS::Application::Cat::AssetCommon->org_cannot_have_vols($editor, $vol->owning_lib) );
670 my $vols = $editor->search_asset_call_number({
671 owning_lib => $vol->owning_lib,
672 record => $vol->record,
673 label => $vol->label,
675 id => {'!=' => $vol->id}
682 # If the auto-merge option is on, merge our updated volume into the existing
683 # volume with the same record + owner + label.
684 ($merge_vol, $evt) = OpenILS::Application::Cat::Merge::merge_volumes($editor, [$vol], $vols->[0]);
685 return {evt => $evt, merge_vol => $merge_vol};
688 return {evt => OpenILS::Event->new('VOLUME_LABEL_EXISTS', payload => $vol->id)};
692 return {evt => $editor->die_event} unless $editor->update_asset_call_number($vol);
698 __PACKAGE__->register_method (
699 method => 'delete_bib_record',
700 api_name => 'open-ils.cat.biblio.record_entry.delete');
702 sub delete_bib_record {
703 my($self, $conn, $auth, $rec_id) = @_;
704 my $e = new_editor(xact=>1, authtoken=>$auth);
705 return $e->die_event unless $e->checkauth;
706 return $e->die_event unless $e->allowed('DELETE_RECORD', $e->requestor->ws_ou);
707 my $vols = $e->search_asset_call_number({record=>$rec_id, deleted=>'f'});
708 return OpenILS::Event->new('RECORD_NOT_EMPTY', payload=>$rec_id) if @$vols;
709 my $evt = OpenILS::Application::Cat::BibCommon->delete_rec($e, $rec_id);
710 if($evt) { $e->rollback; return $evt; }
717 __PACKAGE__->register_method (
718 method => 'batch_volume_transfer',
719 api_name => 'open-ils.cat.asset.volume.batch.transfer',
722 __PACKAGE__->register_method (
723 method => 'batch_volume_transfer',
724 api_name => 'open-ils.cat.asset.volume.batch.transfer.override',
728 sub batch_volume_transfer {
729 my( $self, $conn, $auth, $args ) = @_;
732 my $rec = $$args{docid};
733 my $o_lib = $$args{lib};
734 my $vol_ids = $$args{volumes};
736 my $override = 1 if $self->api_name =~ /override/;
738 $logger->info("merge: transferring volumes to lib=$o_lib and record=$rec");
740 my $e = new_editor(authtoken => $auth, xact =>1);
741 return $e->event unless $e->checkauth;
742 return $e->event unless $e->allowed('UPDATE_VOLUME', $o_lib);
744 my $dorg = $e->retrieve_actor_org_unit($o_lib)
747 my $ou_type = $e->retrieve_actor_org_unit_type($dorg->ou_type)
750 return $evt if ( $evt = OpenILS::Application::Cat::AssetCommon->org_cannot_have_vols($e, $o_lib) );
752 my $vols = $e->batch_retrieve_asset_call_number($vol_ids);
757 for my $vol (@$vols) {
759 # if we've already looked at this volume, go to the next
760 next if !$vol or grep { $vol->id == $_ } @seen;
762 # grab all of the volumes in the list that have
763 # the same label so they can be merged
764 my @all = grep { $_->label eq $vol->label } @$vols;
766 # take note of the fact that we've looked at this set of volumes
767 push( @seen, $_->id ) for @all;
768 push( @rec_ids, $_->record ) for @all;
770 # for each volume, see if there are any copies that have a
771 # remote circ_lib (circ_lib != vol->owning_lib and != $o_lib ).
773 unless( $override ) {
776 $logger->debug("merge: searching for copies with remote circ_lib for volume ".$v->id);
778 call_number => $v->id,
779 circ_lib => { "not in" => [ $o_lib, $v->owning_lib ] },
783 my $copies = $e->search_asset_copy($args, {idlist=>1});
785 # if the copy's circ_lib matches the destination lib,
787 return OpenILS::Event->new('COPY_REMOTE_CIRC_LIB') if @$copies;
791 # see if there is a volume at the destination lib that
792 # already has the requested label
793 my $existing_vol = $e->search_asset_call_number(
795 label => $vol->label,
802 if( $existing_vol ) {
804 if( grep { $_->id == $existing_vol->id } @all ) {
805 # this volume is already accounted for in our list of volumes to merge
806 $existing_vol = undef;
809 # this volume exists on the destination record/owning_lib and must
810 # be used as the destination for merging
811 $logger->debug("merge: volume already exists at destination record: ".
812 $existing_vol->id.' : '.$existing_vol->label) if $existing_vol;
816 if( @all > 1 || $existing_vol ) {
817 $logger->info("merge: found collisions in volume transfer");
818 my @args = ($e, \@all);
819 @args = ($e, \@all, $existing_vol) if $existing_vol;
820 ($vol, $evt) = OpenILS::Application::Cat::Merge::merge_volumes(@args);
824 if( !$existing_vol ) {
826 $vol->owning_lib($o_lib);
828 $vol->editor($e->requestor->id);
829 $vol->edit_date('now');
831 $logger->info("merge: updating volume ".$vol->id);
832 $e->update_asset_call_number($vol) or return $e->event;
835 $logger->info("merge: bypassing volume update because existing volume used as target");
838 # regardless of what volume was used as the destination,
839 # update any copies that have moved over to the new lib
840 my $copies = $e->search_asset_copy({call_number=>$vol->id, deleted => 'f'});
842 # update circ lib on the copies - make this a method flag?
843 for my $copy (@$copies) {
844 next if $copy->circ_lib == $o_lib;
845 $logger->info("merge: transfer moving circ lib on copy ".$copy->id);
846 $copy->circ_lib($o_lib);
847 $copy->editor($e->requestor->id);
848 $copy->edit_date('now');
849 $e->update_asset_copy($copy) or return $e->event;
852 # Now see if any empty records need to be deleted after all of this
855 $logger->debug("merge: seeing if we should delete record $_...");
856 $evt = OpenILS::Application::Cat::BibCommon->delete_rec($e, $_)
857 if OpenILS::Application::Cat::BibCommon->title_is_empty($e, $_);
862 $logger->info("merge: transfer succeeded");
870 __PACKAGE__->register_method(
871 api_name => 'open-ils.cat.call_number.find_or_create',
872 method => 'find_or_create_volume',
875 sub find_or_create_volume {
876 my( $self, $conn, $auth, $label, $record_id, $org_id ) = @_;
877 my $e = new_editor(authtoken=>$auth, xact=>1);
878 return $e->die_event unless $e->checkauth;
879 my ($vol, $evt, $exists) =
880 OpenILS::Application::Cat::AssetCommon->find_or_create_volume($e, $label, $record_id, $org_id);
882 $e->rollback if $exists;
888 __PACKAGE__->register_method(
889 method => "create_serial_record_xml",
890 api_name => "open-ils.cat.serial.record.xml.create.override",
891 signature => q/@see open-ils.cat.serial.record.xml.create/);
893 __PACKAGE__->register_method(
894 method => "create_serial_record_xml",
895 api_name => "open-ils.cat.serial.record.xml.create",
897 Inserts a new serial record with the given XML
901 sub create_serial_record_xml {
902 my( $self, $client, $login, $source, $owning_lib, $record_id, $xml ) = @_;
904 my $override = 1 if $self->api_name =~ /override/; # not currently used
906 my $e = new_editor(xact=>1, authtoken=>$login);
907 return $e->die_event unless $e->checkauth;
908 return $e->die_event unless $e->allowed('CREATE_MFHD_RECORD', $owning_lib);
910 # Auto-populate the location field of a placeholder MFHD record with the library name
911 my $aou = $e->retrieve_actor_org_unit($owning_lib) or return $e->die_event;
913 my $mfhd = Fieldmapper::serial::record_entry->new;
915 $mfhd->source($source) if $source;
916 $mfhd->record($record_id);
917 $mfhd->creator($e->requestor->id);
918 $mfhd->editor($e->requestor->id);
919 $mfhd->create_date('now');
920 $mfhd->edit_date('now');
921 $mfhd->owning_lib($owning_lib);
923 # If the caller did not pass in MFHD XML, create a placeholder record.
924 # The placeholder will only contain the name of the owning library.
925 # The goal is to generate common patterns for the caller in the UI that
926 # then get passed in here.
928 my $aou_name = $aou->name;
931 xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
932 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
933 xmlns="http://www.loc.gov/MARC21/slim">
934 <leader>00307ny a22001094 4500</leader>
935 <controlfield tag="001">42153</controlfield>
936 <controlfield tag="005">20090601182414.0</controlfield>
937 <controlfield tag="004">$record_id</controlfield>
938 <controlfield tag="008"> 4u####8###l# 4 uueng1 </controlfield>
939 <datafield tag="852" ind1=" " ind2=" "> <subfield code="b">$aou_name</subfield></datafield>
943 my $marcxml = XML::LibXML->new->parse_string($xml);
944 $marcxml->documentElement->setNamespace("http://www.loc.gov/MARC21/slim", "marc", 1 );
945 $marcxml->documentElement->setNamespace("http://www.loc.gov/MARC21/slim");
947 $mfhd->marc($U->entityize($marcxml->documentElement->toString));
949 $e->create_serial_record_entry($mfhd) or return $e->die_event;