Class: Queries::Image::Filter

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

Constant Summary collapse

PARAMS =
[
  :biocuration_class_id,
  :collection_object_id,
  :collection_object_scope,
  :copyright_after_year,
  :copyright_holder_id,
  :copyright_holder_id_all,
  :copyright_holder_organization_id,
  :copyright_prior_to_year,
  :copyright_year,
  :creator_id,
  :creator_id_all,
  :depiction_caption,
  :depiction_caption_exact,
  :depiction_object_type,
  :depictions,
  :editor_id,
  :editor_id_all,
  :field_occurrence_id,
  :field_occurrence_scope,
  :freeform_svg,
  :image_id,
  :license,
  :owner_id,
  :owner_id_all,
  :owner_organization_id,
  :otu_id,
  :otu_scope,
  :sled_image,
  :sled_image_id,
  :source_id,
  :sqed_image,
  :taxon_name_id,
  :taxon_name_id_target,
  :type_material_depictions,

  biocuration_class_id: [],
  collection_object_id: [],
  collection_object_scope: [],
  copyright_holder_id: [],
  copyright_holder_organization_id: [],
  creator_id: [],
  depiction_object_type: [],
  editor_id: [],
  field_occurrence_id: [],
  field_occurrence_scope: [],
  image_id: [],
  license: [],
  otu_id: [],
  otu_scope: [],
  owner_id: [],
  owner_organization_id: [],
  sled_image_id: [],
  source_id: [],
  taxon_name_id: [],
].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, #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, #and_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.

Parameters:

  • params (Hash)


265
266
267
268
269
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
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/queries/image/filter.rb', line 265

def initialize(query_params)
  super

  @biocuration_class_id = params[:biocuration_class_id]
  @collection_object_id = params[:collection_object_id]
  @collection_object_scope = params[:collection_object_scope]
  @copyright_after_year = params[:copyright_after_year]
  @copyright_holder_id = params[:copyright_holder_id]
  @copyright_holder_id_all = boolean_param(params, :copyright_holder_id_all)
  @copyright_holder_organization_id =
    params[:copyright_holder_organization_id]
  @copyright_prior_to_year = params[:copyright_prior_to_year]
  @copyright_year = params[:copyright_year]
  @creator_id = params[:creator_id]
  @creator_id_all = boolean_param(params, :creator_id_all)
  @depiction_caption = params[:depiction_caption]
  @depiction_caption_exact = boolean_param(params, :depiction_caption_exact)
  @depiction_object_type = params[:depiction_object_type]
  @depictions = boolean_param(params, :depictions)
  @editor_id = params[:editor_id]
  @editor_id_all = boolean_param(params, :editor_id_all)
  @field_occurrence_id = params[:field_occurrence_id]
  @field_occurrence_scope = params[:field_occurrence_scope]
  @freeform_svg = boolean_param(params, :freeform_svg)
  @image_id = params[:image_id]
  @license = params[:license]
  @otu_id = params[:otu_id]
  @otu_scope = params[:otu_scope]
  @owner_id = params[:owner_id]
  @owner_id_all = boolean_param(params, :owner_id_all)
  @owner_organization_id = params[:owner_organization_id]
  @sled_image = boolean_param(params, :sled_image)
  @sled_image_id = params[:sled_image_id]
  @source_id = params[:source_id]
  @sqed_depiction_id = params[:sqed_depiction_id]
  @sqed_image = boolean_param(params, :sqed_image)
  @taxon_name_id = params[:taxon_name_id]
  @taxon_name_id_target = params[:taxon_name_id_target]
  @type_material_depictions = boolean_param(params, :type_material_depictions)

  set_citations_params(params)
  set_tags_params(params)
end

Instance Attribute Details

#biocuration_class_idArray

Returns of biocuration_class ids.

Returns:

  • (Array)

    of biocuration_class ids



143
144
145
# File 'lib/queries/image/filter.rb', line 143

def biocuration_class_id
  @biocuration_class_id
end

#collection_object_idArray

Returns images depicting collecting_object.

Returns:

  • (Array)

    images depicting collecting_object



