Class: Queries::Sound::Filter

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

Constant Summary collapse

PARAMS =
[
  :conveyance_object_type,
  :conveyances,
  :name_exact,
  :sound_id,
  :name,
  :otu_id,
  :otu_scope,
  :field_occurrence,
  :with_name,

  field_occurrence_id: [],
  collection_object_id: [],
  collecting_event_id: [],
  conveyance_object_type: [],
  sound_id: [],
  otu_id: [],
  otu_scope: [],
].freeze

Constants inherited from Query::Filter

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

Instance Attribute Summary collapse

Attributes inherited from Query::Filter

#anatomical_part_query, #api, #asserted_distribution_query, #asserted_environment_query, #biological_association_query, #biological_associations_graph_query, #collecting_event_query, #collection_object_query, #content_query, #controlled_vocabulary_term_query, #conveyance_query, #data_attribute_query, #depiction_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, #roll_call, #sound_query, #taxon_name_query, #taxon_name_relationship_query, #venn, #venn_ignore_pagination, #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, #disable_paging, included_annotator_facets, instantiated_base_filter, inverted_subqueries, #model_id_facet, #object_global_id_facet, #only_project?, #only_project_or_less?, #paging_state, params, #permitted_params, #process_url_into_params, #project_id_facet, query_name, #set_nested_queries, #set_paging, set_paging, #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.

Parameters:

  • params (Hash)


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

def initialize(query_params)
  super

  @collecting_event_id = params[:collecting_event_id]
  @collection_object_id = params[:collection_object_id]
  @conveyance_object_type = params[:conveyance_object_type]
  @conveyances = boolean_param(params, :conveyances)
  @field_occurrence_id = params[:field_occurrence_id]
  @name = params[:name]
  @name_exact = boolean_param(params, :name_exact)
  @otu_id = params[:otu_id]
  @otu_scope = params[:otu_scope]
  @sound_id = params[:sound_id]
  @with_name = boolean_param(params, :with_name)

  set_citations_params(params)
  set_data_attributes_params(params)
  set_notes_params(params)
  set_tags_params(params)
end

Instance Attribute Details

#collecting_event_id ⇒ Array

Returns:

  • (Array)


30
31
32
# File 'lib/queries/sound/filter.rb', line 30

def collecting_event_id
  @collecting_event_id
end

#collection_object_id ⇒ Array

Returns:

  • (Array)


33
34
35
# File 'lib/queries/sound/filter.rb', line 33

def collection_object_id
  @collection_object_id
end

#conveyance_object_type ⇒ Array

Parameters:

  • conveyance_object_type —

    one or more names of classes.

Returns:

  • (Array)


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

def conveyance_object_type
  @conveyance_object_type
end

#conveyances ⇒ Boolean?

Returns true - sound is used (in a conveyance) false - sound is not used nil - either.

Returns:

  • (Boolean, nil) —

    true - sound is used (in a conveyance) false - sound is not used nil - either



42
43
44
# File 'lib/queries/sound/filter.rb', line 42

def conveyances
  @conveyances
end

#field_occurrence_id ⇒ Array

Returns:

  • (Array)


36
37
38
# File 'lib/queries/sound/filter.rb', line 36

def field_occurrence_id
  @field_occurrence_id
end

#name ⇒ Object

Returns String.

Returns:

  • String



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

def name
  @name
end

#name_exact ⇒ Boolean?

Returns:

  • (Boolean, nil)


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

def name_exact
  @name_exact
end

#otu_id ⇒ Array

Returns:

  • (Array)


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

def otu_id
  @otu_id
end

#otu_scope ⇒ Array

Returns:

  • (Array)


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

def otu_scope
  @otu_scope
end

#sound_id ⇒ Array

Returns:

  • (Array)


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

def sound_id
  @sound_id
end

#with_name ⇒ Object

Returns Boolean true - has name false - has no name nil - both.

Returns:

  • Boolean true - has name false - has no name nil - both



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

def with_name
  @with_name
end

Instance Method Details

#anatomical_part_query_facet ⇒ Object



252
253
254
255
256
257
258
259
260
# File 'lib/queries/sound/filter.rb', line 252

def anatomical_part_query_facet
  return nil if anatomical_part_query.nil?

  ::Sound
    .joins(:related_origin_relationships)
    .where(origin_relationships: { old_object_type: 'AnatomicalPart' })
    .where("origin_relationships.old_object_id IN (#{ anatomical_part_query.all.select(:id).to_sql })")
    .distinct
end

#and_clauses ⇒ Object



292
293
294
295
296
297
# File 'lib/queries/sound/filter.rb', line 292

def and_clauses
  [
    name_facet,
    sound_id_facet
  ]
end

#collecting_event_facet ⇒ Object



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

