module ActiveFedora::Model::ClassMethods
def attribute_get(name)
def attribute_get(name) instance_variable_get("@#{name}") end
def attribute_set(name, value)
def attribute_set(name, value) instance_variable_set("@#{name}", value) end
def class_fields
def class_fields #create dummy object that is empty by passing in fake pid object = self.new()#{:pid=>'FAKE'}) fields = object.fields #reset id to nothing fields[:id][:values] = [] return fields end
def count(args = {})
Get a count of the number of objects from solr
def count(args = {}) escaped_class_uri = SolrService.escape_uri_for_query(self.to_class_uri) q = "#{ActiveFedora::SolrService.solr_name(:has_model, :symbol)}:#{escaped_class_uri}" q << " AND #{args[:conditions]}" if args[:conditions] SolrService.query(q, :raw=>true, :rows=>0)['response']['numFound'] end
def exists?(pid)
@param[String] pid
Returns true if the pid exists in the repository
def exists?(pid) inner = DigitalObject.find(self, pid) !inner.new? end
def find(args, opts={})
(**opts)
-
:cast
(Boolean
) -- when true, examine the model and cast it to the first known cModel -
:rows
(Integer
) -- when :all is passed, the maximum number of rows to load from solr
Parameters:
-
opts
(Hash
) -- the options to create a message with.
def find(args, opts={}) opts = {:rows=>25}.merge(opts) if args == :all escaped_class_uri = SolrService.escape_uri_for_query(self.to_class_uri) q = "#{ActiveFedora::SolrService.solr_name(:has_model, :symbol)}:#{escaped_class_uri}" hits = SolrService.query(q, :rows=>opts[:rows]) return hits.map do |hit| pid = hit[SOLR_DOCUMENT_ID] find_one(pid, opts[:cast]) end elsif args.class == String return find_one(args, opts[:cast]) end end
def find_by_fields_by_solr(query_fields,opts={})
:field_list => array, defaults to ["*", "score"]
:start => defaults to 0
:operator => :or / :and
:explain_other, :facets, :highlighting, :mlt,
:default_field, :rows, :filter_queries, :debug_query,
:sort => array of hash with one hash per sort by field... defaults to [{system_create=>:descending}]
options may include:
opts specifies options for the solr query
query_fields a hash of object field names and values to filter on (query_fields must be the solr_field_name for non-MetadataDatastream derived datastreams)
passed in by querying Solr. Like find_by_solr this returns a solr result.
Find all ActiveFedora objects for this model that match arguments
def find_by_fields_by_solr(query_fields,opts={}) ActiveSupport::Deprecation.warn("find_by_fields_by_solr is deprecated and will be removed in the next release") #create solr_args from fields passed in, needs to be comma separated list of form field1=value1,field2=value2,... escaped_class_name = self.name.gsub(/(:)/, '\\:') query = "#{ActiveFedora::SolrService.solr_name(:active_fedora_model, :symbol)}:#{escaped_class_name}" query_fields.each_pair do |key,value| unless value.nil? solr_key = key #convert to symbol if need be key = key.to_sym if !class_fields.has_key?(key)&&class_fields.has_key?(key.to_sym) #do necessary mapping with suffix in most cases, otherwise ignore as a solr field key that activefedora does not know about if class_fields.has_key?(key) && class_fields[key].has_key?(:type) type = class_fields[key][:type] type = :string unless type.kind_of?(Symbol) solr_key = ActiveFedora::SolrService.solr_name(key,type) end escaped_value = value.gsub(/(:)/, '\\:') #escaped_value = escaped_value.gsub(/ /, '\\ ') key = SOLR_DOCUMENT_ID if (key === :id || key === :pid) query = key.to_s.eql?(SOLR_DOCUMENT_ID) ? "#{query} AND #{key}:#{escaped_value}" : "#{query} AND #{solr_key}:#{escaped_value}" end end query_opts = {} opts.each do |key,value| key = key.to_sym query_opts[key] = value end #set default sort to created date ascending unless query_opts.include?(:sort) query_opts.merge!({:sort=>[ActiveFedora::SolrService.solr_name(:system_create,:date)+' asc']}) else #need to convert to solr names for all fields sort_array =[] opts[:sort].collect do |sort| sort_direction = 'ascending' if sort.respond_to?(:keys) key = sort.keys[0] sort_direction = sort[key] else key = sort.to_s end sort_direction = sort_direction =~ /^desc/ ? 'desc' : 'asc' field_name = key if key.to_s =~ /^system_create/ field_name = :system_create_date key = :system_create elsif key.to_s =~ /^system_mod/ field_name = :system_modified_date key = :system_modified end solr_name = field_name if class_fields.include?(field_name.to_sym) solr_name = ActiveFedora::SolrService.solr_name(key,class_fields[field_name.to_sym][:type]) end sort_array.push("#{solr_name} #{sort_direction}") end query_opts[:sort] = sort_array.join(",") end logger.debug "Querying solr for #{self.name} objects with query: '#{query}'" SolrService.query(query, query_opts) end
def find_by_solr(query, args={})
Args is an options hash, which is passed into the SolrService
objects, but rather an array of SolrResults.
Note that this method does _not_ return ActiveFedora::Model
a pid based search (id:query).
by Solr). If the query is any other string, this method simply does
of self.type (based on active_fedora_model_s as indexed
If query is :all, this method will query Solr for all instances
def find_by_solr(query, args={}) if query == :all escaped_class_name = self.name.gsub(/(:)/, '\\:') SolrService.query("#{ActiveFedora::SolrService.solr_name(:active_fedora_model, :symbol)}:#{escaped_class_name}", args) elsif query.class == String escaped_id = query.gsub(/(:)/, '\\:') SolrService.query("#{SOLR_DOCUMENT_ID}:#{escaped_id}", args) end end
def find_model(pid)
def find_model(pid) ActiveSupport::Deprecation.warn("find_model is deprecated. Use find instead") find(pid) end
def find_one(pid, cast=false)
- Example: because the object hydra:dataset1 asserts it is a Dataset (hasModel info:fedora/afmodel:Dataset), return a Dataset object (not a Book). -
Parameters:
-
cast
(Boolean
) -- when true, cast the found object to the class of the first known model defined in it's RELS-EXT -
pid
(String
) -- of the object to load
def find_one(pid, cast=false) inner = DigitalObject.find(self, pid) raise ActiveFedora::ObjectNotFoundError, "Unable to find '#{pid}' in fedora" if inner.new? af_base = self.allocate.init_with(inner) cast ? af_base.adapt_to_cmodel : af_base end
def load_instance(pid)
- Example: this will return an instance of Book, even if the object hydra:dataset1 asserts that it is a Dataset -
Parameters:
-
pid
(String
) -- of the object to load
def load_instance(pid) ActiveSupport::Deprecation.warn("load_instance is deprecated. Use find instead") find(pid) end
def solr_search(query, args={})
def solr_search(query, args={}) SolrService.instance.conn.query(query, args) end
def to_class_uri(attrs = {})
Returns a suitable uri object for :has_model
def to_class_uri(attrs = {}) unless self.respond_to? :pid_suffix pid_suffix = attrs.has_key?(:pid_suffix) ? attrs[:pid_suffix] : ContentModel::CMODEL_PID_SUFFIX else pid_suffix = self.pid_suffix end unless self.respond_to? :pid_namespace namespace = attrs.has_key?(:namespace) ? attrs[:namespace] : ContentModel::CMODEL_NAMESPACE else namespace = self.pid_namespace end "info:fedora/#{namespace}:#{ContentModel.sanitized_class_name(self)}#{pid_suffix}" end