Class: Bulkrax::ValkyrieObjectFactory

Inherits:
ObjectFactoryInterface show all
Includes:
FileFactory
Defined in:
app/factories/bulkrax/valkyrie_object_factory.rb

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: FileFactoryInnerWorkings

Instance Attribute Summary

Attributes inherited from ObjectFactoryInterface

#attributes, #importer_run_id, #klass, #object, #related_parents_parsed_mapping, #replace_files, #source_identifier_value, #update_files, #user, #work_identifier, #work_identifier_search_field

Class Method Interface collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ObjectFactoryInterface

add_user_to_collection_permissions, #add_user_to_collection_permissions, #base_permitted_attributes, clean!, #create, default_admin_set_id, default_admin_set_or_nil, export_properties, #find, #find_or_create, find_or_nil, #initialize, #run, #search_by_identifier, #transformation_removes_blank_hash_values, #update

Methods included from Loggable

#log_created, #log_deleted_fs, #log_updated

Methods included from DynamicRecordLookup

#find_record

Constructor Details

This class inherits a constructor from Bulkrax::ObjectFactoryInterface

Class Method Details

.add_child_to_parent_work(parent:, child:) ⇒ Object

Note:

This does not save either object. We need to do that in another loop. Why? Because we might be adding many items to the parent.



68
69
70
71
72
73
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 68

def self.add_child_to_parent_work(parent:, child:)
  return true if parent.member_ids.include?(child.id)

  parent.member_ids << child.id
  parent.save
end

.add_resource_to_collection(collection:, resource:, user:) ⇒ Object



75
76
77
78
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 75

def self.add_resource_to_collection(collection:, resource:, user:)
  resource.member_of_collection_ids << collection.id
  save!(resource: resource, user: user)
end

.field_multi_value?(field:, model:) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 80

def self.field_multi_value?(field:, model:)
  return false unless field_supported?(field: field, model: model)

  if model.respond_to?(:schema)
    schema = model.new.singleton_class.schema || model.schema
    dry_type = schema.key(field.to_sym)
    return true if dry_type.respond_to?(:primitive) && dry_type.primitive == Array

    false
  else
    Bulkrax::ObjectFactory.field_multi_value?(field: field, model: model)
  end
end

.field_supported?(field:, model:) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
102
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 94

def self.field_supported?(field:, model:)
  if model.respond_to?(:schema)
    schema_properties(model).include?(field)
  else
    # We *might* have a Fedora object, so we need to consider that approach as
    # well.
    Bulkrax::ObjectFactory.field_supported?(field: field, model: model)
  end
end

.file_sets_for(resource:) ⇒ Object



104
105
106
107
108
109
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 104

def self.file_sets_for(resource:)
  return [] if resource.blank?
  return [resource] if resource.is_a?(Bulkrax.file_model_class)

  Hyrax.query_service.custom_queries.find_child_file_sets(resource: resource)
end

.find(id) ⇒ Object



111
112
113
114
115
116
117
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 111

def self.find(id)
  Hyrax.query_service.find_by(id: id)
  # Because Hyrax is not a hard dependency, we need to transform the Hyrax exception into a
  # common exception so that callers can handle a generalize exception.
rescue Hyrax::ObjectNotFoundError => e
  raise ObjectFactoryInterface::ObjectNotFoundError, e.message
end

.find_or_create_default_admin_setObject



119
120
121
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 119

def self.find_or_create_default_admin_set
  Hyrax::AdminSetCreateService.find_or_create_default_admin_set
end

.ordered_file_sets_for(object) ⇒ Object



203
204
205
206
207
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 203

def self.ordered_file_sets_for(object)
  return [] if object.blank?

  Hyrax.custom_queries.find_child_file_sets(resource: object)
end

.publish(event:, **kwargs) ⇒ Object



129
130
131
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 129

def self.publish(event:, **kwargs)
  Hyrax.publisher.publish(event, **kwargs)
end

.query(q, **kwargs) ⇒ Object

Raises:

  • (NotImplementedError)


133
134
135
136
137
138
139
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 133