66
67
68
# File 'lib/queries/image/filter.rb', line 66

def collection_object_id
  @collection_object_id
end

#collection_object_scopeObject

Parameters:

  • collection_object_scope

    One or more of:

    :all (default, includes all below)
    
    :collection_objects (those on the collection_object)
    :observations (those on just the CollectionObject observations)
    :collecting_events (those on the associated collecting event)
    


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

def collection_object_scope
  @collection_object_scope
end

Parameters:

  • copyright_after_year (Number)

    Images match if their copyright year is > copyright_after_year

Returns:

  • (Number)


241
242
243
# File 'lib/queries/image/filter.rb', line 241

def copyright_after_year
  @copyright_after_year
end

Parameters:

  • copyright_holder_id (Array or Person#id)

    one or more people id

Returns:

  • (Array)


218
219
220
# File 'lib/queries/image/filter.rb', line 218

def copyright_holder_id
  @copyright_holder_id
end

Parameters:

  • copryight_holder_id_all (String, nil)

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

Returns:

  • (Boolean)


229
230
231
# File 'lib/queries/image/filter.rb', line 229

def copyright_holder_id_all
  @copyright_holder_id_all
end

Parameters:

  • copyright_holder_organization_id (Array or Organization#id)

    one or more organization ids

Returns:

  • (Array)


223
224
225
# File 'lib/queries/image/filter.rb', line 223

def copyright_holder_organization_id
  @copyright_holder_organization_id
end

Parameters:

  • copyright_prior_to_year (Number)

    Images match if their copyright year is < copyright_prior_to_year

Returns:

  • (Number)


246
247
248
# File 'lib/queries/image/filter.rb', line 246

def copyright_prior_to_year
  @copyright_prior_to_year
end

Parameters:

  • copyright_year (Number)

    Images match if their copyright year == copyright_year

Returns:

  • (Number)


251
252
253
# File 'lib/queries/image/filter.rb', line 251

def copyright_year
  @copyright_year
end

#creator_idArray

Parameters:

  • creator_id (Array or Person#id)

    one or more people id

Returns:

  • (Array)


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

def creator_id
  @creator_id
end

#creator_id_allBoolean

Parameters:

  • creator_id_all (String, nil)

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

Returns:

  • (Boolean)


181
182
183
# File 'lib/queries/image/filter.rb', line 181

def creator_id_all
  @creator_id_all
end

#depiction_captionString

Parameters:

  • depiction_caption (String)

    Images match if they have a depiction whose caption matches depiction_caption.

Returns:

  • (String)


257
258
259
# File 'lib/queries/image/filter.rb', line 257

def depiction_caption
  @depiction_caption
end

#depiction_caption_exactBoolean

Parameters:

  • depiction_caption_exact (Boolean)

    depiction_caption must match exactly if true.

Returns:

  • (Boolean)


262
263
264
# File 'lib/queries/image/filter.rb', line 262

def depiction_caption_exact
  @depiction_caption_exact
end

#depiction_object_typeArray

Restricts Images to those that depict these classes

Parameters:

  • depiction_object_type

    one or more names of classes.

Returns:

  • (Array)


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

def depiction_object_type
  @depiction_object_type
end

#depictionsBoolean?

Returns true - image is used (in a depiction) false - image is not used nil - either.

Returns:

  • (Boolean, nil)

    true - image is used (in a depiction) false - image is not used nil - either



81
82
83
# File 'lib/queries/image/filter.rb', line 81

def depictions
  @depictions
end

#editor_idArray

Parameters:

  • editor_id (Array or Person#id)

    one or more people id

Returns:

  • (Array)


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

def editor_id
  @editor_id
end

#editor_id_allBoolean

Parameters:

  • editor_id_all (String, nil)

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

Returns:

  • (Boolean)


192
193
194
# File 'lib/queries/image/filter.rb', line 192

def editor_id_all
  @editor_id_all
end

#field_occurrence_idArray

Returns images depicting field_occurrence.

Returns:

  • (Array)

    images depicting field_occurrence



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

def field_occurrence_id
  @field_occurrence_id
end

#field_occurrence_scopeObject

Parameters:

  • field_occurrence_scope

    One or more of:

    :all (default, includes all below)
    
    :field_occurrence (those on the field_occurrence)
    :observations (those on just the FieldOccurrence observations)
    :collecting_events (those on the associated collecting event)
    


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

def field_occurrence_scope
  @field_occurrence_scope
end

#freeform_svgBoolean?

Returns true - image is used (in a depiction) that has svg false - is not a true image nil - ignored.

Returns:

  • (Boolean, nil)

    true - image is used (in a depiction) that has svg false - is not a true image nil - ignored



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

def freeform_svg
  @freeform_svg
end

#image_idArray

Returns:

  • (Array)


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

def image_id
  @image_id
end

#licenseArray

Parameters:

  • license (Array or String)

    string keys of the CREATIVE_COMMONS_LICENSES list

Returns:

  • (Array)


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

def license
  @license
end

#otu_idArray

Returns images depicting otus.

Returns:

  • (Array)

    images depicting otus



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

def otu_id
  @otu_id
end

#otu_scopeArray

A sub scope of sorts. Purpose is to gather all images possible under an OTU that are of an OTU, CollectionObject or Observation.

!! Must be used with an otu_id !!

Parameters:

  • otu_scope

    One or more of:

    :all (default, includes all below)
    
    :otu (those on the OTU)
    :otu_observations (those on just the OTU)
    :collection_object_observations (those on just those determined as the OTU)
    :collection_objects (those on just those on the collection objects)
    :field_occurrences (those on just field occurrences)
    :type_material (those on CollectionObjects that have TaxonName used in OTU)
    :type_material_observations (those on CollectionObjects that have TaxonName used in OTU)
    
    :coordinate_otus  If present adds both.  Use downstream substraction to to diffs of with/out?
    

Returns:

  • (Array)


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

def otu_scope
  @otu_scope
end

#owner_idArray

Parameters:

  • owner_id (Array or Person#id)

    one or more people id

Returns:

  • (Array)


202
203
204
# File 'lib/queries/image/filter.rb', line 202

def owner_id
  @owner_id
end

#owner_id_allBoolean

Parameters:

  • owner_id_all (String, nil)

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

Returns:

  • (Boolean)


213
214
215
# File 'lib/queries/image/filter.rb', line 213

def owner_id_all
  @owner_id_all
end

#owner_organization_idArray

Parameters:

  • owner_organization_id (Array or Organization#id)

    one or more organization ids

Returns:

  • (Array)


207
208
209
# File 'lib/queries/image/filter.rb', line 207

def owner_organization_id
  @owner_organization_id
end

#sled_imageBoolean

Returns true - yes false - is not nil - ignored.

Returns:

  • (Boolean)

    true - yes false - is not nil - ignored



152
153
154
# File 'lib/queries/image/filter.rb', line 152

def sled_image
  @sled_image
end

#sled_image_idArray

Returns:

  • (Array)


146
147
148
# File 'lib/queries/image/filter.rb', line 146

def sled_image_id
  @sled_image_id
end

#source_idArray

Parameters:

  • source_id (Array or Source#id)

    One or more source_ids cited by matching images. Note that a given image may only have one citation, so the ‘all’ option doesn’t exist here.

Returns:

  • (Array)


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

def source_id
  @source_id
end

#sqed_depiction_idArray

Returns:

  • (Array)


155
156
157
# File 'lib/queries/image/filter.rb', line 155

def sqed_depiction_id
  @sqed_depiction_id
end

#sqed_imageBoolean

Returns true - yes false - is not nil - ignored.

Returns:

  • (Boolean)

    true - yes false - is not nil - ignored



161
162
163
# File 'lib/queries/image/filter.rb', line 161

def sqed_image
  @sqed_image
end

#taxon_name_idProtonym.id?

Return all images depicting an Otu that is self or descendant linked to this TaxonName

Returns:

  • (Protonym.id, nil)

    return all images depicting an Otu that is self or descendant linked to this TaxonName



115
116
117
# File 'lib/queries/image/filter.rb', line 115

def taxon_name_id
  @taxon_name_id
end

#taxon_name_id_targetArray

Only used when ‘taxon_name_id` provided

Returns:

  • (Array)

    one or all of ‘Otu’, ‘CollectionObject’, ‘FieldOccurrence’, defaults to all if nothing provided



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

def taxon_name_id_target
  @taxon_name_id_target
end

#type_material_depictionsArray

Returns depicts some that is a type specimen.

Returns:

  • (Array)

    depicts some that is a type specimen



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

def type_material_depictions
  @type_material_depictions
end

Instance Method Details

#biocuration_facetObject



425
426
427
428
429
430
431
# File 'lib/queries/image/filter.rb', line 425

def biocuration_facet
  return nil if biocuration_class_id.empty?
  ::Image.joins(collection_objects: [:depictions]).merge(
    ::CollectionObject::BiologicalCollectionObject.joins(:biocuration_classifications)
    .where(biocuration_classifications: {biocuration_class_id:})
  )
end

#build_depiction_facet(kind, ids) ⇒ Object



778
779
780
781
# File 'lib/queries/image/filter.rb', line 778

def build_depiction_facet(kind, ids)
  return nil if ids.empty?
  ::Image.joins(:depictions).where(depictions: {depiction_object_id: ids, depiction_object_type: kind})
end

#collection_object_scope_facetObject



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

def collection_object_scope_facet
  return nil if collection_object_id.empty?

  scope_facet_for(
    :collection_objects, collection_object_id, collection_object_scope
  )
end

#collection_object_tableArel::Table

Returns:

  • (Arel::Table)


406
407
408
# File 'lib/queries/image/filter.rb', line 406

def collection_object_table
  ::CollectionObject.arel_table
end


632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'lib/queries/image/filter.rb', line 632

def copyright_holder_facet
  return nil if copyright_holder_id.empty? &&
    copyright_holder_organization_id.empty?

  q = ::Image
    .joins(attribution: :roles)
    .where(roles: {type: 'AttributionCopyrightHolder'})
    .where('roles.person_id IN (?) OR roles.organization_id IN (?)',
      copyright_holder_id, copyright_holder_organization_id
    )

  if copyright_holder_id_all
    q
      .group(:id)
      .having("count(images.id) = #{copyright_holder_id.length + copyright_holder_organization_id.length}")
  else
    q.distinct
  end
end


664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
# File 'lib/queries/image/filter.rb', line 664

def copyright_year_facet
  return nil if copyright_year.nil? && copyright_after_year.nil? &&
    copyright_prior_to_year.nil?

  if copyright_year
    a = ::Image.joins(:attribution).where(attribution: { copyright_year: })
    return a
  end

  t = ::Attribution.arel_table
  if copyright_after_year && copyright_prior_to_year.nil?
    y = t[:copyright_year].gt(copyright_after_year)
  elsif copyright_after_year.nil? && copyright_prior_to_year
    y = t[:copyright_year].lt(copyright_prior_to_year)
  else
    y = t[:copyright_year].gt(copyright_after_year).and(
        t[:copyright_year].lt(copyright_prior_to_year)
      )
  end

  ::Image.joins(:attribution).where(y)
end

#creator_facetObject



579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# File 'lib/queries/image/filter.rb', line 579

def creator_facet
  return nil if creator_id.empty?

  q = ::Image
    .joins(attribution: :roles)
    .where(roles: {type: 'AttributionCreator'})
    .where(roles: {person_id: creator_id})

  if creator_id_all
    q
      .group(:id)
      .having("count(images.id) = #{creator_id.length}")
  else
    q.distinct
  end
end

#depiction_caption_facetObject



687
688
689
690
691
692
693
694
695
696
697
698
699
700
# File 'lib/queries/image/filter.rb', line 687

def depiction_caption_facet
  return nil if depiction_caption.nil?

  if depiction_caption_exact
    ::Image
      .joins(:depictions)
      .where('depictions.caption = ?', depiction_caption)
  else
    caption = "%#{depiction_caption.strip.gsub(/\s+/, '%')}%"
    ::Image
      .joins(:depictions)
      .where('depictions.caption ILIKE ?', caption)
  end
end

#depiction_object_type_facetObject



433
434
435
436
# File 'lib/queries/image/filter.rb', line 433

def depiction_object_type_facet
  return nil if depiction_object_type.empty?
  ::Image.joins(:depictions).where(depictions: {depiction_object_type:})
end

#depiction_tableArel::Table

Returns:

  • (Arel::Table)


421
422
423
# File 'lib/queries/image/filter.rb', line 421

def depiction_table
  ::Depiction.arel_table
end

#depictions_facetObject



438
439
440
441
442
443
444
445
# File 'lib/queries/image/filter.rb', line 438

def depictions_facet
  return nil if depictions.nil?
  if depictions
    ::Image.joins(:depictions)
  else
    ::Image.where.missing(:depictions)
  end
end

#editor_facetObject



596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'lib/queries/image/filter.rb', line 596

def editor_facet
  return nil if editor_id.empty?

  q = ::Image
    .joins(attribution: :roles)
    .where(roles: {type: 'AttributionEditor'})
    .where(roles: {person_id: editor_id})

  if editor_id_all
    q
      .group(:id)
      .having("count(images.id) = #{editor_id.length}")
  else
    q.distinct
  end
end

#field_occurrence_scope_facetObject



541
542
543
544
545
546
547
# File 'lib/queries/image/filter.rb', line 541

def field_occurrence_scope_facet
  return nil if field_occurrence_id.empty?

  scope_facet_for(
    :field_occurrences, field_occurrence_id, field_occurrence_scope
  )
end

#field_occurrence_tableArel::Table

Returns:

  • (Arel::Table)


411
412
413
# File 'lib/queries/image/filter.rb', line 411

def field_occurrence_table
  ::FieldOccurrence.arel_table
end

#freeform_svg_facetObject



447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/queries/image/filter.rb', line 447

def freeform_svg_facet
  return nil if freeform_svg.nil?

  q = ::Image.left_joins(depictions: [:sled_image, :sqed_depiction])
    .where.missing(:sled_image)
    .where.not(depictions: {svg_clip: nil}).distinct

  if freeform_svg
    q
  else
    ::Queries.except(::Image.where(project_id:), q)
  end
end

#images_on(t, t_id) ⇒ Object



702
703
704
# File 'lib/queries/image/filter.rb', line 702

def images_on(t, t_id)
  ::Image.joins(t).where(**{t => {id: t_id}})
end

#images_on_collecting_events_via(t, t_id) ⇒ Object



714
715
716
717
718
# File 'lib/queries/image/filter.rb', line 714

def images_on_collecting_events_via(t, t_id)
  ::Image
    .joins(collecting_events: t)
    .where("#{t}.id IN (?)", t_id)
end

#images_on_observations_linked_to(t, t_id) ⇒ Object



706
707
708
709
710
711
712
# File 'lib/queries/image/filter.rb', line 706

def images_on_observations_linked_to(t, t_id)
  ::Image.joins(:observations)
    .where(observations: {
      observation_object_type: t.to_s.classify,
      observation_object_id: t_id
    })
end

#license_facetObject



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

def license_facet
  return nil if license.empty?

  ::Image.joins(:attribution).where(attribution: { license: }).distinct
end

#merge_clausesObject



876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
# File 'lib/queries/image/filter.rb', line 876

def merge_clauses
  s = ::Queries::Query::Filter::SUBQUERIES.select{|k,v| v.include?(:image)}.keys.map(&:to_s) - ['source']
  [
    *s.collect{|m| query_facets_facet(m)}, # Reference all the Image referencing SUBQUERIES
    biocuration_facet,
    collection_object_scope_facet,
    copyright_holder_facet,
    copyright_year_facet,
    creator_facet,
    editor_facet,
    depiction_caption_facet,
    depiction_object_type_facet,
    depictions_facet,
    field_occurrence_scope_facet,
    freeform_svg_facet,
    license_facet,
    otu_id_facet,
    otu_scope_facet,
    owner_facet,
    sled_image_facet,
    sled_image_id_facet,
    source_facet,
    sqed_image_facet,
    sqed_depiction_id_facet,
    sqed_image_facet,
    taxon_name_id_facet,
    type_material_depictions_facet,
  ]
end

#otu_facet_collection_object_observations(otu_ids) ⇒ Object



761
762
763
764
765
# File 'lib/queries/image/filter.rb', line 761

def otu_facet_collection_object_observations(otu_ids)
  ::Image.joins(:observations)
    .joins("INNER JOIN taxon_determinations on taxon_determinations.taxon_determination_object_id = observations.observation_object_id AND taxon_determinations.taxon_determination_object_type = 'CollectionObject'")
    .where(taxon_determinations: {otu_id: otu_ids}, observations: {observation_object_type: 'CollectionObject'})
end

#otu_facet_collection_objects(otu_ids) ⇒ Object



751
752
753
754
# File 'lib/queries/image/filter.rb', line 751

def otu_facet_collection_objects(otu_ids)
  ::Image.joins(collection_objects: [:taxon_determinations])
    .where(taxon_determinations: {otu_id: otu_ids})
end

#otu_facet_field_occurrences(otu_ids) ⇒ Object



756
757
758
759
# File 'lib/queries/image/filter.rb', line 756

def otu_facet_field_occurrences(otu_ids)
  ::Image.joins(field_occurrences: [:taxon_determinations])
    .where(taxon_determinations: {otu_id: otu_ids})
end

#otu_facet_otu_observations(otu_ids) ⇒ Object



767
768
769
770
# File 'lib/queries/image/filter.rb', line 767

def otu_facet_otu_observations(otu_ids)
  ::Image.joins(:observations)
    .where(observations: {observation_object_id: otu_ids, observation_object_type: 'Otu'})
end

#otu_facet_otus(otu_ids) ⇒ Object



747
748
749
# File 'lib/queries/image/filter.rb', line 747

def otu_facet_otus(otu_ids)
  ::Image.joins(:depictions).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



728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
# File 'lib/queries/image/filter.rb', line 728

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



  ::Image.joins(collection_objects: [type_materials: [:protonym]]).where(collection_objects: {type_materials: {protonym: protonyms}})
end

#otu_facet_type_material_observations(otu_ids) ⇒ Object



720
721
722
723
724
725
# File 'lib/queries/image/filter.rb', line 720

def otu_facet_type_material_observations(otu_ids)
  ::Image.joins(:observations)
    .joins("INNER JOIN type_materials on type_materials.collection_object_id = observations.observation_object_id AND observations.observation_object_type = 'CollectionObject'")
    .joins('INNER JOIN otus on otus.taxon_name_id = type_materials.protonym_id')
    .where(otus: {id: otu_ids})
end

#otu_id_facetObject



489
490
491
492
493
494
# File 'lib/queries/image/filter.rb', line 489

def otu_id_facet
  # only run this when scope not provided
  return nil if otu_id.empty? || !otu_scope.empty?

  ::Image.joins(:otus).where(otus: {id: otu_id})
end

#otu_scope_facetObject



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
522
523
524
525
526
527
528
529
530
531
# File 'lib/queries/image/filter.rb', line 496

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) # unused
    selected = [
      :otu_facet_otus,
      :otu_facet_collection_objects,
      :otu_facet_field_occurrences,
      :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_field_occurrences if otu_scope.include?(:field_occurrences)
    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

  q = selected.collect{|a| '(' + send(a, otu_ids).to_sql + ')'}.join(' UNION ')

  d = ::Image.from('(' + q + ')' + ' as images')
  d
end

#otu_tableArel::Table

Returns:

  • (Arel::Table)


401
402
403
# File 'lib/queries/image/filter.rb', line 401

def otu_table
  ::Otu.arel_table
end

#owner_facetObject



613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# File 'lib/queries/image/filter.rb', line 613

def owner_facet
  return nil if owner_id.empty? && owner_organization_id.empty?

  q = ::Image
    .joins(attribution: :roles)
    .where(roles: {type: 'AttributionOwner'})
    .where('roles.person_id IN (?) OR roles.organization_id IN (?)',
      owner_id, owner_organization_id
    )

  if owner_id_all
    q
      .group(:id)
      .having("count(images.id) = #{owner_id.length + owner_organization_id.length}")
  else
    q.distinct
  end
end

#query_facets_facet(name = nil) ⇒ Object



858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
# File 'lib/queries/image/filter.rb', line 858

def query_facets_facet(name = nil)
  return nil if name.nil?

  q = send((name + '_query').to_sym)

  return nil if q.nil?

  n = "query_#{name}_img"

  s = "WITH #{n} AS (" + q.all.to_sql + ') ' +
    ::Image
    .joins(:depictions)
    .joins("JOIN #{n} as #{n}1 on depictions.depiction_object_id = #{n}1.id AND depictions.depiction_object_type = '#{name.treetop_camelize}'")
    .to_sql

  ::Image.from('(' + s + ') as images').distinct
end

#scope_facet_for(t, t_id, t_scope) ⇒ Object



549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# File 'lib/queries/image/filter.rb', line 549

def scope_facet_for(t, t_id, t_scope)
  selected = []
  if t_scope.include?(:all) # unused
    selected = [
      images_on(t, t_id).to_sql,
      images_on_observations_linked_to(t, t_id).to_sql,
      images_on_collecting_events_via_observed(t, t_id).to_sql
    ]
  elsif t_scope.empty?
    selected =
      [images_on(t, t_id).to_sql]
  else
    selected.push(
      images_on(t, t_id).to_sql
    ) if t_scope.include?(t)

    selected.push(
      images_on_observations_linked_to(t, t_id).to_sql
    ) if t_scope.include?(:observations)

    selected.push(
      images_on_collecting_events_via(t, t_id).to_sql
    ) if t_scope.include?(:collecting_events)
  end

  q = selected.map{ |q| '(' + q + ')' }.join(' UNION ')

  ::Image.from('(' + q + ')' + ' as images')
end

#sled_image_facetObject



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

def sled_image_facet
  return nil if sled_image.nil?
  if sled_image
    ::Image.joins(:sled_image).distinct
  else
    ::Image.where.missing(:sled_image).distinct
  end
end

#sled_image_id_facetObject



479
480
481
482
# File 'lib/queries/image/filter.rb', line 479

def sled_image_id_facet
  return nil if sled_image_id.empty?
  ::Image.joins(:sled_image).where(sled_images: {id: sled_image_id})
end

#source_facetObject



652
653
654
655
656
# File 'lib/queries/image/filter.rb', line 652

def source_facet
  return nil if source_id.empty?

  ::Image.joins(:citations).where(citations: { source_id: })
end

#sqed_depiction_id_facetObject



484
485
486
487
# File 'lib/queries/image/filter.rb', line 484

def sqed_depiction_id_facet
  return nil if sqed_depiction_id.empty?
  ::Image.joins(depictions: [:sqed_depiction]).where(sqed_depictions: {id: sqed_depiction_id})
end

#sqed_image_facetObject



461
462
463
464
465
466
467
468
# File 'lib/queries/image/filter.rb', line 461

def sqed_image_facet
  return nil if sqed_image.nil?
  if sqed_image
    ::Image.joins(depictions: [:sqed_depiction]).distinct
  else
    ::Image.left_joins(depictions: [:sqed_depiction]).where(sqed_depiction: {id: nil}).distinct
  end
end

#taxon_determination_tableArel::Table

Returns:

  • (Arel::Table)


396
397
398
# File 'lib/queries/image/filter.rb', line 396

def taxon_determination_table
  ::TaxonDetermination.arel_table
end

#taxon_name_id_facetObject



783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
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
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
# File 'lib/queries/image/filter.rb', line 783

def taxon_name_id_facet
  #  Image -> Depictions -> Otu -> TaxonName -> Ancestors
  return nil if taxon_name_id.empty?

  h = Arel::Table.new(:taxon_name_hierarchies)
  t = ::TaxonName.arel_table

  j1, j2, j3, q1, q2, q3 = nil, nil, nil, nil, nil, nil

  if taxon_name_id_target.include?('Otu')
    a = otu_table.alias('oj1')
    b = t.alias('tj1')
    h_alias = h.alias('th1')

    j1 = table
      .join(depiction_table, Arel::Nodes::InnerJoin).on(table[:id].eq(depiction_table[:image_id]))
      .join(a, Arel::Nodes::InnerJoin).on( depiction_table[:depiction_object_id].eq(a[:id]).and( depiction_table[:depiction_object_type].eq('Otu') ))
      .join(b, Arel::Nodes::InnerJoin).on( a[:taxon_name_id].eq(b[:id]))
      .join(h_alias, Arel::Nodes::InnerJoin).on(b[:id].eq(h_alias[:descendant_id]))

    z = h_alias[:ancestor_id].in(taxon_name_id)
    q1 = ::Image.joins(j1.join_sources).where(z)
  end

  if taxon_name_id_target.include?('CollectionObject')
    a = otu_table.alias('oj2')
    b = t.alias('tj2')
    h_alias = h.alias('th2')

    j2 = table
      .join(depiction_table, Arel::Nodes::InnerJoin).on(table[:id].eq(depiction_table[:image_id]))
      .join(collection_object_table, Arel::Nodes::InnerJoin).on( depiction_table[:depiction_object_id].eq(collection_object_table[:id]).and( depiction_table[:depiction_object_type].eq('CollectionObject') ))
      .join(taxon_determination_table, Arel::Nodes::InnerJoin).on(
        collection_object_table[:id].eq(taxon_determination_table[:taxon_determination_object_id])
      .and(taxon_determination_table[:taxon_determination_object_type].eq('CollectionObject'))
      )
        .join(a, Arel::Nodes::InnerJoin).on(  taxon_determination_table[:otu_id].eq(a[:id]) )
        .join(b, Arel::Nodes::InnerJoin).on( a[:taxon_name_id].eq(b[:id]))
        .join(h_alias, Arel::Nodes::InnerJoin).on(b[:id].eq(h_alias[:descendant_id]))

      z = h_alias[:ancestor_id].in(taxon_name_id)
      q2 = ::Image.joins(j2.join_sources).where(z)
  end

  if taxon_name_id_target.include?('FieldOccurrence')
    a = otu_table.alias('oj3')
    b = t.alias('tj3')
    h_alias = h.alias('th3')

    j3 = table
      .join(depiction_table, Arel::Nodes::InnerJoin).on(table[:id].eq(depiction_table[:image_id]))
      .join(field_occurrence_table, Arel::Nodes::InnerJoin).on( depiction_table[:depiction_object_id].eq(field_occurrence_table[:id]).and( depiction_table[:depiction_object_type].eq('FieldOccurrence') ))
      .join(taxon_determination_table, Arel::Nodes::InnerJoin).on(
        field_occurrence_table[:id].eq(taxon_determination_table[:taxon_determination_object_id])
      .and(taxon_determination_table[:taxon_determination_object_type].eq('FieldOccurrence'))
      )
        .join(a, Arel::Nodes::InnerJoin).on(  taxon_determination_table[:otu_id].eq(a[:id]) )
        .join(b, Arel::Nodes::InnerJoin).on( a[:taxon_name_id].eq(b[:id]))
        .join(h_alias, Arel::Nodes::InnerJoin).on(b[:id].eq(h_alias[:descendant_id]))

      z = h_alias[:ancestor_id].in(taxon_name_id)
      q3 = ::Image.joins(j3.join_sources).where(z)
  end

  if q1 && q2 && q3
    ::Image.from("((#{q1.to_sql}) UNION (#{q2.to_sql}) UNION (#{q3.to_sql})) AS images")
  elsif q1
    q1.distinct
  elsif q2
    q2.distinct
  else
    q3.distinct
  end
end

#type_material_depictions_facetScope

Returns:

  • (Scope)


773
774
775
776
# File 'lib/queries/image/filter.rb', line 773

def type_material_depictions_facet
  return nil if type_material_depictions.nil?
  ::Image.joins(collection_objects: [:type_materials])
end

#type_materials_tableArel::Table

Returns:

  • (Arel::Table)


416
417
418
# File 'lib/queries/image/filter.rb', line 416

def type_materials_table
  ::TypeMaterial.arel_table
end