Class: Dor::IdentityMetadataDS

Inherits:
ActiveFedora::OmDatastream
  • Object
show all
Includes:
SolrDocHelper
Defined in:
lib/dor/datastreams/identity_metadata_ds.rb

Overview

Object identity and source metadata

Constant Summary collapse

CATKEY_TYPE_ID =

ids for previous and current catkeys

'catkey'
PREVIOUS_CATKEY_TYPE_ID =
'previous_catkey'
BARCODE_TYPE_ID =
'barcode'

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SolrDocHelper

#add_solr_value

Class Method Details

.xml_templateObject



34
35
36
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 34

def self.xml_template
  Nokogiri::XML('<identityMetadata/>')
end

Instance Method Details

#add_other_Id(type, val) ⇒ Object



96
97
98
99
100
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 96

def add_other_Id(type, val)
  raise 'There is an existing entry for ' + type + ', consider using update_other_Id().' if otherId(type).length > 0

  add_otherId(type + ':' + val)
end

#add_otherId(other_id) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 87

def add_otherId(other_id)
  ng_xml_will_change!
  (name, val) = other_id.split(/:/, 2)
  node = ng_xml.root.add_child('<otherId/>').first
  node['name'] = name
  node.content = val
  node
end

#add_value(name, value, attrs = {}) ⇒ Object



38
39
40
41
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 38

def add_value(name, value, attrs = {})
  ng_xml_will_change!
  add_child_node(ng_xml.root, :value, name, value, attrs)
end

#barcodeObject



148
149
150
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 148

def barcode
  otherId(BARCODE_TYPE_ID).first
end

#barcode=(val) ⇒ String

Convenience method to set the barcode

Parameters:

  • val (String)

    the new barcode

Returns:

  • (String)

    same value, as per Ruby assignment convention



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 155

def barcode=(val)
  if val.blank? # if we are setting the barcode to blank, remove the node from XML
    remove_other_Id(BARCODE_TYPE_ID)
  elsif barcode.blank? # if there is no current barcode, then add it
    add_other_Id(BARCODE_TYPE_ID, val)
  else # if there is a current barcode, update the current barcode to the new value
    update_other_Id(BARCODE_TYPE_ID, val)
  end

  val
end

#catkeyString

Convenience method to get the current catkey

Returns:

  • (String)

    current catkey value (or nil if none found)



120
121
122
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 120

def catkey
  otherId(CATKEY_TYPE_ID).first
end

#catkey=(val) ⇒ String

Convenience method to set the catkey

Parameters:

  • val (String)

    the new source identifier

Returns:

  • (String)

    same value, as per Ruby assignment convention



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 127

def catkey=(val)
  # if there was already a catkey in the record, store that in the "previous" spot (assuming there is no change)
  add_otherId("#{PREVIOUS_CATKEY_TYPE_ID}:#{catkey}") if val != catkey && !catkey.blank?

  if val.blank? # if we are setting the catkey to blank, remove the node from XML
    remove_other_Id(CATKEY_TYPE_ID)
  elsif catkey.blank? # if there is no current catkey, then add it
    add_other_Id(CATKEY_TYPE_ID, val)
  else # if there is a current catkey, update the current catkey to the new value
    update_other_Id(CATKEY_TYPE_ID, val)
  end

  val
end

#objectIdObject



43
44
45
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 43

def objectId
  find_by_terms(:objectId).text
end

#other_ids=(values) ⇒ Object

Parameters:

  • values (Array<String>)


83
84
85
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 83

def other_ids=(values)
  values.each { |value| add_otherId(value) }
end

#otherId(type = nil) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 73

def otherId(type = nil)
  result = find_by_terms(:otherId).to_a
  if type.nil?
    result.collect { |n| [n['name'], n.text].join(':') }
  else
    result.select { |n| n['name'] == type }.collect(&:text)
  end
end

#previous_catkeysArray

Convenience method to get the previous catkeys (will be an array)

Returns:

  • (Array)

    previous catkey values (empty array if none found)



144
145
146
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 144

def previous_catkeys
  otherId(PREVIOUS_CATKEY_TYPE_ID)
end

#release_tagsNokogiri::XML::NodeSet

Helper method to get the release tags as a nodeset

Returns:

  • (Nokogiri::XML::NodeSet)

    all release tags and their attributes



169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 169

def release_tags
  release_tags = ng_xml.xpath('//release')
  return_hash = {}
  release_tags.each do |release_tag|
    hashed_node = release_tag_node_to_hash(release_tag)
    if !return_hash[hashed_node[:to]].nil?
      return_hash[hashed_node[:to]] << hashed_node[:attrs]
    else
      return_hash[hashed_node[:to]] = [hashed_node[:attrs]]
    end
  end
  return_hash
end

#remove_other_Id(type, val = nil) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 110

def remove_other_Id(type, val = nil)
  ng_xml.search('//otherId[@name=\'' + type + '\']')
        .select { |node| val.nil? || node.content == val }
        .each { ng_xml_will_change! }
        .each(&:remove)
        .any?
end

#sourceIdObject Also known as: source_id



47
48
49
50
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 47

def sourceId
  node = find_by_terms(:sourceId).first
  node ? [node['source'], node.text].join(':') : nil
end

#sourceId=(value) ⇒ String, Nil Also known as: source_id=

Note:

The actual values assigned will have leading/trailing whitespace stripped.

Returns The same value, as per Ruby convention for assignment operators.

Parameters:

  • value (String, Nil)

    The value to set or a nil/empty string to delete sourceId node

Returns:

  • (String, Nil)

    The same value, as per Ruby convention for assignment operators

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 56

def sourceId=(value)
  ng_xml_will_change!
  node = find_by_terms(:sourceId).first
  unless value.present? # so setting it to '' is the same as removal: worth documenting maybe?
    node&.remove
    return nil
  end
  parts = value.split(':', 2).map(&:strip)
  raise ArgumentError, "Source ID must follow the format 'namespace:value', not '#{value}'" unless
    parts.length == 2 && parts[0].present? && parts[1].present?

  node ||= ng_xml.root.add_child('<sourceId/>').first
  node['source'] = parts[0]
  node.content = parts[1]
end

#update_other_Id(type, new_val, val = nil) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 102

def update_other_Id(type, new_val, val = nil)
  ng_xml.search('//otherId[@name=\'' + type + '\']')
        .select { |node| val.nil? || node.content == val }
        .each { ng_xml_will_change! }
        .each { |node| node.content = new_val }
        .any?
end