1 package OpenILS::Utils::MFHDParser;
5 use OpenSRF::EX qw/:try/;
6 use Time::HiRes qw(time);
7 use OpenILS::Utils::Fieldmapper;
8 use OpenSRF::Utils::SettingsClient;
9 use OpenSRF::Utils::Logger qw/$logger/;
11 use OpenILS::Utils::MFHD;
12 use MARC::File::XML (BinaryEncoding => 'utf8');
15 sub new { return bless({}, shift()); }
21 =item * format_textual_holdings($field)
25 Returns concatenated subfields $a with $z for textual holdings (866-868)
29 sub format_textual_holdings {
30 my ($self, $field) = @_;
34 $holdings = $field->subfield('a');
39 $public_note = $field->subfield('z');
41 return "$holdings - $public_note";
48 =item * mfhd_to_hash($mfhd_xml)
52 Returns a Perl hash containing fields of interest from the MFHD record
57 my ($self, $mfhd_xml) = @_;
66 my $current_holdings = [];
67 my $current_supplements = [];
68 my $current_indexes = [];
69 my $online = []; # Laurentian extension to MFHD standard
70 my $missing = []; # Laurentian extension to MFHD standard
71 my $incomplete = []; # Laurentian extension to MFHD standard
74 $marc = MARC::Record->new_from_xml($mfhd_xml);
77 $logger->error("Failed to convert MFHD XML to MARC: " . shift());
78 $logger->error("Failed MFHD XML: $mfhd_xml");
86 $mfhd = MFHD->new($marc);
89 $logger->error("Failed to parse MFHD: " . shift());
90 $logger->error("Failed MFHD XML: $mfhd_xml");
98 foreach my $field ($marc->field('852')) {
99 foreach my $subfield_ref ($field->subfields) {
100 my ($subfield, $data) = @$subfield_ref;
101 $location .= $data . " -- ";
106 $logger->error("MFHD location parsing error: " . shift());
109 $location =~ s/ -- $//;
112 foreach my $field ($marc->field('866')) {
113 my $textual_holdings = $self->format_textual_holdings($field);
114 if ($textual_holdings) {
115 push @$holdings, $textual_holdings;
118 foreach my $field ($marc->field('867')) {
119 my $textual_holdings = $self->format_textual_holdings($field);
120 if ($textual_holdings) {
121 push @$supplements, $textual_holdings;
124 foreach my $field ($marc->field('868')) {
125 my $textual_holdings = $self->format_textual_holdings($field);
126 if ($textual_holdings) {
127 push @$indexes, $textual_holdings;
131 foreach my $cap_id ($mfhd->captions('853')) {
132 my @curr_holdings = $mfhd->holdings('863', $cap_id);
133 next unless scalar @curr_holdings;
134 foreach (@curr_holdings) {
135 push @$current_holdings, $_->format();
139 foreach my $cap_id ($mfhd->captions('854')) {
140 my @curr_supplements = $mfhd->holdings('864', $cap_id);
141 next unless scalar @curr_supplements;
142 foreach (@curr_supplements) {
143 push @$current_supplements, $_->format();
147 foreach my $cap_id ($mfhd->captions('855')) {
148 my @curr_indexes = $mfhd->holdings('865', $cap_id);
149 next unless scalar @curr_indexes;
150 foreach (@curr_indexes) {
151 push @$current_indexes, $_->format();
155 # Laurentian extensions
156 foreach my $field ($marc->field('530')) {
157 my $online_stmt = $self->format_textual_holdings($field);
159 push @$online, $online_stmt;
163 foreach my $field ($marc->field('590')) {
164 my $missing_stmt = $self->format_textual_holdings($field);
166 push @$missing, $missing_stmt;
170 foreach my $field ($marc->field('591')) {
171 my $incomplete_stmt = $self->format_textual_holdings($field);
172 if ($incomplete_stmt) {
173 push @$incomplete, $incomplete_stmt;
178 $logger->error("MFHD statement parsing error: " . shift());
182 location => $location,
183 holdings => $holdings,
184 current_holdings => $current_holdings,
185 supplements => $supplements,
186 current_supplements => $current_supplements,
188 current_indexes => $current_indexes,
190 incomplete => $incomplete,
196 =item * init_holdings_virtual_record()
200 Initialize the serial virtual record (svr) instance
204 sub init_holdings_virtual_record {
205 my $record = Fieldmapper::serial::virtual_record->new;
208 $record->owning_lib();
209 $record->holdings([]);
210 $record->current_holdings([]);
211 $record->supplements([]);
212 $record->current_supplements([]);
213 $record->indexes([]);
214 $record->current_indexes([]);
216 $record->missing([]);
217 $record->incomplete([]);
223 =item * init_holdings_virtual_record($mfhd)
227 Given an MFHD record, return a populated svr instance
232 my ($self, $id, $mfhd, $owning_lib) = @_;
238 my $record = init_holdings_virtual_record();
239 my $holdings = $self->mfhd_to_hash($mfhd);
242 $record->owning_lib($owning_lib);
248 $record->location($holdings->{location});
249 $record->holdings($holdings->{holdings});
250 $record->current_holdings($holdings->{current_holdings});
251 $record->supplements($holdings->{supplements});
252 $record->current_supplements($holdings->{current_supplements});
253 $record->indexes($holdings->{indexes});
254 $record->current_indexes($holdings->{current_indexes});
255 $record->online($holdings->{online});
256 $record->missing($holdings->{missing});
257 $record->incomplete($holdings->{incomplete});
264 # vim: ts=4:sw=4:noet