Class: Queries::FieldOccurrence::Filter

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

Constant Summary collapse

PARAMS =
[
  *::Queries::CollectingEvent::Filter::BASE_PARAMS,
  *Queries::Concerns::DateRanges.params, # Fead to CE query. Revisit possibly.

# :biological_association_id,
# :biological_associations,
# :biological_relationship_id,
# :collecting_event,
 :field_occurrence_id,
# :collection_object_type,
# :collectors,
# :current_determinations,
# :current_repository,
# :current_repository_id,
# :dates,
# :deaccessioned,
# :descendants,
# :determiner_id_or,
# :determiner_name_regex,
# :determiners,
# :dwc_indexed,
# :extract_id,
# :georeferences,
# :import_dataset_id,
# :repository,
# :repository_id,
# :sled_image_id,
# :spatial_geographic_areas,
# :taxon_determination_id,
# :taxon_determinations,
# :taxon_name_id,
# :validity,
# biocuration_class_id: [],
# biological_association_id: [],
# biological_relationship_id: [],
# collecting_event_id: [],
# field_occurrence_id: [],
# determiner_id: [],
# extract_id: [],
# geographic_area_id: [],
# import_dataset_id: [],
# is_type: [],
# otu_id: [],
# taxon_name_id: [],
].inject([{}]) { |ary, k| k.is_a?(Hash) ? ary.last.merge!(k) : ary.unshift(k); ary }.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, #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, #taxon_name_query, #venn, #venn_mode

Attributes inherited from Query

#query_string, #terms

Instance Method Summary collapse

Methods included from Helpers

#boolean_param, #integer_param

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, included_annotator_facets, instantiated_base_filter, inverted_subqueries, #model_id_facet, #object_global_id_facet, #only_project?, params, #permitted_params, #process_url_into_params, #project_id_facet, query_name, #query_name, #set_nested_queries, #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

rubocop:disable Metric/MethodLength



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/queries/field_occurrence/filter.rb', line 230

def initialize(query_params)
  super

  # Only CollectingEvent fields are permitted, for advanced nesting (e.g. tags on CEs), use collecting_event_query
  collecting_event_params = ::Queries::CollectingEvent::Filter.base_params + Queries::Concerns::DateRanges.params

  # project_id is handled during use
  @base_collecting_event_query = ::Queries::CollectingEvent::Filter.new(
    params.select{ |a, b| collecting_event_params.include?(a) } # maintain this to avoid sub query initialization for now
  )

  @biological_association_id = params[:biological_association_id]
  @biocuration_class_id = params[:biocuration_class_id]
  @biological_relationship_id = params[:biological_relationship_id] # TODO: no reference?
  @biological_associations = boolean_param(params, :biological_associations)
  @collecting_event = boolean_param(params, :collecting_event)
  @collectors = boolean_param(params, :collectors)
  @collecting_event_id = params[:collecting_event_id]
  @field_occurrence_id = params[:field_occurrence_id]
  @collection_object_type = params[:collection_object_type].presence
  @current_determinations = boolean_param(params, :current_determinations)
  @current_repository = boolean_param(params, :current_repository)
  @current_repository_id = params[:current_repository_id].presence
  @dates = boolean_param(params, :dates)
  @descendants = boolean_param(params, :descendants)
  @deaccessioned = boolean_param(params, :deaccessioned)
  @determiners = boolean_param(params, :determiners)
  @determiner_id = params[:determiner_id]
  @determiner_id_or = boolean_param(params, :determiner_id_or)
  @determiner_name_regex = params[:determiner_name_regex]
  @dwc_indexed = boolean_param(params, :dwc_indexed)
  @extract_id = params[:extract_id]
  @geographic_area = boolean_param(params, :geographic_area)
  @georeferences = boolean_param(params, :georeferences)
  @import_dataset_id = params[:import_dataset_id]
  @is_type = params[:is_type] || []
  @otu_descendants = boolean_param(params, :otu_descendants)
  @otu_id = params[:otu_id]
  @repository = boolean_param(params, :repository)
  @repository_id = params[:repository_id]
  @sled_image_id = (params[:sled_image_id].presence)
  @taxon_determinations = boolean_param(params, :taxon_determinations)
  @taxon_name_id = params[:taxon_name_id]
  @validity = boolean_param(params, :validity)

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

