use Modern::Perl;
-use Carp;
-
-use C4::Biblio qw();
+use MARC::Record;
+use MARC::File::XML;
use Koha::Database;
+use Koha::Exceptions::Metadata;
use base qw(Koha::Object);
=head1 API
-=head2 Class Methods
+=head2 Class methods
=cut
=head3 record
-my @record = $biblio->record($params);
+my $record = $metadata->record;
+
+Returns an object representing the metadata record. The expected record type
+corresponds to this table:
+
+ -------------------------------
+ | format | object type |
+ -------------------------------
+ | marcxml | MARC::Record |
+ -------------------------------
+
+=head4 Error handling
-Returns a MARC::Record object for a record.
+=over
-This method accepts the same paramters as C4::Biblio::GetMarcBiblio,
-but does not require the 'biblionumber' parameter.
+=item If an unsupported format is found, it throws a I<Koha::Exceptions::Metadata> exception.
+
+=item If it fails to create the record object, it throws a I<Koha::Exceptions::Metadata::Invalid> exception.
+
+=back
=cut
sub record {
- my ( $self, $params ) = @_;
-
- $params->{biblionumber} = $self->biblionumber;
- my $record = C4::Biblio::GetMarcBiblio($params);
+ my ($self) = @_;
+
+ my $record;
+
+ if ( $self->format eq 'marcxml' ) {
+ $record = eval { MARC::Record::new_from_xml( $self->metadata, 'utf-8', $self->schema ); };
+ unless ($record) {
+ Koha::Exceptions::Metadata::Invalid->throw(
+ id => $self->id,
+ format => $self->format,
+ schema => $self->schema
+ );
+ }
+ }
+ else {
+ Koha::Exceptions::Metadata->throw(
+ 'Koha::Biblio::Metadata->record called on unhandled format: ' . $self->format );
+ }
return $record;
}
+=head2 Internal methods
-=head3 type
+=head3 _type
=cut
--- /dev/null
+#!/usr/bin/perl
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Test::More tests => 2;
+use Test::Exception;
+
+use t::lib::TestBuilder;
+
+use C4::Biblio;
+use Koha::Database;
+
+BEGIN {
+ use_ok('Koha::Biblio::Metadatas');
+}
+
+my $schema = Koha::Database->new->schema;
+my $builder = t::lib::TestBuilder->new;
+
+subtest 'record() tests' => sub {
+
+ plan tests => 8;
+
+ $schema->storage->txn_begin;
+
+ my $title = 'Oranges and Peaches';
+
+ # Create a valid record
+ my $record = MARC::Record->new();
+ my $field = MARC::Field->new( '245', '', '', 'a' => $title );
+ $record->append_fields($field);
+ my ($biblio_id) = C4::Biblio::AddBiblio( $record, '' );
+
+ my $metadata = Koha::Biblios->find($biblio_id)->metadata;
+ my $record2 = $metadata->record;
+
+ is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' );
+ is( $record2->field('245')->subfield("a"),
+ $title, 'Title in 245$a matches title from original record object' );
+
+ my $bad_data = $builder->build_object(
+ { class => 'Koha::Biblio::Metadatas',
+ value => { format => 'marcxml', schema => 'MARC21', metadata => 'this_is_not_marcxml' }
+ }
+ );
+
+ throws_ok { $bad_data->record; }
+ 'Koha::Exceptions::Metadata::Invalid', 'Exception thrown on bad record';
+
+ my $exception = $@;
+ is( $exception->id, $bad_data->id, 'id passed correctly to exception' );
+ is( $exception->format, 'marcxml', 'format passed correctly to exception' );
+ is( $exception->schema, 'MARC21', 'schema passed correctly to exception' );
+
+ my $bad_format = $builder->build_object(
+ { class => 'Koha::Biblio::Metadatas',
+ value => { format => 'mij', schema => 'MARC21', metadata => 'something' }
+ }
+ );
+
+ throws_ok { $bad_format->record; }
+ 'Koha::Exceptions::Metadata', 'Exception thrown on unhandled format';
+
+ is( "$@",
+ 'Koha::Biblio::Metadata->record called on unhandled format: mij',
+ 'Exception message built correctly'
+ );
+
+ $schema->storage->txn_rollback;
+};