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
-
#active_id_for_authority?(value, field) ⇒ Boolean
Provided value is a present, active authority ID for the provided field.
- #add_admin_set_id ⇒ Object
- #add_collections ⇒ Object
- #add_rights_statement ⇒ Object
- #add_user_to_permission_templates! ⇒ Object
- #add_visibility ⇒ Object
- #build_for_importer ⇒ Object
- #build_metadata ⇒ Object
- #child_jobs ⇒ Object
-
#collections_created? ⇒ Boolean
override this in a sub-class of Entry to ensure any collections have been created before building the work.
- #factory ⇒ Object
- #factory_class ⇒ Object
- #find_collection_ids ⇒ Object
-
#override_rights_statement ⇒ Object
try and deal with a couple possible states for this input field.
- #parent_jobs ⇒ Object
- #rights_statement ⇒ Object
- #sanitize_controlled_uri_value(field, value) ⇒ Object
-
#sanitize_controlled_uri_values! ⇒ Boolean
Attempt to sanitize Questioning Authority URI values for configured controlled fields of common data entry mistakes.
-
#validate_value(value, field) ⇒ String?
Validated URI value or nil.
Instance Method Details
#active_id_for_authority?(value, field) ⇒ Boolean
Returns provided value is a present, active authority ID for the provided field.
169 170 171 172 173 174 175 |
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 169 def (value, field) return false unless defined?(::Hyrax) field_service = ('Hyrax::' + "#{field}_service".camelcase).constantize = field_service.new.active_elements.map { |ae| ae['id'] } .include?(value) end |
#add_admin_set_id ⇒ Object
95 96 97 98 99 |
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 95 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_collections ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 101 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_statement ⇒ Object
87 88 89 |
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 87 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 41 42 43 44 45 46 47 |
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 30 def = Hyrax::PermissionTemplate.find_or_create_by!(source_id: @item.id) Hyrax::PermissionTemplateAccess.find_or_create_by!( permission_template_id: .id, agent_id: user.user_key, agent_type: 'user', access: 'manage' ) Hyrax::PermissionTemplateAccess.find_or_create_by!( permission_template_id: .id, agent_id: 'admin', agent_type: 'group', access: 'manage' ) @item.reset_access_controls! end |
#add_visibility ⇒ Object
91 92 93 |
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 91 def add_visibility self.['visibility'] = importerexporter.visibility if self.['visibility'].blank? end |
#build_for_importer ⇒ Object
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! if self.class.to_s.include?("Collection") && defined?(::Hyrax) parent_jobs if self.[]&.join.present? child_jobs if self.[]&.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_metadata ⇒ Object
74 75 76 |
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 74 def raise StandardError, 'Not Implemented' end |
#child_jobs ⇒ Object
57 58 59 60 61 62 63 |
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 57 def child_jobs self.[].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
70 71 72 |
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 70 def collections_created? true end |
#factory ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 177 def factory of = Bulkrax.object_factory || Bulkrax::ObjectFactory @factory ||= of.new(attributes: self., source_identifier_value: identifier, work_identifier: parser.work_identifier, related_parents_parsed_mapping: parser., replace_files: replace_files, user: user, klass: factory_class, importer_run_id: importerexporter.last_run.id, update_files: update_files) end |
#factory_class ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 190 def factory_class fc = if self.&.[]('model').present? self.&.[]('model').is_a?(Array) ? self.&.[]('model')&.first : self.&.[]('model') elsif self.mapping&.[]('work_type').present? self.&.[]('work_type').is_a?(Array) ? self.&.[]('work_type')&.first : self.&.[]('work_type') else Bulkrax.default_work_type end # return the name of the collection or work fc.tr!(' ', '_') fc.downcase! if fc.match?(/[-_]/) fc.camelcase.constantize rescue NameError nil rescue Bulkrax.default_work_type.constantize end |
#find_collection_ids ⇒ Object
65 66 67 |
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 65 def find_collection_ids self.collection_ids end |
#override_rights_statement ⇒ Object
try and deal with a couple possible states for this input field
83 84 85 |
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 83 def override_rights_statement %w[true 1].include?(parser.parser_fields['override_rights_statement'].to_s) end |
#parent_jobs ⇒ Object
49 50 51 52 53 54 55 |
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 49 def parent_jobs self.[].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_statement ⇒ Object
78 79 80 |
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 78 def rights_statement parser.parser_fields['rights_statement'] end |
#sanitize_controlled_uri_value(field, value) ⇒ Object
135 136 137 138 139 140 141 142 143 144 |
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 135 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)
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 118 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.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'app/models/concerns/bulkrax/import_behavior.rb', line 149 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 (value, field) true else value.include?('https') ? value.sub!('https', 'http') : value.sub!('http', 'https') (value, field) end valid ? value : nil end |