Class: ProjectImport

Inherits:
Object
  • Object
show all
Defined in:
app/services/project_import.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(csv_data, test_run: false) ⇒ ProjectImport

Returns a new instance of ProjectImport.



7
8
9
10
# File 'app/services/project_import.rb', line 7

def initialize(csv_data, test_run: false)
    @csv_data = csv_data
    @test_run = test_run
end

Instance Attribute Details

#csv_dataObject

Returns the value of attribute csv_data.



5
6
7
# File 'app/services/project_import.rb', line 5

def csv_data
  @csv_data
end

#test_runObject

Returns the value of attribute test_run.



5
6
7
# File 'app/services/project_import.rb', line 5

def test_run
  @test_run
end

Class Method Details

.run_with_report(mediaflux_session:) ⇒ Object



12
13
14
15
16
17
18
# File 'app/services/project_import.rb', line 12

def self.run_with_report(mediaflux_session:)
  report = Mediaflux::ProjectReport.new(session_token: mediaflux_session)
  return [report.response_error[:message]] if report.error?

  importer = self.new(report.csv_data.gsub("\r\n",""))
  importer.run
end

Instance Method Details

#runObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/services/project_import.rb', line 20

def run
    output = []
    mediaflux_projects = CSV.new(csv_data, headers: true, liberal_parsing: true)
    mediaflux_projects.each do ||
      project_id = ["projectID"]
      existing_project = Project.where("metadata_json @> ?", JSON.dump(project_id:))
      if existing_project.count > 0
        output << "Skipping project #{project_id}.  There are already #{existing_project.count} version of that project in the system"
      else
        begin
           = convert_csv(project_metadata:, project_id:)
          if test_run
            output << .to_json
          else
            if .data_user_read_only.first == "n/a" && .data_user_read_only.count == 1
              .data_user_read_only = []
            end
            # Create the Rails record for the project
            project = Project.create(metadata:, mediaflux_id: ["asset"])
            if (project.valid?)
              output << "Created project for #{project_id}"
            else
              output << "Error creating project for #{["asset"]}: #{project.errors.to_a.join(";")}"
            end
          end
        rescue => ex
          output << "Error processing #{project_id}, #{ex.message}"
        end
      end
    end
    output
rescue CSV::MalformedCSVError => error
  ["Error parsing response #{ csv_data.to_s.slice(0,200) } error: #{error}"]
end