Class: Queries::CollectionObject::Filter

Inherits:
Query::Filter show all
Includes:
Queries::Concerns::Citations, Queries::Concerns::Containable, Queries::Concerns::DataAttributes, Queries::Concerns::Depictions, Queries::Concerns::Notes, Queries::Concerns::Protocols, Queries::Concerns::Tags, Helpers
Defined in:
lib/queries/collection_object/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,
  :buffered_collecting_event,
  :buffered_determinations,
  :buffered_other_labels,
  :collecting_event,
  :collection_object_id,
  :collection_object_type,
  :collectors,
  :current_determinations,
  :current_repository,
  :current_repository_id,
  :dates,
  :deaccessioned,
  :descendants,
  :determiner_id_or,
  :determiner_id,
  :determiner_name_regex,
  :determiners,
  :dwc_indexed,
  :exact_buffered_collecting_event,
  :exact_buffered_determinations,
  :exact_buffered_other_labels,
  :extract_id,
  :georeferences,
  :import_dataset_id,
  :loaned,
  :never_loaned,
  :on_loan,
  :preparation_type,
  :preparation_type_id,
  :repository,
  :repository_id,
  :sled_image_id,
  :spatial_geographic_areas,
  :taxon_determination_id,
  :taxon_determinations,
  :taxon_name_id,
  :type_material,
  :type_specimen_taxon_name_id,
  :validity,
  :with_buffered_collecting_event,
  :with_buffered_determinations,
  :with_buffered_other_labels,
  biocuration_class_id: [],
  biological_association_id: [],
  biological_relationship_id: [],
  collecting_event_id: [],
  collection_object_id: [],
  determiner_id: [],
  extract_id: [],
  geographic_area_id: [],
  import_dataset_id: [],
  is_type: [],
  loan_id: [],
  otu_id: [],
  preparation_type_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, #extract_query, #field_occurrence_query, #image_query, #loan_query, #object_global_id, #observation_query, #otu_query, #page, #paginate, #params, #per, #person_query, #project_id, #recent, #taxon_name_query, #venn, #venn_mode

Attributes inherited from Query

#query_string, #terms

Instance Method Summary collapse

Methods included from Helpers

#boolean_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, #deep_permit, included_annotator_facets, inverted_subqueries, #model_id_facet, #object_global_id_facet, params, #permitted_params, #process_url_into_params, #project_id_facet, query_name, #query_name, #set_nested_queries, #shared_and_clauses, #subquery_vector, #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



317
318
319
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/queries/collection_object/filter.rb', line 317

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?
  @buffered_collecting_event = params[:buffered_collecting_event]
  @buffered_determinations = params[:buffered_determinations]
  @buffered_other_labels = params[:buffered_other_labels]
  @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]
  @collection_object_id = params[:collection_object_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)
  @exact_buffered_collecting_event = boolean_param(params, :exact_buffered_collecting_event)
  @exact_buffered_determinations = boolean_param(params, :exact_buffered_determinations)
  @exact_buffered_other_labels = boolean_param(params, :exact_buffered_other_labels)
  @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] || []
  @loan_id = params[:loan_id]
  @loaned = boolean_param(params, :loaned)
  @never_loaned = boolean_param(params, :never_loaned)
  @on_loan = boolean_param(params, :on_loan)
  @otu_descendants = boolean_param(params, :otu_descendants)
  @otu_id = params[:otu_id]
  @preparation_type = boolean_param(params, :preparation_type)
  @preparation_type_id = params[:preparation_type_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]
  @type_material = boolean_param(params, :type_material)
  @type_specimen_taxon_name_id = (params[:type_specimen_taxon_name_id].presence)
  @validity = boolean_param(params, :validity)
  @with_buffered_collecting_event = boolean_param(params, :with_buffered_collecting_event)
  @with_buffered_determinations = boolean_param(params, :with_buffered_determinations)
  @with_buffered_other_labels = boolean_param(params, :with_buffered_other_labels)

  set_citations_params(params)
  set_containable_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



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

def base_collecting_event_query
  @base_collecting_event_query
end

#biocuration_class_idArray

