Module: Cul::Hydra::Models::Common

Extended by:
ActiveSupport::Concern
Included in:
AdministrativeSet, Concept, DcDocument, GenericAggregator, GenericObject, GenericResource, METSStructuredAggregator, Resource, ResourceAggregator
Defined in:
app/models/concerns/cul/hydra/models/common.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#cmodel_pid(klass) ⇒ Object



74
75
76
# File 'app/models/concerns/cul/hydra/models/common.rb', line 74

def cmodel_pid(klass)
  klass.pid_namespace + ":" + klass.name.split("::")[-1]
end

#datastreams_in_fedoraObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/concerns/cul/hydra/models/common.rb', line 78

def datastreams_in_fedora
  mds = {}
  self.datastreams_xml['datastream'].each do |ds|
    dsid = ds["dsid"]
    ds.merge!({:pid => self.pid, :dsID => dsid, :dsLabel => ds["label"]})
    if dsid == "RELS-EXT"
      mds.merge!({dsid => ActiveFedora::RelsExtDatastream.new(ds)})
    else
      if self.class.ds_specs.has_key? dsid
        mds.merge!({dsid => self.class.ds_specs[dsid][0].new(ds)})
      else
        mds.merge!({dsid => ActiveFedora::Datastream.new(ds)})
      end
    end
    mds[dsid].new_object = false
  end
  mds
end

#get_representative_generic_resource(force_use_of_non_pid_identifier = false) ⇒ GenericResource

Return a representative file resource for the object This method generally shouldn’t be called with any parameters (unless we’re doing testing)

Parameters:

  • force_use_of_non_pid_identifier (Boolean) (defaults to: false)

    switch to require use of application id in struct map parsing

Returns:



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'app/models/concerns/cul/hydra/models/common.rb', line 186

def get_representative_generic_resource(force_use_of_non_pid_identifier=false)
  return self if self.is_a?(GenericResource)

  # if there's an explicit assignment of representative image, return it
  assigned_image = get_singular_rel(:schema_image)
  return ActiveFedora::Base.find(assigned_image.split('/')[-1]) if assigned_image

  return nil unless self.is_a?(Cul::Hydra::Models::Aggregator) # Only Aggregators have struct metadata
  # If we're here, then the object was not a Generic resource.
  # Try to get child info from a structMat datastream, and fall back to
  # the first :cul_member_of child if a structMap isn't present

  # Check for the presence of a structMap and get first GenericResource in that structMap
  if self.has_struct_metadata?
    begin
      struct = Cul::Hydra::Datastreams::StructMetadata.from_xml(self.datastreams['structMetadata'].content)
    rescue Rubydora::FedoraInvalidRequest => e
      Rails.logger.error "Error: Problem accessing struct datastream data in #{self.pid}" # More specific error notice
      raise e
    end
    ng_div = struct.first_ordered_content_div #Nokogiri node response
    found_struct_div = (! ng_div.nil?)
  else
    found_struct_div = false
  end

  if found_struct_div
    content_ids = ng_div.attr('CONTENTIDS').split(' ') # Get all space-delimited content ids
    child_obj = nil

    # Try to do a PID lookup first
    unless force_use_of_non_pid_identifier
      content_ids.each do |content_id|
        next unless content_id.match(/^([A-Za-z0-9]|-|\.)+:(([A-Za-z0-9])|-|\.|~|_|(%[0-9A-F]{2}))+$/) # Don't do a lookup on identifiers that can't possibly be valid PID (otherwise we'd encounter an error)
        child_obj ||= ActiveFedora::Base.exists?(content_id) ? ActiveFedora::Base.find(content_id) : nil
      end
    end

    # Then fall back to identifier lookup
    if child_obj.nil?
      child_pid = nil
      content_ids.each do |content_id|
        child_pid ||= Cul::Hydra::RisearchMembers.get_pid_for_identifier(content_id)
        if force_use_of_non_pid_identifier && child_pid && content_id == child_pid
          # This really only runs when we're doing testing, if we want to specifically ensure that we're searching by a non-pid identifier
          child_pid = nil
        end
      end

      if child_pid
        child_obj = ActiveFedora::Base.find(child_pid)
      end
    end

    if child_obj
      # Recursion!  Woo!
      return child_obj.get_representative_generic_resource(force_use_of_non_pid_identifier)
    else
      #Rails.logger.error "No object for dc:identifier in #{content_ids.inspect}"
      return nil
    end
  else
    # If there isn't a structMap, just get the first child
    member_pids = Cul::Hydra::RisearchMembers.get_direct_member_pids(self.pid, true)
    logger.warn "Warning: #{self.pid} is a member of itself!" if member_pids.include?(self.pid)
    if member_pids.first
      child_obj = ActiveFedora::Base.find(member_pids.first)
      return child_obj.get_representative_generic_resource
    else
      #Rails.logger.error "No child objects or resources for #{self.pid}"
      return nil
    end
  end
