Class: Queries::Sound::Filter

Inherits:
Query::Filter show all
Includes:
Concerns::Citations, 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

#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?, #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)


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

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_notes_params(params)
  set_tags_params(params)
end

Instance Attribute Details

#collecting_event_idArray

Returns:

  • (Array)


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

def collecting_event_id
  @collecting_event_id
end

#collection_object_idArray

Returns:

  • (Array)


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

def collection_object_id
  @collection_object_id
end

#conveyance_object_typeArray

Parameters:

  • conveyance_object_type

    one or more names of classes.

Returns:

  • (Array)


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

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



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

def conveyances
  @conveyances
end

#field_occurrence_idArray

Returns:

  • (Array)


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

def field_occurrence_id
  @field_occurrence_id
end

#nameObject

Returns String.

Returns:

  • String



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

def name
  @name
end

#name_exactBoolean?

Returns:

  • (Boolean, nil)


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

def name_exact
  @name_exact
end

#otu_idArray

Returns:

  • (Array)


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

def otu_id
  @otu_id
end

#otu_scopeArray

Returns:

  • (Array)


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

def otu_scope
  @otu_scope
end

#sound_idArray

Returns:

  • (Array)


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

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



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

def with_name
  @with_name
end

Instance Method Details

#and_clausesObject



264
265
266
267
268
# File 'lib/queries/sound/filter.rb', line 264

def and_clauses
  [
    name_facet
  ]
end

#collecting_event_facetObject



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

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



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

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)


124
125
126
# File 'lib/queries/sound/filter.rb', line 124

def collection_object_table
  ::CollectionObject.arel_table
end

#conveyance_object_type_facetObject



147
148
149
150
# File 'lib/queries/sound/filter.rb', line 147

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)


134
135
136
# File 'lib/queries/sound/filter.rb', line 134

def conveyance_table
  ::Conveyance.arel_table
end

#conveyances_facetObject



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

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

#field_occurrence_facetObject



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

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)


129
130
131
# File 'lib/queries/sound/filter.rb', line 129

def field_occurrence_table
  ::FieldOccurrence.arel_table
end

#merge_clausesObject



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

def merge_clauses
  s = ::Queries::Query::Filter::SUBQUERIES.select{|k,v| v.include?(:sound)}.keys.map(&:to_s) - ['source', 'observation']
  [
    *s.collect{|m| query_facets_facet(m)}, # Reference all the Sound referencing SUBQUERIES
    conveyance_object_type_facet,
    conveyances_facet,
    observation_query_facet,
    otu_id_facet,
    otu_scope_facet,
    collecting_event_facet,
    collection_object_facet,
    field_occurrence_facet,
    with_name_facet
  ]
end

#name_facetObject



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

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



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

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



190
191
192
193
# File 'lib/queries/sound/filter.rb', line 190

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



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

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



195
196
197
# File 'lib/queries/sound/filter.rb', line 195

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

#otu_id_facetObject



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

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

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

#otu_scope_facetObject



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

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)


119
120
121
# File 'lib/queries/sound/filter.rb', line 119

def otu_table
  ::Otu.arel_table
end

#query_facets_facet(name = nil) ⇒ Object



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

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

#with_name_facetObject



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

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