Instance Attribute Details

#base_collecting_event_queryObject

All params managed by CollectingEvent filter are available here as well



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

def base_collecting_event_query
  @base_collecting_event_query
end

#biocuration_class_idArray

Returns of biocuration_class ids.

Returns:

  • (Array)

    of biocuration_class ids



133
134
135
# File 'lib/queries/field_occurrence/filter.rb', line 133

def biocuration_class_id
  @biocuration_class_id
end

#biological_association_idObject

Returns Array Matching records in this BiologicalAssociation.

Returns:

  • Array Matching records in this BiologicalAssociation



72
73
74
# File 'lib/queries/field_occurrence/filter.rb', line 72

def biological_association_id
  @biological_association_id
end

#biological_associationsTrue, ...

Returns true - has collecting event that has geographic_area false - does not have collecting event that has geographic area nil - not applied.

Returns:

  • (True, False, nil)

    true - has collecting event that has geographic_area false - does not have collecting event that has geographic area nil - not applied



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

def biological_associations
  @biological_associations
end

#biological_relationship_idArray

Returns of biological_relationship#id.

Returns:

  • (Array)

    of biological_relationship#id



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

def biological_relationship_id
  @biological_relationship_id
end

#collecting_eventTrue, ...

Parameters:

  • collecting_event ('true', 'false')

    true - has collecting_event_id false - does not have collecting_event_id nil - not applied

Returns:

  • (True, False, nil)


194
195
196
# File 'lib/queries/field_occurrence/filter.rb', line 194

def collecting_event
  @collecting_event
end

#collecting_event_idObject

Array

only return objects with these collecting event ID



84
85
86
# File 'lib/queries/field_occurrence/filter.rb', line 84

def collecting_event_id
  @collecting_event_id
end

#collection_object_typeObject

Parameters:

  • one (String, nil)

    of ‘Specimen’, ‘Lot’, or ‘RangedLot’



80
81
82
# File 'lib/queries/field_occurrence/filter.rb', line 80

def collection_object_type
  @collection_object_type
end

#collectorsBoolean?

Returns true - A collector role exists false - A collector role exists nil - not applied.

Returns:

  • (Boolean, nil)

    true - A collector role exists false - A collector role exists nil - not applied



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

def collectors
  @collectors
end

#current_determinationsBoolean?

Returns nil = TaxonDeterminations match regardless of current or historical true = TaxonDetermination must be .current false = TaxonDetermination must be .historical.

Returns:

  • (Boolean, nil)

    nil = TaxonDeterminations match regardless of current or historical true = TaxonDetermination must be .current false = TaxonDetermination must be .historical



117
118
119
# File 'lib/queries/field_occurrence/filter.rb', line 117

def current_determinations
  @current_determinations
end

#current_repositoryTrue, ...

Returns true - has current_repository_id false - does not have current_repository_id nil - not applied.

Returns:

  • (True, False, nil)

    true - has current_repository_id false - does not have current_repository_id nil - not applied



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

def current_repository
  @current_repository
end

#current_repository_idCurrentRepository#id?