rescue ActiveFedora::ObjectNotFoundError
  logger.warn "#{get_singular_rel(:schema_image)} not found in repository for #{self.pid}"
  return nil
end

#get_singular_rel(predicate) ⇒ Object



292
293
294
295
296
# File 'app/models/concerns/cul/hydra/models/common.rb', line 292

def get_singular_rel(predicate)
  property = relationships(predicate).first
  return nil unless property
  return (property.kind_of? RDF::Literal) ? property.value : property
end

#has_desc?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
109
110
111
# File 'app/models/concerns/cul/hydra/models/common.rb', line 101

def has_desc?
  has_desc = false
  begin
    has_desc = self.datastreams.include? "descMetadata"
    has_desc = has_desc and self.inner_object.datastreams["descMetadata"].content.length > 0
    has_desc = has_desc and self.datastreams["descMetadata"].term_values(:identifier).length > 0
  rescue
    has_desc = false
  end
  has_desc
end

#initialize(attrs = nil) ⇒ Object



68
69
70
71
72
# File 'app/models/concerns/cul/hydra/models/common.rb', line 68

def initialize(attrs = nil)
  attrs = {} if attrs.nil?
  attrs[:namespace] = self.class.pid_namespace unless attrs[:namespace]
  super
end

#label=(new_label) ⇒ Object

A Fedora object label can only contain a string value of up to 255 characters. If we try to set a longer value, it causes errors upon object save. Truncate labels to 255 characters. Note: this method maps to a method_missing hanlder that converts input into a String, so we use the super method first, and then post-process the output of that super method call.



59
60
61
62
# File 'app/models/concerns/cul/hydra/models/common.rb', line 59

def label=(new_label)
  super(new_label)
  super(self.label[0,255])
end

#rdf_typeObject



64
65
66
# File 'app/models/concerns/cul/hydra/models/common.rb', line 64

def rdf_type
  relationships[:rdf_type]
end

#rdf_types!Object



46
47
48
49
50
51
52
# File 'app/models/concerns/cul/hydra/models/common.rb', line 46

def rdf_types!
  self.class.rdf_types.each do |type|
    add_relationship(:rdf_type, type.to_s)
  end
  @metadata_is_dirty = true
  self.save
end

#route_asObject



97
98
99
# File 'app/models/concerns/cul/hydra/models/common.rb', line 97

def route_as
  "default"
end

#set_singular_rel(predicate, value, literal = false) ⇒ Object



298
299
300
301
302
# File 'app/models/concerns/cul/hydra/models/common.rb', line 298

def set_singular_rel(predicate, value, literal=false)
  raise "#{predicate} is a singular property" if value.respond_to? :each
  clear_relationship(predicate)
  add_relationship(predicate, value, literal) unless value.nil? || value.empty?
end

#set_size_labels(solr_doc = {}) ⇒ Object

set the index type label and any RI-based fields



114
115
116
# File 'app/models/concerns/cul/hydra/models/common.rb', line 114

def set_size_labels(solr_doc={})
  solr_doc["index_type_label_ssi"] = [self.index_type_label]
end

