Net/Z3950/FOLIO/Record.pm untested

filter: Statementstmt 0.0% 0 / 96 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 158 CC 13 SCAR 29.3
CC
13
Cov
0%
_sanitizeTree
56.0
marcRecord
30.0
holdings
6.0
CRAP 18.7
1 package Net::Z3950::FOLIO::Record;
2  
3 use strict;
4 use warnings;
5  
6 use Scalar::Util qw(blessed reftype);
7 use XML::Simple;
8  
9 use Net::Z3950::FOLIO::HoldingsRecords qw(makeHoldingsRecords);
10 use Net::Z3950::FOLIO::MARCHoldings qw(insertMARCHoldings);
11 use Net::Z3950::FOLIO::PostProcess::MARC qw(postProcessMARCRecord);
12  
13  
14 sub new {
15 my $class = shift();
16 my($rs, $offset, $json) = @_;
17  
18 return bless {
19 rs => $rs, # back-reference
20 offset => $offset, # zero-based position within rs
21 json => $json,
22 holdingsStructure => undef,
23 processed => 0,
24 }, $class;
25 }
26  
27 sub id {
28 my $this = shift();
29 my $id = $this->{json}->{id};
30 return $id;
31 }
32  
33 sub rs {
34 my $this = shift();
35 return $this->{rs};
36 }
37  
38 sub jsonStructure {
39 my $this = shift();
40 return $this->{json};
41 }
42  
43 sub prettyJSON {
44 my $this = shift();
45 return _formatJSON($this->{json});
46 }
47  
48 sub prettyXML {
49 my $this = shift();
50 return _formatXML($this->{json});
51 }
52  
53 sub holdings {
54 my $this = shift();
55 my($marc) = @_;
56  
57 if (!$this->{holdingsStructure}) {
58 $this->{holdingsStructure} = makeHoldingsRecords($this, $marc);
59 }
60  
61 return $this->{holdingsStructure};
62 }
63  
64 sub _marc2folioId {
65 my $marc = shift;
66  
67 my @fields999 = $marc->field(999);
68 foreach my $field999 (@fields999) {
69 my $folioId = $field999->subfield('i');
70 return $folioId if $folioId;
71 }
72 return undef;
73 }
74  
75 sub marcRecord {
76 my $this = shift();
77 my $rs = $this->{rs};
78 my $session = $rs->session();
79 my $marc = $this->{marc};
80  
81 if (!defined $marc) {
82 # Fetch a chunk of records that contains the requested one.
83 my $chunkSize = $session->{cfg}->{chunkSize} || 10;
84 my $chunk = int($this->{offset} / $chunkSize);
85 my @marcRecords = $session->_getSRSRecords($rs, $chunk * $chunkSize, $chunkSize);
86 for (my $i = 0; $i < @marcRecords; $i++) {
87 my $marc = $marcRecords[$i];
88 my $id = _marc2folioId($marc);
89 Net::Z3950::FOLIO::_throw(1, "can't find FOLIO ID in MARC record", undef, 1) if !defined $id;
90 my $rec = $rs->recordById($id);
91 $rec->{marc} = $marcRecords[$i];
92 }
93  
94 $marc = $this->{marc};
95 Net::Z3950::FOLIO::_throw(1, "missing MARC record", undef, 1) if !defined $marc;
96 }
97  
98 if (!$this->{processed}) {
99 insertMARCHoldings($this, $marc, $session->{cfg}, $rs->barcode());
100 $marc = postProcessMARCRecord(($session->{cfg}->{postProcessing} || {})->{marc}, $marc);
101 $this->{marc} = $marc;
102 $this->{processed} = 1;
103 }
104  
105 return $marc;
106 }
107  
108  
109 # ----------------------------------------------------------------------------
110  
111 sub _formatJSON {
112 my($obj) = @_;
113  
114 my $coder = Cpanel::JSON::XS->new->ascii->pretty->allow_blessed->space_before(0)->indent_length(2)->sort_by;
115 return $coder->encode($obj);
116 }
117  
118 sub _formatXML {
119 my($json) = @_;
120  
121 my $xml;
122 {
123 # Sanitize output to remove JSON::PP::Boolean values, which XMLout can't handle
124 _sanitizeTree($json);
125  
126 # I have no idea why this generates an "uninitialized value" warning
127 local $SIG{__WARN__} = sub {};
128 $xml = XMLout($json, NoAttr => 1);
129 }
130 $xml =~ s/<@/<__/;
131 $xml =~ s/<\/@/<\/__/;
132 return $xml;
133 }
134  
135 # This code modified from https://www.perlmonks.org/?node_id=773738
136 sub _sanitizeTree {
137 for my $node (@_) {
138 if (!defined($node)) {
139 next;
140 } elsif (ref($node) eq 'JSON::PP::Boolean') {
141 $node += 0;
142 } elsif (blessed($node)) {
143 use Data::Dumper;
144 die('_sanitizeTree: unexpected ', ref($node), ' object: ', Dumper($node));
145 } elsif (reftype($node)) {
146 if (ref($node) eq 'ARRAY') {
147 _sanitizeTree(@$node);
148 } elsif (ref($node) eq 'HASH') {
149 _sanitizeTree(values(%$node));
150 } else {
151 die('_sanitizeTree: unexpected reference type');
152 }
153 }
154 }
155 }
156  
157  
158 1;