]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/WWW/Exporter.pm
git-svn-id: svn://svn.open-ils.org/ILS/trunk@7600 dcc99617-32d9-48b4-a31d-7c20da2025e4
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / WWW / Exporter.pm
1 package OpenILS::WWW::Exporter;
2 use strict;
3 use warnings;
4 use bytes;
5
6 use Apache2::Log;
7 use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND :log);
8 use APR::Const    -compile => qw(:error SUCCESS);
9 use Apache2::RequestRec ();
10 use Apache2::RequestIO ();
11 use Apache2::RequestUtil;
12 use CGI;
13 use Data::Dumper;
14
15 use OpenSRF::EX qw(:try);
16 use OpenSRF::Utils qw/:datetime/;
17 use OpenSRF::Utils::Cache;
18 use OpenSRF::System;
19 use OpenSRF::AppSession;
20 use XML::LibXML;
21 use XML::LibXSLT;
22
23 use Encode;
24 use Unicode::Normalize;
25 use OpenILS::Utils::Fieldmapper;
26 use OpenSRF::Utils::Logger qw/$logger/;
27
28 use MARC::Record;
29 use MARC::File::XML;
30
31 use UNIVERSAL::require;
32
33 our @formats = qw/USMARC UNIMARC XML BRE/;
34
35 # set the bootstrap config and template include directory when
36 # this module is loaded
37 my $bootstrap;
38
39 sub import {
40         my $self = shift;
41         $bootstrap = shift;
42 }
43
44
45 sub child_init {
46         OpenSRF::System->bootstrap_client( config_file => $bootstrap );
47 }
48
49 sub handler {
50         my $r = shift;
51         my $cgi = new CGI;
52         
53         my @records = $cgi->param('id');
54
55         return 200 unless (@records);
56
57         my $type = $cgi->param('rectype') || 'biblio';
58         if ($type ne 'biblio' && $type ne 'authority') {
59                 die "Bad record type: $type";
60         }
61
62         my $tcn_v = 'tcn_value';
63         my $tcn_s = 'tcn_source';
64
65         if ($type eq 'authority') {
66                 $tcn_v = 'arn_value';
67                 $tcn_s = 'arn_source';
68         }
69
70         my $holdings = $cgi->param('holdings') if ($type eq 'biblio');
71         my $location = $cgi->param('location') || 'gaaagpl'; # just because...
72
73         my $format = $cgi->param('format') || 'USMARC';
74         $format = uc($format);
75
76         my $encoding = $cgi->param('encoding') || 'UTF-8';
77         $encoding = uc($encoding);
78
79         binmode(STDOUT, ':raw') if ($encoding ne 'UTF-8');
80         binmode(STDOUT, ':utf8') if ($encoding eq 'UTF-8');
81
82         if (!grep { uc($format) eq $_ } @formats) {
83                 die     "Please select a supported format.  ".
84                         "Right now that means one of [".
85                         join('|',@formats). "]\n";
86         }
87
88         if ($format ne 'XML') {
89                 my $ftype = 'MARC::File::' . $format;
90                 $ftype->require;
91         }
92
93         my $ses = OpenSRF::AppSession->create('open-ils.cstore');
94
95         $r->content_type('application/octet-stream') if (uc($format) ne 'XML');
96         $r->content_type('application/xml') if (uc($format) eq 'XML');
97
98         my $content = '';
99
100
101         $content .= <<" HEADER" if (uc($format) eq 'XML');
102 <?xml version="1.0" encoding="$encoding"?>
103 <collection xmlns='http://www.loc.gov/MARC21/slim'>
104         HEADER
105
106         my %orgs;
107         my %shelves;
108
109         my $flesh = {};
110         if ($holdings) {
111
112                 my $r = $ses->request( 'open-ils.cstore.direct.actor.org_unit.search', { id => { '!=' => undef } } );
113
114                 while (my $o = $r->recv) {
115                         die $r->failed->stringify if ($r->failed);
116                         $o = $o->content;
117                         last unless ($o);
118                         $orgs{$o->id} = $o;
119                 }
120                 $r->finish;
121
122                 $r = $ses->request( 'open-ils.cstore.direct.asset.copy_location.search', { id => { '!=' => undef } } );
123
124                 while (my $s = $r->recv) {
125                         die $r->failed->stringify if ($r->failed);
126                         $s = $s->content;
127                         last unless ($s);
128                         $shelves{$s->id} = $s;
129                 }
130                 $r->finish;
131
132                 $flesh = { flesh => 2, flesh_fields => { bre => [ 'call_numbers' ], acn => [ 'copies' ] } };
133         }
134
135         for my $i ( @records ) {
136                 my $bib;
137                 try {
138                         local $SIG{ALRM} = sub { die "TIMEOUT\n" };
139                         alarm(1);
140                         $bib = $ses->request( "open-ils.cstore.direct.$type.record_entry.retrieve", $i, $flesh )->gather(1);
141                         alarm(0);
142                 } otherwise {
143                         warn "\n!!!!!! Timed out trying to read record $i\n";
144                 };
145                 alarm(0);
146
147                 next unless $bib;
148
149                 if (uc($format) eq 'BRE') {
150                         $content .= OpenSRF::Utils::JSON->perl2JSON($bib);
151                         next;
152                 }
153
154                 try {
155
156                         my $r = MARC::Record->new_from_xml( $bib->marc, $encoding, $format );
157                         $r->delete_field( $_ ) for ($r->field(901));
158
159                         $r->append_fields(
160                                 MARC::Field->new(
161                                         901, '', '', 
162                                         a => $bib->$tcn_v,
163                                         b => $bib->$tcn_s,
164                                         c => $bib->id
165                                 )
166                         );
167
168
169                         if ($holdings) {
170                                 my $cn_list = $bib->call_numbers;
171                                 if ($cn_list && @$cn_list) {
172
173                                         my $cp_list = [ map { @{ $_->copies } } @$cn_list ];
174                                         if ($cp_list && @$cp_list) {
175
176                                                 my %cn_map;
177                                                 push @{$cn_map{$_->call_number}}, $_ for (@$cp_list);
178                                         
179                                                 for my $cn ( @$cn_list ) {
180                                                         my $cn_map_list = $cn_map{$cn->id};
181         
182                                                         for my $cp ( @$cn_map_list ) {
183                                         
184                                                                 $r->append_fields(
185                                                                         MARC::Field->new(
186                                                                                 852, '4', '', 
187                                                                                 a => $location,
188                                                                                 b => $orgs{$cn->owning_lib}->shortname,
189                                                                                 b => $orgs{$cp->circ_lib}->shortname,
190                                                                                 c => $shelves{$cp->location}->name,
191                                                                                 j => $cn->label,
192                                                                                 ($cp->circ_modifier ? ( g => $cp->circ_modifier ) : ()),
193                                                                                 p => $cp->barcode,
194                                                                                 ($cp->price ? ( y => $cp->price ) : ()),
195                                                                                 ($cp->copy_number ? ( t => $cp->copy_number ) : ()),
196                                                                                 ($cp->ref eq 't' ? ( x => 'reference' ) : ()),
197                                                                                 ($cp->holdable eq 'f' ? ( x => 'unholdable' ) : ()),
198                                                                                 ($cp->circulate eq 'f' ? ( x => 'noncirculating' ) : ()),
199                                                                                 ($cp->opac_visible eq 'f' ? ( x => 'hidden' ) : ()),
200                                                                         )
201                                                                 );
202
203                                                         }
204                                                 }
205                                         }
206                                 }
207                         }
208
209                         if (uc($format) eq 'XML') {
210                                 my $x = $r->as_xml_record;
211                                 $x =~ s/^<\?xml version="1.0" encoding="UTF-8"\?>//o;
212                                 $content .= $x;
213                         } elsif (uc($format) eq 'UNIMARC') {
214                                 $content .= $r->as_unimarc
215                         } elsif (uc($format) eq 'USMARC') {
216                                 $content .= $r->as_usmarc
217                         }
218
219                 } otherwise {
220                         my $e = shift;
221                         warn "\n$e\n";
222                 };
223
224         }
225
226         $content .= "</collection>\n" if ($format eq 'XML');
227
228         $r->print($content);
229         return Apache2::Const::OK;
230
231 }
232
233 1;