Class: Queries::Depiction::Filter

Inherits:
Query::Filter show all
Includes:
Concerns::Tags
Defined in:
lib/queries/depiction/filter.rb

Constant Summary collapse

DISTINCT_ATTRIBUTES =

We can’t use distinct in SQL on XML column types, this may come back to bite us if we try to use distinct Depictions + overlays

(::Depiction.column_names - ['svg_clip']).map(&:to_sym).freeze
PARAMS =
[
  :depiction_id,
  :depiction_object_id,
  :depiction_object_type,
  :image_id,
  :otu_id,
  depiction_id: [],
  depiction_object_id: [],
  depiction_object_type: [],
  image_id: [],
  otu_id: [],
  otu_scope: [], # ripped from Image filter
].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 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.

Parameters:

  • params (Hash)


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/queries/depiction/filter.rb', line 40

def initialize(query_params)
  super

  @depiction_id = params[:depiction_id]
  @depiction_object_id = params[:depiction_object_id]
  @depiction_object_type = params[:depiction_object_type]
  @image_id = params[:image_id]
  @otu_id = params[:otu_id]
  @otu_scope = params[:otu_scope]

  set_tags_params(params)
end

Instance Attribute Details

#depiction_idObject

Returns the value of attribute depiction_id.



25
26
27
# File 'lib/queries/depiction/filter.rb', line 25

def depiction_id
  @depiction_id
end

#depiction_object_idObject

Returns the value of attribute depiction_object_id.



26
27
28
# File 'lib/queries/depiction/filter.rb', line 26

def depiction_object_id
  @depiction_object_id
end

#depiction_object_typeObject

Returns the value of attribute depiction_object_type.



27
28
29
# File 'lib/queries/depiction/filter.rb', line 27

def depiction_object_type
  @depiction_object_type
end

#image_idObject

Returns the value of attribute image_id.



28
29
30
# File 'lib/queries/depiction/filter.rb', line 28

def image_id
  @image_id
end

#otu_idObject

Returns the value of attribute otu_id.



29
30
31
# File 'lib/queries/depiction/filter.rb', line 29

def otu_id
  @otu_id
end

#otu_scopeObject

Returns the value of attribute otu_scope.



30
31
32
# File 'lib/queries/depiction/filter.rb', line 30

def otu_scope
  @otu_scope
end

Class Method Details

.base_queryObject

