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

!! 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, #extract_query, #image_query, #loan_query, #object_global_id, #observation_query, #otu_query, #page, #paginate, #params, #per, #person_query, #project_id, #recent, #taxon_name_query

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, #attribute_exact_facet, #deep_permit, included_annotator_facets, inverted_subqueries, #model_id_facet, #object_global_id_facet, params, #permitted_params, #project_id_facet, #set_nested_queries, #shared_and_clauses, #subquery_vector

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_idObject

Returns Array.

Returns:

  • Array



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

def character_state_id
  @character_state_id
end

#collection_object_idObject

Returns Array.

Returns:

  • Array



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

def collection_object_id
  @collection_object_id
end

#descendantsObject

Returns Boolean.

Returns:

  • Boolean



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

def descendants
  @descendants
end

#descriptor_idObject

Returns Array.

Returns:

  • Array



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

def descriptor_id
  @descriptor_id
end

#observation_idObject

Returns Array.

Returns:

  • Array



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

def observation_id
  @observation_id
end

#observation_matrix_idArray

Returns:

  • (Array)


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

def observation_matrix_id
  @observation_matrix_id
end

#observation_object_global_idObject

Returns String, nil.

Returns:

  • String, nil



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

def observation_object_global_id
  @observation_object_global_id
end

#observation_typeObject

Returns Array.

Returns:

  • Array



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

def observation_type
  @observation_type
end

#otu_idObject

Returns Array.

Returns:

  • Array



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

def otu_id
  @otu_id
end

#taxon_name_idObject

Returns Array.

Returns:

  • Array



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

def taxon_name_id
  @taxon_name_id
end

Instance Method Details

#and_clausesObject



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

Returns:

  • (Arel::Node, nil)


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_facetObject



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_facetObject



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_facetObject



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_facetObject



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

Returns:

  • (Arel::Node, nil)


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_clausesObject



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_facetObject



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

Returns:

  • (Arel::Node, nil)


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?

  # 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



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_facetObject



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

Returns:

  • (Arel::Node, nil)


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_facetObject



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_facetObject



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_facetObject



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.biological_collection_object_id = collection_objects.id')
    .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



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.biological_collection_object_id')
    .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