-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from notch8/csv_importer
CSV Importer
- Loading branch information
Showing
25 changed files
with
359 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module Bulkrax | ||
class CsvMatcher < ApplicationMatcher | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require 'csv' | ||
|
||
module Bulkrax | ||
class CsvEntry < Entry | ||
include Bulkrax::Concerns::HasMatchers | ||
|
||
serialize :raw_metadata, JSON | ||
|
||
matcher 'contributor', split: true | ||
matcher 'creator', split: true | ||
matcher 'date', split: true | ||
matcher 'description' | ||
matcher 'format_digital', parsed: true | ||
matcher 'format_original', parsed: true | ||
matcher 'identifier' | ||
matcher 'language', parsed: true, split: true | ||
matcher 'place' | ||
matcher 'publisher', split: true | ||
matcher 'rights_statement' | ||
matcher 'subject', split: true | ||
matcher 'title' | ||
matcher 'alternative_title' | ||
matcher 'types', from: %w[types type], split: true, parsed: true | ||
matcher 'file', split: true | ||
|
||
def build_metadata | ||
self.parsed_metadata = {} | ||
|
||
if record.nil? | ||
raise StandardError, 'Record not found' | ||
elsif required_elements?(record.keys) == false | ||
raise StandardError, "Missing required elements, required elements are: #{required_elements.join(', ')}" | ||
end | ||
|
||
record.each do |key, value| | ||
add_metadata(key, value) | ||
end | ||
add_visibility | ||
add_rights_statement | ||
|
||
parsed_metadata | ||
end | ||
|
||
def record | ||
@record ||= raw_metadata | ||
end | ||
|
||
def matcher_class | ||
Bulkrax::CsvMatcher | ||
end | ||
|
||
def required_elements?(keys) | ||
!required_elements.map { |el| keys.include?(el) }.include?(false) | ||
end | ||
|
||
def required_elements | ||
%w[title identifier] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
module Bulkrax | ||
class CsvParser < ApplicationParser | ||
delegate :errors, :increment_counters, :parser_fields, to: :importer | ||
|
||
def self.parser_fields | ||
{ | ||
csv_path: :string, | ||
rights_statements: :string, | ||
override_rights_statement: :boolean | ||
} | ||
end | ||
|
||
def run | ||
create_works | ||
end | ||
|
||
def records(_opts = {}) | ||
CSV.foreach( | ||
parser_fields['csv_path'], | ||
headers: true, | ||
header_converters: :symbol, | ||
encoding: 'utf-8' | ||
) | ||
end | ||
|
||
def create_works | ||
records.with_index(0) do |record, index| | ||
next if record[:identifier].blank? | ||
break if !limit.nil? && index >= limit | ||
|
||
seen[record[:identifier]] = true | ||
new_entry = entry_class.where(importer: importer, identifier: record[:identifier], raw_metadata: record.to_h.compact).first_or_create! | ||
ImportWorkJob.perform_later(new_entry.id, importer.current_importer_run.id) | ||
increment_counters(index) | ||
end | ||
rescue StandardError => e | ||
errors.add(:base, e.class.to_s.to_sym, message: e.message) | ||
end | ||
|
||
def files_path | ||
arr = parser_fields['csv_path'].split('/') | ||
arr.pop | ||
arr << 'files' | ||
arr.join('/') | ||
end | ||
|
||
def entry_class | ||
CsvEntry | ||
end | ||
|
||
# See https://stackoverflow.com/questions/2650517/count-the-number-of-lines-in-a-file-without-reading-entire-file-into-memory | ||
def total | ||
@total ||= `wc -l #{parser_fields['csv_path']}`.to_i -1 | ||
rescue StandardError | ||
@total = 0 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<div class='csv_fields'> | ||
<%= fi.input :csv_path, as: :string, input_html: { value: importer.parser_fields['csv_path'] } %> | ||
<% rights_statements = Hyrax.config.rights_statement_service_class.new %> | ||
<%= fi.input :rights_statement, | ||
collection: rights_statements.select_active_options, | ||
selected: importer.parser_fields['rights_statement'], | ||
include_blank: true, | ||
item_helper: rights_statements.method(:include_current_value), | ||
input_html: { class: 'form-control' } %> | ||
<%= fi.input :override_rights_statement, as: :boolean, hint: 'If checked, always use the selected rights statment. If unchecked, use dc:rights from the record and only use the provided value if dc:rights is blank.', input_html: { checked: (importer.parser_fields['override_rights_statement'] == "1") } %> | ||
|
||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.