Bug 22144: (QA follow-up) Prepare the ground for other formats
[koha.git] / Koha / Biblio / Metadata.pm
1 package Koha::Biblio::Metadata;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19
20 use MARC::Record;
21 use MARC::File::XML;
22
23 use Koha::Database;
24 use Koha::Exceptions::Metadata;
25
26 use base qw(Koha::Object);
27
28 =head1 NAME
29
30 Koha::Metadata - Koha Metadata Object class
31
32 =head1 API
33
34 =head2 Class methods
35
36 =cut
37
38 =head3 record
39
40 my $record = $metadata->record;
41
42 Returns an object representing the metadata record. The expected record type
43 corresponds to this table:
44
45     -------------------------------
46     | format     | object type    |
47     -------------------------------
48     | marcxml    | MARC::Record   |
49     -------------------------------
50
51 =head4 Error handling
52
53 =over
54
55 =item If an unsupported format is found, it throws a I<Koha::Exceptions::Metadata> exception.
56
57 =item If it fails to create the record object, it throws a I<Koha::Exceptions::Metadata::Invalid> exception.
58
59 =back
60
61 =cut
62
63 sub record {
64
65     my ($self) = @_;
66
67     my $record;
68
69     if ( $self->format eq 'marcxml' ) {
70         $record = eval { MARC::Record::new_from_xml( $self->metadata, 'utf-8', $self->schema ); };
71         unless ($record) {
72             Koha::Exceptions::Metadata::Invalid->throw(
73                 id     => $self->id,
74                 format => $self->format,
75                 schema => $self->schema
76             );
77         }
78     }
79     else {
80         Koha::Exceptions::Metadata->throw(
81             'Koha::Biblio::Metadata->record called on unhandled format: ' . $self->format );
82     }
83
84     return $record;
85 }
86
87 =head2 Internal methods
88
89 =head3 _type
90
91 =cut
92
93 sub _type {
94     return 'BiblioMetadata';
95 }
96
97 1;