We can’t .distinct on xml fields If you need to return the svg_clip attribute then you must ‘all.unscope(:select).select(’*‘)`



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

def self.base_query
  ::Depiction.select(DISTINCT_ATTRIBUTES)
end

Instance Method Details

#and_clausesObject



216
217
218
219
220
221
222
223
# File 'lib/queries/depiction/filter.rb', line 216

def and_clauses
  [
    depiction_id_facet,
    depiction_object_id_facet,
    depiction_object_type_facet,
    image_id_facet
  ]
end

#depiction_id_facetObject



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

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

#depiction_object_id_facetObject



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

def depiction_object_id_facet
  return nil if depiction_object_id.empty?
  table[:depiction_object_id].in(depiction_object_id)
end

#depiction_object_type_facetObject



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

def depiction_object_type_facet
  return nil if depiction_object_type.empty?
  table[:depiction_object_type].in(depiction_object_type)
end

#image_id_facetObject



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

def image_id_facet
  return nil if image_id.empty?
  table[:image_id].in(image_id)
end

#image_query_facetObject



97
98
99
100
101
102
103
# File 'lib/queries/depiction/filter.rb', line 97

def image_query_facet
  return nil if image_query.nil?
  ::Depiction.with(image_query: image_query.all )
    .joins('JOIN image_query as image_query1 on image_query1.id = depictions.image_id')
    .select(DISTINCT_ATTRIBUTES)
    .distinct
end

#merge_clausesObject



208
209
210
211
212
213
214
# File 'lib/queries/depiction/filter.rb', line 208

def merge_clauses
  [
    image_query_facet,
    otu_facet,
    otu_scope_facet
  ].compact
end

#otu_facetObject



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

def otu_facet
  return nil if otu_id.empty? || !otu_scope.empty?
  otu_facet_otus(otu_id)
end

#otu_facet_collection_object_observations(otu_ids) ⇒ Object



189
190
191
192
193
194
195
196
# File 'lib/queries/depiction/filter.rb', line 189

def otu_facet_collection_object_observations(otu_ids)
  co = ::CollectionObject.joins(:taxon_determinations, :observations)
    .where(taxon_determinations: {otu_id: otu_ids})
    .select('observations.id as observation_id')

  ::Depiction.select(DISTINCT_ATTRIBUTES).with(co_query: co)
    .joins("JOIN co_query on co_query.observation_id = depiction_object_id and depiction_object_type = 'Observation'")
end

#otu_facet_collection_objects(otu_ids) ⇒ Object



180
181
182
183
184
185
186
187
# File 'lib/queries/depiction/filter.rb', line 180

def otu_facet_collection_objects(otu_ids)
  co = ::CollectionObject.joins(:taxon_determinations)
    .where(taxon_determinations: {otu_id: otu_ids})

  ::Depiction.select(DISTINCT_ATTRIBUTES)
    .with(co_query: co)
    .joins("JOIN co_query on co_query.id = depiction_object_id and depiction_object_type = 'CollectionObject'")
end

#otu_facet_otu_observations(otu_ids) ⇒ Object



198
199
200
201
202
203
204
205
206
# File 'lib/queries/depiction/filter.rb', line 198

def otu_facet_otu_observations(otu_ids)
  co = ::Otu.joins(:observations)
    .where(otus: {id: otu_ids})
    .select('observations.id as observation_id')

  ::Depiction.select(DISTINCT_ATTRIBUTES)
    .with(co_query: co)
    .joins("JOIN co_query on co_query.observation_id = depiction_object_id and depiction_object_type = 'Observation'")
end

#otu_facet_otus(otu_ids) ⇒ Object



176
177
178
# File 'lib/queries/depiction/filter.rb', line 176

def otu_facet_otus(otu_ids)
  ::Depiction.select(DISTINCT_ATTRIBUTES).where(depictions: {depiction_object_type: 'Otu', depiction_object_id: otu_ids})
end

#otu_facet_type_material(otu_ids) ⇒ Object

Find all TaxonNames, and their synonyms



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/queries/depiction/filter.rb', line 156

def otu_facet_type_material(otu_ids)
  # Double check that there are otu_ids,
  #  this check exists in calling methods, but re-inforce here.
  protonyms = if otu_ids.any?
                ::Queries::TaxonName::Filter.new(
                  otu_query: { otu_id: otu_ids},
                  synonymify: true,
                  project_id:
                ).all.where(type: 'Protonym')
              else
                TaxonName.none
              end

  co = ::CollectionObject.joins(type_materials: [:protonym]).where(collection_objects: {type_materials: {protonym: protonyms}})

  ::Depiction.select(DISTINCT_ATTRIBUTES)
    .with(co_query: co)
    .joins("JOIN co_query cq on cq.id = depiction_object_id AND depiction_object_type = 'CollectionObject'")
end

#otu_facet_type_material_observations(otu_ids) ⇒ Object



146
147
148
149
150
151
152
153
# File 'lib/queries/depiction/filter.rb', line 146

def otu_facet_type_material_observations(otu_ids)
  o = ::TypeMaterial.joins(protonym: [:otus], collection_object: [:observations])
    .where(otus: {id: otu_ids})
    .select('observations.id')

  ::Depiction.select(DISTINCT_ATTRIBUTES).with(obs: o)
    .joins("JOIN obs on depictions.depiction_object_type = 'Observation' AND depictions.depiction_object_id = obs.id")
end

#otu_scope_facetObject

Otu scope facet is identical in images but used here to return depictions specific to an OTU context



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/queries/depiction/filter.rb', line 114

def otu_scope_facet
  return nil if otu_id.empty? || otu_scope.empty?

  otu_ids = otu_id
  otu_ids += ::Otu.coordinate_otu_ids(otu_id) if otu_scope.include?(:coordinate_otus)
  otu_ids.uniq!

  selected = []

  if otu_scope.include?(:all)
    selected = [
      :otu_facet_otus,
      :otu_facet_collection_objects,
      :otu_facet_otu_observations,
      :otu_facet_collection_object_observations,
      :otu_facet_type_material,
      :otu_facet_type_material_observations
    ]
  elsif otu_scope.empty?
    selected = [:otu_facet_otus]
  else
    selected.push :otu_facet_otus if otu_scope.include?(:otus)
    selected.push :otu_facet_collection_objects if otu_scope.include?(:collection_objects)
    selected.push :otu_facet_collection_object_observations if otu_scope.include?(:collection_object_observations)
    selected.push :otu_facet_otu_observations if otu_scope.include?(:otu_observations)
    selected.push :otu_facet_type_material if otu_scope.include?(:type_material)
    selected.push :otu_facet_type_material_observations if otu_scope.include?(:type_material_observations)
  end

  ::Queries.union(::Depiction, selected.collect{|a| send(a, otu_ids) })
end