Class: CdmMigrator::CsvController

Inherits:
ApplicationController
  • Object
show all
Includes:
ActionView::Helpers::UrlHelper
Defined in:
app/controllers/cdm_migrator/csv_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/cdm_migrator/csv_controller.rb', line 40

def create
  csv_upload_params
  dir = Rails.root.join('public', 'uploads', 'csvs')
  FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
  time     = DateTime.now.strftime('%s')
  filename = params[:csv_import][:csv_file].original_filename.gsub('.csv', "#{time}.csv")
  csv      = dir.join(filename).to_s
  File.open(csv, 'wb') do |file|
    file.write(params[:csv_import][:csv_file].read)
  end
  check_csv csv
  if @error_list.present?
    flash[:error] = "Cdm Migrator found some problems with the CSV. Use the CSV Checker for more details."
  end
  parse_csv(csv, params[:csv_import][:mvs])

  ingest = BatchIngest.new({
                             data:          @works,
                             size:          @works.length,
                             csv:           csv,
                             admin_set_id:  params[:csv_import][:admin_set],
                             collection_id: params[:csv_import][:collection],
                             user_id:       current_user.id,
                             message:       @path_list.blank? ? nil : @path_list.to_s.gsub("\"", """)
                           })
  if ingest.save! && @path_list.blank?
    BatchCreateWorksJob.perform_later(ingest, current_user)
    flash[:notice] = "csv successfully uploaded, check this page to see the status while the batch is running"
    redirect_to csv_my_batches_path
  else
    flash[:error] ||= "csv could not be parsed, please check and re-upload"
    redirect_to csv_upload_path
  end
end

#csv_checkerObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/cdm_migrator/csv_controller.rb', line 11

def csv_checker
  if params[:file]
    results = CsvChecker.new.check_csv(params[:file].path)
    # Set warning messages
    results['alerts'].each { |alert| flash[:alert] = alert }
    if results['errors'].nil?
      redirect_to csv_checker_path, notice: "All data are valid."
    else
      flash[:error] = "The CSV Checker found some errors in the CSV. Please correct them and check again."
      @error_list = results['errors']
      render :csv_checker
    end
  end
end

#editObject



96
97
98
# File 'app/controllers/cdm_migrator/csv_controller.rb', line 96

def edit
  # Intentionally blank
end

#exportObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/controllers/cdm_migrator/csv_controller.rb', line 123

def export
  csv_export_params
  # Get a collection's member works from Solr
  solr = RSolr.connect url: Blacklight.connection_config[:url]
  response = solr.get 'select', params: {
    q: "member_of_collection_ids_ssim:#{params[:csv_export][:collection]}",
    fq: ["has_model_ssim:FileSet OR has_model_ssim:*Work"],
    rows: 3400,
    fl: "id"
  }
  unless response['response']['docs'].empty? || response['response']['docs'][0].empty?
    work_ids = response['response']['docs'].map { |doc| doc['id'] }
  end

  send_data CsvExportService.new(available_works).csv_for(work_ids),
            :type => 'text/csv; charset=iso-8859-5; header=present',
            :disposition => "attachment; filename=export.csv"
end

#generateObject



83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/cdm_migrator/csv_controller.rb', line 83

def generate
  headers = %w(type url)
  skip    = %w(id head tail depositor date_uploaded date_modified import_url thumbnail_id embargo_id lease_id access_control_id representative_id)
  GenericWork.new.attributes.each do |key, val|
    headers << "work_#{key}" unless skip.include? key
  end
  FileSet.new.attributes.each do |key, val|
    headers << "file_#{key}" unless skip.include? key
  end
  fname = "template_#{DateTime.now.to_i}"
  render plain: CSV.generate { |csv| csv << headers }, content_type: 'text/csv'
end

#indexObject



26
27
28
29
30
31
32
33
34
# File 'app/controllers/cdm_migrator/csv_controller.rb', line 26

def index
  if current_page?(main_app.csv_my_batches_path(locale: nil))
    @batches = BatchIngest.where(user_id: current_user.id).reverse_order
  elsif current_page?(main_app.csv_all_batches_path(locale: nil))
    @batches = BatchIngest.all.reverse_order
  else
    @batches = []
  end
end

#rerunObject



75
76
77
78
79
80
81
# File 'app/controllers/cdm_migrator/csv_controller.rb', line 75

def rerun
  ingest = BatchIngest.find(params[:id]).deep_dup
  ingest.save
  BatchCreateWorksJob.perform_later(ingest, current_user)
  flash[:notice] = "csv successfully uploaded, check this page to see the status while the batch is running"
  redirect_to csv_my_batches_path
end

#updateObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/cdm_migrator/csv_controller.rb', line 100

def update
  mvs = params[:csv_update][:mvs]
  csv = CSV.parse(params[:csv_update][:csv_file].read.force_encoding("UTF-8"), headers: true, encoding: 'utf-8').map(&:to_hash)
  csv.each do |row|
    obj = ActiveFedora::Base.find row['id']
    type = row.first.last
    if type.nil?
      next
    elsif type.include? "Work"
       = create_data(row.except('id', 'type'), work_form(type), obj, mvs)
    elsif type.include? "File"
       = create_data(row.except('id', 'type'), file_form, obj, mvs)
    end
    unless .nil?
      obj.attributes = 
      obj.try(:to_controlled_vocab)
      obj.save
    end
  end
  flash[:notice] = "csv successfully uploaded"
  redirect_to csv_edit_path
end

#uploadObject



36
37
38
# File 'app/controllers/cdm_migrator/csv_controller.rb', line 36

def upload
  @admin_sets  = AdminSet.all.map { |as| [as.title.first, as.id] }
end