Continuing with the CheckInItem handler
authorChris Cormack <chrisc@catalyst.net.nz>
Wed, 5 Mar 2014 21:46:51 +0000 (10:46 +1300)
committerChris Cormack <chrisc@catalyst.net.nz>
Wed, 5 Mar 2014 21:46:51 +0000 (10:46 +1300)
Refactoring a bit of common code to the base Handler class

lib/NCIP/Handler.pm
lib/NCIP/Handler/AcceptItem.pm
lib/NCIP/Handler/CheckInItem.pm
lib/NCIP/Handler/LookupUser.pm

index df25530..7f58b1b 100644 (file)
@@ -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 );
 }
 
index dc39b5f..f744f4e 100644 (file)
@@ -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
index 5a759fd..a71a7b1 100644 (file)
@@ -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 );
         }
index 12285e7..94440fe 100644 (file)
@@ -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