Skip to content
This repository has been archived by the owner on May 14, 2022. It is now read-only.

Commit

Permalink
Index and use literals in IIIF manifests
Browse files Browse the repository at this point in the history
Closes #577
  • Loading branch information
tpendragon committed Aug 26, 2016
1 parent 687d413 commit e9b3e04
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 4 deletions.
17 changes: 17 additions & 0 deletions app/indexers/work_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ def generate_solr_document
object.member_of_collections.each do |col|
solr_doc[Solrizer.solr_name('member_of_collection_slugs', :symbol)] = col.exhibit_id
end
(PlumSchema.display_fields + [:title]).each do |field|
objects = object.get_values(field, literal: true)
statements = objects.map do |obj|
::RDF::Statement.from([object.rdf_subject, ::RDF::URI(""), obj])
end
output = JSON::LD::API.fromRdf(statements)
next unless output.length > 0
output = output[0][""]
output.map! do |object|
if object.is_a?(Hash) && object["@value"] && object.keys.length == 1
object["@value"]
else
object.to_json
end
end
solr_doc[Solrizer.solr_name("#{field}_literals", :symbol)] = output
end
end
end
end
8 changes: 8 additions & 0 deletions app/models/solr_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ def collection
def method_missing(meth_name, *args, &block)
if ScannedResource.properties.values.map(&:term).include?(meth_name)
self[Solrizer.solr_name(meth_name.to_s)]
elsif ScannedResource.properties.values.map { |x| "#{x.term}_literals".to_sym }.include?(meth_name)
Array(self[Solrizer.solr_name(meth_name.to_s, :symbol)]).map do |x|
if x.start_with?("{")
JSON.parse(x)
else
x
end
end
else
super
end
Expand Down
1 change: 1 addition & 0 deletions app/presenters/curation_concerns_show_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class CurationConcernsShowPresenter < CurationConcerns::WorkShowPresenter
delegate :viewing_hint, :viewing_direction, :state, :type, :identifier, :workflow_note, :logical_order, :logical_order_object, :ocr_language, :thumbnail_id, :source_metadata_identifier, :collection, to: :solr_document
delegate :flaggable?, to: :state_badge_instance
delegate(*ScannedResource.properties.values.map(&:term), to: :solr_document, allow_nil: true)
delegate(*ScannedResource.properties.values.map { |x| "#{x.term}_literals" }, to: :solr_document, allow_nil: true)

def state_badge
state_badge_instance.render
Expand Down
2 changes: 1 addition & 1 deletion app/schemas/plum_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ class PlumSchema < ActiveTriples::Schema
# Ignore things like admin data (workflow note), title, description, etc, as
# those have custom display logic.
def self.display_fields
ScannedResource.properties.values.map(&:term) - [:description, :state, :rights_statement, :holding_location, :title, :depositor, :source_metadata_identifier, :source_metadata, :date_modified, :date_uploaded, :workflow_note, :nav_date, :pdf_type, :ocr_language, :keyword] - IIIFBookSchema.properties.map(&:name)
ScannedResource.properties.values.map(&:term) - [:description, :state, :rights_statement, :holding_location, :title, :depositor, :source_metadata_identifier, :source_metadata, :date_modified, :date_uploaded, :workflow_note, :nav_date, :pdf_type, :ocr_language, :keyword, :create_date, :modified_date, :head, :tail] - IIIFBookSchema.properties.map(&:name)
end
end
# rubocop:enable Metrics/ClassLength
14 changes: 11 additions & 3 deletions app/services/manifest_builder/metadata_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ def apply(manifest)

def metadata_objects
metadata_fields.map do |field|
if record.respond_to?(field)
if record.respond_to?("#{field}_literals") && record.try("#{field}_literals").present?
MetadataObject.new(field, record.try("#{field}_literals")).to_h
elsif record.respond_to?(field)
MetadataObject.new(field, record.try(field)).to_h
end
end.select(&:present?)
end

def metadata_fields
PlumSchema.display_fields + [:exhibit_id, :collection]
PlumSchema.display_fields + [:exhibit_id, :collection] - [:has_model]
end

class MetadataObject
Expand All @@ -47,7 +49,13 @@ def to_h

def hash_values
return values unless values.respond_to?(:each)
values.map(&:to_s)
values.map do |value|
if value.is_a?(Hash)
value
else
value.to_s
end
end
end

def renderer
Expand Down
9 changes: 9 additions & 0 deletions spec/models/scanned_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,13 @@
expect(subject.pdf_type).to eq ["color"]
end
end

describe "literal indexing" do
let(:scanned_resource) { FactoryGirl.create(:scanned_resource_in_collection, title: [::RDF::Literal.new("Test", language: :fr)]) }
let(:solr_doc) { scanned_resource.to_solr }
it "indexes literals with tags in a new field" do
expect(solr_doc['title_tesim']).to eq ['Test']
expect(solr_doc['title_literals_ssim']).to eq [JSON.dump("@value" => "Test", "@language" => "fr")]
end
end
end
12 changes: 12 additions & 0 deletions spec/services/polymorphic_manifest_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,18 @@ def build_file_set(id)
]
)
end
it "can handle RDF literals" do
record.creator = [::RDF::Literal.new("Test Author", language: "fr")]
expect(result.metadata.first).to eql(
"label" => "Creator",
"value" => [
{
"@value" => "Test Author",
"@language" => "fr"
}
]
)
end
it "is empty with no metadata" do
expect(result.metadata).to be_empty
end
Expand Down

0 comments on commit e9b3e04

Please sign in to comment.