Class: Queries::CollectingEvent::Filter

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

Constant Summary collapse

ATTRIBUTES =

Params exists for all CollectingEvent attributes except these. geographic_area_id is excluded because we handle it specially in conjunction with ‘geographic_area_mode“ Definition must preceed include.

(::CollectingEvent.core_attributes - %w{geographic_area_id} + %w(cached_level0_geographic_name cached_level1_geographic_name cached_level2_geographic_name)).map(&:to_sym).freeze
BASE_PARAMS =

This list of params are those that only occur in this CollectingEvent filter. For those that overlap other filters place in PARAMS.

Used to define a base collecting query for CollectionObject filters scope, this is still used in CollectionObject query. This is “necessary” so that we can use CollectingEvent referencing facets in the CollectionObject filter for convienience.

[
  *Queries::Concerns::Attributes.params,
  *ATTRIBUTES,
  :collectors,
  :collecting_event_object_id,
  :collection_objects,
  :collector_id,
  :collector_id_or,
  :collecting_event_id,
  :determiner_name_regex,
  :geo_json,
  :geographic_area,
  :geographic_area_id,
  :geographic_area_mode,
  :georeferences,
  :in_labels,
  :md5_verbatim_label,
  :radius,
  :use_max,
  :use_min,
  :wkt,
  collecting_event_id: [],
  collector_id: [],
  geographic_area_id: [],
].inject([{}]){|ary, k| k.is_a?(Hash) ? ary.last.merge!(k) : ary.unshift(k); ary}.freeze
PARAMS =
[
  *BASE_PARAMS,
  :otu_id,
  :collection_object_id,
  collection_object_id: [],
  otu_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, #depiction_query, #descriptor_query, #document_query, #dwc_occurrence_query, #extract_query, #field_occurrence_query, #image_query, #loan_query, #object_global_id, #observation_query, #order_by, #otu_query, #page, #paginate, #params, #per, #person_query, #project_id, #recent, #recent_target, #taxon_name_query, #venn, #venn_mode

Attributes inherited from Query

#query_string, #terms

Class Method Summary collapse

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

Returns a new instance of Filter.

[View source]

150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/queries/collecting_event/filter.rb', line 150

def initialize(query_params)
  super

  @collectors = boolean_param(params, :collectors )
  @collecting_event_id = params[:collecting_event_id]
  @collection_object_id = params[:collection_object_id]
  @collection_objects = boolean_param(params, :collection_objects )
  @collector_id = params[:collector_id]
  @collector_id_or = boolean_param(params, :collector_id_or )
  @geo_json = params[:geo_json]
  @geographic_area = boolean_param(params, :geographic_area)
  @geographic_area_id = params[:geographic_area_id]
  @geographic_area_mode = boolean_param(params, :geographic_area_mode)
  @georeferences = boolean_param(params, :georeferences)
  @in_labels = params[:in_labels]
  @md5_verbatim_label = params[:md5_verbatim_label]&.to_s&.downcase == 'true'
  @otu_id = params[:otu_id].presence
  @radius = params[:radius].presence || 100.0
  @use_max = params[:use_max]
  @use_min = params[:use_min]
  @wkt = params[:wkt]

  set_confidences_params(params)
  set_attributes_params(params)
  set_citations_params(params)
  set_data_attributes_params(params)
  set_date_params(params)
  set_depiction_params(params)
  set_notes_params(params)
  set_protocols_params(params)
  set_tags_params(params)
end

Instance Attribute Details

#collecting_event_idObject

Parameters:

  • collecting_event_id (Array, Integer, nil)

    One or more collecting_event_id


79
80
81
# File 'lib/queries/collecting_event/filter.rb', line 79

def collecting_event_id
  @collecting_event_id
end

#collection_object_idObject

Returns Array.

Parameters:

  • collection_object_id (Array, Integer, String)

    all collecting events matching collection objects

Returns:

  • Array


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

def collection_object_id
  @collection_object_id
end

#collection_objectsObject

Parameters:

  • collection_objects (String, nil)

    legal values are ‘true’, ‘false’ ‘true` - match only CollectingEvents with associated CollectionObjects `false` - match only CollectingEvents without associated CollectionObjects


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

def collection_objects
  @collection_objects
end

#collector_idArray

DONE: singularize and handle array or single

Returns:

  • (Array)

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

def collector_id
  @collector_id
end

#collector_id_orBoolean

Parameters:

  • collector_id_or (String)

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

Returns:

  • (Boolean)

110
111
112
# File 'lib/queries/collecting_event/filter.rb', line 110

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


75
76
77
# File 'lib/queries/collecting_event/filter.rb', line 75

def collectors
  @collectors
end

#geo_jsonHash?

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

Returns:

  • (Hash, nil)

    in geo_json format (no Feature …) ?!


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

def geo_json
  @geo_json
end

#geographic_areaTrue, ...

Returns true - has geographic_area present false - without geographic_area nil - not applied.

Returns:

  • (True, False, nil)

    true - has geographic_area present false - without geographic_area nil - not applied


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

def geographic_area
  @geographic_area
end

#geographic_area_idObject

See /lib/queries/otu/filter.rb


139
140
141
# File 'lib/queries/collecting_event/filter.rb', line 139

def geographic_area_id
  @geographic_area_id
end

#geographic_area_modeObject

Returns the value of attribute geographic_area_mode.


140
141
142
# File 'lib/queries/collecting_event/filter.rb', line 140

def geographic_area_mode
  @geographic_area_mode
end

#georeferencesTrue, ...

Returns true - georeferences false - not georeferenced nil - not applied.

Returns:

  • (True, False, nil)

    true - georeferences false - not georeferenced nil - not applied


130
131
132
# File 'lib/queries/collecting_event/filter.rb', line 130

def georeferences
  @georeferences
end

#in_labelsObject

Wildcard wrapped matching any label


82
83
84
# File 'lib/queries/collecting_event/filter.rb', line 82

def in_labels
  @in_labels
end

#md5_verbatim_labelObject

If true then in_labels checks only the MD5


85
86
87
# File 'lib/queries/collecting_event/filter.rb', line 85

def md5_verbatim_label
  @md5_verbatim_label
end

#otu_idArray

DONE: singularize and handle array or single

Returns:

  • (Array)

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

def otu_id
  @otu_id
end

#radiusObject

Integer in Meters

!! defaults to 100m

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

def radius
  @radius
end

#use_maxString?

Returns the maximum number of CollectionObjects linked to CollectingEvent.

Returns:

  • (String, nil)

    the maximum number of CollectionObjects linked to CollectingEvent


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

def use_max
  @use_max
end

#use_minString?

Returns the minimum number of CollectionObjects linked to CollectingEvent.

Returns:

  • (String, nil)

    the minimum number of CollectionObjects linked to CollectingEvent


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

def use_min
  @use_min
end

#wktObject

A spatial representation in well known text


88
89
90
# File 'lib/queries/collecting_event/filter.rb', line 88

def wkt
  @wkt
end

Class Method Details

.base_paramsObject

[View source]

65
66
67
68
69
# File 'lib/queries/collecting_event/filter.rb', line 65

def self.base_params
  a = BASE_PARAMS.dup
  b = a.pop.keys
  (a + b).uniq
end

Instance Method Details

#and_clausesArray

Returns:

  • (Array)
[View source]

453
454
455
456
457
458
459
460
# File 'lib/queries/collecting_event/filter.rb', line 453

def and_clauses
  [
    between_date_range_facet,
    any_label_facet,
    collecting_event_id_facet,
    verbatim_label_md5_facet,
  ]
end

#any_label_facetObject

[View source]

336
337
338
339
340
# File 'lib/queries/collecting_event/filter.rb', line 336

def any_label_facet
  return nil if in_labels.blank?
  t = "%#{in_labels}%"
  table[:verbatim_label].matches(t).or(table[:print_label].matches(t)).or(table[:document_label].matches(t))
end

#biological_association_query_facetObject

[View source]

375
376
377
378
379
380
381
382
383
384
385
# File 'lib/queries/collecting_event/filter.rb', line 375

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

  ::CollectingEvent.from('(' + s + ') as collecting_events').distinct
end

#collecting_event_id_facetObject

[View source]

349
350
351
352
# File 'lib/queries/collecting_event/filter.rb', line 349

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

#collection_object_query_facetObject

[View source]

397
398
399
400
401
402
403
404
405
406
# File 'lib/queries/collecting_event/filter.rb', line 397

def collection_object_query_facet
  return nil if collection_object_query.nil?
  s = 'WITH query_co_ce AS (' + collection_object_query.all.to_sql + ') ' +
    ::CollectingEvent
    .joins(:collection_objects)
    .joins('JOIN query_co_ce as query_co_ce1 on collection_objects.id = query_co_ce1.id')
    .to_sql

  ::CollectingEvent.from('(' + s + ') as collecting_events').distinct
end

#collection_objects_facetObject

Returns Scope.

Returns:

  • Scope

[View source]

263
264
265
266
267
# File 'lib/queries/collecting_event/filter.rb', line 263

def collection_objects_facet
  return nil if collection_objects.nil?
  subquery = ::CollectionObject.where(::CollectionObject.arel_table[:collecting_event_id].eq(::CollectingEvent.arel_table[:id])).arel.exists
  ::CollectingEvent.where(collection_objects ? subquery : subquery.not)
end

#collector_id_facetObject

TODO: dry with Source, TaxonName, etc.

[View source]

270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/queries/collecting_event/filter.rb', line 270

def collector_id_facet
  return nil if collector_id.empty?
  o = table
  r = ::Role.arel_table

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

  c = r.alias('r1')

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

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

  b = b.where(e.and(f))
  b = b.group(a['id'])
  b = b.having(a['id'].count.eq(collector_id.length)) unless collector_id_or
  b = b.as('col_z_')

  ::CollectingEvent.joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['id'].eq(o['id']))))
end

#collectors_facetObject

[View source]

366
367
368
369
370
371
372
373
# File 'lib/queries/collecting_event/filter.rb', line 366

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

#dwc_occurrence_query_facetObject

[View source]

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

def dwc_occurrence_query_facet
  return nil if dwc_occurrence_query.nil?

  s = ::CollectingEvent
    .with(query_dwc_ce: dwc_occurrence_query.all.select(:dwc_occurrence_object_id, :dwc_occurrence_object_type, :id))
    .joins('JOIN collection_objects co on co.collecting_event_id = collecting_events.id')
    .joins("JOIN dwc_occurrences dwo on dwo.dwc_occurrence_object_type = 'CollectionObject' and dwo.dwc_occurrence_object_id = co.id")
    .joins('JOIN query_dwc_ce as query_dwc_ce1 on query_dwc_ce1.id = dwo.id')
    .to_sql

  ::CollectingEvent.from('(' + s + ') as collecting_events').distinct
end

#field_occurrence_query_facetObject

[View source]

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

def field_occurrence_query_facet
  return nil if field_occurrence_query.nil?
  s = 'WITH query_fo_ce AS (' + field_occurrence_query.all.to_sql + ') ' +
    ::CollectingEvent
    .joins(:field_occurrences)
    .joins('JOIN query_fo_ce as query_fo_ce1 on field_occurrences.id = query_fo_ce1.id')
    .to_sql

  ::CollectingEvent.from('(' + s + ') as collecting_events').distinct
end

#from_wkt(wkt_shape) ⇒ Object

TODO: check, this should be simplifiable.

[View source]

304
305
306
307
308
# File 'lib/queries/collecting_event/filter.rb', line 304

def from_wkt(wkt_shape)
  a = RGeo::WKRep::WKTParser.new(Gis::FACTORY, support_wkt12: true)
  b = a.parse(wkt_shape)
  spatial_query(b.geometry_type.to_s, wkt_shape)
end

#geo_json_facetObject

Shape is a Hash in GeoJSON format

[View source]

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

def geo_json_facet
  return nil if geo_json.nil?
  if a = RGeo::GeoJSON.decode(geo_json)
    return spatial_query(a.geometry_type.to_s, a.to_s)
  else
    return nil
  end
end

#geographic_area_facetObject

[View source]

218
219
220
221
222
223
224
225
# File 'lib/queries/collecting_event/filter.rb', line 218

def geographic_area_facet
  return nil if geographic_area.nil?
  if geographic_area
    ::CollectingEvent.where.not(geographic_area_id: nil).distinct
  else
    ::CollectingEvent.where(geographic_area_id: nil).distinct
  end
end

#geographic_area_id_facetObject

[View source]

227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/queries/collecting_event/filter.rb', line 227

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
  case geographic_area_mode
  when nil, false # exact, descendants
    return ::CollectingEvent.where(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
    return from_wkt(wkt_shape)
  end
end

#georeferences_facetObject

[View source]

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

def georeferences_facet
  return nil if georeferences.nil?

  if georeferences
    ::CollectingEvent.joins(:georeferences).distinct
  else
    ::CollectingEvent.left_outer_joins(:georeferences)
      .where(georeferences: {id: nil})
      .distinct
  end
end

#housekeeping_extensionsObject

[View source]

443
444
445
446
447
448
449
450
# File 'lib/queries/collecting_event/filter.rb', line 443

def housekeeping_extensions
  [
    housekeeping_extension_query(target: ::DataAttribute, joins: [:data_attributes]),
    housekeeping_extension_query(target: ::Georeference, joins: [:georeferences]),
    housekeeping_extension_query(target: ::Note, joins: [:notes]),
    housekeeping_extension_query(target: ::Role, joins: [:roles]),
  ]
end

#matching_collection_object_idObject

[View source]

361
362
363
364
# File 'lib/queries/collecting_event/filter.rb', line 361

def matching_collection_object_id
  return nil if collection_object_id.empty?
  ::CollectingEvent.joins(:collection_objects).where(collection_objects: {id: collection_object_id}).distinct
end

#merge_clausesObject

[View source]

462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/queries/collecting_event/filter.rb', line 462

def merge_clauses
  [
    biological_association_query_facet,
    collection_object_query_facet,
    field_occurrence_query_facet,
    dwc_occurrence_query_facet,
    otu_query_facet,
    taxon_name_query_facet,

    collectors_facet,
    collection_objects_facet,
    collector_id_facet,
    geo_json_facet,
    geographic_area_facet,
    geographic_area_id_facet,
    georeferences_facet,
    matching_collection_object_id,
    otu_id_facet,
    use_facet,
    wkt_facet,
  ]
end

#otu_id_facetObject

[View source]

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

def otu_id_facet
  return nil if otu_id.empty?
  a = ::CollectingEvent.joins(:collection_object_otus).where(otus: {id: otu_id})
  b = ::CollectingEvent.joins(:field_occurrence_otus).where(otus: {id: otu_id})
  ::Queries.union(::CollectingEvent, [a,b]).distinct
end

#otu_query_facetObject

[View source]

387
388
389
390
391
392
393
394
395
# File 'lib/queries/collecting_event/filter.rb', line 387

def otu_query_facet
  return nil if otu_query.nil?
  s = 'WITH query_otu_ces AS (' + otu_query.all.to_sql + ') ' +
    ::CollectingEvent.joins(:otus)
    .joins('JOIN query_otu_ces as query_otu_ces1 on query_otu_ces1.id = otus.id')
    .to_sql

  ::CollectingEvent.from('(' + s + ') as collecting_events').distinct
end

#spatial_query(geometry_type, wkt) ⇒ Object

TODO: Spatial concern?

[View source]

321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/queries/collecting_event/filter.rb', line 321

def spatial_query(geometry_type, wkt)
  case geometry_type
  when 'Point'
    ::CollectingEvent
      .joins(:geographic_items)
      .where(::GeographicItem.within_radius_of_wkt_sql(wkt, radius ))
  when 'Polygon', 'MultiPolygon'
    ::CollectingEvent
      .joins(:geographic_items)
      .where(::GeographicItem.contained_by_wkt_sql(wkt))
  else
    nil
  end
end

#taxon_name_query_facetObject

[View source]

432
433
434
435
436
437
438
439
440
441
# File 'lib/queries/collecting_event/filter.rb', line 432

def taxon_name_query_facet
  return nil if taxon_name_query.nil?
  s = 'WITH query_tn_ce AS (' + taxon_name_query.all.to_sql + ') ' +
    ::CollectingEvent
    .joins(collection_objects: [:otus])
    .joins('JOIN query_tn_ce as query_tn_ce1 on otus.taxon_name_id = query_tn_ce1.id')
    .to_sql

  ::CollectingEvent.from('(' + s + ') as collecting_events').distinct
end

#use_facetObject

[View source]

203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/queries/collecting_event/filter.rb', line 203

def use_facet
  return nil if (use_min.blank? && use_max.blank?)
  min_max = [use_min&.to_i, use_max&.to_i ].compact

  q = ::CollectingEvent.joins(:collection_objects)
    .select('collecting_events.*, COUNT(collection_objects.collecting_event_id)')
    .group('collecting_events.id')
    .having("COUNT(collecting_event_id) >= #{min_max[0]}")

  # Untested
  q = q.having("COUNT(collecting_event_id) <= #{min_max[1]}") if min_max[1]

  ::CollectingEvent.from('(' + q.to_sql + ') as collecting_events').distinct
end

#verbatim_label_md5_facetObject

[View source]

342
343
344
345
346
347
# File 'lib/queries/collecting_event/filter.rb', line 342

def verbatim_label_md5_facet
  return nil unless md5_verbatim_label && in_labels.present?
  md5 = ::Utilities::Strings.generate_md5(in_labels)

  table[:md5_of_verbatim_label].eq(md5)
end

#wkt_facetObject

[View source]

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

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