Method: BCL::ComponentFromSpreadsheet#save

Defined in:
lib/bcl/component_from_spreadsheet.rb

#save(save_path, chunk_size = 1000, delete_old_gather = false) ⇒ Object



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/bcl/component_from_spreadsheet.rb', line 49

def save(save_path, chunk_size = 1000, delete_old_gather = false)
  # FileUtils.rm_rf(save_path) if File.exists?(save_path) and File.directory?(save_path)
  # TODO: validate against taxonomy

  @worksheets.each do |worksheet|
    worksheet.components.each do |component|
      component_xml = Component.new("#{save_path}/components")
      component_xml.name = component.name
      component_xml.uid = component.uid

      # this tag is how we know where this goes in the taxonomy
      component_xml.add_tag(worksheet.name)
      puts "tag: #{worksheet.name}"

      values = component.values

      puts " headers: #{component.headers}"
      component.headers.each do |header|
        if /description/i.match?(header.name)
          name = values.delete_at(0) # name, uid already processed
          uid = values.delete_at(0)
          component_xml.comp_version_id = values.delete_at(0)
          description = values.delete_at(0)
          component_xml.modeler_description = values.delete_at(0)
          component_xml.description = description
        elsif /provenance/i.match?(header.name)
          author = values.delete_at(0)
          datetime = values.delete_at(0)
          if datetime.nil?
            # puts "[ComponentSpreadsheet] WARNING missing the date in the datetime column in the spreadsheet - assuming today"
            datetime = DateTime.new
          end

          comment = values.delete_at(0)
          component_xml.add_provenance(author.to_s, datetime.strftime('%Y-%m-%d'), comment.to_s)
        elsif /tag/i.match?(header.name)
          value = values.delete_at(0)
          component_xml.add_tag(value)
        elsif /attribute/i.match?(header.name)
          value = values.delete_at(0)
          name = header.children[0]
          units = ''
          if match_data = /(.*)\((.*)\)/.match(name)
            name = match_data[1].strip
            units = match_data[2].strip
          end
          component_xml.add_attribute(name, value, units)
        elsif /source/i.match?(header.name)
          manufacturer = values.delete_at(0)
          model = values.delete_at(0)
          serial_no = values.delete_at(0)
          year = values.delete_at(0)
          url = values.delete_at(0)
          component_xml.source_manufacturer = manufacturer
          component_xml.source_model = model
          component_xml.source_serial_no = serial_no
          component_xml.source_year = year
          component_xml.source_url = url
        elsif /file/i.match?(header.name)
          software_program = values.delete_at(0)
          version = values.delete_at(0)
          filename = values.delete_at(0)
          filetype = values.delete_at(0)
          filepath = values.delete_at(0)
          # not all components(rows) have all files; skip if filename "" or nil
          next if filename == '' || filename.nil?

          # skip the file if it doesn't exist at the specified location
          unless File.exist?(filepath)
            puts "[ComponentFromSpreadsheet] ERROR #{filepath} -> File does not exist, will not be included in component xml"
            next # go to the next file
          end
          component_xml.add_file(software_program, version, filepath, filename, filetype)
        else
          raise "Unknown section #{header.name}"
        end
      end

      component_xml.save_tar_gz(false)
    end
  end

  BCL.gather_components(save_path, chunk_size, delete_old_gather)
end