Class: Queries::DwcOccurrence::Filter

Inherits:
Query::Filter show all
Includes:
Concerns::Attributes, Concerns::Users, Helpers
Defined in:
lib/queries/dwc_occurrence/filter.rb

Overview

Keep this minimal, in pricinple filtering should be done on the base objects, not the core here.

Constant Summary collapse

ATTRIBUTES =
::DwcOccurrence.column_names.reject{ |c| %w{
        id project_id created_by_id updated_by_id created_at updated_at
}.include?(c)}.map(&:to_sym).freeze
PARAMS =
[
  *Queries::Concerns::Attributes.params,
  *ATTRIBUTES,
  :dwc_occurrence_id,
  :dwc_occurrence_object_type,
  :dwc_occurrence_object_id,
  :person_id,
  :taxon_name_id,
  #  :otu_id,

  dwc_occurrence_object_type: [],
  dwc_occurrence_object_id: [],
  dwc_occurrence_id: [],
  #   otu_id: [],
  person_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

Returns a new instance of Filter.



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/queries/dwc_occurrence/filter.rb', line 45

def initialize(query_params)
  super

  @dwc_occurrence_id = params[:dwc_occurrence_id]
  @dwc_occurrence_object_id = params[:dwc_occurrence_object_id]
  @dwc_occurrence_object_type = params[:dwc_occurrence_object_type]

  #  @otu_id = params[:otu_id]
  @person_id = params[:person_id]
  @taxon_name_id = params[:taxon_name_id]

  set_attributes_params(params)
end

Instance Attribute Details

#dwc_occurrence_idObject

Returns the value of attribute dwc_occurrence_id.



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

def dwc_occurrence_id
  @dwc_occurrence_id
end

#dwc_occurrence_object_idObject

Used independantly now, not paired



38
39
40
# File 'lib/queries/dwc_occurrence/filter.rb', line 38

def dwc_occurrence_object_id
  @dwc_occurrence_object_id
end

#dwc_occurrence_object_typeObject

Returns the value of attribute dwc_occurrence_object_type.



39
40
41
# File 'lib/queries/dwc_occurrence/filter.rb', line 39

def dwc_occurrence_object_type
  @dwc_occurrence_object_type
end

#person_idObject

def otu_id

[@otu_id].flatten.compact

end



42
43
44
# File 'lib/queries/dwc_occurrence/filter.rb', line 42

def person_id
  @person_id
end

#taxon_name_idObject

Returns the value of attribute taxon_name_id.



43
44
45
# File 'lib/queries/dwc_occurrence/filter.rb', line 43

def taxon_name_id
  @taxon_name_id
end

Instance Method Details

#all_collected_byObject



131
132
133
134
135
136
137
138
# File 'lib/queries/dwc_occurrence/filter.rb', line 131

def all_collected_by
  ::DwcOccurrence
    .joins("JOIN collection_objects co on co.id = dwc_occurrences.dwc_occurrence_object_id AND dwc_occurrences.dwc_occurrence_object_type = 'CollectionObject'")
    .joins('JOIN collecting_events ce on co.collecting_event_id = ce.id')
    .joins("JOIN roles r on r.role_object_id = ce.id AND r.role_object_type = 'CollectingEvent' AND r.type = 'Collector'")
    .where(r: {person_id:})
    .distinct
end

#all_determined_byObject



112
113
114
115
116
117
118
119
# File 'lib/queries/dwc_occurrence/filter.rb', line 112

def all_determined_by
  ::DwcOccurrence
    .joins("JOIN collection_objects co on co.id = dwc_occurrences.dwc_occurrence_object_id AND dwc_occurrences.dwc_occurrence_object_type = 'CollectionObject'")
    .joins("JOIN taxon_determinations td on td.taxon_determination_object_id = co.id AND td.taxon_determination_object_type = 'CollectionObject'")
    .joins("JOIN roles r on r.role_object_id = td.id AND r.role_object_type = 'TaxonDetermination' AND r.type = 'Determiner'")
    .where(r: {person_id:})
    .distinct
end

#all_georeferenced_byObject



121
122
123
124
125
126
127
128
129
# File 'lib/queries/dwc_occurrence/filter.rb', line 121

def all_georeferenced_by
  ::DwcOccurrence
    .joins("JOIN collection_objects co on co.id = dwc_occurrences.dwc_occurrence_object_id AND dwc_occurrences.dwc_occurrence_object_type = 'CollectionObject'")
    .joins('JOIN collecting_events ce on co.collecting_event_id = ce.id')
    .joins('JOIN georeferences g on g.collecting_event_id = ce.id')
    .joins("JOIN roles r on r.role_object_id = g.id AND r.role_object_type = 'Georeference' AND r.type = 'Georeferencer'")
    .where(r: {person_id:})
    .distinct
end

#and_clausesObject



210
211
212
213
214
215
# File 'lib/queries/dwc_occurrence/filter.rb', line 210

def and_clauses
  [ dwc_occurrence_id_facet,
    dwc_occurrence_object_id_facet,
    dwc_occurrence_object_type_facet,
  ]
end

#asserted_distribution_query_facetObject



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/queries/dwc_occurrence/filter.rb', line 164

def asserted_distribution_query_facet
  return nil if asserted_distribution_query.nil?
  s = 'WITH query_ad_dwco AS (' + asserted_distribution_query.all.unscope(:select).select(:id).to_sql + ') ' +
    ::DwcOccurrence
    .select(:id, :dwc_occurrence_object_type, :dwc_occurrence_object_id)
    .joins("JOIN query_ad_dwco as query_ad_dwco1 on dwc_occurrences.dwc_occurrence_object_id = query_ad_dwco1.id
                AND dwc_occurrences.dwc_occurrence_object_type = 'AssertedDistribution'")
    .to_sql

  ::DwcOccurrence.from('(' + s + ') as dwc_occurrences').distinct
end

#collecting_event_query_facetObject



188
189
190
191
192
193
194
195
196
197
198
# File 'lib/queries/dwc_occurrence/filter.rb', line 188

def collecting_event_query_facet
  return nil if collecting_event_query.nil?
  s = 'WITH query_ce_dwco AS (' + collecting_event_query.all.unscope(:select).select(:id).to_sql + ') ' +
    ::DwcOccurrence
    .select(:id, :dwc_occurrence_object_type, :dwc_occurrence_object_id)
    .joins("JOIN collection_objects co on co.id = dwc_occurrences.dwc_occurrence_object_id AND dwc_occurrences.dwc_occurrence_object_type = 'CollectionObject'")
    .joins('JOIN query_ce_dwco as query_ce_dwco1 on co.collecting_event_id = query_ce_dwco1.id')
    .to_sql

  ::DwcOccurrence.from('(' + s + ') as dwc_occurrences').distinct
end

#collection_object_query_facetObject



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/queries/dwc_occurrence/filter.rb', line 176

def collection_object_query_facet
  return nil if collection_object_query.nil?
  s = 'WITH query_co_dwco AS (' + collection_object_query.all.unscope(:select).select(:id).to_sql + ') ' +
    ::DwcOccurrence
    .select(:id, :dwc_occurrence_object_type, :dwc_occurrence_object_id)
    .joins("JOIN query_co_dwco as query_co_dwco1 on dwc_occurrences.dwc_occurrence_object_id = query_co_dwco1.id
                AND dwc_occurrences.dwc_occurrence_object_type = 'CollectionObject'")
    .to_sql

  ::DwcOccurrence.from('(' + s + ') as dwc_occurrences').distinct
end

#dwc_occurrence_id_facetObject



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

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

#dwc_occurrence_object_id_facetObject



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

def dwc_occurrence_object_id_facet
  return nil if dwc_occurrence_object_id.empty?
  table[:dwc_occurrence_object_id].in(dwc_occurrence_object_id)
end

#dwc_occurrence_object_type_facetObject



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

def dwc_occurrence_object_type_facet
  return nil if dwc_occurrence_object_type.empty?
  table[:dwc_occurrence_object_type].in(dwc_occurrence_object_type)
end

#merge_clausesObject



200
201
202
203
204
205
206
207
208
# File 'lib/queries/dwc_occurrence/filter.rb', line 200

def merge_clauses
  [
    asserted_distribution_query_facet,
    collecting_event_query_facet,
    collection_object_query_facet,
    person_id_facet,
    taxon_name_id_facet
  ]
end

#person_id_facetObject

TODO: these should be referenced through base queries not logic here i.e. ::Queries::CollectionObject::Filter…



101
102
103
104
105
106
107
108
109
110
# File 'lib/queries/dwc_occurrence/filter.rb', line 101

def person_id_facet
  return nil if person_id.empty?

  ::Queries.union(
    ::DwcOccurrence, [
      all_determined_by,
      all_georeferenced_by,
      all_collected_by
    ])
end

#taxon_name_id_facetObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/queries/dwc_occurrence/filter.rb', line 140

def taxon_name_id_facet
  return nil if taxon_name_id.empty?

  a = ::Queries::DwcOccurrence::Filter.new(
    asserted_distribution_query: {
      taxon_name_query: {
        taxon_name_id:,
        descendants: false, # include self
        synonymify: true } }
  ).all

  b = ::Queries::DwcOccurrence::Filter.new(
    collection_object_query: {
      taxon_name_query: {
        taxon_name_id:,
        descendants: false, # include self
        synonymify: true } }
  ).all

  ::Queries.union(
    ::DwcOccurrence, [ a,b ]
  )
end