unapi/supercat mod_perl handler
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / WWW / SuperCat.pm
1 package OpenILS::WWW::SuperCat;
2 use strict; use warnings;
3
4 use Apache2 ();
5 use Apache2::Log;
6 use Apache2::Const -compile => qw(OK REDIRECT DECLINED :log);
7 use APR::Const    -compile => qw(:error SUCCESS);
8 use Apache2::RequestRec ();
9 use Apache2::RequestIO ();
10 use Apache2::RequestUtil;
11 use CGI;
12 use Data::Dumper;
13
14 use OpenSRF::EX qw(:try);
15 use OpenSRF::System;
16 use OpenSRF::AppSession;
17 use XML::LibXML;
18
19 use OpenILS::Utils::Fieldmapper;
20
21
22 # set the bootstrap config when this module is loaded
23 my ($bootstrap, $supercat);
24
25 sub import {
26         my $self = shift;
27         $bootstrap = shift;
28 }
29
30
31 sub child_init {
32         OpenSRF::System->bootstrap_client( config_file => $bootstrap );
33         $supercat = OpenSRF::AppSession->create('open-ils.supercat');
34 }
35
36 sub handler {
37
38         my $apache = shift;
39         return Apache2::Const::DECLINED if (-e $apache->filename);
40
41         my $path = $apache->path_info;
42
43         my ($id,$type,$format,$command) = reverse split '/', $path;
44
45         print "Content-type: text/xml; charset=utf-8\n\n";
46         
47         if ( $path =~ m{^/?$}o ) {
48                 my $cgi = new CGI;
49
50                 my $uri = $cgi->param('uri') || '';
51
52                 $format = $cgi->param('format');
53                 ($id,$type) = ('','');
54
55                 if (!$format) {
56                         if ($uri =~ m{^oils:/([^\/]+)/(\d+)}o) {
57                                 $id = $2;
58                                 $type = 'record';
59                                 $type = 'metarecord' if ($1 =~ /^m/o);
60
61                                 my $list = $supercat
62                                         ->request("open-ils.supercat.$type.formats")
63                                         ->gather(1);
64
65                                 print '<formats>'.
66                                         join('',
67                                                 map { 
68                                                         "<format><name>$_</name><type>text/xml</type></format>" 
69                                                 } @$list
70                                         ).'</formats>';
71                                 return 300;
72                         } else {
73                                 my $list = $supercat
74                                         ->request("open-ils.supercat.record.formats")
75                                         ->gather(1);
76                                 push @$list,
77                                         @{ $supercat
78                                                 ->request("open-ils.supercat.metarecord.formats")
79                                                 ->gather(1);
80                                         };
81
82                                 my %hash = map { ($_ => $_) } @$list;
83                                 $list = [ sort keys %hash ];
84
85                                 print '<formats>'.
86                                         join('',
87                                                 map { 
88                                                         "<format><name>$_</name><type>text/xml</type></format>" 
89                                                 } @$list
90                                         ).'</formats>';
91                                 return Apache2::Const::OK;
92                         }
93                 }
94
95                 
96                 if ($uri =~ m{^oils:/([^\/]+)/(\d+)}o) {
97                         $id = $2;
98                         $type = 'record';
99                         $type = 'metarecord' if ($1 =~ /^m/o);
100                         $command = 'retrieve';
101                 }
102
103         } elsif ( $path =~ m{^/formats/([^\/]+)$}o ) {
104                 my $list = $supercat
105                         ->request("open-ils.supercat.$1.formats")
106                         ->gather(1);
107
108                 print '<formats>'.
109                         join('',
110                                 map { 
111                                         "<format><name>$_</name><type>text/xml</type></format>" 
112                                 } @$list
113                         ).'</formats>';
114                 return Apache2::Const::OK;
115         }
116
117         print $supercat
118                 ->request("open-ils.supercat.$type.$format.$command",$id)
119                 ->gather(1);
120
121         return Apache2::Const::OK;
122 }
123
124 1;