Skip to content

Commit

Permalink
Read ORCID record for possible put-codes should we have lost our put-…
Browse files Browse the repository at this point in the history
…code but orcid.org reports that the record is already present.
wfyson committed Nov 7, 2018
1 parent f7ec0db commit 3003bc7
Showing 2 changed files with 118 additions and 63 deletions.
127 changes: 64 additions & 63 deletions cfg/cfg.d/z_orcid_support_advance.pl
Original file line number Diff line number Diff line change
@@ -291,75 +291,76 @@
# my( $repo, $eprint, $changed ) = @args{qw( repository dataobj changed )};
# return unless $eprint->dataset->has_field( "creators_orcid" );
#
# my $creators = $eprint->get_value('creators');
# my @new_creators;
#
# my $old_eprint = $eprint->dataset->dataobj( $eprint->id );
# my $old_creators = $old_eprint->get_value( 'creators' ) if defined $old_eprint;
#
# my $prev_ids = $changed->{creators_id};
#
# #loop through the existing creators and update them
# foreach my $c (@{$creators})
# if( !$eprint->{orcid_update} ) #this update hasn't come from orcid, therefore we want to check the orcids and put-codes
# {
# my $new_c = $c;
# my $creators = $eprint->get_value('creators');
# my @new_creators;
#
# #ensure orcid is set via user profile
# $new_c->{orcid} = undef;
# #get id and user profile
# my $email = $c->{id};
# $email = lc($email) if defined $email;
# my $user = EPrints::DataObj::User::user_with_email($eprint->repository, $email);
# if( $user )
# {
# if( EPrints::Utils::is_set( $user->value( 'orcid' ) ) ) #user has an orcid
# {
# #set the orcid
# $new_c->{orcid} = $user->value( 'orcid' );
# my $old_eprint = $eprint->dataset->dataobj( $eprint->id );
# my $old_creators = $old_eprint->get_value( 'creators' ) if defined $old_eprint;
#
# }
# }
# my $prev_ids = $changed->{creators_id};
#
# #need to update any put-codes associated with creators
# if( defined( $old_creators) && @{$old_creators} )
# {
# #first delete any put-code we've carried over, but keep a record of an existing put-code
# $new_c->{putcode} = undef;
# if( defined $new_c->{orcid} )
# {
# #we have an orcid, so see if this orcid had a put code attached previously
# foreach my $old_c ( @{$old_creators} )
# {
# if( $old_c->{orcid} eq $new_c->{orcid} && defined $old_c->{putcode} )
# {
# $new_c->{putcode} = $old_c->{putcode};
# }
# }
# }
# }
# push( @new_creators, $new_c );
# }
# #loop through the existing creators and update them
# foreach my $c (@{$creators})
# {
# my $new_c = $c;
#
# #now we have a list of new and old creators, see if any put-codes have been removed and if so, remove those records from ORCID
# foreach my $old_c ( @{$old_creators} )
# {
# my $seen = 0;
# foreach my $new_c( @new_creators )
# {
# if( $old_c->{putcode} eq $new_c->{putcode} )
# {
# $seen = 1;
# last;
# }
# }
# if( !$seen )
# {
# #this record has been removed
# #To Do: Think about deleting item in orcid record
# }
# }
# #ensure orcid is set via user profile
# $new_c->{orcid} = undef;
# #get id and user profile
# my $email = $c->{id};
# $email = lc($email) if defined $email;
# my $user = EPrints::DataObj::User::user_with_email($eprint->repository, $email);
# if( $user )
# {
# if( EPrints::Utils::is_set( $user->value( 'orcid' ) ) ) #user has an orcid
# {
# #set the orcid
# $new_c->{orcid} = $user->value( 'orcid' );
# }
# }
#
# #need to update any put-codes associated with creators
# if( defined( $old_creators) && @{$old_creators} )
# {
# #first delete any put-code we've carried over, but keep a record of an existing put-code
# $new_c->{putcode} = undef;
# if( defined $new_c->{orcid} )
# {
# #we have an orcid, so see if this orcid had a put code attached previously
# foreach my $old_c ( @{$old_creators} )
# {
# if( $old_c->{orcid} eq $new_c->{orcid} && defined $old_c->{putcode} )
# {
# $new_c->{putcode} = $old_c->{putcode};
# }
# }
# }
# }
# push( @new_creators, $new_c );
# }
#
# $eprint->set_value("creators", \@new_creators);
# #now we have a list of new and old creators, see if any put-codes have been removed and if so, remove those records from ORCID
# foreach my $old_c ( @{$old_creators} )
# {
# my $seen = 0;
# foreach my $new_c( @new_creators )
# {
# if( $old_c->{putcode} eq $new_c->{putcode} )
# {
# $seen = 1;
# last;
# }
# }
# if( !$seen )
# {
# #this record has been removed
# #To Do: Think about deleting item in orcid record
# }
# }
# $eprint->set_value("creators", \@new_creators);
# }
#}, priority => 60 );

