From 79c30654fe76409c63db43d3a72ac0216e5e0f58 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Thu, 28 Feb 2013 15:43:08 -0500 Subject: [PATCH] Strip configured fields during import/overlay II Support on-demand and always-apply strip groups. Move stripping out to shared util function. Specifically, this adds support for optional strip-groups to these APIs: open-ils.cat.biblio.record.xml.create open-ils.cat.biblio.record.marc.replace open-ils.cat.biblio.record.xml.import open-ils.vandelay.bib_record.list.import open-ils.vandelay.bib_queue.import Signed-off-by: Bill Erickson Signed-off-by: Mike Rylander --- .../lib/OpenILS/Application/AppUtils.pm | 38 +++++++++++++++++++ .../perlmods/lib/OpenILS/Application/Cat.pm | 17 +++------ .../lib/OpenILS/Application/Cat/BibCommon.pm | 35 ++++------------- .../lib/OpenILS/Application/Vandelay.pm | 32 +++++++--------- 4 files changed, 65 insertions(+), 57 deletions(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm index b8124f2ec5..87b53ec430 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm @@ -2081,5 +2081,43 @@ sub datecmp { } +# marcdoc is an XML::LibXML document +# updates the doc and returns the entityized MARC string +sub strip_marc_fields { + my ($class, $e, $marcdoc, $grps) = @_; + + my $orgs = $class->get_org_ancestors($e->requestor->ws_ou); + + my $query = { + select => {vibtf => ['field']}, + from => {vibtf => 'vibtg'}, + where => {'+vibtg' => {owner => $orgs}}, + distinct => 1 + }; + + # give me always-apply groups plus any selected groups + if ($grps and @$grps) { + $query->{where}->{'+vibtg'}->{'-or'} = [ + {id => $grps}, + {always_apply => 't'} + ]; + + } else { + $query->{where}->{'+vibtg'}->{always_apply} = 't'; + } + + my $fields = $e->json_query($query); + + for my $field (@$fields) { + my $tag = $field->{field}; + for my $node ($marcdoc->findnodes('//*[@tag="'.$tag.'"]')) { + $node->parentNode->removeChild($node); + } + } + + return $class->entityize($marcdoc->documentElement->toString); +} + + 1; diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat.pm index 8bc2cc95cf..fbf140a0ef 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat.pm @@ -106,7 +106,7 @@ __PACKAGE__->register_method( ); sub create_record_xml { - my( $self, $client, $login, $xml, $source, $oargs ) = @_; + my( $self, $client, $login, $xml, $source, $oargs, $strip_grps ) = @_; my $override = 1 if $self->api_name =~ /override/; $oargs = { all => 1 } unless defined $oargs; @@ -114,8 +114,6 @@ sub create_record_xml { my( $user_obj, $evt ) = $U->checksesperm($login, 'CREATE_MARC'); return $evt if $evt; - $$oargs{import_location} = $e->requestor->ws_ou; - $logger->activity("user ".$user_obj->id." creating new MARC record"); my $meth = $self->method_lookup("open-ils.cat.biblio.record.xml.import"); @@ -123,7 +121,7 @@ sub create_record_xml { $meth = $self->method_lookup( "open-ils.cat.biblio.record.xml.import.override") if $override; - my ($s) = $meth->run($login, $xml, $source, $oargs); + my ($s) = $meth->run($login, $xml, $source, $oargs, $strip_grps); return $s; } @@ -158,7 +156,7 @@ __PACKAGE__->register_method( ); sub biblio_record_replace_marc { - my( $self, $conn, $auth, $recid, $newxml, $source, $oargs ) = @_; + my( $self, $conn, $auth, $recid, $newxml, $source, $oargs, $strip_grps ) = @_; my $e = new_editor(authtoken=>$auth, xact=>1); return $e->die_event unless $e->checkauth; return $e->die_event unless $e->allowed('UPDATE_MARC', $e->requestor->ws_ou); @@ -170,10 +168,8 @@ sub biblio_record_replace_marc { $oargs = {}; } - $$oargs{import_location} = $e->requestor->ws_ou; - my $res = OpenILS::Application::Cat::BibCommon->biblio_record_replace_marc( - $e, $recid, $newxml, $source, $fix_tcn, $oargs); + $e, $recid, $newxml, $source, $fix_tcn, $oargs, $strip_grps); $e->commit unless $U->event_code($res); @@ -413,7 +409,7 @@ __PACKAGE__->register_method( sub biblio_record_xml_import { - my( $self, $client, $authtoken, $xml, $source, $auto_tcn, $oargs) = @_; + my( $self, $client, $authtoken, $xml, $source, $auto_tcn, $oargs, $strip_grps) = @_; my $e = new_editor(xact=>1, authtoken=>$authtoken); return $e->die_event unless $e->checkauth; return $e->die_event unless $e->allowed('IMPORT_MARC', $e->requestor->ws_ou); @@ -423,9 +419,8 @@ sub biblio_record_xml_import { } else { $oargs = {}; } - $$oargs{import_location} = $e->requestor->ws_ou; my $record = OpenILS::Application::Cat::BibCommon->biblio_record_xml_import( - $e, $xml, $source, $auto_tcn, $oargs); + $e, $xml, $source, $auto_tcn, $oargs, $strip_grps); return $record if $U->event_code($record); diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat/BibCommon.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat/BibCommon.pm index babe2e082f..7f7ade73c1 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat/BibCommon.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Cat/BibCommon.pm @@ -36,7 +36,7 @@ sub fetch_bib_sources { sub biblio_record_replace_marc { - my($class, $e, $recid, $newxml, $source, $fixtcn, $override) = @_; + my($class, $e, $recid, $newxml, $source, $fixtcn, $override, $strip_grps) = @_; $override = { all => 1 } if($override && !ref $override); $override = { all => 0 } if(!ref $override); @@ -66,30 +66,19 @@ sub biblio_record_replace_marc { $marcdoc = __make_marc_doc($newxml); } - my $import_loc = $$override{import_location}; - my $ancestors = $U->get_org_ancestors($import_loc) if ($import_loc); - my $trash_tags = $e->search_vandelay_import_bib_trash_fields({owner => $ancestors}) if ($ancestors); - - if ($trash_tags && @$trash_tags) { - for my $tag (@$trash_tags) { - for my $node ($marcdoc->findnodes('//*[@tag="'.$tag->field.'"]')) { - $node->parentNode->removeChild($node); - } - } - } - + my $marc = $U->strip_marc_fields($e, $marcdoc, $strip_grps); $rec->source(bib_source_from_name($source)) if $source; $rec->editor($e->requestor->id); $rec->edit_date('now'); - $rec->marc( $U->entityize( $marcdoc->documentElement->toString ) ); + $rec->marc($marc); $e->update_biblio_record_entry($rec) or return $e->die_event; return $rec; } sub biblio_record_xml_import { - my($class, $e, $xml, $source, $auto_tcn, $override) = @_; + my($class, $e, $xml, $source, $auto_tcn, $override, $strip_grps) = @_; $override = { all => 1 } if($override && !ref $override); $override = { all => 0 } if(!ref $override); @@ -107,24 +96,14 @@ sub biblio_record_xml_import { return $evt if $evt; } - my $import_loc = $$override{import_location}; - my $ancestors = $U->get_org_ancestors($import_loc) if ($import_loc); - my $trash_tags = $e->search_vandelay_import_bib_trash_fields({owner => $ancestors}) if ($ancestors); - - if ($trash_tags && @$trash_tags) { - for my $tag (@$trash_tags) { - for my $node ($marcdoc->findnodes('//*[@tag="'.$tag.'"]')) { - $node->parentNode->removeChild($node); - } - } - } - # Silence warnings when _find_tcn_info() fails $tcn ||= ''; $tcn_source ||= ''; $logger->info("user ".$e->requestor->id. " creating new biblio entry with tcn=$tcn and tcn_source $tcn_source"); + my $marc = $U->strip_marc_fields($e, $marcdoc, $strip_grps); + my $record = Fieldmapper::biblio::record_entry->new; $record->source(bib_source_from_name($source)) if $source; @@ -134,7 +113,7 @@ sub biblio_record_xml_import { $record->editor($e->requestor->id); $record->create_date('now'); $record->edit_date('now'); - $record->marc($U->entityize($marcdoc->documentElement->toString)); + $record->marc($marc); $record = $e->create_biblio_record_entry($record) or return $e->die_event; diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Vandelay.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Vandelay.pm index 776fa776ab..67964478f8 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Vandelay.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Vandelay.pm @@ -886,6 +886,7 @@ sub import_record_list_impl { my $ft_merge_profile = $$args{fall_through_merge_profile}; my $bib_source = $$args{bib_source}; my $import_no_match = $$args{import_no_match}; + my $strip_grps = $$args{strip_field_groups}; # bib-only my $overlay_func = 'vandelay.overlay_bib_record'; my $auto_overlay_func = 'vandelay.auto_overlay_bib_record'; @@ -916,10 +917,6 @@ sub import_record_list_impl { $rec_class = 'vqar'; } - my $import_loc = $requestor->ws_ou; - my $ancestors = $U->get_org_ancestors($import_loc) if ($import_loc); - my $trash_tags = $editor->search_vandelay_import_bib_trash_fields({owner => $ancestors}) if ($ancestors); - my $new_rec_perm_cache; my @success_rec_ids; for my $rec_id (@$rec_ids) { @@ -956,26 +953,25 @@ sub import_record_list_impl { next; } - if ($trash_tags && @$trash_tags) { - my $marcxml = XML::LibXML->new->parse_string($rec->marc); - if ($marcxml) { - for my $tag (@$trash_tags) { - for my $node ($marcxml->findnodes('//*[@tag="'.$tag->field.'"]')) { - $node->parentNode->removeChild($node); - } - } - - $rec->marc( $U->entityize( $marcdoc->documentElement->toString ) ); - $e->$update_func($rec); - } - } - $$report_args{rec} = $rec; $queues{$rec->queue} = 1; my $record; my $imported = 0; + if ($type eq 'bib') { + # strip configured / selected MARC tags from inbound records + + my $marcdoc = XML::LibXML->new->parse_string($rec->marc); + $rec->marc($U->strip_marc_fields($e, $marcdoc, $strip_grps)); + + unless ($e->$update_func($rec)) { + $$report_args{evt} = $e->die_event; + finish_rec_import_attempt($report_args); + next; + } + } + if(defined $overlay_target) { # Caller chose an explicit overlay target -- 2.43.2