Skip to content

Commit

Permalink
Merge pull request #602 from scientist-softserv/uri-to-human-readable…
Browse files Browse the repository at this point in the history
…-string

🎁 Convert URI to human readable strings
  • Loading branch information
kirkkwang committed Feb 28, 2024
1 parent ce7276e commit 552e66a
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 11 deletions.
54 changes: 54 additions & 0 deletions app/indexers/allinson_flex/dynamic_indexer_behavior.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

# OVERRIDE AllinsonFlex v0.1.0 to turn URI's into human readable strings
#
# For whatever reason our decorator pattern was not overriding #generate_solr_document
# If you check AllinsonFlex::DynamicIndexerBehavior.ancestors, you get what you expect
# with a decorator. If you checked the #source_location of :generate_solr_document
# you also get what you expect. However, when running a reindex, I was not going
# through the decorator. I decided since this is only one method, overriding the entire
# mixin was acceptable for now.

module AllinsonFlex
module DynamicIndexerBehavior
include UriToStringBehavior
extend ActiveSupport::Concern

RANGE = "http://www.w3.org/2001/XMLSchema#anyURI"

included do
class_attribute :model_class
end

def generate_solr_document
dynamic_schema_service = object.dynamic_schema_service
uri_properties = uri_properties_from(dynamic_schema_service)

super.tap do |solr_doc|
dynamic_schema_service.indexing_properties.each_pair do |prop_name, index_field_names|
value = if uri_properties.include?(prop_name.to_s)
uri_to_value_for(object.send(prop_name))
else
object.send(prop_name)
end

index_field_names.each { |index_field_name| solr_doc[index_field_name] = value }
end
end
end

private

def uri_properties_from(dynamic_schema_service)
schema = HashWithIndifferentAccess.new(dynamic_schema_service.dynamic_schema.schema)
props_hash = schema[:properties]
# remove rdf_type since they are also URIs but not for our purposes
props_hash.keys.select { |k, _v| props_hash[k][:range] == RANGE } - local_authorities - ['rdf_type']
end

def local_authorities
# Hyku has these pluralized while m3 has these singularized
Qa::Authorities::Local.names.map(&:singularize)
end
end
end
5 changes: 3 additions & 2 deletions app/indexers/app_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class AppIndexer < Hyrax::WorkIndexer
# Utk does not include based_near and does not need deep indexing.
# include Hyrax::IndexesLinkedMetadata

include AllinsonFlex::DynamicIndexerBehavior

# Uncomment this block if you want to add custom indexing behavior:
def generate_solr_document
super.tap do |solr_doc|
Expand All @@ -24,7 +26,6 @@ def generate_solr_document
private

def all_creators
props = SolrDocument.creator_fields
props.map { |prop| Array(object.try(prop)) }.flatten.compact
SolrDocument.creator_fields.map { |prop| uri_to_value_for(object.try(prop)) }.flatten.compact
end
end
1 change: 0 additions & 1 deletion app/indexers/attachment_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ class AttachmentIndexer < AppIndexer
# solr_doc['my_custom_field_ssim'] = object.my_custom_property
# end
# end
include AllinsonFlex::DynamicIndexerBehavior
self.model_class = ::Attachment
end
1 change: 0 additions & 1 deletion app/indexers/audio_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ class AudioIndexer < AppIndexer
# solr_doc['my_custom_field_ssim'] = object.my_custom_property
# end
# end
include AllinsonFlex::DynamicIndexerBehavior
self.model_class = ::Audio
end
1 change: 0 additions & 1 deletion app/indexers/book_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ class BookIndexer < AppIndexer
# solr_doc['my_custom_field_ssim'] = object.my_custom_property
# end
# end
include AllinsonFlex::DynamicIndexerBehavior
self.model_class = ::Book
end
1 change: 0 additions & 1 deletion app/indexers/compound_object_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ class CompoundObjectIndexer < AppIndexer
# solr_doc['my_custom_field_ssim'] = object.my_custom_property
# end
# end
include AllinsonFlex::DynamicIndexerBehavior
self.model_class = ::CompoundObject
end
1 change: 0 additions & 1 deletion app/indexers/generic_work_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ class GenericWorkIndexer < AppIndexer
# solr_doc['my_custom_field_ssim'] = object.my_custom_property
# end
# end
include AllinsonFlex::DynamicIndexerBehavior
self.model_class = ::GenericWork
end
1 change: 0 additions & 1 deletion app/indexers/image_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ class ImageIndexer < AppIndexer
# solr_doc['my_custom_field_ssim'] = object.my_custom_property
# end
# end
include AllinsonFlex::DynamicIndexerBehavior
self.model_class = ::Image
end
1 change: 0 additions & 1 deletion app/indexers/newspaper_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ class NewspaperIndexer < AppIndexer
# solr_doc['my_custom_field_ssim'] = object.my_custom_property
# end
# end
include AllinsonFlex::DynamicIndexerBehavior
self.model_class = ::Newspaper
end
1 change: 0 additions & 1 deletion app/indexers/pdf_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ class PdfIndexer < AppIndexer
# solr_doc['my_custom_field_ssim'] = object.my_custom_property
# end
# end
include AllinsonFlex::DynamicIndexerBehavior
self.model_class = ::Pdf
end
40 changes: 40 additions & 0 deletions app/indexers/uri_to_string_behavior.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module UriToStringBehavior
extend ActiveSupport::Concern

# UTK uses this label to house the value that needs to be rendered
LABEL = "http://www.w3.org/2004/02/skos/core#prefLabel"

# Retrieves a value for a given URI.
#
# @param value [String] the value to retrieve. If this value starts with 'http', it is treated as a URI.
# @return [String]
#
# @example
# uri_to_value_for('http://example.com') #=> "Failed to load RDF data: ..."
# uri_to_value_for('http://id.loc.gov/authorities/names/n2017180154') #=> "University of Tennessee"
# uri_to_value_for('Doe, John') #=> "Doe, John"
def uri_to_value_for(value)
return value.map { |v| uri_to_value_for(v) } if value.is_a?(Enumerable)
return if value.blank?
return value unless value.is_a?(String)
return value unless value.start_with?('http')

uri = value

begin
graph = RDF::Graph.load(uri)
rescue StandardError => e
Rails.logger.error("Failed to load RDF data: #{e.message}")
return "#{uri} (Failed to load URI)"
end

subject = RDF::URI.new(uri)
predicate = RDF::URI.new(LABEL)
object = graph.query([subject, predicate, nil]).objects.first
return "#{uri} (No label found)" if object.blank?

object.to_s
end
end
1 change: 0 additions & 1 deletion app/indexers/video_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ class VideoIndexer < AppIndexer
# solr_doc['my_custom_field_ssim'] = object.my_custom_property
# end
# end
include AllinsonFlex::DynamicIndexerBehavior
self.model_class = ::Video
end
Loading

0 comments on commit 552e66a

Please sign in to comment.