def collecting_event_facet
  return nil if collecting_event_id.empty?

  ::Sound.joins(:collecting_events).where(collecting_events: {id: collecting_event_id})
end

#collection_object_facet ⇒ Object



174
175
176
177
178
# File 'lib/queries/sound/filter.rb', line 174

def collection_object_facet
  return nil if collection_object_id.empty?

  ::Sound.joins(:collection_objects).where(collection_objects: {id: collection_object_id})
end

#collection_object_table ⇒ Arel::Table

Returns:

  • (Arel::Table)


126
127
128
# File 'lib/queries/sound/filter.rb', line 126

def collection_object_table
  ::CollectionObject.arel_table
end

#conveyance_object_type_facet ⇒ Object



154
155
156
157
# File 'lib/queries/sound/filter.rb', line 154

def conveyance_object_type_facet
  return nil if conveyance_object_type.empty?
  ::Sound.joins(:conveyances).where(conveyances: {conveyance_object_type:}).distinct
end

#conveyance_table ⇒ Arel::Table

Returns:

  • (Arel::Table)


136
137
138
# File 'lib/queries/sound/filter.rb', line 136

def conveyance_table
  ::Conveyance.arel_table
end

#conveyances_facet ⇒ Object



159
160
161
162
163
164
165
166
# File 'lib/queries/sound/filter.rb', line 159

def conveyances_facet
  return nil if conveyances.nil?
  if conveyances
    ::Sound.joins(:conveyances)
  else
    ::Sound.where.missing(:conveyances)
  end
end

#field_occurrence_facet ⇒ Object



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

def field_occurrence_facet
  return nil if field_occurrence_id.empty?

  ::Sound.joins(:field_occurrences).where(field_occurrences: {id: field_occurrence_id})
end

#field_occurrence_table ⇒ Arel::Table

Returns:

  • (Arel::Table)


131
132
133
# File 'lib/queries/sound/filter.rb', line 131

def field_occurrence_table
  ::FieldOccurrence.arel_table
end

#merge_clauses ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/queries/sound/filter.rb', line 299

def merge_clauses
  # anatomical_part, observation, otu, taxon_name each have a dedicated
  # facet below; the rest are resolved through conveyances.
  s = ::Queries::Query::Filter::SUBQUERIES.select{|k,v| v.include?(:sound)}.keys.map(&:to_s) - ['source', 'anatomical_part', 'observation', 'otu', 'taxon_name']
  [
    *s.collect{|m| query_facets_facet(m)}, # Reference all the Sound referencing SUBQUERIES
    anatomical_part_query_facet,
    conveyance_object_type_facet,
    conveyances_facet,
    observation_query_facet,
    otu_query_facet,
    otu_id_facet,
    otu_scope_facet,
    collecting_event_facet,
    collection_object_facet,
    field_occurrence_facet,
    taxon_name_query_facet,
    with_name_facet
  ]
end

#name_facet ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/queries/sound/filter.rb', line 140

def name_facet
  return nil if name.blank?
  if name_exact
    table[:name].eq(name.strip)
  else
    table[:name].matches('%' + name.strip.gsub(/\s/, '%') + '%')
  end
end

#observation_query_facet ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
# File 'lib/queries/sound/filter.rb', line 240

def observation_query_facet
  return nil if observation_query.nil?

  s = ::Sound
    .with(obs_query: observation_query.all)
    .joins(:observations)
    .joins('JOIN obs_query on observations.id = obs_query.id')
    .to_sql

  ::Sound.from('(' + s + ') as sound').distinct
end

#otu_facet_collection_objects(otu_ids) ⇒ Object



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

def otu_facet_collection_objects(otu_ids)
  ::Sound.joins(collection_objects: [:taxon_determinations])
   .where(taxon_determinations: {otu_id: otu_ids})
end

#otu_facet_field_occurrence(otu_ids) ⇒ Object



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

def otu_facet_field_occurrence(otu_ids)
  ::Sound.joins(field_occurrences: [:taxon_determinations])
    .where(taxon_determinations: {otu_id: otu_ids})
end

#otu_facet_otus(otu_ids) ⇒ Object



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

def otu_facet_otus(otu_ids)
  ::Sound.joins(:conveyances).where(conveyances: { conveyance_object_type: 'Otu', conveyance_object_id: otu_ids })
end

#otu_id_facet ⇒ Object



168
169
170
171
172
# File 'lib/queries/sound/filter.rb', line 168

def otu_id_facet
  return nil if otu_id.empty? || !otu_scope.empty?

  ::Sound.joins(:otus).where(otus: {id: otu_id})
end

#otu_query_facet ⇒ Object



262
263
264
265
# File 'lib/queries/sound/filter.rb', line 262

def otu_query_facet
  return nil if otu_query.nil?
  sound_from_otu_ids(otu_query.all.select(:id))
