Class: Rubydora::Datastream

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Includes:
ActiveModel::Dirty, ExtensionParameters
Defined in:
lib/rubydora/datastream.rb

Overview

This class represents a Fedora datastream object and provides helper methods for creating and manipulating them.

Constant Summary collapse

DS_ATTRIBUTES =

mapping datastream attributes (and api parameters) to datastream profile names

{:controlGroup => :dsControlGroup, :dsLocation => :dsLocation, :altIDs => nil, :dsLabel => :dsLabel, :versionable => :dsVersionable, :dsState => :dsState, :formatURI => :dsFormatURI, :checksumType => :dsChecksumType, :checksum => :dsChecksum, :mimeType => :dsMIME, :logMessage => nil, :ignoreContent => nil, :lastModifiedDate => nil, :content => nil, :asOfDateTime => nil}
DS_DEFAULT_ATTRIBUTES =
{ :controlGroup => 'M', :dsState => 'A', :versionable => true }
DS_READONLY_ATTRIBUTES =
[ :dsCreateDate , :dsSize, :dsVersionID ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ExtensionParameters

#apply_extensions, included

Constructor Details

#initialize(digital_object, dsid, options = {}) ⇒ Datastream

Initialize a Rubydora::Datastream object, which may or may not already exist in the datastore.

Provides ‘after_initialize` callback for extensions

Parameters:

  • (Rubydora::DigitalObject)
  • Datastream (String)

    ID

  • default (Hash)

    attribute values (used esp. for creating new datastreams)



88
89
90
91
92
93
94
95
96
97
# File 'lib/rubydora/datastream.rb', line 88

def initialize digital_object, dsid, options = {}
  _run_initialize_callbacks do
    @digital_object = digital_object
    @dsid = dsid
    @options = options
    options.each do |key, value|
      self.send(:"#{key}=", value)
    end
  end
end

Instance Attribute Details

#digital_objectObject (readonly)

Returns the value of attribute digital_object.



15
16
17
# File 'lib/rubydora/datastream.rb', line 15

def digital_object
  @digital_object
end

#dsidObject (readonly)

Returns the value of attribute dsid.



15
16
17
# File 'lib/rubydora/datastream.rb', line 15

def dsid
  @dsid
end

Instance Method Details

#asOfDateTime(asOfDateTime = nil) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/rubydora/datastream.rb', line 71

def asOfDateTime asOfDateTime = nil
  if asOfDateTime == nil
    return @asOfDateTime
  end

  return self.class.new(@digital_object, @dsid, @options.merge(:asOfDateTime => asOfDateTime))
end

#contentString Also known as: read

Retrieve the content of the datastream (and cache it)

Returns:

  • (String)


112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rubydora/datastream.rb', line 112

def content
  begin
    options = { :pid => pid, :dsid => dsid }
    options[:asOfDateTime] = asOfDateTime if asOfDateTime

    @content ||= repository.datastream_dissemination options
  rescue RestClient::ResourceNotFound
  end

  content = @content.read and @content.rewind if @content.kind_of? IO
  content ||= @content
end

#content=(new_content) ⇒ String or IO

Set the content of the datastream

Parameters:

  • (String or IO)

Returns:

  • (String or IO)


137
138
139
140
# File 'lib/rubydora/datastream.rb', line 137

def content= new_content
   content_will_change!
   @content = new_content
end

#content_will_change!Object

Content_will_change! would be dynamically created by ActiveModel::Dirty, but it would eagerly load the content. We don’t want to do that.



144
145
146
147
# File 'lib/rubydora/datastream.rb', line 144

def content_will_change!
  raise "Can't change values on older versions" if @asOfDateTime
  changed_attributes[:content] = nil
end

#createRubydora::Datastream

Add datastream to Fedora



207
208
209
210
211
212
213
214
# File 'lib/rubydora/datastream.rb', line 207

def create
  check_if_read_only
  run_callbacks :create do
    repository.add_datastream to_api_params.merge({ :pid => pid, :dsid => dsid })
    reset_profile_attributes
    Datastream.new(digital_object, dsid)
  end
end

#deleteRubydora::Datastream

Purge the datastream from Fedora

Returns:



230
231
232
233
234
235
236
237
238
# File 'lib/rubydora/datastream.rb', line 230

def delete
  check_if_read_only
  run_callbacks :destroy do
    repository.purge_datastream(:pid => pid, :dsid => dsid) unless self.new?
    digital_object.datastreams.delete(dsid)
    reset_profile_attributes
    self
  end
end

#new?Boolean

Does this datastream already exist?

Returns:

  • (Boolean)


106
107
108
# File 'lib/rubydora/datastream.rb', line 106

def new?
  profile.empty?
end

#pidObject

Helper method to get digital object pid



100
101
102
# File 'lib/rubydora/datastream.rb', line 100

def pid
  digital_object.pid
end

#profile(opts = {}) ⇒ Hash

Retrieve the datastream profile as a hash (and cache it)

Parameters:

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

    :validateChecksum if you want fedora to validate the checksum

Returns:

  • (Hash)

    see Fedora #getDatastream documentation for keys



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rubydora/datastream.rb', line 152

def profile opts= {}
  if @profile && !(opts[:validateChecksum] && !@profile.has_key?('dsChecksumValid'))
    ## Force a recheck of the profile if they've passed :validateChecksum and we don't have dsChecksumValid
    return @profile
  end
  return @profile = {} unless digital_object.respond_to? :repository
  @profile = begin
    options = { :pid => pid, :dsid => dsid }
    options.merge!(opts)
    options[:asOfDateTime] = asOfDateTime if asOfDateTime
    options[:validateChecksum] = true if repository.config[:validateChecksum]
    self.profile_xml_to_hash(repository.datastream(options))
  rescue RestClient::Unauthorized => e
    raise e
  rescue RestClient::ResourceNotFound
    # the datastream is new
    {}
  end
end

#profile=(profile_xml) ⇒ Object



172
173
174
# File 'lib/rubydora/datastream.rb', line 172

def profile= profile_xml
  @profile = self.profile_xml_to_hash(profile_xml)
end

#profile_xml_to_hash(profile_xml) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/rubydora/datastream.rb', line 176

def profile_xml_to_hash profile_xml
  profile_xml.gsub! '<datastreamProfile', '<datastreamProfile xmlns="http://www.fedora.info/definitions/1/0/management/"' unless profile_xml =~ /xmlns=/
  doc = Nokogiri::XML(profile_xml)
  h = doc.xpath('/management:datastreamProfile/*', {'management' => "http://www.fedora.info/definitions/1/0/management/"} ).inject({}) do |sum, node|
               sum[node.name] ||= []
               sum[node.name] << node.text
               sum
             end
  h.select { |key, value| value.length == 1 }.each do |key, value|
    h[key] = value.first
  end

  h['dsSize'] &&= h['dsSize'].to_i rescue h['dsSize']
  h['dsCreateDate'] &&= Time.parse(h['dsCreateDate']) rescue h['dsCreateDate']
  h['dsChecksumValid'] &&= h['dsChecksumValid'] == 'true' 
  h['dsVersionable'] &&= h['dsVersionable'] == 'true' 
  h
end

#saveRubydora::Datastream

Modify or save the datastream



218
219
220
221
222
223
224
225
226
# File 'lib/rubydora/datastream.rb', line 218

def save
  check_if_read_only
  run_callbacks :save do
    return create if new?
    repository.modify_datastream to_api_params.merge({ :pid => pid, :dsid => dsid })
    reset_profile_attributes
    Datastream.new(digital_object, dsid)
  end
end

#urlString

Get the URL for the datastream content

Returns:

  • (String)


128
129
130
131
132
# File 'lib/rubydora/datastream.rb', line 128

def url
  options = { }
  options[:asOfDateTime] = asOfDateTime if asOfDateTime
  repository.datastream_url(pid, dsid, options) + "/content"
end

#versionsObject



195
196
197
198
199
200
201
202
203
# File 'lib/rubydora/datastream.rb', line 195

def versions
  versions_xml = repository.datastream_versions(:pid => pid, :dsid => dsid)
  return [] if versions_xml.nil?
  versions_xml.gsub! '<datastreamProfile', '<datastreamProfile xmlns="http://www.fedora.info/definitions/1/0/management/"' unless versions_xml =~ /xmlns=/
  doc = Nokogiri::XML(versions_xml)
  doc.xpath('//management:datastreamProfile', {'management' => "http://www.fedora.info/definitions/1/0/management/"} ).map do |ds|
    self.class.new @digital_object, @dsid, :profile => ds.to_s, :asOfDateTime => ds.xpath('management:dsCreateDate', 'management' => "http://www.fedora.info/definitions/1/0/management/").text
  end
end