Class: DatasetRecord::DarwinCore::Occurrence::ImportProtonym

Inherits:
Object
  • Object
show all
Defined in:
app/models/dataset_record/darwin_core/occurrence.rb

Direct Known Subclasses

CreateIfNotExists, MatchExisting

Defined Under Namespace

Classes: CreateIfNotExists, MatchExisting

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_if_not_existsObject



27
28
29
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 27

def self.create_if_not_exists
  @create_if_not_exists ||= CreateIfNotExists.new
end

.match_existingObject



31
32
33
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 31

def self.match_existing
  @match_existing ||= MatchExisting.new
end

Instance Method Details

#execute(origins, parent, name) ⇒ Object



35
36
37
38
39
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 35

def execute(origins, parent, name)
  protonym = get_protonym(parent, name)
  raise DatasetRecord::DarwinCore::InvalidData.new(exception_args(origins, parent, name, protonym)) unless protonym&.persisted?
  protonym
end

#get_protonym(parent, name) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 41

def get_protonym(parent, name)
  name = name.except(:rank_class) if name[:rank_class].nil?

  %I(name masculine_name feminine_name neuter_name).inject(nil) do |protonym, field|
    break protonym unless protonym.nil?

    potential_protonyms = Protonym.where(name.slice(:rank_class).merge({ field => name[:name], :parent => parent }))

    # if multiple potential protonyms, this is a homonym situation
    if potential_protonyms.count > 1
      # verbatim author field (if present) applies to finest name only
      if name[:cached_author]
        potential_protonyms_narrowed = potential_protonyms.where(cached_author: name[:cached_author])

        if name[:year_of_publication]
          potential_protonyms_narrowed = potential_protonyms_narrowed.where(year_of_publication: name[:year_of_publication])
        end

        # if only one result, everything's ok. Safe to take it as the protonym
        if potential_protonyms_narrowed.count == 1
          potential_protonyms = potential_protonyms_narrowed
        else
          potential_protonym_strings = potential_protonyms.map { |proto| "[id: #{proto.id} #{proto.cached_html_name_and_author_year}]" }.join(', ')
          raise DatasetRecord::DarwinCore::InvalidData.new(
              { "scientificName" => ["Multiple matches found for name #{name[:name]} and author name #{name[:cached_author]}, year #{name[:year_of_publication]}: #{potential_protonym_strings}"] }
            )
        end
      else
        # for intermediate homonyms, skip it, we don't have any info
        return parent
      end
    end

    p = potential_protonyms.first

    # Protonym might not exist, or might have intermediate parent not listed in file
    # if it exists, run more expensive query to see if it has an ancestor matching parent name and rank
    if p.nil? && Protonym.where(name.slice(:rank_class).merge({ field => name[:name] })).where(project_id: parent.project_id).exists?
      potential_protonyms = Protonym.where(name.slice(:rank_class, :cached_author, :year_of_publication).merge({ field => name[:name] }).compact).with_ancestor(parent)
      if potential_protonyms.count > 1
        return parent
        # potential_protonym_strings = potential_protonyms.map { |proto| "[id: #{proto.id} #{proto.cached_html_name_and_author_year}]" }
        # raise DatasetRecord::DarwinCore::InvalidData.new(
        #   { "scientificName" => ["Intermediate name not present, and multiple matches found: #{potential_protonym_strings.join(', ')}"] }
        # )
      end
      p = potential_protonyms.first
      # check parent.cached_valid_taxon_name_id if not valid, can have obsolete subgenus Aus (Aus) bus -> Aus bus, bus won't have ancestor (Aus)
      if p.nil? && !parent.cached_is_valid
        p = Protonym.where(name.slice(:rank_class).merge!({ field => name[:name] })).with_ancestor(parent.valid_taxon_name).first
      end

    end
    p
  end
end