Class: Uc3DmpId::Creator

Inherits:
Object
  • Object
show all
Defined in:
lib/uc3-dmp-id/creator.rb

Overview

Class that registers a new DMP ID

Constant Summary collapse

MSG_NO_BASE_URL =
'No base URL found for DMP ID (e.g. `doi.org`)'
MSG_NO_SHOULDER =
'No DOI shoulder found. (e.g. `10.12345/`)'
MSG_UNABLE_TO_MINT =
'Unable to mint a unique DMP ID.'

Class Method Summary collapse

Class Method Details

.create(provenance:, json:, logger: nil) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/uc3-dmp-id/creator.rb', line 18

def create(provenance:, json:, logger: nil)
  raise CreatorError, MSG_NO_SHOULDER if ENV['DMP_ID_SHOULDER'].nil?
  raise CreatorError, MSG_NO_BASE_URL if ENV['DMP_ID_BASE_URL'].nil?

  # Fail if the provenance is not defined
  raise CreatorError, Helper::MSG_DMP_FORBIDDEN unless provenance.is_a?(Hash) && !provenance['PK'].nil?

  # Validate the incoming JSON first
  json = Helper.parse_json(json:)
  errs = Validator.validate(mode: 'author', json:)
  raise CreatorError, errs.join(', ') if errs.is_a?(Array) && errs.any? && errs.first != Validator::MSG_VALID_JSON

  # Try to find it by the :dmp_id first and Fail if found
  dmp_id = Helper.dmp_id_to_pk(json: json.fetch('dmp', {})['dmp_id'])
  result = Finder.exists?(p_key: dmp_id, logger:) unless dmp_id.nil?
  raise CreatorError, Helper::MSG_DMP_EXISTS if result.is_a?(Hash)

  # raise CreatorError, Uc3DmpId::MSG_DMP_EXISTS unless json['PK'].nil?

  client = Uc3DmpDynamo::Client.new
  p_key = _preregister_dmp_id(client:, provenance:, json:, logger:)
  raise CreatorError, MSG_UNABLE_TO_MINT if p_key.nil?

  # Add the DMPHub specific attributes and then save
  annotated = Helper.annotate_dmp_json(provenance:, p_key:, json: json['dmp'])
  logger.info(message: "Creating DMP ID: #{p_key}") if logger.respond_to?(:debug)

  # Set the :created and :modified timestamps
  now = Time.now.utc.iso8601
  seeding = provenance.fetch('seedingWithLiveDmpIds', false).to_s.downcase == 'true'
  # Do not overwrite the created/modified timestamps if we are seeding!
  annotated['created'] = now unless seeding
  annotated['modified'] = now unless seeding

  # Make sure we set the registration date. This will change in the new system
  annotated['registered'] = annotated['created'] if annotated['registered'].nil?

  # Create the item
  annotated['dmphub_modifications'] = []
  resp = client.put_item(json: annotated, logger:)
  raise CreatorError, Helper::MSG_DMP_NO_DMP_ID if resp.nil?

  _post_process(json: annotated, logger:)
  Helper.cleanse_dmp_json(json: JSON.parse({ dmp: annotated }.to_json))
end