]> git.evergreen-ils.org Git - working/NCIPServer.git/blob - lib/NCIP/Handler.pm
Fix lookupuser problem reporting in Evergreen driver.
[working/NCIPServer.git] / lib / NCIP / Handler.pm
1 package NCIP::Handler;
2 #
3 #===============================================================================
4 #
5 #         FILE: Hander.pm
6 #
7 #  DESCRIPTION:
8 #
9 #        FILES: ---
10 #         BUGS: ---
11 #        NOTES: ---
12 #       AUTHOR: Chris Cormack (rangi), chrisc@catalyst.net.nz
13 # ORGANIZATION: Koha Development Team
14 #      VERSION: 1.0
15 #      CREATED: 19/09/13 10:43:14
16 #     REVISION: ---
17 #===============================================================================
18
19 # Copyright 2014 Catalyst IT <chrisc@catalyst.net.nz>
20
21 # This file is part of NCIPServer
22 #
23 # NCIPServer is free software; you can redistribute it and/or modify it under the
24 # terms of the GNU General Public License as published by the Free Software
25 # Foundation; either version 2 of the License, or (at your option) any later
26 # version.
27 #
28 # NCIPServer is distributed in the hope that it will be useful, but WITHOUT ANY
29 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
30 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
31 #
32 # You should have received a copy of the GNU General Public License along
33 # with NCIPServer; if not, write to the Free Software Foundation, Inc.,
34 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
35
36 =head1 NAME
37
38     NCIP::Handler
39
40 =head1 SYNOPSIS
41
42     use NCIP::Handler;
43     my $handler = NCIP::Handler->new( { namespace    => $namespace,
44                                         type         => $request_type,
45                                         ils          => $ils,
46                                         template_dir => $templates
47                                        } );
48
49 =head1 FUNCTIONS
50 =cut
51
52 use Modern::Perl;
53 use Object::Tiny qw{ type namespace ils templates };
54 use Module::Load;
55 use Template;
56
57 =head2 new()
58
59     Set up a new handler object, this will actually create one of the request type
60     eg NCIP::Handler::LookupUser
61
62 =cut
63
64 sub new {
65     my $class    = shift;
66     my $params   = shift;
67     my $subclass = __PACKAGE__ . "::" . $params->{type};
68     load $subclass || die "Can't load module $subclass";
69     my $self = bless {
70         type      => $params->{type},
71         namespace => $params->{namespace},
72         ils       => $params->{ils},
73         templates => $params->{template_dir}
74     }, $subclass;
75     return $self;
76 }
77
78 =head2 xpc()
79
80     Give back an XPathContext Object, registered to the correct namespace
81
82 =cut
83
84 sub xpc {
85     my $self = shift;
86     my $xpc  = XML::LibXML::XPathContext->new;
87     $xpc->registerNs( 'ns', $self->namespace() );
88     return $xpc;
89 }
90
91 =head2 get_user_elements($xml)
92
93     When passed an xml dom, this will find the user elements and pass convert them into an arrayref
94
95 =cut
96
97 sub get_user_elements {
98     my $self   = shift;
99     my $xmldoc = shift;
100     my $xpc    = $self->xpc();
101
102     my $root = $xmldoc->documentElement();
103     my @elements =
104       $xpc->findnodes( '//ns:LookupUser/UserElementType/Value', $root );
105     unless ( $elements[0] ) {
106         @elements = $xpc->findnodes( '//ns:UserElementType', $root );
107     }
108     return \@elements;
109 }
110
111 sub get_agencies {
112     my $self   = shift;
113     my $xmldoc = shift;
114     my $xpc    = XML::LibXML::XPathContext->new;
115     $xpc->registerNs( 'ns', $self->namespace() );
116
117     my $root = $xmldoc->documentElement();
118
119     my $from = $xpc->find( '//ns:FromAgencyId', $root );
120     my $to   = $xpc->find( '//ns:ToAgencyId',   $root );
121     return ( $from, $to );
122 }
123
124 sub render_output {
125     my $self         = shift;
126     my $templatename = shift;
127
128     my $vars     = shift;
129     my $template = Template->new(
130         {
131             INCLUDE_PATH => $self->templates,
132             POST_CHOMP   => 1
133         }
134     );
135     my $output;
136     $template->process( $templatename, $vars, \$output );
137     return $output;
138 }
139 1;