Module: BatchLoad::ColumnResolver

Defined in:
lib/batch_load/column_resolver.rb,
lib/batch_load/column_resolver/result.rb

Overview

Stores the attributes of a Column Resolver result

Defined Under Namespace

Classes: Result

Constant Summary collapse

BOOLEAN_TRUE_VALUES =
%w{t true 1}.freeze
BOOLEAN_FALSE_VALUES =
%w{f false 0}.freeze

Class Method Summary collapse

Class Method Details

.boolean(columns, column) ⇒ BatchLoad::ColumnResolver::Result

Case insensitive t/true/1 or f/false/0. Blank isn't an error, it resolves to nil: callers decide what blank means.

Parameters:

  • columns (Hash)
  • column (String) —

    the column name

Returns:

  • (BatchLoad::ColumnResolver::Result) —

    item is true, false, or nil (blank). A bad value shows up only in error_messages; don't use resolvable? here, it needs a truthy item, so it's false for a valid false or blank.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/batch_load/column_resolver.rb', line 20

def boolean(columns, column)
  r = BatchLoad::ColumnResolver::Result.new
  value = columns[column]&.strip
  return r if value.blank?

  case value.downcase
  when *BOOLEAN_TRUE_VALUES
    r.assign true
  when *BOOLEAN_FALSE_VALUES
    r.assign false
  else
    r.error_messages << "Unrecognized #{column} value '#{value}'."
  end
  r
end

.collection_object_by_identifier(columns) ⇒ BatchLoad::ColumnResolver::Result

Parameters:

  • columns (Hash)

Returns:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/batch_load/column_resolver.rb', line 104

def collection_object_by_identifier(columns)
  r = BatchLoad::ColumnResolver::Result.new

  # find a namespace (ns1) with a short_name of row[headers[0]] (id1)
  # find a collection_object which has an identifier which has a namespace (ns1), and a cached of
  # (ns1.short_name + ' ' + identifier.identifier)
  # ns1    = Namespace.where(short_name: id1).first

  ident_text = columns['catalog_number']
  ident_text = columns['collection_object_identifier'] if ident_text.blank?

  ident_text = "#{columns['collection_object_identifier_namespace_short_name']}" + ' ' +
      " #{columns['collection_object_identifier_identifier']}".strip if ident_text.blank?

  if ident_text.blank?
    r.error_messages << 'No column combination suitable for collection object resolution was provided.'
  else
    r.assign CollectionObject.joins(:identifiers).where(identifiers: {cached: ident_text}).to_a
    r.error_messages << "No collection object with cached identifier '#{ident_text}' exists." if r.no_matches?
  end
  r
end

.data_attribute(columns, type = 'import') ⇒ BatchLoad::ColumnResolver::Result

Parameters:

  • columns (Hash)
  • type (String) (defaults to: 'import') —

    of DataAttribute

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/batch_load/column_resolver.rb', line 39

def data_attribute(columns, type = 'import')
  r = BatchLoad::ColumnResolver::Result.new

  predicate = columns['predicate']
  value = columns['value']
  proj_id = columns['project_id']
  r.error_messages << 'No column for \'Value\' was provided.' unless columns.key?('value')
  r.error_messages << 'No column for \'Predicate\' was provided.' unless columns.key?('predicate')
  r.error_messages << 'No content for \'Value\' was provided.' if value.blank?
  r.error_messages << 'No content for \'Predicate\' was provided.' if predicate.blank?

  if type.start_with?('im')
    r.assign(DataAttribute.where(import_predicate: predicate, value: value, project_id: proj_id).to_a)
  else
    cvt = ControlledVocabularyTerm.find_by(name: predicate, project_id: proj_id)
    r.assign(DataAttribute.where(controlled_vocabulary_term_id: cvt&.id, value: value, project_id: proj_id).to_a)
  end
  r.error_messages << "Multiple data_attributes match for '#{predicate}' and '#{value}'.'" if r.multiple_matches?

  r
end

.otu(columns) ⇒ BatchLoad::ColumnResolver::Result

rubocop:disable Metrics/MethodLength

Parameters:

  • columns (Hash)

Returns:



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
# File 'lib/batch_load/column_resolver.rb', line 64

def otu(columns)
  r = BatchLoad::ColumnResolver::Result.new

  if columns['otu_id'].present?
    begin
      r.assign Otu.find(columns['otu_id'])
    rescue => _e # ActiveRecord::RecordNotFound
      r.error_messages << "No OTU with id #{columns['otu_id']} exists."
    end
  elsif columns['otu_name'].present?
    r.assign Otu.where(name: columns['otu_name'], project_id: columns['project_id']).limit(10).to_a
    r.error_messages << "Multiple OTUs matched the name '#{columns['otu_name']}'." if r.multiple_matches?
    r.error_messages << "No OTU with name '#{columns['otu_name']}' exists." if r.no_matches?
  elsif columns['taxon_name'].present?
    r.assign Otu.joins(:taxon_name).where(taxon_names: {cached: columns['taxon_name']}, project_id: columns['project_id']).limit(10).to_a
    r.error_messages << "Multiple OTUs matched the taxon name '#{columns['taxon_name']}'." if r.multiple_matches?
    r.error_messages << "No OTU with taxon name '#{columns['taxon_name']}' exists." if r.no_matches?
  elsif columns.key?('otuname') || columns.key?('name')
    name = columns['otuname'] || columns['name']
    proj_id = columns['project_id']
    list = Otu.where(name: name, project_id: proj_id).limit(2)
    if list.blank? # treat it like a taxon name
      list = Otu.joins(:taxon_name)
                 .where(taxon_names: {cached: name}, project_id: proj_id)
                 .limit(2)
    end
    r.assign(list.to_a)
    r.error_messages << "Multiple OTUs matched the name '#{name}'." if r.multiple_matches?
    r.error_messages << "No OTU with name '#{name}' exists." if r.no_matches?
  else
    r.error_messages << 'No column suitable for OTU resolution was provided.'
  end

  r
