diff --git a/app/models/doi.rb b/app/models/doi.rb index 2d13fc58a..522165095 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -339,6 +339,22 @@ def self.find_by_id(id, options={}) }) end + def self.import_one(doi: nil) + doi = Doi.where(doi: doi).first + return nil unless doi.present? + + string = doi.current_metadata.present? ? doi.current_metadata.xml : nil + meta = doi.read_datacite(string: string, sandbox: doi.sandbox) + attrs = %w(creators contributors titles publisher publication_year types descriptions container sizes formats language dates identifiers related_identifiers funding_references geo_locations rights_list subjects content_url).map do |a| + [a.to_sym, meta[a]] + end.to_h.merge(schema_version: meta["schema_version"] || "http://datacite.org/schema/kernel-4", version_info: meta["version"], xml: string) + + doi.update_attributes(attrs) + logger.info "[MySQL] Imported metadata for DOI " + doi.doi + "." + rescue TypeError, NoMethodError => error + logger.error "[MySQL] Error importing metadata for " + doi.doi + ": " + error.message + end + def self.import_all(options={}) from_date = options[:from_date].present? ? Date.parse(options[:from_date]) : Date.current until_date = options[:until_date].present? ? Date.parse(options[:until_date]) : Date.current diff --git a/lib/tasks/doi.rake b/lib/tasks/doi.rake index fe0658fc4..3ef15cf94 100644 --- a/lib/tasks/doi.rake +++ b/lib/tasks/doi.rake @@ -27,6 +27,16 @@ namespace :doi do puts "DOIs created on #{from_date} imported." end + desc 'Import one DOI' + task :import_one => :environment do + if ENV['DOI'].nil? + puts "ENV['DOI'] is required" + exit + end + + Doi.import_one(doi: ENV['DOI']) + end + desc 'Index all DOIs' task :index => :environment do if ENV['YEAR'].present?