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