Returns:

  • (CurrentRepository#id, nil)


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

def current_repository_id
  @current_repository_id
end

#datesObject

Returns Boolean true - any of start/end date or verbatim date are populated false - none of start/end date or verbatim date are populated nil - ignored.

Returns:

  • Boolean true - any of start/end date or verbatim date are populated false - none of start/end date or verbatim date are populated nil - ignored



227
228
229
# File 'lib/queries/field_occurrence/filter.rb', line 227

def dates
  @dates
end

#deaccessionedBoolean?

Returns nil = Ignored true = FieldOccurrence with deaccessioned_at OR deaccessioned_reason populated or deaccession_recipient.present? false = without true.

Returns:

  • (Boolean, nil)

    nil = Ignored true = FieldOccurrence with deaccessioned_at OR deaccessioned_reason populated or deaccession_recipient.present? false = without true



105
106
107
# File 'lib/queries/field_occurrence/filter.rb', line 105

def deaccessioned
  @deaccessioned
end

#descendantsObject

Returns the value of attribute descendants.



99
100
101
# File 'lib/queries/field_occurrence/filter.rb', line 99

def descendants
  @descendants
end

#determiner_idArray

Parameters:

  • determiner (Array or Person#id)

    one ore more people id

Returns:

  • (Array)


205
206
207
# File 'lib/queries/field_occurrence/filter.rb', line 205

def determiner_id
  @determiner_id
end

#determiner_id_orBoolean

Parameters:

  • determiner_id_or (String, nil)

    ‘false`, nil - treat the ids in determiner_id as “or” ’true’ - treat the ids in determiner_id as “and” (only collection objects with all and only all will match)

Returns:

  • (Boolean)


211
212
213
# File 'lib/queries/field_occurrence/filter.rb', line 211

def determiner_id_or
  @determiner_id_or
end

#determiner_name_regexObject

A PostgreSQL valid regular expression. Note that simple strings evaluate as wildcard matches. !! Probably shouldn’t expose to external API.

Returns:

  • String



217
218
219
# File 'lib/queries/field_occurrence/filter.rb', line 217

def determiner_name_regex
  @determiner_name_regex
end

#determinersBoolean?

Returns true - A determiner role exists false - No determiner role exists nil - not applied.

Returns:

  • (Boolean, nil)

    true - A determiner role exists false - No determiner role exists nil - not applied



123
124
125
# File 'lib/queries/field_occurrence/filter.rb', line 123

def determiners
  @determiners
end

#dwc_indexedTrue, ...

Returns true - index is built false - index is not built nil - not applied.

Returns:

  • (True, False, nil)

    true - index is built false - index is not built nil - not applied



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

def dwc_indexed
  @dwc_indexed
end

#extract_idObject

Returns Array match all FieldOccurrences that are the origin of these Extracts.

Returns:

  • Array match all FieldOccurrences that are the origin of these Extracts



221
222
223
# File 'lib/queries/field_occurrence/filter.rb', line 221

def extract_id
  @extract_id
end

#field_occurrence_idObject

Parameters:

  • Array (String, nil)

    or Integer of FieldOccurrence ids



76
77
78
# File 'lib/queries/field_occurrence/filter.rb', line 76

def field_occurrence_id
  @field_occurrence_id
end

#geographic_areaTrue, ...

Returns true - has collecting event that has geographic_area false - does not have collecting event that has geographic area nil - not applied.

Returns:

  • (True, False, nil)

    true - has collecting event that has geographic_area false - does not have collecting event that has geographic area nil - not applied



200
201
202
# File 'lib/queries/field_occurrence/filter.rb', line 200

def geographic_area
  @geographic_area
end

#georeferencesTrue, ...

Returns true - has one ore more georeferences false - does not have any georeferences nil - not applied.

Returns:

  • (True, False, nil)

    true - has one ore more georeferences false - does not have any georeferences nil - not applied



175
176
177
# File 'lib/queries/field_occurrence/filter.rb', line 175

def georeferences
  @georeferences
end

#import_dataset_idArray

Returns of ImportDataset ids.

Returns:

  • (Array)

    of ImportDataset ids



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

def import_dataset_id
  @import_dataset_id
end

#is_typeArray?

Returns one of ‘holotype`, `lectotype` etc.

nil - not applied.

Returns:

  • (Array, nil)

    one of ‘holotype`, `lectotype` etc.

    nil - not applied
    


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

def is_type
  @is_type
end

#otu_idArray?

Returns Otu ids, matches on the TaxonDetermination, see also current_determinations.

Parameters:

  • (Array)

Returns:

  • (Array, nil)

    Otu ids, matches on the TaxonDetermination, see also current_determinations



92
93
94
# File 'lib/queries/field_occurrence/filter.rb', line 92

def otu_id
  @otu_id
end

#repositoryTrue, ...

Returns true - has repository_id false - does not have repository_id nil - not applied.

Returns:

  • (True, False, nil)

    true - has repository_id false - does not have repository_id nil - not applied



181
182
183
# File 'lib/queries/field_occurrence/filter.rb', line 181

def repository
  @repository
end

#repository_idRepository#id?

Returns:



146
147
148
# File 'lib/queries/field_occurrence/filter.rb', line 146

def repository_id
  @repository_id
end

#sled_image_idSledImage#id?

Returns:



157
158
159
# File 'lib/queries/field_occurrence/filter.rb', line 157

def sled_image_id
  @sled_image_id
end

#taxon_determinationsTrue, ...

Returns true - has one ore more taxon_determinations false - does not have any taxon_determinations nil - not applied.

Returns:

  • (True, False, nil)

    true - has one ore more taxon_determinations false - does not have any taxon_determinations nil - not applied



163
164
165
# File 'lib/queries/field_occurrence/filter.rb', line 163

def taxon_determinations
  @taxon_determinations
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



169
170
171
# File 'lib/queries/field_occurrence/filter.rb', line 169

def taxon_name
  @taxon_name
end

#taxon_name_idArray of Protonym.id?

Return all collection objects determined as an Otu that is self or descendant linked to this TaxonName

Returns:

  • (Array of Protonym.id, nil)

    return all collection objects determined as an Otu that is self or descendant linked to this TaxonName



97
98
99
# File 'lib/queries/field_occurrence/filter.rb', line 97

def taxon_name_id
  @taxon_name_id
end

#validityBoolean?

Returns nil = Match against all ancestors, valid or invalid true = Match against only valid ancestors false = Match against only invalid ancestors.

Returns:

  • (Boolean, nil)

    nil = Match against all ancestors, valid or invalid true = Match against only valid ancestors false = Match against only invalid ancestors



111
112
113
# File 'lib/queries/field_occurrence/filter.rb', line 111

def validity
  @validity
end

Instance Method Details

#and_clausesObject

::FieldOccurrence.from(‘(’ + s + ‘) as collection_objects’).distinct

end


731
732
733
734
735
736
# File 'lib/queries/field_occurrence/filter.rb', line 731

def and_clauses
  [
    #collecting_event_id_facet,
    field_occurrence_id_facet,
  ]
end

#base_collecting_event_query_facetObject



622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# File 'lib/queries/field_occurrence/filter.rb', line 622

def base_collecting_event_query_facet
  # Turn project_id off and check for a truly empty query
  base_collecting_event_query.project_id = nil
  return nil if base_collecting_event_query.all(true).nil?

  # Turn project_id back on
  base_collecting_event_query.project_id = project_id

  s = 'WITH query_ce_base_co AS (' + base_collecting_event_query.all.to_sql + ') ' +
      ::FieldOccurrence
        .joins('JOIN query_ce_base_co as query_ce_base_co1 on query_ce_base_co1.id = collection_objects.collecting_event_id')
        .to_sql

  ::FieldOccurrence.from('(' + s + ') as collection_objects').distinct
end

#biocuration_facetObject



477
478
479
480
481
# File 'lib/queries/field_occurrence/filter.rb', line 477

def biocuration_facet
  return nil if biocuration_class_id.empty?
  ::FieldOccurrence::BiologicalFieldOccurrence.joins(:biocuration_classifications)
    .where(biocuration_classifications: { biocuration_class_id: })
end

#biological_relationship_id_facetObject



488
489
490
491
# File 'lib/queries/field_occurrence/filter.rb', line 488

def biological_relationship_id_facet
  return nil if biological_relationship_id.empty?
  ::FieldOccurrence.with_biological_relationship_id(biological_relationship_id)
end

#collecting_event_facetObject



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

def collecting_event_facet
  return nil if collecting_event.nil?
  if collecting_event
    ::FieldOccurrence.where.not(collecting_event_id: nil)
  else
    ::FieldOccurrence.where(collecting_event_id: nil)
  end
end

#collecting_event_id_facetObject



500
501
502
503
# File 'lib/queries/field_occurrence/filter.rb', line 500

def collecting_event_id_facet
  return nil if collecting_event_id.empty?
  table[:collecting_event_id].in(collecting_event_id)
end

#collecting_event_tableArel::Table

Returns:

  • (Arel::Table)


284
285
286
# File 'lib/queries/field_occurrence/filter.rb', line 284

def collecting_event_table
  ::CollectingEvent.arel_table
end

#collectors_facetObject



412
413
414
415
416
417
418
419
# File 'lib/queries/field_occurrence/filter.rb', line 412

def collectors_facet
  return nil if collectors.nil?
  if collectors
    ::FieldOccurrence.joins(:collectors)
  else
    ::FieldOccurrence.where.missing(:collectors)
  end
end

#current_repository_facetObject



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

def current_repository_facet
  return nil if current_repository.nil?
  if current_repository
    ::FieldOccurrence.where.not(current_repository_id: nil)
  else
    ::FieldOccurrence.where(current_repository_id: nil)
  end
end

#current_repository_id_facetObject



510
511
512
513
# File 'lib/queries/field_occurrence/filter.rb', line 510

def current_repository_id_facet
  return nil if current_repository_id.blank?
  table[:current_repository_id].eq(current_repository_id)
end

#dates_facetObject



578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/queries/field_occurrence/filter.rb', line 578

def dates_facet
  return nil if dates.nil?
  if dates
    ::FieldOccurrence.left_joins(:collecting_event).where(
      'start_date_year IS NOT NULL OR ' \
      'start_date_month IS NOT NULL OR ' \
      'start_date_day IS NOT NULL OR ' \
      'end_date_year IS NOT NULL OR ' \
      'end_date_month IS NOT NULL OR ' \
      'end_date_day IS NOT NULL OR ' \
      'verbatim_date IS NOT NULL'
    )
  else
    ::FieldOccurrence.left_joins(:collecting_event).where(
      collecting_event: {
        start_date_year: nil,
        start_date_month: nil,
        start_date_day: nil,
        end_date_year: nil,
        end_date_month: nil,
        end_date_day: nil,
        verbatim_date: nil,
      },
    )
  end
end

#deaccessioned_facetObject



605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# File 'lib/queries/field_occurrence/filter.rb', line 605

def deaccessioned_facet
  return nil if deaccessioned.nil?
  a = ::FieldOccurrence.where('(deaccession_reason IS NOT NULL) OR (deaccessioned_at IS NOT NULL)')
  b = ::FieldOccurrence.joins(:deaccession_recipient)

  c = referenced_klass_union([a, b])
  if deaccessioned
    c
  else
    s = 'WITH query_deac AS (' + c.all.to_sql + ') ' +
        ::FieldOccurrence
          .joins('LEFT JOIN query_deac as query_deac1 on query_deac1.id = collection_objects.id')
          .where('query_deac1.id IS NULL').to_sql
    ::FieldOccurrence.from('(' + s + ') as collection_objects').distinct
  end
end

#determiner_facetObject

TODO: DRY with Source (author), TaxonName, etc. See Queries::ColletingEvent::Filter for other use



372
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
399
400
401
# File 'lib/queries/field_occurrence/filter.rb', line 372

def determiner_facet
  return nil if determiner_id.empty?
  tt = table

  o = ::TaxonDetermination.arel_table
  r = ::Role.arel_table

  a = o.alias('a_det__')
  b = o.project(a[Arel.star]).from(a)

  c = r.alias('det_r1')

  b = b.join(c, Arel::Nodes::OuterJoin)
    .on(
      a[:id].eq(c[:role_object_id])
        .and(c[:role_object_type].eq('TaxonDetermination'))
        .and(c[:type].eq('Determiner'))
    )

  e = c[:id].not_eq(nil)
  f = c[:person_id].in(determiner_id)

  b = b.where(e.and(f))
  b = b.group(a['id'])
  b = b.having(a['id'].count.eq(determiner_id.length)) unless determiner_id_or

  b = b.as('det_z1_')

  ::FieldOccurrence.joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['biological_field_occurrence_id'].eq(tt['id']))))
end

#determiner_name_regex_facetObject



421
422
423
424
# File 'lib/queries/field_occurrence/filter.rb', line 421

def determiner_name_regex_facet
  return nil if determiner_name_regex.nil?
  ::FieldOccurrence.joins(:determiners).where('people.cached ~* ?', determiner_name_regex)
end

#determiners_facetObject



403
404
405
406
407
408
409
410
# File 'lib/queries/field_occurrence/filter.rb', line 403

def determiners_facet
  return nil if determiners.nil?
  if determiners
    ::FieldOccurrence.joins(:determiners)
  else
    ::FieldOccurrence.where.missing(:determiners)
  end
end

#dwc_indexed_facetObject



493
494
495
496
497
498
# File 'lib/queries/field_occurrence/filter.rb', line 493

def dwc_indexed_facet
  return nil if dwc_indexed.nil?
  dwc_indexed ?
    ::FieldOccurrence.dwc_indexed :
    ::FieldOccurrence.dwc_not_indexed
end

#extract_id_facetObject



353
354
355
356
357
358
# File 'lib/queries/field_occurrence/filter.rb', line 353

def extract_id_facet
  return nil if extract_id.empty?
  ::FieldOccurrence
    .joins(:origin_relationships)
    .where(origin_relationships: { new_object_id: extract_id, new_object_type: 'Extract' })
end

#field_occurrence_id_facetObject



342
343
344
345
# File 'lib/queries/field_occurrence/filter.rb', line 342

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

#geographic_area_facetObject

This is not spatial



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

def geographic_area_facet
  return nil if geographic_area.nil?

  if geographic_area
    ::FieldOccurrence.joins(:collecting_event).where.not(collecting_events: { geographic_area_id: nil }).distinct
  else
    ::FieldOccurrence.left_outer_joins(:collecting_event)
      .where(collecting_events: { geographic_area_id: nil })
      .distinct
  end
end

#georeferences_facetObject



426
427
428
429
430
431
432
433
434
435
# File 'lib/queries/field_occurrence/filter.rb', line 426

def georeferences_facet
  return nil if georeferences.nil?
  if georeferences
    ::FieldOccurrence.joins(:georeferences).distinct
  else
    ::FieldOccurrence.left_outer_joins(:georeferences)
      .where(georeferences: { id: nil })
      .distinct
  end
end

#import_dataset_id_facetObject



347
348
349
350
351
# File 'lib/queries/field_occurrence/filter.rb', line 347

def import_dataset_id_facet
  return nil if import_dataset_id.blank?
  ::FieldOccurrence.joins(:related_origin_relationships)
  .where(origin_relationships: {old_object_id: import_dataset_id, old_object_type: 'ImportDataset'})
end

#merge_clausesObject



738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
# File 'lib/queries/field_occurrence/filter.rb', line 738

def merge_clauses
  [

 #  import_dataset_id_facet,
 #  observation_query_facet,
 #  biological_association_id_facet,
 #  base_collecting_event_query_facet,
 #  biological_association_query_facet,
 #  collecting_event_query_facet,
 #  extract_query_facet,
 #  otu_query_facet,
 #  taxon_name_query_facet,

 #  biocuration_facet,
 #  biological_associations_facet,
 #  biological_relationship_id_facet,
 #  collecting_event_facet,
 #  collectors_facet,
 #  current_repository_facet,
 #  dates_facet,
 #  deaccessioned_facet,
 #  determiner_facet,
 #  determiner_name_regex_facet,
 #  determiners_facet,
 #  dwc_indexed_facet,
 #  extract_id_facet,
 #  geographic_area_facet,
 #  georeferences_facet,
 #  otu_id_facet,
 #  repository_facet,
 #  sled_image_facet,
 #  taxon_determinations_facet,
 #  taxon_name_id_facet,
  ]
end

#otu_id_facetObject



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/queries/field_occurrence/filter.rb', line 515

def otu_id_facet
  return nil if otu_id.empty?

  w = taxon_determination_table[:biological_field_occurrence_id].eq(table[:id])
    .and(taxon_determination_table[:otu_id].in(otu_id))

  if current_determinations
    w = w.and(taxon_determination_table[:position].eq(1))
  elsif current_determinations == false
    w = w.and(taxon_determination_table[:position].gt(1))
  end

  ::FieldOccurrence.where(
    ::TaxonDetermination.where(w).arel.exists
  )
end

#otu_tableArel::Table

Returns:

  • (Arel::Table)


289
290
291
# File 'lib/queries/field_occurrence/filter.rb', line 289

def otu_table
  ::Otu.arel_table
end

#repository_facetObject



437
438
439
440
441
442
443
444
# File 'lib/queries/field_occurrence/filter.rb', line 437

def repository_facet
  return nil if repository.nil?
  if repository
    ::FieldOccurrence.where.not(repository_id: nil)
  else
    ::FieldOccurrence.where(repository_id: nil)
  end
end

#repository_id_facetObject



505
506
507
508
# File 'lib/queries/field_occurrence/filter.rb', line 505

def repository_id_facet
  return nil if repository_id.blank?
  table[:repository_id].in(repository_id)
end

#sled_image_facetObject



483
484
485
486
# File 'lib/queries/field_occurrence/filter.rb', line 483

def sled_image_facet
  return nil if sled_image_id.nil?
  ::FieldOccurrence::BiologicalFieldOccurrence.joins(:depictions).where('depictions.sled_image_id = ?', sled_image_id)
end

#taxon_determination_tableArel::Table

Returns:

  • (Arel::Table)


294
295
296
# File 'lib/queries/field_occurrence/filter.rb', line 294

def taxon_determination_table
  ::TaxonDetermination.arel_table
end

#taxon_determinations_facetObject



360
361
362
363
364
365
366
367
368
# File 'lib/queries/field_occurrence/filter.rb', line 360

def taxon_determinations_facet
  return nil if taxon_determinations.nil?

  if taxon_determinations
    ::FieldOccurrence.joins(:taxon_determinations).distinct
  else
    ::FieldOccurrence.where.missing(:taxon_determinations)
  end
end

#taxon_name_id_facetObject



532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'lib/queries/field_occurrence/filter.rb', line 532

def taxon_name_id_facet
  return nil if taxon_name_id.empty?

  q = nil
  z = nil

  if descendants
    h = Arel::Table.new(:taxon_name_hierarchies)
    t = ::TaxonName.arel_table

    q = table.join(taxon_determination_table, Arel::Nodes::InnerJoin).on(
      table[:id].eq(taxon_determination_table[:biological_field_occurrence_id])
    ).join(otu_table, Arel::Nodes::InnerJoin).on(
      taxon_determination_table[:otu_id].eq(otu_table[:id])
    ).join(t, Arel::Nodes::InnerJoin).on(
      otu_table[:taxon_name_id].eq(t[:id])
    ).join(h, Arel::Nodes::InnerJoin).on(
      t[:id].eq(h[:descendant_id])
    )
    z = h[:ancestor_id].in(taxon_name_id)

    if validity == true
      z = z.and(t[:cached_valid_taxon_name_id].eq(t[:id]))
    elsif validity == false
      z = z.and(t[:cached_valid_taxon_name_id].not_eq(t[:id]))
    end

    if current_determinations == true
      z = z.and(taxon_determination_table[:position].eq(1))
    elsif current_determinations == false
      z = z.and(taxon_determination_table[:position].gt(1))
    end
  else
    q = ::FieldOccurrence.joins(taxon_determinations: [:otu])
      .where(otus: { taxon_name_id: })

    if current_determinations
      q = q.where(taxon_determinations: { position: 1 })
    end

    return q
  end

  ::FieldOccurrence.joins(q.join_sources).where(z).distinct
end