From eb01f95140453af32bded9d6b07f2bc72d869f4a Mon Sep 17 00:00:00 2001 From: Jason Stephenson Date: Wed, 18 Feb 2015 11:34:53 -0500 Subject: [PATCH] LP#957466: A simpler version of set_marc_905u. This version only adds a 905$u if none already exist in the document. It does so by creating a new 905 tag with subfield u. It does not add on to any possibly existing 905 tag that does not have a subfield u. Signed-off-by: Jason Stephenson --- .../perlmods/lib/OpenILS/Application/AppUtils.pm | 45 +++++++--------------- 1 file changed, 13 insertions(+), 32 deletions(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm index 8de64a5467..ff018d0257 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm @@ -2138,41 +2138,22 @@ sub strip_marc_fields { sub set_marc_905u { my ($class, $marcdoc, $username) = @_; - # We add to the $parentNode, if set. - my $parentNode; - # Look for existing 905$u subfields and remove them: - for my $node ($marcdoc->findnodes('//field[@tag="905"]/subfield[@code="u"]')) { - $parentNode = $node->parentNode(); - $parentNode->removeChild($node); - # If the 905 has no subfield nodes, remove it, too: - unless ($parentNode->findnodes('child::subfield')) { - $parentNode->parentNode->removeChild($parentNode); - undef($parentNode); - } - } + # Look for existing 905$u subfields. If any exist, do nothing. + my @nodes = $marcdoc->findnodes('//field[@tag="905"]/subfield[@code="u"]'); + unless (@nodes) { + # We create a new 905 and the subfield u to that. + my $parentNode = $marcdoc->createElement('field'); + $parentNode->setAttribute('tag', '905'); + $parentNode->setAttribute('ind1', ''); + $parentNode->setAttribute('ind2', ''); + $marcdoc->documentElement->addChild($parentNode); + my $node = $marcdoc->createElement('subfield'); + $node->setAttribute('code', 'u'); + $node->appendTextNode($username); + $parentNode->addChild($node); - # Check if we deleted any existing 905s or don't have any. - unless ($parentNode) { - # Look for the last one, if any. - my @nodes = $marcdoc->findnodes('(//field[@tag="905"])[last()]'); - if (@nodes) { - $parentNode = $nodes[0]; - } else { - # We have to create a new one. - $parentNode = $marcdoc->createElement('field'); - $parentNode->setAttribute('tag', '905'); - $parentNode->setAttribute('ind1', ''); - $parentNode->setAttribute('ind2', ''); - $marcdoc->documentElement->addChild($parentNode); - } } - # Now we add the subfield u to parentNode. - my $node = $marcdoc->createElement('subfield'); - $node->setAttribute('code', 'u'); - $node->appendTextNode($username); - $parentNode->addChild($node); - return $class->entityize($marcdoc->documentElement->toString); } -- 2.11.0