Module: Bulkrax::ImportBehavior

Extended by:
ActiveSupport::Concern
Included in:
Entry
Defined in:
app/models/concerns/bulkrax/import_behavior.rb

Overview

Import Behavior for Entry classes

Instance Method Summary collapse

Instance Method Details

#active_id_for_authority?(value, field) ⇒ Boolean

Returns provided value is a present, active authority ID for the provided field.

Parameters:

  • value (String)

    value to check

  • field (String)

    name of the controlled property

Returns:

  • (Boolean)

    provided value is a present, active authority ID for the provided field



162
163
164
165
166
167
168
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 162

def active_id_for_authority?(value, field)
  return false unless defined?(::Hyrax)
  field_service = ('Hyrax::' + "#{field}_service".camelcase).constantize
  active_authority_ids = field_service.new.active_elements.map { |ae| ae['id'] }

  active_authority_ids.include?(value)
end

#add_admin_set_idObject



88
89
90
91
92
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 88

def add_admin_set_id
  return unless defined?(::Hyrax)

  self.['admin_set_id'] = importerexporter.admin_set_id if self.['admin_set_id'].blank?
end

#add_collectionsObject



94
95
96
97
98
99
100
101
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 94

def add_collections
  return if find_collection_ids.blank?

  self.['member_of_collections_attributes'] = {}
  find_collection_ids.each_with_index do |c, i|
    self.['member_of_collections_attributes'][i.to_s] = { id: c }
  end
end

#add_rights_statementObject



80
81
82
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 80

def add_rights_statement
  self.['rights_statement'] = [parser.parser_fields['rights_statement']] if override_rights_statement || self.['rights_statement'].blank?
end

#add_user_to_permission_templates!Object



30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 30

def add_user_to_permission_templates!
  # NOTE: This is a cheat for the class is a CollectionEntry.  Consider
  # that we have default_work_type.
  #
  # TODO: This guard clause is not necessary as we can handle it in the
  # underlying factory.  However, to do that requires adjusting about 7
  # failing specs.  So for now this refactor appears acceptable
  return unless defined?(::Hyrax)
  return unless self.class.to_s.include?("Collection")
  factory.add_user_to_collection_permissions(collection: @item, user: user)
end

#add_visibilityObject



84
85
86
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 84

def add_visibility
  self.['visibility'] = importerexporter.visibility if self.['visibility'].blank?
end

#build_for_importerObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 8

def build_for_importer
  begin
    
    unless self.importerexporter.validate_only
      raise CollectionsCreatedError unless collections_created?
      @item = factory.run!
      add_user_to_permission_templates!
      parent_jobs if self.[related_parents_parsed_mapping]&.join.present?
      child_jobs if self.[related_children_parsed_mapping]&.join.present?
    end
  rescue RSolr::Error::Http, CollectionsCreatedError => e
    raise e
  rescue StandardError => e
    set_status_info(e)
  else
    set_status_info
  ensure
    self.save!
  end
  return @item
end

#build_metadataObject

Raises:

  • (StandardError)


67
68
69
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 67

def 
  raise StandardError, 'Not Implemented'
end

#child_jobsObject



50
51
52
53
54
55
56
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 50

def child_jobs
  self.[related_children_parsed_mapping].each do |child_identifier|
    next if child_identifier.blank?

    PendingRelationship.create!(parent_id: self.identifier, child_id: child_identifier, importer_run_id: importerexporter.last_run.id, order: self.id)
  end
end

#collections_created?Boolean

override this in a sub-class of Entry to ensure any collections have been created before building the work

Returns:

  • (Boolean)


63
64
65
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 63

def collections_created?
  true
end

#factoryObject



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 170

def factory
  of = Bulkrax.object_factory || Bulkrax::ObjectFactory
  @factory ||= of.new(attributes: self.,
                      source_identifier_value: identifier,
                      work_identifier: parser.work_identifier,
                      work_identifier_search_field: parser.work_identifier_search_field,
                      related_parents_parsed_mapping: parser.related_parents_parsed_mapping,
                      replace_files: replace_files,
                      user: user,
                      klass: factory_class,
                      importer_run_id: importerexporter.last_run.id,
                      update_files: update_files)
end

#factory_classObject



184
185
186
187
188
189
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 184

def factory_class
  # ATTENTION: Do not memoize this here; tests should catch the problem, but through out the
  # lifecycle of parsing a CSV row or what not, we end up having different factory classes based
  # on the encountered metadata.
  FactoryClassFinder.find(entry: self)
end

#find_collection_idsObject



58
59
60
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 58

def find_collection_ids
  self.collection_ids
end

#override_rights_statementObject

try and deal with a couple possible states for this input field



76
77
78
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 76

def override_rights_statement
  %w[true 1].include?(parser.parser_fields['override_rights_statement'].to_s)
end

#parent_jobsObject



42
43
44
45
46
47
48
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 42

def parent_jobs
  self.[related_parents_parsed_mapping].each do |parent_identifier|
    next if parent_identifier.blank?

    PendingRelationship.create!(child_id: self.identifier, parent_id: parent_identifier, importer_run_id: importerexporter.last_run.id, order: self.id)
  end
end

#rights_statementObject



71
72
73
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 71

def rights_statement
  parser.parser_fields['rights_statement']
end

#sanitize_controlled_uri_value(field, value) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 128

def sanitize_controlled_uri_value(field, value)
  if (validated_uri_value = validate_value(value, field))
    validated_uri_value
  else
    debug_msg = %(Unable to locate active authority ID "#{value}" in config/authorities/#{field.pluralize}.yml)
    Rails.logger.debug(debug_msg)
    error_msg = %("#{value}" is not a valid and/or active authority ID for the :#{field} field)
    raise ::StandardError, error_msg
  end
end

#sanitize_controlled_uri_values!Boolean

Attempt to sanitize Questioning Authority URI values for configured controlled fields of common data entry mistakes. Controlled URI values are only valid if they are an exact match. Example:

Valid value:     http://rightsstatements.org/vocab/InC/1.0/
Provided value:  https://rightsstatements.org/vocab/InC/1.0
Sanitized value: http://rightsstatements.org/vocab/InC/1.0/ ("s" from "https" removed, trailing "/" added)

Returns:

  • (Boolean)

    true if all controlled URI values are sanitized successfully



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 111

def sanitize_controlled_uri_values!
  Bulkrax.qa_controlled_properties.each do |field|
    next if [field].blank?

    if multiple?(field)
      [field].each_with_index do |value, i|
        next if value.blank?
        [field][i] = sanitize_controlled_uri_value(field, value)
      end
    else
      [field] = sanitize_controlled_uri_value(field, [field])
    end
  end

  true
end

#validate_value(value, field) ⇒ String?

Returns validated URI value or nil.

Parameters:

  • value (String)

    value to validate

  • field (String)

    name of the controlled property

Returns:

  • (String, nil)

    validated URI value or nil



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 142

def validate_value(value, field)
  if value.match?(::URI::DEFAULT_PARSER.make_regexp)
    value = value.strip.chomp
    # add trailing forward slash unless one is already present
    value << '/' unless value.match?(%r{/$})
  end

  valid = if active_id_for_authority?(value, field)
            true
          else
            value.include?('https') ? value.sub!('https', 'http') : value.sub!('http', 'https')
            active_id_for_authority?(value, field)
          end

  valid ? value : nil
end