Class: Queries::Otu::Filter

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

Constant Summary collapse

PARAMS =
[
  :asserted_distributions,
  :biological_association_id,
  :biological_associations,
  :collecting_event_id,
  :collection_objects,
  :contents,
  :coordinatify,
  :descendants,
  :descriptor_id,
  :geo_json,
  :geographic_area_id,
  :geographic_area_mode,
  :name,
  :name_exact,
  :observations,
  :otu_id,
  :radius,
  :taxon_name,
  :taxon_name_id,
  :wkt,

  collecting_event_id: [],
  descriptor_id: [],
  geographic_area_id: [],
  name: [],
  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, #descriptor_query, #extract_query, #image_query, #loan_query, #object_global_id, #observation_query, #otu_query, #params, #person_query, #project_id, #recent, #taxon_name_query

Attributes inherited from Query

#query_string, #terms

Instance Method Summary collapse

Methods included from Helpers

#boolean_param

Methods inherited from Query::Filter

#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_union, #start_and_end_wildcard, #start_wildcard, #table, #wildcard_pieces

Constructor Details

#initialize(query_params) ⇒ Filter

Returns a new instance of Filter.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/queries/otu/filter.rb', line 161

def initialize(query_params)
  super

  @asserted_distributions = boolean_param(params, :asserted_distributions)
  @biological_association_id = params[:biological_association_id]
  @biological_associations = boolean_param(params, :biological_associations)
  @collecting_event_id = params[:collecting_event_id]
  @collection_objects = boolean_param(params, :collection_objects)
  @contents = boolean_param(params, :contents)
  @coordinatify = boolean_param(params, :coordinatify)
  @descendants = boolean_param(params, :descendants)
  @descriptor_id = params[:descriptor_id]
  @geo_json = params[:geo_json]
  @geographic_area_id = params[:geographic_area_id]
  @geographic_area_mode = boolean_param(params, :geographic_area_mode)
  @historical_determinations = boolean_param(params, :historical_determinations)
  @name = params[:name]
  @name_exact = boolean_param(params, :name_exact)
  @observations = boolean_param(params, :observations)
  @otu_id = params[:otu_id]
  @radius = params[:radius].presence || 100.0
  @taxon_name = boolean_param(params, :taxon_name)
  @taxon_name_id = params[:taxon_name_id]
  @wkt = params[:wkt]

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

Instance Attribute Details

#asserted_distributionsTrue, ...

Returns true - Otu has AssertedDistribution false - Otu without AssertedDistribution nil - not applied.

Returns:

  • (True, False, nil)

    true - Otu has AssertedDistribution false - Otu without AssertedDistribution nil - not applied



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

def asserted_distributions
  @asserted_distributions
end

#biological_association_idObject

Finds all OTUs that are the current determination of a CollectionObject that is in the BiologicalAssociation or are part of the Biological association itself. Altered by historical_determinations.

Parameters:

  • biological_association_id (Integer, Array)

Returns:

  • Array one or more biological_association_id



96
97
98
# File 'lib/queries/otu/filter.rb', line 96

def biological_association_id
  @biological_association_id
end

#biological_associationsTrue, ...

Returns true - Otu has BiologicalAssocation (subject or object) false - Otu without BiologicalAssociation nil - not applied.

Returns:

  • (True, False, nil)

    true - Otu has BiologicalAssocation (subject or object) false - Otu without BiologicalAssociation nil - not applied



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

def biological_associations
  @biological_associations
end

#collecting_event_idObject

Finds all OTUs that are the current determination of a CollectionObject that was collecting in these Collecting events. Altered by historical_determinations.

Parameters:

  • collecting_event_id (Integer, Array)

Returns:

  • Array one or more collecting_event_id



78
79
80
# File 'lib/queries/otu/filter.rb', line 78

def collecting_event_id
  @collecting_event_id
end

#collection_objectsTrue, ...

Returns true - Otu has CollectionObjects false - Otu without CollectionObjects nil - not applied.

Returns:

  • (True, False, nil)

    true - Otu has CollectionObjects false - Otu without CollectionObjects nil - not applied



143
144
145
# File 'lib/queries/otu/filter.rb', line 143

def collection_objects
  @collection_objects
end

#contentsTrue, ...

Returns true - Otu has Conten false - Otu without Conten nil - not applied.

Returns:

  • (True, False, nil)

    true - Otu has Conten false - Otu without Conten nil - not applied



137
138
139
# File 'lib/queries/otu/filter.rb', line 137

def contents
  @contents
end

#coordinatifyObject

See `coordinatify_result` for more.

!! This param is not like the others. !! See parallel in TaxonName filter 'validify'.

Returns:

  • Boolean if true then, additionally, all coordinate otus for the result are included



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

def coordinatify
  @coordinatify
end

#descendantsObject

Returns Boolean.

Returns:

  • Boolean



69
70
71
# File 'lib/queries/otu/filter.rb', line 69

def descendants
  @descendants
end

#descriptor_idArray

Parameters:

  • descriptor_id (String, Array)

Returns:

  • (Array)


125
126
127
# File 'lib/queries/otu/filter.rb', line 125

def descriptor_id
  @descriptor_id
end

#geo_jsonHash?

Returns in geo_json format (no Feature …) ?!.

Returns:

  • (Hash, nil)

    in geo_json format (no Feature …) ?!



104
105
106
# File 'lib/queries/otu/filter.rb', line 104

def geo_json
  @geo_json
end

#geographic_area_idArray

Parameters:

  • geographic_area_id (String, Array)

Returns:

  • (Array)


108
109
110
# File 'lib/queries/otu/filter.rb', line 108

def geographic_area_id
  @geographic_area_id
end

#geographic_area_modeBoolean?

Returns How to treat GeographicAreas

nil - non-spatial match by only those records matching the geographic_area_id exactly
true - spatial match
false - non-spatial match (exact and descendants).

Returns:

  • (Boolean, nil)

    How to treat GeographicAreas

    nil - non-spatial match by only those records matching the geographic_area_id exactly
    true - spatial match
    false - non-spatial match (exact and descendants)
    


115
116
117
# File 'lib/queries/otu/filter.rb', line 115

def geographic_area_mode
  @geographic_area_mode
end

#historical_determinationsBoolean?

Where applicable:

true - include only historical determinations (ignores only determinations)
false - include both current and historical
nil - include only current determinations

Impacts all TaxonDetermination referencing queries

Parameters:

  • historical_determinations (Boolean)

Returns:

  • (Boolean, nil)


87
88
89
# File 'lib/queries/otu/filter.rb', line 87

def historical_determinations
  @historical_determinations
end

#nameObject

Returns Array literal match against one or more Otu#name.

Parameters:

  • name (String, Array)

Returns:

  • Array literal match against one or more Otu#name



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

def name
  @name
end

#name_exactObject

Returns Boolean if true then match on `name` exactly.

Returns:

  • Boolean if true then match on `name` exactly



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

def name_exact
  @name_exact
end

#observationsTrue, ...

Returns true - Otu has observations false - Otu without observations nil - not applied.

Returns:

  • (True, False, nil)

    true - Otu has observations false - Otu without observations nil - not applied



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

def observations
  @observations
end

#otu_idObject

Returns Array one or more Otu ids.

Parameters:

  • otu_id (Integer, Array)

Returns:

  • Array one or more Otu ids



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

def otu_id
  @otu_id
end

#radiusObject

Integer in Meters

!! defaults to 100m


159
160
161
# File 'lib/queries/otu/filter.rb', line 159

def radius
  @radius
end

#taxon_nameTrue, ...

Returns true - Otu has taxon name false - Otu without taxon name nil - not applied.

Returns:

  • (True, False, nil)

    true - Otu has taxon name false - Otu without taxon name nil - not applied



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

def taxon_name
  @taxon_name
end

#taxon_name_idObject

Returns Array one or more Otu taxon_name_id.

Parameters:

  • taxon_name_id (Integer, Array)

Returns:

  • Array one or more Otu taxon_name_id



66
67
68
# File 'lib/queries/otu/filter.rb', line 66

def taxon_name_id
  @taxon_name_id
end

#wktObject

Parameters:

  • wkt (String)

    A Spatial area in WKT format



100
101
102
# File 'lib/queries/otu/filter.rb', line 100

def wkt
  @wkt
end

Instance Method Details

#all(nil_empty = false) ⇒ ActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


593
594
595
596
597
# File 'lib/queries/otu/filter.rb', line 593

def all(nil_empty = false)
  q = super
  q = coordinatify_result(q) if coordinatify
  q
end

#and_clausesObject



557
558
559
560
561
562
# File 'lib/queries/otu/filter.rb', line 557

def and_clauses
  [
    name_facet,
    taxon_name_facet,
  ]
end

#asserted_distribution_query_facetObject



409
410
411
412
413
414
415
416
417
# File 'lib/queries/otu/filter.rb', line 409

def asserted_distribution_query_facet
  return nil if asserted_distribution_query.nil?
  s = "WITH query_ad_otus AS (" + asserted_distribution_query.all.to_sql + ") " +
      ::Otu
        .joins("JOIN query_ad_otus as query_ad_otus1 on otus.id = query_ad_otus1.otu_id")
        .to_sql

  ::Otu.from("(" + s + ") as otus").distinct
end

#asserted_distributions_facetObject



274
275
276
277
278
279
280
281
# File 'lib/queries/otu/filter.rb', line 274

def asserted_distributions_facet
  return nil if asserted_distributions.nil?
  if asserted_distributions
    ::Otu.joins(:asserted_distributions)
  else
    ::Otu.where.missing(:asserted_distributions)
  end
end

#biological_association_id_facetObject



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/queries/otu/filter.rb', line 352

def biological_association_id_facet
  return nil if biological_association_id.empty?

  q1 = ::Otu.joins(:biological_associations).where(biological_associations: { id: biological_association_id, biological_association_subject_type: "Otu" })
  q2 = ::Otu.joins(:related_biological_associations).where(related_biological_associations: { id: biological_association_id, biological_association_object_type: "Otu" })
  q3 = ::Otu.joins(collection_objects: [:biological_associations]).where(biological_associations: { id: biological_association_id, biological_association_subject_type: "CollectionObject" })
  q4 = ::Otu.joins(collection_objects: [:related_biological_associations]).where(related_biological_associations: { id: biological_association_id, biological_association_object_type: "CollectionObject" })

  if historical_determinations.nil?
    q3 = q3.where(taxon_determinations: { position: 1 })
    q4 = q4.where(taxon_determinations: { position: 1 })
  elsif historical_determinations
    q3 = q3.where.not(taxon_determinations: { position: 1 })
    q4 = q4.where.not(taxon_determinations: { position: 1 })
  end

  query = [q1, q2, q3, q4].collect { |s| "(" + s.to_sql + ")" }.join(" UNION ")

  ::Otu.from("(#{query}) as otus")
end

#biological_association_query_facetObject



429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/queries/otu/filter.rb', line 429

def biological_association_query_facet
  return nil if biological_association_query.nil?
  s = "WITH query_ba_otu AS (" + biological_association_query.all.to_sql + ") "

  a = ::Otu
    .joins("JOIN query_ba_otu as query_ba_otu1 on otus.id = query_ba_otu1.biological_association_subject_id AND query_ba_otu1.biological_association_subject_type = 'Otu'")

  b = ::Otu
    .joins("JOIN query_ba_otu as query_ba_otu2 on otus.id = query_ba_otu2.biological_association_object_id AND query_ba_otu2.biological_association_object_type = 'Otu'")

  s << referenced_klass_union([a, b]).to_sql

  ::Otu.from("(" + s + ") as otus")
end

#biological_associations_facetObject

UNION, NOT EXISTS example



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

def biological_associations_facet
  return nil if biological_associations.nil?

  a = ::Otu.joins(:biological_associations)
  b = ::Otu.joins(:related_biological_associations)

  c = ::Otu.from("((#{a.to_sql}) UNION (#{b.to_sql})) as otus")

  if biological_associations
    ::Otu.from("((#{a.to_sql}) UNION (#{b.to_sql})) as otus")
  else
    ::Otu.where("NOT EXISTS (" + ::Otu.select("1").from( # not exists ( a select(1) from <opposite query>  )
      ::Otu.from(
        "((#{a.to_sql}) UNION (#{b.to_sql})) as ba_otus_join"
      ).where("otus.id = ba_otus_join.id")
    ).to_sql + ")")  # where includes in/out link
  end
end

#biological_associations_tableObject



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

def biological_associations_table
  ::BiologicalAssociation.arel_table
end

#collecting_event_id_facetObject



341
342
343
344
345
346
347
348
349
350
# File 'lib/queries/otu/filter.rb', line 341

def collecting_event_id_facet
  return nil if collecting_event_id.empty?
  if historical_determinations.nil?
    ::Otu.joins(:collection_objects).where(collection_objects: { collecting_event_id: collecting_event_id }, taxon_determinations: { position: 1 })
  elsif historical_determinations
    ::Otu.joins(:collection_objects).where(collection_objects: { collecting_event_id: collecting_event_id }).where.not(taxon_determinations: { position: 1 })
  else
    ::Otu.joins(:collection_objects).where(collection_objects: { collecting_event_id: collecting_event_id })
  end
end

#collecting_event_query_facetObject



455
456
457
458
459
460
461
462
463
464
# File 'lib/queries/otu/filter.rb', line 455

def collecting_event_query_facet
  return nil if collecting_event_query.nil?
  s = "WITH query_ce_otus AS (" + collecting_event_query.all.to_sql + ") " +
      ::Otu
        .joins(:collecting_events)
        .joins("JOIN query_ce_otus as query_ce_otus1 on collecting_events.id = query_ce_otus1.id")
        .to_sql

  ::Otu.from("(" + s + ") as otus").distinct
end

#collection_object_query_facetObject



444
445
446
447
448
449
450
451
452
453
# File 'lib/queries/otu/filter.rb', line 444

def collection_object_query_facet
  return nil if collection_object_query.nil?
  s = "WITH query_co_otus AS (" + collection_object_query.all.to_sql + ") " +
      ::Otu
        .joins(:collection_objects)
        .joins("JOIN query_co_otus as query_co_otus1 on collection_objects.id = query_co_otus1.id")
        .to_sql

  ::Otu.from("(" + s + ") as otus").distinct
end

#collection_objects_facetObject



312
313
314
315
316
317
318
319
# File 'lib/queries/otu/filter.rb', line 312

def collection_objects_facet
  return nil if collection_objects.nil?
  if collection_objects
    ::Otu.joins(:collection_objects)
  else
    ::Otu.where.missing(:collection_objects)
  end
end

#content_query_facetObject



419
420
421
422
423
424
425
426
427
# File 'lib/queries/otu/filter.rb', line 419

def content_query_facet
  return nil if content_query.nil?
  s = "WITH query_con_otus AS (" + content_query.all.to_sql + ") " +
      ::Otu
        .joins("JOIN query_con_otus as query_con_otus1 on otus.id = query_con_otus1.otu_id")
        .to_sql

  ::Otu.from("(" + s + ") as otus").distinct
end

#contents_facetObject



283
284
285
286
287
288
289
290
# File 'lib/queries/otu/filter.rb', line 283

def contents_facet
  return nil if contents.nil?
  if contents
    ::Otu.joins(:contents)
  else
    ::Otu.where.missing(:contents)
  end
end

#coordinatify_result(q) ⇒ Object

Expands result of OTU filter query in 2 ways:

1 - to include all valid OTUs by (proxy of TaxonName) if OTU is by proxy invalid
2 - to include all invalid OTUS (by proxy of TaxonName) if OTU is by proxy valid

In essence this creates full sets of TaxonConcepts from partial results. The result can be used to, for example, get a comprehensive list of Sources for the concept, or a comprehensive historical list of Specimens, etc.



532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# File 'lib/queries/otu/filter.rb', line 532

def coordinatify_result(q)
  i = q.joins(:taxon_name).where("taxon_names.id != taxon_names.cached_valid_taxon_name_id")
  v = q.joins(:taxon_name).where("taxon_names.id = taxon_names.cached_valid_taxon_name_id")

  # Find valid for invalid
  s = "WITH invalid_otu_result AS (" + i.to_sql + ") " +
      ::Otu
        .joins("JOIN taxon_names tn1 on otus.taxon_name_id = tn1.cached_valid_taxon_name_id")
        .joins("JOIN invalid_otu_result AS invalid_otu_result1 ON invalid_otu_result1.taxon_name_id = tn1.id") # invalid otus matching valid names
        .to_sql

  a = ::Otu.from("(" + s + ") as otus")

  # Find invalid for valid
  t = "WITH valid_otu_result AS (" + v.to_sql + ") " +
      ::Otu
        .joins("JOIN taxon_names tn2 on otus.taxon_name_id = tn2.id")
        .joins("JOIN valid_otu_result AS valid_otu_result1 ON valid_otu_result1.taxon_name_id = tn2.cached_valid_taxon_name_id") # valid otus matching invalid names
        .to_sql

  b = ::Otu.from("(" + t + ") as otus")

  referenced_klass_union([a, b, q])
end

#descriptor_id_facetObject



400
401
402
403
404
405
406
407
# File 'lib/queries/otu/filter.rb', line 400

def descriptor_id_facet
  return nil if descriptor_id.empty?

  q1 = ::Otu.joins(:descriptors).where(descriptors: { id: descriptor_id })
  q2 = ::Otu.joins(collection_objects: [:descriptors]).where(descriptors: { id: descriptor_id })

  ::Otu.from("((#{q1.to_sql}) UNION (#{q2.to_sql})) as otus")
end

#descriptor_query_facetObject



488
489
490
491
492
493
494
495
496
497
# File 'lib/queries/otu/filter.rb', line 488

def descriptor_query_facet
  return nil if descriptor_query.nil?
  s = "WITH query_de_otus AS (" + descriptor_query.all.to_sql + ") " +
      ::Otu
        .joins(:observations)
        .joins("JOIN query_de_otus as query_de_otus1 on observations.descriptor_id = query_de_otus1.id")
        .to_sql

  ::Otu.from("(" + s + ") as otus").distinct
end

#extract_query_facetObject



466
467
468
469
470
471
472
473
474
475
# File 'lib/queries/otu/filter.rb', line 466

def extract_query_facet
  return nil if extract_query.nil?
  s = "WITH query_ex_otus AS (" + extract_query.all.to_sql + ") " +
      ::Otu
        .joins(:origin_relationships)
        .joins("JOIN query_ex_otus as query_ex_otus1 on origin_relationships.new_object_id = query_ex_otus1.id and origin_relationships.new_object_type = 'Extract'")
        .to_sql

  ::Otu.from("(" + s + ") as otus").distinct
end

#from_wkt(wkt_shape) ⇒ Object



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

def from_wkt(wkt_shape)
  c = ::Queries::CollectingEvent::Filter.new(wkt: wkt_shape, project_id: project_id)
  a = ::Queries::AssertedDistribution::Filter.new(wkt: wkt_shape, project_id: project_id)

  q1 = ::Otu.joins(collection_objects: [:collecting_event]).where(collecting_events: c.all)
  q2 = ::Otu.joins(:asserted_distributions).where(asserted_distributions: a.all)

  ::Otu.from("((#{q1.to_sql}) UNION (#{q2.to_sql})) as otus")
end

#geo_json_facetObject



262
263
264
265
266
267
268
269
270
271
272
# File 'lib/queries/otu/filter.rb', line 262

def geo_json_facet
  return nil if geo_json.nil?

  c = ::Queries::CollectingEvent::Filter.new(geo_json: geo_json, project_id: project_id, radius: radius)
  a = ::Queries::AssertedDistribution::Filter.new(geo_json: geo_json, project_id: project_id, radius: radius)

  q1 = ::Otu.joins(collection_objects: [:collecting_event]).where(collecting_events: c.all).to_sql
  q2 = ::Otu.joins(:asserted_distributions).where(asserted_distributions: a.all).to_sql

  ::Otu.from("((#{q1}) UNION (#{q2})) as otus")
end

#geographic_area_id_facetObject



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/queries/otu/filter.rb', line 373

def geographic_area_id_facet
  return nil if geographic_area_id.empty?
  a = nil

  case geographic_area_mode
  when nil, true # exact and spatial start the same
    a = ::GeographicArea.where(id: geographic_area_id)
  when false # descendants
    a = ::GeographicArea.descendants_of_any(geographic_area_id)
  end

  b = nil # from AssertedDistributions
  c = nil # from CollectionObjects

  case geographic_area_mode
  when nil, false # exact, descendants
    b = ::Otu.joins(:asserted_distributions).where(asserted_distributions: { geographic_area: a })
    c = ::Otu.joins(collection_objects: [:collecting_event]).where(collecting_events: { geographic_area: a })
  when true # spatial
    i = ::GeographicItem.joins(:geographic_areas).where(geographic_areas: a) # .unscope
    wkt_shape = ::GeographicItem.st_union(i).to_a.first["collection"].to_s # todo, check
    return from_wkt(wkt_shape)
  end

  ::Otu.from("((#{b.to_sql}) UNION (#{c.to_sql})) as otus")
end

#loan_query_facetObject



499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/queries/otu/filter.rb', line 499

def loan_query_facet
  return nil if loan_query.nil?
  s = "WITH query_loan_otus AS (" + loan_query.all.to_sql + ") "

  a = ::Otu.joins(:loan_items)
    .joins("JOIN query_loan_otus as query_loan_otus1 on loan_items.loan_id = query_loan_otus1.id")

  b = ::Otu.joins(collection_objects: [:loan_items])
    .joins("JOIN query_loan_otus as query_loan_otus1 on loan_items.loan_id = query_loan_otus1.id")

  s << referenced_klass_union([a, b]).to_sql

  ::Otu.from("(" + s + ") as otus").distinct
end

#merge_clausesObject



564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# File 'lib/queries/otu/filter.rb', line 564

def merge_clauses
  [
    asserted_distribution_query_facet,
    asserted_distributions_facet,
    biological_association_query_facet,
    collecting_event_query_facet,
    collection_object_query_facet,
    content_query_facet,
    descriptor_query_facet,
    extract_query_facet,
    loan_query_facet,
    observation_query_facet,
    taxon_name_query_facet,

    biological_association_id_facet,
    biological_associations_facet,
    collecting_event_id_facet,
    collection_objects_facet,
    contents_facet,
    descriptor_id_facet,
    geo_json_facet,
    geographic_area_id_facet,
    observations_facet,
    taxon_name_id_facet,
    wkt_facet,
  ].compact
end

#name_facetObject



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

def name_facet
  return nil if name.empty?
  if name_exact
    table[:name].eq_any(name)
  else
    table[:name].matches_any(name.collect { |n| "%" + n.gsub(/\s+/, "%") + "%" })
  end
end

#observation_query_facetObject



514
515
516
517
518
519
520
521
522
523
# File 'lib/queries/otu/filter.rb', line 514

def observation_query_facet
  return nil if observation_query.nil?
  s = "WITH query_obs_otus AS (" + observation_query.all.to_sql + ") " +
      ::Otu
        .joins(:observations)
        .joins("JOIN query_obs_otus as query_obs_otus1 on observations.id = query_obs_otus1.id")
        .to_sql

  ::Otu.from("(" + s + ") as otus")
end

#observations_facetObject



331
332
333
334
335
336
337
338
339
# File 'lib/queries/otu/filter.rb', line 331

def observations_facet
  return nil if observations.nil?

  if observations
    ::Otu.joins(:observations)
  else
    ::Otu.where.missing(:observations)
  end
end

#taxon_name_facetObject



321
322
323
324
325
326
327
328
329
# File 'lib/queries/otu/filter.rb', line 321

def taxon_name_facet
  return nil if taxon_name.nil?

  if taxon_name
    table[:taxon_name_id].not_eq(nil)
  else
    table[:taxon_name_id].eq(nil)
  end
end

#taxon_name_id_facetObject



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

def taxon_name_id_facet
  return nil if taxon_name_id.empty?
  if descendants
    h = Arel::Table.new(:taxon_name_hierarchies)
    j = table.join(h, Arel::Nodes::InnerJoin).on(table[:taxon_name_id].eq(h[:descendant_id]))
    z = h[:ancestor_id].eq_any(taxon_name_id)

    ::Otu.joins(j.join_sources).where(z)
  else
    ::Otu.where(taxon_name_id: taxon_name_id)
  end
end

#taxon_name_query_facetObject



477
478
479
480
481
482
483
484
485
486
# File 'lib/queries/otu/filter.rb', line 477

def taxon_name_query_facet
  return nil if taxon_name_query.nil?
  s = "WITH query_taxon_names AS (" + taxon_name_query.all.to_sql + ") " +
      ::Otu
        .joins(:taxon_name)
        .joins("JOIN query_taxon_names as query_taxon_names1 on otus.taxon_name_id = query_taxon_names1.id")
        .to_sql

  ::Otu.from("(" + s + ") as otus").distinct
end

#wkt_facetObject



247
248
249
250
# File 'lib/queries/otu/filter.rb', line 247

def wkt_facet
  return nil if wkt.nil?
  from_wkt(wkt)
end