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
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
166
167
168
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
|
# File 'backend/app/converters/assessment_converter.rb', line 67
def self.configure
config = {}
records = 0
agents = 0
reviewers = 0
.each do |section_field|
(section, field) = section_field.split('_', 2)
name = section_field
data_path = "assessment.#{field}"
val_filter = nil
val_filter ||= @booleans.include?(section_field) ? normalize_boolean : nil
val_filter ||= @dates.include?(section_field) ? reformat_date : nil
if section_field.start_with?('basic_record')
records += 1
data_path = "records_#{records}.uri"
val_filter = record_to_uri
config["records_#{records}".intern] = {
:record_type => Proc.new {|data|
JSONModel.parse_reference(data['uri'])[:type]
},
:on_row_complete => Proc.new { |cache, record|
assessment = cache.find {|obj| obj && obj.class.record_type == 'assessment' }
assessment.records << {'ref' => record.uri}
cache.map! {|obj| (obj && obj.key == record.key) ? nil : obj}
}
}
elsif section_field.start_with?('basic_surveyed_by')
agents += 1
data_path = "agents_#{agents}.uri"
val_filter = user_to_uri
config["agents_#{agents}".intern] = {
:record_type => Proc.new {|data|
JSONModel.parse_reference(data['uri'])[:type]
},
:on_row_complete => Proc.new { |cache, agent|
assessment = cache.find {|obj| obj && obj.class.record_type == 'assessment' }
assessment.surveyed_by << {'ref' => agent.uri}
cache.map! {|obj| (obj && obj.key == agent.key) ? nil : obj}
}
}
elsif section_field.start_with?('basic_reviewer')
reviewers += 1
data_path = "reviewers_#{reviewers}.uri"
val_filter = user_to_uri
config["reviewers_#{reviewers}".intern] = {
:record_type => Proc.new {|data|
JSONModel.parse_reference(data['uri'])[:type]
},
:on_row_complete => Proc.new { |cache, agent|
assessment = cache.find {|obj| obj && obj.class.record_type == 'assessment' }
assessment.reviewer << {'ref' => agent.uri}
cache.map! {|obj| (obj && obj.key == agent.key) ? nil : obj}
}
}
elsif section == 'format'
defn = match_definition('format', field)
data_path = "#{section_field}.value"
val_filter = boolean_to_s
config[section_field.intern] = {
:record_type => 'assessment_attribute',
:on_row_complete => Proc.new { |cache, attr|
if attr.value == 'true'
assessment = cache.find {|obj| obj && obj.class.record_type == 'assessment' }
assessment.formats << { :value => 'true', :definition_id => defn[:id] }
end
}
}
elsif section == 'rating'
if field.end_with?('_note')
data_path = section_field.sub(/_note$/, '') + '.note'
else
defn = match_definition('rating', field)
data_path = "#{section_field}.value"
config[section_field.intern] = {
:record_type => 'assessment_attribute',
:on_row_complete => Proc.new { |cache, attr|
assessment = cache.find {|obj| obj && obj.class.record_type == 'assessment' }
assessment.formats << {
:value => attr.value,
:note => attr.note,
:definition_id => defn[:id]
}
}
}
end
elsif section == 'conservation'
defn = match_definition('conservation_issue', field)
data_path = "#{section_field}.value"
val_filter = boolean_to_s
config[section_field.intern] = {
:record_type => 'assessment_attribute',
:on_row_complete => Proc.new { |cache, attr|
if attr.value == 'true'
assessment = cache.find {|obj| obj && obj.class.record_type == 'assessment' }
assessment.conservation_issues << { :value => 'true', :definition_id => defn[:id] }
end
}
}
end
config[name] = [val_filter, data_path]
end
config
end
|