Class: Queries::Observation::Filter

Inherits:
Query::Filter show all
Includes:
Concerns::Citations, Concerns::Confidences, Concerns::DataAttributes, Concerns::Depictions, Concerns::Notes, Concerns::Protocols, Concerns::Tags
Defined in:
lib/queries/observation/filter.rb

Overview

!! TODO: needs tests

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

Constants inherited from Query::Filter

Query::Filter::FILTER_QUERIES, Query::Filter::SUBQUERIES

Instance Attribute Summary collapse

Attributes inherited from Query::Filter

#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, #order_by, #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

Methods inherited from Query::Filter

#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, base_query_to_h, #deep_permit, included_annotator_facets, instantiated_base_filter, inverted_subqueries, #model_id_facet, #object_global_id_facet, #only_project?, params, #permitted_params, #process_url_into_params, #project_id_facet, query_name, #query_name, #set_nested_queries, #shared_and_clauses, #subquery_vector, #target_and_clauses, #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.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/queries/observation/filter.rb', line 70

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_confidences_params(params)
  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_idObject

Returns Array.

Returns:

  • Array



44
45
46
# File 'lib/queries/observation/filter.rb', line 44

def character_state_id
  @character_state_id
end

#collection_object_idObject

Returns Array.

Returns:

  • Array



47
48
49
# File 'lib/queries/observation/filter.rb', line 47

def collection_object_id
  @collection_object_id
end

#descendantsObject

Returns Boolean.

Returns:

  • Boolean



50
51
52
# File 'lib/queries/observation/filter.rb', line 50

def descendants
  @descendants
end

#descriptor_idObject

Returns Array.

Returns:

  • Array



53
54
55
# File 'lib/queries/observation/filter.rb', line 53

def descriptor_id
  @descriptor_id
end

#observation_idObject

Returns Array.

Returns:

  • Array



41
42
43
# File 'lib/queries/observation/filter.rb', line 41

def observation_id
  @observation_id
end

#observation_matrix_idArray

Returns:

  • (Array)


56
57
58
# File 'lib/queries/observation/filter.rb', line 56

def observation_matrix_id
  @observation_matrix_id
end

#observation_object_global_idObject

Returns String, nil.

Returns:

  • String, nil



59
60
61
# File 'lib/queries/observation/filter.rb', line 59

def observation_object_global_id
  @observation_object_global_id
end

#observation_typeObject

Returns Array.

Returns:

  • Array



62
63
64
# File 'lib/queries/observation/filter.rb', line 62

def observation_type
  @observation_type
end

#otu_idObject

Returns Array.

Returns:

  • Array



65
66
67
# File 'lib/queries/observation/filter.rb', line 65

def otu_id
  @otu_id
end

#taxon_name_idObject

Returns Array.

Returns:

  • Array



68
69
70
# File 'lib/queries/observation/filter.rb', line 68

def taxon_name_id
  @taxon_name_id
end

Instance Method Details

#and_clausesObject



266
267
268
269
270
271
272
273
274
275
276
# File 'lib/queries/observation/filter.rb', line 266

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_facetArel::Node?

Returns:

  • (Arel::Node, nil)


192
193
194
195
# File 'lib/queries/observation/filter.rb', line 192

def character_state_id_facet
  return nil if character_state_id.empty?
  table[:character_state_id].in(character_state_id)
end

#collection_object_id_facetObject



180
181
182
183
# File 'lib/queries/observation/filter.rb', line 180

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_facetObject



223
224
225
226
227
228
229
230
231
232
# File 'lib/queries/observation/filter.rb', line 223

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_facetObject



207
208
209
210
# File 'lib/queries/observation/filter.rb', line 207

def descriptor_id_facet
  return nil if descriptor_id.empty?
  table[:descriptor_id].in(descriptor_id)
end

#descriptor_query_facetObject



212
213
214
215
216
217
218
219
220
221
# File 'lib/queries/observation/filter.rb', line 212

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_idArel::Node?

Returns:

  • (Arel::Node, nil)


176
177
178
# File 'lib/queries/observation/filter.rb', line 176

def matching_character_state_id
  character_state_id.blank? ? nil : table[:character_state_id].eq(character_state_id)
end

#merge_clausesObject



278
279
280
281
282
283
284
285
286
287
288
# File 'lib/queries/observation/filter.rb', line 278

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_facetObject



157
158
159
160
# File 'lib/queries/observation/filter.rb', line 157

def observation_matrix_id_facet
  return nil if observation_matrix_id.empty?
  ::Observation.in_observation_matrix(observation_matrix_id)
end

#observation_object_global_id_facetArel::Node?

Returns:

  • (Arel::Node, nil)


163
164
165
166
167
168
169
170
171
172
173
# File 'lib/queries/observation/filter.rb', line 163

def observation_object_global_id_facet
  return nil if observation_object_global_id.blank?

  # TODO - make a hash method to parameterize these values
  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_typeObject



99
100
101
# File 'lib/queries/observation/filter.rb', line 99

def observation_object_type
  [@observation_object_type].flatten.compact.uniq
end

#observation_object_type_facetObject



202
203
204
205
# File 'lib/queries/observation/filter.rb', line 202

def observation_object_type_facet
  return nil if observation_object_type.empty?
  table[:observation_object_type].in(observation_object_type)
end

#observation_type_facetArel::Node?

Returns:

  • (Arel::Node, nil)


186
187
188
189
# File 'lib/queries/observation/filter.rb', line 186

def observation_type_facet
  return nil if observation_type.empty?
  table[:type].in(observation_type)
end

#otu_id_facetObject



197
198
199
200
# File 'lib/queries/observation/filter.rb', line 197

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_facetObject



234
235
236
237
238
239
240
241
242
243
# File 'lib/queries/observation/filter.rb', line 234

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_facetObject



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
# File 'lib/queries/observation/filter.rb', line 131

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_facetObject



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/queries/observation/filter.rb', line 245

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