Skip to content

Commit

Permalink
Updating plugin code to resolve server error issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Hercules committed Aug 12, 2016
1 parent 4db3b82 commit 1683cd6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 40 deletions.
36 changes: 19 additions & 17 deletions lib/plugins/EPrints/Plugin/Event/DataCiteEvent.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ EPrints::Plugin::Event::DataCiteEvent
=cut

package EPrints::Plugin::Event::DataCiteEvent;

use EPrints::Plugin::Event;
use LWP;
use Crypt::SSLeay;
# use Crypt::SSLeay;
use HTTP::Headers::Util;

@ISA = qw( EPrints::Plugin::Event );

sub datacite_doi
{
my( $self, $dataobj) = @_;

my $repository = $self->repository();

#Check object status first.... TODO Make work for dataobj == doument (just in case)
my $shoulddoi = $repository->get_conf( "datacitedoi", "eprintstatus", $dataobj->value( "eprint_status" ));
my $shoulddoi = $repository->get_conf( "datacitedoi", "eprintstatus", $dataobj->value( "eprint_status" ));
#Check Doi Status
if(!$shoulddoi){
if(!$shoulddoi){
$repository->log("Attempt to coin DOI for item that is not in the required area (see \$c->{datacitedoi}->{eprintstatus})");
return EPrints::Const::HTTP_INTERNAL_SERVER_ERROR;
}
}

my $thisdoi = $self->coin_doi($repository,$dataobj);
#coin_doi may return an event error code if no prefix present assume this is the case
Expand All @@ -41,12 +41,12 @@ sub datacite_doi
$url.="/" if($url !~ /\/$/); #attach slash if config has forgotten
my $user_name = $repository->get_conf( "datacitedoi", "user");
my $user_pw = $repository->get_conf( "datacitedoi", "pass");

#register metadata;
my($response_content, $response_code) = datacite_request("POST", $url."metadata", $user_name, $user_pw, $xml, "application/xml;charset=UTF-8");
if($response_code !~ /20(1|0)/){
$repository->log("Metadata response from datacite api: $response_code: $response_content");
$repository->log("BTW the \$doi was:\n$thisdoi");
$repository->log("BTW the \$doi was:\n$thisdoi");
return EPrints::Const::HTTP_INTERNAL_SERVER_ERROR;
}
#register doi
Expand Down Expand Up @@ -94,7 +94,7 @@ sub datacite_request {
return ($res->content(),$res->code());
}

#RM lets do the DOI coining somewhere (reasonably) accessible
#RM lets do the DOI coining somewhere (reasonably) accessible
sub coin_doi {

my( $self, $repository, $dataobj) = @_;
Expand All @@ -113,7 +113,7 @@ sub coin_doi {
my $thisdoi = $prefix.$delim1.$repository->get_conf( "datacitedoi", "repoid").$delim2.$id;

my $eprintdoifield = $repository->get_conf( "datacitedoi", "eprintdoifield");

#Custom DOIS
#if DOI field is set attempt to use that if config allows
if($dataobj->exists_and_set( $eprintdoifield) ){
Expand All @@ -125,12 +125,14 @@ sub coin_doi {
}
#we are allowed (check prefix just in case)
$thisdoi = $dataobj->get_value( $eprintdoifield );
if($thisdoi !~ /^$prefix/){
$repository->log("Prefix does not match ($prefix) for custom DOI: $thisdoi");
$dataobj->set_value($eprintdoifield, ""); #unset the bad DOI!!
$dataobj->commit();
return EPrints::Const::HTTP_INTERNAL_SERVER_ERROR;
}#We'll leave Datacite to do any further syntax checking etc...
# AH commented out because when there is an existing DOI (e.g. one issued by the publisher)
# the condition is always true and therefore, existing DOI becomes an empty string
# if($thisdoi !~ /^$prefix/){
# $repository->log("Prefix does not match ($prefix) for custom DOI: $thisdoi");
# $dataobj->set_value($eprintdoifield, ""); #unset the bad DOI!!
# $dataobj->commit();
# return EPrints::Const::HTTP_INTERNAL_SERVER_ERROR;
# }#We'll leave Datacite to do any further syntax checking etc...
}

return $thisdoi;
Expand Down
49 changes: 26 additions & 23 deletions lib/plugins/EPrints/Plugin/Export/DataCiteXML.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ sub new
$self->{suffix} = '.xml';
$self->{mimetype} = 'application/xml; charset=utf-8';
$self->{arguments}->{doi} = undef;

return $self;
}

Expand All @@ -33,17 +33,20 @@ sub output_dataobj

my $repo = $self->{repository};
my $xml = $repo->xml;

#reference the datacite schema from config
my $entry = $xml->create_element( "resource",
xmlns=> $repo->get_conf( "datacitedoi", "xmlns"),
"xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance",
"xsi:schemaLocation" => $repo->get_conf( "datacitedoi", "schemaLocation"));

#RM We pass in the DOI from Event::DataCite... or from --args on the cmd line
my $thisdoi = $opts{doi};
#RM coin a DOI if either
# - not come via event or
my $entry = $xml->create_element( "resource",
xmlns=> $repo->get_conf( "datacitedoi", "xmlns"),
"xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance",
"xsi:schemaLocation" => $repo->get_conf( "datacitedoi", "schemaLocation"));

#RM We pass in the DOI from Event::DataCite... or from --args on the cmd line
# AH my $thisdoi = $opts{doi}; always returns undefined, even when DOI exists
# Ideally coining should NOT happen in this script but opts{doi} should have it
# but is always blank
my $thisdoi = $dataobj->get_value("id_number");
#RM coin a DOI if either
# - not come via event or
# - no doi arg passed in via cmd_line
# ie when someone exports DataCiteXML from the Action tab
if(!defined $thisdoi){
Expand All @@ -56,13 +59,13 @@ sub output_dataobj
}

$entry->appendChild( $xml->create_data_element( "identifier", $thisdoi , identifierType=>"DOI" ) );

#RM otherwise we'll leave this alone for now

my $creators = $xml->create_element( "creators" );
if( $dataobj->exists_and_set( "creators" ) )
{

my $names = $dataobj->get_value( "creators" );
foreach my $name ( @$names )
{
Expand All @@ -83,13 +86,13 @@ sub output_dataobj
$titles->appendChild( $xml->create_data_element( "title", $dataobj->render_value( "title" ) ) );
$entry->appendChild( $titles );
}

$entry->appendChild( $xml->create_data_element( "publisher", $repo->get_conf( "datacitedoi", "publisher") ) );

if ($dataobj->exists_and_set( "datestamp" )) {
$dataobj->get_value( "datestamp" ) =~ /^([0-9]{4})/;
$entry->appendChild( $xml->create_data_element( "publicationYear", $1 ) ) if $1;

}


Expand All @@ -101,23 +104,23 @@ sub output_dataobj
my $subject = EPrints::DataObj::Subject->new( $repo, $val );
next unless defined $subject;
$subjects_tag->appendChild( $xml->create_data_element( "subject", $subject->render_description ) );

}
$entry->appendChild( $subjects_tag );
}
}
my $thisresourceType = $repo->get_conf( "datacitedoi", "typemap", $dataobj->get_value("type") );


my $thisresourceType = $repo->get_conf( "datacitedoi", "typemap", $dataobj->get_value("type") );
if(defined $thisresourceType ){
$entry->appendChild( $xml->create_data_element( "resourceType", $thisresourceType->{'v'}, resourceTypeGeneral=>$thisresourceType->{'a'}) );
}


my $alternateIdentifiers = $xml->create_element( "alternateIdentifiers" );
$alternateIdentifiers->appendChild( $xml->create_data_element( "alternateIdentifier", $dataobj->get_url() , alternateIdentifierType=>"URL" ) );
$entry->appendChild( $alternateIdentifiers );


#TODO Seek, identify and include for registration the optional datacite fields
return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$xml->to_string($entry);
Expand Down

0 comments on commit 1683cd6

Please sign in to comment.