]> git.evergreen-ils.org Git - working/NCIPServer.git/blob - lib/NCIP.pm
8bc10b5c5dcd7c28721c4461084f00418b4733e4
[working/NCIPServer.git] / lib / NCIP.pm
1 package NCIP;
2 use NCIP::Configuration;
3 use NCIP::Handler;
4 use Modern::Perl;
5 use XML::LibXML;
6 use Try::Tiny;
7 use Module::Load;
8 use Template;
9 use Log::Log4perl;
10
11 use Object::Tiny qw{xmldoc config namespace ils};
12
13 our $VERSION           = '0.01';
14 our $strict_validation = 0;        # move to config file
15
16 =head1 NAME
17   
18     NCIP
19
20 =head1 SYNOPSIS
21
22     use NCIP;
23     my $nicp = NCIP->new($config_dir);
24
25 =head1 FUNCTIONS
26
27 =cut
28
29 sub new {
30     my $proto      = shift;
31     my $class      = ref $proto || $proto;
32     my $config_dir = shift;
33     my $self       = {};
34     my $config     = NCIP::Configuration->new($config_dir);
35     $self->{config}    = $config;
36     $self->{namespace} = $config->('NCIP.namespace.value');
37     Log::Log4perl->init($config_dir . "/log4perl.conf");
38     # load the ILS dependent module
39     my $module = 'NCIP::ILS::' . $config->('NCIP.ils.value');
40     load $module || die "Can not load ILS module $module";
41     my $ils = $module->new( name => $config->('NCIP.ils.value') );
42     $self->{'ils'} = $ils;
43     return bless $self, $class;
44
45 }
46
47 =head2 process_request()
48
49  my $response = $ncip->process_request($xml);
50
51 =cut
52
53 sub process_request {
54     my $self           = shift;
55     my $xml            = shift;
56     my ($request_type) = $self->handle_initiation($xml);
57     unless ($request_type) {
58
59       # We have invalid xml, or we can't figure out what kind of request this is
60       # Handle error here
61 #        warn "We can't find request type";
62         my $output = $self->_error("We can't find request type");
63         return $output;
64     }
65     my $handler = NCIP::Handler->new(
66         {
67             namespace    => $self->namespace(),
68             type         => $request_type,
69             ils          => $self->ils,
70             template_dir => $self->config->('NCIP.templates.value'),
71         }
72     );
73     return $handler->handle( $self->xmldoc );
74 }
75
76 =head2 handle_initiation
77
78 =cut
79
80 sub handle_initiation {
81     my $self = shift;
82     my $xml  = shift;
83     my $dom;
84     my $log = Log::Log4perl->get_logger("NCIP");
85     eval { $dom = XML::LibXML->load_xml( string => $xml ); };
86     if ($@) {
87         $log->info("Invalid xml we can not parse it ");        
88     }
89     if ($dom) {
90
91         # should check validity with validate at this point
92         if ( $strict_validation && !$self->validate($dom) ) {
93
94             # we want strict validation, bail out if dom doesnt validate
95 #            warn " Not valid xml";
96
97             # throw/log error
98             return;
99         }
100         my $request_type = $self->parse_request($dom);
101
102         # do whatever we should do to initiate, then hand back request_type
103         if ($request_type) {
104             $self->{xmldoc} = $dom;
105             return $request_type;
106         }
107     }
108     else {
109         $log->info("We have no DOM");
110
111         return;
112     }
113 }
114
115 sub validate {
116
117     # this should perhaps be in it's own module
118     my $self = shift;
119     my $dom  = shift;
120     try {
121         $dom->validate();
122     }
123     catch {
124         warn "Bad xml, caught error: $_";
125         return;
126     };
127
128     # we could validate against the schema here, might be good?
129     # my $schema = XML::LibXML::Schema->new(string => $schema_str);
130     # eval { $schema->validate($dom); }
131     # perhaps we could check the ncip version and validate that too
132     return 1;
133 }
134
135 sub parse_request {
136     my $self = shift;
137     my $dom  = shift;
138     my $nodes =
139       $dom->getElementsByTagNameNS( $self->namespace(), 'NCIPMessage' );
140     if ($nodes) {
141         my @childnodes = $nodes->[0]->childNodes();
142         if ( $childnodes[1] ) {
143             return $childnodes[1]->localname();
144         }
145         else {
146             warn "Got a node, but no child node";
147             return;
148         }
149     }
150     else {
151         warn "Invalid XML";
152         return;
153     }
154     return;
155 }
156
157 sub _error {
158     my $self         = shift;
159     my $error_detail = shift;
160     my $vars;
161     $vars->{'error_detail'} = $error_detail;
162     $vars->{'messagetype'} = 'ItemRequestedResponse'; # No idea what this type should be 
163     my $template = Template->new(
164         { INCLUDE_PATH => $self->config->('NCIP.templates.value'), } );
165     my $output;
166     $template->process( 'problem.tt', $vars, \$output );
167     return $output;
168 }
169 1;