class Dor::VersionMetadataDS

Public Class Methods

xml_template() click to toggle source

Default EventsDS xml

# File lib/dor/datastreams/version_metadata_ds.rb, line 67
def self.xml_template
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.versionMetadata do
      xml.version(versionId: '1', tag: '1.0.0') do
        xml.description 'Initial Version'
      end
    end
  end
  builder.doc
end

Public Instance Methods

current_description() click to toggle source

@return [String] The description for the current version

# File lib/dor/datastreams/version_metadata_ds.rb, line 191
def current_description
  desc_node = current_version_node.at_xpath('description')
  return desc_node.content if desc_node

  ''
end
current_tag() click to toggle source

@return [String] The tag for the newest version

# File lib/dor/datastreams/version_metadata_ds.rb, line 167
def current_tag
  current_version_node[:tag].to_s
end
current_version_closeable?() click to toggle source

@return [Boolean] returns true if the current version has a tag and a description, false otherwise

# File lib/dor/datastreams/version_metadata_ds.rb, line 157
def current_version_closeable?
  current = current_version_node
  if current[:tag] && current.at_xpath('description')
    true
  else
    false
  end
end
current_version_id() click to toggle source

@return [String] value of the most current versionId

# File lib/dor/datastreams/version_metadata_ds.rb, line 112
def current_version_id
  current_version = current_version_node
  if current_version.nil?
    '1'
  else
    current_version[:versionId].to_s
  end
end
description_for_version(versionId) click to toggle source

@return [String] The description for the specified version, or empty string if there is no description

# File lib/dor/datastreams/version_metadata_ds.rb, line 181
def description_for_version(versionId)
  nodes = ng_xml.search('//version[@versionId=\'' + versionId + '\']')
  if nodes.length == 1 && nodes.first.at_xpath('description')
    nodes.first.at_xpath('description').content.to_s
  else
    ''
  end
end
ensure_non_versionable() click to toggle source
# File lib/dor/datastreams/version_metadata_ds.rb, line 78
def ensure_non_versionable
  self.versionable = 'false'
end
increment_version(description = nil, significance = nil) click to toggle source

@param [String] description optional text describing version change @param [Symbol] significance optional which part of the version tag to increment

:major, :minor, :admin (see VersionTag#increment)
# File lib/dor/datastreams/version_metadata_ds.rb, line 85
def increment_version(description = nil, significance = nil)
  ng_xml_will_change!
  if find_by_terms(:version).size == 0
    v = ng_xml.create_element 'version',
                              versionId: '1', tag: '1.0.0'
    d = ng_xml.create_element 'description', 'Initial Version'
    ng_xml.root['objectId'] = pid
    ng_xml.root.add_child(v)
    v.add_child d
  else
    current = current_version_node
    current_id = current[:versionId].to_i
    current_tag = VersionTag.parse(current[:tag])

    v = ng_xml.create_element 'version', versionId: (current_id + 1).to_s
    v[:tag] = current_tag.increment(significance).to_s if significance && current_tag
    ng_xml.root['objectId'] = pid
    ng_xml.root.add_child(v)

    if description
      d = ng_xml.create_element 'description', description
      v.add_child d
    end
  end
end
prefix() click to toggle source

maintain AF < 8 indexing behavior

# File lib/dor/datastreams/version_metadata_ds.rb, line 221
def prefix
  ''
end
sync_then_increment_version(known_version, opts = {}) click to toggle source

Compares the current_version with the passed in known_version (usually SDRs version)

If the known_version is greater than the current version, then all version nodes greater than the known_version are removed,
then the current_version is incremented.  This repairs the case where a previous call to open a new verison
updates the versionMetadata datastream, but versioningWF is not initiated.  Prevents the versions from getting
out of synch with SDR

@param [Integer] known_version object version you wish to synch to, usually SDR's version @param [Hash] opts optional parameters @option opts [String] :description describes the version change @option opts [Symbol] :significance which part of the version tag to increment

# File lib/dor/datastreams/version_metadata_ds.rb, line 208
def sync_then_increment_version(known_version, opts = {})
  cv = current_version_id.to_i
  raise Dor::Exception, "Cannot sync to a version greater than current: #{cv}, requested #{known_version}" if cv < known_version

  while cv != known_version &&
        current_version_node.remove
    cv = current_version_id.to_i
  end

  increment_version(opts[:description], opts[:significance])
end
tag_for_version(versionId) click to toggle source
# File lib/dor/datastreams/version_metadata_ds.rb, line 171
def tag_for_version(versionId)
  nodes = ng_xml.search('//version[@versionId=\'' + versionId + '\']')
  if nodes.length == 1
    nodes.first['tag'].to_s
  else
    ''
  end
end
update_current_version(opts = {}) click to toggle source

@param [Hash] opts optional params @option opts [String] :description describes the version change @option opts [Symbol] :significance which part of the version tag to increment

:major, :minor, :admin (see VersionTag#increment)
# File lib/dor/datastreams/version_metadata_ds.rb, line 125
def update_current_version(opts = {})
  ng_xml.root['objectId'] = pid
  return if find_by_terms(:version).size == 1
  return if opts.empty?

  ng_xml_will_change!
  current = current_version_node
  if opts.include? :description
    d = current.at_xpath('description')
    if d
      d.content = opts[:description]
    else
      d_node = ng_xml.create_element 'description', opts[:description]
      current.add_child d_node
    end
  end
  if opts.include? :significance
    # tricky because if there is no tag, we have to find the newest
    if current[:tag].nil?
      current[:tag] = newest_tag.increment(opts[:significance]).to_s
    else
      # get rid of the current tag
      tags = find_by_terms(:version, :tag)
      sorted_tags = tags.map { |t| VersionTag.parse(t.value) }.sort
      current_tag = sorted_tags[sorted_tags.length - 2] # Get the second greatest tag since we are dropping the current, greatest
      current[:tag] = current_tag.increment(opts[:significance]).to_s
    end

  end
end

Private Instance Methods

current_version_node() click to toggle source

@return [Nokogiri::XML::Node] Node representing the current version

# File lib/dor/datastreams/version_metadata_ds.rb, line 228
def current_version_node
  versions = find_by_terms(:version)
  versions.max_by { |v| v[:versionId].to_i }
end
newest_tag() click to toggle source
# File lib/dor/datastreams/version_metadata_ds.rb, line 233
def newest_tag
  tags = find_by_terms(:version, :tag)
  tags.map { |t| VersionTag.parse(t.value) }.max
end