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

Returns:

  • (Array)


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

def collecting_event_id
  @collecting_event_id
end

#collection_object_idArray

Returns:

  • (Array)


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

def collection_object_id
  @collection_object_id
end

#conveyance_object_typeArray

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

#conveyancesBoolean?

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_idArray

Returns:

  • (Array)


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

def field_occurrence_id
  @field_occurrence_id
end

#nameObject

Returns String.

Returns:

  • String



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

def name
  @name
end

#name_exactBoolean?

Returns:

  • (Boolean, nil)


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

def name_exact
  @name_exact
end

#otu_idArray

Returns:

  • (Array)


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

def otu_id
  @otu_id
end

#otu_scopeArray

Returns:

  • (Array)


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

def otu_scope
  @otu_scope
end

#sound_idArray

Returns:

  • (Array)


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

def sound_id
  @sound_id
end

#with_nameObject

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_facetObject



247
248
249
250
251
252
253
# File 'lib/queries/sound/filter.rb', line 247

def anatomical_part_query_facet
  return nil if anatomical_part_query.nil?

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

#and_clausesObject



285
286
287
288
289
# File 'lib/queries/sound/filter.rb', line 285

def and_clauses
  [
    name_facet
  ]
end

#collecting_event_facetObject



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

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_facetObject



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

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_tableArel::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_facetObject



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

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

#conveyance_tableArel::Table

Returns:

  • (Arel::Table)


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

def conveyance_table
  ::Conveyance.arel_table
end

#conveyances_facetObject



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

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

#field_occurrence_facetObject



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

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_tableArel::Table

Returns:

  • (Arel::Table)


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

def field_occurrence_table
  ::FieldOccurrence.arel_table
end

#merge_clausesObject



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/queries/sound/filter.rb', line 291

def merge_clauses
  s = ::Queries::Query::Filter::SUBQUERIES.select{|k,v| v.include?(:sound)}.keys.map(&:to_s) - ['source', '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_facetObject



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_facetObject



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

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



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

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



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

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



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

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

#otu_id_facetObject



163
164
165
166
167
# File 'lib/queries/sound/filter.rb', line 163

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

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

#otu_query_facetObject



255
256
257
258
# File 'lib/queries/sound/filter.rb', line 255

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

#otu_scope_facetObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/queries/sound/filter.rb', line 201

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_tableArel::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



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/queries/sound/filter.rb', line 267

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



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/queries/sound/filter.rb', line 310

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

#taxon_name_query_facetObject



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

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_facetObject



226
227
228
229
230
231
232
233
# File 'lib/queries/sound/filter.rb', line 226

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