]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Serial.pm
Whitespace. gah.
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Search / Serial.pm
1 package OpenILS::Application::Search::Serial;
2 use base qw/OpenILS::Application/;
3 use strict; use warnings;
4
5
6 use OpenSRF::Utils::JSON;
7 use OpenILS::Utils::Fieldmapper;
8 use OpenILS::Utils::MFHDParser;
9 use OpenSRF::Utils::SettingsClient;
10 use OpenILS::Utils::CStoreEditor q/:funcs/;
11 use OpenSRF::Utils::Cache;
12 use Encode;
13
14 use OpenSRF::Utils::Logger qw/:logger/;
15
16 use Data::Dumper;
17
18 use OpenSRF::Utils::JSON;
19
20 use Time::HiRes qw(time);
21 use OpenSRF::EX qw(:try);
22 use Digest::MD5 qw(md5_hex);
23
24 use XML::LibXML;
25 use XML::LibXSLT;
26
27 use OpenILS::Const qw/:const/;
28
29 use OpenILS::Application::AppUtils;
30 my $apputils = "OpenILS::Application::AppUtils";
31 my $U = $apputils;
32
33 my $pfx = "open-ils.search_";
34
35 =over
36
37 =item * mfhd_to_hash
38
39 =back
40
41 Takes an MFHD record ID and returns a hash of holdings statements
42
43 =cut
44
45 sub mfhd_to_hash {
46         my ($self, $client, $id) = @_;
47         
48         my $session = OpenSRF::AppSession->create("open-ils.cstore");
49         my $request = $session->request(
50                         "open-ils.cstore.direct.serial.record_entry.retrieve", $id )->gather(1);
51
52         my $u = OpenILS::Utils::MFHDParser->new();
53         my $mfhd_hash = $u->generate_svr( $request->id, $request->marc, $request->owning_lib );
54
55         $session->disconnect();
56         return $mfhd_hash;
57 }
58
59 __PACKAGE__->register_method(
60         method  => "mfhd_to_hash",
61         api_name        => "open-ils.search.serial.record.mfhd.retrieve",
62         argc            => 1, 
63         note            => "Given a serial record ID, return MFHD holdings"
64 );
65
66 =over
67
68 =item * bib_to_mfhd_hash 
69
70 =back
71
72 Given a bib record ID, returns a hash of holdings statements
73
74 =cut
75
76 # DEFUNCT ?
77 #sub bib_to_mfhd_hash {
78 #       my ($self, $client, $bib) = @_;
79 #       
80 #       my $mfhd_hash;
81 #
82 #       # XXX perhaps this? --miker
83 ##      my $e = OpenILS::Utils::CStoreEditor->new();
84 ##      my $mfhd = $e->search_serial_record_entry({ record => $bib });
85 ##      return $u->generate_svr( $mfhd->[0] ) if (ref $mfhd);
86 ##      return undef;
87 #
88 #       my @mfhd = $U->cstorereq( "open-ils.cstore.json_query.atomic", {
89 #               select  => { sre => 'marc' },
90 #               from    => 'sre',
91 #               where   => { record => $bib, deleted => 'f' },
92 #               distinct => 1
93 #       });
94 #       
95 #       if (!@mfhd or scalar(@mfhd) == 0) {
96 #               return undef;
97 #       }
98 #
99 #       my $u = OpenILS::Utils::MFHDParser->new();
100 #       $mfhd_hash = $u->generate_svr( $mfhd[0][0]->{id}, $mfhd[0][0]->{marc}, $mfhd[0][0]->{owning_lib} );
101 #
102 #       return $mfhd_hash;
103 #}
104 #
105 #__PACKAGE__->register_method(
106 #       method  => "bib_to_mfhd_hash",
107 #       api_name        => "open-ils.search.serial.record.bib_to_mfhd.retrieve",
108 #       argc            => 1, 
109 #       note            => "Given a bibliographic record ID, return MFHD holdings"
110 #);
111
112 sub bib_to_svr {
113         my ($self, $client, $bib) = @_;
114         
115         my $svrs = [];
116
117         my $e = OpenILS::Utils::CStoreEditor->new();
118     # TODO: 'deleted' ssub support
119     my $sdists = $e->search_serial_distribution([{ "+ssub" => {"record_entry" => $bib} }, { "flesh" => 1, "flesh_fields" => {'sdist' => [ "record_entry", "holding_lib", "basic_summary", "supplement_summary", "index_summary" ]}, "join" => {"ssub" => {}} }]);
120         my $sres = $e->search_serial_record_entry([{ record => $bib, deleted => 'f', "+sdist" => {"id" => undef} }, { "join" => {"sdist" => { 'type' => 'left' }} }]);
121         if (!ref $sres and !ref $sdists) {
122                 return undef;
123         }
124
125         my $mfhd_parser = OpenILS::Utils::MFHDParser->new();
126         foreach (@$sdists) {
127         my $svr;
128         if (ref $_->record_entry) {
129             $svr = $mfhd_parser->generate_svr($_->record_entry->id, $_->record_entry->marc, $_->record_entry->owning_lib);
130         } else {
131             $svr = Fieldmapper::serial::virtual_record->new;
132             $svr->sre_id(-1);
133             $svr->location($_->holding_lib->name);
134             $svr->owning_lib($_->holding_lib);
135             $svr->basic_holdings([]);
136             $svr->supplement_holdings([]);
137             $svr->index_holdings([]);
138             $svr->basic_holdings_add([]);
139             $svr->supplement_holdings_add([]);
140             $svr->index_holdings_add([]);
141             $svr->online([]);
142             $svr->missing([]);
143             $svr->incomplete([]);
144         }
145         if (ref $_->basic_summary) { #TODO: 'show-generated' boolean on summaries
146             if ($_->basic_summary->generated_coverage) {
147                 push(@{$svr->basic_holdings}, $_->basic_summary->generated_coverage);
148             }
149             if ($_->basic_summary->textual_holdings) {
150                 push(@{$svr->basic_holdings_add}, $_->basic_summary->textual_holdings);
151             }
152         }
153         if (ref $_->supplement_summary) {
154             if ($_->supplement_summary->generated_coverage) {
155                 push(@{$svr->supplement_holdings}, $_->supplement_summary->generated_coverage);
156             }
157             if ($_->supplement_summary->textual_holdings) {
158                 push(@{$svr->supplement_holdings_add}, $_->supplement_summary->textual_holdings);
159             }
160         }
161         if (ref $_->index_summary) {
162             if ($_->index_summary->generated_coverage) {
163                 push(@{$svr->index_holdings}, $_->index_summary->generated_coverage);
164             }
165             if ($_->index_summary->textual_holdings) {
166                 push(@{$svr->index_holdings_add}, $_->index_summary->textual_holdings);
167             }
168         }
169         push(@$svrs, $svr);
170         }
171         foreach (@$sres) {
172                 push(@$svrs, $mfhd_parser->generate_svr($_->id, $_->marc, $_->owning_lib));
173         }
174
175     # do a basic location sort for simple predictability
176     @$svrs = sort { $a->location cmp $b->location } @$svrs;
177
178         return $svrs;
179 }
180
181 __PACKAGE__->register_method(
182         method  => "bib_to_svr",
183         api_name        => "open-ils.search.serial.record.bib.retrieve",
184         argc            => 1, 
185         note            => "Given a bibliographic record ID, return holdings in svr form"
186 );
187
188 1;