Class: Uc3DmpId::Updater

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

Overview

Class that handles updating a DMP ID

Class Method Summary collapse

Class Method Details

.attach_narrative(provenance:, p_key:, url:, logger: nil) ⇒ Object

Save a DMP ID’s corresponding narrative PDF document to S3 and add the download URL for that document to the DMP ID’s :dmpraodmap_related_identifiers array as an ‘is_metadata_for` relation rubocop:disable Metrics/AbcSize

Raises:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/uc3-dmp-id/updater.rb', line 80

def attach_narrative(provenance:, p_key:, url:, logger: nil)
  raise UpdaterError, Helper::MSG_DMP_INVALID_DMP_ID unless p_key.is_a?(String) && !p_key.strip.empty?

  # fetch the existing latest version of the DMP ID
  client = Uc3DmpDynamo::Client.new(logger:)
  # dmp = Finder.by_pk(p_key:, client:, logger:, cleanse: false)
  resp = client.get_item(
    key: { PK: Helper.append_pk_prefix(p_key:), SK: Helper::DMP_LATEST_VERSION },
    logger:
  )
  raise UpdaterError, Helper::MSG_DMP_INVALID_DMP_ID unless resp.is_a?(Hash)

  dmp = resp['dmp'].nil? ? resp : resp['dmp']
  logger.info(message: 'Existing latest record', details: dmp) if logger.respond_to?(:debug)
  raise UpdaterError, Helper::MSG_DMP_FORBIDDEN unless provenance.is_a?(Hash) && !provenance['PK'].nil? &&
                                                       provenance['PK'] == dmp['dmphub_provenance_id']

  logger&.debug(message: "DMP Prior to narrative attachment", details: dmp)

  # Add the download URl for the PDF as a related identifier on the DMP ID record
  dmp['dmproadmap_related_identifiers'] = [] if dmp['dmproadmap_related_identifiers'].nil?
  dmp['dmproadmap_related_identifiers'] << JSON.parse({
    descriptor: 'is_metadata_for', work_type: 'output_management_plan', type: 'url', identifier: url
  }.to_json)

  # Save the changes without creating a new version!
  logger&.debug(message: "DMP After narrative attachment", details: dmp)
  resp = client.put_item(json: dmp, logger:)
  raise UpdaterError, Helper::MSG_DMP_UNABLE_TO_VERSION if resp.nil?

  logger&.debug(message: "Added DMP ID narrative for PK: #{p_key}, Narrative: #{url}")
  true
end

.update(provenance:, p_key:, json: {}, logger: nil) ⇒ Object

Update a DMP ID rubocop:disable Metrics/AbcSize, Metrics/MethodLength rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity


Raises:



16
17
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
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/uc3-dmp-id/updater.rb', line 16

def update(provenance:, p_key:, json: {}, logger: nil)
  raise UpdaterError, Helper::MSG_DMP_INVALID_DMP_ID unless p_key.is_a?(String) && !p_key.strip.empty?

  mods = Helper.parse_json(json:).fetch('dmp', {})
  p_key = Helper.append_pk_prefix(p_key:)
  logger.debug(message: "Incoming modifications for PK #{p_key}", details: mods) if logger.respond_to?(:debug)

  # Fetch the latest version of the DMP ID
  client = Uc3DmpDynamo::Client.new
  latest_version = Finder.by_pk(p_key:, client:, logger:, cleanse: false)
  latest_version = latest_version.fetch('dmp', {}) unless latest_version['dmp'].nil?
  logger.debug(message: "Latest version for PK #{p_key}", details: latest_version) if logger.respond_to?(:debug)

  # Verify that the DMP ID is updateable with the info passed in
  errs = _updateable?(provenance:, p_key:, latest_version: latest_version['dmp'],
                      mods: mods['dmp'])
  logger.error(message: errs.join(', ')) if logger.respond_to?(:error) && errs.is_a?(Array) && errs.any?
  raise UpdaterError, errs if errs.is_a?(Array) && errs.any?
  # Don't continue if nothing has changed!
  raise UpdaterError, Helper::MSG_NO_CHANGE if Helper.eql?(dmp_a: latest_version, dmp_b: mods)

  # Version the DMP ID record (if applicable).
  owner = latest_version['dmphub_provenance_id']
  updater = provenance['PK']
  version = Versioner.generate_version(client:, latest_version:, owner:,
                                       updater:, logger:)
  raise UpdaterError, Helper::MSG_DMP_UNABLE_TO_VERSION if version.nil?
  # Bail if the system trying to make the update is not the creator of the DMP ID
  raise UpdaterError, Helper::MSG_DMP_FORBIDDEN if owner != updater

  # Handle any changes to the dmphub_modifications section
  version = _process_harvester_mods(client:, p_key:, json: version, logger:)

  # Remove the version info because we don't want to save it on the record
  version.delete('dmphub_versions')

  # Splice the assertions
  version = _process_modifications(owner:, updater:, version:, mods:, logger:)
  # Set the :modified timestamps
  now = Time.now.utc
  version['modified'] = now.iso8601
  version['dmphub_modification_day'] = now.strftime('%Y-%m-%d')

  # Save the changes
  resp = client.put_item(json: version, logger:)
  raise UpdaterError, Helper::MSG_DMP_UNABLE_TO_VERSION if resp.nil?

  # Send the updates to EZID
  _post_process(provenance:, json: version, logger:)

  # Return the new version record
  logger.info(message: "Updated DMP ID: #{p_key}") if logger.respond_to?(:debug)

  # Append the :dmphub_versions Array
  json = JSON.parse({ dmp: version }.to_json)
  json = Versioner.append_versions(p_key:, dmp: json, client:, logger:)
  Helper.cleanse_dmp_json(json:)
end