Module: Dor::Describable
- Extended by:
- ActiveSupport::Concern
- Included in:
- AdminPolicyObject, Collection, Publishable, Set
- Defined in:
- lib/dor/models/describable.rb
Defined Under Namespace
Classes: CrosswalkError
Constant Summary collapse
- DESC_MD_FORMATS =
{ 'http://www.tei-c.org/ns/1.0' => 'tei', 'http://www.loc.gov/mods/v3' => 'mods' }
Class Method Summary collapse
Instance Method Summary collapse
-
#add_access_conditions(doc) ⇒ Object
Create MODS accessCondition statements from rightsMetadata.
-
#add_collection_reference(doc) ⇒ Void
Adds to desc metadata a relatedItem with information about the collection this object belongs to.
-
#add_constituent_relations(doc) ⇒ Void
expand constituent relations into relatedItem references – see JUMBO-18.
- #add_identifier(type, value) ⇒ Object
- #build_descMetadata_datastream(ds) ⇒ Object
- #delete_identifier(type, value = nil) ⇒ Object
- #fetch_descMetadata_datastream ⇒ Object
-
#generate_dublin_core ⇒ Nokogiri::Doc
Generates Dublin Core from the MODS in the descMetadata datastream using the LoC mods2dc stylesheet Should not be used for the Fedora DC datastream.
-
#generate_public_desc_md ⇒ String
Public descriptive medatada XML.
- #metadata_format ⇒ Object
- #metadata_namespace ⇒ Object
-
#set_desc_metadata_using_label(force = false) ⇒ String
DescMetadata.content XML.
-
#stanford_mods(content = nil, ns_aware = true) ⇒ Object
intended for read-access, “as SearchWorks would see it”, mostly for to_solr().
- #to_solr(solr_doc = {}, *args) ⇒ Object
- #update_title(new_title) ⇒ Object
Class Method Details
.get_collection_title(obj) ⇒ Object
296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/dor/models/describable.rb', line 296 def self.get_collection_title(obj) xml = obj.descMetadata.ng_xml title = '' title_node = xml.at_xpath('//mods:mods/mods:titleInfo/mods:title', 'mods' => 'http://www.loc.gov/mods/v3') if title_node title = title_node.content subtitle = xml.at_xpath('//mods:mods/mods:titleInfo/mods:subTitle', 'mods' => 'http://www.loc.gov/mods/v3') title += " (#{subtitle.content})" if subtitle end title end |
Instance Method Details
#add_access_conditions(doc) ⇒ Object
this method modifies the passed in doc
Create MODS accessCondition statements from rightsMetadata
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/dor/models/describable.rb', line 73 def add_access_conditions(doc) # clear out any existing accessConditions doc.xpath('//mods:accessCondition', 'mods' => 'http://www.loc.gov/mods/v3').each {|n| n.remove} rights = datastreams['rightsMetadata'].ng_xml rights.xpath('//use/human[@type="useAndReproduction"]').each do |use| txt = use.text.strip next if txt.empty? doc.root.element_children.last.add_next_sibling doc.create_element('accessCondition', txt, :type => 'useAndReproduction') end rights.xpath('//copyright/human[@type="copyright"]').each do |cr| txt = cr.text.strip next if txt.empty? doc.root.element_children.last.add_next_sibling doc.create_element('accessCondition', txt, :type => 'copyright') end rights.xpath("//use/machine[#{ci_compare('type', 'creativecommons')}]").each do |lic_type| next if lic_type.text =~ /none/i lic_text = rights.at_xpath("//use/human[#{ci_compare('type', 'creativecommons')}]").text.strip next if lic_text.empty? new_text = "CC #{lic_type.text}: #{lic_text}" doc.root.element_children.last.add_next_sibling doc.create_element('accessCondition', new_text, :type => 'license') end rights.xpath("//use/machine[#{ci_compare('type', 'opendatacommons')}]").each do |lic_type| next if lic_type.text =~ /none/i lic_text = rights.at_xpath("//use/human[#{ci_compare('type', 'opendatacommons')}]").text.strip next if lic_text.empty? new_text = "ODC #{lic_type.text}: #{lic_text}" doc.root.element_children.last.add_next_sibling doc.create_element('accessCondition', new_text, :type => 'license') end end |
#add_collection_reference(doc) ⇒ Void
this method modifies the passed in doc
Adds to desc metadata a relatedItem with information about the collection this object belongs to. For use in published mods and mods-to-DC conversion.
109 110 111 112 113 114 115 116 117 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 |
# File 'lib/dor/models/describable.rb', line 109 def add_collection_reference(doc) return unless methods.include? :public_relationships collections = public_relationships.search('//rdf:RDF/rdf:Description/fedora:isMemberOfCollection', 'fedora' => 'info:fedora/fedora-system:def/relations-external#', 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' ) return if collections.empty? # Remove any existing collections in the descMetadata doc.search('/mods:mods/mods:relatedItem[@type="host"]/mods:typeOfResource[@collection=\'yes\']', 'mods' => 'http://www.loc.gov/mods/v3').each do |node| node.parent.remove end collections.each do |collection_node| druid = collection_node['rdf:resource'] druid = druid.gsub('info:fedora/', '') collection_obj = Dor::Item.find(druid) collection_title = Dor::Describable.get_collection_title(collection_obj) = Nokogiri::XML::Node.new('relatedItem', doc) ['type'] = 'host' title_info_node = Nokogiri::XML::Node.new('titleInfo', doc) title_node = Nokogiri::XML::Node.new('title', doc) title_node.content = collection_title # e.g.: # <location> # <url>http://purl.stanford.edu/rh056sr3313</url> # </location> loc_node = doc.create_element('location') url_node = doc.create_element('url') url_node.content = "https://#{Dor::Config.stacks.document_cache_host}/#{druid.split(':').last}" loc_node << url_node type_node = Nokogiri::XML::Node.new('typeOfResource', doc) type_node['collection'] = 'yes' doc.root.add_child() .add_child(title_info_node) title_info_node.add_child(title_node) .add_child(loc_node) .add_child(type_node) end end |
#add_constituent_relations(doc) ⇒ Void
expand constituent relations into relatedItem references – see JUMBO-18
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 181 182 183 184 |
# File 'lib/dor/models/describable.rb', line 154 def add_constituent_relations(doc) public_relationships.search('//rdf:RDF/rdf:Description/fedora:isConstituentOf', 'fedora' => 'info:fedora/fedora-system:def/relations-external#', 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' ).each do |parent| # fetch the parent object to get title druid = parent['rdf:resource'].gsub(/^info:fedora\//, '') parent_item = Dor::Item.find(druid) # create the MODS relation = doc.create_element 'relatedItem' ['type'] = 'host' ['displayLabel'] = 'Appears in' # load the title from the parent's DC.title titleInfo = doc.create_element 'titleInfo' title = doc.create_element 'title' title.content = parent_item.datastreams['DC'].title.first titleInfo << title << titleInfo # point to the PURL for the parent location = doc.create_element 'location' url = doc.create_element 'url' url.content = "http://#{Dor::Config.stacks.document_cache_host}/#{druid.split(':').last}" location << url << location # finish up by adding relation to public MODS doc.root << end end |
#add_identifier(type, value) ⇒ Object
258 259 260 261 262 263 264 265 266 |
# File 'lib/dor/models/describable.rb', line 258 def add_identifier(type, value) ds_xml = descMetadata.ng_xml ds_xml.search('//mods:mods', 'mods' => 'http://www.loc.gov/mods/v3').each do |node| new_node = Nokogiri::XML::Node.new('identifier', ds_xml) # this ends up being mods:identifier without having to specify the namespace new_node['type'] = type new_node.content = value node.add_child(new_node) end end |
#build_descMetadata_datastream(ds) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/dor/models/describable.rb', line 33 def build_descMetadata_datastream(ds) content = fetch_descMetadata_datastream return nil if content.nil? ds.dsLabel = 'Descriptive Metadata' ds.ng_xml = Nokogiri::XML(content) ds.ng_xml.normalize_text! ds.content = ds.ng_xml.to_xml end |
#delete_identifier(type, value = nil) ⇒ Object
268 269 270 271 272 273 274 275 276 277 |
# File 'lib/dor/models/describable.rb', line 268 def delete_identifier(type, value = nil) ds_xml = descMetadata.ng_xml ds_xml.search('//mods:identifier', 'mods' => 'http://www.loc.gov/mods/v3').each do |node| if node.content == value || value.nil? node.remove return true end end false end |
#fetch_descMetadata_datastream ⇒ Object
27 28 29 30 31 |
# File 'lib/dor/models/describable.rb', line 27 def fetch_descMetadata_datastream candidates = datastreams['identityMetadata'].otherId.collect { |oid| oid.to_s } = Dor::MetadataService.resolvable(candidates).first .nil? ? nil : Dor::MetadataService.fetch(.to_s) end |
#generate_dublin_core ⇒ Nokogiri::Doc
Generates Dublin Core from the MODS in the descMetadata datastream using the LoC mods2dc stylesheet
Should not be used for the Fedora DC datastream
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/dor/models/describable.rb', line 46 def generate_dublin_core raise CrosswalkError, "Unknown descMetadata namespace: #{.inspect}" if .nil? xslt = Nokogiri::XSLT(File.new(File.(File.dirname(__FILE__) + "/#{}2dc.xslt")) ) desc_md = descMetadata.ng_xml.dup(1) add_collection_reference(desc_md) dc_doc = xslt.transform(desc_md) dc_doc.xpath('/oai_dc:dc/*[count(text()) = 0]').remove # Remove empty nodes raise CrosswalkError, "Dor::Item#generate_dublin_core produced incorrect xml (no root):\n#{dc_doc.to_xml}" if dc_doc.root.nil? raise CrosswalkError, "Dor::Item#generate_dublin_core produced incorrect xml (no children):\n#{dc_doc.to_xml}" if dc_doc.root.children.size == 0 dc_doc end |
#generate_public_desc_md ⇒ String
Returns Public descriptive medatada XML.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/dor/models/describable.rb', line 59 def generate_public_desc_md doc = descMetadata.ng_xml.dup(1) add_collection_reference(doc) add_access_conditions(doc) add_constituent_relations(doc) doc.xpath('//comment()').remove new_doc = Nokogiri::XML(doc.to_xml) { |x| x.noblanks } new_doc.encoding = 'UTF-8' new_doc.to_xml end |
#metadata_format ⇒ Object
192 193 194 |
# File 'lib/dor/models/describable.rb', line 192 def DESC_MD_FORMATS[] end |
#metadata_namespace ⇒ Object
186 187 188 189 190 |
# File 'lib/dor/models/describable.rb', line 186 def desc_md = datastreams['descMetadata'].ng_xml return nil if desc_md.nil? || desc_md.root.nil? || desc_md.root.namespace.nil? desc_md.root.namespace.href end |
#set_desc_metadata_using_label(force = false) ⇒ String
Returns descMetadata.content XML.
281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/dor/models/describable.rb', line 281 def (force = false) unless force || descMetadata.new? raise 'Cannot proceed, there is already content in the descriptive metadata datastream: ' + descMetadata.content.to_s end label = self.label builder = Nokogiri::XML::Builder.new { |xml| xml.mods( 'xmlns' => 'http://www.loc.gov/mods/v3', 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', :version => '3.3', 'xsi:schemaLocation' => 'http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-3.xsd') { xml.titleInfo { xml.title label } } } descMetadata.content = builder.to_xml end |
#stanford_mods(content = nil, ns_aware = true) ⇒ Object
intended for read-access, “as SearchWorks would see it”, mostly for to_solr()
20 21 22 23 24 25 |
# File 'lib/dor/models/describable.rb', line 20 def stanford_mods(content = nil, ns_aware = true) m = Stanford::Mods::Record.new desc = content.nil? ? descMetadata.ng_xml : content m.from_nk_node(desc.root, ns_aware) m end |
#to_solr(solr_doc = {}, *args) ⇒ Object
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 |
# File 'lib/dor/models/describable.rb', line 196 def to_solr(solr_doc = {}, *args) super solr_doc, *args mods_sources = { 'sw_language_ssim' => :sw_language_facet, 'sw_language_tesim' => :sw_language_facet, 'sw_genre_ssim' => :sw_genre, 'sw_genre_tesim' => :sw_genre, 'sw_format_ssim' => :format_main, # basically sw_typeOfResource_ssim 'sw_format_tesim' => :format_main, # basically sw_typeOfResource_tesim 'sw_subject_temporal_ssim' => :era_facet, 'sw_subject_temporal_tesim' => :era_facet, 'sw_subject_geographic_ssim' => :geographic_facet, 'sw_subject_geographic_tesim' => :geographic_facet, 'mods_typeOfResource_ssim' => [:term_values, :typeOfResource], 'mods_typeOfResource_tesim' => [:term_values, :typeOfResource] } keys = mods_sources.keys.concat(%w( metadata_format_ssim )) keys.each { |key| solr_doc[key] ||= [] # initialize multivalue targts if necessary } solr_doc['metadata_format_ssim'] << begin dc_doc = generate_dublin_core dc_doc.xpath('/oai_dc:dc/*').each do |node| add_solr_value(solr_doc, "public_dc_#{node.name}", node.text, :string, [:stored_searchable]) end creator = '' dc_doc.xpath('//dc:creator').each do |node| creator = node.text end title = '' dc_doc.xpath('//dc:title').each do |node| title = node.text end creator_title = creator + title add_solr_value(solr_doc, 'creator_title', creator_title , :string, [:stored_sortable]) rescue CrosswalkError => e ActiveFedora.logger.warn "Cannot index #{pid}.descMetadata: #{e.}" end begin mods = stanford_mods mods_sources.each_pair do |solr_key, meth| vals = meth.is_a?(Array) ? mods.send(meth.shift, *meth) : mods.send(meth) solr_doc[solr_key].push *vals unless vals.nil? || vals.empty? # asterisk to avoid multi-dimensional array: push values, not the array end solr_doc['sw_pub_date_sort_ssi' ] = mods.pub_date_sort # e.g. '0800' solr_doc['sw_pub_date_facet_ssi'] = mods.pub_date_facet # e.g. '9th century' end # some fields get explicit "(none)" placeholder values, mostly for faceting %w(sw_language_tesim sw_genre_tesim sw_format_tesim).each { |key| solr_doc[key] = ['(none)'] if solr_doc[key].empty? } # otherwise remove empties keys.each { |key| solr_doc.delete(key) if solr_doc[key].nil? || solr_doc[key].empty?} solr_doc end |
#update_title(new_title) ⇒ Object
254 255 256 |
# File 'lib/dor/models/describable.rb', line 254 def update_title(new_title) raise 'Descriptive metadata has no title to update!' unless update_simple_field('mods:mods/mods:titleInfo/mods:title', new_title) end |