Class: BatchLoad::Import::Otus::DataAttributesInterpreter

Inherits:
BatchLoad::Import show all
Defined in:
lib/batch_load/import/otus/data_attributes_interpreter.rb

Constant Summary collapse

SAVE_ORDER =
[:predicate, :otu, :data_attribute, :citation]

Instance Attribute Summary collapse

Attributes inherited from BatchLoad::Import

#create_attempted, #csv, #errors, #file, #file_errors, #import_level, #processed, #processed_rows, #project, #project_id, #successful_rows, #total_data_lines, #total_lines, #user, #user_header_map, #user_id

Instance Method Summary collapse

Methods inherited from BatchLoad::Import

#all_objects, #create_attempted?, #import_level_ok?, #line_strict_level_ok?, #processed?, #ready_to_create?, #save_order, #sorted_processed_rows, #strict_level_ok?, #total_records_created, #user_map, #valid?, #valid_objects, #warn_level_ok?

Constructor Details

#initialize(**args) ⇒ DataAttributesInterpreter

Returns a new instance of DataAttributesInterpreter.

Parameters:

  • args (Hash)


23
24
25
26
27
28
29
30
31
# File 'lib/batch_load/import/otus/data_attributes_interpreter.rb', line 23

def initialize(**args)
  @create_new_otu = args.delete(:create_new_otu).present?
  @create_new_predicate = args.delete(:create_new_predicate).present?
  @type_select = args.delete(:type_select)
  source_id = args.delete(:source_id)
  @source = Source.find(source_id) if source_id.present?
  @create_citation = @source.present?
  super(**args)
end

Instance Attribute Details

#create_citationObject

Whether or not to create citationss



12
13
14
# File 'lib/batch_load/import/otus/data_attributes_interpreter.rb', line 12

def create_citation
  @create_citation
end

#create_new_otuObject

Whether or not to create unfound OTUs



6
7
8
# File 'lib/batch_load/import/otus/data_attributes_interpreter.rb', line 6

def create_new_otu
  @create_new_otu
end

#create_new_predicateObject

Whether or not to create unfound Predicates



9
10
11
# File 'lib/batch_load/import/otus/data_attributes_interpreter.rb', line 9

def create_new_predicate
  @create_new_predicate
end

#sourceObject

Sourde for citation



18
19
20
# File 'lib/batch_load/import/otus/data_attributes_interpreter.rb', line 18

def source
  @source
end

#type_selectObject

Import or Internal Attribute



15
16
17
# File 'lib/batch_load/import/otus/data_attributes_interpreter.rb', line 15

def type_select
  @type_select
end

Instance Method Details

#buildBoolean

Returns true if build process has run.

Returns:

  • (Boolean)

    true if build process has run



159
160
161
162
163
164
# File 'lib/batch_load/import/otus/data_attributes_interpreter.rb', line 159

def build
  if valid?
    build_da_for_otus
    @processed = true
  end
end

#build_da_for_otusInteger

Returns total data lines.

Returns:

  • (Integer)

    total data lines



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
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
# File 'lib/batch_load/import/otus/data_attributes_interpreter.rb', line 34

def build_da_for_otus
  @total_data_lines = 0
  i = 0
  import_klass = type_select.start_with?('im')
  att_klass = (type_select.capitalize + 'Attribute').safe_constantize

  csv.each do |real_row|
    i += 1
    row = real_row
    real_project_id = @project_id.to_s if real_row['project_id'].blank?
    row['project_id'] = real_project_id
    parse_result = BatchLoad::RowParse.new
    parse_result.objects[:otu] = []
    parse_result.objects[:data_attribute] = []
    parse_result.objects[:citation] = []  # if create_citation
    parse_result.objects[:predicate] = [] # unless import_klass
    @processed_rows[i] = parse_result

    begin # processing
      # assume we will be creating a new da
      predicate = row['predicate']
      new_da_attributes = {value: row['value'], project_id: real_project_id}
      new_da_attributes[:citations_attributes] = [{source_id: @source.id,
                                                   project_id: real_project_id}] if create_citation
      ias = BatchLoad::ColumnResolver.data_attribute(row, type_select)
      if import_klass
        new_da_attributes[:import_predicate] = predicate
      else
        new_cvt = Predicate.find_or_initialize_by(
          name: predicate,
          definition: "Imported from #{file.original_filename}",
          project_id: real_project_id)
      end
      new_da = att_klass.new(new_da_attributes)

      otus = BatchLoad::ColumnResolver.otu(row)
      find_name = row['otuname']

      if otus.no_matches? # can't find any by that name
        otus.assign(Otu.new(name: find_name, project_id: real_project_id))
      end

      # search for (and remove) pairs (otu and da) which are already represented in the database.
      [otus.item, otus.items].flatten.compact.each do |l_otu| # found one or more otus
        if l_otu.persisted? # means it was found in the database, not 'new' here
          [ias.item, ias.items].flatten.compact.each do |l_ia|
            if l_ia.attribute_subject == l_otu
              parse_result.parse_errors << 'otu/predicate/value combination already exists.'
              break
            end
          end
        end
      end
      parse_result.parse_errors << 'Can\'t resolve multiple found otus.' if otus.multiple_matches?
      ias.assign(new_da) # finished with the found das, prepare a new one to attach to a remaining otu

      parse_result.parsed = true
      otu_valid = otus.item&.valid?
      # connect data_attribute to otu
      ias.item.attribute_subject = otus.item
      # connect citation to otu
      if create_citation
        cite = new_da.citations.first
        cite.citation_object = otus.item
        # add citation
        parse_result.objects[:citation].push(cite) #if otu_valid
        cite.valid?
        parse_result.parse_errors << cite.errors.messages if cite.errors.messages.any?
      end
      unless import_klass
        # connect cvt to data_attribute
        ias.item.predicate = new_cvt
        # add predicate
        parse_result.objects[:predicate].push(new_cvt) if new_cvt.present? # (new_cvt.present? && otu_valid)
        parse_result.parse_errors << new_cvt.errors.messages if new_cvt.errors.messages.any?
      end
      # add data_attribute
      parse_result.objects[:data_attribute].push(ias.item) #if otu_valid
      parse_result.parse_errors << ias.error_messages if ias.error_messages.any?

      # add otu
      parse_result.objects[:otu].push(otus.item) #if otu_valid
      parse_result.parse_errors << otus.error_messages if otus.error_messages.any?

      @total_data_lines += 1 # if find_name.present?
    rescue => _e
      raise(_e)
    end
  end

  @total_lines = i
end

#createBoolean

Remove new objects which are not wanted, per args

Returns:

  • (Boolean)

    true



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
# File 'lib/batch_load/import/otus/data_attributes_interpreter.rb', line 129

def create
  sorted_processed_rows.each_value do |processed_row|
    otu = processed_row.objects[:otu].first
    d_a = processed_row.objects[:data_attribute].first
    cvt = processed_row.objects[:predicate].first

    if otu&.valid? && d_a&.valid?
      if (not create_new_predicate)
        if cvt&.new_record?
          processed_row.objects[:predicate] = []
          processed_row.objects[:data_attribute] = []
          processed_row.objects[:citation] = []
        end
      end
      if (not create_new_otu)
        if otu&.new_record?
          processed_row.objects[:otu] = []
          processed_row.objects[:predicate] = []
          processed_row.objects[:data_attribute] = []
          processed_row.objects[:citation] = []
        end
      end
    else # wipe out all objects
      processed_row.objects.each_key { |kee| processed_row.objects[kee] = [] }
    end
  end
  super
end