Class: Dmp::MetadataHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/dmp/metadata_handler.rb

Overview

Handles alterations to DMP metadata elements

Constant Summary collapse

PK_DMP_PREFIX =
'DMP#'.freeze
PK_PROVENANCE_PREFIX =
'PROVENANCE#'.freeze
SK_PREFIX =
'VERSION#'.freeze
LATEST_VERSION =
"#{SK_PREFIX}latest".freeze
TOMBSTONE_VERSION =
"#{SK_PREFIX}tombstone".freeze

Class Method Summary collapse

Class Method Details

.annotate_json(provenance:, p_key:, json:) ⇒ Object

Add all attributes necessary for the DMPHub rubocop:disable Metrics/MethodLength, Metrics/AbcSize



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dmp/metadata_handler.rb', line 60

def annotate_json(provenance:, p_key:, json:)
  return nil if provenance.nil? || p_key.nil? || json.nil?

p "ANNOTATING METADATA:"

  # Fail if the :PK does not match the :dmp_id if the json has a :PK
  id = Dmp::DmpIdHandler.dmp_id_to_pk(json: json.fetch('dmp_id', {}))
  id = nil if id != p_key && !json['PK'].nil?

  annotated = deep_copy(obj: json)
  annotated['PK'] = json['PK'] || p_key
  annotated['SK'] = LATEST_VERSION

  # Ensure that the :dmp_id matches the :PK
  annotated['dmp_id'] = Dmp::DmpIdHandler.pk_to_dmp_id(p_key: annotated['PK'])

  # Update the modification timestamps
  annotated['dmphub_modification_day'] = Time.now.strftime('%Y-%m-%d')
  annotated['dmphub_updated_at'] = Time.now.iso8601
  # Only add the Creation date if it is blank
  annotated['dmphub_created_at'] = Time.now.iso8601 if json['dmphub_created_at'].nil?
  return annotated unless json['dmphub_provenance_id'].nil?

  annotated['dmphub_provenance_id'] = provenance
  return annotated if !annotated['dmphub_provenance_identifier'].nil? ||
                      json.fetch('dmp_id', {})['identifier'].nil?

  # Record the original Provenance system's identifier
  annotated['dmphub_provenance_identifier'] = json.fetch('dmp_id', {})['identifier']
  annotated
end

.append_pk_prefix(dmp: nil, provenance: nil) ⇒ Object

Append the PK prefix for the object



37
38
39
40
41
42
43
44
45
# File 'lib/dmp/metadata_handler.rb', line 37

def append_pk_prefix(dmp: nil, provenance: nil)
  # If all the :PK types were passed return nil because we only want one
  return nil if !dmp.nil? && !provenance.nil?

  return "#{PK_DMP_PREFIX}#{remove_pk_prefix(dmp: dmp)}" unless dmp.nil?
  return "#{PK_PROVENANCE_PREFIX}#{remove_pk_prefix(provenance: provenance)}" unless provenance.nil?

  nil
end

.eql(dmp_a:, dmp_b:) ⇒ Object

determine if the objects are equal. This ignores :SK, :dmphub_modification_day and :dmphub_updated_at attributes



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dmp/metadata_handler.rb', line 19

def eql(dmp_a:, dmp_b:)
  dmp_a = {} if dmp_a.nil?
  dmp_b = {} if dmp_b.nil?
  # They are not equal if the :PK do not match (and aren't blank)
  return false if !dmp_a['PK'].nil? && !dmp_b['PK'].nil? && dmp_a['PK'] != dmp_b['PK']

  a = deep_copy(obj: dmp_a)
  b = deep_copy(obj: dmp_b)

  # ignore some of the attributes before comparing
  %w[SK dmphub_modification_day dmphub_updated_at dmphub_created_at].each do |key|
    a.delete(key) unless a[key].nil?
    b.delete(key) unless b[key].nil?
  end
  a == b
end

.process_update(updater:, original_version:, new_version:) ⇒ Object

Process an update on the DMP metadata



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/dmp/metadata_handler.rb', line 94

def process_update(updater:, original_version:, new_version:)
  return nil if updater.nil? || new_version.nil?
  # If there is no :original_version then assume it's a new DMP
  return new_version if original_version.nil?
  # does not allow tombstoned DMPs to be updated
  return original_version if original_version['SK'] == TOMBSTONE_VERSION
  return original_version if eql(dmp_a: original_version, dmp_b: new_version)

  owner = original_version['dmphub_provenance_id']
  args = { owner: owner, updater: updater }

  # If the system of provenance is making the change then just use the
  # new version as the base and then splice in any mods made by others
  args = args.merge({ base: new_version, mods: original_version})
  return splice_for_owner(args) if owner == updater

  # Otherwise use the original version as the base and then update the
  # metadata owned by the updater system
  args = args.merge({ base: original_version, mods: new_version})
  splice_for_others(args)
end

.remove_pk_prefix(dmp: nil, provenance: nil) ⇒ Object

Strip off the PK prefix



48
49
50
51
52
53
54
55
56
# File 'lib/dmp/metadata_handler.rb', line 48

def remove_pk_prefix(dmp: nil, provenance: nil)
  # If all the :PK types were passed return nil because we only want one
  return nil if !dmp.nil? && !provenance.nil?

  return dmp.gsub(PK_DMP_PREFIX, '') unless dmp.nil?
  return provenance.gsub(PK_PROVENANCE_PREFIX, '') unless provenance.nil?

  nil
end