Returns of biocuration_class ids.

Returns:

  • (Array)

    of biocuration_class ids



170
171
172
# File 'lib/queries/collection_object/filter.rb', line 170

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



93
94
95
# File 'lib/queries/collection_object/filter.rb', line 93

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



89
90
91
# File 'lib/queries/collection_object/filter.rb', line 89

def biological_associations
  @biological_associations
end

#biological_relationship_idArray

Returns of biological_relationship#id.

Returns:

  • (Array)

    of biological_relationship#id



174
175
176
# File 'lib/queries/collection_object/filter.rb', line 174

def biological_relationship_id
  @biological_relationship_id
end

#buffered_collecting_eventString?

Returns:

  • (String, nil)


271
272
273
# File 'lib/queries/collection_object/filter.rb', line 271

def buffered_collecting_event
  @buffered_collecting_event
end

#buffered_determinationsString?

Returns:

  • (String, nil)


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

def buffered_determinations
  @buffered_determinations
end

#buffered_other_labelsString?

Returns:

  • (String, nil)


280
281
282
# File 'lib/queries/collection_object/filter.rb', line 280

def buffered_other_labels
  @buffered_other_labels
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)


243
244
245
# File 'lib/queries/collection_object/filter.rb', line 243

def collecting_event
  @collecting_event
end

#collecting_event_idObject

Array

only return objects with these collecting event ID



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

def collecting_event_id
  @collecting_event_id
end

#collection_object_idObject

Parameters:

  • Array (String, nil)

    or Integer of CollectionObject ids



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

def collection_object_id
  @collection_object_id
end

#collection_object_typeObject

Parameters:

  • one (String, nil)

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



101
102
103
# File 'lib/queries/collection_object/filter.rb', line 101

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



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

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



138
139
140
# File 'lib/queries/collection_object/filter.rb', line 138

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



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

def current_repository
  @current_repository
end

#current_repository_idCurrentRepository#id?

