lib/ddr/models/solr_document.rb in ddr-models-2.11.0 vs lib/ddr/models/solr_document.rb in ddr-models-3.0.0.alpha.1

- old
+ new

@@ -1,36 +1,26 @@ require 'json' module Ddr::Models module SolrDocument extend ActiveSupport::Concern - extend Deprecation - self.deprecation_horizon = 'ddr-models v3.0' - included do alias_method :pid, :id end class NotFound < Error; end module ClassMethods def find(pid_or_uri) - query = Ddr::Index::Query.new { id pid_or_uri.sub(/\Ainfo:fedora\//, "") } + pid = pid_or_uri.sub(/\Ainfo:fedora\//, "") + query = Ddr::Index::QueryBuilder.build { |q| q.id(pid) } if doc = query.docs.first return doc end raise NotFound, "SolrDocument not found for \"#{pid_or_uri}\"." end - - def find_by_permanent_id(ark) - query = Ddr::Index::Query.new { where permanent_id: ark } - if doc = query.docs.first - return doc - end - raise NotFound, "SolrDocument not found for permanent id \"#{ark}\"." - end end def inspect "#<#{self.class.name} id=#{id.inspect}>" end @@ -139,18 +129,10 @@ def has_content? has_datastream?(Ddr::Datastreams::CONTENT) end - def has_intermediate_file? - has_datastream?(Ddr::Datastreams::INTERMEDIATE_FILE) - end - - def has_extracted_text? - has_datastream?(Ddr::Datastreams::EXTRACTED_TEXT) - end - def content_ds datastreams[Ddr::Datastreams::CONTENT] end def content_mime_type @@ -181,33 +163,35 @@ def controller_name active_fedora_model.tableize end - def rights_statement - @rights_statement ||= RightsStatement.call(self) + def effective_license + @effective_license ||= EffectiveLicense.call(self) end - alias_method :effective_license, :rights_statement - deprecation_deprecate :effective_license def roles @roles ||= Ddr::Auth::Roles::DetachedRoleSet.from_json(access_role) end - def structure - JSON.parse(fetch(Ddr::Index::Fields::STRUCTURE)) + def struct_maps + JSON.parse(fetch(Ddr::Index::Fields::STRUCT_MAPS)) rescue - nil + {} end + def struct_map(type='default') + struct_maps.present? ? struct_maps.fetch(type) : nil + end + def effective_permissions(agents) Ddr::Auth::EffectivePermissions.call(self, agents) end def research_help research_help_contact = self[Ddr::Index::Fields::RESEARCH_HELP_CONTACT] || inherited_research_help_contact - Ddr::Models::Contact.call(research_help_contact) if research_help_contact + Ddr::Contacts.get(research_help_contact) if research_help_contact end def parent_uri is_part_of || is_member_of_collection end @@ -220,111 +204,21 @@ if has_parent? self.class.find(parent_uri) end end - def multires_image_file_paths - if structure - structure_docs.map { |doc| doc.multires_image_file_path }.compact - else - [] - end + def multires_image_file_paths(type='default') + struct_map_docs(type).map { |doc| doc.multires_image_file_path }.compact end # DRY HasAdminMetadata def finding_aid if ead_id FindingAid.new(ead_id) end end - def intermediate_type - if has_intermediate_file? - datastreams[Ddr::Datastreams::INTERMEDIATE_FILE]["dsMIME"] - end - end - - def intermediate_path - if has_intermediate_file? - Ddr::Utils.path_from_uri(datastreams[Ddr::Datastreams::INTERMEDIATE_FILE]["dsLocation"]) - end - end - - def intermediate_extension - if has_intermediate_file? - extensions = Ddr::Models.preferred_file_extensions - if extensions.include? intermediate_type - extensions[intermediate_type] - else - intermediate_extension_default - end - end - end - - def captionable? - has_datastream?(Ddr::Datastreams::CAPTION) - end - - def caption_type - if captionable? - datastreams[Ddr::Datastreams::CAPTION]["dsMIME"] - end - end - - def caption_extension - if captionable? - extensions = Ddr::Models.preferred_file_extensions - if extensions.include? caption_type - extensions[caption_type] - else - caption_extension_default - end - end - end - - def caption_path - if captionable? - Ddr::Utils.path_from_uri(datastreams[Ddr::Datastreams::CAPTION]["dsLocation"]) - end - end - - def streamable? - has_datastream?(Ddr::Datastreams::STREAMABLE_MEDIA) - end - - def streamable_media_extension - if streamable? - extensions = Ddr::Models.preferred_file_extensions - if extensions.include? streamable_media_type - extensions[streamable_media_type] - else - streamable_media_extension_default - end - end - end - - def streamable_media_type - if streamable? - datastreams[Ddr::Datastreams::STREAMABLE_MEDIA]["dsMIME"] - end - end - - def streamable_media_path - if streamable? - Ddr::Utils.path_from_uri(datastreams[Ddr::Datastreams::STREAMABLE_MEDIA]["dsLocation"]) - end - end - - # FIXME - Probably need a more general solution mapping object reader methods to index field names. - def rights - self["rights_tesim"] - end - - def children - children_query.docs rescue [] - end - private def targets_query "#{Ddr::Index::Fields::IS_EXTERNAL_TARGET_FOR}:#{internal_uri_for_query}" end @@ -353,47 +247,21 @@ if doc = admin_policy doc.research_help_contact end end - def structure_docs - structure_repo_ids.map { |repo_id| self.class.find(repo_id) }.compact + def struct_map_docs(type='default') + struct_map_pids(type).map { |pid| self.class.find(pid) }.compact end - # For simplicity, initial implementation returns repo ID's only from top-level - # (i.e., not nested) contents. This is done since we have not clarified what - # an _ordered_ list of repo ID's should look like if structure contains nested - # contents. - def structure_repo_ids - default_struct_map['contents'].map { |content| content['contents'].map { |content| content['repo_id'] } }.flatten + # For simplicity, initial implementation returns PID's only from top-level + # (i.e., not nested) div's. This is done since we have not clarified what + # an _ordered_ list of PID's should look like if struct map contains nested + # div's. + def struct_map_pids(type='default') + struct_map(type)['divs'].map { |d| d['fptrs'].present? ? d['fptrs'].first : nil}.compact + rescue + [] end - def default_struct_map - structure['default'] || structure.values.first - end - - def intermediate_extension_default - datastreams[Ddr::Datastreams::INTERMEDIATE_FILE].default_file_extension - end - - def caption_extension_default - datastreams[Ddr::Datastreams::CAPTION].default_file_extension - end - - def streamable_media_extension_default - datastreams[Ddr::Datastreams::STREAMABLE_MEDIA].default_file_extension - end - - def children_query - case self[Ddr::Index::Fields::ACTIVE_FEDORA_MODEL] - when 'Collection' - Ddr::Index::Query.build(self) do |parent| - is_member_of_collection parent.id - end - when 'Item' - Ddr::Index::Query.build(self) do |parent| - is_part_of parent.id - end - end - end end end