Class: Queries::BiologicalAssociationsGraph::Filter

Inherits:
Query::Filter show all
Includes:
Concerns::Citations, Concerns::DataAttributes, Concerns::Geo, Concerns::Notes, Concerns::Tags
Defined in:
lib/queries/biological_associations_graph/filter.rb

Constant Summary collapse

PARAMS =
[
  :biological_association_id,
  :biological_associations_graph_id,
  :biological_relationship_id,
  :geo_json,
  :geo_mode,
  :geo_shape_id,
  :geo_shape_type,
  :radius,
  :wkt,

  biological_association_id: [],
  biological_associations_graph_id: [],
  biological_relationship_id: [],
  geo_shape_id: [],
  geo_shape_type: [],
].freeze
API_PARAM_EXCLUSIONS =
[ ]

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, #conveyance_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, #roll_call, #sound_query, #taxon_name_query, #taxon_name_relationship_query, #venn, #venn_ignore_pagination, #venn_mode

Attributes inherited from Query

#query_string, #terms

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



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/queries/biological_associations_graph/filter.rb', line 52

def initialize(query_params)
  super

  @biological_association_id = params[:biological_association_id]
  @biological_associations_graph_id = params[:biological_associations_graph_id]
  @biological_relationship_id = params[:biological_relationship_id]

  set_data_attributes_params(params)
  set_citations_params(params)
  set_notes_params(params)
  set_tags_params(params)
  set_geo_params(params)
end

Instance Attribute Details

#biological_association_idArray

Returns one or more BiologicalAssociation#id.

Parameters:

  • biological_association_id (Array, Integer)

Returns:

  • (Array)

    one or more BiologicalAssociation#id



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

def biological_association_id
  @biological_association_id
end

#biological_associations_graph_idObject

Parameters:

  • biological_associations_graph_id

    These biological association graphs



33
34
35
# File 'lib/queries/biological_associations_graph/filter.rb', line 33

def biological_associations_graph_id
  @biological_associations_graph_id
end

#biological_relationship_idArray

See also exclude_taxon_name_relationship

Parameters:

  • biological_relationship_id (Array, Integer)

Returns:

  • (Array)

    one or more biological relationship ID



44
45
46
# File 'lib/queries/biological_associations_graph/filter.rb', line 44

def biological_relationship_id
  @biological_relationship_id
end

#geo_jsonObject

Returns the value of attribute geo_json.



47
48
49
# File 'lib/queries/biological_associations_graph/filter.rb', line 47

def geo_json
  @geo_json
end

#radiusObject

Integer in Meters

!! defaults to 100m


50
51
52
# File 'lib/queries/biological_associations_graph/filter.rb', line 50

def radius
  @radius
end

#wktObject

Returns the value of attribute wkt.



46
47
48
# File 'lib/queries/biological_associations_graph/filter.rb', line 46

def wkt
  @wkt
end

Instance Method Details

#and_clausesObject



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

def and_clauses
  [ biological_associations_graph_id_facet ]
end

#biological_association_id_facetObject



159
160
161
162
163
# File 'lib/queries/biological_associations_graph/filter.rb', line 159

def biological_association_id_facet
  return nil if biological_association_id.empty?
  ::BiologicalAssociationsGraph.joins(biological_associations_biological_associations_graphs: [:biological_association])
  .where(biological_associations_biological_associations_graphs: { biological_association_id: }).distinct
end

#biological_association_query_facetObject



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

def biological_association_query_facet
  return nil if biological_association_query.nil?
  s = 'WITH query_ba_bag AS (' + biological_association_query.all.to_sql + ') '

  s << ::BiologicalAssociationsGraph.joins(:biological_associations_biological_associations_graphs)
    .joins('JOIN query_ba_bag as query_ba_bag1 on biological_associations_biological_associations_graphs.biological_association_id = query_ba_bag1.id').to_sql

  ::BiologicalAssociation.from('(' + s + ') as biological_associations')
end

#biological_associations_graph_geo_facetObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/queries/biological_associations_graph/filter.rb', line 108

def biological_associations_graph_geo_facet
  return nil if geo_shape_id.empty? || geo_shape_type.empty? ||
    # TODO: this should raise an error(?)
    geo_shape_id.length != geo_shape_type.length
  return ::BiologicalAssociationsGraph.none if roll_call

  geographic_area_shapes, gazetteer_shapes = shapes_for_geo_mode

  a = biological_associations_graph_geo_facet_by_type(
    'GeographicArea', geographic_area_shapes
  )

  b = biological_associations_graph_geo_facet_by_type(
    'Gazetteer', gazetteer_shapes
  )

  if geo_mode == true # spatial
    i = ::Queries.union(::GeographicItem, [a,b])
    u = ::Queries::GeographicItem.st_union_text(i).to_a.first

    return from_wkt(u['st_astext'])
  end

  referenced_klass_union([a,b])
end

#biological_associations_graph_geo_facet_by_type(shape_string, shape_ids) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/queries/biological_associations_graph/filter.rb', line 134

def biological_associations_graph_geo_facet_by_type(shape_string, shape_ids)
  case geo_mode
  when nil, false # exact, descendants
    ::BiologicalAssociationsGraph
      .joins("JOIN asserted_distributions ON asserted_distributions.asserted_distribution_object_id = biological_associations_graphs.id AND asserted_distributions.asserted_distribution_object_type = 'BiologicalAssociationsGraph'")
      .where(asserted_distributions: {
        asserted_distribution_shape: shape_ids
     })
  when true # spatial
    m = shape_string.tableize
    b = ::GeographicItem.joins(m.to_sym).where(m => shape_ids)
  end
end

#biological_associations_graph_id_facetObject



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

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

#biological_relationship_id_facetObject



153
154
155
156
157
# File 'lib/queries/biological_associations_graph/filter.rb', line 153

def biological_relationship_id_facet
  return nil if biological_relationship_id.empty?
  ::BiologicalAssociationsGraph.joins(biological_associations_biological_associations_graphs: [:biological_association])
  .where(biological_associations: { biological_association_relationship_id: biological_relationship_id }).distinct
end

#from_wkt(wkt_shape) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/queries/biological_associations_graph/filter.rb', line 83

def from_wkt(wkt_shape)
  a = ::Queries::AssertedDistribution::Filter.new(
    wkt: wkt_shape, project_id:,
    asserted_distribution_object_type: 'BiologicalAssociationsGraph'
  )

  ::BiologicalAssociationsGraph
    .with(ad: a.all)
    .joins('JOIN ad ON ad.asserted_distribution_object_id = biological_associations_graphs.id')
end

#geo_json_facetObject



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/queries/biological_associations_graph/filter.rb', line 94

def geo_json_facet
  return nil if geo_json.blank?
  return ::BiologicalAssociationsGraph.none if roll_call

  a = ::Queries::AssertedDistribution::Filter.new(
    geo_json:, project_id:, radius:,
    asserted_distribution_object_type: 'BiologicalAssociationsGraph'
  )

  ::BiologicalAssociationsGraph
    .with(ad: a.all)
    .joins('JOIN ad ON ad.asserted_distribution_object_id = biological_associations_graphs.id')
end

#merge_clausesObject



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

def merge_clauses
  [
    biological_association_id_facet,
    biological_relationship_id_facet,
    wkt_facet,
    geo_json_facet,
    biological_associations_graph_geo_facet,
  ]
end

#wkt_facetObject



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

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