From 1fdad985f35467f2e7ffd3f2e2c3f7770c0762c3 Mon Sep 17 00:00:00 2001 From: Chris Cormack Date: Thu, 6 Mar 2014 10:46:51 +1300 Subject: [PATCH] Continuing with the CheckInItem handler Refactoring a bit of common code to the base Handler class --- lib/NCIP/Handler.pm | 22 ++++++++++++++++------ lib/NCIP/Handler/AcceptItem.pm | 3 +-- lib/NCIP/Handler/CheckInItem.pm | 6 +++--- lib/NCIP/Handler/LookupUser.pm | 3 +-- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/NCIP/Handler.pm b/lib/NCIP/Handler.pm index df25530..7f58b1b 100644 --- a/lib/NCIP/Handler.pm +++ b/lib/NCIP/Handler.pm @@ -75,6 +75,19 @@ sub new { return $self; } +=head2 xpc() + + Give back an XPathContext Object, registered to the correct namespace + +=cut + +sub xpc { + my $self = shift; + my $xpc = XML::LibXML::XPathContext->new; + $xpc->registerNs( 'ns', $self->namespace() ); + return $xpc; +} + =head2 get_user_elements($xml) When passed an xml dom, this will find the user elements and pass convert them into an arrayref @@ -84,8 +97,7 @@ sub new { sub get_user_elements { my $self = shift; my $xmldoc = shift; - my $xpc = XML::LibXML::XPathContext->new; - $xpc->registerNs( 'ns', $self->namespace() ); + my $xpc = $self->xpc(); my $root = $xmldoc->documentElement(); my @elements = @@ -104,10 +116,8 @@ sub get_agencies { my $root = $xmldoc->documentElement(); - my $from = - $xpc->find( '//ns:FromAgencyId', $root ); - my $to = - $xpc->find( '//ns:ToAgencyId', $root ); + my $from = $xpc->find( '//ns:FromAgencyId', $root ); + my $to = $xpc->find( '//ns:ToAgencyId', $root ); return ( $from, $to ); } diff --git a/lib/NCIP/Handler/AcceptItem.pm b/lib/NCIP/Handler/AcceptItem.pm index dc39b5f..f744f4e 100644 --- a/lib/NCIP/Handler/AcceptItem.pm +++ b/lib/NCIP/Handler/AcceptItem.pm @@ -24,8 +24,7 @@ sub handle { my $xmldoc = shift; if ($xmldoc) { my $root = $xmldoc->documentElement(); - my $xpc = XML::LibXML::XPathContext->new; - $xpc->registerNs( 'ns', $self->namespace() ); + my $xpc = $self->xpc(); my $itemid = $xpc->findnodes( '//ns:ItemId', $root ); # checkin the item diff --git a/lib/NCIP/Handler/CheckInItem.pm b/lib/NCIP/Handler/CheckInItem.pm index 5a759fd..a71a7b1 100644 --- a/lib/NCIP/Handler/CheckInItem.pm +++ b/lib/NCIP/Handler/CheckInItem.pm @@ -24,9 +24,9 @@ sub handle { my $xmldoc = shift; if ($xmldoc) { my $root = $xmldoc->documentElement(); + my $xpc = $self->xpc(); my $itemid = - $root->findnodes('CheckInItem/UniqueItemId/ItemIdentifierValue'); - my @elements = $root->findnodes('CheckInItem/ItemElementType/Value'); + $xpc->findnodes('//ns:ItemId',$root); # checkin the item my $branch='AS'; # where the hell do we get this from??? @@ -43,7 +43,7 @@ sub handle { } else { - $vars->{'elements'} = \@elements; + $vars->{'elements'} = $self->get_user_elements($xmldoc); $vars->{'checkin'} = $checkin; $output = $self->render_output( 'response.tt', $vars ); } diff --git a/lib/NCIP/Handler/LookupUser.pm b/lib/NCIP/Handler/LookupUser.pm index 12285e7..94440fe 100644 --- a/lib/NCIP/Handler/LookupUser.pm +++ b/lib/NCIP/Handler/LookupUser.pm @@ -29,8 +29,7 @@ sub handle { my ($user_id) = $xmldoc->getElementsByTagNameNS( $self->namespace(), 'UserIdentifierValue' ); - my $xpc = XML::LibXML::XPathContext->new; - $xpc->registerNs( 'ns', $self->namespace() ); + my $xpc = $self->xpc(); unless ($user_id) { # We may get a password, username combo instead of userid -- 2.11.0