end

#otu_scope_facet ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/queries/sound/filter.rb', line 206

def otu_scope_facet
  return nil if otu_id.empty? || otu_scope.empty?

  selected = []

  if otu_scope.include?(:all)
    selected = [
      :otu_facet_otus,
      :otu_facet_collection_objects,
      :otu_facet_field_occurrence,
    ]
  elsif otu_scope.empty?
    selected = [:otu_facet_otus]
  else
    selected.push :otu_facet_otus if otu_scope.include?(:otus)
    selected.push :otu_facet_collection_objects if otu_scope.include?(:collection_objects)
    selected.push :otu_facet_field_occurrence if otu_scope.include?(:field_occurrences)
  end

  q = selected.collect{|a| '(' + send(a, otu_id).to_sql + ')'}.join(' UNION ')

  d = ::Sound.from('(' + q + ')' + ' as sounds')
  d
end

#otu_table ⇒ Arel::Table

Returns:

  • (Arel::Table)


121
122
123
# File 'lib/queries/sound/filter.rb', line 121

def otu_table
  ::Otu.arel_table
end

#query_facets_facet(name = nil) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/queries/sound/filter.rb', line 274

def query_facets_facet(name = nil)
  return nil if name.nil?

  q = send((name + '_query').to_sym)

  return nil if q.nil?

  n = "query_#{name}_snd"

  s = "WITH #{n} AS (" + q.all.to_sql + ') ' +
    ::Sound
    .joins(:conveyances)
    .joins("JOIN #{n} as #{n}1 on conveyances.conveyance_object_id = #{n}1.id AND conveyances.conveyance_object_type = '#{name.treetop_camelize}'")
    .to_sql

  ::Sound.from('(' + s + ') as sounds').distinct
end

#sound_from_otu_ids(otu_ids) ⇒ Object



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/queries/sound/filter.rb', line 320

def sound_from_otu_ids(otu_ids)
  anatomical_part_ids = ::AnatomicalPart.where(cached_otu_id: otu_ids).select(:id)
  collection_object_ids = ::CollectionObject
    .joins(:taxon_determinations)
    .where(taxon_determinations: {otu_id: otu_ids})
    .select(:id)
  field_occurrence_ids = ::FieldOccurrence
    .joins(:taxon_determinations)
    .where(taxon_determinations: {otu_id: otu_ids})
    .select(:id)
  collecting_event_ids = ::CollectingEvent
    .joins(collection_objects: :taxon_determinations)
    .where(taxon_determinations: {otu_id: otu_ids})
    .select(:id)

  queries = [
    otu_facet_otus(otu_ids),
    otu_facet_collection_objects(otu_ids),
    otu_facet_field_occurrence(otu_ids),
    ::Sound.joins(:collecting_events).where(collecting_events: {id: collecting_event_ids}),
    ::Sound.joins(:conveyances).where(conveyances: {conveyance_object_type: 'AnatomicalPart', conveyance_object_id: anatomical_part_ids}),
    ::Sound.joins(:related_origin_relationships).where(origin_relationships: {old_object_type: 'AnatomicalPart', old_object_id: anatomical_part_ids}),
    ::Sound.joins(:related_origin_relationships).where(origin_relationships: {old_object_type: 'Otu', old_object_id: otu_ids}),
    ::Sound.joins(:related_origin_relationships).where(origin_relationships: {old_object_type: ['CollectionObject', 'Specimen', 'Lot', 'RangedLot'], old_object_id: collection_object_ids}),
    ::Sound.joins(:related_origin_relationships).where(origin_relationships: {old_object_type: 'FieldOccurrence', old_object_id: field_occurrence_ids}),
    ::Sound.joins(:related_origin_relationships).where(origin_relationships: {old_object_type: 'CollectingEvent', old_object_id: collecting_event_ids})
  ]

  referenced_klass_union(queries)
end

#sound_id_facet ⇒ Object



149
150
151
152
# File 'lib/queries/sound/filter.rb', line 149

def sound_id_facet
  return nil if sound_id.empty?
  table[:id].in(sound_id)
end

#taxon_name_query_facet ⇒ Object



267
268
269
270
271
272
# File 'lib/queries/sound/filter.rb', line 267

def taxon_name_query_facet
  return nil if taxon_name_query.nil?

  otu_ids = ::Otu.where(taxon_name_id: taxon_name_query.all.select(:id)).select(:id)
  sound_from_otu_ids(otu_ids)
end

#with_name_facet ⇒ Object



231
232
233
234
235
236
237
238
# File 'lib/queries/sound/filter.rb', line 231

def with_name_facet
  return nil if with_name.nil?
  if with_name
    ::Sound.where.not(name: nil)
  else
    ::Sound.where(name: nil)
  end
end