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 results

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

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

Parameters:

  • columns (Hash)

Returns:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/batch_load/column_resolver.rb', line 76

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:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/batch_load/column_resolver.rb', line 12

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 contents for \'Value\' was provided.' if value.blank?
  r.error_messages << 'No contents 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

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

rubocop:disable Metrics/MethodLength

Parameters:

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

Returns:



126
127
128
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
164
165
# File 'lib/batch_load/column_resolver.rb', line 126

def geographic_area(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['source_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?

  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?
  end

  r
end

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

rubocop:disable Metrics/MethodLength

Parameters:

  • columns (Hash)

Returns:



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
68
69
70
# File 'lib/batch_load/column_resolver.rb', line 37

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

  if columns['otu_id']
    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']
    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']
    r.assign Otu.joins(:taxon_names).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?
  elsif columns.key?('otuname')
    name = columns['otuname']
    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

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

Parameters:

  • columns (Hash)

Returns:



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/batch_load/column_resolver.rb', line 101

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(10).to_a # identifier is cached here, so we don't have to join Identifers
    r.error_messages << "Multiple matches to the DOI '#{column['doi']}' were found." if r.multiple_matches?
  elsif columns['citation']
    r.assign Source.where(cached: columns['citation']).limit(10).to_a
    r.error_messages << "Multiple matches to the citation '#{column['citation']}' were found." if r.multiple_matches?
    r.error_messages << "No source with citation '#{columns['citation']}' exists." if r.no_matches?
  end

  r
end