Class: Dor::VersionMetadataDS

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.xml_templateObject

Default EventsDS xml



67
68
69
70
71
72
73
74
75
76
# File 'lib/dor/datastreams/version_metadata_ds.rb', line 67

def self.xml_template
  builder = Nokogiri::XML::Builder.new do |xml|
    xml. do
      xml.version(versionId: '1', tag: '1.0.0') do
        xml.description 'Initial Version'
      end
    end
  end
  builder.doc
end

Instance Method Details

#current_descriptionString

Returns The description for the current version.

Returns:

  • (String)

    The description for the current version



191
192
193
194
195
196
# 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_tagString

Returns The tag for the newest version.

Returns:

  • (String)

    The tag for the newest version



167
168
169
# File 'lib/dor/datastreams/version_metadata_ds.rb', line 167

def current_tag
  current_version_node[:tag].to_s
end

#current_version_closeable?Boolean

Returns true if the current version has a tag and a description, false otherwise

Returns:

  • (Boolean)

    returns true if the current version has a tag and a description, false otherwise



157
158
159
160
161
162
163
164
# 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')
    return true
  else
    return false
  end
end

#current_version_idString

Returns value of the most current versionId.

Returns:

  • (String)

    value of the most current versionId



112
113
114
115
116
117
118
119
# File 'lib/dor/datastreams/version_metadata_ds.rb', line 112

def current_version_id
  current_version = current_version_node
  if current_version.nil?
    return '1'
  else
    current_version[:versionId].to_s
  end
end

#description_for_version(versionId) ⇒ String

Returns The description for the specified version, or empty string if there is no description.

Returns:

  • (String)

    The description for the specified version, or empty string if there is no description



181
182
183
184
185
186
187
188
# 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_versionableObject



78
79
80
# File 'lib/dor/datastreams/version_metadata_ds.rb', line 78

def ensure_non_versionable
  self.versionable = 'false'
end

#increment_version(description = nil, significance = nil) ⇒ Object

Parameters:

  • description (String) (defaults to: nil)

    optional text describing version change

  • significance (Symbol) (defaults to: nil)

    optional which part of the version tag to increment :major, :minor, :admin (see VersionTag#increment)



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
# 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

#prefixObject

maintain AF < 8 indexing behavior



221
222
223
# File 'lib/dor/datastreams/version_metadata_ds.rb', line 221

def prefix
  ''
end

#sync_then_increment_version(known_version, opts = {}) ⇒ Object

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

Parameters:

  • known_version (Integer)

    object version you wish to synch to, usually SDR’s version

  • opts (Hash) (defaults to: {})

    optional parameters

Options Hash (opts):

  • :description (String)

    describes the version change

  • :significance (Symbol)

    which part of the version tag to increment

Raises:



208
209
210
211
212
213
214
215
216
217
218
# 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) ⇒ Object



171
172
173
174
175
176
177
178
# 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 = {}) ⇒ Object

Parameters:

  • opts (Hash) (defaults to: {})

    optional params

Options Hash (opts):

  • :description (String)

    describes the version change

  • :significance (Symbol)

    which part of the version tag to increment :major, :minor, :admin (see VersionTag#increment)



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# 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