end

.shape(columns, data_origin = nil) ⇒ BatchLoad::ColumnResolver::Result

rubocop:disable Metrics/MethodLength

Parameters:

  • columns (Hash)
  • data_origin (String) (defaults to: nil)

Returns:



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/batch_load/column_resolver.rb', line 169

def shape(columns, data_origin = nil)
  r = BatchLoad::ColumnResolver::Result.new

  if columns['geographic_area_id']
    begin
      r.assign(GeographicArea.find(columns['geographic_area_id']))
    rescue ActiveRecord::RecordNotFound
      r.error_messages << "No Geographic area with id #{columns['geographic_area_id']} exists."
    end
  elsif columns['geographic_area_name']
    search_list = columns['geographic_area_name']
    # @tuckerjd - fix the data_origin logic
    if data_origin.blank?
      data_origin = 'blank'
      r.assign(GeographicArea.where(name: search_list).limit(10).to_a)
    else
      r.assign(GeographicArea.where(name: search_list, data_origin: data_origin).limit(10).to_a)
    end

    # @tuckerjd - tweak as necessary
    r.error_messages << "Multiple matches to '#{search_list}' (data_origin: #{data_origin}) were found." if r.multiple_matches?
    r.error_messages << "'#{search_list}' (data_origin: #{data_origin}): Geographic area was not determinable." if r.no_matches?

  elsif columns['country'] || columns['state'] || columns['county']
    search_list = [columns['country'], columns['state'], columns['county']].compact.join(', ')

    # @tuckerjd - tweak as necessary to include data_origin
    if data_origin.blank?
      data_origin = 'blank'
      r.assign(GeographicArea.with_name_and_parent_names([columns['county'], columns['state'], columns['country']].compact).to_a)
    else
      r.assign(GeographicArea.where(data_origin: data_origin).with_name_and_parent_names([columns['county'], columns['state'], columns['country']].compact).to_a)
    end
    # @tuckerjd - tweak as necessary
    r.error_messages << "'#{search_list}' (data_origin: #{data_origin}): Geographic area was not determinable." if r.no_matches?
    r.error_messages << "Multiple matches to '#{search_list}' (data_origin: #{data_origin}) were found." if r.multiple_matches?
  elsif columns['gazetteer_id']
    begin
      r.assign(Gazetteer.find(columns['gazetteer_id']))
    rescue ActiveRecord::RecordNotFound
      r.error_messages << "No Gazetteer with id #{columns['gazetteer_id']} exists."
    end
  end

  r
end

.source(columns) ⇒ BatchLoad::ColumnResolver::Result

Parameters:

  • columns (Hash)

Returns:



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/batch_load/column_resolver.rb', line 129

def source(columns)
  r = BatchLoad::ColumnResolver::Result.new

  if columns['source_id']
    begin
      r.assign Source.find(columns['source_id'])
    rescue ActiveRecord::RecordNotFound
      r.error_messages << "No source with id #{columns['source_id']} exists."
    end
  elsif columns['doi']
    r.assign Source.where(doi: columns['doi']).limit(2).to_a # identifier is cached here, so we don't have to join Identifers
    r.error_messages << "Multiple matches to the DOI '#{columns['doi']}' were found." if r.multiple_matches?
  elsif columns['citation']
    citation_value = columns['citation']
    matches = Source.where(cached: citation_value).limit(2).to_a

    if matches.empty? && citation_value.present?
      normalized_citation = strip_html(citation_value).squish
      # Slow un-indexed search
      matches = Source
        .where(
          "#{strip_html_sql('cached')} = ?",
          normalized_citation
        )
        .limit(2)
        .to_a
    end

    r.assign matches
    r.error_messages << "Multiple matches to the citation '#{citation_value}' were found." if r.multiple_matches?
    r.error_messages << "No source with citation '#{citation_value}' exists." if r.no_matches?
  end

  r
end

.strip_html(value) ⇒ Object

rubocop:enable Metrics/MethodLength



217
218
219
220
221
222
223
224
# File 'lib/batch_load/column_resolver.rb', line 217

def strip_html(value)
  value
    .to_s
    .gsub('<i>', '')
    .gsub('</i>', '')
    .gsub('<em>', '')
    .gsub('</em>', '')
end

.strip_html_sql(column_name) ⇒ Object



226
227
228
229
230
231
232
# File 'lib/batch_load/column_resolver.rb', line 226

def strip_html_sql(column_name)
  clean = column_name.to_s
  clean = "replace(#{clean}, '<i>', '')"
  clean = "replace(#{clean}, '</i>', '')"
  clean = "replace(#{clean}, '<em>', '')"
  "replace(#{clean}, '</em>', '')"
end