DBIServer/MARC.pm untested

filter: Statementstmt 0.0% 0 / 33 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 3 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 61 CC 8 SCAR 40.6
CC
8
Cov
0%
format
72.0
BEGIN
2.0
BEGIN
2.0
CRAP 58.0
1 # $Id: MARC.pm,v 1.5 2005-04-11 18:48:20 mike Exp $
2  
3 package Net::Z3950::DBIServer::MARC;
4 use MARC::Record;
5 use strict;
6  
7 =head1 NAME
8  
9 Net::Z3950::DBIServer::MARC - build MARC records for DBIServer
10  
11 =head1 SYNOPSIS
12  
13 $rec = Net::Z3950::DBIServer::MARC::format(
14 { title=>'Mr', forename=>'Eric', surname=>'1/2 Bee' },
15 $config);
16  
17 =head1 DESCRIPTION
18  
19 This non-OO module exists only to provide a single function which
20 formats a set of fields as a MARC record.
21  
22 =head1 METHODS
23  
24 =head2 format()
25  
26 $rec = Net::Z3950::DBIServer::MARC::format($hashref, $config);
27  
28 Creates and returns, as an unblessed string, a new MARC record
29 containing the specified fields according to the configuration
30 specified in the database-and-record-syntax-specific configuration
31 segment I<$config>, of type C<Net::Z3950::DBIServer::Config::XMLSpec>.
32  
33 =cut
34  
35 sub format {
36 my($hashref, $config) = @_;
37  
38 my @fields; # List of fields, in the order
39 # specified in the configuration.
40 my %current; # Maps tags to references into @fields
41 foreach my $field ($config->fields()) {
42 my $sqlField = $field->columnName();
43 my $data = Net::Z3950::DBIServer::XML::make_data($sqlField, $hashref);
44 next if !defined $data || $data eq "";
45  
46 my($tag, $i1, $i2, $subtag) = ($field->tagname(), "", "");
47 if ($tag =~ s/\$(.*)//) {
48 $subtag = $1;
49 }
50 if ($tag =~ s/\/(.*)//) {
51 $i1 = $1;
52 if ($i1 =~ s/\/(.*)//) {
53 $i2 = $1;
54 }
55 }
56  
57 if ($tag =~ /^00/) {
58 # Control fields (no subfields or indicators involved)
59 push @fields, MARC::Field->new($tag, $data);
60 next;
61 }
62  
63 if (!defined $current{$tag} ||
64 defined $current{$tag}->subfield($subtag)) {
65 # Either it's the first time we've has data for this
66 # field, or we've already created this subfield within the
67 # specified field, so we need to create a new field with
68 # the same tag to hold the new subfield.
69 #print "*** creating new field '$tag' with '$subtag'='$data'\n";
70 my $marcfield = MARC::Field->new($tag, $i1, $i2, $subtag => $data);
71 push @fields, $marcfield;
72 $current{$tag} = $marcfield;
73 } else {
74 # The already have this field, but the subfield is new within it.
75 #print "*** adding subfield '$subtag' to '$tag': ='$data'\n";
76 $current{$tag}->add_subfields($subtag => $data);
77 }
78 }
79  
80 my $rec = new MARC::Record();
81 my $leader = $config->recordName();
82 $rec->leader($leader) if defined $leader;
83 $rec->append_fields(@fields);
84  
85 return $rec->as_usmarc();
86 }
87  
88  
89 =head1 AUTHOR
90  
91 Mike Taylor E<lt>mike@miketaylor.org.ukE<gt>
92  
93 First version Thursdat 11th November 2004.
94  
95 =head1 SEE ALSO
96  
97 C<Net::Z3950::DBIServer>
98 is the module that uses this.
99  
100 =cut
101  
102  
103 1;