Module: Services::MdsFileProcessor

Defined in:
app/roles/services/mds_file_processor.rb

Constant Summary collapse

PCC_METADATA_FILE_RE =
%r{PCC_.+_metadata.xml\z}

Instance Method Summary collapse

Instance Method Details

#processObject

So, the work we’re going to do here is to crack open the MDS file and save each of the assessments to the MDS assessments table. We won’t process it any further at this point.



10
11
12
13
14
15
16
17
18
19
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
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/roles/services/mds_file_processor.rb', line 10

def process
  # Do nothing if we dont' have a status of NEW
  return unless self.status == MdsUpload::NEW
  return unless self.mds_content.present?

  start_time = Time.now
  num_records = 0
  num_duplicated = 0
  num_rejected = 0
  
  # Loop over all the files in the upload file
  self.mds_content.each_entry do | entry, relative_path |

    next if File.directory?(entry)
    basename = File.basename(entry)
    next if basename =~ PCC_METADATA_FILE_RE

    num_records += 1

    parser = MdsXmlFileParser.new(entry)
    assessment_data = parser.parse

    if assessment_data.blank?
      num_rejected += 1
      self.file_errors.merge!(parser.file_errors) if parser.file_errors.present?
    else
      # We use the relative_path to the file from the extration
      # directory so that the error report will properly show 
      # the path to the file with the problem, even if the file
      # was located in an embedded zip file.
      begin
        mds = create_assessment(relative_path, assessment_data)
        if mds.blank?
          num_duplicated += 1
          add_file_errors(relative_path, "Duplicate assessment") 
        elsif !mds.valid?
          num_rejected += 1
          add_file_errors(relative_path, mds.errors.values.flatten)
        end
      rescue => ex
        num_rejected += 1
        add_file_errors(relative_path, ex.message)
      end
    end
  end

  # Need to save any errors
  update_attributes(status: MdsUpload::ASSESSMENTS_LOADED,
                    num_records: num_records,
                    num_duplicated: num_duplicated,
                    num_rejected: num_rejected,
                    parse_time: ((Time.now - start_time) * 1000).round)
rescue => ex
  Rails.logger.error("#{ex.message}\n#{ex.backtrace.join("\n")}")
  update_attribute(:fatal_error, "#{ex.message}\n#{ex.backtrace.join("\n")}")
end