Class: Queries::Observation::Filter
- Inherits:
-
Query::Filter
show all
- Includes:
- Concerns::Citations, Concerns::DataAttributes, Concerns::Depictions, Concerns::Notes, Concerns::Protocols, Concerns::Tags
- Defined in:
- lib/queries/observation/filter.rb
Overview
Constant Summary
collapse
- PARAMS =
[
:character_state_id,
:collection_object_id,
:descendants,
:descriptor_id,
:descriptor_id,
:observation_id,
:observation_matrix_id,
:observation_object_global_id,
:observation_object_type,
:observation_type,
:otu_id,
:taxon_name_id,
charater_state_id: [],
collection_object_id: [],
descriptor_id: [],
observation_id: [],
observation_matrix_id: [],
observation_object_type: [],
observation_type: [],
otu_id: [],
taxon_name_id: [],
].freeze
Query::Filter::FILTER_QUERIES, Query::Filter::SUBQUERIES
Instance Attribute Summary collapse
#api, #asserted_distribution_query, #biological_association_query, #biological_associations_graph_query, #collecting_event_query, #collection_object_query, #content_query, #controlled_vocabulary_term_query, #data_attribute_query, #descriptor_query, #document_query, #dwc_occurrence_query, #extract_query, #field_occurrence_query, #image_query, #loan_query, #object_global_id, #observation_query, #otu_query, #page, #paginate, #params, #per, #person_query, #project_id, #recent, #recent_target, #taxon_name_query, #venn, #venn_mode
Attributes inherited from Query
#query_string, #terms
Instance Method Summary
collapse
#all, #all_and_clauses, #all_merge_clauses, #annotator_and_clauses, #annotator_merge_clauses, annotator_params, api_except_params, api_excluded_params, #apply_venn, #attribute_exact_facet, base_filter, base_query_name, #deep_permit, included_annotator_facets, instatiated_base_filter, inverted_subqueries, #model_id_facet, #object_global_id_facet, params, #permitted_params, #process_url_into_params, #project_id_facet, query_name, #query_name, #set_nested_queries, #shared_and_clauses, #subquery_vector, #venn_query
Methods inherited from Query
#alphabetic_strings, #alphanumeric_strings, base_name, #base_name, #base_query, #build_terms, #cached_facet, #end_wildcard, #levenshtein_distance, #match_ordered_wildcard_pieces_in_cached, #no_terms?, referenced_klass, #referenced_klass, #referenced_klass_except, #referenced_klass_intersection, #referenced_klass_union, #start_and_end_wildcard, #start_wildcard, #table, #wildcard_pieces
Constructor Details
#initialize(query_params) ⇒ Filter
Returns a new instance of Filter.
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/queries/observation/filter.rb', line 69
def initialize(query_params)
super
@observation_id = params[:observation_id]
@otu_id = params[:otu_id]
@collection_object_id = params[:collection_object_id]
@observation_object_global_id = params[:observation_object_global_id]
@descriptor_id = params[:descriptor_id]
@observation_type = params[:observation_type]
@character_state_id = params[:character_state_id]
@observation_matrix_id = params[:observation_matrix_id]
@observation_object_type = params[:observation_object_type]
@taxon_name_id = params[:taxon_name_id]
@descendants = boolean_param(params, :descendants)
set_data_attributes_params(params)
set_protocols_params(params)
set_citations_params(params)
set_depiction_params(params)
set_tags_params(params)
set_notes_params(params)
end
|
Instance Attribute Details
#character_state_id ⇒ Object
43
44
45
|
# File 'lib/queries/observation/filter.rb', line 43
def character_state_id
@character_state_id
end
|
#collection_object_id ⇒ Object
46
47
48
|
# File 'lib/queries/observation/filter.rb', line 46
def collection_object_id
@collection_object_id
end
|
#descendants ⇒ Object
49
50
51
|
# File 'lib/queries/observation/filter.rb', line 49
def descendants
@descendants
end
|
#descriptor_id ⇒ Object
52
53
54
|
# File 'lib/queries/observation/filter.rb', line 52
def descriptor_id
@descriptor_id
end
|
#observation_id ⇒ Object
40
41
42
|
# File 'lib/queries/observation/filter.rb', line 40
def observation_id
@observation_id
end
|
#observation_matrix_id ⇒ Array
55
56
57
|
# File 'lib/queries/observation/filter.rb', line 55
def observation_matrix_id
@observation_matrix_id
end
|
#observation_object_global_id ⇒ Object
58
59
60
|
# File 'lib/queries/observation/filter.rb', line 58
def observation_object_global_id
@observation_object_global_id
end
|
#observation_type ⇒ Object
61
62
63
|
# File 'lib/queries/observation/filter.rb', line 61
def observation_type
@observation_type
end
|
#otu_id ⇒ Object
64
65
66
|
# File 'lib/queries/observation/filter.rb', line 64
def otu_id
@otu_id
end
|
#taxon_name_id ⇒ Object
67
68
69
|
# File 'lib/queries/observation/filter.rb', line 67
def taxon_name_id
@taxon_name_id
end
|
Instance Method Details
#and_clauses ⇒ Object
264
265
266
267
268
269
270
271
272
273
274
|
# File 'lib/queries/observation/filter.rb', line 264
def and_clauses
[
character_state_id_facet,
collection_object_id_facet,
descriptor_id_facet,
observation_object_global_id_facet,
observation_object_type_facet,
observation_type_facet,
otu_id_facet,
]
end
|
#character_state_id_facet ⇒ Arel::Node?
190
191
192
193
|
# File 'lib/queries/observation/filter.rb', line 190
def character_state_id_facet
return nil if character_state_id.empty?
table[:character_state_id].in(character_state_id)
end
|
#collection_object_id_facet ⇒ Object
178
179
180
181
|
# File 'lib/queries/observation/filter.rb', line 178
def collection_object_id_facet
return nil if collection_object_id.empty?
table[:observation_object_id].eq(collection_object_id).and(table[:observation_object_type].eq('CollectionObject'))
end
|
#collection_object_query_facet ⇒ Object
221
222
223
224
225
226
227
228
229
230
|
# File 'lib/queries/observation/filter.rb', line 221
def collection_object_query_facet
return nil if collection_object_query.nil?
s = 'WITH query_co_obs AS (' + collection_object_query.all.to_sql + ') ' +
::Observation
.joins("JOIN query_co_obs as query_co_obs1 on observations.observation_object_id = query_co_obs1.id and observations.observation_object_type = 'CollectionObject'")
.to_sql
::Observation.from('(' + s + ') as observations')
end
|
#descriptor_id_facet ⇒ Object
205
206
207
208
|
# File 'lib/queries/observation/filter.rb', line 205
def descriptor_id_facet
return nil if descriptor_id.empty?
table[:descriptor_id].in(descriptor_id)
end
|
#descriptor_query_facet ⇒ Object
210
211
212
213
214
215
216
217
218
219
|
# File 'lib/queries/observation/filter.rb', line 210
def descriptor_query_facet
return nil if descriptor_query.nil?
s = 'WITH query_desc_obs AS (' + descriptor_query.all.to_sql + ') ' +
::Observation
.joins('JOIN query_desc_obs as query_desc_obs1 on observations.descriptor_id = query_desc_obs1.id')
.to_sql
::Observation.from('(' + s + ') as observations')
end
|
#matching_character_state_id ⇒ Arel::Node?
174
175
176
|
# File 'lib/queries/observation/filter.rb', line 174
def matching_character_state_id
character_state_id.blank? ? nil : table[:character_state_id].eq(character_state_id)
end
|
#merge_clauses ⇒ Object
276
277
278
279
280
281
282
283
284
285
286
|
# File 'lib/queries/observation/filter.rb', line 276
def merge_clauses
[
collection_object_query_facet,
descriptor_query_facet,
observation_matrix_id_facet,
otu_query_facet,
taxon_name_query_facet,
taxon_name_id_facet,
]
end
|
#observation_matrix_id_facet ⇒ Object
155
156
157
158
|
# File 'lib/queries/observation/filter.rb', line 155
def observation_matrix_id_facet
return nil if observation_matrix_id.empty?
::Observation.in_observation_matrix(observation_matrix_id)
end
|
#observation_object_global_id_facet ⇒ Arel::Node?
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/queries/observation/filter.rb', line 161
def observation_object_global_id_facet
return nil if observation_object_global_id.blank?
o = GlobalID::Locator.locate(observation_object_global_id)
a = o.id
b = o.class.base_class.name
table[:observation_object_id].eq(a).and(table[:observation_object_type].eq(b))
end
|
#observation_object_type ⇒ Object
97
98
99
|
# File 'lib/queries/observation/filter.rb', line 97
def observation_object_type
[@observation_object_type].flatten.compact.uniq
end
|
#observation_object_type_facet ⇒ Object
200
201
202
203
|
# File 'lib/queries/observation/filter.rb', line 200
def observation_object_type_facet
return nil if observation_object_type.empty?
table[:observation_object_type].in(observation_object_type)
end
|
#observation_type_facet ⇒ Arel::Node?
184
185
186
187
|
# File 'lib/queries/observation/filter.rb', line 184
def observation_type_facet
return nil if observation_type.empty?
table[:type].in(observation_type)
end
|
#otu_id_facet ⇒ Object
195
196
197
198
|
# File 'lib/queries/observation/filter.rb', line 195
def otu_id_facet
return nil if otu_id.empty?
table[:observation_object_id].in(otu_id).and(table[:observation_object_type].eq('Otu'))
end
|
#otu_query_facet ⇒ Object
232
233
234
235
236
237
238
239
240
241
|
# File 'lib/queries/observation/filter.rb', line 232
def otu_query_facet
return nil if otu_query.nil?
s = 'WITH query_otu_obs AS (' + otu_query.all.to_sql + ') ' +
::Observation
.joins("JOIN query_otu_obs as query_otu_obs1 on observations.observation_object_id = query_otu_obs1.id and observations.observation_object_type = 'Otu'")
.to_sql
::Observation.from('(' + s + ') as observations')
end
|
#taxon_name_id_facet ⇒ Object
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
|
# File 'lib/queries/observation/filter.rb', line 129
def taxon_name_id_facet
return nil if taxon_name_id.empty?
if descendants
t = ::TaxonName.descendants_of(taxon_name_id)
else
t = ::TaxonName.where(id: taxon_name_id)
end
a = ::Observation.joins("JOIN otus ON observations.observation_object_id = otus.id AND observations.observation_object_type = 'Otu'")
.where(otus: {taxon_name_id: t})
b = ::Observation.joins("JOIN collection_objects ON observations.observation_object_id = collection_objects.id AND observations.observation_object_type = 'CollectionObject'")
.joins("JOIN taxon_determinations ON taxon_determinations.taxon_determination_object_id = collection_objects.id AND taxon_determinations.taxon_determination_object_type = 'CollectionObject'")
.joins('JOIN otus ON taxon_determinations.otu_id = otus.id')
.joins('JOIN taxon_names ON taxon_names.id = otus.id')
.where(taxon_names: {id: t})
e = ::Queries::Extract::Filter.new(taxon_name_id:, descendants:, project_id:).all
c = ::Observation.joins("JOIN extracts ON observations.observation_object_id = extracts.id AND observations.observation_object_type = 'Extract'")
.where(extracts: {id: e})
::Observation.from( '(' + [a,b,c].collect{|q| "(#{q.to_sql})" }.join(' UNION ') + ') as observations' )
end
|
#taxon_name_query_facet ⇒ Object
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
# File 'lib/queries/observation/filter.rb', line 243
def taxon_name_query_facet
return nil if taxon_name_query.nil?
s = 'WITH query_tn_obs AS (' + taxon_name_query.all.to_sql + ') '
a = ::Observation
.joins("JOIN collection_objects on observations.observation_object_id = collection_objects.id and observations.observation_object_type = 'CollectionObject'")
.joins("JOIN taxon_determinations on collection_objects.id = taxon_determinations.taxon_determination_object_id AND taxon_determinations.taxon_determination_objec_type = 'CollectionObject'")
.joins('JOIN otus on taxon_determinations.otu_id = otus.id')
.joins('JOIN query_tn_obs as query_tn_obs1 on otus.taxon_name_id = query_tn_obs1.id')
.to_sql
b = ::Observation
.joins("JOIN otus on observations.observation_object_id = otus.id and observations.observation_object_type = 'Otu'")
.joins('JOIN query_tn_obs as query_tn_obs2 on otus.taxon_name_id = query_tn_obs2.id')
.to_sql
s << ::Observation.from("((#{a}) UNION (#{b})) as observations").to_sql
::Observation.from('(' + s + ') as observations')
end
|