Class: CdmMigrator::CsvController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#default_url_options

Instance Method Details

#createObject



35
36
37
38
39
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
# File 'app/controllers/cdm_migrator/csv_controller.rb', line 35

def create
  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[:admin_set],
                               collection_id: params[: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



9
10
11
12
13
14
15
16
17
18
# File 'app/controllers/cdm_migrator/csv_controller.rb', line 9

def csv_checker
  if params[:file]
    check_csv params[:file].path
    if @error_list.blank?
      flash[:notice] = "All data are valid."
    else
      flash[:error] = "The CSV Checker found some errors in the CSV. Please correct them and check again."
    end
  end
end

#editObject



90
91
92
# File 'app/controllers/cdm_migrator/csv_controller.rb', line 90

def edit
  @collections = ::Collection.all.map { |c| [c.title.first, c.id] }
end

#exportObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/controllers/cdm_migrator/csv_controller.rb', line 116

def export
  solr = RSolr.connect url: Account.find_by(tenant: Apartment::Tenant.current).solr_endpoint.url
  response = solr.get 'select', params: {
      q: "member_of_collection_ids_ssim:#{params[:collection_id]}",
      rows: 3400,
      fl: "id"
  }
  unless response['response']['docs'].empty? || response['response']['docs'][0].empty?
    work_ids = response['response']['docs'].map { |doc| doc['id'] }
  end
  #works    = ::ActiveFedora::Base.where member_of_collection_ids_ssim: params[:collection_id]
  @csv_headers = ['type'] + work_fields
  @csv_array   = [@csv_headers.join(',')]
  work_ids.each do |work_id|
    doc = ::SolrDocument.find work_id
    add_line doc
    doc._source[:file_set_ids_ssim].each do |file_id|
      file_doc = ::SolrDocument.find file_id
      add_line file_doc
    end
  end

  send_data @csv_array.join("\n"),
            :type => 'text/csv; charset=iso-8859-5; header=present',
            :disposition => "attachment; filename=export.csv"
end

#generateObject



77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/cdm_migrator/csv_controller.rb', line 77

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



20
21
22
23
24
25
26
27
28
# File 'app/controllers/cdm_migrator/csv_controller.rb', line 20

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



69
70
71
72
73
74
75
# File 'app/controllers/cdm_migrator/csv_controller.rb', line 69

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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/cdm_migrator/csv_controller.rb', line 94

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'), work_form(type), obj, mvs)
    end
    unless .nil?
      obj.attributes = 
      obj.save
    end
  end
  flash[:notice] = "csv successfully uploaded"
  redirect_to csv_edit_path
end

#uploadObject



30
31
32
33
# File 'app/controllers/cdm_migrator/csv_controller.rb', line 30

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