lib/ddr/models/solr_document.rb in ddr-models-2.0.1 vs lib/ddr/models/solr_document.rb in ddr-models-2.1.0.rc1

- old
+ new

@@ -1,275 +1,259 @@ require 'json' -module Ddr - module Models - module SolrDocument - extend ActiveSupport::Concern +module Ddr::Models + module SolrDocument + extend ActiveSupport::Concern - included do - alias_method :pid, :id - end + included do + alias_method :pid, :id + end - def to_partial_path - 'document' - end + class NotFound < Error; end - def safe_id - id.sub(/:/, "-") + module ClassMethods + def find(pid_or_uri) + 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 + end - def admin_set - get(Ddr::IndexFields::ADMIN_SET) - end + def inspect + "#<#{self.class.name} id=#{id.inspect}>" + end - def local_id - get(Ddr::IndexFields::LOCAL_ID) + def method_missing(name, *args, &block) + if args.empty? && !block + begin + field = Ddr::Index::Fields.get(name) + rescue NameError + # pass + else + # Preserves the default behavior of the deprecated method + # Blacklight::Solr::Document#get, which this procedure + # effectively replaces. + val = self[field] + return val.is_a?(Array) ? val.join(", ") : val + end end + super + end - def active_fedora_model - get(Ddr::IndexFields::ACTIVE_FEDORA_MODEL) - end + def to_partial_path + 'document' + end - def internal_uri - get(Ddr::IndexFields::INTERNAL_URI) - end + def safe_id + id.sub(/:/, "-") + end - def object_profile - @object_profile ||= get_json(Ddr::IndexFields::OBJECT_PROFILE) - end + def object_profile + @object_profile ||= get_json(Ddr::Index::Fields::OBJECT_PROFILE) + end - def object_state - object_profile["objState"] - end + def object_state + object_profile["objState"] + end - def object_create_date - parse_date(object_profile["objCreateDate"]) - end + def object_create_date + parse_date(object_profile["objCreateDate"]) + end - def object_modified_date - parse_date(object_profile["objLastModDate"]) - end + def object_modified_date + parse_date(object_profile["objLastModDate"]) + end - def last_fixity_check_on - get_date(Ddr::IndexFields::LAST_FIXITY_CHECK_ON) - end + def last_fixity_check_on + get_date(Ddr::Index::Fields::LAST_FIXITY_CHECK_ON) + end - def last_fixity_check_outcome - get(Ddr::IndexFields::LAST_FIXITY_CHECK_OUTCOME) - end + def last_virus_check_on + get_date(Ddr::Index::Fields::LAST_VIRUS_CHECK_ON) + end - def last_virus_check_on - get_date(Ddr::IndexFields::LAST_VIRUS_CHECK_ON) - end + def datastreams + object_profile["datastreams"] + end - def last_virus_check_outcome - get(Ddr::IndexFields::LAST_VIRUS_CHECK_OUTCOME) - end + def has_datastream?(dsID) + datastreams[dsID].present? + end - def datastreams - object_profile["datastreams"] - end + def has_admin_policy? + admin_policy_uri.present? + end - def has_datastream?(dsID) - datastreams[dsID].present? - end + def admin_policy_uri + is_governed_by + end - def has_admin_policy? - admin_policy_uri.present? - end + def admin_policy_pid + uri = admin_policy_uri + uri &&= ActiveFedora::Base.pid_from_uri(uri) + end - def admin_policy_uri - get(Ddr::IndexFields::IS_GOVERNED_BY) + def admin_policy + if has_admin_policy? + self.class.find(admin_policy_uri) end + end - def admin_policy_pid - uri = admin_policy_uri - uri &&= ActiveFedora::Base.pid_from_uri(uri) - end + def has_children? + ActiveFedora::SolrService.class_from_solr_document(self).reflect_on_association(:children).present? + end - def admin_policy - if admin_policy_pid - query = ActiveFedora::SolrService.construct_query_for_pids([admin_policy_pid]) - self.class.new(ActiveFedora::SolrService.query(query).first) - end - end + def label + object_profile["objLabel"] + end - def has_children? - ActiveFedora::SolrService.class_from_solr_document(self).reflect_on_association(:children).present? - end + def title_display + title + end - def label - object_profile["objLabel"] - end + def identifier + # We want the multivalued version here + self[ActiveFedora::SolrService.solr_name(:identifier, :stored_searchable, type: :text)] + end - def title - get(Ddr::IndexFields::TITLE) - end - alias_method :title_display, :title # duck-type Ddr::Models::Base + def source + self[ActiveFedora::SolrService.solr_name(:source, :stored_searchable, type: :text)] + end - def identifier - # We want the multivalued version here - get(ActiveFedora::SolrService.solr_name(:identifier, :stored_searchable, type: :text)) - end + def has_thumbnail? + has_datastream?(Ddr::Datastreams::THUMBNAIL) + end - def source - get(ActiveFedora::SolrService.solr_name(:source, :stored_searchable, type: :text)) - end + def has_content? + has_datastream?(Ddr::Datastreams::CONTENT) + end - def has_thumbnail? - has_datastream?(Ddr::Datastreams::THUMBNAIL) - end + def content_ds + datastreams[Ddr::Datastreams::CONTENT] + end - def has_content? - has_datastream?(Ddr::Datastreams::CONTENT) - end + def content_mime_type + content_ds["dsMIME"] rescue nil + end + # For duck-typing with Ddr::Models::HasContent + alias_method :content_type, :content_mime_type - def content_ds - datastreams[Ddr::Datastreams::CONTENT] - end + def content_checksum + content_ds["dsChecksum"] rescue nil + end - def content_mime_type - content_ds["dsMIME"] rescue nil - end - # For duck-typing with Ddr::Models::HasContent - alias_method :content_type, :content_mime_type + def targets + @targets ||= ActiveFedora::SolrService.query(targets_query) + end - def content_size - get(Ddr::IndexFields::CONTENT_SIZE) - end + def targets_count + @targets_count ||= ActiveFedora::SolrService.count(targets_query) + end - def content_size_human - get(Ddr::IndexFields::CONTENT_SIZE_HUMAN) - end + def has_target? + targets_count > 0 + end - def content_checksum - content_ds["dsChecksum"] rescue nil - end + def association(name) + get_pid(ActiveFedora::SolrService.solr_name(name, :symbol)) + end - def targets - @targets ||= ActiveFedora::SolrService.query(targets_query) - end + def controller_name + active_fedora_model.tableize + end - def targets_count - @targets_count ||= ActiveFedora::SolrService.count(targets_query) - end + def effective_license + @effective_license ||= EffectiveLicense.call(self) + end - def has_target? - targets_count > 0 - end + def roles + @roles ||= Ddr::Auth::Roles::DetachedRoleSet.from_json(access_role) + end - def has_default_rights? - has_datastream?(Ddr::Datastreams::DEFAULT_RIGHTS) - end + def struct_maps + JSON.parse(fetch(Ddr::Index::Fields::STRUCT_MAPS)) + rescue + {} + end - def association(name) - get_pid(ActiveFedora::SolrService.solr_name(name, :symbol)) - end + def struct_map(type='default') + struct_maps.present? ? struct_maps.fetch(type) : nil + end - def controller_name - active_fedora_model.tableize - end + def effective_permissions(agents) + Ddr::Auth::EffectivePermissions.call(self, agents) + end - def inherited_license - if admin_policy_pid - query = ActiveFedora::SolrService.construct_query_for_pids([admin_policy_pid]) - results = ActiveFedora::SolrService.query(query) - doc = results.map { |result| ::SolrDocument.new(result) }.first - { title: doc.get(Ddr::IndexFields::DEFAULT_LICENSE_TITLE), - description: doc.get(Ddr::IndexFields::DEFAULT_LICENSE_DESCRIPTION), - url: doc.get(Ddr::IndexFields::DEFAULT_LICENSE_URL) } - end - end + def research_help + research_help_contact = self[Ddr::Index::Fields::RESEARCH_HELP_CONTACT] || inherited_research_help_contact + Ddr::Contacts.get(research_help_contact) if research_help_contact + end - def license - if get(Ddr::IndexFields::LICENSE_TITLE) || get(Ddr::IndexFields::LICENSE_DESCRIPTION) || get(Ddr::IndexFields::LICENSE_URL) - { title: get(Ddr::IndexFields::LICENSE_TITLE), - description: get(Ddr::IndexFields::LICENSE_DESCRIPTION), - url: get(Ddr::IndexFields::LICENSE_URL) } - end - end + def parent_uri + is_part_of || is_member_of_collection + end - def effective_license - @effective_license ||= license || inherited_license || {} - end + def has_parent? + parent_uri.present? + end - def permanent_id - get(Ddr::IndexFields::PERMANENT_ID) + def parent + if has_parent? + self.class.find(parent_uri) end + end - def multires_image_file_path - get(Ddr::IndexFields::MULTIRES_IMAGE_FILE_PATH) - end + def multires_image_file_paths(type='default') + struct_map_docs(type).map { |doc| doc.multires_image_file_path }.compact + end - def roles - @roles ||= Ddr::Auth::Roles::DetachedRoleSet.from_json(access_role) - end + private - def access_role - get(Ddr::IndexFields::ACCESS_ROLE) - end + def targets_query + "#{Ddr::Index::Fields::IS_EXTERNAL_TARGET_FOR}:#{internal_uri_for_query}" + end - def display_format - get(Ddr::IndexFields::DISPLAY_FORMAT) - end + def internal_uri_for_query + ActiveFedora::SolrService.escape_uri_for_query(internal_uri) + end - def struct_maps - JSON.parse(fetch(Ddr::IndexFields::STRUCT_MAPS)) - rescue - {} - end + def get_date(field) + parse_date(self[field]) + end - def struct_map(type='default') - struct_maps.present? ? struct_maps.fetch(type) : nil - end + def get_json(field) + JSON.parse Array(self[field]).first + end - def effective_permissions(agents) - Ddr::Auth::EffectivePermissions.call(self, agents) - end + def parse_date(date) + Time.parse(date).localtime if date + end - def display_format - get(Ddr::IndexFields::DISPLAY_FORMAT) - end + def get_pid(field) + ActiveFedora::Base.pid_from_uri(self[field]) rescue nil + end - def research_help - research_help_contact = self[Ddr::IndexFields::RESEARCH_HELP_CONTACT] || inherited_research_help_contact - Ddr::Contacts.get(research_help_contact) if research_help_contact + def inherited_research_help_contact + if doc = admin_policy + doc.research_help_contact end + end - private + def struct_map_docs(type='default') + struct_map_pids(type).map { |pid| self.class.find(pid) }.compact + end - def targets_query - "#{Ddr::IndexFields::IS_EXTERNAL_TARGET_FOR}:#{internal_uri_for_query}" - end - - def internal_uri_for_query - ActiveFedora::SolrService.escape_uri_for_query(internal_uri) - end - - def get_date(field) - parse_date(get(field)) - end - - def get_json(field) - JSON.parse Array(self[field]).first - end - - def parse_date(date) - Time.parse(date).localtime if date - end - - def get_pid(field) - ActiveFedora::Base.pid_from_uri(get(field)) rescue nil - end - - def inherited_research_help_contact - if admin_policy_pid - query = ActiveFedora::SolrService.construct_query_for_pids([admin_policy_pid]) - results = ActiveFedora::SolrService.query(query) - doc = results.map { |result| ::SolrDocument.new(result) }.first - doc[Ddr::IndexFields::RESEARCH_HELP_CONTACT] - end - end - + # 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 + end end