def self.query(q, **kwargs)
  # Someone could choose ActiveFedora::SolrService.  But I think we're
  # assuming Valkyrie is specifcally working for Hyrax.  Someone could make
  # another object factory.
  raise NotImplementedError, "#{self}.#{__method__}" unless defined?(Hyrax)
  Hyrax::SolrService.query(q, **kwargs)
end

.save!(resource:, user:) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 141

def self.save!(resource:, user:)
  if resource.respond_to?(:save!)
    resource.save!
  else
    result = Hyrax.persister.save(resource: resource)
    raise Valkyrie::Persistence::ObjectNotFoundError unless result
    Hyrax.index_adapter.save(resource: result)
    if result.collection?
      publish('collection.metadata.updated', collection: result, user: user)
    else
      publish('object.metadata.updated', object: result, user: user)
    end
    resource
  end
end

.schema_properties(klass) ⇒ Array<String>

Retrieve properties from M3 model

Parameters:

  • klass

    the model

Returns:

  • (Array<String>)


193
194
195
196
197
198
199
200
201
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 193

def self.schema_properties(klass)
  @schema_properties_map ||= {}

  klass_key = klass.name
  schema = klass.new.singleton_class.schema || klass.schema
  @schema_properties_map[klass_key] = schema.map { |k| k.name.to_s } unless @schema_properties_map.key?(klass_key)

  @schema_properties_map[klass_key]
end

.search_by_property(value:, klass:, field: nil, name_field: nil) ⇒ NilClass, Valkyrie::Resource

rubocop:disable Metrics/ParameterLists

Parameters:

  • value (String)
  • klass (Class, #where)
  • field (String, Symbol) (defaults to: nil)

    A convenience parameter where we pass the same value to search_field and name_field.

  • name_field (String) (defaults to: nil)

    the ActiveFedora::Base property name (e.g. “title”)

Returns:

  • (NilClass)

    when no object is found.

  • (Valkyrie::Resource)

    when a match is found, an instance of given :klass



179
180
181
182
183
184
185
186
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 179

def self.search_by_property(value:, klass:, field: nil, name_field: nil, **)
  name_field ||= field
  raise "Expected named_field or field got nil" if name_field.blank?
  return if value.blank?

  # Return nil or a single object.
  Hyrax.query_service.custom_query.find_by_model_and_property_value(model: klass, property: name_field, value: value)
end

.solr_name(field_name) ⇒ Object

Raises:

  • (NotImplementedError)


123
124
125
126
127
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 123

def self.solr_name(field_name)
  # It's a bit unclear what this should be if we can't rely on Hyrax.
  raise NotImplementedError, "#{self}.#{__method__}" unless defined?(Hyrax)
  Hyrax.config.index_field_mapper.solr_name(field_name)
end

.transactionsObject

Note:

Within Bulkrax::ValkyrieObjectFactory there are several calls to transactions; so you’ll need your container to register those transactions.

When you want a different set of transactions you can change the container.



54
55
56
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 54

def self.transactions
  @transactions || Hyrax::Transactions::Container
end

.update_index(resources:) ⇒ Object



157
158
159
160
161
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 157

def self.update_index(resources:)
  Array(resources).each do |resource|
    Hyrax.index_adapter.save(resource: resource)
  end
end

.update_index_for_file_sets_of(resource:) ⇒ Object



163
164
165
166
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 163

def self.update_index_for_file_sets_of(resource:)
  file_sets = Hyrax.query_service.custom_queries.find_child_file_sets(resource: resource)
  update_index(resources: file_sets)
end

Instance Method Details

#delete(user) ⇒ Object



209
210
211
212
213
214
215
216
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 209

def delete(user)
  obj = find
  return false unless obj

  Hyrax.persister.delete(resource: obj)
  Hyrax.index_adapter.delete(resource: obj)
  self.class.publish(event: 'object.deleted', object: obj, user: user)
end

#run!Object



218
219
220
221
222
223
224
225
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 218

def run!
  run
  # reload the object
  object = find
  return object if object&.persisted?

  raise(ObjectFactoryInterface::RecordInvalid, object)
end

#transactionsObject



58
59
60
# File 'app/factories/bulkrax/valkyrie_object_factory.rb', line 58

def transactions
  self.class.transactions
end