#automatic update of eprint editor field - orcid must be set to user's orcid value
54 changes: 54 additions & 0 deletions lib/plugins/EPrints/Plugin/Screen/ExportToOrcid.pm
Original file line number Diff line number Diff line change
@@ -172,6 +172,59 @@ sub action_export{
{
$count_successful++;
}
}
elsif( $error_code eq "9021" && $method eq "POST" )
{
# This record already exists in ORCID, but we've lost our PUT code for it - lets see if we can retrieve it

#first retrieve the external ids that we've got on record
my %work_ids;
foreach my $work_ext_id ( @{$work->{'external-ids'}->{'external-id'}} )
{
$work_ids{$work_ext_id->{'external-id-type'}} = $work_ext_id->{'external-id-value'};
}

my $orcid_works = EPrints::ORCID::AdvanceUtils::read_orcid_works( $repo, $user, 0 );

foreach my $orcid_work ( @{$orcid_works} )
{
my $ext_ids = $orcid_work->{'external-ids'}->{'external-id'};
foreach my $ext_id ( @$ext_ids )
{
#if( $ext_id->{'external-id-type'} eq "doi" && $ext_id->{'external-id-value'} eq "thing" ) #we've found a matching record in the user's orcid profile
if( exists $work_ids{$ext_id->{'external-id-type'}} && $work_ids{$ext_id->{'external-id-type'}} eq $ext_id->{'external-id-value'} )
{
#update the put-code for the work we're trying to export
my @new_creators;
my $update = 0;
foreach my $c ( @{$eprint->value( "creators" )} )
{
my $new_c = $c;
if( $c->{orcid} eq $users_orcid ) #we have the matching user
{
$putcode = $orcid_work->{'put-code'};
$new_c->{putcode} = $putcode;
$update = 1;
}
push( @new_creators, $new_c );
}
if( $update )
{
$eprint->{orcid_update} = 1;
$eprint->set_value("creators", \@new_creators);
$eprint->commit;

#now we have an updated eprint with a new put code, try to PUT the record again
my $new_work = $self->eprint_to_orcid_work( $repo, $eprint );
$result = EPrints::ORCID::AdvanceUtils::write_orcid_record( $repo, $user, "PUT", "/work/$putcode", $new_work );
if( $result->is_success ) {
$count_overwrite++;
}
}
last;
}
}
}
}
}

@@ -842,6 +895,7 @@ sub _post
}
push (@new_creators, $creator);
}
$eprint->{orcid_update} = 1;
$eprint->set_value( "creators", \@new_creators );
$eprint->commit;
}

3 comments on commit 3003bc7

@wfyson
Copy link
Collaborator Author

@wfyson wfyson commented on 3003bc7 Nov 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still have an issue to resolve with regards to retrieving the correct put-code. If there is more than one version of the record we need to retrieve the put-code that matches the repository as the source - not just the first one that is returned!

@wfyson
Copy link
Collaborator Author

@wfyson wfyson commented on 3003bc7 Nov 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to find the put code for the version of the item where its source ->source-client-id ->path value is the same as our client ID... and if we can't find it just use the first put code instead!

@wfyson
Copy link
Collaborator Author

@wfyson wfyson commented on 3003bc7 Nov 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Above issues fixed with commit 292455a

Please sign in to comment.