Catmandu-FedoraCommons
view release on metacpan or search on metacpan
lib/Catmandu/Store/FedoraCommons/DC.pm view on Meta::CPAN
package Catmandu::Store::FedoraCommons::DC;
use Moo;
use XML::LibXML;
use Data::Validate::Type qw(:boolean_tests);
has fedora => (is => 'ro' , required => 1);
# REQUIRED METHODS FOR A MODEL
sub get {
my ($self,$pid) = @_;
return undef unless $pid;
my $res = $self->fedora->getDatastreamDissemination( pid => $pid , dsID => 'DC');
return undef unless $res->is_ok;
my $data = $res->parse_content;
my $perl = $self->deserialize($data);
{ _id => $pid , %$perl };
}
sub update {
my ($self,$obj) = @_;
my $pid = $obj->{_id};
return undef unless $pid;
my ($valid,$reason) = $self->valid($obj);
unless ($valid) {
warn "data is not valid";
return undef;
}
my $xml = $self->serialize($obj);
my $result = $self->fedora->modifyDatastream( pid => $pid , dsID => 'DC', xml => $xml);
return $result->is_ok;
}
# HELPER METHODS
# Die fast data validator
sub valid {
my ($self,$perl) = @_;
unless (is_hashref($perl)) {
return wantarray ? (0, "not a HASH ref") : undef ;
}
unless (Data::Validate::Type::filter_hashref($perl, allow_empty => 0)) {
return wantarray ? (0, "empty HASH ref") : undef;
}
my $found = undef;
for my $key (keys %$perl) {
my $value = $perl->{$key};
next if $key eq '_id';
unless ($key =~ m{^(contributor|coverage|creator|date|description|format|identifier|language|publisher|relation|rights|source|subject|title|type)$}) {
return wantarray ? (0, "unknown field $key") : undef;
}
unless (is_arrayref($value)) {
return wantarray ? (0, "field $key isn't an ARRAY") : undef;
}
for my $value_str (@$value) {
unless (is_string($value_str)) {
return wantarray ? (0, "field $key value isn't a string") : undef;
}
}
$found = 1;
}
unless (defined $found) {
return wantarray ? (0, "need at least one field") : undef;
}
( run in 0.883 second using v1.01-cache-2.11-cpan-39bf76dae61 )