#thumbnail_infoObject



288
289
290
# File 'app/models/concerns/cul/hydra/models/common.rb', line 288

def thumbnail_info
  {:asset=>("cul_hydra/crystal/kmultiple.png"),:mime_type=>"image/png"}
end

#to_solr(solr_doc = Hash.new, opts = {}) ⇒ Object



118
119
120
121
122
123
124
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/models/concerns/cul/hydra/models/common.rb', line 118

def to_solr(solr_doc = Hash.new, opts={})
  solr_doc = super(solr_doc, opts)

  if has_desc?
    solr_doc["descriptor_ssi"] = ["mods"]
  else
    solr_doc["descriptor_ssi"] = ["dublin core"]
  end
  # if no mods, pull some values from DC
  if (solr_doc["title_display_ssm"].nil? or solr_doc["title_display_ssm"].length == 0)
    if self.datastreams["DC"].term_values(:dc_title).first
      solr_doc["title_display_ssm"] = self.datastreams["DC"].term_values(:dc_title)
    else
      solr_doc["title_display_ssm"] = self.datastreams["DC"].term_values(:dc_identifier).reject {|dcid| dcid.eql? self.id}
    end
    solr_doc["title_si"] = self.datastreams["DC"].term_values(:dc_title).first
    if self.datastreams["DC"].term_values(:dc_relation).first
      self.datastreams["DC"].term_values(:dc_relation).each {|val|
        if val =~ /clio:/
          solr_doc["clio_ssim"] ||= []
          solr_doc["clio_ssim"] << val.split(':')[-1]
        end
      }
    end
  end
  if (solr_doc["identifier_ssim"].nil? or solr_doc["identifier_ssim"].length == 0)
      solr_doc["identifier_ssim"] = self.datastreams["DC"].term_values(:dc_identifier).reject {|dcid| dcid.eql? self.id}
  end
  if (solr_doc["title_display_ssm"].length > 1)
    solr_doc["title_display_ssm"].uniq!
  end

  if solr_doc["contributor_ssim"].present?
    if solr_doc["contributor_ssim"].is_a?(Array)
      solr_doc["contributor_first_si"] = solr_doc["contributor_ssim"].first
    else
      solr_doc["contributor_first_si"] = solr_doc["contributor_ssim"]
    end
  end


  solr_doc["format_ssi"] = [self.route_as]

  set_size_labels(solr_doc)

  solr_doc.each_pair {|key, value|
    if value.is_a? Array
      value.each {|v| v.strip! unless v.nil? }
    elsif value.is_a? String
      value.strip!
    end
  }
  solr_doc[:structured_bsi] = 'false' unless solr_doc.has_key? :structured_bsi

  representative_child = get_representative_generic_resource
  solr_doc['representative_generic_resource_pid_ssi'] = representative_child.pid unless representative_child.nil?

  # Index URI form of pid to facilitate solr joins
  solr_doc['fedora_pid_uri_ssi'] = 'info:fedora/' + self.pid if self.pid.present?
  solr_doc['datastreams_ssim'] = self.datastreams.keys.map {|k| k.to_s }.sort

  solr_doc
end

#update_datastream_attributes(params = {}, opts = {}) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'app/models/concerns/cul/hydra/models/common.rb', line 264

def update_datastream_attributes(params={}, opts={})
  logger.debug "Common.update_datastream_attributes"
  result = params.dup
  params.each_pair do |dsid, ds_params|
    if datastreams.include?(dsid)
      verify_params = ds_params.dup
      changed = false
      verify_params.each { |pointer, values|
        changed ||= value_changed?(datastreams[dsid],pointer,values)
      }
      if changed
        logger.debug "Common.update_datastream_attributes calling update_indexed_attributes"
        result[dsid] = datastreams[dsid].update_indexed_attributes(ds_params)
      else
        result[dsid] = no_update(ds_params)
      end
      logger.debug "change detected? #{changed} digital_object? #{datastreams[dsid].changed?}"
    else
      result.delete(dsid)
    end
  end
  return result
end