Returns:

  • (CurrentRepository#id, nil)


189
190
191
# File 'lib/queries/collection_object/filter.rb', line 189

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



314
315
316
# File 'lib/queries/collection_object/filter.rb', line 314

def dates
  @dates
end

#deaccessionedBoolean?

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

Returns:

  • (Boolean, nil)

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



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

def deaccessioned
  @deaccessioned
end

#descendantsObject

Returns the value of attribute descendants.



120
121
122
# File 'lib/queries/collection_object/filter.rb', line 120

def descendants
  @descendants
end

#determiner_idArray

Parameters:

  • determiner (Array or Person#id)

    one ore more people id

Returns:

  • (Array)


254
255
256
# File 'lib/queries/collection_object/filter.rb', line 254

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)


260
261
262
# File 'lib/queries/collection_object/filter.rb', line 260

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



304
305
306
# File 'lib/queries/collection_object/filter.rb', line 304

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



144
145
146
# File 'lib/queries/collection_object/filter.rb', line 144

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



180
181
182
# File 'lib/queries/collection_object/filter.rb', line 180

def dwc_indexed
  @dwc_indexed
end

#exact_buffered_collecting_eventBoolean?

Returns:

  • (Boolean, nil)


274
275
276
# File 'lib/queries/collection_object/filter.rb', line 274

def exact_buffered_collecting_event
  @exact_buffered_collecting_event
end

#exact_buffered_determinationsBoolean?

Returns:

  • (Boolean, nil)


268
269
270
# File 'lib/queries/collection_object/filter.rb', line 268

def exact_buffered_determinations
  @exact_buffered_determinations
end

#exact_buffered_other_labelsBoolean?

Returns:

  • (Boolean, nil)


277
278
279
# File 'lib/queries/collection_object/filter.rb', line 277

def exact_buffered_other_labels
  @exact_buffered_other_labels
end

#extract_idObject

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

Returns:

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



308
309
310
# File 'lib/queries/collection_object/filter.rb', line 308

def extract_id
  @extract_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



249
250
251
# File 'lib/queries/collection_object/filter.rb', line 249

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



215
216
217
# File 'lib/queries/collection_object/filter.rb', line 215

def georeferences
  @georeferences
end

#import_dataset_idArray

Returns of ImportDataset ids.

Returns:

  • (Array)

    of ImportDataset ids



83
84
85
# File 'lib/queries/collection_object/filter.rb', line 83

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
    


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

def is_type
  @is_type
end

#loan_idArray

Returns a list of loan#id, all collection objects inside them will be included.

Returns:

  • (Array)

    a list of loan#id, all collection objects inside them will be included



166
167
168
# File 'lib/queries/collection_object/filter.rb', line 166

def loan_id
  @loan_id
end

#loanedObject

Parameters:

  • loaned (True)

    true - the CollectionObject has been loaned at least once



158
159
160
# File 'lib/queries/collection_object/filter.rb', line 158

def loaned
  @loaned
end

#never_loanedObject

Parameters:

  • never_loaned (True)

    true - the CollectionObject has never been loaned



162
163
164
# File 'lib/queries/collection_object/filter.rb', line 162

def never_loaned
  @never_loaned
end

#on_loanObject

Parameters:

  • on_loan (True)

    true - the CollectionObject currently on loan



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

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



113
114
115
# File 'lib/queries/collection_object/filter.rb', line 113

def otu_id
  @otu_id
end

#preparation_typeTrue, ...

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

Returns:

  • (True, False, nil)

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



233
234
235
# File 'lib/queries/collection_object/filter.rb', line 233

def preparation_type
  @preparation_type
end

#preparation_type_idArray

Returns:

  • (Array)


236
237
238
# File 'lib/queries/collection_object/filter.rb', line 236

def preparation_type_id
  @preparation_type_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



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

def repository
  @repository
end

#repository_idRepository#id?

Returns:



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

def repository_id
  @repository_id
end

#sled_image_idSledImage#id?

Returns:



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

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



203
204
205
# File 'lib/queries/collection_object/filter.rb', line 203

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



209
210
211
# File 'lib/queries/collection_object/filter.rb', line 209

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



118
119
120
# File 'lib/queries/collection_object/filter.rb', line 118

def taxon_name_id
  @taxon_name_id
end

#type_materialTrue, ...

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



286
287
288
# File 'lib/queries/collection_object/filter.rb', line 286

def type_material
  @type_material
end

#type_specimen_taxon_name_idProtonym#id?

Returns:



183
184
185
# File 'lib/queries/collection_object/filter.rb', line 183

def type_specimen_taxon_name_id
  @type_specimen_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



132
133
134
# File 'lib/queries/collection_object/filter.rb', line 132

def validity
  @validity
end

#with_buffered_collecting_eventObject

See with_buffered_determinations



295
296
297
# File 'lib/queries/collection_object/filter.rb', line 295

def with_buffered_collecting_event
  @with_buffered_collecting_event
end

#with_buffered_determinationsBoolean?

Returns Boolean, nil.

Parameters:

  • with_buffered_determinations (String, nil)

    ‘false`, nil - without buffered determination field value ’true’ - with buffered_determinations field value

Returns:

  • (Boolean, nil)

    Boolean, nil



292
293
294
# File 'lib/queries/collection_object/filter.rb', line 292

def with_buffered_determinations
  @with_buffered_determinations
end

#with_buffered_other_labelsObject

See with_buffered_determinations



298
299
300
# File 'lib/queries/collection_object/filter.rb', line 298

def with_buffered_other_labels
  @with_buffered_other_labels
end

Instance Method Details

#and_clausesObject



978
979
980
981
982
983
984
985
986
987
988
989
990
# File 'lib/queries/collection_object/filter.rb', line 978

def and_clauses
  [
    attribute_exact_facet(:buffered_collecting_event),
    attribute_exact_facet(:buffered_determinations),
    attribute_exact_facet(:buffered_other_labels),
    collecting_event_id_facet,
    collection_object_id_facet,
    current_repository_id_facet,
    preparation_type_id_facet,
    repository_id_facet,
    type_facet,
  ]
end

#base_collecting_event_query_facetObject



875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
# File 'lib/queries/collection_object/filter.rb', line 875

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 + ') ' +
    ::CollectionObject
    .joins('JOIN query_ce_base_co as query_ce_base_co1 on query_ce_base_co1.id = collection_objects.collecting_event_id')
    .to_sql

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

#biocuration_facetObject



633
634
635
636
637
# File 'lib/queries/collection_object/filter.rb', line 633

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

#biological_association_id_facetObject

TODO: turn into UNION!



912
913
914
915
916
917
918
919
920
921
922
923
# File 'lib/queries/collection_object/filter.rb', line 912

def biological_association_id_facet
  return nil if biological_association_id.empty?
  b = ::BiologicalAssociation.where(id: biological_association_id)
  s = 'WITH query_ba_id_co AS (' + b.all.to_sql + ') ' +
    ::CollectionObject
    .joins("LEFT JOIN query_ba_id_co as query_ba_id_co1 on collection_objects.id = query_ba_id_co1.biological_association_subject_id AND query_ba_id_co1.biological_association_subject_type = 'CollectionObject'")
    .joins("LEFT JOIN query_ba_id_co as query_ba_id_co2 on collection_objects.id = query_ba_id_co2.biological_association_object_id AND query_ba_id_co2.biological_association_object_type = 'CollectionObject'")
    .where('(query_ba_id_co1.id) IS NOT NULL OR (query_ba_id_co2.id IS NOT NULL)')
    .to_sql

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

#biological_association_query_facetObject

TODO: turn into UNION!



926
927
928
929
930
931
932
933
934
935
936
# File 'lib/queries/collection_object/filter.rb', line 926

def biological_association_query_facet
  return nil if biological_association_query.nil?
  s = 'WITH query_ba_co AS (' + biological_association_query.all.to_sql + ') ' +
    ::CollectionObject
    .joins("LEFT JOIN query_ba_co as query_ba_co1 on collection_objects.id = query_ba_co1.biological_association_subject_id AND query_ba_co1.biological_association_subject_type = 'CollectionObject'")
    .joins("LEFT JOIN query_ba_co as query_ba_co2 on collection_objects.id = query_ba_co2.biological_association_object_id AND query_ba_co2.biological_association_object_type = 'CollectionObject'")
    .where('(query_ba_co1.id) IS NOT NULL OR (query_ba_co2.id IS NOT NULL)')
    .to_sql

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

#biological_associations_facetObject



903
904
905
906
907
908
909
# File 'lib/queries/collection_object/filter.rb', line 903

def biological_associations_facet
  return nil if biological_associations.nil?
  a = ::CollectionObject.joins(:biological_associations)
  b = ::CollectionObject.joins(:related_biological_associations)

  referenced_klass_union([a, b])
end

#biological_relationship_id_facetObject



654
655
656
657
# File 'lib/queries/collection_object/filter.rb', line 654

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

#collecting_event_facetObject



584
585
586
587
588
589
590
591
# File 'lib/queries/collection_object/filter.rb', line 584

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

#collecting_event_id_facetObject



682
683
684
685
# File 'lib/queries/collection_object/filter.rb', line 682

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

#collecting_event_query_facetObject



855
856
857
858
859
860
861
862
863
# File 'lib/queries/collection_object/filter.rb', line 855

def collecting_event_query_facet
  return nil if collecting_event_query.nil?
  s = 'WITH query_ce_co AS (' + collecting_event_query.all.to_sql + ') ' +
    ::CollectionObject
    .joins('JOIN query_ce_co as query_ce_co1 on query_ce_co1.id = collection_objects.collecting_event_id')
    .to_sql

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

#collecting_event_tableArel::Table

Returns:

  • (Arel::Table)


389
390
391
# File 'lib/queries/collection_object/filter.rb', line 389

def collecting_event_table
  ::CollectingEvent.arel_table
end

#collection_object_id_facetObject



460
461
462
463
# File 'lib/queries/collection_object/filter.rb', line 460

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

#collectors_facetObject



532
533
534
535
536
537
538
539
# File 'lib/queries/collection_object/filter.rb', line 532

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

#current_repository_facetObject



566
567
568
569
570
571
572
573
# File 'lib/queries/collection_object/filter.rb', line 566

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

#current_repository_id_facetObject



697
698
699
700
# File 'lib/queries/collection_object/filter.rb', line 697

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

#dates_facetObject



800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
# File 'lib/queries/collection_object/filter.rb', line 800

def dates_facet
  return nil if dates.nil?
  if dates
    ::CollectionObject.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
    ::CollectionObject.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



827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
# File 'lib/queries/collection_object/filter.rb', line 827

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

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

#determiner_facetObject

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



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/queries/collection_object/filter.rb', line 490

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_')

  ::CollectionObject.joins(Arel::Nodes::InnerJoin.new(b,
  Arel::Nodes::On.new( b['taxon_determination_object_id'].eq(tt['id']).and( b['taxon_determination_object_type'].eq('CollectionObject') )
  )))
end

#determiner_name_regex_facetObject



541
542
543
544
# File 'lib/queries/collection_object/filter.rb', line 541

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

#determiners_facetObject



523
524
525
526
527
528
529
530
# File 'lib/queries/collection_object/filter.rb', line 523

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

#dwc_indexed_facetObject



675
676
677
678
679
680
# File 'lib/queries/collection_object/filter.rb', line 675

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

#extract_id_facetObject



471
472
473
474
475
476
# File 'lib/queries/collection_object/filter.rb', line 471

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

#extract_query_facetObject



938
939
940
941
942
943
944
945
946
947
948
# File 'lib/queries/collection_object/filter.rb', line 938

def extract_query_facet
  return nil if extract_query.nil?

  s = 'WITH query_extract_co AS (' + extract_query.all.to_sql + ') ' +
    ::CollectionObject
    .joins(:origin_relationships)
    .joins("JOIN query_extract_co as query_extract_co1 on origin_relationships.new_object_id = query_extract_co1.id and origin_relationships.new_object_type = 'Extract'")
    .to_sql

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

#geographic_area_facetObject

This is not spatial



621
622
623
624
625
626
627
628
629
630
631
# File 'lib/queries/collection_object/filter.rb', line 621

def geographic_area_facet
  return nil if geographic_area.nil?

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

#georeferences_facetObject



546
547
548
549
550
551
552
553
554
555
# File 'lib/queries/collection_object/filter.rb', line 546

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

#housekeeping_extensionsObject



962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
# File 'lib/queries/collection_object/filter.rb', line 962

def housekeeping_extensions
  [
    housekeeping_extension_query(target: ::CollectingEvent, joins: [:collecting_event]),
    housekeeping_extension_query(target: ::TaxonDetermination, joins: [:taxon_determinations]),
    housekeeping_extension_query(target: ::DataAttribute, joins: [:data_attributes]),
    housekeeping_extension_query(target: ::Georeference, joins: [:georeferences]),
    housekeeping_extension_query(target: ::DataAttribute, joins: {collecting_event: [:data_attributes]}),
    housekeeping_extension_query(target: ::DataAttribute, joins: {collecting_event: [:data_attributes]}),
    housekeeping_extension_query(target: ::Note, joins: {collecting_event: [:notes]}),
    housekeeping_extension_query(target: ::Note, joins: [:notes]),
    housekeeping_extension_query(target: ::Role, joins: [:roles]),
    housekeeping_extension_query(target: ::Role, joins: {collecting_event: [:roles]}),
    housekeeping_extension_query(target: ::Role, joins: { taxon_determinations: [:determiner_roles]}),
  ]
end

#import_dataset_id_facetObject



465
466
467
468
469
# File 'lib/queries/collection_object/filter.rb', line 465

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

#loan_facetObject



639
640
641
642
# File 'lib/queries/collection_object/filter.rb', line 639

def loan_facet
  return nil if loan_id.empty?
  ::CollectionObject::BiologicalCollectionObject.joins(:loans).where(loans: { id: loan_id })
end

#loan_query_facetObject



865
866
867
868
869
870
871
872
873
# File 'lib/queries/collection_object/filter.rb', line 865

def loan_query_facet
  return nil if loan_query.nil?
  s = 'WITH query_loan_co AS (' + loan_query.all.to_sql + ') ' +
    ::CollectionObject.joins(:loan_items)
    .joins('JOIN query_loan_co as query_loan_co1 on query_loan_co1.id = loan_items.loan_id')
    .to_sql

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

#loaned_facetObject



659
660
661
662
# File 'lib/queries/collection_object/filter.rb', line 659

def loaned_facet
  return nil unless loaned
  ::CollectionObject.loaned
end

#merge_clausesObject



992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
# File 'lib/queries/collection_object/filter.rb', line 992

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,
    loan_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,
    loan_facet,
    loaned_facet,
    never_loaned_facet,
    on_loan_facet,
    otu_id_facet,
    preparation_type_facet,
    repository_facet,
    sled_image_facet,
    taxon_determinations_facet,
    taxon_name_id_facet,
    type_by_taxon_name_facet,
    type_material_facet,
    type_material_type_facet,
    with_buffered_collecting_event_facet,
    with_buffered_determinations_facet,
    with_buffered_other_labels_facet,
  ]
end

#never_loaned_facetObject



664
665
666
667
# File 'lib/queries/collection_object/filter.rb', line 664

def never_loaned_facet
  return nil unless never_loaned
  ::CollectionObject.never_loaned
end

#observation_query_facetObject



950
951
952
953
954
955
956
957
958
959
960
# File 'lib/queries/collection_object/filter.rb', line 950

def observation_query_facet
  return nil if observation_query.nil?

  s = 'WITH query_obs_co AS (' + observation_query.all.to_sql + ') ' +
    ::CollectionObject
    .joins(:observations)
    .joins('JOIN query_obs_co as query_obs_co1 on observations.id = query_obs_co1.id')
    .to_sql

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

#on_loan_facetObject

TODO: change to not/on



670
671
672
673
# File 'lib/queries/collection_object/filter.rb', line 670

def on_loan_facet
  return nil unless on_loan
  ::CollectionObject.on_loan
end

#otu_id_facetObject



735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
# File 'lib/queries/collection_object/filter.rb', line 735

def otu_id_facet
  return nil if otu_id.empty?

  w = taxon_determination_table[:taxon_determination_object_id].eq(table[:id])
    .and(taxon_determination_table[:otu_id].in(otu_id))
    .and(taxon_determination_table[:taxon_determination_object_type].eq('CollectionObject'))

  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

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

#otu_query_facetObject



891
892
893
894
895
896
897
898
899
900
901
# File 'lib/queries/collection_object/filter.rb', line 891

def otu_query_facet
  return nil if otu_query.nil?
  s = 'WITH query_otu_co AS (' + otu_query.all.to_sql + ') ' +
    ::CollectionObject
    .joins(:taxon_determinations)
    .joins('JOIN query_otu_co as query_otu_co1 on query_otu_co1.id = taxon_determinations.otu_id')
    .where(taxon_determinations: { position: 1 })
    .to_sql

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

#otu_tableArel::Table

Returns:

  • (Arel::Table)


394
395
396
# File 'lib/queries/collection_object/filter.rb', line 394

def otu_table
  ::Otu.arel_table
end

#preparation_type_facetObject



575
576
577
578
579
580
581
582
# File 'lib/queries/collection_object/filter.rb', line 575

def preparation_type_facet
  return nil if preparation_type.nil?
  if preparation_type
    ::CollectionObject.where.not(preparation_type_id: nil)
  else
    ::CollectionObject.where(preparation_type_id: nil)
  end
end

#preparation_type_id_facetObject



687
688
689
690
# File 'lib/queries/collection_object/filter.rb', line 687

def preparation_type_id_facet
  return nil if preparation_type_id.empty?
  table[:preparation_type_id].in(preparation_type_id)
end

#repository_facetObject



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

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

#repository_id_facetObject



692
693
694
695
# File 'lib/queries/collection_object/filter.rb', line 692

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

#sled_image_facetObject



649
650
651
652
# File 'lib/queries/collection_object/filter.rb', line 649

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

#taxon_determination_tableArel::Table

Returns:

  • (Arel::Table)


404
405
406
# File 'lib/queries/collection_object/filter.rb', line 404

def taxon_determination_table
  ::TaxonDetermination.arel_table
end

#taxon_determinations_facetObject



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

def taxon_determinations_facet
  return nil if taxon_determinations.nil?

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

#taxon_name_id_facetObject



753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
# File 'lib/queries/collection_object/filter.rb', line 753

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[:taxon_determination_object_id])
      .and(taxon_determination_table[:taxon_determination_object_type]).eq('CollectionObject')
    ).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 = ::CollectionObject.joins(taxon_determinations: [:otu])
      .where(otus: { taxon_name_id: })

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

    return q
  end

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

#taxon_name_query_facetObject



844
845
846
847
848
849
850
851
852
853
# File 'lib/queries/collection_object/filter.rb', line 844

def taxon_name_query_facet
  return nil if taxon_name_query.nil?
  s = 'WITH query_tn_co AS (' + taxon_name_query.all.to_sql + ') ' +
    ::CollectionObject
    .joins(:taxon_names)
    .joins('JOIN query_tn_co as query_tn_co1 on query_tn_co1.id = taxon_names.id')
    .to_sql

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

#type_by_taxon_name_facetObject



702
703
704
705
706
707
708
709
710
711
# File 'lib/queries/collection_object/filter.rb', line 702

def type_by_taxon_name_facet
  return nil if type_specimen_taxon_name_id.nil?

  w = type_materials_table[:collection_object_id].eq(table[:id])
    .and(type_materials_table[:protonym_id].eq(type_specimen_taxon_name_id))

  ::CollectionObject.where(
    ::TypeMaterial.where(w).arel.exists
  )
end

#type_facetObject



644
645
646
647
# File 'lib/queries/collection_object/filter.rb', line 644

def type_facet
  return nil if collection_object_type.nil?
  table[:type].eq(collection_object_type)
end

#type_material_facetObject



724
725
726
727
728
729
730
731
732
733
# File 'lib/queries/collection_object/filter.rb', line 724

def type_material_facet
  return nil if type_material.nil?
  if type_material
    ::CollectionObject.joins(:type_materials).distinct
  else
    ::CollectionObject.left_outer_joins(:type_materials)
      .where(type_materials: { id: nil })
      .distinct
  end
end

#type_material_type_facetObject



713
714
715
716
717
718
719
720
721
722
# File 'lib/queries/collection_object/filter.rb', line 713

def type_material_type_facet
  return nil if is_type.empty?

  w = type_materials_table[:collection_object_id].eq(table[:id])
    .and(type_materials_table[:type_type].in(is_type))

  ::CollectionObject.where(
    ::TypeMaterial.where(w).arel.exists
  )
end

#type_materials_tableArel::Table

Returns:

  • (Arel::Table)


399
400
401
# File 'lib/queries/collection_object/filter.rb', line 399

def type_materials_table
  ::TypeMaterial.arel_table
end

#with_buffered_collecting_event_facetObject



593
594
595
596
597
598
599
600
# File 'lib/queries/collection_object/filter.rb', line 593

def with_buffered_collecting_event_facet
  return nil if with_buffered_collecting_event.nil?
  if with_buffered_collecting_event
    ::CollectionObject.where.not(buffered_collecting_event: nil)
  else
    ::CollectionObject.where(buffered_collecting_event: nil)
  end
end

#with_buffered_determinations_facetObject



611
612
613
614
615
616
617
618
# File 'lib/queries/collection_object/filter.rb', line 611

def with_buffered_determinations_facet
  return nil if with_buffered_determinations.nil?
  if with_buffered_determinations
    ::CollectionObject.where.not(buffered_determinations: nil)
  else
    ::CollectionObject.where(buffered_determinations: nil)
  end
end

#with_buffered_other_labels_facetObject



602
603
604
605
606
607
608
609
# File 'lib/queries/collection_object/filter.rb', line 602

def with_buffered_other_labels_facet
  return nil if with_buffered_other_labels.nil?
  if with_buffered_other_labels
    ::CollectionObject.where.not(buffered_other_labels: nil)
  else
    ::CollectionObject.where(buffered_other_labels: nil)
  end
end