Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/datacite/lupo into schema…
Browse files Browse the repository at this point in the history
…-4.5-r3
  • Loading branch information
codycooperross committed Dec 7, 2023
2 parents acde070 + ad5b17c commit 93cead1
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 7 deletions.
42 changes: 35 additions & 7 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ def validate_publisher_obj?(doi)
indexes :provider_id, type: :keyword
indexes :consortium_id, type: :keyword
indexes :resource_type_id, type: :keyword
indexes :person_id, type: :keyword
indexes :affiliation_id, type: :keyword
indexes :fair_affiliation_id, type: :keyword
indexes :organization_id, type: :keyword
Expand Down Expand Up @@ -563,6 +564,14 @@ def validate_publisher_obj?(doi)
indexes :fields_of_science, type: :keyword
indexes :fields_of_science_combined, type: :keyword
indexes :fields_of_science_repository, type: :keyword
indexes :related_doi, type: :object, properties: {
client_id: { type: :keyword },
doi: { type: :keyword },
organization_id: { type: :keyword },
person_id: { type: :keyword },
resource_type_id: { type: :keyword },
resource_type_id_and_name: { type: :keyword },
}
indexes :publisher_obj, type: :object, properties: {
name: { type: :text, fields: { keyword: { type: "keyword" } } },
publisherIdentifier: { type: :keyword, normalizer: "keyword_lowercase" },
Expand Down Expand Up @@ -1681,6 +1690,10 @@ def consortium_id
client.provider.consortium_id.downcase if client.present? && client.provider.consortium_id.present?
end

def related_dois
Doi::Indexer::RelatedDoiIndexer.new(related_identifiers).as_indexed_json
end

def related_dmp_ids
Array.wrap(related_identifiers).select { |related_identifier|
related_identifier["relatedIdentifierType"] == "DOI"
Expand Down Expand Up @@ -1711,6 +1724,19 @@ def sponsor_contributors
end


def person_id
(Array.wrap(creators) + Array.wrap(contributors)).reduce([]) do |sum, c|
Array.wrap(c.fetch("nameIdentifiers", nil)).each do |name_identifier|
if name_identifier.is_a?(Hash) && name_identifier.fetch("nameIdentifierScheme", nil) == "ORCID" && name_identifier.fetch("nameIdentifier", nil).present?
sum << orcid_as_url(
orcid_from_url(name_identifier.fetch("nameIdentifier", nil))
)
end
end
sum.uniq
end
end

def organization_id
organization_ids = (Array.wrap(creators) + Array.wrap(contributors)).reduce([]) do |sum, c|
Array.wrap(c.fetch("nameIdentifiers", nil)).each do |name_identifier|
Expand All @@ -1719,7 +1745,7 @@ def organization_id
sum
end
organization_ids << ror_from_url(publisher["publisherIdentifier"]) if publisher_has_ror?
organization_ids
organization_ids.uniq
end

def publisher_has_ror?
Expand All @@ -1731,9 +1757,11 @@ def publisher_has_ror?
def fair_organization_id
(Array.wrap(creators) + sponsor_contributors).reduce([]) do |sum, c|
Array.wrap(c.fetch("nameIdentifiers", nil)).each do |name_identifier|
sum << ror_from_url(name_identifier.fetch("nameIdentifier", nil)) if name_identifier.is_a?(Hash) && name_identifier.fetch("nameIdentifierScheme", nil) == "ROR" && name_identifier.fetch("nameIdentifier", nil).present?
if name_identifier.is_a?(Hash) && name_identifier.fetch("nameIdentifierScheme", nil) == "ROR" && name_identifier.fetch("nameIdentifier", nil).present?
sum << ror_from_url(name_identifier.fetch("nameIdentifier", nil))
end
end
sum
sum.uniq
end
end

Expand All @@ -1742,7 +1770,7 @@ def affiliation_id
Array.wrap(c.fetch("affiliation", nil)).each do |affiliation|
sum << ror_from_url(affiliation.fetch("affiliationIdentifier", nil)) if affiliation.is_a?(Hash) && affiliation.fetch("affiliationIdentifierScheme", nil) == "ROR" && affiliation.fetch("affiliationIdentifier", nil).present?
end
sum
sum.uniq
end
end

Expand All @@ -1751,7 +1779,7 @@ def fair_affiliation_id
Array.wrap(c.fetch("affiliation", nil)).each do |affiliation|
sum << ror_from_url(affiliation.fetch("affiliationIdentifier", nil)) if affiliation.is_a?(Hash) && affiliation.fetch("affiliationIdentifierScheme", nil) == "ROR" && affiliation.fetch("affiliationIdentifier", nil).present?
end
sum
sum.uniq
end
end

Expand All @@ -1760,7 +1788,7 @@ def affiliation_id_and_name
Array.wrap(c.fetch("affiliation", nil)).each do |affiliation|
sum << "#{ror_from_url(affiliation.fetch('affiliationIdentifier', nil))}:#{affiliation.fetch('name', nil)}" if affiliation.is_a?(Hash) && affiliation.fetch("affiliationIdentifierScheme", nil) == "ROR" && affiliation.fetch("affiliationIdentifier", nil).present?
end
sum
sum.uniq
end
end

Expand All @@ -1769,7 +1797,7 @@ def fair_affiliation_id_and_name
Array.wrap(c.fetch("affiliation", nil)).each do |affiliation|
sum << "#{ror_from_url(affiliation.fetch('affiliationIdentifier', nil))}:#{affiliation.fetch('name', nil)}" if affiliation.is_a?(Hash) && affiliation.fetch("affiliationIdentifierScheme", nil) == "ROR" && affiliation.fetch("affiliationIdentifier", nil).present?
end
sum
sum.uniq
end
end

Expand Down
45 changes: 45 additions & 0 deletions app/models/doi/indexer/related_doi_indexer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

module Doi::Indexer
class RelatedDoiIndexer
def initialize(related_identifiers)
@related_identifiers = related_identifiers
@related_dois = nil
end

def related_dois
@related_dois ||= @related_identifiers.select { |r| r["relatedIdentifierType"] == "DOI" }
end

def related_grouped_by_id
related_dois.group_by { |r| r["relatedIdentifier"].downcase }
end

def relation_types_gouped_by_id
related_grouped_by_id.transform_values do |values|
values.map { |val| val["relationType"].underscore }.uniq
end
end

def related_doi_ids
related_grouped_by_id.keys
end

def dois
Doi.where(doi: related_doi_ids)
end

def indexed_dois
dois.map { |d| RelatedIdentifierDenormalizer.new(d).to_hash }
end

def as_indexed_json
rtypes = relation_types_gouped_by_id
indexed_dois.map do |indexed_doi|
doi = indexed_doi["doi"]
indexed_doi["relation_type"] = rtypes[doi]
indexed_doi
end
end
end
end
32 changes: 32 additions & 0 deletions app/models/doi/indexer/related_identifier_denormalizer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module Doi::Indexer
class RelatedIdentifierDenormalizer
attr_reader :related_doi

def initialize(doi)
@related_doi = doi
end

def to_hash
%w[
client_id
doi
organization_id
person_id
resource_type_id
resource_type_id_and_name
].index_with { |method_name| send(method_name) }
end

delegate :resource_type_id, to: :related_doi
delegate :resource_type_id_and_name, to: :related_doi
delegate :organization_id, to: :related_doi
delegate :person_id, to: :related_doi
delegate :client_id, to: :related_doi

def doi
@related_doi.doi.downcase
end
end
end
60 changes: 60 additions & 0 deletions spec/models/doi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,66 @@
end
end

describe "person_ids" do
it "from creators and contributors" do
subject = build(
:doi,
creators: [
{
"familyName" => "Garza",
"givenName" => "Kristian",
"name" => "Garza, Kristian",
"nameIdentifiers" => [
{
"nameIdentifier" => "https://orcid.org/0000-0003-3484-6875",
"nameIdentifierScheme" => "ORCID",
"schemeUri" => "https://orcid.org",
},
],
"nameType" => "Personal",
"affiliation" => [
{
"name" => "University of Cambridge",
"affiliationIdentifier" => "https://ror.org/013meh722",
"affiliationIdentifierScheme" => "ROR",
},
],
},
],
contributors: [
{
"contributorType" => "Sponsor",
"familyName" => "Bob",
"givenName" => "Jones",
"name" => "Jones, Bob",
"nameIdentifiers" => [
{
"nameIdentifier" => "https://orcid.org/0000-0003-3484-0000",
"nameIdentifierScheme" => "ORCID",
"schemeUri" => "https://orcid.org",
},
],
"nameType" => "Personal",
"affiliation" => [
{
"name" => "University of Examples",
"affiliationIdentifier" => "https://ror.org/013meh8888",
"affiliationIdentifierScheme" => "ROR",
},
],
},
]
)
expect(subject).to be_valid
expect(subject.person_id).to eq(
[
"https://orcid.org/0000-0003-3484-6875",
"https://orcid.org/0000-0003-3484-0000",
]
)
end
end

describe "related_identifiers" do
it "has part" do
subject = build(:doi, related_identifiers: [
Expand Down
60 changes: 60 additions & 0 deletions spec/models/doi_verified_related_identifiers_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

# frozen_string_literal: true

require "rails_helper"

describe Doi, type: :model, vcr: true, elasticsearch: true do
describe "related_doi" do
let(:client) { create(:client) }
let(:target_doi) do
create(:doi,
client: client,
aasm_state: "findable",
types: { "resourceTypeGeneral" => "Dataset" }
)
end
let(:doi) do
create(:doi,
client: client,
aasm_state: "findable",
related_identifiers: [
{
"relatedIdentifier": target_doi.doi,
"relatedIdentifierType": "DOI",
"relationType": "HasPart",
"resourceTypeGeneral": "OutputManagementPlan",
},
{
"relatedIdentifier": target_doi.doi,
"relatedIdentifierType": "DOI",
"relationType": "Cites",
"resourceTypeGeneral": "Text",
}
])
end

it "indexes related_dois" do
expect(doi.related_dois.first["doi"]).to eq(target_doi.doi.downcase)
end

it "indexes related doi's client_id" do
expect(doi.related_dois.first["client_id"]).to eq(target_doi.client_id)
end

it "indexes related doi's person_id" do
expect(doi.related_dois.first["person_id"]).to eq(target_doi.person_id)
end

it "does not index related doi's claimed resource_type_id" do
expect(doi.related_dois.first["resource_type_id"]).not_to eq("output_management_plan")
end

it "indexes related doi's true resource_type_id" do
expect(doi.related_dois.first["resource_type_id"]).to eq("dataset")
end

it "indexes all relations to the related doi" do
expect(doi.related_dois.first["relation_type"]).to eq(["has_part", "cites"])
end
end
end

0 comments on commit 93cead1

Please sign in to comment.